Audio plugin host https://kx.studio/carla
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.

263 lines
9.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. class FileChooserDialogBox::ContentComponent : public Component
  21. {
  22. public:
  23. ContentComponent (const String& name, const String& desc, FileBrowserComponent& chooser)
  24. : Component (name),
  25. chooserComponent (chooser),
  26. okButton (chooser.getActionVerb()),
  27. cancelButton (TRANS ("Cancel")),
  28. newFolderButton (TRANS ("New Folder")),
  29. instructions (desc)
  30. {
  31. addAndMakeVisible (chooserComponent);
  32. addAndMakeVisible (okButton);
  33. okButton.addShortcut (KeyPress (KeyPress::returnKey));
  34. addAndMakeVisible (cancelButton);
  35. cancelButton.addShortcut (KeyPress (KeyPress::escapeKey));
  36. addChildComponent (newFolderButton);
  37. setInterceptsMouseClicks (false, true);
  38. }
  39. void paint (Graphics& g) override
  40. {
  41. text.draw (g, getLocalBounds().reduced (6)
  42. .removeFromTop ((int) text.getHeight()).toFloat());
  43. }
  44. void resized() override
  45. {
  46. const int buttonHeight = 26;
  47. auto area = getLocalBounds();
  48. text.createLayout (getLookAndFeel().createFileChooserHeaderText (getName(), instructions),
  49. (float) getWidth() - 12.0f);
  50. area.removeFromTop (roundToInt (text.getHeight()) + 10);
  51. chooserComponent.setBounds (area.removeFromTop (area.getHeight() - buttonHeight - 20));
  52. auto buttonArea = area.reduced (16, 10);
  53. okButton.changeWidthToFitText (buttonHeight);
  54. okButton.setBounds (buttonArea.removeFromRight (okButton.getWidth() + 16));
  55. buttonArea.removeFromRight (16);
  56. cancelButton.changeWidthToFitText (buttonHeight);
  57. cancelButton.setBounds (buttonArea.removeFromRight (cancelButton.getWidth()));
  58. newFolderButton.changeWidthToFitText (buttonHeight);
  59. newFolderButton.setBounds (buttonArea.removeFromLeft (newFolderButton.getWidth()));
  60. }
  61. FileBrowserComponent& chooserComponent;
  62. TextButton okButton, cancelButton, newFolderButton;
  63. String instructions;
  64. TextLayout text;
  65. };
  66. //==============================================================================
  67. FileChooserDialogBox::FileChooserDialogBox (const String& name,
  68. const String& instructions,
  69. FileBrowserComponent& chooserComponent,
  70. bool shouldWarn,
  71. Colour backgroundColour,
  72. Component* parentComp)
  73. : ResizableWindow (name, backgroundColour, parentComp == nullptr),
  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. if (parentComp != nullptr)
  86. parentComp->addAndMakeVisible (this);
  87. else
  88. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  89. }
  90. FileChooserDialogBox::~FileChooserDialogBox()
  91. {
  92. content->chooserComponent.removeListener (this);
  93. }
  94. //==============================================================================
  95. #if JUCE_MODAL_LOOPS_PERMITTED
  96. bool FileChooserDialogBox::show (int w, int h)
  97. {
  98. return showAt (-1, -1, w, h);
  99. }
  100. bool FileChooserDialogBox::showAt (int x, int y, int w, int h)
  101. {
  102. if (w <= 0) w = getDefaultWidth();
  103. if (h <= 0) h = 500;
  104. if (x < 0 || y < 0)
  105. centreWithSize (w, h);
  106. else
  107. setBounds (x, y, w, h);
  108. const bool ok = (runModalLoop() != 0);
  109. setVisible (false);
  110. return ok;
  111. }
  112. #endif
  113. void FileChooserDialogBox::centreWithDefaultSize (Component* componentToCentreAround)
  114. {
  115. centreAroundComponent (componentToCentreAround, getDefaultWidth(), 500);
  116. }
  117. int FileChooserDialogBox::getDefaultWidth() const
  118. {
  119. if (auto* previewComp = content->chooserComponent.getPreviewComponent())
  120. return 400 + previewComp->getWidth();
  121. return 600;
  122. }
  123. //==============================================================================
  124. void FileChooserDialogBox::closeButtonPressed()
  125. {
  126. setVisible (false);
  127. }
  128. void FileChooserDialogBox::selectionChanged()
  129. {
  130. content->okButton.setEnabled (content->chooserComponent.currentFileIsValid());
  131. content->newFolderButton.setVisible (content->chooserComponent.isSaveMode()
  132. && content->chooserComponent.getRoot().isDirectory());
  133. }
  134. void FileChooserDialogBox::fileDoubleClicked (const File&)
  135. {
  136. selectionChanged();
  137. content->okButton.triggerClick();
  138. }
  139. void FileChooserDialogBox::fileClicked (const File&, const MouseEvent&) {}
  140. void FileChooserDialogBox::browserRootChanged (const File&) {}
  141. void FileChooserDialogBox::okToOverwriteFileCallback (int result, FileChooserDialogBox* box)
  142. {
  143. if (result != 0 && box != nullptr)
  144. box->exitModalState (1);
  145. }
  146. void FileChooserDialogBox::okButtonPressed()
  147. {
  148. if (warnAboutOverwritingExistingFiles
  149. && content->chooserComponent.isSaveMode()
  150. && content->chooserComponent.getSelectedFile(0).exists())
  151. {
  152. AlertWindow::showOkCancelBox (MessageBoxIconType::WarningIcon,
  153. TRANS("File already exists"),
  154. TRANS("There's already a file called: FLNM")
  155. .replace ("FLNM", content->chooserComponent.getSelectedFile(0).getFullPathName())
  156. + "\n\n"
  157. + TRANS("Are you sure you want to overwrite it?"),
  158. TRANS("Overwrite"),
  159. TRANS("Cancel"),
  160. this,
  161. ModalCallbackFunction::forComponent (okToOverwriteFileCallback, this));
  162. }
  163. else
  164. {
  165. exitModalState (1);
  166. }
  167. }
  168. void FileChooserDialogBox::createNewFolderCallback (int result, FileChooserDialogBox* box,
  169. Component::SafePointer<AlertWindow> alert)
  170. {
  171. if (result != 0 && alert != nullptr && box != nullptr)
  172. {
  173. alert->setVisible (false);
  174. box->createNewFolderConfirmed (alert->getTextEditorContents ("Folder Name"));
  175. }
  176. }
  177. void FileChooserDialogBox::createNewFolder()
  178. {
  179. auto parent = content->chooserComponent.getRoot();
  180. if (parent.isDirectory())
  181. {
  182. auto* aw = new AlertWindow (TRANS("New Folder"),
  183. TRANS("Please enter the name for the folder"),
  184. MessageBoxIconType::NoIcon, this);
  185. aw->addTextEditor ("Folder Name", String(), String(), false);
  186. aw->addButton (TRANS("Create Folder"), 1, KeyPress (KeyPress::returnKey));
  187. aw->addButton (TRANS("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  188. aw->enterModalState (true,
  189. ModalCallbackFunction::forComponent (createNewFolderCallback, this,
  190. Component::SafePointer<AlertWindow> (aw)),
  191. true);
  192. }
  193. }
  194. void FileChooserDialogBox::createNewFolderConfirmed (const String& nameFromDialog)
  195. {
  196. auto name = File::createLegalFileName (nameFromDialog);
  197. if (! name.isEmpty())
  198. {
  199. auto parent = content->chooserComponent.getRoot();
  200. if (! parent.getChildFile (name).createDirectory())
  201. AlertWindow::showMessageBoxAsync (MessageBoxIconType::WarningIcon,
  202. TRANS ("New Folder"),
  203. TRANS ("Couldn't create the folder!"));
  204. content->chooserComponent.refresh();
  205. }
  206. }
  207. } // namespace juce