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.

juce_FileChooserDialogBox.cpp 8.7KB

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