BrowseFunc.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. unit BrowseFunc;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2024 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. {$I VERSION.INC}
  11. uses
  12. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms;
  13. function BrowseForFolder(const Prompt: String; var Directory: String;
  14. const ParentWnd: HWND; const NewFolderButton: Boolean): Boolean;
  15. function NewGetOpenFileName(const Prompt: String; var FileName: String;
  16. const InitialDirectory, Filter, DefaultExtension: String;
  17. const ParentWnd: HWND): Boolean;
  18. function NewGetOpenFileNameMulti(const Prompt: String; const FileNameList: TStrings;
  19. const InitialDirectory, Filter, DefaultExtension: String;
  20. const ParentWnd: HWND): Boolean;
  21. function NewGetSaveFileName(const Prompt: String; var FileName: String;
  22. const InitialDirectory, Filter, DefaultExtension: String;
  23. const ParentWnd: HWND): Boolean;
  24. implementation
  25. uses
  26. CommDlg, ShlObj, ActiveX,
  27. PathFunc;
  28. function BrowseCallback(Wnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
  29. var
  30. ShouldEnable: Boolean;
  31. Path: array[0..MAX_PATH-1] of Char;
  32. begin
  33. case uMsg of
  34. BFFM_INITIALIZED:
  35. begin
  36. if lpData <> 0 then
  37. SendMessage(Wnd, BFFM_SETSELECTION, 1, lpData);
  38. end;
  39. BFFM_SELCHANGED:
  40. begin
  41. { In a BIF_NEWDIALOGSTYLE dialog, BIF_RETURNONLYFSDIRS does not cause
  42. the OK button to be disabled automatically when the user clicks a
  43. non-FS item (e.g. My Computer), so do that ourself. }
  44. ShouldEnable := SHGetPathFromIDList(PItemIDList(lParam), Path);
  45. SendMessage(Wnd, BFFM_ENABLEOK, 0, Ord(ShouldEnable));
  46. end;
  47. end;
  48. Result := 0;
  49. end;
  50. function BrowseForFolder(const Prompt: String; var Directory: String;
  51. const ParentWnd: HWND; const NewFolderButton: Boolean): Boolean;
  52. const
  53. BIF_NONEWFOLDERBUTTON = $200;
  54. BIF_NEWDIALOGSTYLE = $0040;
  55. var
  56. InitialDir: String;
  57. Malloc: IMalloc;
  58. BrowseInfo: TBrowseInfo;
  59. DisplayName, Path: array[0..MAX_PATH-1] of Char;
  60. ActiveWindow: HWND;
  61. WindowList: Pointer;
  62. IDList: PItemIDList;
  63. begin
  64. Result := False;
  65. InitialDir := RemoveBackslashUnlessRoot(Directory); { Win95 doesn't allow trailing backslash }
  66. if FAILED(SHGetMalloc(Malloc)) then
  67. Malloc := nil;
  68. FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
  69. with BrowseInfo do begin
  70. hwndOwner := ParentWnd;
  71. pszDisplayName := @DisplayName;
  72. lpszTitle := PChar(Prompt);
  73. ulFlags := BIF_RETURNONLYFSDIRS or BIF_NEWDIALOGSTYLE;
  74. if not NewFolderButton then
  75. ulFlags := ulFlags or BIF_NONEWFOLDERBUTTON;
  76. lpfn := BrowseCallback;
  77. if InitialDir <> '' then
  78. Pointer(lParam) := PChar(InitialDir);
  79. end;
  80. ActiveWindow := GetActiveWindow;
  81. WindowList := DisableTaskWindows(0);
  82. CoInitialize(nil);
  83. try
  84. IDList := SHBrowseForFolder(BrowseInfo);
  85. finally
  86. CoUninitialize();
  87. EnableTaskWindows(WindowList);
  88. { SetActiveWindow(Application.Handle) is needed or else the focus doesn't
  89. properly return to ActiveWindow }
  90. SetActiveWindow(Application.Handle);
  91. SetActiveWindow(ActiveWindow);
  92. end;
  93. try
  94. if (IDList = nil) or not SHGetPathFromIDList(IDList, Path) then
  95. Exit;
  96. Directory := Path;
  97. finally
  98. if Assigned(Malloc) then
  99. Malloc.Free(IDList);
  100. end;
  101. Result := True;
  102. end;
  103. type
  104. TGetOpenOrSaveFileNameFunc = function(var OpenFile: TOpenFilename): Bool; stdcall;
  105. function NewGetOpenOrSaveFileName(const Prompt: String; var FileName: String;
  106. const InitialDirectory, Filter, DefaultExtension: String;
  107. const ParentWnd: HWND; const GetOpenOrSaveFileNameFunc: TGetOpenOrSaveFileNameFunc;
  108. const Flags: DWORD): Boolean;
  109. function AllocFilterStr(const S: string): string;
  110. var
  111. P: PChar;
  112. begin
  113. Result := '';
  114. if S <> '' then
  115. begin
  116. Result := S + #0; // double null terminators
  117. P := PathStrScan(PChar(Result), '|');
  118. while P <> nil do
  119. begin
  120. P^ := #0;
  121. Inc(P);
  122. P := PathStrScan(P, '|');
  123. end;
  124. end;
  125. end;
  126. function GetMultiSelectString(const P: PChar): String;
  127. var
  128. E: PChar;
  129. begin
  130. E := P;
  131. while E^ <> #0 do
  132. Inc(E, StrLen(E) + 1);
  133. SetString(Result, P, E - P);
  134. end;
  135. var
  136. ofn: TOpenFileName;
  137. lpstrFile: array[0..8191] of Char;
  138. TempFilter: String;
  139. SaveCurDir: String;
  140. ActiveWindow: HWND;
  141. WindowList: Pointer;
  142. FPUControlWord: Word;
  143. begin
  144. StrPLCopy(lpstrFile, FileName, (SizeOf(lpstrFile) div SizeOf(lpstrFile[0])) - 1);
  145. FillChar(ofn, SizeOf(ofn), 0);
  146. ofn.lStructSize := SizeOf(ofn);
  147. ofn.hwndOwner := ParentWnd;
  148. TempFilter := AllocFilterStr(Filter);
  149. ofn.lpstrFilter := PChar(TempFilter);
  150. ofn.lpstrFile := lpstrFile;
  151. ofn.nMaxFile := SizeOf(lpstrFile) div SizeOf(lpstrFile[0]);
  152. ofn.lpstrInitialDir := PChar(InitialDirectory);
  153. ofn.lpstrTitle := PChar(Prompt);
  154. ofn.Flags := Flags or OFN_NOCHANGEDIR;
  155. ofn.lpstrDefExt := Pointer(DefaultExtension);
  156. ActiveWindow := GetActiveWindow;
  157. WindowList := DisableTaskWindows(0);
  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. EnableTaskWindows(WindowList);
  186. { SetActiveWindow(Application.Handle) is needed or else the focus doesn't
  187. properly return to ActiveWindow }
  188. SetActiveWindow(Application.Handle);
  189. SetActiveWindow(ActiveWindow);
  190. end;
  191. end;
  192. function NewGetOpenFileName(const Prompt: String; var FileName: String;
  193. const InitialDirectory, Filter, DefaultExtension: String;
  194. const ParentWnd: HWND): Boolean;
  195. begin
  196. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  197. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST);
  198. end;
  199. function NewGetOpenFileNameMulti(const Prompt: String; const FileNameList: TStrings;
  200. const InitialDirectory, Filter, DefaultExtension: String;
  201. const ParentWnd: HWND): Boolean;
  202. function ExtractStr(var P: PChar): String;
  203. var
  204. L: Integer;
  205. begin
  206. L := StrLen(P);
  207. SetString(Result, P, L);
  208. if L > 0 then
  209. Inc(P, L + 1);
  210. end;
  211. var
  212. Files, Dir, FileName: String;
  213. P: PChar;
  214. begin
  215. Result := NewGetOpenOrSaveFileName(Prompt, Files, InitialDirectory, Filter, DefaultExtension,
  216. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST or
  217. OFN_ALLOWMULTISELECT or OFN_EXPLORER);
  218. if Result then begin
  219. FileNameList.Clear;
  220. P := PChar(Files);
  221. Dir := ExtractStr(P);
  222. FileName := ExtractStr(P);
  223. if FileName = '' then begin
  224. { When only one file is selected, the list contains just a file name }
  225. FileNameList.Add(Dir);
  226. end
  227. else begin
  228. repeat
  229. { The filenames can include paths if the user typed them in manually }
  230. FileNameList.Add(PathCombine(Dir, FileName));
  231. FileName := ExtractStr(P);
  232. until FileName = '';
  233. end;
  234. end;
  235. end;
  236. function NewGetSaveFileName(const Prompt: String; var FileName: String;
  237. const InitialDirectory, Filter, DefaultExtension: String;
  238. const ParentWnd: HWND): Boolean;
  239. begin
  240. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  241. ParentWnd, GetSaveFileName, OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY or OFN_PATHMUSTEXIST);
  242. end;
  243. end.