BrowseFunc.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. FPUControlWord: Word;
  129. begin
  130. StrPLCopy(lpstrFile, FileName, (SizeOf(lpstrFile) div SizeOf(lpstrFile[0])) - 1);
  131. FillChar(ofn, SizeOf(ofn), 0);
  132. ofn.lStructSize := SizeOf(ofn);
  133. ofn.hwndOwner := ParentWnd;
  134. TempFilter := AllocFilterStr(Filter);
  135. ofn.lpstrFilter := PChar(TempFilter);
  136. ofn.lpstrFile := lpstrFile;
  137. ofn.nMaxFile := SizeOf(lpstrFile) div SizeOf(lpstrFile[0]);
  138. ofn.lpstrInitialDir := PChar(InitialDirectory);
  139. ofn.lpstrTitle := PChar(Prompt);
  140. ofn.Flags := Flags or OFN_NOCHANGEDIR;
  141. ofn.lpstrDefExt := Pointer(DefaultExtension);
  142. ActiveWindow := GetActiveWindow;
  143. WindowList := DisableTaskWindows(ParentWnd);
  144. { Temporarily clear SystemHooks to make it support dark mode if Windows is in dark mode.
  145. Taken from Vcl.Dialogs' TCustomFileDialog.Execute. }
  146. const SaveHooks = TStyleManager.SystemHooks;
  147. TStyleManager.SystemHooks := [];
  148. try
  149. asm
  150. // Avoid FPU control word change in NETRAP.dll, NETAPI32.dll, etc
  151. FNSTCW FPUControlWord
  152. end;
  153. try
  154. SaveCurDir := GetCurrentDir;
  155. if GetOpenOrSaveFileNameFunc(ofn) then begin
  156. if Flags and OFN_ALLOWMULTISELECT <> 0 then
  157. FileName := GetMultiSelectString(lpstrFile)
  158. else
  159. FileName := lpstrFile;
  160. Result := True;
  161. end else
  162. Result := False;
  163. { Restore current directory. The OFN_NOCHANGEDIR flag we pass is
  164. supposed to do that, but the MSDN docs claim: "This flag is
  165. ineffective for GetOpenFileName." I see no such problem on
  166. Windows 2000 or XP, but to be safe... }
  167. SetCurrentDir(SaveCurDir);
  168. finally
  169. asm
  170. FNCLEX
  171. FLDCW FPUControlWord
  172. end;
  173. end;
  174. finally
  175. TStyleManager.SystemHooks := SaveHooks;
  176. EnableTaskWindows(WindowList);
  177. SetActiveWindow(ActiveWindow);
  178. end;
  179. end;
  180. function NewGetOpenFileName(const Prompt: String; var FileName: String;
  181. const InitialDirectory, Filter, DefaultExtension: String;
  182. const ParentWnd: HWND): Boolean;
  183. begin
  184. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  185. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST);
  186. end;
  187. function NewGetOpenFileNameMulti(const Prompt: String; const FileNameList: TStrings;
  188. const InitialDirectory, Filter, DefaultExtension: String;
  189. const ParentWnd: HWND): Boolean;
  190. function ExtractStr(var P: PChar): String;
  191. var
  192. L: Integer;
  193. begin
  194. L := StrLen(P);
  195. SetString(Result, P, L);
  196. if L > 0 then
  197. Inc(P, L + 1);
  198. end;
  199. var
  200. Files, Dir, FileName: String;
  201. P: PChar;
  202. begin
  203. Result := NewGetOpenOrSaveFileName(Prompt, Files, InitialDirectory, Filter, DefaultExtension,
  204. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST or
  205. OFN_ALLOWMULTISELECT or OFN_EXPLORER);
  206. if Result then begin
  207. FileNameList.Clear;
  208. P := PChar(Files);
  209. Dir := ExtractStr(P);
  210. FileName := ExtractStr(P);
  211. if FileName = '' then begin
  212. { When only one file is selected, the list contains just a file name }
  213. FileNameList.Add(Dir);
  214. end
  215. else begin
  216. repeat
  217. { The filenames can include paths if the user typed them in manually }
  218. FileNameList.Add(PathCombine(Dir, FileName));
  219. FileName := ExtractStr(P);
  220. until FileName = '';
  221. end;
  222. end;
  223. end;
  224. function NewGetSaveFileName(const Prompt: String; var FileName: String;
  225. const InitialDirectory, Filter, DefaultExtension: String;
  226. const ParentWnd: HWND): Boolean;
  227. begin
  228. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  229. ParentWnd, GetSaveFileName, OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY or OFN_PATHMUSTEXIST);
  230. end;
  231. end.