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.

264 lines
9.0KB

  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. class FileChooserDialogBox::ContentComponent : public Component
  22. {
  23. public:
  24. ContentComponent (const String& name, const String& desc, FileBrowserComponent& chooser)
  25. : Component (name),
  26. chooserComponent (chooser),
  27. okButton (chooser.getActionVerb()),
  28. cancelButton (TRANS ("Cancel")),
  29. newFolderButton (TRANS ("New Folder")),
  30. instructions (desc)
  31. {
  32. addAndMakeVisible (chooserComponent);
  33. addAndMakeVisible (okButton);
  34. okButton.addShortcut (KeyPress (KeyPress::returnKey));
  35. addAndMakeVisible (cancelButton);
  36. cancelButton.addShortcut (KeyPress (KeyPress::escapeKey));
  37. addChildComponent (newFolderButton);
  38. setInterceptsMouseClicks (false, true);
  39. }
  40. void paint (Graphics& g) override
  41. {
  42. text.draw (g, getLocalBounds().reduced (6)
  43. .removeFromTop ((int) text.getHeight()).toFloat());
  44. }
  45. void resized() override
  46. {
  47. const int buttonHeight = 26;
  48. auto area = getLocalBounds();
  49. text.createLayout (getLookAndFeel().createFileChooserHeaderText (getName(), instructions),
  50. getWidth() - 12.0f);
  51. area.removeFromTop (roundToInt (text.getHeight()) + 10);
  52. chooserComponent.setBounds (area.removeFromTop (area.getHeight() - buttonHeight - 20));
  53. auto buttonArea = area.reduced (16, 10);
  54. okButton.changeWidthToFitText (buttonHeight);
  55. okButton.setBounds (buttonArea.removeFromRight (okButton.getWidth() + 16));
  56. buttonArea.removeFromRight (16);
  57. cancelButton.changeWidthToFitText (buttonHeight);
  58. cancelButton.setBounds (buttonArea.removeFromRight (cancelButton.getWidth()));
  59. newFolderButton.changeWidthToFitText (buttonHeight);
  60. newFolderButton.setBounds (buttonArea.removeFromLeft (newFolderButton.getWidth()));
  61. }
  62. FileBrowserComponent& chooserComponent;
  63. TextButton okButton, cancelButton, newFolderButton;
  64. String instructions;
  65. TextLayout text;
  66. };
  67. //==============================================================================
  68. FileChooserDialogBox::FileChooserDialogBox (const String& name,
  69. const String& instructions,
  70. FileBrowserComponent& chooserComponent,
  71. bool shouldWarn,
  72. Colour backgroundColour)
  73. : ResizableWindow (name, backgroundColour, true),
  74. warnAboutOverwritingExistingFiles (shouldWarn)
  75. {
  76. content = new ContentComponent (name, instructions, chooserComponent);
  77. setContentOwned (content, false);
  78. setResizable (true, true);
  79. setResizeLimits (300, 300, 1200, 1000);
  80. content->okButton.addListener (this);
  81. content->cancelButton.addListener (this);
  82. content->newFolderButton.addListener (this);
  83. content->chooserComponent.addListener (this);
  84. FileChooserDialogBox::selectionChanged();
  85. }
  86. FileChooserDialogBox::~FileChooserDialogBox()
  87. {
  88. content->chooserComponent.removeListener (this);
  89. }
  90. //==============================================================================
  91. #if JUCE_MODAL_LOOPS_PERMITTED
  92. bool FileChooserDialogBox::show (int w, int h)
  93. {
  94. return showAt (-1, -1, w, h);
  95. }
  96. bool FileChooserDialogBox::showAt (int x, int y, int w, int h)
  97. {
  98. if (w <= 0) w = getDefaultWidth();
  99. if (h <= 0) h = 500;
  100. if (x < 0 || y < 0)
  101. centreWithSize (w, h);
  102. else
  103. setBounds (x, y, w, h);
  104. const bool ok = (runModalLoop() != 0);
  105. setVisible (false);
  106. return ok;
  107. }
  108. #endif
  109. void FileChooserDialogBox::centreWithDefaultSize (Component* componentToCentreAround)
  110. {
  111. centreAroundComponent (componentToCentreAround, getDefaultWidth(), 500);
  112. }
  113. int FileChooserDialogBox::getDefaultWidth() const
  114. {
  115. if (auto* previewComp = content->chooserComponent.getPreviewComponent())
  116. return 400 + previewComp->getWidth();
  117. return 600;
  118. }
  119. //==============================================================================
  120. void FileChooserDialogBox::buttonClicked (Button* button)
  121. {
  122. if (button == &(content->okButton)) okButtonPressed();
  123. if (button == &(content->cancelButton)) closeButtonPressed();
  124. if (button == &(content->newFolderButton)) createNewFolder();
  125. }
  126. void FileChooserDialogBox::closeButtonPressed()
  127. {
  128. setVisible (false);
  129. }
  130. void FileChooserDialogBox::selectionChanged()
  131. {
  132. content->okButton.setEnabled (content->chooserComponent.currentFileIsValid());
  133. content->newFolderButton.setVisible (content->chooserComponent.isSaveMode()
  134. && content->chooserComponent.getRoot().isDirectory());
  135. }
  136. void FileChooserDialogBox::fileDoubleClicked (const File&)
  137. {
  138. selectionChanged();
  139. content->okButton.triggerClick();
  140. }
  141. void FileChooserDialogBox::fileClicked (const File&, const MouseEvent&) {}
  142. void FileChooserDialogBox::browserRootChanged (const File&) {}
  143. void FileChooserDialogBox::okToOverwriteFileCallback (int result, FileChooserDialogBox* box)
  144. {
  145. if (result != 0 && box != nullptr)
  146. box->exitModalState (1);
  147. }
  148. void FileChooserDialogBox::okButtonPressed()
  149. {
  150. if (warnAboutOverwritingExistingFiles
  151. && content->chooserComponent.isSaveMode()
  152. && content->chooserComponent.getSelectedFile(0).exists())
  153. {
  154. AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  155. TRANS("File already exists"),
  156. TRANS("There's already a file called: FLNM")
  157. .replace ("FLNM", content->chooserComponent.getSelectedFile(0).getFullPathName())
  158. + "\n\n"
  159. + TRANS("Are you sure you want to overwrite it?"),
  160. TRANS("Overwrite"),
  161. TRANS("Cancel"),
  162. this,
  163. ModalCallbackFunction::forComponent (okToOverwriteFileCallback, this));
  164. }
  165. else
  166. {
  167. exitModalState (1);
  168. }
  169. }
  170. void FileChooserDialogBox::createNewFolderCallback (int result, FileChooserDialogBox* box,
  171. Component::SafePointer<AlertWindow> alert)
  172. {
  173. if (result != 0 && alert != nullptr && box != nullptr)
  174. {
  175. alert->setVisible (false);
  176. box->createNewFolderConfirmed (alert->getTextEditorContents ("Folder Name"));
  177. }
  178. }
  179. void FileChooserDialogBox::createNewFolder()
  180. {
  181. auto parent = content->chooserComponent.getRoot();
  182. if (parent.isDirectory())
  183. {
  184. auto* aw = new AlertWindow (TRANS("New Folder"),
  185. TRANS("Please enter the name for the folder"),
  186. AlertWindow::NoIcon, this);
  187. aw->addTextEditor ("Folder Name", String(), String(), false);
  188. aw->addButton (TRANS("Create Folder"), 1, KeyPress (KeyPress::returnKey));
  189. aw->addButton (TRANS("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  190. aw->enterModalState (true,
  191. ModalCallbackFunction::forComponent (createNewFolderCallback, this,
  192. Component::SafePointer<AlertWindow> (aw)),
  193. true);
  194. }
  195. }
  196. void FileChooserDialogBox::createNewFolderConfirmed (const String& nameFromDialog)
  197. {
  198. auto name = File::createLegalFileName (nameFromDialog);
  199. if (! name.isEmpty())
  200. {
  201. auto parent = content->chooserComponent.getRoot();
  202. if (! parent.getChildFile (name).createDirectory())
  203. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  204. TRANS ("New Folder"),
  205. TRANS ("Couldn't create the folder!"));
  206. content->chooserComponent.refresh();
  207. }
  208. }
  209. } // namespace juce