|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2017 - ROLI Ltd.
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- By using JUCE, you agree to the terms of both the JUCE 5 End-User License
- Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
- 27th April 2017).
-
- End User License Agreement: www.juce.com/juce-5-licence
- Privacy Policy: www.juce.com/juce-5-privacy-policy
-
- Or: You may also use this code under the terms of the GPL v3 (see
- www.gnu.org/licenses).
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- class FileChooserDialogBox::ContentComponent : public Component
- {
- public:
- ContentComponent (const String& name, const String& desc, FileBrowserComponent& chooser)
- : Component (name),
- chooserComponent (chooser),
- okButton (chooser.getActionVerb()),
- cancelButton (TRANS ("Cancel")),
- newFolderButton (TRANS ("New Folder")),
- instructions (desc)
- {
- addAndMakeVisible (chooserComponent);
-
- addAndMakeVisible (okButton);
- okButton.addShortcut (KeyPress (KeyPress::returnKey));
-
- addAndMakeVisible (cancelButton);
- cancelButton.addShortcut (KeyPress (KeyPress::escapeKey));
-
- addChildComponent (newFolderButton);
-
- setInterceptsMouseClicks (false, true);
- }
-
- void paint (Graphics& g) override
- {
- text.draw (g, getLocalBounds().reduced (6)
- .removeFromTop ((int) text.getHeight()).toFloat());
- }
-
- void resized() override
- {
- const int buttonHeight = 26;
-
- Rectangle<int> area (getLocalBounds());
-
- text.createLayout (getLookAndFeel().createFileChooserHeaderText (getName(), instructions),
- getWidth() - 12.0f);
-
- area.removeFromTop (roundToInt (text.getHeight()) + 10);
-
- chooserComponent.setBounds (area.removeFromTop (area.getHeight() - buttonHeight - 20));
- Rectangle<int> buttonArea (area.reduced (16, 10));
-
- okButton.changeWidthToFitText (buttonHeight);
- okButton.setBounds (buttonArea.removeFromRight (okButton.getWidth() + 16));
-
- buttonArea.removeFromRight (16);
-
- cancelButton.changeWidthToFitText (buttonHeight);
- cancelButton.setBounds (buttonArea.removeFromRight (cancelButton.getWidth()));
-
- newFolderButton.changeWidthToFitText (buttonHeight);
- newFolderButton.setBounds (buttonArea.removeFromLeft (newFolderButton.getWidth()));
- }
-
- FileBrowserComponent& chooserComponent;
- TextButton okButton, cancelButton, newFolderButton;
-
- private:
- String instructions;
- TextLayout text;
- };
-
- //==============================================================================
- FileChooserDialogBox::FileChooserDialogBox (const String& name,
- const String& instructions,
- FileBrowserComponent& chooserComponent,
- const bool warnAboutOverwritingExistingFiles_,
- Colour backgroundColour)
- : ResizableWindow (name, backgroundColour, true),
- warnAboutOverwritingExistingFiles (warnAboutOverwritingExistingFiles_)
- {
- content = new ContentComponent (name, instructions, chooserComponent);
- setContentOwned (content, false);
-
- setResizable (true, true);
- setResizeLimits (300, 300, 1200, 1000);
-
- content->okButton.addListener (this);
- content->cancelButton.addListener (this);
- content->newFolderButton.addListener (this);
- content->chooserComponent.addListener (this);
-
- FileChooserDialogBox::selectionChanged();
- }
-
- FileChooserDialogBox::~FileChooserDialogBox()
- {
- content->chooserComponent.removeListener (this);
- }
-
- //==============================================================================
- #if JUCE_MODAL_LOOPS_PERMITTED
- bool FileChooserDialogBox::show (int w, int h)
- {
- return showAt (-1, -1, w, h);
- }
-
- bool FileChooserDialogBox::showAt (int x, int y, int w, int h)
- {
- if (w <= 0) w = getDefaultWidth();
- if (h <= 0) h = 500;
-
- if (x < 0 || y < 0)
- centreWithSize (w, h);
- else
- setBounds (x, y, w, h);
-
- const bool ok = (runModalLoop() != 0);
- setVisible (false);
- return ok;
- }
- #endif
-
- void FileChooserDialogBox::centreWithDefaultSize (Component* componentToCentreAround)
- {
- centreAroundComponent (componentToCentreAround, getDefaultWidth(), 500);
- }
-
- int FileChooserDialogBox::getDefaultWidth() const
- {
- if (Component* const previewComp = content->chooserComponent.getPreviewComponent())
- return 400 + previewComp->getWidth();
-
- return 600;
- }
-
- //==============================================================================
- void FileChooserDialogBox::buttonClicked (Button* button)
- {
- if (button == &(content->okButton))
- {
- okButtonPressed();
- }
- else if (button == &(content->cancelButton))
- {
- closeButtonPressed();
- }
- else if (button == &(content->newFolderButton))
- {
- createNewFolder();
- }
- }
-
- void FileChooserDialogBox::closeButtonPressed()
- {
- setVisible (false);
- }
-
- void FileChooserDialogBox::selectionChanged()
- {
- content->okButton.setEnabled (content->chooserComponent.currentFileIsValid());
-
- content->newFolderButton.setVisible (content->chooserComponent.isSaveMode()
- && content->chooserComponent.getRoot().isDirectory());
- }
-
- void FileChooserDialogBox::fileDoubleClicked (const File&)
- {
- selectionChanged();
- content->okButton.triggerClick();
- }
-
- void FileChooserDialogBox::fileClicked (const File&, const MouseEvent&) {}
- void FileChooserDialogBox::browserRootChanged (const File&) {}
-
- void FileChooserDialogBox::okToOverwriteFileCallback (int result, FileChooserDialogBox* box)
- {
- if (result != 0 && box != nullptr)
- box->exitModalState (1);
- }
-
- void FileChooserDialogBox::okButtonPressed()
- {
- if (warnAboutOverwritingExistingFiles
- && content->chooserComponent.isSaveMode()
- && content->chooserComponent.getSelectedFile(0).exists())
- {
- AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
- TRANS("File already exists"),
- TRANS("There's already a file called: FLNM")
- .replace ("FLNM", content->chooserComponent.getSelectedFile(0).getFullPathName())
- + "\n\n"
- + TRANS("Are you sure you want to overwrite it?"),
- TRANS("Overwrite"),
- TRANS("Cancel"),
- this,
- ModalCallbackFunction::forComponent (okToOverwriteFileCallback, this));
- }
- else
- {
- exitModalState (1);
- }
- }
-
- void FileChooserDialogBox::createNewFolderCallback (int result, FileChooserDialogBox* box,
- Component::SafePointer<AlertWindow> alert)
- {
- if (result != 0 && alert != nullptr && box != nullptr)
- {
- alert->setVisible (false);
- box->createNewFolderConfirmed (alert->getTextEditorContents ("Folder Name"));
- }
- }
-
- void FileChooserDialogBox::createNewFolder()
- {
- File parent (content->chooserComponent.getRoot());
-
- if (parent.isDirectory())
- {
- AlertWindow* aw = new AlertWindow (TRANS("New Folder"),
- TRANS("Please enter the name for the folder"),
- AlertWindow::NoIcon, this);
-
- aw->addTextEditor ("Folder Name", String(), String(), false);
- aw->addButton (TRANS("Create Folder"), 1, KeyPress (KeyPress::returnKey));
- aw->addButton (TRANS("Cancel"), 0, KeyPress (KeyPress::escapeKey));
-
- aw->enterModalState (true,
- ModalCallbackFunction::forComponent (createNewFolderCallback, this,
- Component::SafePointer<AlertWindow> (aw)),
- true);
- }
- }
-
- void FileChooserDialogBox::createNewFolderConfirmed (const String& nameFromDialog)
- {
- const String name (File::createLegalFileName (nameFromDialog));
-
- if (! name.isEmpty())
- {
- const File parent (content->chooserComponent.getRoot());
-
- if (! parent.getChildFile (name).createDirectory())
- {
- AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
- TRANS ("New Folder"),
- TRANS ("Couldn't create the folder!"));
- }
-
- content->chooserComponent.refresh();
- }
- }
-
- } // namespace juce
|