Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_win32_FileChooser.cpp 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. namespace FileChooserHelpers
  18. {
  19. struct FileChooserCallbackInfo
  20. {
  21. String initialPath;
  22. String returnedString; // need this to get non-existent pathnames from the directory chooser
  23. ScopedPointer<Component> customComponent;
  24. };
  25. static int CALLBACK browseCallbackProc (HWND hWnd, UINT msg, LPARAM lParam, LPARAM lpData)
  26. {
  27. FileChooserCallbackInfo* info = (FileChooserCallbackInfo*) lpData;
  28. if (msg == BFFM_INITIALIZED)
  29. SendMessage (hWnd, BFFM_SETSELECTIONW, TRUE, (LPARAM) info->initialPath.toWideCharPointer());
  30. else if (msg == BFFM_VALIDATEFAILEDW)
  31. info->returnedString = (LPCWSTR) lParam;
  32. else if (msg == BFFM_VALIDATEFAILEDA)
  33. info->returnedString = (const char*) lParam;
  34. return 0;
  35. }
  36. static UINT_PTR CALLBACK openCallback (HWND hdlg, UINT uiMsg, WPARAM /*wParam*/, LPARAM lParam)
  37. {
  38. if (uiMsg == WM_INITDIALOG)
  39. {
  40. Component* customComp = ((FileChooserCallbackInfo*) (((OPENFILENAMEW*) lParam)->lCustData))->customComponent;
  41. HWND dialogH = GetParent (hdlg);
  42. jassert (dialogH != 0);
  43. if (dialogH == 0)
  44. dialogH = hdlg;
  45. RECT r, cr;
  46. GetWindowRect (dialogH, &r);
  47. GetClientRect (dialogH, &cr);
  48. SetWindowPos (dialogH, 0,
  49. r.left, r.top,
  50. customComp->getWidth() + jmax (150, (int) (r.right - r.left)),
  51. jmax (150, (int) (r.bottom - r.top)),
  52. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  53. customComp->setBounds (cr.right, cr.top, customComp->getWidth(), cr.bottom - cr.top);
  54. customComp->addToDesktop (0, dialogH);
  55. }
  56. else if (uiMsg == WM_NOTIFY)
  57. {
  58. LPOFNOTIFY ofn = (LPOFNOTIFY) lParam;
  59. if (ofn->hdr.code == CDN_SELCHANGE)
  60. {
  61. FileChooserCallbackInfo* info = (FileChooserCallbackInfo*) ofn->lpOFN->lCustData;
  62. if (FilePreviewComponent* comp = dynamic_cast<FilePreviewComponent*> (info->customComponent->getChildComponent(0)))
  63. {
  64. WCHAR path [MAX_PATH * 2] = { 0 };
  65. CommDlg_OpenSave_GetFilePath (GetParent (hdlg), (LPARAM) &path, MAX_PATH);
  66. comp->selectedFileChanged (File (path));
  67. }
  68. }
  69. }
  70. return 0;
  71. }
  72. class CustomComponentHolder : public Component
  73. {
  74. public:
  75. CustomComponentHolder (Component* const customComp)
  76. {
  77. setVisible (true);
  78. setOpaque (true);
  79. addAndMakeVisible (customComp);
  80. setSize (jlimit (20, 800, customComp->getWidth()), customComp->getHeight());
  81. }
  82. void paint (Graphics& g) override
  83. {
  84. g.fillAll (Colours::lightgrey);
  85. }
  86. void resized() override
  87. {
  88. if (Component* const c = getChildComponent(0))
  89. c->setBounds (getLocalBounds());
  90. }
  91. private:
  92. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomComponentHolder)
  93. };
  94. }
  95. //==============================================================================
  96. bool FileChooser::isPlatformDialogAvailable()
  97. {
  98. return true;
  99. }
  100. void FileChooser::showPlatformDialog (Array<File>& results, const String& title_, const File& currentFileOrDirectory,
  101. const String& filter, bool selectsDirectory, bool /*selectsFiles*/,
  102. bool isSaveDialogue, bool warnAboutOverwritingExistingFiles,
  103. bool selectMultipleFiles, FilePreviewComponent* extraInfoComponent)
  104. {
  105. using namespace FileChooserHelpers;
  106. const String title (title_);
  107. String defaultExtension; // scope of these strings must extend beyond dialog's lifetime.
  108. HeapBlock<WCHAR> files;
  109. const size_t charsAvailableForResult = 32768;
  110. files.calloc (charsAvailableForResult + 1);
  111. int filenameOffset = 0;
  112. FileChooserCallbackInfo info;
  113. // use a modal window as the parent for this dialog box
  114. // to block input from other app windows
  115. Component parentWindow (String::empty);
  116. const Rectangle<int> mainMon (Desktop::getInstance().getDisplays().getMainDisplay().userArea);
  117. parentWindow.setBounds (mainMon.getX() + mainMon.getWidth() / 4,
  118. mainMon.getY() + mainMon.getHeight() / 4,
  119. 0, 0);
  120. parentWindow.setOpaque (true);
  121. parentWindow.setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  122. parentWindow.addToDesktop (0);
  123. if (extraInfoComponent == nullptr)
  124. parentWindow.enterModalState();
  125. if (currentFileOrDirectory.isDirectory())
  126. {
  127. info.initialPath = currentFileOrDirectory.getFullPathName();
  128. }
  129. else
  130. {
  131. currentFileOrDirectory.getFileName().copyToUTF16 (files, charsAvailableForResult * sizeof (WCHAR));
  132. info.initialPath = currentFileOrDirectory.getParentDirectory().getFullPathName();
  133. }
  134. if (selectsDirectory)
  135. {
  136. BROWSEINFO bi = { 0 };
  137. bi.hwndOwner = (HWND) parentWindow.getWindowHandle();
  138. bi.pszDisplayName = files;
  139. bi.lpszTitle = title.toWideCharPointer();
  140. bi.lParam = (LPARAM) &info;
  141. bi.lpfn = browseCallbackProc;
  142. #ifdef BIF_USENEWUI
  143. bi.ulFlags = BIF_USENEWUI | BIF_VALIDATE;
  144. #else
  145. bi.ulFlags = 0x50;
  146. #endif
  147. LPITEMIDLIST list = SHBrowseForFolder (&bi);
  148. if (! SHGetPathFromIDListW (list, files))
  149. {
  150. files[0] = 0;
  151. info.returnedString.clear();
  152. }
  153. LPMALLOC al;
  154. if (list != nullptr && SUCCEEDED (SHGetMalloc (&al)))
  155. al->Free (list);
  156. if (info.returnedString.isNotEmpty())
  157. {
  158. results.add (File (String (files)).getSiblingFile (info.returnedString));
  159. return;
  160. }
  161. }
  162. else
  163. {
  164. DWORD flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
  165. if (warnAboutOverwritingExistingFiles)
  166. flags |= OFN_OVERWRITEPROMPT;
  167. if (selectMultipleFiles)
  168. flags |= OFN_ALLOWMULTISELECT;
  169. if (extraInfoComponent != nullptr)
  170. {
  171. flags |= OFN_ENABLEHOOK;
  172. info.customComponent = new CustomComponentHolder (extraInfoComponent);
  173. info.customComponent->enterModalState();
  174. }
  175. const size_t filterSpaceNumChars = 2048;
  176. HeapBlock<WCHAR> filters;
  177. filters.calloc (filterSpaceNumChars);
  178. const size_t bytesWritten = filter.copyToUTF16 (filters.getData(), filterSpaceNumChars * sizeof (WCHAR));
  179. filter.copyToUTF16 (filters + (bytesWritten / sizeof (WCHAR)),
  180. ((filterSpaceNumChars - 1) * sizeof (WCHAR) - bytesWritten));
  181. OPENFILENAMEW of = { 0 };
  182. String localPath (info.initialPath);
  183. #ifdef OPENFILENAME_SIZE_VERSION_400W
  184. of.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
  185. #else
  186. of.lStructSize = sizeof (of);
  187. #endif
  188. of.hwndOwner = (HWND) parentWindow.getWindowHandle();
  189. of.lpstrFilter = filters.getData();
  190. of.nFilterIndex = 1;
  191. of.lpstrFile = files;
  192. of.nMaxFile = (DWORD) charsAvailableForResult;
  193. of.lpstrInitialDir = localPath.toWideCharPointer();
  194. of.lpstrTitle = title.toWideCharPointer();
  195. of.Flags = flags;
  196. of.lCustData = (LPARAM) &info;
  197. if (extraInfoComponent != nullptr)
  198. of.lpfnHook = &openCallback;
  199. if (isSaveDialogue)
  200. {
  201. StringArray tokens;
  202. tokens.addTokens (filter, ";,", "\"'");
  203. tokens.trim();
  204. tokens.removeEmptyStrings();
  205. if (tokens.size() == 1 && tokens[0].removeCharacters ("*.").isNotEmpty())
  206. {
  207. defaultExtension = tokens[0].fromFirstOccurrenceOf (".", false, false);
  208. of.lpstrDefExt = defaultExtension.toWideCharPointer();
  209. }
  210. if (! GetSaveFileName (&of))
  211. return;
  212. }
  213. else
  214. {
  215. if (! GetOpenFileName (&of))
  216. return;
  217. }
  218. filenameOffset = of.nFileOffset;
  219. }
  220. if (selectMultipleFiles && filenameOffset > 0 && files [filenameOffset - 1] == 0)
  221. {
  222. const WCHAR* filename = files + filenameOffset;
  223. while (*filename != 0)
  224. {
  225. results.add (File (String (files) + "\\" + String (filename)));
  226. filename += wcslen (filename) + 1;
  227. }
  228. }
  229. else if (files[0] != 0)
  230. {
  231. results.add (File (String (files)));
  232. }
  233. }