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.

262 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. Component* parentComponent)
  74. : ResizableWindow (name, backgroundColour, parentComponent == nullptr),
  75. warnAboutOverwritingExistingFiles (shouldWarn)
  76. {
  77. content = new ContentComponent (name, instructions, chooserComponent);
  78. setContentOwned (content, false);
  79. setResizable (true, true);
  80. setResizeLimits (300, 300, 1200, 1000);
  81. content->okButton.onClick = [this] { okButtonPressed(); };
  82. content->cancelButton.onClick = [this] { closeButtonPressed(); };
  83. content->newFolderButton.onClick = [this] { createNewFolder(); };
  84. content->chooserComponent.addListener (this);
  85. FileChooserDialogBox::selectionChanged();
  86. if (parentComponent != nullptr)
  87. parentComponent->addAndMakeVisible (this);
  88. }
  89. FileChooserDialogBox::~FileChooserDialogBox()
  90. {
  91. content->chooserComponent.removeListener (this);
  92. }
  93. //==============================================================================
  94. #if JUCE_MODAL_LOOPS_PERMITTED
  95. bool FileChooserDialogBox::show (int w, int h)
  96. {
  97. return showAt (-1, -1, w, h);
  98. }
  99. bool FileChooserDialogBox::showAt (int x, int y, int w, int h)
  100. {
  101. if (w <= 0) w = getDefaultWidth();
  102. if (h <= 0) h = 500;
  103. if (x < 0 || y < 0)
  104. centreWithSize (w, h);
  105. else
  106. setBounds (x, y, w, h);
  107. const bool ok = (runModalLoop() != 0);
  108. setVisible (false);
  109. return ok;
  110. }
  111. #endif
  112. void FileChooserDialogBox::centreWithDefaultSize (Component* componentToCentreAround)
  113. {
  114. centreAroundComponent (componentToCentreAround, getDefaultWidth(), 500);
  115. }
  116. int FileChooserDialogBox::getDefaultWidth() const
  117. {
  118. if (auto* previewComp = content->chooserComponent.getPreviewComponent())
  119. return 400 + previewComp->getWidth();
  120. return 600;
  121. }
  122. //==============================================================================
  123. void FileChooserDialogBox::closeButtonPressed()
  124. {
  125. setVisible (false);
  126. }
  127. void FileChooserDialogBox::selectionChanged()
  128. {
  129. content->okButton.setEnabled (content->chooserComponent.currentFileIsValid());
  130. content->newFolderButton.setVisible (content->chooserComponent.isSaveMode()
  131. && content->chooserComponent.getRoot().isDirectory());
  132. }
  133. void FileChooserDialogBox::fileDoubleClicked (const File&)
  134. {
  135. selectionChanged();
  136. content->okButton.triggerClick();
  137. }
  138. void FileChooserDialogBox::fileClicked (const File&, const MouseEvent&) {}
  139. void FileChooserDialogBox::browserRootChanged (const File&) {}
  140. void FileChooserDialogBox::okToOverwriteFileCallback (int result, FileChooserDialogBox* box)
  141. {
  142. if (result != 0 && box != nullptr)
  143. box->exitModalState (1);
  144. }
  145. void FileChooserDialogBox::okButtonPressed()
  146. {
  147. if (warnAboutOverwritingExistingFiles
  148. && content->chooserComponent.isSaveMode()
  149. && content->chooserComponent.getSelectedFile(0).exists())
  150. {
  151. AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  152. TRANS("File already exists"),
  153. TRANS("There's already a file called: FLNM")
  154. .replace ("FLNM", content->chooserComponent.getSelectedFile(0).getFullPathName())
  155. + "\n\n"
  156. + TRANS("Are you sure you want to overwrite it?"),
  157. TRANS("Overwrite"),
  158. TRANS("Cancel"),
  159. this,
  160. ModalCallbackFunction::forComponent (okToOverwriteFileCallback, this));
  161. }
  162. else
  163. {
  164. exitModalState (1);
  165. }
  166. }
  167. void FileChooserDialogBox::createNewFolderCallback (int result, FileChooserDialogBox* box,
  168. Component::SafePointer<AlertWindow> alert)
  169. {
  170. if (result != 0 && alert != nullptr && box != nullptr)
  171. {
  172. alert->setVisible (false);
  173. box->createNewFolderConfirmed (alert->getTextEditorContents ("Folder Name"));
  174. }
  175. }
  176. void FileChooserDialogBox::createNewFolder()
  177. {
  178. auto parent = content->chooserComponent.getRoot();
  179. if (parent.isDirectory())
  180. {
  181. auto* aw = new AlertWindow (TRANS("New Folder"),
  182. TRANS("Please enter the name for the folder"),
  183. AlertWindow::NoIcon, this);
  184. aw->addTextEditor ("Folder Name", String(), String(), false);
  185. aw->addButton (TRANS("Create Folder"), 1, KeyPress (KeyPress::returnKey));
  186. aw->addButton (TRANS("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  187. aw->enterModalState (true,
  188. ModalCallbackFunction::forComponent (createNewFolderCallback, this,
  189. Component::SafePointer<AlertWindow> (aw)),
  190. true);
  191. }
  192. }
  193. void FileChooserDialogBox::createNewFolderConfirmed (const String& nameFromDialog)
  194. {
  195. auto name = File::createLegalFileName (nameFromDialog);
  196. if (! name.isEmpty())
  197. {
  198. auto parent = content->chooserComponent.getRoot();
  199. if (! parent.getChildFile (name).createDirectory())
  200. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  201. TRANS ("New Folder"),
  202. TRANS ("Couldn't create the folder!"));
  203. content->chooserComponent.refresh();
  204. }
  205. }
  206. } // namespace juce