1
votes

In Delphi 2007 and later, the global variable UseLatestCommonDialogs causes TOpenDialog to use the new Vista-style dialog on Windows Vista and 7. That's cool.

Unfortunately, the Vista-style dialog does not seem to support the ofHideReadOnly option of TOpenDialog. The read-only checkbox never appears regardless whether this option is set to True or False.

How can I make TOpenDialog show a read-only checkbox on the Vista-style dialog?

Since this breaks backwards compatibility, I've reported this as a bug: QC 83606 A Delphi 2006 application that had ofHideReadOnly set to False will lose its read only checkbox when compiled without changes with Delphi 2007, 2009, or 2010 and run on Windows Vista or 7.

1
Related question: stackoverflow.com/questions/1076827/…. Doing anything that the VCL doesn't support OOTB requires using the IFileDialogCustomize interface on the new style dialogs.mghie

1 Answers

1
votes

I've managed to implement this in Delphi 2010 by modifying Dialogs.pas as follows:

First, declare and implement the class TFileOpenDialogWrapperReadOnlyEvent:

{ TFileOpenDialogWrapperReadOnlyEvent }

type
  TFileOpenDialogWrapperReadOnlyEvent = class(TInterfacedObject, IFileDialogEvents, IFileDialogControlEvents)
  private
    FOpenDialog: TOpenDialog;
  public
    function OnButtonClicked(const pfdc: IFileDialogCustomize; dwIDCtl: Cardinal): HRESULT; stdcall;
    function OnCheckButtonToggled(const pfdc: IFileDialogCustomize; dwIDCtl: Cardinal; bChecked: LongBool): HRESULT; stdcall;
    function OnControlActivating(const pfdc: IFileDialogCustomize; dwIDCtl: Cardinal): HRESULT; stdcall;
    function OnItemSelected(const pfdc: IFileDialogCustomize; dwIDCtl: Cardinal; dwIDItem: Cardinal): HRESULT; stdcall;
    function OnFileOk(const pfd: IFileDialog): HRESULT; stdcall;
    function OnFolderChange(const pfd: IFileDialog): HRESULT; stdcall;
    function OnFolderChanging(const pfd: IFileDialog; const psiFolder: IShellItem): HRESULT; stdcall;
    function OnOverwrite(const pfd: IFileDialog; const psi: IShellItem; out pResponse: Cardinal): HRESULT; stdcall;
    function OnSelectionChange(const pfd: IFileDialog): HRESULT; stdcall;
    function OnShareViolation(const pfd: IFileDialog; const psi: IShellItem; out pResponse: Cardinal): HRESULT; stdcall;
    function OnTypeChange(const pfd: IFileDialog): HRESULT; stdcall;
  end;

function TFileOpenDialogWrapperReadOnlyEvent.OnButtonClicked(const pfdc: IFileDialogCustomize; dwIDCtl: Cardinal): HRESULT;
begin
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnCheckButtonToggled(const pfdc: IFileDialogCustomize; dwIDCtl: Cardinal; bChecked: LongBool): HRESULT;
begin
  if bChecked then FOpenDialog.Options := FOpenDialog.Options + [ofReadOnly]
    else FOpenDialog.Options := FOpenDialog.Options - [ofReadOnly];
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnControlActivating(const pfdc: IFileDialogCustomize; dwIDCtl: Cardinal): HRESULT;
begin
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnFileOk(const pfd: IFileDialog): HRESULT;
begin
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnFolderChange(const pfd: IFileDialog): HRESULT;
begin
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnFolderChanging(const pfd: IFileDialog; const psiFolder: IShellItem): HRESULT;
begin
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnItemSelected(const pfdc: IFileDialogCustomize; dwIDCtl, dwIDItem: Cardinal): HRESULT;
begin
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnOverwrite(const pfd: IFileDialog; const psi: IShellItem; out pResponse: Cardinal): HRESULT;
begin
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnSelectionChange(const pfd: IFileDialog): HRESULT;
begin
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnShareViolation(const pfd: IFileDialog; const psi: IShellItem; out pResponse: Cardinal): HRESULT;
begin
  Result := S_OK
end;

function TFileOpenDialogWrapperReadOnlyEvent.OnTypeChange(const pfd: IFileDialog): HRESULT;
begin
  Result := S_OK
end;

Declare this string as a resource so it can be localized (if you care about that):

resourcestring
  SReadOnly = 'Read only';

Then modify TFileOpenDialogWrapper.OnExecuteEvent in two steps. First, add these variable declarations:

C: IFileDialogCustomize;
E: TFileOpenDialogWrapperReadOnlyEvent;
Cookie: Cardinal;

Then add this code at the end of the procedure:

if not (ofHideReadOnly in FOpenDialog.Options) then begin
  if FFileDialog.Dialog.QueryInterface(IFileDialogCustomize, C) = S_OK then begin
    C.AddCheckButton(1, PChar(SReadOnly), ofReadOnly in FOpenDialog.Options);
    E := TFileOpenDialogWrapperReadOnlyEvent.Create;
    E.FOpenDialog := FOpenDialog;
    FFileDialog.Dialog.Advise(E, Cookie);
  end;
end;

Copy the new Dialogs.pas to your source code folder and delete or rename the two Dialogs.dcu files in Delphi's Lib folder. Recompile your app an ofHideReadOnly will work with Vista-style dialogs.