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.

258 lines
8.8KB

  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.onClick = [this] { okButtonPressed(); };
  81. content->cancelButton.onClick = [this] { closeButtonPressed(); };
  82. content->newFolderButton.onClick = [this] { createNewFolder(); };
  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::closeButtonPressed()
  121. {
  122. setVisible (false);
  123. }
  124. void FileChooserDialogBox::selectionChanged()
  125. {
  126. content->okButton.setEnabled (content->chooserComponent.currentFileIsValid());
  127. content->newFolderButton.setVisible (content->chooserComponent.isSaveMode()
  128. && content->chooserComponent.getRoot().isDirectory());
  129. }
  130. void FileChooserDialogBox::fileDoubleClicked (const File&)
  131. {
  132. selectionChanged();
  133. content->okButton.triggerClick();
  134. }
  135. void FileChooserDialogBox::fileClicked (const File&, const MouseEvent&) {}
  136. void FileChooserDialogBox::browserRootChanged (const File&) {}
  137. void FileChooserDialogBox::okToOverwriteFileCallback (int result, FileChooserDialogBox* box)
  138. {
  139. if (result != 0 && box != nullptr)
  140. box->exitModalState (1);
  141. }
  142. void FileChooserDialogBox::okButtonPressed()
  143. {
  144. if (warnAboutOverwritingExistingFiles
  145. && content->chooserComponent.isSaveMode()
  146. && content->chooserComponent.getSelectedFile(0).exists())
  147. {
  148. AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  149. TRANS("File already exists"),
  150. TRANS("There's already a file called: FLNM")
  151. .replace ("FLNM", content->chooserComponent.getSelectedFile(0).getFullPathName())
  152. + "\n\n"
  153. + TRANS("Are you sure you want to overwrite it?"),
  154. TRANS("Overwrite"),
  155. TRANS("Cancel"),
  156. this,
  157. ModalCallbackFunction::forComponent (okToOverwriteFileCallback, this));
  158. }
  159. else
  160. {
  161. exitModalState (1);
  162. }
  163. }
  164. void FileChooserDialogBox::createNewFolderCallback (int result, FileChooserDialogBox* box,
  165. Component::SafePointer<AlertWindow> alert)
  166. {
  167. if (result != 0 && alert != nullptr && box != nullptr)
  168. {
  169. alert->setVisible (false);
  170. box->createNewFolderConfirmed (alert->getTextEditorContents ("Folder Name"));
  171. }
  172. }
  173. void FileChooserDialogBox::createNewFolder()
  174. {
  175. auto parent = content->chooserComponent.getRoot();
  176. if (parent.isDirectory())
  177. {
  178. auto* aw = new AlertWindow (TRANS("New Folder"),
  179. TRANS("Please enter the name for the folder"),
  180. AlertWindow::NoIcon, this);
  181. aw->addTextEditor ("Folder Name", String(), String(), false);
  182. aw->addButton (TRANS("Create Folder"), 1, KeyPress (KeyPress::returnKey));
  183. aw->addButton (TRANS("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  184. aw->enterModalState (true,
  185. ModalCallbackFunction::forComponent (createNewFolderCallback, this,
  186. Component::SafePointer<AlertWindow> (aw)),
  187. true);
  188. }
  189. }
  190. void FileChooserDialogBox::createNewFolderConfirmed (const String& nameFromDialog)
  191. {
  192. auto name = File::createLegalFileName (nameFromDialog);
  193. if (! name.isEmpty())
  194. {
  195. auto parent = content->chooserComponent.getRoot();
  196. if (! parent.getChildFile (name).createDirectory())
  197. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  198. TRANS ("New Folder"),
  199. TRANS ("Couldn't create the folder!"));
  200. content->chooserComponent.refresh();
  201. }
  202. }
  203. } // namespace juce