The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

775 lines
25KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. // Win32NativeFileChooser needs to be a reference counted object as there
  21. // is no way for the parent to know when the dialog HWND has actually been
  22. // created without pumping the message thread (which is forbidden when modal
  23. // loops are disabled). However, the HWND pointer is the only way to cancel
  24. // the dialog box. This means that the actual native FileChooser HWND may
  25. // not have been created yet when the user deletes JUCE's FileChooser class. If this
  26. // occurs the Win32NativeFileChooser will still have a reference count of 1 and will
  27. // simply delete itself immediately once the HWND will have been created a while later.
  28. class Win32NativeFileChooser : public ReferenceCountedObject,
  29. private Thread
  30. {
  31. public:
  32. using Ptr = ReferenceCountedObjectPtr<Win32NativeFileChooser>;
  33. enum { charsAvailableForResult = 32768 };
  34. Win32NativeFileChooser (Component* parent, int flags, FilePreviewComponent* previewComp,
  35. const File& startingFile, const String& titleToUse,
  36. const String& filtersToUse)
  37. : Thread ("Native Win32 FileChooser"),
  38. owner (parent), title (titleToUse), filtersString (filtersToUse),
  39. selectsDirectories ((flags & FileBrowserComponent::canSelectDirectories) != 0),
  40. isSave ((flags & FileBrowserComponent::saveMode) != 0),
  41. warnAboutOverwrite ((flags & FileBrowserComponent::warnAboutOverwriting) != 0),
  42. selectMultiple ((flags & FileBrowserComponent::canSelectMultipleItems) != 0),
  43. nativeDialogRef (nullptr), shouldCancel (0)
  44. {
  45. auto parentDirectory = startingFile.getParentDirectory();
  46. // Handle nonexistent root directories in the same way as existing ones
  47. files.calloc (static_cast<size_t> (charsAvailableForResult) + 1);
  48. if (startingFile.isDirectory() || startingFile.isRoot())
  49. {
  50. initialPath = startingFile.getFullPathName();
  51. }
  52. else
  53. {
  54. startingFile.getFileName().copyToUTF16 (files,
  55. static_cast<size_t> (charsAvailableForResult) * sizeof (WCHAR));
  56. initialPath = parentDirectory.getFullPathName();
  57. }
  58. if (! selectsDirectories)
  59. {
  60. if (previewComp != nullptr)
  61. customComponent.reset (new CustomComponentHolder (previewComp));
  62. setupFilters();
  63. }
  64. }
  65. ~Win32NativeFileChooser()
  66. {
  67. signalThreadShouldExit();
  68. waitForThreadToExit (-1);
  69. }
  70. void open (bool async)
  71. {
  72. results.clear();
  73. // the thread should not be running
  74. nativeDialogRef.set (nullptr);
  75. if (async)
  76. {
  77. jassert (! isThreadRunning());
  78. threadHasReference.reset();
  79. startThread();
  80. threadHasReference.wait (-1);
  81. }
  82. else
  83. {
  84. results = openDialog (false);
  85. owner->exitModalState (results.size() > 0 ? 1 : 0);
  86. }
  87. }
  88. void cancel()
  89. {
  90. ScopedLock lock (deletingDialog);
  91. customComponent = nullptr;
  92. shouldCancel.set (1);
  93. if (auto hwnd = nativeDialogRef.get())
  94. EndDialog (hwnd, 0);
  95. }
  96. Component* getCustomComponent() { return customComponent.get(); }
  97. Array<URL> results;
  98. private:
  99. //==============================================================================
  100. class CustomComponentHolder : public Component
  101. {
  102. public:
  103. CustomComponentHolder (Component* const customComp)
  104. {
  105. setVisible (true);
  106. setOpaque (true);
  107. addAndMakeVisible (customComp);
  108. setSize (jlimit (20, 800, customComp->getWidth()), customComp->getHeight());
  109. }
  110. void paint (Graphics& g) override
  111. {
  112. g.fillAll (Colours::lightgrey);
  113. }
  114. void resized() override
  115. {
  116. if (Component* const c = getChildComponent(0))
  117. c->setBounds (getLocalBounds());
  118. }
  119. private:
  120. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomComponentHolder)
  121. };
  122. //==============================================================================
  123. Component::SafePointer<Component> owner;
  124. String title, filtersString;
  125. std::unique_ptr<CustomComponentHolder> customComponent;
  126. String initialPath, returnedString;
  127. WaitableEvent threadHasReference;
  128. CriticalSection deletingDialog;
  129. bool selectsDirectories, isSave, warnAboutOverwrite, selectMultiple;
  130. HeapBlock<WCHAR> files;
  131. HeapBlock<WCHAR> filters;
  132. Atomic<HWND> nativeDialogRef;
  133. Atomic<int> shouldCancel;
  134. bool showDialog (IFileDialog& dialog, bool async) const
  135. {
  136. FILEOPENDIALOGOPTIONS flags = {};
  137. if (FAILED (dialog.GetOptions (&flags)))
  138. return false;
  139. const auto setBit = [] (FILEOPENDIALOGOPTIONS& field, bool value, FILEOPENDIALOGOPTIONS option)
  140. {
  141. if (value)
  142. field |= option;
  143. else
  144. field &= ~option;
  145. };
  146. setBit (flags, selectsDirectories, FOS_PICKFOLDERS);
  147. setBit (flags, warnAboutOverwrite, FOS_OVERWRITEPROMPT);
  148. setBit (flags, selectMultiple, FOS_ALLOWMULTISELECT);
  149. setBit (flags, customComponent != nullptr, FOS_FORCEPREVIEWPANEON);
  150. if (FAILED (dialog.SetOptions (flags)) || FAILED (dialog.SetTitle (title.toUTF16())))
  151. return false;
  152. PIDLIST_ABSOLUTE pidl = {};
  153. if (FAILED (SHParseDisplayName (initialPath.toWideCharPointer(), nullptr, &pidl, SFGAO_FOLDER, nullptr)))
  154. return false;
  155. const auto item = [&]
  156. {
  157. ComSmartPtr<IShellItem> ptr;
  158. SHCreateShellItem (nullptr, nullptr, pidl, ptr.resetAndGetPointerAddress());
  159. return ptr;
  160. }();
  161. if (item == nullptr || FAILED (dialog.SetFolder (item)))
  162. return false;
  163. String filename (files.getData());
  164. if (FAILED (dialog.SetFileName (filename.toWideCharPointer())))
  165. return false;
  166. auto extension = getDefaultFileExtension (filename);
  167. if (extension.isNotEmpty() && FAILED (dialog.SetDefaultExtension (extension.toWideCharPointer())))
  168. return false;
  169. const COMDLG_FILTERSPEC spec[] { { filtersString.toWideCharPointer(), filtersString.toWideCharPointer() } };
  170. if (! selectsDirectories && FAILED (dialog.SetFileTypes (numElementsInArray (spec), spec)))
  171. return false;
  172. return dialog.Show (static_cast<HWND> (async ? nullptr : owner->getWindowHandle())) == S_OK;
  173. }
  174. //==============================================================================
  175. Array<URL> openDialogVistaAndUp (bool async)
  176. {
  177. const auto getUrl = [] (IShellItem& item)
  178. {
  179. struct Free
  180. {
  181. void operator() (LPWSTR ptr) const noexcept { CoTaskMemFree (ptr); }
  182. };
  183. LPWSTR ptr = nullptr;
  184. item.GetDisplayName (SIGDN_URL, &ptr);
  185. return std::unique_ptr<WCHAR, Free> { ptr };
  186. };
  187. if (isSave)
  188. {
  189. const auto dialog = [&]
  190. {
  191. ComSmartPtr<IFileDialog> ptr;
  192. ptr.CoCreateInstance (CLSID_FileSaveDialog, CLSCTX_INPROC_SERVER);
  193. return ptr;
  194. }();
  195. if (dialog == nullptr)
  196. return {};
  197. showDialog (*dialog, async);
  198. const auto item = [&]
  199. {
  200. ComSmartPtr<IShellItem> ptr;
  201. dialog->GetResult (ptr.resetAndGetPointerAddress());
  202. return ptr;
  203. }();
  204. if (item == nullptr)
  205. return {};
  206. return { URL (String (getUrl (*item).get())) };
  207. }
  208. const auto dialog = [&]
  209. {
  210. ComSmartPtr<IFileOpenDialog> ptr;
  211. ptr.CoCreateInstance (CLSID_FileOpenDialog, CLSCTX_INPROC_SERVER);
  212. return ptr;
  213. }();
  214. if (dialog == nullptr)
  215. return {};
  216. showDialog (*dialog, async);
  217. const auto items = [&]
  218. {
  219. ComSmartPtr<IShellItemArray> ptr;
  220. dialog->GetResults (ptr.resetAndGetPointerAddress());
  221. return ptr;
  222. }();
  223. if (items == nullptr)
  224. return {};
  225. Array<URL> result;
  226. DWORD numItems = 0;
  227. items->GetCount (&numItems);
  228. for (DWORD i = 0; i < numItems; ++i)
  229. {
  230. ComSmartPtr<IShellItem> scope;
  231. items->GetItemAt (i, scope.resetAndGetPointerAddress());
  232. if (scope != nullptr)
  233. result.add (String (getUrl (*scope).get()));
  234. }
  235. return result;
  236. }
  237. Array<URL> openDialogPreVista (bool async)
  238. {
  239. Array<URL> selections;
  240. if (selectsDirectories)
  241. {
  242. BROWSEINFO bi = {};
  243. bi.hwndOwner = (HWND) (async ? nullptr : owner->getWindowHandle());
  244. bi.pszDisplayName = files;
  245. bi.lpszTitle = title.toWideCharPointer();
  246. bi.lParam = (LPARAM) this;
  247. bi.lpfn = browseCallbackProc;
  248. #ifdef BIF_USENEWUI
  249. bi.ulFlags = BIF_USENEWUI | BIF_VALIDATE;
  250. #else
  251. bi.ulFlags = 0x50;
  252. #endif
  253. LPITEMIDLIST list = SHBrowseForFolder (&bi);
  254. if (! SHGetPathFromIDListW (list, files))
  255. {
  256. files[0] = 0;
  257. returnedString.clear();
  258. }
  259. LPMALLOC al;
  260. if (list != nullptr && SUCCEEDED (SHGetMalloc (&al)))
  261. al->Free (list);
  262. if (files[0] != 0)
  263. {
  264. File result (String (files.get()));
  265. if (returnedString.isNotEmpty())
  266. result = result.getSiblingFile (returnedString);
  267. selections.add (URL (result));
  268. }
  269. }
  270. else
  271. {
  272. OPENFILENAMEW of = {};
  273. #ifdef OPENFILENAME_SIZE_VERSION_400W
  274. of.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
  275. #else
  276. of.lStructSize = sizeof (of);
  277. #endif
  278. of.hwndOwner = (HWND) (async ? nullptr : owner->getWindowHandle());
  279. of.lpstrFilter = filters.getData();
  280. of.nFilterIndex = 1;
  281. of.lpstrFile = files;
  282. of.nMaxFile = (DWORD) charsAvailableForResult;
  283. of.lpstrInitialDir = initialPath.toWideCharPointer();
  284. of.lpstrTitle = title.toWideCharPointer();
  285. of.Flags = getOpenFilenameFlags (async);
  286. of.lCustData = (LPARAM) this;
  287. of.lpfnHook = &openCallback;
  288. if (isSave)
  289. {
  290. auto extension = getDefaultFileExtension (files.getData());
  291. if (extension.isNotEmpty())
  292. of.lpstrDefExt = extension.toWideCharPointer();
  293. if (! GetSaveFileName (&of))
  294. return {};
  295. }
  296. else
  297. {
  298. if (! GetOpenFileName (&of))
  299. return {};
  300. }
  301. if (selectMultiple && of.nFileOffset > 0 && files[of.nFileOffset - 1] == 0)
  302. {
  303. const WCHAR* filename = files + of.nFileOffset;
  304. while (*filename != 0)
  305. {
  306. selections.add (URL (File (String (files.get())).getChildFile (String (filename))));
  307. filename += wcslen (filename) + 1;
  308. }
  309. }
  310. else if (files[0] != 0)
  311. {
  312. selections.add (URL (File (String (files.get()))));
  313. }
  314. }
  315. return selections;
  316. }
  317. Array<URL> openDialog (bool async)
  318. {
  319. struct Remover
  320. {
  321. explicit Remover (Win32NativeFileChooser& chooser) : item (chooser) {}
  322. ~Remover() { getNativeDialogList().removeValue (&item); }
  323. Win32NativeFileChooser& item;
  324. };
  325. const Remover remover (*this);
  326. if (SystemStats::getOperatingSystemType() >= SystemStats::WinVista)
  327. return openDialogVistaAndUp (async);
  328. return openDialogPreVista (async);
  329. }
  330. void run() override
  331. {
  332. // We use a functor rather than a lambda here because
  333. // we want to move ownership of the Ptr into the function
  334. // object, and C++11 doesn't support general lambda capture
  335. struct AsyncCallback
  336. {
  337. AsyncCallback (Ptr p, Array<URL> r)
  338. : ptr (std::move (p)),
  339. results (std::move (r)) {}
  340. void operator()()
  341. {
  342. ptr->results = std::move (results);
  343. if (ptr->owner != nullptr)
  344. ptr->owner->exitModalState (ptr->results.size() > 0 ? 1 : 0);
  345. }
  346. Ptr ptr;
  347. Array<URL> results;
  348. };
  349. // as long as the thread is running, don't delete this class
  350. Ptr safeThis (this);
  351. threadHasReference.signal();
  352. auto r = openDialog (true);
  353. MessageManager::callAsync (AsyncCallback (std::move (safeThis), std::move (r)));
  354. }
  355. static HashMap<HWND, Win32NativeFileChooser*>& getNativeDialogList()
  356. {
  357. static HashMap<HWND, Win32NativeFileChooser*> dialogs;
  358. return dialogs;
  359. }
  360. static Win32NativeFileChooser* getNativePointerForDialog (HWND hWnd)
  361. {
  362. return getNativeDialogList()[hWnd];
  363. }
  364. //==============================================================================
  365. void setupFilters()
  366. {
  367. const size_t filterSpaceNumChars = 2048;
  368. filters.calloc (filterSpaceNumChars);
  369. const size_t bytesWritten = filtersString.copyToUTF16 (filters.getData(), filterSpaceNumChars * sizeof (WCHAR));
  370. filtersString.copyToUTF16 (filters + (bytesWritten / sizeof (WCHAR)),
  371. ((filterSpaceNumChars - 1) * sizeof (WCHAR) - bytesWritten));
  372. for (size_t i = 0; i < filterSpaceNumChars; ++i)
  373. if (filters[i] == '|')
  374. filters[i] = 0;
  375. }
  376. DWORD getOpenFilenameFlags (bool async)
  377. {
  378. DWORD ofFlags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_ENABLESIZING;
  379. if (warnAboutOverwrite)
  380. ofFlags |= OFN_OVERWRITEPROMPT;
  381. if (selectMultiple)
  382. ofFlags |= OFN_ALLOWMULTISELECT;
  383. if (async || customComponent != nullptr)
  384. ofFlags |= OFN_ENABLEHOOK;
  385. return ofFlags;
  386. }
  387. String getDefaultFileExtension (const String& filename) const
  388. {
  389. auto extension = filename.fromLastOccurrenceOf (".", false, false);
  390. if (extension.isEmpty())
  391. {
  392. auto tokens = StringArray::fromTokens (filtersString, ";,", "\"'");
  393. tokens.trim();
  394. tokens.removeEmptyStrings();
  395. if (tokens.size() == 1 && tokens[0].removeCharacters ("*.").isNotEmpty())
  396. extension = tokens[0].fromFirstOccurrenceOf (".", false, false);
  397. }
  398. return extension;
  399. }
  400. //==============================================================================
  401. void initialised (HWND hWnd)
  402. {
  403. SendMessage (hWnd, BFFM_SETSELECTIONW, TRUE, (LPARAM) initialPath.toWideCharPointer());
  404. initDialog (hWnd);
  405. }
  406. void validateFailed (const String& path)
  407. {
  408. returnedString = path;
  409. }
  410. void initDialog (HWND hdlg)
  411. {
  412. ScopedLock lock (deletingDialog);
  413. getNativeDialogList().set (hdlg, this);
  414. if (shouldCancel.get() != 0)
  415. {
  416. EndDialog (hdlg, 0);
  417. }
  418. else
  419. {
  420. nativeDialogRef.set (hdlg);
  421. if (customComponent != nullptr)
  422. {
  423. Component::SafePointer<Component> safeCustomComponent (customComponent.get());
  424. RECT dialogScreenRect, dialogClientRect;
  425. GetWindowRect (hdlg, &dialogScreenRect);
  426. GetClientRect (hdlg, &dialogClientRect);
  427. auto screenRectangle = Rectangle<int>::leftTopRightBottom (dialogScreenRect.left, dialogScreenRect.top,
  428. dialogScreenRect.right, dialogScreenRect.bottom);
  429. auto scale = Desktop::getInstance().getDisplays().findDisplayForRect (screenRectangle, true).scale;
  430. auto physicalComponentWidth = roundToInt (safeCustomComponent->getWidth() * scale);
  431. SetWindowPos (hdlg, nullptr, screenRectangle.getX(), screenRectangle.getY(),
  432. physicalComponentWidth + jmax (150, screenRectangle.getWidth()),
  433. jmax (150, screenRectangle.getHeight()),
  434. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  435. auto appendCustomComponent = [safeCustomComponent, dialogClientRect, scale, hdlg]() mutable
  436. {
  437. if (safeCustomComponent != nullptr)
  438. {
  439. auto scaledClientRectangle = Rectangle<int>::leftTopRightBottom (dialogClientRect.left, dialogClientRect.top,
  440. dialogClientRect.right, dialogClientRect.bottom) / scale;
  441. safeCustomComponent->setBounds (scaledClientRectangle.getRight(), scaledClientRectangle.getY(),
  442. safeCustomComponent->getWidth(), scaledClientRectangle.getHeight());
  443. safeCustomComponent->addToDesktop (0, hdlg);
  444. }
  445. };
  446. if (MessageManager::getInstance()->isThisTheMessageThread())
  447. appendCustomComponent();
  448. else
  449. MessageManager::callAsync (appendCustomComponent);
  450. }
  451. }
  452. }
  453. void destroyDialog (HWND hdlg)
  454. {
  455. ScopedLock exiting (deletingDialog);
  456. getNativeDialogList().remove (hdlg);
  457. nativeDialogRef.set (nullptr);
  458. if (MessageManager::getInstance()->isThisTheMessageThread())
  459. customComponent = nullptr;
  460. else
  461. MessageManager::callAsync ([this] { customComponent = nullptr; });
  462. }
  463. void selectionChanged (HWND hdlg)
  464. {
  465. ScopedLock lock (deletingDialog);
  466. if (customComponent != nullptr && shouldCancel.get() == 0)
  467. {
  468. if (FilePreviewComponent* comp = dynamic_cast<FilePreviewComponent*> (customComponent->getChildComponent (0)))
  469. {
  470. WCHAR path [MAX_PATH * 2] = { 0 };
  471. CommDlg_OpenSave_GetFilePath (hdlg, (LPARAM) &path, MAX_PATH);
  472. if (MessageManager::getInstance()->isThisTheMessageThread())
  473. {
  474. comp->selectedFileChanged (File (path));
  475. }
  476. else
  477. {
  478. Component::SafePointer<FilePreviewComponent> safeComp (comp);
  479. File selectedFile (path);
  480. MessageManager::callAsync ([safeComp, selectedFile]() mutable
  481. {
  482. safeComp->selectedFileChanged (selectedFile);
  483. });
  484. }
  485. }
  486. }
  487. }
  488. //==============================================================================
  489. static int CALLBACK browseCallbackProc (HWND hWnd, UINT msg, LPARAM lParam, LPARAM lpData)
  490. {
  491. auto* self = reinterpret_cast<Win32NativeFileChooser*> (lpData);
  492. switch (msg)
  493. {
  494. case BFFM_INITIALIZED: self->initialised (hWnd); break;
  495. case BFFM_VALIDATEFAILEDW: self->validateFailed (String ((LPCWSTR) lParam)); break;
  496. case BFFM_VALIDATEFAILEDA: self->validateFailed (String ((const char*) lParam)); break;
  497. default: break;
  498. }
  499. return 0;
  500. }
  501. static UINT_PTR CALLBACK openCallback (HWND hwnd, UINT uiMsg, WPARAM /*wParam*/, LPARAM lParam)
  502. {
  503. auto hdlg = getDialogFromHWND (hwnd);
  504. switch (uiMsg)
  505. {
  506. case WM_INITDIALOG:
  507. {
  508. if (auto* self = reinterpret_cast<Win32NativeFileChooser*> (((OPENFILENAMEW*) lParam)->lCustData))
  509. self->initDialog (hdlg);
  510. break;
  511. }
  512. case WM_DESTROY:
  513. {
  514. if (auto* self = getNativeDialogList()[hdlg])
  515. self->destroyDialog (hdlg);
  516. break;
  517. }
  518. case WM_NOTIFY:
  519. {
  520. auto ofn = reinterpret_cast<LPOFNOTIFY> (lParam);
  521. if (ofn->hdr.code == CDN_SELCHANGE)
  522. if (auto* self = reinterpret_cast<Win32NativeFileChooser*> (ofn->lpOFN->lCustData))
  523. self->selectionChanged (hdlg);
  524. break;
  525. }
  526. default:
  527. break;
  528. }
  529. return 0;
  530. }
  531. static HWND getDialogFromHWND (HWND hwnd)
  532. {
  533. if (hwnd == nullptr)
  534. return nullptr;
  535. HWND dialogH = GetParent (hwnd);
  536. if (dialogH == nullptr)
  537. dialogH = hwnd;
  538. return dialogH;
  539. }
  540. //==============================================================================
  541. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Win32NativeFileChooser)
  542. };
  543. class FileChooser::Native : public Component,
  544. public FileChooser::Pimpl
  545. {
  546. public:
  547. Native (FileChooser& fileChooser, int flags, FilePreviewComponent* previewComp)
  548. : owner (fileChooser),
  549. nativeFileChooser (new Win32NativeFileChooser (this, flags, previewComp, fileChooser.startingFile,
  550. fileChooser.title, fileChooser.filters))
  551. {
  552. auto mainMon = Desktop::getInstance().getDisplays().getMainDisplay().userArea;
  553. setBounds (mainMon.getX() + mainMon.getWidth() / 4,
  554. mainMon.getY() + mainMon.getHeight() / 4,
  555. 0, 0);
  556. setOpaque (true);
  557. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  558. addToDesktop (0);
  559. }
  560. ~Native() override
  561. {
  562. exitModalState (0);
  563. nativeFileChooser->cancel();
  564. nativeFileChooser = nullptr;
  565. }
  566. void launch() override
  567. {
  568. SafePointer<Native> safeThis (this);
  569. enterModalState (true, ModalCallbackFunction::create (
  570. [safeThis] (int)
  571. {
  572. if (safeThis != nullptr)
  573. safeThis->owner.finished (safeThis->nativeFileChooser->results);
  574. }));
  575. nativeFileChooser->open (true);
  576. }
  577. void runModally() override
  578. {
  579. enterModalState (true);
  580. nativeFileChooser->open (false);
  581. exitModalState (nativeFileChooser->results.size() > 0 ? 1 : 0);
  582. nativeFileChooser->cancel();
  583. owner.finished (nativeFileChooser->results);
  584. }
  585. bool canModalEventBeSentToComponent (const Component* targetComponent) override
  586. {
  587. if (targetComponent == nullptr)
  588. return false;
  589. if (targetComponent == nativeFileChooser->getCustomComponent())
  590. return true;
  591. return targetComponent->findParentComponentOfClass<FilePreviewComponent>() != nullptr;
  592. }
  593. private:
  594. FileChooser& owner;
  595. Win32NativeFileChooser::Ptr nativeFileChooser;
  596. };
  597. //==============================================================================
  598. bool FileChooser::isPlatformDialogAvailable()
  599. {
  600. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  601. return false;
  602. #else
  603. return true;
  604. #endif
  605. }
  606. FileChooser::Pimpl* FileChooser::showPlatformDialog (FileChooser& owner, int flags,
  607. FilePreviewComponent* preview)
  608. {
  609. return new FileChooser::Native (owner, flags, preview);
  610. }
  611. } // namespace juce