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.

256 lines
8.8KB

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