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.

268 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. namespace FileChooserHelpers
  19. {
  20. struct FileChooserCallbackInfo
  21. {
  22. String initialPath;
  23. String returnedString; // need this to get non-existent pathnames from the directory chooser
  24. ScopedPointer<Component> customComponent;
  25. };
  26. static int CALLBACK browseCallbackProc (HWND hWnd, UINT msg, LPARAM lParam, LPARAM lpData)
  27. {
  28. FileChooserCallbackInfo* info = (FileChooserCallbackInfo*) lpData;
  29. if (msg == BFFM_INITIALIZED)
  30. SendMessage (hWnd, BFFM_SETSELECTIONW, TRUE, (LPARAM) info->initialPath.toWideCharPointer());
  31. else if (msg == BFFM_VALIDATEFAILEDW)
  32. info->returnedString = (LPCWSTR) lParam;
  33. else if (msg == BFFM_VALIDATEFAILEDA)
  34. info->returnedString = (const char*) lParam;
  35. return 0;
  36. }
  37. static UINT_PTR CALLBACK openCallback (HWND hdlg, UINT uiMsg, WPARAM /*wParam*/, LPARAM lParam)
  38. {
  39. if (uiMsg == WM_INITDIALOG)
  40. {
  41. Component* customComp = ((FileChooserCallbackInfo*) (((OPENFILENAMEW*) lParam)->lCustData))->customComponent;
  42. HWND dialogH = GetParent (hdlg);
  43. jassert (dialogH != 0);
  44. if (dialogH == 0)
  45. dialogH = hdlg;
  46. RECT r, cr;
  47. GetWindowRect (dialogH, &r);
  48. GetClientRect (dialogH, &cr);
  49. SetWindowPos (dialogH, 0,
  50. r.left, r.top,
  51. customComp->getWidth() + jmax (150, (int) (r.right - r.left)),
  52. jmax (150, (int) (r.bottom - r.top)),
  53. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  54. customComp->setBounds (cr.right, cr.top, customComp->getWidth(), cr.bottom - cr.top);
  55. customComp->addToDesktop (0, dialogH);
  56. }
  57. else if (uiMsg == WM_NOTIFY)
  58. {
  59. LPOFNOTIFY ofn = (LPOFNOTIFY) lParam;
  60. if (ofn->hdr.code == CDN_SELCHANGE)
  61. {
  62. FileChooserCallbackInfo* info = (FileChooserCallbackInfo*) ofn->lpOFN->lCustData;
  63. FilePreviewComponent* comp = dynamic_cast<FilePreviewComponent*> (info->customComponent->getChildComponent(0));
  64. if (comp != nullptr)
  65. {
  66. WCHAR path [MAX_PATH * 2] = { 0 };
  67. CommDlg_OpenSave_GetFilePath (GetParent (hdlg), (LPARAM) &path, MAX_PATH);
  68. comp->selectedFileChanged (File (path));
  69. }
  70. }
  71. }
  72. return 0;
  73. }
  74. class CustomComponentHolder : public Component
  75. {
  76. public:
  77. CustomComponentHolder (Component* const customComp)
  78. {
  79. setVisible (true);
  80. setOpaque (true);
  81. addAndMakeVisible (customComp);
  82. setSize (jlimit (20, 800, customComp->getWidth()), customComp->getHeight());
  83. }
  84. void paint (Graphics& g)
  85. {
  86. g.fillAll (Colours::lightgrey);
  87. }
  88. void resized()
  89. {
  90. Component* const c = getChildComponent(0);
  91. if (c != nullptr)
  92. c->setBounds (getLocalBounds());
  93. }
  94. private:
  95. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomComponentHolder)
  96. };
  97. }
  98. //==============================================================================
  99. bool FileChooser::isPlatformDialogAvailable()
  100. {
  101. return true;
  102. }
  103. void FileChooser::showPlatformDialog (Array<File>& results, const String& title_, const File& currentFileOrDirectory,
  104. const String& filter, bool selectsDirectory, bool /*selectsFiles*/,
  105. bool isSaveDialogue, bool warnAboutOverwritingExistingFiles,
  106. bool selectMultipleFiles, FilePreviewComponent* extraInfoComponent)
  107. {
  108. using namespace FileChooserHelpers;
  109. const String title (title_);
  110. HeapBlock<WCHAR> files;
  111. const size_t charsAvailableForResult = 32768;
  112. files.calloc (charsAvailableForResult + 1);
  113. int filenameOffset = 0;
  114. FileChooserCallbackInfo info;
  115. // use a modal window as the parent for this dialog box
  116. // to block input from other app windows
  117. Component parentWindow (String::empty);
  118. const Rectangle<int> mainMon (Desktop::getInstance().getDisplays().getMainDisplay().userArea);
  119. parentWindow.setBounds (mainMon.getX() + mainMon.getWidth() / 4,
  120. mainMon.getY() + mainMon.getHeight() / 4,
  121. 0, 0);
  122. parentWindow.setOpaque (true);
  123. parentWindow.setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  124. parentWindow.addToDesktop (0);
  125. if (extraInfoComponent == nullptr)
  126. parentWindow.enterModalState();
  127. if (currentFileOrDirectory.isDirectory())
  128. {
  129. info.initialPath = currentFileOrDirectory.getFullPathName();
  130. }
  131. else
  132. {
  133. currentFileOrDirectory.getFileName().copyToUTF16 (files, charsAvailableForResult * sizeof (WCHAR));
  134. info.initialPath = currentFileOrDirectory.getParentDirectory().getFullPathName();
  135. }
  136. if (selectsDirectory)
  137. {
  138. BROWSEINFO bi = { 0 };
  139. bi.hwndOwner = (HWND) parentWindow.getWindowHandle();
  140. bi.pszDisplayName = files;
  141. bi.lpszTitle = title.toWideCharPointer();
  142. bi.lParam = (LPARAM) &info;
  143. bi.lpfn = browseCallbackProc;
  144. #ifdef BIF_USENEWUI
  145. bi.ulFlags = BIF_USENEWUI | BIF_VALIDATE;
  146. #else
  147. bi.ulFlags = 0x50;
  148. #endif
  149. LPITEMIDLIST list = SHBrowseForFolder (&bi);
  150. if (! SHGetPathFromIDListW (list, files))
  151. {
  152. files[0] = 0;
  153. info.returnedString = String::empty;
  154. }
  155. LPMALLOC al;
  156. if (list != 0 && SUCCEEDED (SHGetMalloc (&al)))
  157. al->Free (list);
  158. if (info.returnedString.isNotEmpty())
  159. {
  160. results.add (File (String (files)).getSiblingFile (info.returnedString));
  161. return;
  162. }
  163. }
  164. else
  165. {
  166. DWORD flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
  167. if (warnAboutOverwritingExistingFiles)
  168. flags |= OFN_OVERWRITEPROMPT;
  169. if (selectMultipleFiles)
  170. flags |= OFN_ALLOWMULTISELECT;
  171. if (extraInfoComponent != nullptr)
  172. {
  173. flags |= OFN_ENABLEHOOK;
  174. info.customComponent = new CustomComponentHolder (extraInfoComponent);
  175. info.customComponent->enterModalState();
  176. }
  177. const size_t filterSpaceNumChars = 2048;
  178. HeapBlock<WCHAR> filters;
  179. filters.calloc (filterSpaceNumChars);
  180. const size_t bytesWritten = filter.copyToUTF16 (filters.getData(), filterSpaceNumChars * sizeof (WCHAR));
  181. filter.copyToUTF16 (filters + (bytesWritten / sizeof (WCHAR)),
  182. ((filterSpaceNumChars - 1) * sizeof (WCHAR) - bytesWritten));
  183. OPENFILENAMEW of = { 0 };
  184. String localPath (info.initialPath);
  185. #ifdef OPENFILENAME_SIZE_VERSION_400W
  186. of.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
  187. #else
  188. of.lStructSize = sizeof (of);
  189. #endif
  190. of.hwndOwner = (HWND) parentWindow.getWindowHandle();
  191. of.lpstrFilter = filters.getData();
  192. of.nFilterIndex = 1;
  193. of.lpstrFile = files;
  194. of.nMaxFile = (DWORD) charsAvailableForResult;
  195. of.lpstrInitialDir = localPath.toWideCharPointer();
  196. of.lpstrTitle = title.toWideCharPointer();
  197. of.Flags = flags;
  198. of.lCustData = (LPARAM) &info;
  199. if (extraInfoComponent != nullptr)
  200. of.lpfnHook = &openCallback;
  201. if (! (isSaveDialogue ? GetSaveFileName (&of)
  202. : GetOpenFileName (&of)))
  203. return;
  204. filenameOffset = of.nFileOffset;
  205. }
  206. if (selectMultipleFiles && filenameOffset > 0 && files [filenameOffset - 1] == 0)
  207. {
  208. const WCHAR* filename = files + filenameOffset;
  209. while (*filename != 0)
  210. {
  211. results.add (File (String (files) + "\\" + String (filename)));
  212. filename += wcslen (filename) + 1;
  213. }
  214. }
  215. else if (files[0] != 0)
  216. {
  217. results.add (File (String (files)));
  218. }
  219. }