BrowseFunc.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. unit BrowseFunc;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2025 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Functions for browsing for folders/files
  8. }
  9. interface
  10. uses
  11. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms;
  12. function BrowseForFolder(const Prompt: String; var Directory: String;
  13. const ParentWnd: HWND; const NewFolderButton: Boolean): Boolean;
  14. function NewGetOpenFileName(const Prompt: String; var FileName: String;
  15. const InitialDirectory, Filter, DefaultExtension: String;
  16. const ParentWnd: HWND): Boolean;
  17. function NewGetOpenFileNameMulti(const Prompt: String; const FileNameList: TStrings;
  18. const InitialDirectory, Filter, DefaultExtension: String;
  19. const ParentWnd: HWND): Boolean;
  20. function NewGetSaveFileName(const Prompt: String; var FileName: String;
  21. const InitialDirectory, Filter, DefaultExtension: String;
  22. const ParentWnd: HWND): Boolean;
  23. implementation
  24. uses
  25. CommDlg, ShlObj, ActiveX, Themes,
  26. PathFunc;
  27. function BrowseCallback(Wnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
  28. var
  29. ShouldEnable: Boolean;
  30. Path: array[0..MAX_PATH-1] of Char;
  31. begin
  32. case uMsg of
  33. BFFM_INITIALIZED:
  34. begin
  35. if lpData <> 0 then
  36. SendMessage(Wnd, BFFM_SETSELECTION, 1, lpData);
  37. end;
  38. BFFM_SELCHANGED:
  39. begin
  40. { In a BIF_NEWDIALOGSTYLE dialog, BIF_RETURNONLYFSDIRS does not cause
  41. the OK button to be disabled automatically when the user clicks a
  42. non-FS item (e.g. My Computer), so do that ourself. }
  43. ShouldEnable := SHGetPathFromIDList(PItemIDList(lParam), Path);
  44. SendMessage(Wnd, BFFM_ENABLEOK, 0, Ord(ShouldEnable));
  45. end;
  46. end;
  47. Result := 0;
  48. end;
  49. function BrowseForFolder(const Prompt: String; var Directory: String;
  50. const ParentWnd: HWND; const NewFolderButton: Boolean): Boolean;
  51. const
  52. BIF_NONEWFOLDERBUTTON = $200;
  53. BIF_NEWDIALOGSTYLE = $0040;
  54. var
  55. InitialDir: String;
  56. Malloc: IMalloc;
  57. BrowseInfo: TBrowseInfo;
  58. DisplayName, Path: array[0..MAX_PATH-1] of Char;
  59. ActiveWindow: HWND;
  60. WindowList: Pointer;
  61. IDList: PItemIDList;
  62. begin
  63. Result := False;
  64. InitialDir := RemoveBackslashUnlessRoot(Directory); { Win95 doesn't allow trailing backslash }
  65. if FAILED(SHGetMalloc(Malloc)) then
  66. Malloc := nil;
  67. FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
  68. with BrowseInfo do begin
  69. hwndOwner := ParentWnd;
  70. pszDisplayName := @DisplayName;
  71. lpszTitle := PChar(Prompt);
  72. ulFlags := BIF_RETURNONLYFSDIRS or BIF_NEWDIALOGSTYLE;
  73. if not NewFolderButton then
  74. ulFlags := ulFlags or BIF_NONEWFOLDERBUTTON;
  75. lpfn := BrowseCallback;
  76. if InitialDir <> '' then
  77. Pointer(lParam) := PChar(InitialDir);
  78. end;
  79. ActiveWindow := GetActiveWindow;
  80. WindowList := DisableTaskWindows(ParentWnd);
  81. CoInitialize(nil);
  82. try
  83. IDList := SHBrowseForFolder(BrowseInfo);
  84. finally
  85. CoUninitialize();
  86. EnableTaskWindows(WindowList);
  87. SetActiveWindow(ActiveWindow);
  88. end;
  89. try
  90. if (IDList = nil) or not SHGetPathFromIDList(IDList, Path) then
  91. Exit;
  92. Directory := Path;
  93. finally
  94. if Assigned(Malloc) then
  95. Malloc.Free(IDList);
  96. end;
  97. Result := True;
  98. end;
  99. type
  100. TGetOpenOrSaveFileNameFunc = function(var OpenFile: TOpenFilename): Bool; stdcall;
  101. function NewGetOpenOrSaveFileName(const Prompt: String; var FileName: String;
  102. const InitialDirectory, Filter, DefaultExtension: String;
  103. const ParentWnd: HWND; const GetOpenOrSaveFileNameFunc: TGetOpenOrSaveFileNameFunc;
  104. const Flags: DWORD): Boolean;
  105. function AllocFilterStr(const S: string): string;
  106. var
  107. P: PChar;
  108. begin
  109. Result := '';
  110. if S <> '' then
  111. begin
  112. Result := S + #0; // double null terminators
  113. P := PathStrScan(PChar(Result), '|');
  114. while P <> nil do
  115. begin
  116. P^ := #0;
  117. Inc(P);
  118. P := PathStrScan(P, '|');
  119. end;
  120. end;
  121. end;
  122. function GetMultiSelectString(const P: PChar): String;
  123. var
  124. E: PChar;
  125. begin
  126. E := P;
  127. while E^ <> #0 do
  128. Inc(E, StrLen(E) + 1);
  129. SetString(Result, P, E - P);
  130. end;
  131. var
  132. ofn: TOpenFileName;
  133. lpstrFile: array[0..8191] of Char;
  134. TempFilter: String;
  135. SaveCurDir: String;
  136. ActiveWindow: HWND;
  137. WindowList: Pointer;
  138. FPUControlWord: Word;
  139. begin
  140. StrPLCopy(lpstrFile, FileName, (SizeOf(lpstrFile) div SizeOf(lpstrFile[0])) - 1);
  141. FillChar(ofn, SizeOf(ofn), 0);
  142. ofn.lStructSize := SizeOf(ofn);
  143. ofn.hwndOwner := ParentWnd;
  144. TempFilter := AllocFilterStr(Filter);
  145. ofn.lpstrFilter := PChar(TempFilter);
  146. ofn.lpstrFile := lpstrFile;
  147. ofn.nMaxFile := SizeOf(lpstrFile) div SizeOf(lpstrFile[0]);
  148. ofn.lpstrInitialDir := PChar(InitialDirectory);
  149. ofn.lpstrTitle := PChar(Prompt);
  150. ofn.Flags := Flags or OFN_NOCHANGEDIR;
  151. ofn.lpstrDefExt := Pointer(DefaultExtension);
  152. ActiveWindow := GetActiveWindow;
  153. WindowList := DisableTaskWindows(ParentWnd);
  154. { Temporarily clear SystemHooks to make it support dark mode if Windows is in dark mode.
  155. Taken from Vcl.Dialogs' TCustomFileDialog.Execute. }
  156. const SaveHooks = TStyleManager.SystemHooks;
  157. TStyleManager.SystemHooks := [];
  158. try
  159. asm
  160. // Avoid FPU control word change in NETRAP.dll, NETAPI32.dll, etc
  161. FNSTCW FPUControlWord
  162. end;
  163. try
  164. SaveCurDir := GetCurrentDir;
  165. if GetOpenOrSaveFileNameFunc(ofn) then begin
  166. if Flags and OFN_ALLOWMULTISELECT <> 0 then
  167. FileName := GetMultiSelectString(lpstrFile)
  168. else
  169. FileName := lpstrFile;
  170. Result := True;
  171. end else
  172. Result := False;
  173. { Restore current directory. The OFN_NOCHANGEDIR flag we pass is
  174. supposed to do that, but the MSDN docs claim: "This flag is
  175. ineffective for GetOpenFileName." I see no such problem on
  176. Windows 2000 or XP, but to be safe... }
  177. SetCurrentDir(SaveCurDir);
  178. finally
  179. asm
  180. FNCLEX
  181. FLDCW FPUControlWord
  182. end;
  183. end;
  184. finally
  185. TStyleManager.SystemHooks := SaveHooks;
  186. EnableTaskWindows(WindowList);
  187. SetActiveWindow(ActiveWindow);
  188. end;
  189. end;
  190. function NewGetOpenFileName(const Prompt: String; var FileName: String;
  191. const InitialDirectory, Filter, DefaultExtension: String;
  192. const ParentWnd: HWND): Boolean;
  193. begin
  194. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  195. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST);
  196. end;
  197. function NewGetOpenFileNameMulti(const Prompt: String; const FileNameList: TStrings;
  198. const InitialDirectory, Filter, DefaultExtension: String;
  199. const ParentWnd: HWND): Boolean;
  200. function ExtractStr(var P: PChar): String;
  201. var
  202. L: Integer;
  203. begin
  204. L := StrLen(P);
  205. SetString(Result, P, L);
  206. if L > 0 then
  207. Inc(P, L + 1);
  208. end;
  209. var
  210. Files, Dir, FileName: String;
  211. P: PChar;
  212. begin
  213. Result := NewGetOpenOrSaveFileName(Prompt, Files, InitialDirectory, Filter, DefaultExtension,
  214. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST or
  215. OFN_ALLOWMULTISELECT or OFN_EXPLORER);
  216. if Result then begin
  217. FileNameList.Clear;
  218. P := PChar(Files);
  219. Dir := ExtractStr(P);
  220. FileName := ExtractStr(P);
  221. if FileName = '' then begin
  222. { When only one file is selected, the list contains just a file name }
  223. FileNameList.Add(Dir);
  224. end
  225. else begin
  226. repeat
  227. { The filenames can include paths if the user typed them in manually }
  228. FileNameList.Add(PathCombine(Dir, FileName));
  229. FileName := ExtractStr(P);
  230. until FileName = '';
  231. end;
  232. end;
  233. end;
  234. function NewGetSaveFileName(const Prompt: String; var FileName: String;
  235. const InitialDirectory, Filter, DefaultExtension: String;
  236. const ParentWnd: HWND): Boolean;
  237. begin
  238. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  239. ParentWnd, GetSaveFileName, OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY or OFN_PATHMUSTEXIST);
  240. end;
  241. end.