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.

304 lines
10KB

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