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.

294 lines
10KB

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