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.

1492 lines
55KB

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