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.

1384 lines
53KB

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