BrowseFunc.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. 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,
  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. try
  155. asm
  156. // Avoid FPU control word change in NETRAP.dll, NETAPI32.dll, etc
  157. FNSTCW FPUControlWord
  158. end;
  159. try
  160. SaveCurDir := GetCurrentDir;
  161. if GetOpenOrSaveFileNameFunc(ofn) then begin
  162. if Flags and OFN_ALLOWMULTISELECT <> 0 then
  163. FileName := GetMultiSelectString(lpstrFile)
  164. else
  165. FileName := lpstrFile;
  166. Result := True;
  167. end else
  168. Result := False;
  169. { Restore current directory. The OFN_NOCHANGEDIR flag we pass is
  170. supposed to do that, but the MSDN docs claim: "This flag is
  171. ineffective for GetOpenFileName." I see no such problem on
  172. Windows 2000 or XP, but to be safe... }
  173. SetCurrentDir(SaveCurDir);
  174. finally
  175. asm
  176. FNCLEX
  177. FLDCW FPUControlWord
  178. end;
  179. end;
  180. finally
  181. EnableTaskWindows(WindowList);
  182. SetActiveWindow(ActiveWindow);
  183. end;
  184. end;
  185. function NewGetOpenFileName(const Prompt: String; var FileName: String;
  186. const InitialDirectory, Filter, DefaultExtension: String;
  187. const ParentWnd: HWND): Boolean;
  188. begin
  189. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  190. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST);
  191. end;
  192. function NewGetOpenFileNameMulti(const Prompt: String; const FileNameList: TStrings;
  193. const InitialDirectory, Filter, DefaultExtension: String;
  194. const ParentWnd: HWND): Boolean;
  195. function ExtractStr(var P: PChar): String;
  196. var
  197. L: Integer;
  198. begin
  199. L := StrLen(P);
  200. SetString(Result, P, L);
  201. if L > 0 then
  202. Inc(P, L + 1);
  203. end;
  204. var
  205. Files, Dir, FileName: String;
  206. P: PChar;
  207. begin
  208. Result := NewGetOpenOrSaveFileName(Prompt, Files, InitialDirectory, Filter, DefaultExtension,
  209. ParentWnd, GetOpenFileName, OFN_HIDEREADONLY or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST or
  210. OFN_ALLOWMULTISELECT or OFN_EXPLORER);
  211. if Result then begin
  212. FileNameList.Clear;
  213. P := PChar(Files);
  214. Dir := ExtractStr(P);
  215. FileName := ExtractStr(P);
  216. if FileName = '' then begin
  217. { When only one file is selected, the list contains just a file name }
  218. FileNameList.Add(Dir);
  219. end
  220. else begin
  221. repeat
  222. { The filenames can include paths if the user typed them in manually }
  223. FileNameList.Add(PathCombine(Dir, FileName));
  224. FileName := ExtractStr(P);
  225. until FileName = '';
  226. end;
  227. end;
  228. end;
  229. function NewGetSaveFileName(const Prompt: String; var FileName: String;
  230. const InitialDirectory, Filter, DefaultExtension: String;
  231. const ParentWnd: HWND): Boolean;
  232. begin
  233. Result := NewGetOpenOrSaveFileName(Prompt, FileName, InitialDirectory, Filter, DefaultExtension,
  234. ParentWnd, GetSaveFileName, OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY or OFN_PATHMUSTEXIST);
  235. end;
  236. end.