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.

778 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. && customComponent == nullptr)
  328. {
  329. return openDialogVistaAndUp (async);
  330. }
  331. return openDialogPreVista (async);
  332. }
  333. void run() override
  334. {
  335. // We use a functor rather than a lambda here because
  336. // we want to move ownership of the Ptr into the function
  337. // object, and C++11 doesn't support general lambda capture
  338. struct AsyncCallback
  339. {
  340. AsyncCallback (Ptr p, Array<URL> r)
  341. : ptr (std::move (p)),
  342. results (std::move (r)) {}
  343. void operator()()
  344. {
  345. ptr->results = std::move (results);
  346. if (ptr->owner != nullptr)
  347. ptr->owner->exitModalState (ptr->results.size() > 0 ? 1 : 0);
  348. }
  349. Ptr ptr;
  350. Array<URL> results;
  351. };
  352. // as long as the thread is running, don't delete this class
  353. Ptr safeThis (this);
  354. threadHasReference.signal();
  355. auto r = openDialog (true);
  356. MessageManager::callAsync (AsyncCallback (std::move (safeThis), std::move (r)));
  357. }
  358. static HashMap<HWND, Win32NativeFileChooser*>& getNativeDialogList()
  359. {
  360. static HashMap<HWND, Win32NativeFileChooser*> dialogs;
  361. return dialogs;
  362. }
  363. static Win32NativeFileChooser* getNativePointerForDialog (HWND hWnd)
  364. {
  365. return getNativeDialogList()[hWnd];
  366. }
  367. //==============================================================================
  368. void setupFilters()
  369. {
  370. const size_t filterSpaceNumChars = 2048;
  371. filters.calloc (filterSpaceNumChars);
  372. const size_t bytesWritten = filtersString.copyToUTF16 (filters.getData(), filterSpaceNumChars * sizeof (WCHAR));
  373. filtersString.copyToUTF16 (filters + (bytesWritten / sizeof (WCHAR)),
  374. ((filterSpaceNumChars - 1) * sizeof (WCHAR) - bytesWritten));
  375. for (size_t i = 0; i < filterSpaceNumChars; ++i)
  376. if (filters[i] == '|')
  377. filters[i] = 0;
  378. }
  379. DWORD getOpenFilenameFlags (bool async)
  380. {
  381. DWORD ofFlags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_ENABLESIZING;
  382. if (warnAboutOverwrite)
  383. ofFlags |= OFN_OVERWRITEPROMPT;
  384. if (selectMultiple)
  385. ofFlags |= OFN_ALLOWMULTISELECT;
  386. if (async || customComponent != nullptr)
  387. ofFlags |= OFN_ENABLEHOOK;
  388. return ofFlags;
  389. }
  390. String getDefaultFileExtension (const String& filename) const
  391. {
  392. auto extension = filename.fromLastOccurrenceOf (".", false, false);
  393. if (extension.isEmpty())
  394. {
  395. auto tokens = StringArray::fromTokens (filtersString, ";,", "\"'");
  396. tokens.trim();
  397. tokens.removeEmptyStrings();
  398. if (tokens.size() == 1 && tokens[0].removeCharacters ("*.").isNotEmpty())
  399. extension = tokens[0].fromFirstOccurrenceOf (".", false, false);
  400. }
  401. return extension;
  402. }
  403. //==============================================================================
  404. void initialised (HWND hWnd)
  405. {
  406. SendMessage (hWnd, BFFM_SETSELECTIONW, TRUE, (LPARAM) initialPath.toWideCharPointer());
  407. initDialog (hWnd);
  408. }
  409. void validateFailed (const String& path)
  410. {
  411. returnedString = path;
  412. }
  413. void initDialog (HWND hdlg)
  414. {
  415. ScopedLock lock (deletingDialog);
  416. getNativeDialogList().set (hdlg, this);
  417. if (shouldCancel.get() != 0)
  418. {
  419. EndDialog (hdlg, 0);
  420. }
  421. else
  422. {
  423. nativeDialogRef.set (hdlg);
  424. if (customComponent != nullptr)
  425. {
  426. Component::SafePointer<Component> safeCustomComponent (customComponent.get());
  427. RECT dialogScreenRect, dialogClientRect;
  428. GetWindowRect (hdlg, &dialogScreenRect);
  429. GetClientRect (hdlg, &dialogClientRect);
  430. auto screenRectangle = Rectangle<int>::leftTopRightBottom (dialogScreenRect.left, dialogScreenRect.top,
  431. dialogScreenRect.right, dialogScreenRect.bottom);
  432. auto scale = Desktop::getInstance().getDisplays().getDisplayForRect (screenRectangle, true)->scale;
  433. auto physicalComponentWidth = roundToInt (safeCustomComponent->getWidth() * scale);
  434. SetWindowPos (hdlg, nullptr, screenRectangle.getX(), screenRectangle.getY(),
  435. physicalComponentWidth + jmax (150, screenRectangle.getWidth()),
  436. jmax (150, screenRectangle.getHeight()),
  437. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  438. auto appendCustomComponent = [safeCustomComponent, dialogClientRect, scale, hdlg]() mutable
  439. {
  440. if (safeCustomComponent != nullptr)
  441. {
  442. auto scaledClientRectangle = Rectangle<int>::leftTopRightBottom (dialogClientRect.left, dialogClientRect.top,
  443. dialogClientRect.right, dialogClientRect.bottom) / scale;
  444. safeCustomComponent->setBounds (scaledClientRectangle.getRight(), scaledClientRectangle.getY(),
  445. safeCustomComponent->getWidth(), scaledClientRectangle.getHeight());
  446. safeCustomComponent->addToDesktop (0, hdlg);
  447. }
  448. };
  449. if (MessageManager::getInstance()->isThisTheMessageThread())
  450. appendCustomComponent();
  451. else
  452. MessageManager::callAsync (appendCustomComponent);
  453. }
  454. }
  455. }
  456. void destroyDialog (HWND hdlg)
  457. {
  458. ScopedLock exiting (deletingDialog);
  459. getNativeDialogList().remove (hdlg);
  460. nativeDialogRef.set (nullptr);
  461. if (MessageManager::getInstance()->isThisTheMessageThread())
  462. customComponent = nullptr;
  463. else
  464. MessageManager::callAsync ([this] { customComponent = nullptr; });
  465. }
  466. void selectionChanged (HWND hdlg)
  467. {
  468. ScopedLock lock (deletingDialog);
  469. if (customComponent != nullptr && shouldCancel.get() == 0)
  470. {
  471. if (FilePreviewComponent* comp = dynamic_cast<FilePreviewComponent*> (customComponent->getChildComponent (0)))
  472. {
  473. WCHAR path [MAX_PATH * 2] = { 0 };
  474. CommDlg_OpenSave_GetFilePath (hdlg, (LPARAM) &path, MAX_PATH);
  475. if (MessageManager::getInstance()->isThisTheMessageThread())
  476. {
  477. comp->selectedFileChanged (File (path));
  478. }
  479. else
  480. {
  481. Component::SafePointer<FilePreviewComponent> safeComp (comp);
  482. File selectedFile (path);
  483. MessageManager::callAsync ([safeComp, selectedFile]() mutable
  484. {
  485. safeComp->selectedFileChanged (selectedFile);
  486. });
  487. }
  488. }
  489. }
  490. }
  491. //==============================================================================
  492. static int CALLBACK browseCallbackProc (HWND hWnd, UINT msg, LPARAM lParam, LPARAM lpData)
  493. {
  494. auto* self = reinterpret_cast<Win32NativeFileChooser*> (lpData);
  495. switch (msg)
  496. {
  497. case BFFM_INITIALIZED: self->initialised (hWnd); break;
  498. case BFFM_VALIDATEFAILEDW: self->validateFailed (String ((LPCWSTR) lParam)); break;
  499. case BFFM_VALIDATEFAILEDA: self->validateFailed (String ((const char*) lParam)); break;
  500. default: break;
  501. }
  502. return 0;
  503. }
  504. static UINT_PTR CALLBACK openCallback (HWND hwnd, UINT uiMsg, WPARAM /*wParam*/, LPARAM lParam)
  505. {
  506. auto hdlg = getDialogFromHWND (hwnd);
  507. switch (uiMsg)
  508. {
  509. case WM_INITDIALOG:
  510. {
  511. if (auto* self = reinterpret_cast<Win32NativeFileChooser*> (((OPENFILENAMEW*) lParam)->lCustData))
  512. self->initDialog (hdlg);
  513. break;
  514. }
  515. case WM_DESTROY:
  516. {
  517. if (auto* self = getNativeDialogList()[hdlg])
  518. self->destroyDialog (hdlg);
  519. break;
  520. }
  521. case WM_NOTIFY:
  522. {
  523. auto ofn = reinterpret_cast<LPOFNOTIFY> (lParam);
  524. if (ofn->hdr.code == CDN_SELCHANGE)
  525. if (auto* self = reinterpret_cast<Win32NativeFileChooser*> (ofn->lpOFN->lCustData))
  526. self->selectionChanged (hdlg);
  527. break;
  528. }
  529. default:
  530. break;
  531. }
  532. return 0;
  533. }
  534. static HWND getDialogFromHWND (HWND hwnd)
  535. {
  536. if (hwnd == nullptr)
  537. return nullptr;
  538. HWND dialogH = GetParent (hwnd);
  539. if (dialogH == nullptr)
  540. dialogH = hwnd;
  541. return dialogH;
  542. }
  543. //==============================================================================
  544. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Win32NativeFileChooser)
  545. };
  546. class FileChooser::Native : public Component,
  547. public FileChooser::Pimpl
  548. {
  549. public:
  550. Native (FileChooser& fileChooser, int flags, FilePreviewComponent* previewComp)
  551. : owner (fileChooser),
  552. nativeFileChooser (new Win32NativeFileChooser (this, flags, previewComp, fileChooser.startingFile,
  553. fileChooser.title, fileChooser.filters))
  554. {
  555. auto mainMon = Desktop::getInstance().getDisplays().getPrimaryDisplay()->userArea;
  556. setBounds (mainMon.getX() + mainMon.getWidth() / 4,
  557. mainMon.getY() + mainMon.getHeight() / 4,
  558. 0, 0);
  559. setOpaque (true);
  560. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  561. addToDesktop (0);
  562. }
  563. ~Native() override
  564. {
  565. exitModalState (0);
  566. nativeFileChooser->cancel();
  567. nativeFileChooser = nullptr;
  568. }
  569. void launch() override
  570. {
  571. SafePointer<Native> safeThis (this);
  572. enterModalState (true, ModalCallbackFunction::create (
  573. [safeThis] (int)
  574. {
  575. if (safeThis != nullptr)
  576. safeThis->owner.finished (safeThis->nativeFileChooser->results);
  577. }));
  578. nativeFileChooser->open (true);
  579. }
  580. void runModally() override
  581. {
  582. enterModalState (true);
  583. nativeFileChooser->open (false);
  584. exitModalState (nativeFileChooser->results.size() > 0 ? 1 : 0);
  585. nativeFileChooser->cancel();
  586. owner.finished (nativeFileChooser->results);
  587. }
  588. bool canModalEventBeSentToComponent (const Component* targetComponent) override
  589. {
  590. if (targetComponent == nullptr)
  591. return false;
  592. if (targetComponent == nativeFileChooser->getCustomComponent())
  593. return true;
  594. return targetComponent->findParentComponentOfClass<FilePreviewComponent>() != nullptr;
  595. }
  596. private:
  597. FileChooser& owner;
  598. Win32NativeFileChooser::Ptr nativeFileChooser;
  599. };
  600. //==============================================================================
  601. bool FileChooser::isPlatformDialogAvailable()
  602. {
  603. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  604. return false;
  605. #else
  606. return true;
  607. #endif
  608. }
  609. FileChooser::Pimpl* FileChooser::showPlatformDialog (FileChooser& owner, int flags,
  610. FilePreviewComponent* preview)
  611. {
  612. return new FileChooser::Native (owner, flags, preview);
  613. }
  614. } // namespace juce