The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

1371 lines
52KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: WidgetsDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Showcases various widgets.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: WidgetsDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #ifndef PIP_DEMO_UTILITIES_INCLUDED
  34. #include "../Assets/DemoUtilities.h"
  35. #endif
  36. //==============================================================================
  37. static void showBubbleMessage (Component& targetComponent, const String& textToShow,
  38. std::unique_ptr<BubbleMessageComponent>& bmc,
  39. bool isRunningComponentTransformDemo);
  40. //==============================================================================
  41. /** To demonstrate how sliders can have custom snapping applied to their values,
  42. this simple class snaps the value to 50 if it comes near.
  43. */
  44. struct SnappingSlider : public Slider
  45. {
  46. double snapValue (double attemptedValue, DragMode dragMode) override
  47. {
  48. if (dragMode == notDragging)
  49. return attemptedValue; // if they're entering the value in the text-box, don't mess with it.
  50. if (attemptedValue > 40 && attemptedValue < 60)
  51. return 50.0;
  52. return attemptedValue;
  53. }
  54. };
  55. /** A TextButton that pops up a colour chooser to change its colours. */
  56. class ColourChangeButton : public TextButton,
  57. public ChangeListener
  58. {
  59. public:
  60. ColourChangeButton()
  61. : TextButton ("Click to change colour...")
  62. {
  63. setSize (10, 24);
  64. changeWidthToFitText();
  65. }
  66. void clicked() override
  67. {
  68. auto* colourSelector = new ColourSelector();
  69. colourSelector->setName ("background");
  70. colourSelector->setCurrentColour (findColour (TextButton::buttonColourId));
  71. colourSelector->addChangeListener (this);
  72. colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  73. colourSelector->setSize (300, 400);
  74. CallOutBox::launchAsynchronously (colourSelector, getScreenBounds(), nullptr);
  75. }
  76. void changeListenerCallback (ChangeBroadcaster* source) override
  77. {
  78. if (auto* cs = dynamic_cast<ColourSelector*> (source))
  79. setColour (TextButton::buttonColourId, cs->getCurrentColour());
  80. }
  81. };
  82. //==============================================================================
  83. struct SlidersPage : public Component
  84. {
  85. SlidersPage()
  86. {
  87. Rectangle<int> layoutArea { 20, 20, 580, 430 };
  88. auto sliderArea = layoutArea.removeFromTop (320);
  89. auto* s = createSlider (false);
  90. s->setSliderStyle (Slider::LinearVertical);
  91. s->setTextBoxStyle (Slider::TextBoxBelow, false, 100, 20);
  92. s->setBounds (sliderArea.removeFromLeft (70));
  93. s->setDoubleClickReturnValue (true, 50.0); // double-clicking this slider will set it to 50.0
  94. s->setTextValueSuffix (" units");
  95. s = createSlider (false);
  96. s->setSliderStyle (Slider::LinearVertical);
  97. s->setVelocityBasedMode (true);
  98. s->setSkewFactor (0.5);
  99. s->setTextBoxStyle (Slider::TextBoxAbove, true, 100, 20);
  100. s->setBounds (sliderArea.removeFromLeft (70));
  101. s->setTextValueSuffix (" rels");
  102. sliderArea.removeFromLeft (20);
  103. auto horizonalSliderArea = sliderArea.removeFromLeft (180);
  104. s = createSlider (true);
  105. s->setSliderStyle (Slider::LinearHorizontal);
  106. s->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20);
  107. s->setBounds (horizonalSliderArea.removeFromTop (20));
  108. s = createSlider (false);
  109. s->setSliderStyle (Slider::LinearHorizontal);
  110. s->setTextBoxStyle (Slider::NoTextBox, false, 0, 0);
  111. horizonalSliderArea.removeFromTop (20);
  112. s->setBounds (horizonalSliderArea.removeFromTop (20));
  113. s->setPopupDisplayEnabled (true, false, this);
  114. s->setTextValueSuffix (" nuns required to change a lightbulb");
  115. s = createSlider (false);
  116. s->setSliderStyle (Slider::LinearHorizontal);
  117. s->setTextBoxStyle (Slider::TextEntryBoxPosition::TextBoxAbove, false, 70, 20);
  118. horizonalSliderArea.removeFromTop (20);
  119. s->setBounds (horizonalSliderArea.removeFromTop (50));
  120. s->setPopupDisplayEnabled (true, false, this);
  121. s = createSlider (false);
  122. s->setSliderStyle (Slider::IncDecButtons);
  123. s->setTextBoxStyle (Slider::TextBoxLeft, false, 50, 20);
  124. horizonalSliderArea.removeFromTop (20);
  125. s->setBounds (horizonalSliderArea.removeFromTop (20));
  126. s->setIncDecButtonsMode (Slider::incDecButtonsDraggable_Vertical);
  127. s = createSlider (false);
  128. s->setSliderStyle (Slider::Rotary);
  129. s->setRotaryParameters (MathConstants<float>::pi * 1.2f, MathConstants<float>::pi * 2.8f, false);
  130. s->setTextBoxStyle (Slider::TextBoxRight, false, 70, 20);
  131. horizonalSliderArea.removeFromTop (15);
  132. s->setBounds (horizonalSliderArea.removeFromTop (70));
  133. s->setTextValueSuffix (" mm");
  134. s = createSlider (false);
  135. s->setSliderStyle (Slider::LinearBar);
  136. horizonalSliderArea.removeFromTop (10);
  137. s->setBounds (horizonalSliderArea.removeFromTop (30));
  138. s->setTextValueSuffix (" gallons");
  139. sliderArea.removeFromLeft (20);
  140. auto twoValueSliderArea = sliderArea.removeFromLeft (180);
  141. s = createSlider (false);
  142. s->setSliderStyle (Slider::TwoValueHorizontal);
  143. s->setBounds (twoValueSliderArea.removeFromTop (40));
  144. s = createSlider (false);
  145. s->setSliderStyle (Slider::ThreeValueHorizontal);
  146. s->setPopupDisplayEnabled (true, false, this);
  147. twoValueSliderArea.removeFromTop (10);
  148. s->setBounds (twoValueSliderArea.removeFromTop (40));
  149. s = createSlider (false);
  150. s->setSliderStyle (Slider::TwoValueVertical);
  151. twoValueSliderArea.removeFromLeft (30);
  152. s->setBounds (twoValueSliderArea.removeFromLeft (40));
  153. s = createSlider (false);
  154. s->setSliderStyle (Slider::ThreeValueVertical);
  155. s->setPopupDisplayEnabled (true, false, this);
  156. twoValueSliderArea.removeFromLeft (30);
  157. s->setBounds (twoValueSliderArea.removeFromLeft (40));
  158. s = createSlider (false);
  159. s->setSliderStyle (Slider::LinearBarVertical);
  160. s->setTextBoxStyle (Slider::NoTextBox, false, 0, 0);
  161. sliderArea.removeFromLeft (20);
  162. s->setBounds (sliderArea.removeFromLeft (20));
  163. s->setPopupDisplayEnabled (true, true, this);
  164. s->setTextValueSuffix (" mickles in a muckle");
  165. /* Here, we'll create a Value object, and tell a bunch of our sliders to use it as their
  166. value source. By telling them all to share the same Value, they'll stay in sync with
  167. each other.
  168. We could also optionally keep a copy of this Value elsewhere, and by changing it,
  169. cause all the sliders to automatically update.
  170. */
  171. Value sharedValue;
  172. sharedValue = Random::getSystemRandom().nextDouble() * 100;
  173. for (int i = 0; i < 8; ++i)
  174. sliders.getUnchecked (i)->getValueObject().referTo (sharedValue);
  175. // ..and now we'll do the same for all our min/max slider values..
  176. Value sharedValueMin, sharedValueMax;
  177. sharedValueMin = Random::getSystemRandom().nextDouble() * 40.0;
  178. sharedValueMax = Random::getSystemRandom().nextDouble() * 40.0 + 60.0;
  179. for (int i = 8; i <= 11; ++i)
  180. {
  181. auto* selectedSlider = sliders.getUnchecked(i);
  182. selectedSlider->setTextBoxStyle (Slider::NoTextBox, false, 0, 0);
  183. selectedSlider->getMaxValueObject().referTo (sharedValueMax);
  184. selectedSlider->getMinValueObject().referTo (sharedValueMin);
  185. }
  186. hintLabel.setBounds (layoutArea);
  187. addAndMakeVisible (hintLabel);
  188. }
  189. private:
  190. OwnedArray<Slider> sliders;
  191. Label hintLabel { "hint", "Try right-clicking on a slider for an options menu. \n\n"
  192. "Also, holding down CTRL while dragging will turn on a slider's velocity-sensitive mode" };
  193. Slider* createSlider (bool isSnapping)
  194. {
  195. auto* s = isSnapping ? new SnappingSlider()
  196. : new Slider();
  197. sliders.add (s);
  198. addAndMakeVisible (s);
  199. s->setRange (0.0, 100.0, 0.1);
  200. s->setPopupMenuEnabled (true);
  201. s->setValue (Random::getSystemRandom().nextDouble() * 100.0, dontSendNotification);
  202. return s;
  203. }
  204. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SlidersPage)
  205. };
  206. //==============================================================================
  207. struct ButtonsPage : public Component
  208. {
  209. ButtonsPage (bool isRunningComponentTransformDemo)
  210. {
  211. {
  212. auto* group = addToList (new GroupComponent ("group", "Radio buttons"));
  213. group->setBounds (20, 20, 220, 140);
  214. }
  215. for (int i = 0; i < 4; ++i)
  216. {
  217. auto* tb = addToList (new ToggleButton ("Radio Button #" + String (i + 1)));
  218. tb->setRadioGroupId (1234);
  219. tb->setBounds (45, 46 + i * 22, 180, 22);
  220. tb->setTooltip ("A set of mutually-exclusive radio buttons");
  221. if (i == 0)
  222. tb->setToggleState (true, dontSendNotification);
  223. }
  224. for (int i = 0; i < 4; ++i)
  225. {
  226. DrawablePath normal, over;
  227. Path p;
  228. p.addStar ({}, i + 5, 20.0f, 50.0f, -0.2f);
  229. normal.setPath (p);
  230. normal.setFill (Colours::lightblue);
  231. normal.setStrokeFill (Colours::black);
  232. normal.setStrokeThickness (4.0f);
  233. over.setPath (p);
  234. over.setFill (Colours::blue);
  235. over.setStrokeFill (Colours::black);
  236. over.setStrokeThickness (4.0f);
  237. auto* db = addToList (new DrawableButton (String (i + 5) + " points", DrawableButton::ImageAboveTextLabel));
  238. db->setImages (&normal, &over, 0);
  239. db->setClickingTogglesState (true);
  240. db->setRadioGroupId (23456);
  241. int buttonSize = 50;
  242. db->setBounds (25 + i * buttonSize, 180, buttonSize, buttonSize);
  243. if (i == 0)
  244. db->setToggleState (true, dontSendNotification);
  245. }
  246. for (int i = 0; i < 4; ++i)
  247. {
  248. auto* tb = addToList (new TextButton ("Button " + String (i + 1)));
  249. tb->setClickingTogglesState (true);
  250. tb->setRadioGroupId (34567);
  251. tb->setColour (TextButton::textColourOffId, Colours::black);
  252. tb->setColour (TextButton::textColourOnId, Colours::black);
  253. tb->setColour (TextButton::buttonColourId, Colours::white);
  254. tb->setColour (TextButton::buttonOnColourId, Colours::blueviolet.brighter());
  255. tb->setBounds (20 + i * 55, 260, 55, 24);
  256. tb->setConnectedEdges (((i != 0) ? Button::ConnectedOnLeft : 0)
  257. | ((i != 3) ? Button::ConnectedOnRight : 0));
  258. if (i == 0)
  259. tb->setToggleState (true, dontSendNotification);
  260. }
  261. {
  262. auto* colourChangeButton = new ColourChangeButton();
  263. components.add (colourChangeButton);
  264. addAndMakeVisible (colourChangeButton);
  265. colourChangeButton->setTopLeftPosition (20, 320);
  266. }
  267. {
  268. auto* hyperlink = addToList (new HyperlinkButton ("This is a HyperlinkButton",
  269. { "http://www.juce.com" }));
  270. hyperlink->setBounds (260, 20, 200, 24);
  271. }
  272. // create some drawables to use for our drawable buttons...
  273. DrawablePath normal, over;
  274. {
  275. Path p;
  276. p.addStar ({}, 5, 20.0f, 50.0f, 0.2f);
  277. normal.setPath (p);
  278. normal.setFill (getRandomDarkColour());
  279. }
  280. {
  281. Path p;
  282. p.addStar ({}, 9, 25.0f, 50.0f, 0.0f);
  283. over.setPath (p);
  284. over.setFill (getRandomBrightColour());
  285. over.setStrokeFill (getRandomDarkColour());
  286. over.setStrokeThickness (5.0f);
  287. }
  288. DrawableImage down;
  289. down.setImage (getImageFromAssets ("juce_icon.png"));
  290. down.setOverlayColour (Colours::black.withAlpha (0.3f));
  291. auto popupMessageCallback = [this, isRunningComponentTransformDemo]
  292. {
  293. if (auto* focused = Component::getCurrentlyFocusedComponent())
  294. showBubbleMessage (*focused,
  295. "This is a demo of the BubbleMessageComponent, which lets you pop up a message pointing "
  296. "at a component or somewhere on the screen.\n\n"
  297. "The message bubbles will disappear after a timeout period, or when the mouse is clicked.",
  298. this->bubbleMessage,
  299. isRunningComponentTransformDemo);
  300. };
  301. {
  302. // create an image-above-text button from these drawables..
  303. auto db = addToList (new DrawableButton ("Button 1", DrawableButton::ImageAboveTextLabel));
  304. db->setImages (&normal, &over, &down);
  305. db->setBounds (260, 60, 80, 80);
  306. db->setTooltip ("This is a DrawableButton with a label");
  307. db->onClick = popupMessageCallback;
  308. }
  309. {
  310. // create an image-only button from these drawables..
  311. auto db = addToList (new DrawableButton ("Button 2", DrawableButton::ImageFitted));
  312. db->setImages (&normal, &over, &down);
  313. db->setClickingTogglesState (true);
  314. db->setBounds (370, 60, 80, 80);
  315. db->setTooltip ("This is an image-only DrawableButton");
  316. db->onClick = popupMessageCallback;
  317. }
  318. {
  319. // create an image-on-button-shape button from the same drawables..
  320. auto db = addToList (new DrawableButton ("Button 3", DrawableButton::ImageOnButtonBackground));
  321. db->setImages (&normal, 0, 0);
  322. db->setBounds (260, 160, 110, 25);
  323. db->setTooltip ("This is a DrawableButton on a standard button background");
  324. db->onClick = popupMessageCallback;
  325. }
  326. {
  327. auto db = addToList (new DrawableButton ("Button 4", DrawableButton::ImageOnButtonBackground));
  328. db->setImages (&normal, &over, &down);
  329. db->setClickingTogglesState (true);
  330. db->setColour (DrawableButton::backgroundColourId, Colours::white);
  331. db->setColour (DrawableButton::backgroundOnColourId, Colours::yellow);
  332. db->setBounds (400, 150, 50, 50);
  333. db->setTooltip ("This is a DrawableButton on a standard button background");
  334. db->onClick = popupMessageCallback;
  335. }
  336. {
  337. auto sb = addToList (new ShapeButton ("ShapeButton",
  338. getRandomDarkColour(),
  339. getRandomDarkColour(),
  340. getRandomDarkColour()));
  341. sb->setShape (getJUCELogoPath(), false, true, false);
  342. sb->setBounds (260, 220, 200, 120);
  343. }
  344. {
  345. auto ib = addToList (new ImageButton ("ImageButton"));
  346. auto juceImage = getImageFromAssets ("juce_icon.png");
  347. ib->setImages (true, true, true,
  348. juceImage, 0.7f, Colours::transparentBlack,
  349. juceImage, 1.0f, Colours::transparentBlack,
  350. juceImage, 1.0f, getRandomBrightColour().withAlpha (0.8f),
  351. 0.5f);
  352. ib->setBounds (260, 350, 100, 100);
  353. ib->setTooltip ("ImageButton - showing alpha-channel hit-testing and colour overlay when clicked");
  354. }
  355. }
  356. private:
  357. OwnedArray<Component> components;
  358. std::unique_ptr<BubbleMessageComponent> bubbleMessage;
  359. // This little function avoids a bit of code-duplication by adding a component to
  360. // our list as well as calling addAndMakeVisible on it..
  361. template <typename ComponentType>
  362. ComponentType* addToList (ComponentType* newComp)
  363. {
  364. components.add (newComp);
  365. addAndMakeVisible (newComp);
  366. return newComp;
  367. }
  368. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonsPage)
  369. };
  370. //==============================================================================
  371. struct MiscPage : public Component
  372. {
  373. MiscPage()
  374. {
  375. addAndMakeVisible (textEditor1);
  376. textEditor1.setBounds (10, 25, 200, 24);
  377. textEditor1.setText ("Single-line text box");
  378. addAndMakeVisible (textEditor2);
  379. textEditor2.setBounds (10, 55, 200, 24);
  380. textEditor2.setText ("Password");
  381. addAndMakeVisible (comboBox);
  382. comboBox.setBounds (10, 85, 200, 24);
  383. comboBox.setEditableText (true);
  384. comboBox.setJustificationType (Justification::centred);
  385. for (int i = 1; i < 100; ++i)
  386. comboBox.addItem ("combo box item " + String (i), i);
  387. comboBox.setSelectedId (1);
  388. }
  389. void lookAndFeelChanged() override
  390. {
  391. textEditor1.applyFontToAllText (textEditor1.getFont());
  392. textEditor2.applyFontToAllText (textEditor2.getFont());
  393. }
  394. TextEditor textEditor1,
  395. textEditor2 { "Password", (juce_wchar) 0x2022 };
  396. ComboBox comboBox { "Combo" };
  397. };
  398. //==============================================================================
  399. class ToolbarDemoComp : public Component,
  400. private Slider::Listener
  401. {
  402. public:
  403. ToolbarDemoComp()
  404. {
  405. // Create and add the toolbar...
  406. addAndMakeVisible (toolbar);
  407. // And use our item factory to add a set of default icons to it...
  408. toolbar.addDefaultItems (factory);
  409. // Now we'll just create the other sliders and buttons on the demo page, which adjust
  410. // the toolbar's properties...
  411. addAndMakeVisible (infoLabel);
  412. infoLabel.setJustificationType (Justification::topLeft);
  413. infoLabel.setBounds (80, 80, 450, 100);
  414. infoLabel.setInterceptsMouseClicks (false, false);
  415. addAndMakeVisible (depthSlider);
  416. depthSlider.setRange (10.0, 200.0, 1.0);
  417. depthSlider.setValue (50, dontSendNotification);
  418. depthSlider.addListener (this);
  419. depthSlider.setBounds (80, 210, 300, 22);
  420. depthLabel.attachToComponent (&depthSlider, false);
  421. addAndMakeVisible (orientationButton);
  422. orientationButton.onClick = [this] { toolbar.setVertical (! toolbar.isVertical()); resized(); };
  423. orientationButton.changeWidthToFitText (22);
  424. orientationButton.setTopLeftPosition (depthSlider.getX(), depthSlider.getBottom() + 20);
  425. addAndMakeVisible (customiseButton);
  426. customiseButton.onClick = [this] { toolbar.showCustomisationDialog (factory); };
  427. customiseButton.changeWidthToFitText (22);
  428. customiseButton.setTopLeftPosition (orientationButton.getRight() + 20, orientationButton.getY());
  429. }
  430. void resized() override
  431. {
  432. auto toolbarThickness = (int) depthSlider.getValue();
  433. if (toolbar.isVertical())
  434. toolbar.setBounds (getLocalBounds().removeFromLeft (toolbarThickness));
  435. else
  436. toolbar.setBounds (getLocalBounds().removeFromTop (toolbarThickness));
  437. }
  438. void sliderValueChanged (Slider*) override
  439. {
  440. resized();
  441. }
  442. private:
  443. Toolbar toolbar;
  444. Slider depthSlider { Slider::LinearHorizontal, Slider::TextBoxLeft };
  445. Label depthLabel { {}, "Toolbar depth:" },
  446. infoLabel { {}, "As well as showing off toolbars, this demo illustrates how to store "
  447. "a set of SVG files in a Zip file, embed that in your application, and read "
  448. "them back in at runtime.\n\nThe icon images here are taken from the open-source "
  449. "Tango icon project."};
  450. TextButton orientationButton { "Vertical/Horizontal" },
  451. customiseButton { "Customise..." };
  452. //==============================================================================
  453. class DemoToolbarItemFactory : public ToolbarItemFactory
  454. {
  455. public:
  456. DemoToolbarItemFactory() {}
  457. //==============================================================================
  458. // Each type of item a toolbar can contain must be given a unique ID. These
  459. // are the ones we'll use in this demo.
  460. enum DemoToolbarItemIds
  461. {
  462. doc_new = 1,
  463. doc_open = 2,
  464. doc_save = 3,
  465. doc_saveAs = 4,
  466. edit_copy = 5,
  467. edit_cut = 6,
  468. edit_paste = 7,
  469. juceLogoButton = 8,
  470. customComboBox = 9
  471. };
  472. void getAllToolbarItemIds (Array<int>& ids) override
  473. {
  474. // This returns the complete list of all item IDs that are allowed to
  475. // go in our toolbar. Any items you might want to add must be listed here. The
  476. // order in which they are listed will be used by the toolbar customisation panel.
  477. ids.add (doc_new);
  478. ids.add (doc_open);
  479. ids.add (doc_save);
  480. ids.add (doc_saveAs);
  481. ids.add (edit_copy);
  482. ids.add (edit_cut);
  483. ids.add (edit_paste);
  484. ids.add (juceLogoButton);
  485. ids.add (customComboBox);
  486. // If you're going to use separators, then they must also be added explicitly
  487. // to the list.
  488. ids.add (separatorBarId);
  489. ids.add (spacerId);
  490. ids.add (flexibleSpacerId);
  491. }
  492. void getDefaultItemSet (Array<int>& ids) override
  493. {
  494. // This returns an ordered list of the set of items that make up a
  495. // toolbar's default set. Not all items need to be on this list, and
  496. // items can appear multiple times (e.g. the separators used here).
  497. ids.add (doc_new);
  498. ids.add (doc_open);
  499. ids.add (doc_save);
  500. ids.add (doc_saveAs);
  501. ids.add (spacerId);
  502. ids.add (separatorBarId);
  503. ids.add (edit_copy);
  504. ids.add (edit_cut);
  505. ids.add (edit_paste);
  506. ids.add (separatorBarId);
  507. ids.add (flexibleSpacerId);
  508. ids.add (customComboBox);
  509. ids.add (flexibleSpacerId);
  510. ids.add (separatorBarId);
  511. ids.add (juceLogoButton);
  512. }
  513. ToolbarItemComponent* createItem (int itemId) override
  514. {
  515. switch (itemId)
  516. {
  517. case doc_new: return createButtonFromZipFileSVG (itemId, "new", "document-new.svg");
  518. case doc_open: return createButtonFromZipFileSVG (itemId, "open", "document-open.svg");
  519. case doc_save: return createButtonFromZipFileSVG (itemId, "save", "document-save.svg");
  520. case doc_saveAs: return createButtonFromZipFileSVG (itemId, "save as", "document-save-as.svg");
  521. case edit_copy: return createButtonFromZipFileSVG (itemId, "copy", "edit-copy.svg");
  522. case edit_cut: return createButtonFromZipFileSVG (itemId, "cut", "edit-cut.svg");
  523. case edit_paste: return createButtonFromZipFileSVG (itemId, "paste", "edit-paste.svg");
  524. case juceLogoButton:
  525. {
  526. auto* drawable = new DrawableImage();
  527. drawable->setImage (getImageFromAssets ("juce_icon.png"));
  528. return new ToolbarButton (itemId, "juce!", drawable, 0);
  529. }
  530. case customComboBox: return new CustomToolbarComboBox (itemId);
  531. default: break;
  532. }
  533. return nullptr;
  534. }
  535. private:
  536. StringArray iconNames;
  537. OwnedArray<Drawable> iconsFromZipFile;
  538. // This is a little utility to create a button with one of the SVG images in
  539. // our embedded ZIP file "icons.zip"
  540. ToolbarButton* createButtonFromZipFileSVG (const int itemId, const String& text, const String& filename)
  541. {
  542. if (iconsFromZipFile.size() == 0)
  543. {
  544. // If we've not already done so, load all the images from the zip file..
  545. ZipFile icons (createAssetInputStream ("icons.zip"), true);
  546. for (int i = 0; i < icons.getNumEntries(); ++i)
  547. {
  548. std::unique_ptr<InputStream> svgFileStream (icons.createStreamForEntry (i));
  549. if (svgFileStream.get() != nullptr)
  550. {
  551. iconNames.add (icons.getEntry (i)->filename);
  552. iconsFromZipFile.add (Drawable::createFromImageDataStream (*svgFileStream));
  553. }
  554. }
  555. }
  556. auto* image = iconsFromZipFile[iconNames.indexOf (filename)]->createCopy();
  557. return new ToolbarButton (itemId, text, image, 0);
  558. }
  559. // Demonstrates how to put a custom component into a toolbar - this one contains
  560. // a ComboBox.
  561. class CustomToolbarComboBox : public ToolbarItemComponent
  562. {
  563. public:
  564. CustomToolbarComboBox (const int toolbarItemId)
  565. : ToolbarItemComponent (toolbarItemId, "Custom Toolbar Item", false)
  566. {
  567. addAndMakeVisible (comboBox);
  568. for (int i = 1; i < 20; ++i)
  569. comboBox.addItem ("Toolbar ComboBox item " + String (i), i);
  570. comboBox.setSelectedId (1);
  571. comboBox.setEditableText (true);
  572. }
  573. bool getToolbarItemSizes (int /*toolbarDepth*/, bool isVertical,
  574. int& preferredSize, int& minSize, int& maxSize) override
  575. {
  576. if (isVertical)
  577. return false;
  578. preferredSize = 250;
  579. minSize = 80;
  580. maxSize = 300;
  581. return true;
  582. }
  583. void paintButtonArea (Graphics&, int, int, bool, bool) override
  584. {
  585. }
  586. void contentAreaChanged (const Rectangle<int>& newArea) override
  587. {
  588. comboBox.setSize (newArea.getWidth() - 2,
  589. jmin (newArea.getHeight() - 2, 22));
  590. comboBox.setCentrePosition (newArea.getCentreX(), newArea.getCentreY());
  591. }
  592. private:
  593. ComboBox comboBox { "demo toolbar combo box" };
  594. };
  595. };
  596. DemoToolbarItemFactory factory;
  597. };
  598. //==============================================================================
  599. /**
  600. This class shows how to implement a TableListBoxModel to show in a TableListBox.
  601. */
  602. class TableDemoComponent : public Component,
  603. public TableListBoxModel
  604. {
  605. public:
  606. TableDemoComponent()
  607. {
  608. // Load some data from an embedded XML file..
  609. loadData();
  610. // Create our table component and add it to this component..
  611. addAndMakeVisible (table);
  612. table.setModel (this);
  613. // give it a border
  614. table.setColour (ListBox::outlineColourId, Colours::grey);
  615. table.setOutlineThickness (1);
  616. // Add some columns to the table header, based on the column list in our database..
  617. forEachXmlChildElement (*columnList, columnXml)
  618. {
  619. table.getHeader().addColumn (columnXml->getStringAttribute ("name"),
  620. columnXml->getIntAttribute ("columnId"),
  621. columnXml->getIntAttribute ("width"),
  622. 50, 400,
  623. TableHeaderComponent::defaultFlags);
  624. }
  625. // we could now change some initial settings..
  626. table.getHeader().setSortColumnId (1, true); // sort forwards by the ID column
  627. table.getHeader().setColumnVisible (7, false); // hide the "length" column until the user shows it
  628. // un-comment this line to have a go of stretch-to-fit mode
  629. // table.getHeader().setStretchToFitActive (true);
  630. table.setMultipleSelectionEnabled (true);
  631. }
  632. // This is overloaded from TableListBoxModel, and must return the total number of rows in our table
  633. int getNumRows() override
  634. {
  635. return numRows;
  636. }
  637. // This is overloaded from TableListBoxModel, and should fill in the background of the whole row
  638. void paintRowBackground (Graphics& g, int rowNumber, int /*width*/, int /*height*/, bool rowIsSelected) override
  639. {
  640. auto alternateColour = getLookAndFeel().findColour (ListBox::backgroundColourId)
  641. .interpolatedWith (getLookAndFeel().findColour (ListBox::textColourId), 0.03f);
  642. if (rowIsSelected)
  643. g.fillAll (Colours::lightblue);
  644. else if (rowNumber % 2)
  645. g.fillAll (alternateColour);
  646. }
  647. // This is overloaded from TableListBoxModel, and must paint any cells that aren't using custom
  648. // components.
  649. void paintCell (Graphics& g, int rowNumber, int columnId,
  650. int width, int height, bool /*rowIsSelected*/) override
  651. {
  652. g.setColour (getLookAndFeel().findColour (ListBox::textColourId));
  653. g.setFont (font);
  654. if (auto* rowElement = dataList->getChildElement (rowNumber))
  655. {
  656. auto text = rowElement->getStringAttribute (getAttributeNameForColumnId (columnId));
  657. g.drawText (text, 2, 0, width - 4, height, Justification::centredLeft, true);
  658. }
  659. g.setColour (getLookAndFeel().findColour (ListBox::backgroundColourId));
  660. g.fillRect (width - 1, 0, 1, height);
  661. }
  662. // This is overloaded from TableListBoxModel, and tells us that the user has clicked a table header
  663. // to change the sort order.
  664. void sortOrderChanged (int newSortColumnId, bool isForwards) override
  665. {
  666. if (newSortColumnId != 0)
  667. {
  668. DemoDataSorter sorter (getAttributeNameForColumnId (newSortColumnId), isForwards);
  669. dataList->sortChildElements (sorter);
  670. table.updateContent();
  671. }
  672. }
  673. // This is overloaded from TableListBoxModel, and must update any custom components that we're using
  674. Component* refreshComponentForCell (int rowNumber, int columnId, bool /*isRowSelected*/,
  675. Component* existingComponentToUpdate) override
  676. {
  677. if (columnId == 1 || columnId == 7) // The ID and Length columns do not have a custom component
  678. {
  679. jassert (existingComponentToUpdate == nullptr);
  680. return nullptr;
  681. }
  682. if (columnId == 5) // For the ratings column, we return the custom combobox component
  683. {
  684. auto* ratingsBox = static_cast<RatingColumnCustomComponent*> (existingComponentToUpdate);
  685. // If an existing component is being passed-in for updating, we'll re-use it, but
  686. // if not, we'll have to create one.
  687. if (ratingsBox == nullptr)
  688. ratingsBox = new RatingColumnCustomComponent (*this);
  689. ratingsBox->setRowAndColumn (rowNumber, columnId);
  690. return ratingsBox;
  691. }
  692. // The other columns are editable text columns, for which we use the custom Label component
  693. auto* textLabel = static_cast<EditableTextCustomComponent*> (existingComponentToUpdate);
  694. // same as above...
  695. if (textLabel == nullptr)
  696. textLabel = new EditableTextCustomComponent (*this);
  697. textLabel->setRowAndColumn (rowNumber, columnId);
  698. return textLabel;
  699. }
  700. // This is overloaded from TableListBoxModel, and should choose the best width for the specified
  701. // column.
  702. int getColumnAutoSizeWidth (int columnId) override
  703. {
  704. if (columnId == 5)
  705. return 100; // (this is the ratings column, containing a custom combobox component)
  706. int widest = 32;
  707. // find the widest bit of text in this column..
  708. for (int i = getNumRows(); --i >= 0;)
  709. {
  710. if (auto* rowElement = dataList->getChildElement (i))
  711. {
  712. auto text = rowElement->getStringAttribute (getAttributeNameForColumnId (columnId));
  713. widest = jmax (widest, font.getStringWidth (text));
  714. }
  715. }
  716. return widest + 8;
  717. }
  718. // A couple of quick methods to set and get cell values when the user changes them
  719. int getRating (const int rowNumber) const
  720. {
  721. return dataList->getChildElement (rowNumber)->getIntAttribute ("Rating");
  722. }
  723. void setRating (const int rowNumber, const int newRating)
  724. {
  725. dataList->getChildElement (rowNumber)->setAttribute ("Rating", newRating);
  726. }
  727. String getText (const int columnNumber, const int rowNumber) const
  728. {
  729. return dataList->getChildElement (rowNumber)->getStringAttribute ( getAttributeNameForColumnId(columnNumber));
  730. }
  731. void setText (const int columnNumber, const int rowNumber, const String& newText)
  732. {
  733. auto columnName = table.getHeader().getColumnName (columnNumber);
  734. dataList->getChildElement (rowNumber)->setAttribute (columnName, newText);
  735. }
  736. //==============================================================================
  737. void resized() override
  738. {
  739. // position our table with a gap around its edge
  740. table.setBoundsInset (BorderSize<int> (8));
  741. }
  742. private:
  743. TableListBox table; // the table component itself
  744. Font font { 14.0f };
  745. std::unique_ptr<XmlElement> demoData; // This is the XML document loaded from the embedded file "demo table data.xml"
  746. XmlElement* columnList = nullptr; // A pointer to the sub-node of demoData that contains the list of columns
  747. XmlElement* dataList = nullptr; // A pointer to the sub-node of demoData that contains the list of data rows
  748. int numRows; // The number of rows of data we've got
  749. //==============================================================================
  750. // This is a custom Label component, which we use for the table's editable text columns.
  751. class EditableTextCustomComponent : public Label
  752. {
  753. public:
  754. EditableTextCustomComponent (TableDemoComponent& td) : owner (td)
  755. {
  756. // double click to edit the label text; single click handled below
  757. setEditable (false, true, false);
  758. }
  759. void mouseDown (const MouseEvent& event) override
  760. {
  761. // single click on the label should simply select the row
  762. owner.table.selectRowsBasedOnModifierKeys (row, event.mods, false);
  763. Label::mouseDown (event);
  764. }
  765. void textWasEdited() override
  766. {
  767. owner.setText (columnId, row, getText());
  768. }
  769. // Our demo code will call this when we may need to update our contents
  770. void setRowAndColumn (const int newRow, const int newColumn)
  771. {
  772. row = newRow;
  773. columnId = newColumn;
  774. setText (owner.getText(columnId, row), dontSendNotification);
  775. }
  776. void paint (Graphics& g) override
  777. {
  778. auto& lf = getLookAndFeel();
  779. if (! dynamic_cast<LookAndFeel_V4*> (&lf))
  780. lf.setColour (textColourId, Colours::black);
  781. Label::paint (g);
  782. }
  783. private:
  784. TableDemoComponent& owner;
  785. int row, columnId;
  786. Colour textColour;
  787. };
  788. //==============================================================================
  789. // This is a custom component containing a combo box, which we're going to put inside
  790. // our table's "rating" column.
  791. class RatingColumnCustomComponent : public Component
  792. {
  793. public:
  794. RatingColumnCustomComponent (TableDemoComponent& td) : owner (td)
  795. {
  796. // just put a combo box inside this component
  797. addAndMakeVisible (comboBox);
  798. comboBox.addItem ("fab", 1);
  799. comboBox.addItem ("groovy", 2);
  800. comboBox.addItem ("hep", 3);
  801. comboBox.addItem ("mad for it", 4);
  802. comboBox.addItem ("neat", 5);
  803. comboBox.addItem ("swingin", 6);
  804. comboBox.addItem ("wild", 7);
  805. comboBox.onChange = [this] { owner.setRating (row, comboBox.getSelectedId()); };
  806. comboBox.setWantsKeyboardFocus (false);
  807. }
  808. void resized() override
  809. {
  810. comboBox.setBoundsInset (BorderSize<int> (2));
  811. }
  812. // Our demo code will call this when we may need to update our contents
  813. void setRowAndColumn (int newRow, int newColumn)
  814. {
  815. row = newRow;
  816. columnId = newColumn;
  817. comboBox.setSelectedId (owner.getRating (row), dontSendNotification);
  818. }
  819. private:
  820. TableDemoComponent& owner;
  821. ComboBox comboBox;
  822. int row, columnId;
  823. };
  824. //==============================================================================
  825. // A comparator used to sort our data when the user clicks a column header
  826. class DemoDataSorter
  827. {
  828. public:
  829. DemoDataSorter (const String& attributeToSortBy, bool forwards)
  830. : attributeToSort (attributeToSortBy),
  831. direction (forwards ? 1 : -1)
  832. {
  833. }
  834. int compareElements (XmlElement* first, XmlElement* second) const
  835. {
  836. auto result = first->getStringAttribute (attributeToSort)
  837. .compareNatural (second->getStringAttribute (attributeToSort));
  838. if (result == 0)
  839. result = first->getStringAttribute ("ID")
  840. .compareNatural (second->getStringAttribute ("ID"));
  841. return direction * result;
  842. }
  843. private:
  844. String attributeToSort;
  845. int direction;
  846. };
  847. //==============================================================================
  848. // this loads the embedded database XML file into memory
  849. void loadData()
  850. {
  851. demoData.reset (XmlDocument::parse (loadEntireAssetIntoString ("demo table data.xml")));
  852. dataList = demoData->getChildByName ("DATA");
  853. columnList = demoData->getChildByName ("COLUMNS");
  854. numRows = dataList->getNumChildElements();
  855. }
  856. // (a utility method to search our XML for the attribute that matches a column ID)
  857. String getAttributeNameForColumnId (const int columnId) const
  858. {
  859. forEachXmlChildElement (*columnList, columnXml)
  860. {
  861. if (columnXml->getIntAttribute ("columnId") == columnId)
  862. return columnXml->getStringAttribute ("name");
  863. }
  864. return {};
  865. }
  866. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableDemoComponent)
  867. };
  868. //==============================================================================
  869. class DragAndDropDemo : public Component,
  870. public DragAndDropContainer
  871. {
  872. public:
  873. DragAndDropDemo()
  874. {
  875. setName ("Drag-and-Drop");
  876. sourceListBox.setModel (&sourceModel);
  877. sourceListBox.setMultipleSelectionEnabled (true);
  878. addAndMakeVisible (sourceListBox);
  879. addAndMakeVisible (target);
  880. }
  881. void resized() override
  882. {
  883. auto r = getLocalBounds().reduced (8);
  884. sourceListBox.setBounds (r.withSize (250, 180));
  885. target .setBounds (r.removeFromBottom (150).removeFromRight (250));
  886. }
  887. private:
  888. //==============================================================================
  889. struct SourceItemListboxContents : public ListBoxModel
  890. {
  891. // The following methods implement the necessary virtual functions from ListBoxModel,
  892. // telling the listbox how many rows there are, painting them, etc.
  893. int getNumRows() override
  894. {
  895. return 30;
  896. }
  897. void paintListBoxItem (int rowNumber, Graphics& g,
  898. int width, int height, bool rowIsSelected) override
  899. {
  900. if (rowIsSelected)
  901. g.fillAll (Colours::lightblue);
  902. g.setColour (LookAndFeel::getDefaultLookAndFeel().findColour (Label::textColourId));
  903. g.setFont (height * 0.7f);
  904. g.drawText ("Draggable Thing #" + String (rowNumber + 1),
  905. 5, 0, width, height,
  906. Justification::centredLeft, true);
  907. }
  908. var getDragSourceDescription (const SparseSet<int>& selectedRows) override
  909. {
  910. // for our drag description, we'll just make a comma-separated list of the selected row
  911. // numbers - this will be picked up by the drag target and displayed in its box.
  912. StringArray rows;
  913. for (int i = 0; i < selectedRows.size(); ++i)
  914. rows.add (String (selectedRows[i] + 1));
  915. return rows.joinIntoString (", ");
  916. }
  917. };
  918. //==============================================================================
  919. // and this is a component that can have things dropped onto it..
  920. class DragAndDropDemoTarget : public Component,
  921. public DragAndDropTarget,
  922. public FileDragAndDropTarget,
  923. public TextDragAndDropTarget
  924. {
  925. public:
  926. DragAndDropDemoTarget() {}
  927. void paint (Graphics& g) override
  928. {
  929. g.fillAll (Colours::green.withAlpha (0.2f));
  930. // draw a red line around the comp if the user's currently dragging something over it..
  931. if (somethingIsBeingDraggedOver)
  932. {
  933. g.setColour (Colours::red);
  934. g.drawRect (getLocalBounds(), 3);
  935. }
  936. g.setColour (getLookAndFeel().findColour (Label::textColourId));
  937. g.setFont (14.0f);
  938. g.drawFittedText (message, getLocalBounds().reduced (10, 0), Justification::centred, 4);
  939. }
  940. //==============================================================================
  941. // These methods implement the DragAndDropTarget interface, and allow our component
  942. // to accept drag-and-drop of objects from other JUCE components..
  943. bool isInterestedInDragSource (const SourceDetails& /*dragSourceDetails*/) override
  944. {
  945. // normally you'd check the sourceDescription value to see if it's the
  946. // sort of object that you're interested in before returning true, but for
  947. // the demo, we'll say yes to anything..
  948. return true;
  949. }
  950. void itemDragEnter (const SourceDetails& /*dragSourceDetails*/) override
  951. {
  952. somethingIsBeingDraggedOver = true;
  953. repaint();
  954. }
  955. void itemDragMove (const SourceDetails& /*dragSourceDetails*/) override
  956. {
  957. }
  958. void itemDragExit (const SourceDetails& /*dragSourceDetails*/) override
  959. {
  960. somethingIsBeingDraggedOver = false;
  961. repaint();
  962. }
  963. void itemDropped (const SourceDetails& dragSourceDetails) override
  964. {
  965. message = "Items dropped: " + dragSourceDetails.description.toString();
  966. somethingIsBeingDraggedOver = false;
  967. repaint();
  968. }
  969. //==============================================================================
  970. // These methods implement the FileDragAndDropTarget interface, and allow our component
  971. // to accept drag-and-drop of files..
  972. bool isInterestedInFileDrag (const StringArray& /*files*/) override
  973. {
  974. // normally you'd check these files to see if they're something that you're
  975. // interested in before returning true, but for the demo, we'll say yes to anything..
  976. return true;
  977. }
  978. void fileDragEnter (const StringArray& /*files*/, int /*x*/, int /*y*/) override
  979. {
  980. somethingIsBeingDraggedOver = true;
  981. repaint();
  982. }
  983. void fileDragMove (const StringArray& /*files*/, int /*x*/, int /*y*/) override
  984. {
  985. }
  986. void fileDragExit (const StringArray& /*files*/) override
  987. {
  988. somethingIsBeingDraggedOver = false;
  989. repaint();
  990. }
  991. void filesDropped (const StringArray& files, int /*x*/, int /*y*/) override
  992. {
  993. message = "Files dropped: " + files.joinIntoString ("\n");
  994. somethingIsBeingDraggedOver = false;
  995. repaint();
  996. }
  997. //==============================================================================
  998. // These methods implement the TextDragAndDropTarget interface, and allow our component
  999. // to accept drag-and-drop of text..
  1000. bool isInterestedInTextDrag (const String& /*text*/) override
  1001. {
  1002. return true;
  1003. }
  1004. void textDragEnter (const String& /*text*/, int /*x*/, int /*y*/) override
  1005. {
  1006. somethingIsBeingDraggedOver = true;
  1007. repaint();
  1008. }
  1009. void textDragMove (const String& /*text*/, int /*x*/, int /*y*/) override
  1010. {
  1011. }
  1012. void textDragExit (const String& /*text*/) override
  1013. {
  1014. somethingIsBeingDraggedOver = false;
  1015. repaint();
  1016. }
  1017. void textDropped (const String& text, int /*x*/, int /*y*/) override
  1018. {
  1019. message = "Text dropped:\n" + text;
  1020. somethingIsBeingDraggedOver = false;
  1021. repaint();
  1022. }
  1023. private:
  1024. String message { "Drag-and-drop some rows from the top-left box onto this component!\n\n"
  1025. "You can also drag-and-drop files and text from other apps"};
  1026. bool somethingIsBeingDraggedOver = false;
  1027. };
  1028. //==============================================================================
  1029. ListBox sourceListBox { "D+D source", nullptr };
  1030. SourceItemListboxContents sourceModel;
  1031. DragAndDropDemoTarget target;
  1032. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragAndDropDemo)
  1033. };
  1034. //==============================================================================
  1035. struct DemoTabbedComponent : public TabbedComponent
  1036. {
  1037. DemoTabbedComponent (bool isRunningComponenTransformsDemo)
  1038. : TabbedComponent (TabbedButtonBar::TabsAtTop)
  1039. {
  1040. auto colour = findColour (ResizableWindow::backgroundColourId);
  1041. addTab ("Buttons", colour, new ButtonsPage (isRunningComponenTransformsDemo), true);
  1042. addTab ("Sliders", colour, new SlidersPage(), true);
  1043. addTab ("Toolbars", colour, new ToolbarDemoComp(), true);
  1044. addTab ("Misc", colour, new MiscPage(), true);
  1045. addTab ("Tables", colour, new TableDemoComponent(), true);
  1046. addTab ("Drag & Drop", colour, new DragAndDropDemo(), true);
  1047. getTabbedButtonBar().getTabButton (5)->setExtraComponent (new CustomTabButton (isRunningComponenTransformsDemo),
  1048. TabBarButton::afterText);
  1049. }
  1050. // This is a small star button that is put inside one of the tabs. You can
  1051. // use this technique to create things like "close tab" buttons, etc.
  1052. class CustomTabButton : public Component
  1053. {
  1054. public:
  1055. CustomTabButton (bool isRunningComponenTransformsDemo)
  1056. : runningComponenTransformsDemo (isRunningComponenTransformsDemo)
  1057. {
  1058. setSize (20, 20);
  1059. }
  1060. void paint (Graphics& g) override
  1061. {
  1062. Path star;
  1063. star.addStar ({}, 7, 1.0f, 2.0f);
  1064. g.setColour (Colours::green);
  1065. g.fillPath (star, star.getTransformToScaleToFit (getLocalBounds().reduced (2).toFloat(), true));
  1066. }
  1067. void mouseDown (const MouseEvent&) override
  1068. {
  1069. showBubbleMessage (*this,
  1070. "This is a custom tab component\n"
  1071. "\n"
  1072. "You can use these to implement things like close-buttons "
  1073. "or status displays for your tabs.",
  1074. bubbleMessage,
  1075. runningComponenTransformsDemo);
  1076. }
  1077. private:
  1078. bool runningComponenTransformsDemo;
  1079. std::unique_ptr<BubbleMessageComponent> bubbleMessage;
  1080. };
  1081. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoTabbedComponent)
  1082. };
  1083. //==============================================================================
  1084. struct WidgetsDemo : public Component
  1085. {
  1086. WidgetsDemo (bool isRunningComponenTransformsDemo = false)
  1087. : tabs (isRunningComponenTransformsDemo)
  1088. {
  1089. setOpaque (true);
  1090. addAndMakeVisible (tabs);
  1091. setSize (700, 500);
  1092. }
  1093. void paint (Graphics& g) override
  1094. {
  1095. g.fillAll (Colours::lightgrey);
  1096. }
  1097. void resized() override
  1098. {
  1099. tabs.setBounds (getLocalBounds().reduced (4));
  1100. }
  1101. DemoTabbedComponent tabs;
  1102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WidgetsDemo)
  1103. };
  1104. //==============================================================================
  1105. void showBubbleMessage (Component& targetComponent, const String& textToShow,
  1106. std::unique_ptr<BubbleMessageComponent>& bmc,
  1107. bool isRunningComponentTransformDemo)
  1108. {
  1109. bmc.reset (new BubbleMessageComponent());
  1110. if (isRunningComponentTransformDemo)
  1111. {
  1112. targetComponent.findParentComponentOfClass<WidgetsDemo>()->addChildComponent (bmc.get());
  1113. }
  1114. else if (Desktop::canUseSemiTransparentWindows())
  1115. {
  1116. bmc->setAlwaysOnTop (true);
  1117. bmc->addToDesktop (0);
  1118. }
  1119. else
  1120. {
  1121. targetComponent.getTopLevelComponent()->addChildComponent (bmc.get());
  1122. }
  1123. AttributedString text (textToShow);
  1124. text.setJustification (Justification::centred);
  1125. text.setColour (targetComponent.findColour (TextButton::textColourOffId));
  1126. bmc->showAt (&targetComponent, text, 2000, true, false);
  1127. }