1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
/*
==============================================================================
This file is part of the JUCE examples.
Copyright (c) 2017 - ROLI Ltd.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
/*******************************************************************************
The block below describes the properties of this PIP. A PIP is a short snippet
of code that can be read by the Projucer and used to generate a JUCE project.
BEGIN_JUCE_PIP_METADATA
name: DialogsDemo
version: 1.0.0
vendor: JUCE
website: http://juce.com
description: Displays different types of dialog windows.
dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
juce_gui_basics, juce_gui_extra
exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
type: Component
mainClass: DialogsDemo
useLocalCopy: 1
END_JUCE_PIP_METADATA
*******************************************************************************/
#pragma once
#include "../Assets/DemoUtilities.h"
//==============================================================================
class DemoBackgroundThread : public ThreadWithProgressWindow
{
public:
DemoBackgroundThread()
: ThreadWithProgressWindow ("busy doing some important things...", true, true)
{
setStatusMessage ("Getting ready...");
}
void run() override
{
setProgress (-1.0); // setting a value beyond the range 0 -> 1 will show a spinning bar..
setStatusMessage ("Preparing to do some stuff...");
wait (2000);
int thingsToDo = 10;
for (int i = 0; i < thingsToDo; ++i)
{
// must check this as often as possible, because this is
// how we know if the user's pressed 'cancel'
if (threadShouldExit())
return;
// this will update the progress bar on the dialog box
setProgress (i / (double) thingsToDo);
setStatusMessage (String (thingsToDo - i) + " things left to do...");
wait (500);
}
setProgress (-1.0); // setting a value beyond the range 0 -> 1 will show a spinning bar..
setStatusMessage ("Finishing off the last few bits and pieces!");
wait (2000);
}
// This method gets called on the message thread once our thread has finished..
void threadComplete (bool userPressedCancel) override
{
if (userPressedCancel)
{
AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
"Progress window",
"You pressed cancel!");
}
else
{
// thread finished normally..
AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
"Progress window",
"Thread finished ok!");
}
// ..and clean up by deleting our thread object..
delete this;
}
};
//==============================================================================
class DialogsDemo : public Component
{
public:
enum DialogType
{
plainAlertWindow,
warningAlertWindow,
infoAlertWindow,
questionAlertWindow,
okCancelAlertWindow,
extraComponentsAlertWindow,
calloutBoxWindow,
progressWindow,
loadChooser,
loadWithPreviewChooser,
directoryChooser,
saveChooser,
shareText,
shareFile,
shareImage,
numDialogs
};
DialogsDemo()
{
setOpaque (true);
addAndMakeVisible (nativeButton);
nativeButton.setButtonText ("Use Native Windows");
nativeButton.onClick = [this] { getLookAndFeel().setUsingNativeAlertWindows (nativeButton.getToggleState()); };
StringArray windowNames { "Plain Alert Window", "Alert Window With Warning Icon", "Alert Window With Info Icon", "Alert Window With Question Icon",
"OK Cancel Alert Window", "Alert Window With Extra Components", "CalloutBox", "Thread With Progress Window",
"'Load' File Browser", "'Load' File Browser With Image Preview", "'Choose Directory' File Browser", "'Save' File Browser",
"Share Text", "Share Files", "Share Images" };
// warn in case we add any windows
jassert (windowNames.size() == numDialogs);
for (auto windowName : windowNames)
{
auto* newButton = new TextButton();
addAndMakeVisible (windowButtons.add (newButton));
newButton->setButtonText (windowName);
auto index = windowNames.indexOf (windowName);
newButton->onClick = [this, index, newButton] { showWindow (*newButton, static_cast<DialogType> (index)); };
}
setSize (500, 500);
RuntimePermissions::request (RuntimePermissions::readExternalStorage,
[] (bool granted)
{
if (! granted)
{
AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
"Permissions warning",
"External storage access permission not granted, some files"
" may be inaccessible.");
}
});
}
//==============================================================================
void paint (Graphics& g) override
{
g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
}
void resized() override
{
auto area = getLocalBounds().reduced (5, 15);
Rectangle<int> topRow;
for (auto* b : windowButtons)
{
auto index = windowButtons.indexOf (b);
if (topRow.getWidth() < 10 || index == loadChooser)
topRow = area.removeFromTop (26);
if (index == progressWindow)
area.removeFromTop (20);
b->setBounds (topRow.removeFromLeft (area.getWidth() / 2).reduced (4, 2));
}
area.removeFromTop (15);
nativeButton.setBounds (area.removeFromTop (24));
}
private:
OwnedArray<TextButton> windowButtons;
ToggleButton nativeButton;
static void alertBoxResultChosen (int result, DialogsDemo*)
{
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon, "Alert Box",
"Result code: " + String (result));
}
void showWindow (Component& button, DialogType type)
{
if (type >= plainAlertWindow && type <= questionAlertWindow)
{
AlertWindow::AlertIconType icon = AlertWindow::NoIcon;
switch (type)
{
case warningAlertWindow: icon = AlertWindow::WarningIcon; break;
case infoAlertWindow: icon = AlertWindow::InfoIcon; break;
case questionAlertWindow: icon = AlertWindow::QuestionIcon; break;
default: break;
}
AlertWindow::showMessageBoxAsync (icon, "This is an AlertWindow",
"And this is the AlertWindow's message. Blah blah blah blah blah blah blah blah blah blah blah blah blah.",
"OK");
}
else if (type == okCancelAlertWindow)
{
AlertWindow::showOkCancelBox (AlertWindow::QuestionIcon, "This is an ok/cancel AlertWindow",
"And this is the AlertWindow's message. Blah blah blah blah blah blah blah blah blah blah blah blah blah.",
{}, {}, {},
ModalCallbackFunction::forComponent (alertBoxResultChosen, this));
}
else if (type == calloutBoxWindow)
{
auto* colourSelector = new ColourSelector();
colourSelector->setName ("background");
colourSelector->setCurrentColour (findColour (TextButton::buttonColourId));
colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
colourSelector->setSize (300, 400);
CallOutBox::launchAsynchronously (colourSelector, button.getScreenBounds(), nullptr);
}
else if (type == extraComponentsAlertWindow)
{
#if JUCE_MODAL_LOOPS_PERMITTED
AlertWindow w ("AlertWindow demo..",
"This AlertWindow has a couple of extra components added to show how to add drop-down lists and text entry boxes.",
AlertWindow::QuestionIcon);
w.addTextEditor ("text", "enter some text here", "text field:");
w.addComboBox ("option", { "option 1", "option 2", "option 3", "option 4" }, "some options");
w.addButton ("OK", 1, KeyPress (KeyPress::returnKey, 0, 0));
w.addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey, 0, 0));
if (w.runModalLoop() != 0) // is they picked 'ok'
{
// this is the item they chose in the drop-down list..
auto optionIndexChosen = w.getComboBoxComponent ("option")->getSelectedItemIndex();
ignoreUnused (optionIndexChosen);
// this is the text they entered..
auto text = w.getTextEditorContents ("text");
}
#endif
}
else if (type == progressWindow)
{
// This will launch our ThreadWithProgressWindow in a modal state. (Our subclass
// will take care of deleting the object when the task has finished)
(new DemoBackgroundThread())->launchThread();
}
else if (type >= loadChooser && type <= saveChooser)
{
auto useNativeVersion = nativeButton.getToggleState();
if (type == loadChooser)
{
fc.reset (new FileChooser ("Choose a file to open...", File::getCurrentWorkingDirectory(),
"*", useNativeVersion));
fc->launchAsync (FileBrowserComponent::canSelectMultipleItems | FileBrowserComponent::openMode
| FileBrowserComponent::canSelectFiles,
[] (const FileChooser& chooser)
{
String chosen;
auto results = chooser.getURLResults();
for (auto result : results)
chosen << (result.isLocalFile() ? result.getLocalFile().getFullPathName()
: result.toString (false)) << "\n";
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
"File Chooser...",
"You picked: " + chosen);
});
}
else if (type == loadWithPreviewChooser)
{
imagePreview.setSize (200, 200);
fc.reset (new FileChooser ("Choose an image to open...", File::getCurrentWorkingDirectory(),
"*.jpg;*.jpeg;*.png;*.gif", useNativeVersion));
fc->launchAsync (FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles
| FileBrowserComponent::canSelectMultipleItems,
[] (const FileChooser& chooser)
{
String chosen;
auto results = chooser.getURLResults();
for (auto result : results)
chosen << (result.isLocalFile() ? result.getLocalFile().getFullPathName()
: result.toString (false)) << "\n";
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
"File Chooser...",
"You picked: " + chosen);
},
&imagePreview);
}
else if (type == saveChooser)
{
auto fileToSave = File::createTempFile ("saveChooserDemo");
if (fileToSave.createDirectory().wasOk())
{
fileToSave = fileToSave.getChildFile ("JUCE.png");
fileToSave.deleteFile();
FileOutputStream outStream (fileToSave);
if (outStream.openedOk())
if (auto inStream = std::unique_ptr<InputStream> (createAssetInputStream ("juce_icon.png")))
outStream.writeFromInputStream (*inStream, -1);
}
fc.reset (new FileChooser ("Choose a file to save...",
File::getCurrentWorkingDirectory().getChildFile (fileToSave.getFileName()),
"*", useNativeVersion));
fc->launchAsync (FileBrowserComponent::saveMode | FileBrowserComponent::canSelectFiles,
[fileToSave] (const FileChooser& chooser)
{
auto result = chooser.getURLResult();
auto name = result.isEmpty() ? String()
: (result.isLocalFile() ? result.getLocalFile().getFullPathName()
: result.toString (true));
// Android and iOS file choosers will create placeholder files for chosen
// paths, so we may as well write into those files.
#if JUCE_ANDROID || JUCE_IOS
if (! result.isEmpty())
{
std::unique_ptr<InputStream> wi (fileToSave.createInputStream());
std::unique_ptr<OutputStream> wo (result.createOutputStream());
if (wi.get() != nullptr && wo.get() != nullptr)
{
auto numWritten = wo->writeFromInputStream (*wi, -1);
jassert (numWritten > 0);
ignoreUnused (numWritten);
wo->flush();
}
}
#endif
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
"File Chooser...",
"You picked: " + name);
});
}
else if (type == directoryChooser)
{
fc.reset (new FileChooser ("Choose a directory...",
File::getCurrentWorkingDirectory(),
"*",
useNativeVersion));
fc->launchAsync (FileBrowserComponent::openMode | FileBrowserComponent::canSelectDirectories,
[] (const FileChooser& chooser)
{
auto result = chooser.getURLResult();
auto name = result.isLocalFile() ? result.getLocalFile().getFullPathName()
: result.toString (true);
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
"File Chooser...",
"You picked: " + name);
});
}
}
else if (type == shareText)
{
ContentSharer::getInstance()->shareText ("I love JUCE!",
[] (bool success, const String& error)
{
auto resultString = success ? String ("success") : ("failure\n (error: " + error + ")");
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon, "Sharing Text Result",
"Sharing text finished\nwith " + resultString);
});
}
else if (type == shareFile)
{
File fileToSave = File::createTempFile ("DialogsDemoSharingTest");
if (fileToSave.createDirectory().wasOk())
{
fileToSave = fileToSave.getChildFile ("SharingDemoFile.txt");
fileToSave.replaceWithText ("Make it fast!");
Array<URL> urls;
urls.add ({ fileToSave.getFullPathName() });
ContentSharer::getInstance()->shareFiles (urls,
[] (bool success, const String& error)
{
auto resultString = success ? String ("success") : ("failure\n (error: " + error + ")");
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
"Sharing Files Result",
"Sharing files finished\nwith " + resultString);
});
}
}
else if (type == shareImage)
{
auto myImage = getImageFromAssets ("juce_icon.png");
Image myImage2 (Image::RGB, 500, 500, true);
Graphics g (myImage2);
g.setColour (Colours::green);
ColourGradient gradient (Colours::yellow, 170, 170, Colours::cyan, 170, 20, true);
g.setGradientFill (gradient);
g.fillEllipse (20, 20, 300, 300);
Array<Image> images { myImage, myImage2 };
ContentSharer::getInstance()->shareImages (images,
[] (bool success, const String& error)
{
String resultString = success ? String ("success")
: ("failure\n (error: " + error + ")");
AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon, "Sharing Images Result",
"Sharing images finished\nwith " + resultString);
});
}
}
ImagePreviewComponent imagePreview;
std::unique_ptr<FileChooser> fc;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DialogsDemo)
};
|