LMMS
Loading...
Searching...
No Matches
juce_FileChooserDialogBox.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30{
31public:
32 ContentComponent (const String& name, const String& desc, FileBrowserComponent& chooser)
33 : Component (name),
34 chooserComponent (chooser),
35 okButton (chooser.getActionVerb()),
36 cancelButton (TRANS ("Cancel")),
37 newFolderButton (TRANS ("New Folder")),
38 instructions (desc)
39 {
41
44
47
49
50 setInterceptsMouseClicks (false, true);
51 }
52
53 void paint (Graphics& g) override
54 {
55 text.draw (g, getLocalBounds().reduced (6)
56 .removeFromTop ((int) text.getHeight()).toFloat());
57 }
58
59 void resized() override
60 {
61 const int buttonHeight = 26;
62
63 auto area = getLocalBounds();
64
65 text.createLayout (getLookAndFeel().createFileChooserHeaderText (getName(), instructions),
66 (float) getWidth() - 12.0f);
67
68 area.removeFromTop (roundToInt (text.getHeight()) + 10);
69
70 chooserComponent.setBounds (area.removeFromTop (area.getHeight() - buttonHeight - 20));
71 auto buttonArea = area.reduced (16, 10);
72
73 okButton.changeWidthToFitText (buttonHeight);
74 okButton.setBounds (buttonArea.removeFromRight (okButton.getWidth() + 16));
75
76 buttonArea.removeFromRight (16);
77
78 cancelButton.changeWidthToFitText (buttonHeight);
79 cancelButton.setBounds (buttonArea.removeFromRight (cancelButton.getWidth()));
80
81 newFolderButton.changeWidthToFitText (buttonHeight);
82 newFolderButton.setBounds (buttonArea.removeFromLeft (newFolderButton.getWidth()));
83 }
84
89};
90
91//==============================================================================
93 const String& instructions,
94 FileBrowserComponent& chooserComponent,
95 bool shouldWarn,
96 Colour backgroundColour,
97 Component* parentComp)
98 : ResizableWindow (name, backgroundColour, parentComp == nullptr),
100{
101 content = new ContentComponent (name, instructions, chooserComponent);
102 setContentOwned (content, false);
103
104 setResizable (true, true);
105 setResizeLimits (300, 300, 1200, 1000);
106
107 content->okButton.onClick = [this] { okButtonPressed(); };
108 content->cancelButton.onClick = [this] { closeButtonPressed(); };
109 content->newFolderButton.onClick = [this] { createNewFolder(); };
110
111 content->chooserComponent.addListener (this);
112
114
115 if (parentComp != nullptr)
116 parentComp->addAndMakeVisible (this);
117 else
119}
120
122{
123 content->chooserComponent.removeListener (this);
124}
125
126//==============================================================================
127#if JUCE_MODAL_LOOPS_PERMITTED
128bool FileChooserDialogBox::show (int w, int h)
129{
130 return showAt (-1, -1, w, h);
131}
132
133bool FileChooserDialogBox::showAt (int x, int y, int w, int h)
134{
135 if (w <= 0) w = getDefaultWidth();
136 if (h <= 0) h = 500;
137
138 if (x < 0 || y < 0)
139 centreWithSize (w, h);
140 else
141 setBounds (x, y, w, h);
142
143 const bool ok = (runModalLoop() != 0);
144 setVisible (false);
145 return ok;
146}
147#endif
148
150{
151 centreAroundComponent (componentToCentreAround, getDefaultWidth(), 500);
152}
153
155{
156 if (auto* previewComp = content->chooserComponent.getPreviewComponent())
157 return 400 + previewComp->getWidth();
158
159 return 600;
160}
161
162//==============================================================================
167
169{
170 content->okButton.setEnabled (content->chooserComponent.currentFileIsValid());
171
172 content->newFolderButton.setVisible (content->chooserComponent.isSaveMode()
173 && content->chooserComponent.getRoot().isDirectory());
174}
175
177{
179 content->okButton.triggerClick();
180}
181
184
186{
187 if (result != 0 && box != nullptr)
188 box->exitModalState (1);
189}
190
192{
194 && content->chooserComponent.isSaveMode()
195 && content->chooserComponent.getSelectedFile(0).exists())
196 {
198 TRANS("File already exists"),
199 TRANS("There's already a file called: FLNM")
200 .replace ("FLNM", content->chooserComponent.getSelectedFile(0).getFullPathName())
201 + "\n\n"
202 + TRANS("Are you sure you want to overwrite it?"),
203 TRANS("Overwrite"),
204 TRANS("Cancel"),
205 this,
207 }
208 else
209 {
210 exitModalState (1);
211 }
212}
213
216{
217 if (result != 0 && alert != nullptr && box != nullptr)
218 {
219 alert->setVisible (false);
220 box->createNewFolderConfirmed (alert->getTextEditorContents ("Folder Name"));
221 }
222}
223
225{
226 auto parent = content->chooserComponent.getRoot();
227
228 if (parent.isDirectory())
229 {
230 auto* aw = new AlertWindow (TRANS("New Folder"),
231 TRANS("Please enter the name for the folder"),
233
234 aw->addTextEditor ("Folder Name", String(), String(), false);
235 aw->addButton (TRANS("Create Folder"), 1, KeyPress (KeyPress::returnKey));
236 aw->addButton (TRANS("Cancel"), 0, KeyPress (KeyPress::escapeKey));
237
238 aw->enterModalState (true,
241 true);
242 }
243}
244
246{
247 auto name = File::createLegalFileName (nameFromDialog);
248
249 if (! name.isEmpty())
250 {
251 auto parent = content->chooserComponent.getRoot();
252
253 if (! parent.getChildFile (name).createDirectory())
255 TRANS ("New Folder"),
256 TRANS ("Couldn't create the folder!"));
257
258 content->chooserComponent.refresh();
259 }
260}
261
262} // namespace juce
#define nullptr
Definition DistrhoDefines.h:75
static String createLegalFileName(const String &fileNameToFix)
Definition File.cpp:848
Definition juce_AlertWindow.h:45
static bool JUCE_CALLTYPE showOkCancelBox(MessageBoxIconType iconType, const String &title, const String &message, const String &button1Text, const String &button2Text, Component *associatedComponent, ModalComponentManager::Callback *callback)
Definition juce_AlertWindow.cpp:752
static void JUCE_CALLTYPE showMessageBoxAsync(MessageBoxIconType iconType, const String &title, const String &message, const String &buttonText=String(), Component *associatedComponent=nullptr, ModalComponentManager::Callback *callback=nullptr)
Definition juce_AlertWindow.cpp:712
Definition juce_Colour.h:38
Definition juce_Component.h:2287
Definition juce_Component.h:36
void setInterceptsMouseClicks(bool allowClicksOnThisComponent, bool allowClicksOnChildComponents) noexcept
Definition juce_Component.cpp:1420
void addAndMakeVisible(Component *child, int zOrder=-1)
Definition juce_Component.cpp:1554
void setAlwaysOnTop(bool shouldStayOnTop)
Definition juce_Component.cpp:1074
Component() noexcept
Definition juce_Component.cpp:517
void exitModalState(int returnValue)
Definition juce_Component.cpp:1795
int getWidth() const noexcept
Definition juce_Component.h:271
LookAndFeel & getLookAndFeel() const noexcept
Definition juce_Component.cpp:2173
Rectangle< int > getLocalBounds() const noexcept
Definition juce_Component.cpp:2283
void addChildComponent(Component *child, int zOrder=-1)
Definition juce_Component.cpp:1548
virtual void setVisible(bool shouldBeVisible)
Definition juce_Component.cpp:575
String getName() const noexcept
Definition juce_Component.h:76
Definition juce_FileBrowserComponent.h:45
Definition juce_FileChooserDialogBox.cpp:30
TextButton okButton
Definition juce_FileChooserDialogBox.cpp:86
String instructions
Definition juce_FileChooserDialogBox.cpp:87
void paint(Graphics &g) override
Definition juce_FileChooserDialogBox.cpp:53
TextButton newFolderButton
Definition juce_FileChooserDialogBox.cpp:86
FileBrowserComponent & chooserComponent
Definition juce_FileChooserDialogBox.cpp:85
TextButton cancelButton
Definition juce_FileChooserDialogBox.cpp:86
TextLayout text
Definition juce_FileChooserDialogBox.cpp:88
void resized() override
Definition juce_FileChooserDialogBox.cpp:59
ContentComponent(const String &name, const String &desc, FileBrowserComponent &chooser)
Definition juce_FileChooserDialogBox.cpp:32
int getDefaultWidth() const
Definition juce_FileChooserDialogBox.cpp:154
ContentComponent * content
Definition juce_FileChooserDialogBox.h:147
void centreWithDefaultSize(Component *componentToCentreAround=nullptr)
Definition juce_FileChooserDialogBox.cpp:149
~FileChooserDialogBox() override
Definition juce_FileChooserDialogBox.cpp:121
void closeButtonPressed()
Definition juce_FileChooserDialogBox.cpp:163
void fileDoubleClicked(const File &) override
Definition juce_FileChooserDialogBox.cpp:176
const bool warnAboutOverwritingExistingFiles
Definition juce_FileChooserDialogBox.h:148
void createNewFolderConfirmed(const String &name)
Definition juce_FileChooserDialogBox.cpp:245
static void createNewFolderCallback(int result, FileChooserDialogBox *, Component::SafePointer< AlertWindow >)
Definition juce_FileChooserDialogBox.cpp:214
FileChooserDialogBox(const String &title, const String &instructions, FileBrowserComponent &browserComponent, bool warnAboutOverwritingExistingFiles, Colour backgroundColour, Component *parentComponent=nullptr)
Definition juce_FileChooserDialogBox.cpp:92
void createNewFolder()
Definition juce_FileChooserDialogBox.cpp:224
void okButtonPressed()
Definition juce_FileChooserDialogBox.cpp:191
static void okToOverwriteFileCallback(int result, FileChooserDialogBox *)
Definition juce_FileChooserDialogBox.cpp:185
void selectionChanged() override
Definition juce_FileChooserDialogBox.cpp:168
void browserRootChanged(const File &) override
Definition juce_FileChooserDialogBox.cpp:183
void fileClicked(const File &, const MouseEvent &) override
Definition juce_FileChooserDialogBox.cpp:182
Definition juce_File.h:45
Definition juce_GraphicsContext.h:45
Definition juce_KeyPress.h:40
static const int escapeKey
Definition juce_KeyPress.h:190
static const int returnKey
Definition juce_KeyPress.h:191
static ModalComponentManager::Callback * forComponent(void(*functionToCall)(int, ComponentType *), ComponentType *component)
Definition juce_ModalComponentManager.h:276
Definition juce_MouseEvent.h:39
void setResizable(bool shouldBeResizable, bool useBottomRightCornerResizer)
Definition juce_ResizableWindow.cpp:245
void setResizeLimits(int newMinimumWidth, int newMinimumHeight, int newMaximumWidth, int newMaximumHeight) noexcept
Definition juce_ResizableWindow.cpp:291
void setContentOwned(Component *newContentComponent, bool resizeToFitWhenContentChangesSize)
Definition juce_ResizableWindow.cpp:114
ResizableWindow(const String &name, bool addToDesktop)
Definition juce_ResizableWindow.cpp:29
Definition juce_String.h:53
Definition juce_TextButton.h:39
Definition juce_TextLayout.h:41
void centreAroundComponent(Component *componentToCentreAround, int width, int height)
Definition juce_TopLevelWindow.cpp:290
UINT_D64 w
Definition inflate.c:942
int y
Definition inflate.c:1588
int g
Definition inflate.c:1573
unsigned x[BMAX+1]
Definition inflate.c:1586
static const char * name
Definition pugl.h:1582
static uintptr_t parent
Definition pugl.h:1644
#define TRANS(stringLiteral)
Definition juce_LocalisedStrings.h:208
Definition carla_juce.cpp:31
@ WarningIcon
Definition juce_MessageBoxOptions.h:35
@ NoIcon
Definition juce_MessageBoxOptions.h:32
bool juce_areThereAnyAlwaysOnTopWindows()
Definition juce_linux_Windowing.cpp:31
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
Definition jquant2.c:258
uch h[RAND_HEAD_LEN]
Definition crypt.c:459
int result
Definition process.c:1455