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.

270 lines
9.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. extern bool juce_areThereAnyAlwaysOnTopWindows();
  19. namespace FileChooserHelpers
  20. {
  21. struct FileChooserCallbackInfo
  22. {
  23. String initialPath;
  24. String returnedString; // need this to get non-existent pathnames from the directory chooser
  25. ScopedPointer<Component> customComponent;
  26. };
  27. static int CALLBACK browseCallbackProc (HWND hWnd, UINT msg, LPARAM lParam, LPARAM lpData)
  28. {
  29. FileChooserCallbackInfo* info = (FileChooserCallbackInfo*) lpData;
  30. if (msg == BFFM_INITIALIZED)
  31. SendMessage (hWnd, BFFM_SETSELECTIONW, TRUE, (LPARAM) info->initialPath.toWideCharPointer());
  32. else if (msg == BFFM_VALIDATEFAILEDW)
  33. info->returnedString = (LPCWSTR) lParam;
  34. else if (msg == BFFM_VALIDATEFAILEDA)
  35. info->returnedString = (const char*) lParam;
  36. return 0;
  37. }
  38. static UINT_PTR CALLBACK openCallback (HWND hdlg, UINT uiMsg, WPARAM /*wParam*/, LPARAM lParam)
  39. {
  40. if (uiMsg == WM_INITDIALOG)
  41. {
  42. Component* customComp = ((FileChooserCallbackInfo*) (((OPENFILENAMEW*) lParam)->lCustData))->customComponent;
  43. HWND dialogH = GetParent (hdlg);
  44. jassert (dialogH != 0);
  45. if (dialogH == 0)
  46. dialogH = hdlg;
  47. RECT r, cr;
  48. GetWindowRect (dialogH, &r);
  49. GetClientRect (dialogH, &cr);
  50. SetWindowPos (dialogH, 0,
  51. r.left, r.top,
  52. customComp->getWidth() + jmax (150, (int) (r.right - r.left)),
  53. jmax (150, (int) (r.bottom - r.top)),
  54. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  55. customComp->setBounds (cr.right, cr.top, customComp->getWidth(), cr.bottom - cr.top);
  56. customComp->addToDesktop (0, dialogH);
  57. }
  58. else if (uiMsg == WM_NOTIFY)
  59. {
  60. LPOFNOTIFY ofn = (LPOFNOTIFY) lParam;
  61. if (ofn->hdr.code == CDN_SELCHANGE)
  62. {
  63. FileChooserCallbackInfo* info = (FileChooserCallbackInfo*) ofn->lpOFN->lCustData;
  64. FilePreviewComponent* comp = dynamic_cast<FilePreviewComponent*> (info->customComponent->getChildComponent(0));
  65. if (comp != nullptr)
  66. {
  67. WCHAR path [MAX_PATH * 2] = { 0 };
  68. CommDlg_OpenSave_GetFilePath (GetParent (hdlg), (LPARAM) &path, MAX_PATH);
  69. comp->selectedFileChanged (File (path));
  70. }
  71. }
  72. }
  73. return 0;
  74. }
  75. class CustomComponentHolder : public Component
  76. {
  77. public:
  78. CustomComponentHolder (Component* const customComp)
  79. {
  80. setVisible (true);
  81. setOpaque (true);
  82. addAndMakeVisible (customComp);
  83. setSize (jlimit (20, 800, customComp->getWidth()), customComp->getHeight());
  84. }
  85. void paint (Graphics& g)
  86. {
  87. g.fillAll (Colours::lightgrey);
  88. }
  89. void resized()
  90. {
  91. Component* const c = getChildComponent(0);
  92. if (c != nullptr)
  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. return true;
  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. HeapBlock<WCHAR> files;
  112. const int charsAvailableForResult = 32768;
  113. files.calloc (charsAvailableForResult + 1);
  114. int filenameOffset = 0;
  115. FileChooserCallbackInfo info;
  116. // use a modal window as the parent for this dialog box
  117. // to block input from other app windows
  118. Component parentWindow (String::empty);
  119. const Rectangle<int> mainMon (Desktop::getInstance().getDisplays().getMainDisplay().userArea);
  120. parentWindow.setBounds (mainMon.getX() + mainMon.getWidth() / 4,
  121. mainMon.getY() + mainMon.getHeight() / 4,
  122. 0, 0);
  123. parentWindow.setOpaque (true);
  124. parentWindow.setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  125. parentWindow.addToDesktop (0);
  126. if (extraInfoComponent == nullptr)
  127. parentWindow.enterModalState();
  128. if (currentFileOrDirectory.isDirectory())
  129. {
  130. info.initialPath = currentFileOrDirectory.getFullPathName();
  131. }
  132. else
  133. {
  134. currentFileOrDirectory.getFileName().copyToUTF16 (files, charsAvailableForResult * sizeof (WCHAR));
  135. info.initialPath = currentFileOrDirectory.getParentDirectory().getFullPathName();
  136. }
  137. if (selectsDirectory)
  138. {
  139. BROWSEINFO bi = { 0 };
  140. bi.hwndOwner = (HWND) parentWindow.getWindowHandle();
  141. bi.pszDisplayName = files;
  142. bi.lpszTitle = title.toWideCharPointer();
  143. bi.lParam = (LPARAM) &info;
  144. bi.lpfn = browseCallbackProc;
  145. #ifdef BIF_USENEWUI
  146. bi.ulFlags = BIF_USENEWUI | BIF_VALIDATE;
  147. #else
  148. bi.ulFlags = 0x50;
  149. #endif
  150. LPITEMIDLIST list = SHBrowseForFolder (&bi);
  151. if (! SHGetPathFromIDListW (list, files))
  152. {
  153. files[0] = 0;
  154. info.returnedString = String::empty;
  155. }
  156. LPMALLOC al;
  157. if (list != 0 && SUCCEEDED (SHGetMalloc (&al)))
  158. al->Free (list);
  159. if (info.returnedString.isNotEmpty())
  160. {
  161. results.add (File (String (files)).getSiblingFile (info.returnedString));
  162. return;
  163. }
  164. }
  165. else
  166. {
  167. DWORD flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
  168. if (warnAboutOverwritingExistingFiles)
  169. flags |= OFN_OVERWRITEPROMPT;
  170. if (selectMultipleFiles)
  171. flags |= OFN_ALLOWMULTISELECT;
  172. if (extraInfoComponent != nullptr)
  173. {
  174. flags |= OFN_ENABLEHOOK;
  175. info.customComponent = new CustomComponentHolder (extraInfoComponent);
  176. info.customComponent->enterModalState();
  177. }
  178. const int filterSpaceNumChars = 2048;
  179. HeapBlock<WCHAR> filters;
  180. filters.calloc (filterSpaceNumChars);
  181. const int bytesWritten = filter.copyToUTF16 (filters.getData(), filterSpaceNumChars * sizeof (WCHAR));
  182. filter.copyToUTF16 (filters + (bytesWritten / sizeof (WCHAR)),
  183. (int) ((filterSpaceNumChars - 1) * sizeof (WCHAR) - bytesWritten));
  184. OPENFILENAMEW of = { 0 };
  185. String localPath (info.initialPath);
  186. #ifdef OPENFILENAME_SIZE_VERSION_400W
  187. of.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
  188. #else
  189. of.lStructSize = sizeof (of);
  190. #endif
  191. of.hwndOwner = (HWND) parentWindow.getWindowHandle();
  192. of.lpstrFilter = filters.getData();
  193. of.nFilterIndex = 1;
  194. of.lpstrFile = files;
  195. of.nMaxFile = charsAvailableForResult;
  196. of.lpstrInitialDir = localPath.toWideCharPointer();
  197. of.lpstrTitle = title.toWideCharPointer();
  198. of.Flags = flags;
  199. of.lCustData = (LPARAM) &info;
  200. if (extraInfoComponent != nullptr)
  201. of.lpfnHook = &openCallback;
  202. if (! (isSaveDialogue ? GetSaveFileName (&of)
  203. : GetOpenFileName (&of)))
  204. return;
  205. filenameOffset = of.nFileOffset;
  206. }
  207. if (selectMultipleFiles && filenameOffset > 0 && files [filenameOffset - 1] == 0)
  208. {
  209. const WCHAR* filename = files + filenameOffset;
  210. while (*filename != 0)
  211. {
  212. results.add (File (String (files) + "\\" + String (filename)));
  213. filename += wcslen (filename) + 1;
  214. }
  215. }
  216. else if (files[0] != 0)
  217. {
  218. results.add (File (String (files)));
  219. }
  220. }