BrowseFunc.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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, Classes, 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,
  26. SysUtils, Themes,
  27. PathFunc;
  28. function BrowseForFolder(const Prompt: String; var Directory: String;
  29. const ParentWnd: HWND; const NewFolderButton: Boolean): Boolean;
  30. function RemoveSinglePeriod(const S: String): String;
  31. begin
  32. Result := S;
  33. const FirstPeriodPos = Pos('.', S);
  34. if FirstPeriodPos <> 0 then begin
  35. const LastPeriodPos = LastDelimiter('.', S);
  36. if (FirstPeriodPos = LastPeriodPos) and (LastPeriodPos = Length(S)) then
  37. Delete(Result, LastPeriodPos, 1);
  38. end;
  39. end;
  40. var
  41. InitialDir: String;
  42. FileDialog: IFileDialog;
  43. Options: DWORD;
  44. ActiveWindow: HWND;
  45. WindowList: Pointer;
  46. ShellItem: IShellItem;
  47. ResultPath: PChar;
  48. begin
  49. Result := False;
  50. InitialDir := RemoveBackslashUnlessRoot(Directory); { Win95 doesn't allow trailing backslash }
  51. if Failed(CoInitialize(nil)) then
  52. Exit;
  53. ActiveWindow := GetActiveWindow;
  54. WindowList := DisableTaskWindows(ParentWnd);
  55. const SaveHooks = TStyleManager.SystemHooks;
  56. TStyleManager.SystemHooks := []; { See below }
  57. try
  58. if Failed(CoCreateInstance(CLSID_FileOpenDialog, nil, CLSCTX_INPROC_SERVER, IID_IFileDialog, FileDialog)) or
  59. Failed(FileDialog.GetOptions(Options)) then { On Windows 11 this returned FOS_NOCHANGEDIR or FOS_PATHMUSTEXIST or FOS_FILEMUSTEXIST }
  60. Exit;
  61. Options := Options or FOS_PICKFOLDERS or FOS_FORCEFILESYSTEM;
  62. if not NewFolderButton then begin
  63. const FOS_NOCREATEFOLDERS: DWORD = $00000200;
  64. Options := Options or FOS_NOCREATEFOLDERS;
  65. end;
  66. if Failed(FileDialog.SetOptions(Options)) then
  67. Exit;
  68. if Prompt <> '' then
  69. FileDialog.SetTitle(PChar(RemoveSinglePeriod(Prompt)));
  70. if (InitialDir <> '') and Succeeded(SHCreateItemFromParsingName(PChar(InitialDir), nil, IID_IShellItem, ShellItem)) then
  71. FileDialog.SetFolder(ShellItem);
  72. if Failed(FileDialog.Show(ParentWnd)) or
  73. Failed(FileDialog.GetResult(ShellItem)) or
  74. Failed(ShellItem.GetDisplayName(SIGDN_FILESYSPATH, ResultPath)) then
  75. Exit;
  76. try
  77. Directory := ResultPath;
  78. finally
  79. CoTaskMemFree(ResultPath);
  80. end;
  81. finally
  82. TStyleManager.SystemHooks := SaveHooks;
  83. EnableTaskWindows(WindowList);
  84. SetActiveWindow(ActiveWindow);
  85. CoUninitialize();
  86. end;
  87. Result := True;
  88. end;
  89. type
  90. TGetOpenOrSaveFileNameFunc = function(var OpenFile: TOpenFilename): Bool; stdcall;
  91. function NewGetOpenOrSaveFileName(const Prompt: String; var FileName: String;
  92. const InitialDirectory, Filter, DefaultExtension: String;
  93. const ParentWnd: HWND; const GetOpenOrSaveFileNameFunc: TGetOpenOrSaveFileNameFunc;
  94. const Flags: DWORD): Boolean;
  95. function AllocFilterStr(const S: string): string;
  96. var
  97. P: PChar;
  98. begin
  99. Result := '';
  100. if S <> '' then
  101. begin
  102. Result := S + #0; // double null terminators
  103. P := PathStrScan(PChar(Result), '|');
  104. while P <> nil do
  105. begin
  106. P^ := #0;
  107. Inc(P);
  108. P := PathStrScan(P, '|');
  109. end;
  110. end;
  111. end;
  112. function GetMultiSelectString(const P: PChar): String;
  113. var
  114. E: PChar;
  115. begin
  116. E := P;
  117. while E^ <> #0 do
  118. Inc(E, StrLen(E) + 1);
  119. SetString(Result, P, E - P);
  120. end;
  121. var
  122. ofn: TOpenFileName;
  123. lpstrFile: array[0..8191] of Char;
  124. TempFilter: String;
  125. SaveCurDir: String;
  126. ActiveWindow: HWND;
  127. WindowList: Pointer;
  128. begin
  129. StrPLCopy(lpstrFile, FileName, (SizeOf(lpstrFile) div SizeOf(lpstrFile[0])) - 1);
  130. FillChar(ofn, SizeOf(ofn), 0);
  131. ofn.lStructSize := SizeOf(ofn);
  132. ofn.hwndOwner := ParentWnd;
  133. TempFilter := AllocFilterStr(Filter);
  134. ofn.lpstrFilter := PChar(TempFilter);
  135. ofn.lpstrFile := lpstrFile;
  136. ofn.nMaxFile := SizeOf(lpstrFile) div SizeOf(lpstrFile[0]);
  137. ofn.lpstrInitialDir := PChar(InitialDirectory);
  138. ofn.lpstrTitle := PChar(Prompt);
  139. ofn.Flags := Flags or OFN_NOCHANGEDIR;
  140. ofn.lpstrDefExt := Pointer(DefaultExtension);
  141. ActiveWindow := GetActiveWindow;
  142. WindowList := DisableTaskWindows(ParentWnd);
  143. { Temporarily clear SystemHooks to make it support dark mode if Windows is in dark mode.
  144. Taken from Vcl.Dialogs' TCustomFileDialog.Execute. }
  145. const SaveHooks = TStyleManager.SystemHooks;
  146. TStyleManager.SystemHooks := [];
  147. try
  148. {$IFDEF CPUX86}
  149. var FPUControlWord: Word;
  150. asm
  151. // Avoid FPU control word change in NETRAP.dll, NETAPI32.dll, etc
  152. FNSTCW FPUControlWord
  153. end;
  154. {$ENDIF}
  155. try
  156. SaveCurDir := GetCurrentDir;
  157. if GetOpenOrSaveFileNameFunc(ofn) then begin
  158. if Flags and OFN_ALLOWMULTISELECT <> 0 then
  159. FileName := GetMultiSelectString(lpstrFile)
  160. else
  161. FileName := lpstrFile;
  162. Result := True;
  163. end else
  164. Result := False;
  165. { Restore current directory. The OFN_NOCHANGEDIR flag we pass is
  166. supposed to do that, but the MSDN docs claim: "This flag is
  167. ineffective for GetOpenFileName." I see no such problem on
  168. Windows 2000 or XP, but to be safe... }
  169. SetCurrentDir(SaveCurDir);
  170. finally
  171. {$IFDEF CPUX86}
  172. asm
  173. FNCLEX
  174. FLDCW FPUControlWord
  175. end;
  176. {$ENDIF}
  177. end;
  178. finally
  179. TStyleManager.SystemHooks := SaveHooks;
  180. EnableTaskWindows(WindowList);
  181. SetActiveWindow(ActiveWindow);
  182. end;
  183. end;
  184. function NewGetOpenFileName(const Prompt: String; var FileName: String;
  185. const InitialDirectory, Filter, DefaultExtension: String;
  186. const ParentWnd: HWND): Boolean;
  187. begin
  188. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  189. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST);
  190. end;
  191. function NewGetOpenFileNameMulti(const Prompt: String; const FileNameList: TStrings;
  192. const InitialDirectory, Filter, DefaultExtension: String;
  193. const ParentWnd: HWND): Boolean;
  194. function ExtractStr(var P: PChar): String;
  195. begin
  196. const L = Integer(StrLen(P));
  197. SetString(Result, P, L);
  198. if L > 0 then
  199. Inc(P, L + 1);
  200. end;
  201. var
  202. Files, Dir, FileName: String;
  203. P: PChar;
  204. begin
  205. Result := NewGetOpenOrSaveFileName(Prompt, Files, InitialDirectory, Filter, DefaultExtension,
  206. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST or
  207. OFN_ALLOWMULTISELECT or OFN_EXPLORER);
  208. if Result then begin
  209. FileNameList.Clear;
  210. P := PChar(Files);
  211. Dir := ExtractStr(P);
  212. FileName := ExtractStr(P);
  213. if FileName = '' then begin
  214. { When only one file is selected, the list contains just a file name }
  215. FileNameList.Add(Dir);
  216. end
  217. else begin
  218. repeat
  219. { The filenames can include paths if the user typed them in manually }
  220. FileNameList.Add(PathCombine(Dir, FileName));
  221. FileName := ExtractStr(P);
  222. until FileName = '';
  223. end;
  224. end;
  225. end;
  226. function NewGetSaveFileName(const Prompt: String; var FileName: String;
  227. const InitialDirectory, Filter, DefaultExtension: String;
  228. const ParentWnd: HWND): Boolean;
  229. begin
  230. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  231. ParentWnd, GetSaveFileName, OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY or OFN_PATHMUSTEXIST);
  232. end;
  233. end.