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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  99. return false;
  100. #else
  101. return true;
  102. #endif
  103. }
  104. void FileChooser::showPlatformDialog (Array<File>& results, const String& title_, const File& currentFileOrDirectory,
  105. const String& filter, bool selectsDirectory, bool /*selectsFiles*/,
  106. bool isSaveDialogue, bool warnAboutOverwritingExistingFiles,
  107. bool selectMultipleFiles, FilePreviewComponent* extraInfoComponent)
  108. {
  109. using namespace FileChooserHelpers;
  110. const String title (title_);
  111. String defaultExtension; // scope of these strings must extend beyond dialog's lifetime.
  112. HeapBlock<WCHAR> files;
  113. const size_t charsAvailableForResult = 32768;
  114. files.calloc (charsAvailableForResult + 1);
  115. int filenameOffset = 0;
  116. FileChooserCallbackInfo info;
  117. // use a modal window as the parent for this dialog box
  118. // to block input from other app windows
  119. Component parentWindow (String::empty);
  120. const Rectangle<int> mainMon (Desktop::getInstance().getDisplays().getMainDisplay().userArea);
  121. parentWindow.setBounds (mainMon.getX() + mainMon.getWidth() / 4,
  122. mainMon.getY() + mainMon.getHeight() / 4,
  123. 0, 0);
  124. parentWindow.setOpaque (true);
  125. parentWindow.setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  126. parentWindow.addToDesktop (0);
  127. if (extraInfoComponent == nullptr)
  128. parentWindow.enterModalState();
  129. if (currentFileOrDirectory.isDirectory())
  130. {
  131. info.initialPath = currentFileOrDirectory.getFullPathName();
  132. }
  133. else
  134. {
  135. currentFileOrDirectory.getFileName().copyToUTF16 (files, charsAvailableForResult * sizeof (WCHAR));
  136. info.initialPath = currentFileOrDirectory.getParentDirectory().getFullPathName();
  137. }
  138. if (selectsDirectory)
  139. {
  140. BROWSEINFO bi = { 0 };
  141. bi.hwndOwner = (HWND) parentWindow.getWindowHandle();
  142. bi.pszDisplayName = files;
  143. bi.lpszTitle = title.toWideCharPointer();
  144. bi.lParam = (LPARAM) &info;
  145. bi.lpfn = browseCallbackProc;
  146. #ifdef BIF_USENEWUI
  147. bi.ulFlags = BIF_USENEWUI | BIF_VALIDATE;
  148. #else
  149. bi.ulFlags = 0x50;
  150. #endif
  151. LPITEMIDLIST list = SHBrowseForFolder (&bi);
  152. if (! SHGetPathFromIDListW (list, files))
  153. {
  154. files[0] = 0;
  155. info.returnedString.clear();
  156. }
  157. LPMALLOC al;
  158. if (list != nullptr && SUCCEEDED (SHGetMalloc (&al)))
  159. al->Free (list);
  160. if (info.returnedString.isNotEmpty())
  161. {
  162. results.add (File (String (files)).getSiblingFile (info.returnedString));
  163. return;
  164. }
  165. }
  166. else
  167. {
  168. DWORD flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_ENABLESIZING;
  169. if (warnAboutOverwritingExistingFiles)
  170. flags |= OFN_OVERWRITEPROMPT;
  171. if (selectMultipleFiles)
  172. flags |= OFN_ALLOWMULTISELECT;
  173. if (extraInfoComponent != nullptr)
  174. {
  175. flags |= OFN_ENABLEHOOK;
  176. info.customComponent = new CustomComponentHolder (extraInfoComponent);
  177. info.customComponent->enterModalState();
  178. }
  179. const size_t filterSpaceNumChars = 2048;
  180. HeapBlock<WCHAR> filters;
  181. filters.calloc (filterSpaceNumChars);
  182. const size_t bytesWritten = filter.copyToUTF16 (filters.getData(), filterSpaceNumChars * sizeof (WCHAR));
  183. filter.copyToUTF16 (filters + (bytesWritten / sizeof (WCHAR)),
  184. ((filterSpaceNumChars - 1) * sizeof (WCHAR) - bytesWritten));
  185. OPENFILENAMEW of = { 0 };
  186. String localPath (info.initialPath);
  187. #ifdef OPENFILENAME_SIZE_VERSION_400W
  188. of.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
  189. #else
  190. of.lStructSize = sizeof (of);
  191. #endif
  192. of.hwndOwner = (HWND) parentWindow.getWindowHandle();
  193. of.lpstrFilter = filters.getData();
  194. of.nFilterIndex = 1;
  195. of.lpstrFile = files;
  196. of.nMaxFile = (DWORD) charsAvailableForResult;
  197. of.lpstrInitialDir = localPath.toWideCharPointer();
  198. of.lpstrTitle = title.toWideCharPointer();
  199. of.Flags = flags;
  200. of.lCustData = (LPARAM) &info;
  201. if (extraInfoComponent != nullptr)
  202. of.lpfnHook = &openCallback;
  203. if (isSaveDialogue)
  204. {
  205. StringArray tokens;
  206. tokens.addTokens (filter, ";,", "\"'");
  207. tokens.trim();
  208. tokens.removeEmptyStrings();
  209. if (tokens.size() == 1 && tokens[0].removeCharacters ("*.").isNotEmpty())
  210. {
  211. defaultExtension = tokens[0].fromFirstOccurrenceOf (".", false, false);
  212. of.lpstrDefExt = defaultExtension.toWideCharPointer();
  213. }
  214. if (! GetSaveFileName (&of))
  215. return;
  216. }
  217. else
  218. {
  219. if (! GetOpenFileName (&of))
  220. return;
  221. }
  222. filenameOffset = of.nFileOffset;
  223. }
  224. if (selectMultipleFiles && filenameOffset > 0 && files [filenameOffset - 1] == 0)
  225. {
  226. const WCHAR* filename = files + filenameOffset;
  227. while (*filename != 0)
  228. {
  229. results.add (File (String (files) + "\\" + String (filename)));
  230. filename += wcslen (filename) + 1;
  231. }
  232. }
  233. else if (files[0] != 0)
  234. {
  235. results.add (File (String (files)));
  236. }
  237. }