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.

1552 lines
57KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceDemoHeader.h"
  18. static void showBubbleMessage (Component* targetComponent, const String& textToShow,
  19. ScopedPointer<BubbleMessageComponent>& bmc)
  20. {
  21. 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, false);
  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. ScopedPointer<BubbleMessageComponent> bubbleMessage;
  324. // This little function avoids a bit of code-duplication by adding a component to
  325. // our list as well as calling addAndMakeVisible on it..
  326. template <typename ComponentType>
  327. ComponentType* addToList (ComponentType* newComp)
  328. {
  329. components.add (newComp);
  330. addAndMakeVisible (newComp);
  331. return newComp;
  332. }
  333. void buttonClicked (Button* button) override
  334. {
  335. showBubbleMessage (button,
  336. "This is a demo of the BubbleMessageComponent, which lets you pop up a message pointing "
  337. "at a component or somewhere on the screen.\n\n"
  338. "The message bubbles will disappear after a timeout period, or when the mouse is clicked.",
  339. bubbleMessage);
  340. }
  341. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonsPage)
  342. };
  343. //==============================================================================
  344. struct MiscPage : public Component
  345. {
  346. MiscPage()
  347. : textEditor2 ("Password", (juce_wchar) 0x2022),
  348. comboBox ("Combo")
  349. {
  350. addAndMakeVisible (textEditor1);
  351. textEditor1.setBounds (10, 25, 200, 24);
  352. textEditor1.setText ("Single-line text box");
  353. addAndMakeVisible (textEditor2);
  354. textEditor2.setBounds (10, 55, 200, 24);
  355. textEditor2.setText ("Password");
  356. addAndMakeVisible (comboBox);
  357. comboBox.setBounds (10, 85, 200, 24);
  358. comboBox.setEditableText (true);
  359. comboBox.setJustificationType (Justification::centred);
  360. for (int i = 1; i < 100; ++i)
  361. comboBox.addItem ("combo box item " + String (i), i);
  362. comboBox.setSelectedId (1);
  363. }
  364. TextEditor textEditor1, textEditor2;
  365. ComboBox comboBox;
  366. };
  367. //==============================================================================
  368. class ToolbarDemoComp : public Component,
  369. public SliderListener,
  370. public ButtonListener
  371. {
  372. public:
  373. ToolbarDemoComp()
  374. : depthLabel (String(), "Toolbar depth:"),
  375. infoLabel (String(), "As well as showing off toolbars, this demo illustrates how to store "
  376. "a set of SVG files in a Zip file, embed that in your application, and read "
  377. "them back in at runtime.\n\nThe icon images here are taken from the open-source "
  378. "Tango icon project."),
  379. orientationButton ("Vertical/Horizontal"),
  380. customiseButton ("Customise...")
  381. {
  382. // Create and add the toolbar...
  383. addAndMakeVisible (toolbar);
  384. // And use our item factory to add a set of default icons to it...
  385. toolbar.addDefaultItems (factory);
  386. // Now we'll just create the other sliders and buttons on the demo page, which adjust
  387. // the toolbar's properties...
  388. addAndMakeVisible (infoLabel);
  389. infoLabel.setJustificationType (Justification::topLeft);
  390. infoLabel.setBounds (80, 80, 450, 100);
  391. infoLabel.setInterceptsMouseClicks (false, false);
  392. addAndMakeVisible (depthSlider);
  393. depthSlider.setRange (10.0, 200.0, 1.0);
  394. depthSlider.setValue (50, dontSendNotification);
  395. depthSlider.setSliderStyle (Slider::LinearHorizontal);
  396. depthSlider.setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20);
  397. depthSlider.addListener (this);
  398. depthSlider.setBounds (80, 210, 300, 22);
  399. depthLabel.attachToComponent (&depthSlider, false);
  400. addAndMakeVisible (orientationButton);
  401. orientationButton.addListener (this);
  402. orientationButton.changeWidthToFitText (22);
  403. orientationButton.setTopLeftPosition (depthSlider.getX(), depthSlider.getBottom() + 20);
  404. addAndMakeVisible (customiseButton);
  405. customiseButton.addListener (this);
  406. customiseButton.changeWidthToFitText (22);
  407. customiseButton.setTopLeftPosition (orientationButton.getRight() + 20, orientationButton.getY());
  408. }
  409. void resized() override
  410. {
  411. int toolbarThickness = (int) depthSlider.getValue();
  412. if (toolbar.isVertical())
  413. toolbar.setBounds (getLocalBounds().removeFromLeft (toolbarThickness));
  414. else
  415. toolbar.setBounds (getLocalBounds().removeFromTop (toolbarThickness));
  416. }
  417. void sliderValueChanged (Slider*) override
  418. {
  419. resized();
  420. }
  421. void buttonClicked (Button* button) override
  422. {
  423. if (button == &orientationButton)
  424. {
  425. toolbar.setVertical (! toolbar.isVertical());
  426. resized();
  427. }
  428. else if (button == &customiseButton)
  429. {
  430. toolbar.showCustomisationDialog (factory);
  431. }
  432. }
  433. private:
  434. Toolbar toolbar;
  435. Slider depthSlider;
  436. Label depthLabel, infoLabel;
  437. TextButton orientationButton, customiseButton;
  438. //==============================================================================
  439. class DemoToolbarItemFactory : public ToolbarItemFactory
  440. {
  441. public:
  442. DemoToolbarItemFactory() {}
  443. //==============================================================================
  444. // Each type of item a toolbar can contain must be given a unique ID. These
  445. // are the ones we'll use in this demo.
  446. enum DemoToolbarItemIds
  447. {
  448. doc_new = 1,
  449. doc_open = 2,
  450. doc_save = 3,
  451. doc_saveAs = 4,
  452. edit_copy = 5,
  453. edit_cut = 6,
  454. edit_paste = 7,
  455. juceLogoButton = 8,
  456. customComboBox = 9
  457. };
  458. void getAllToolbarItemIds (Array<int>& ids) override
  459. {
  460. // This returns the complete list of all item IDs that are allowed to
  461. // go in our toolbar. Any items you might want to add must be listed here. The
  462. // order in which they are listed will be used by the toolbar customisation panel.
  463. ids.add (doc_new);
  464. ids.add (doc_open);
  465. ids.add (doc_save);
  466. ids.add (doc_saveAs);
  467. ids.add (edit_copy);
  468. ids.add (edit_cut);
  469. ids.add (edit_paste);
  470. ids.add (juceLogoButton);
  471. ids.add (customComboBox);
  472. // If you're going to use separators, then they must also be added explicitly
  473. // to the list.
  474. ids.add (separatorBarId);
  475. ids.add (spacerId);
  476. ids.add (flexibleSpacerId);
  477. }
  478. void getDefaultItemSet (Array<int>& ids) override
  479. {
  480. // This returns an ordered list of the set of items that make up a
  481. // toolbar's default set. Not all items need to be on this list, and
  482. // items can appear multiple times (e.g. the separators used here).
  483. ids.add (doc_new);
  484. ids.add (doc_open);
  485. ids.add (doc_save);
  486. ids.add (doc_saveAs);
  487. ids.add (spacerId);
  488. ids.add (separatorBarId);
  489. ids.add (edit_copy);
  490. ids.add (edit_cut);
  491. ids.add (edit_paste);
  492. ids.add (separatorBarId);
  493. ids.add (flexibleSpacerId);
  494. ids.add (customComboBox);
  495. ids.add (flexibleSpacerId);
  496. ids.add (separatorBarId);
  497. ids.add (juceLogoButton);
  498. }
  499. ToolbarItemComponent* createItem (int itemId) override
  500. {
  501. switch (itemId)
  502. {
  503. case doc_new: return createButtonFromZipFileSVG (itemId, "new", "document-new.svg");
  504. case doc_open: return createButtonFromZipFileSVG (itemId, "open", "document-open.svg");
  505. case doc_save: return createButtonFromZipFileSVG (itemId, "save", "document-save.svg");
  506. case doc_saveAs: return createButtonFromZipFileSVG (itemId, "save as", "document-save-as.svg");
  507. case edit_copy: return createButtonFromZipFileSVG (itemId, "copy", "edit-copy.svg");
  508. case edit_cut: return createButtonFromZipFileSVG (itemId, "cut", "edit-cut.svg");
  509. case edit_paste: return createButtonFromZipFileSVG (itemId, "paste", "edit-paste.svg");
  510. case juceLogoButton: return new ToolbarButton (itemId, "juce!", Drawable::createFromImageData (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize), 0);
  511. case customComboBox: return new CustomToolbarComboBox (itemId);
  512. default: break;
  513. }
  514. return nullptr;
  515. }
  516. private:
  517. StringArray iconNames;
  518. OwnedArray<Drawable> iconsFromZipFile;
  519. // This is a little utility to create a button with one of the SVG images in
  520. // our embedded ZIP file "icons.zip"
  521. ToolbarButton* createButtonFromZipFileSVG (const int itemId, const String& text, const String& filename)
  522. {
  523. if (iconsFromZipFile.size() == 0)
  524. {
  525. // If we've not already done so, load all the images from the zip file..
  526. MemoryInputStream iconsFileStream (BinaryData::icons_zip, BinaryData::icons_zipSize, false);
  527. ZipFile icons (&iconsFileStream, false);
  528. for (int i = 0; i < icons.getNumEntries(); ++i)
  529. {
  530. ScopedPointer<InputStream> svgFileStream (icons.createStreamForEntry (i));
  531. if (svgFileStream != nullptr)
  532. {
  533. iconNames.add (icons.getEntry(i)->filename);
  534. iconsFromZipFile.add (Drawable::createFromImageDataStream (*svgFileStream));
  535. }
  536. }
  537. }
  538. Drawable* image = iconsFromZipFile [iconNames.indexOf (filename)]->createCopy();
  539. return new ToolbarButton (itemId, text, image, 0);
  540. }
  541. // Demonstrates how to put a custom component into a toolbar - this one contains
  542. // a ComboBox.
  543. class CustomToolbarComboBox : public ToolbarItemComponent
  544. {
  545. public:
  546. CustomToolbarComboBox (const int toolbarItemId)
  547. : ToolbarItemComponent (toolbarItemId, "Custom Toolbar Item", false),
  548. comboBox ("demo toolbar combo box")
  549. {
  550. addAndMakeVisible (comboBox);
  551. for (int i = 1; i < 20; ++i)
  552. comboBox.addItem ("Toolbar ComboBox item " + String (i), i);
  553. comboBox.setSelectedId (1);
  554. comboBox.setEditableText (true);
  555. }
  556. bool getToolbarItemSizes (int /*toolbarDepth*/, bool isVertical,
  557. int& preferredSize, int& minSize, int& maxSize) override
  558. {
  559. if (isVertical)
  560. return false;
  561. preferredSize = 250;
  562. minSize = 80;
  563. maxSize = 300;
  564. return true;
  565. }
  566. void paintButtonArea (Graphics&, int, int, bool, bool) override
  567. {
  568. }
  569. void contentAreaChanged (const Rectangle<int>& newArea) override
  570. {
  571. comboBox.setSize (newArea.getWidth() - 2,
  572. jmin (newArea.getHeight() - 2, 22));
  573. comboBox.setCentrePosition (newArea.getCentreX(), newArea.getCentreY());
  574. }
  575. private:
  576. ComboBox comboBox;
  577. };
  578. };
  579. DemoToolbarItemFactory factory;
  580. };
  581. //==============================================================================
  582. /**
  583. This class shows how to implement a TableListBoxModel to show in a TableListBox.
  584. */
  585. class TableDemoComponent : public Component,
  586. public TableListBoxModel
  587. {
  588. public:
  589. TableDemoComponent() : font (14.0f)
  590. {
  591. // Load some data from an embedded XML file..
  592. loadData();
  593. // Create our table component and add it to this component..
  594. addAndMakeVisible (table);
  595. table.setModel (this);
  596. // give it a border
  597. table.setColour (ListBox::outlineColourId, Colours::grey);
  598. table.setOutlineThickness (1);
  599. // Add some columns to the table header, based on the column list in our database..
  600. forEachXmlChildElement (*columnList, columnXml)
  601. {
  602. table.getHeader().addColumn (columnXml->getStringAttribute ("name"),
  603. columnXml->getIntAttribute ("columnId"),
  604. columnXml->getIntAttribute ("width"),
  605. 50, 400,
  606. TableHeaderComponent::defaultFlags);
  607. }
  608. // we could now change some initial settings..
  609. table.getHeader().setSortColumnId (1, true); // sort forwards by the ID column
  610. table.getHeader().setColumnVisible (7, false); // hide the "length" column until the user shows it
  611. // un-comment this line to have a go of stretch-to-fit mode
  612. // table.getHeader().setStretchToFitActive (true);
  613. table.setMultipleSelectionEnabled (true);
  614. }
  615. // This is overloaded from TableListBoxModel, and must return the total number of rows in our table
  616. int getNumRows() override
  617. {
  618. return numRows;
  619. }
  620. // This is overloaded from TableListBoxModel, and should fill in the background of the whole row
  621. void paintRowBackground (Graphics& g, int rowNumber, int /*width*/, int /*height*/, bool rowIsSelected) override
  622. {
  623. if (rowIsSelected)
  624. g.fillAll (Colours::lightblue);
  625. else if (rowNumber % 2)
  626. g.fillAll (Colour (0xffeeeeee));
  627. }
  628. // This is overloaded from TableListBoxModel, and must paint any cells that aren't using custom
  629. // components.
  630. void paintCell (Graphics& g, int rowNumber, int columnId,
  631. int width, int height, bool /*rowIsSelected*/) override
  632. {
  633. g.setColour (Colours::black);
  634. g.setFont (font);
  635. if (const XmlElement* rowElement = dataList->getChildElement (rowNumber))
  636. {
  637. const String text (rowElement->getStringAttribute (getAttributeNameForColumnId (columnId)));
  638. g.drawText (text, 2, 0, width - 4, height, Justification::centredLeft, true);
  639. }
  640. g.setColour (Colours::black.withAlpha (0.2f));
  641. g.fillRect (width - 1, 0, 1, height);
  642. }
  643. // This is overloaded from TableListBoxModel, and tells us that the user has clicked a table header
  644. // to change the sort order.
  645. void sortOrderChanged (int newSortColumnId, bool isForwards) override
  646. {
  647. if (newSortColumnId != 0)
  648. {
  649. DemoDataSorter sorter (getAttributeNameForColumnId (newSortColumnId), isForwards);
  650. dataList->sortChildElements (sorter);
  651. table.updateContent();
  652. }
  653. }
  654. // This is overloaded from TableListBoxModel, and must update any custom components that we're using
  655. Component* refreshComponentForCell (int rowNumber, int columnId, bool /*isRowSelected*/,
  656. Component* existingComponentToUpdate) override
  657. {
  658. if (columnId == 1 || columnId == 7) // The ID and Length columns do not have a custom component
  659. {
  660. jassert (existingComponentToUpdate == nullptr);
  661. return nullptr;
  662. }
  663. if (columnId == 5) // For the ratings column, we return the custom combobox component
  664. {
  665. RatingColumnCustomComponent* ratingsBox = static_cast<RatingColumnCustomComponent*> (existingComponentToUpdate);
  666. // If an existing component is being passed-in for updating, we'll re-use it, but
  667. // if not, we'll have to create one.
  668. if (ratingsBox == nullptr)
  669. ratingsBox = new RatingColumnCustomComponent (*this);
  670. ratingsBox->setRowAndColumn (rowNumber, columnId);
  671. return ratingsBox;
  672. }
  673. // The other columns are editable text columns, for which we use the custom Label component
  674. EditableTextCustomComponent* textLabel = static_cast<EditableTextCustomComponent*> (existingComponentToUpdate);
  675. // same as above...
  676. if (textLabel == nullptr)
  677. textLabel = new EditableTextCustomComponent (*this);
  678. textLabel->setRowAndColumn (rowNumber, columnId);
  679. return textLabel;
  680. }
  681. // This is overloaded from TableListBoxModel, and should choose the best width for the specified
  682. // column.
  683. int getColumnAutoSizeWidth (int columnId) override
  684. {
  685. if (columnId == 5)
  686. return 100; // (this is the ratings column, containing a custom combobox component)
  687. int widest = 32;
  688. // find the widest bit of text in this column..
  689. for (int i = getNumRows(); --i >= 0;)
  690. {
  691. if (const XmlElement* rowElement = dataList->getChildElement (i))
  692. {
  693. const String text (rowElement->getStringAttribute (getAttributeNameForColumnId (columnId)));
  694. widest = jmax (widest, font.getStringWidth (text));
  695. }
  696. }
  697. return widest + 8;
  698. }
  699. // A couple of quick methods to set and get cell values when the user changes them
  700. int getRating (const int rowNumber) const
  701. {
  702. return dataList->getChildElement (rowNumber)->getIntAttribute ("Rating");
  703. }
  704. void setRating (const int rowNumber, const int newRating)
  705. {
  706. dataList->getChildElement (rowNumber)->setAttribute ("Rating", newRating);
  707. }
  708. String getText (const int columnNumber, const int rowNumber) const
  709. {
  710. return dataList->getChildElement (rowNumber)->getStringAttribute ( getAttributeNameForColumnId(columnNumber));
  711. }
  712. void setText (const int columnNumber, const int rowNumber, const String& newText)
  713. {
  714. const String& columnName = table.getHeader().getColumnName (columnNumber);
  715. dataList->getChildElement (rowNumber)->setAttribute (columnName, newText);
  716. }
  717. //==============================================================================
  718. void resized() override
  719. {
  720. // position our table with a gap around its edge
  721. table.setBoundsInset (BorderSize<int> (8));
  722. }
  723. private:
  724. TableListBox table; // the table component itself
  725. Font font;
  726. ScopedPointer<XmlElement> demoData; // This is the XML document loaded from the embedded file "demo table data.xml"
  727. XmlElement* columnList; // A pointer to the sub-node of demoData that contains the list of columns
  728. XmlElement* dataList; // A pointer to the sub-node of demoData that contains the list of data rows
  729. int numRows; // The number of rows of data we've got
  730. //==============================================================================
  731. // This is a custom Label component, which we use for the table's editable text columns.
  732. class EditableTextCustomComponent : public Label
  733. {
  734. public:
  735. EditableTextCustomComponent (TableDemoComponent& td) : owner (td)
  736. {
  737. // double click to edit the label text; single click handled below
  738. setEditable (false, true, false);
  739. setColour (textColourId, Colours::black);
  740. }
  741. void mouseDown (const MouseEvent& event) override
  742. {
  743. // single click on the label should simply select the row
  744. owner.table.selectRowsBasedOnModifierKeys (row, event.mods, false);
  745. Label::mouseDown (event);
  746. }
  747. void textWasEdited() override
  748. {
  749. owner.setText (columnId, row, getText());
  750. }
  751. // Our demo code will call this when we may need to update our contents
  752. void setRowAndColumn (const int newRow, const int newColumn)
  753. {
  754. row = newRow;
  755. columnId = newColumn;
  756. setText (owner.getText(columnId, row), dontSendNotification);
  757. }
  758. private:
  759. TableDemoComponent& owner;
  760. int row, columnId;
  761. };
  762. //==============================================================================
  763. // This is a custom component containing a combo box, which we're going to put inside
  764. // our table's "rating" column.
  765. class RatingColumnCustomComponent : public Component,
  766. private ComboBoxListener
  767. {
  768. public:
  769. RatingColumnCustomComponent (TableDemoComponent& td) : owner (td)
  770. {
  771. // just put a combo box inside this component
  772. addAndMakeVisible (comboBox);
  773. comboBox.addItem ("fab", 1);
  774. comboBox.addItem ("groovy", 2);
  775. comboBox.addItem ("hep", 3);
  776. comboBox.addItem ("mad for it", 4);
  777. comboBox.addItem ("neat", 5);
  778. comboBox.addItem ("swingin", 6);
  779. comboBox.addItem ("wild", 7);
  780. // when the combo is changed, we'll get a callback.
  781. comboBox.addListener (this);
  782. comboBox.setWantsKeyboardFocus (false);
  783. }
  784. void resized() override
  785. {
  786. comboBox.setBoundsInset (BorderSize<int> (2));
  787. }
  788. // Our demo code will call this when we may need to update our contents
  789. void setRowAndColumn (int newRow, int newColumn)
  790. {
  791. row = newRow;
  792. columnId = newColumn;
  793. comboBox.setSelectedId (owner.getRating (row), dontSendNotification);
  794. }
  795. void comboBoxChanged (ComboBox*) override
  796. {
  797. owner.setRating (row, comboBox.getSelectedId());
  798. }
  799. private:
  800. TableDemoComponent& owner;
  801. ComboBox comboBox;
  802. int row, columnId;
  803. };
  804. //==============================================================================
  805. // A comparator used to sort our data when the user clicks a column header
  806. class DemoDataSorter
  807. {
  808. public:
  809. DemoDataSorter (const String& attributeToSortBy, bool forwards)
  810. : attributeToSort (attributeToSortBy),
  811. direction (forwards ? 1 : -1)
  812. {
  813. }
  814. int compareElements (XmlElement* first, XmlElement* second) const
  815. {
  816. int result = first->getStringAttribute (attributeToSort)
  817. .compareNatural (second->getStringAttribute (attributeToSort));
  818. if (result == 0)
  819. result = first->getStringAttribute ("ID")
  820. .compareNatural (second->getStringAttribute ("ID"));
  821. return direction * result;
  822. }
  823. private:
  824. String attributeToSort;
  825. int direction;
  826. };
  827. //==============================================================================
  828. // this loads the embedded database XML file into memory
  829. void loadData()
  830. {
  831. demoData = XmlDocument::parse (BinaryData::demo_table_data_xml);
  832. dataList = demoData->getChildByName ("DATA");
  833. columnList = demoData->getChildByName ("COLUMNS");
  834. numRows = dataList->getNumChildElements();
  835. }
  836. // (a utility method to search our XML for the attribute that matches a column ID)
  837. String getAttributeNameForColumnId (const int columnId) const
  838. {
  839. forEachXmlChildElement (*columnList, columnXml)
  840. {
  841. if (columnXml->getIntAttribute ("columnId") == columnId)
  842. return columnXml->getStringAttribute ("name");
  843. }
  844. return String();
  845. }
  846. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableDemoComponent)
  847. };
  848. //==============================================================================
  849. class DragAndDropDemo : public Component,
  850. public DragAndDropContainer
  851. {
  852. public:
  853. DragAndDropDemo()
  854. : sourceListBox ("D+D source", nullptr)
  855. {
  856. setName ("Drag-and-Drop");
  857. sourceListBox.setModel (&sourceModel);
  858. sourceListBox.setMultipleSelectionEnabled (true);
  859. addAndMakeVisible (sourceListBox);
  860. addAndMakeVisible (target);
  861. }
  862. void resized() override
  863. {
  864. Rectangle<int> r (getLocalBounds().reduced (8));
  865. sourceListBox.setBounds (r.withSize (250, 180));
  866. target.setBounds (r.removeFromBottom (150).removeFromRight (250));
  867. }
  868. private:
  869. //==============================================================================
  870. struct SourceItemListboxContents : public ListBoxModel
  871. {
  872. // The following methods implement the necessary virtual functions from ListBoxModel,
  873. // telling the listbox how many rows there are, painting them, etc.
  874. int getNumRows() override
  875. {
  876. return 30;
  877. }
  878. void paintListBoxItem (int rowNumber, Graphics& g,
  879. int width, int height, bool rowIsSelected) override
  880. {
  881. if (rowIsSelected)
  882. g.fillAll (Colours::lightblue);
  883. g.setColour (Colours::black);
  884. g.setFont (height * 0.7f);
  885. g.drawText ("Draggable Thing #" + String (rowNumber + 1),
  886. 5, 0, width, height,
  887. Justification::centredLeft, true);
  888. }
  889. var getDragSourceDescription (const SparseSet<int>& selectedRows) override
  890. {
  891. // for our drag description, we'll just make a comma-separated list of the selected row
  892. // numbers - this will be picked up by the drag target and displayed in its box.
  893. StringArray rows;
  894. for (int i = 0; i < selectedRows.size(); ++i)
  895. rows.add (String (selectedRows[i] + 1));
  896. return rows.joinIntoString (", ");
  897. }
  898. };
  899. //==============================================================================
  900. // and this is a component that can have things dropped onto it..
  901. class DragAndDropDemoTarget : public Component,
  902. public DragAndDropTarget,
  903. public FileDragAndDropTarget,
  904. public TextDragAndDropTarget
  905. {
  906. public:
  907. DragAndDropDemoTarget()
  908. : message ("Drag-and-drop some rows from the top-left box onto this component!\n\n"
  909. "You can also drag-and-drop files and text from other apps"),
  910. somethingIsBeingDraggedOver (false)
  911. {
  912. }
  913. void paint (Graphics& g) override
  914. {
  915. g.fillAll (Colours::green.withAlpha (0.2f));
  916. // draw a red line around the comp if the user's currently dragging something over it..
  917. if (somethingIsBeingDraggedOver)
  918. {
  919. g.setColour (Colours::red);
  920. g.drawRect (getLocalBounds(), 3);
  921. }
  922. g.setColour (Colours::black);
  923. g.setFont (14.0f);
  924. g.drawFittedText (message, getLocalBounds().reduced (10, 0), Justification::centred, 4);
  925. }
  926. //==============================================================================
  927. // These methods implement the DragAndDropTarget interface, and allow our component
  928. // to accept drag-and-drop of objects from other Juce components..
  929. bool isInterestedInDragSource (const SourceDetails& /*dragSourceDetails*/) override
  930. {
  931. // normally you'd check the sourceDescription value to see if it's the
  932. // sort of object that you're interested in before returning true, but for
  933. // the demo, we'll say yes to anything..
  934. return true;
  935. }
  936. void itemDragEnter (const SourceDetails& /*dragSourceDetails*/) override
  937. {
  938. somethingIsBeingDraggedOver = true;
  939. repaint();
  940. }
  941. void itemDragMove (const SourceDetails& /*dragSourceDetails*/) override
  942. {
  943. }
  944. void itemDragExit (const SourceDetails& /*dragSourceDetails*/) override
  945. {
  946. somethingIsBeingDraggedOver = false;
  947. repaint();
  948. }
  949. void itemDropped (const SourceDetails& dragSourceDetails) override
  950. {
  951. message = "Items dropped: " + dragSourceDetails.description.toString();
  952. somethingIsBeingDraggedOver = false;
  953. repaint();
  954. }
  955. //==============================================================================
  956. // These methods implement the FileDragAndDropTarget interface, and allow our component
  957. // to accept drag-and-drop of files..
  958. bool isInterestedInFileDrag (const StringArray& /*files*/) override
  959. {
  960. // normally you'd check these files to see if they're something that you're
  961. // interested in before returning true, but for the demo, we'll say yes to anything..
  962. return true;
  963. }
  964. void fileDragEnter (const StringArray& /*files*/, int /*x*/, int /*y*/) override
  965. {
  966. somethingIsBeingDraggedOver = true;
  967. repaint();
  968. }
  969. void fileDragMove (const StringArray& /*files*/, int /*x*/, int /*y*/) override
  970. {
  971. }
  972. void fileDragExit (const StringArray& /*files*/) override
  973. {
  974. somethingIsBeingDraggedOver = false;
  975. repaint();
  976. }
  977. void filesDropped (const StringArray& files, int /*x*/, int /*y*/) override
  978. {
  979. message = "Files dropped: " + files.joinIntoString ("\n");
  980. somethingIsBeingDraggedOver = false;
  981. repaint();
  982. }
  983. //==============================================================================
  984. // These methods implement the TextDragAndDropTarget interface, and allow our component
  985. // to accept drag-and-drop of text..
  986. bool isInterestedInTextDrag (const String& /*text*/) override
  987. {
  988. return true;
  989. }
  990. void textDragEnter (const String& /*text*/, int /*x*/, int /*y*/) override
  991. {
  992. somethingIsBeingDraggedOver = true;
  993. repaint();
  994. }
  995. void textDragMove (const String& /*text*/, int /*x*/, int /*y*/) override
  996. {
  997. }
  998. void textDragExit (const String& /*text*/) override
  999. {
  1000. somethingIsBeingDraggedOver = false;
  1001. repaint();
  1002. }
  1003. void textDropped (const String& text, int /*x*/, int /*y*/) override
  1004. {
  1005. message = "Text dropped:\n" + text;
  1006. somethingIsBeingDraggedOver = false;
  1007. repaint();
  1008. }
  1009. private:
  1010. String message;
  1011. bool somethingIsBeingDraggedOver;
  1012. };
  1013. //==============================================================================
  1014. ListBox sourceListBox;
  1015. SourceItemListboxContents sourceModel;
  1016. DragAndDropDemoTarget target;
  1017. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragAndDropDemo)
  1018. };
  1019. //==============================================================================
  1020. class MenusDemo : public Component,
  1021. public MenuBarModel,
  1022. private Button::Listener
  1023. {
  1024. public:
  1025. MenusDemo()
  1026. {
  1027. addAndMakeVisible (menuBar = new MenuBarComponent (this));
  1028. popupButton.setButtonText ("Show Popup Menu");
  1029. popupButton.setTriggeredOnMouseDown (true);
  1030. popupButton.addListener (this);
  1031. addAndMakeVisible (popupButton);
  1032. setApplicationCommandManagerToWatch (&MainAppWindow::getApplicationCommandManager());
  1033. }
  1034. ~MenusDemo()
  1035. {
  1036. #if JUCE_MAC
  1037. MenuBarModel::setMacMainMenu (nullptr);
  1038. #endif
  1039. PopupMenu::dismissAllActiveMenus();
  1040. popupButton.removeListener (this);
  1041. }
  1042. void resized() override
  1043. {
  1044. Rectangle<int> area (getLocalBounds());
  1045. menuBar->setBounds (area.removeFromTop (LookAndFeel::getDefaultLookAndFeel().getDefaultMenuBarHeight()));
  1046. area.removeFromTop (20);
  1047. area = area.removeFromTop (33);
  1048. popupButton.setBounds (area.removeFromLeft (200).reduced (5));
  1049. }
  1050. //==============================================================================
  1051. StringArray getMenuBarNames() override
  1052. {
  1053. const char* const names[] = { "Demo", "Look-and-feel", "Tabs", "Misc", nullptr };
  1054. return StringArray (names);
  1055. }
  1056. PopupMenu getMenuForIndex (int menuIndex, const String& /*menuName*/) override
  1057. {
  1058. ApplicationCommandManager* commandManager = &MainAppWindow::getApplicationCommandManager();
  1059. PopupMenu menu;
  1060. if (menuIndex == 0)
  1061. {
  1062. menu.addCommandItem (commandManager, MainAppWindow::showPreviousDemo);
  1063. menu.addCommandItem (commandManager, MainAppWindow::showNextDemo);
  1064. menu.addSeparator();
  1065. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  1066. }
  1067. else if (menuIndex == 1)
  1068. {
  1069. menu.addCommandItem (commandManager, MainAppWindow::useLookAndFeelV1);
  1070. menu.addCommandItem (commandManager, MainAppWindow::useLookAndFeelV2);
  1071. menu.addCommandItem (commandManager, MainAppWindow::useLookAndFeelV3);
  1072. menu.addSeparator();
  1073. menu.addCommandItem (commandManager, MainAppWindow::useNativeTitleBar);
  1074. #if JUCE_MAC
  1075. menu.addItem (6000, "Use Native Menu Bar");
  1076. #endif
  1077. #if ! JUCE_LINUX
  1078. menu.addCommandItem (commandManager, MainAppWindow::goToKioskMode);
  1079. #endif
  1080. if (MainAppWindow* mainWindow = MainAppWindow::getMainAppWindow())
  1081. {
  1082. StringArray engines (mainWindow->getRenderingEngines());
  1083. if (engines.size() > 1)
  1084. {
  1085. menu.addSeparator();
  1086. for (int i = 0; i < engines.size(); ++i)
  1087. menu.addCommandItem (commandManager, MainAppWindow::renderingEngineOne + i);
  1088. }
  1089. }
  1090. }
  1091. else if (menuIndex == 2)
  1092. {
  1093. if (TabbedComponent* tabs = findParentComponentOfClass<TabbedComponent>())
  1094. {
  1095. menu.addItem (3000, "Tabs at Top", true, tabs->getOrientation() == TabbedButtonBar::TabsAtTop);
  1096. menu.addItem (3001, "Tabs at Bottom", true, tabs->getOrientation() == TabbedButtonBar::TabsAtBottom);
  1097. menu.addItem (3002, "Tabs on Left", true, tabs->getOrientation() == TabbedButtonBar::TabsAtLeft);
  1098. menu.addItem (3003, "Tabs on Right", true, tabs->getOrientation() == TabbedButtonBar::TabsAtRight);
  1099. }
  1100. }
  1101. else if (menuIndex == 3)
  1102. {
  1103. return getDummyPopupMenu();
  1104. }
  1105. return menu;
  1106. }
  1107. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/) override
  1108. {
  1109. // most of our menu items are invoked automatically as commands, but we can handle the
  1110. // other special cases here..
  1111. if (menuItemID == 6000)
  1112. {
  1113. #if JUCE_MAC
  1114. if (MenuBarModel::getMacMainMenu() != nullptr)
  1115. {
  1116. MenuBarModel::setMacMainMenu (nullptr);
  1117. menuBar->setModel (this);
  1118. }
  1119. else
  1120. {
  1121. menuBar->setModel (nullptr);
  1122. MenuBarModel::setMacMainMenu (this);
  1123. }
  1124. #endif
  1125. }
  1126. else if (menuItemID >= 3000 && menuItemID <= 3003)
  1127. {
  1128. if (TabbedComponent* tabs = findParentComponentOfClass<TabbedComponent>())
  1129. {
  1130. TabbedButtonBar::Orientation o = TabbedButtonBar::TabsAtTop;
  1131. if (menuItemID == 3001) o = TabbedButtonBar::TabsAtBottom;
  1132. if (menuItemID == 3002) o = TabbedButtonBar::TabsAtLeft;
  1133. if (menuItemID == 3003) o = TabbedButtonBar::TabsAtRight;
  1134. tabs->setOrientation (o);
  1135. }
  1136. }
  1137. }
  1138. private:
  1139. TextButton popupButton;
  1140. ScopedPointer<MenuBarComponent> menuBar;
  1141. PopupMenu getDummyPopupMenu()
  1142. {
  1143. PopupMenu m;
  1144. m.addItem (1, "Normal item");
  1145. m.addItem (2, "Disabled item", false);
  1146. m.addItem (3, "Ticked item", true, true);
  1147. m.addColouredItem (4, "Coloured item", Colours::green);
  1148. m.addSeparator();
  1149. m.addCustomItem (5, new CustomMenuComponent());
  1150. m.addSeparator();
  1151. for (int i = 0; i < 8; ++i)
  1152. {
  1153. PopupMenu subMenu;
  1154. for (int s = 0; s < 8; ++s)
  1155. {
  1156. PopupMenu subSubMenu;
  1157. for (int item = 0; item < 8; ++item)
  1158. subSubMenu.addItem (1000 + (i * s * item), "Item " + String (item + 1));
  1159. subMenu.addSubMenu ("Sub-sub menu " + String (s + 1), subSubMenu);
  1160. }
  1161. m.addSubMenu ("Sub menu " + String (i + 1), subMenu);
  1162. }
  1163. return m;
  1164. }
  1165. //==============================================================================
  1166. void buttonClicked (Button* button) override
  1167. {
  1168. if (button == &popupButton)
  1169. getDummyPopupMenu().showMenuAsync (PopupMenu::Options().withTargetComponent (&popupButton), nullptr);
  1170. }
  1171. //==============================================================================
  1172. class CustomMenuComponent : public PopupMenu::CustomComponent,
  1173. private Timer
  1174. {
  1175. public:
  1176. CustomMenuComponent()
  1177. {
  1178. // set off a timer to move a blob around on this component every
  1179. // 300 milliseconds - see the timerCallback() method.
  1180. startTimer (300);
  1181. }
  1182. void getIdealSize (int& idealWidth, int& idealHeight) override
  1183. {
  1184. // tells the menu how big we'd like to be..
  1185. idealWidth = 200;
  1186. idealHeight = 60;
  1187. }
  1188. void paint (Graphics& g) override
  1189. {
  1190. g.fillAll (Colours::yellow.withAlpha (0.3f));
  1191. g.setColour (Colours::pink);
  1192. g.fillEllipse (blobPosition);
  1193. g.setFont (Font (14.0f, Font::italic));
  1194. g.setColour (Colours::black);
  1195. g.drawFittedText ("This is a customised menu item (also demonstrating the Timer class)...",
  1196. getLocalBounds().reduced (4, 0),
  1197. Justification::centred, 3);
  1198. }
  1199. private:
  1200. void timerCallback() override
  1201. {
  1202. Random random;
  1203. blobPosition.setBounds ((float) random.nextInt (getWidth()),
  1204. (float) random.nextInt (getHeight()),
  1205. 40.0f, 30.0f);
  1206. repaint();
  1207. }
  1208. Rectangle<float> blobPosition;
  1209. };
  1210. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MenusDemo)
  1211. };
  1212. //==============================================================================
  1213. class DemoTabbedComponent : public TabbedComponent
  1214. {
  1215. public:
  1216. DemoTabbedComponent()
  1217. : TabbedComponent (TabbedButtonBar::TabsAtTop)
  1218. {
  1219. addTab ("Menus", getRandomTabBackgroundColour(), new MenusDemo(), true);
  1220. addTab ("Buttons", getRandomTabBackgroundColour(), new ButtonsPage(), true);
  1221. addTab ("Sliders", getRandomTabBackgroundColour(), new SlidersPage(), true);
  1222. addTab ("Toolbars", getRandomTabBackgroundColour(), new ToolbarDemoComp(), true);
  1223. addTab ("Misc", getRandomTabBackgroundColour(), new MiscPage(), true);
  1224. addTab ("Tables", getRandomTabBackgroundColour(), new TableDemoComponent(), true);
  1225. addTab ("Drag & Drop", getRandomTabBackgroundColour(), new DragAndDropDemo(), true);
  1226. getTabbedButtonBar().getTabButton (5)->setExtraComponent (new CustomTabButton(), TabBarButton::afterText);
  1227. }
  1228. static Colour getRandomTabBackgroundColour()
  1229. {
  1230. return Colour (Random::getSystemRandom().nextFloat(), 0.1f, 0.97f, 1.0f);
  1231. }
  1232. // This is a small star button that is put inside one of the tabs. You can
  1233. // use this technique to create things like "close tab" buttons, etc.
  1234. class CustomTabButton : public Component
  1235. {
  1236. public:
  1237. CustomTabButton()
  1238. {
  1239. setSize (20, 20);
  1240. }
  1241. void paint (Graphics& g) override
  1242. {
  1243. Path star;
  1244. star.addStar (Point<float>(), 7, 1.0f, 2.0f);
  1245. g.setColour (Colours::green);
  1246. g.fillPath (star, star.getTransformToScaleToFit (getLocalBounds().reduced (2).toFloat(), true));
  1247. }
  1248. void mouseDown (const MouseEvent&) override
  1249. {
  1250. showBubbleMessage (this,
  1251. "This is a custom tab component\n"
  1252. "\n"
  1253. "You can use these to implement things like close-buttons "
  1254. "or status displays for your tabs.",
  1255. bubbleMessage);
  1256. }
  1257. private:
  1258. ScopedPointer<BubbleMessageComponent> bubbleMessage;
  1259. };
  1260. };
  1261. //==============================================================================
  1262. class WidgetsDemo : public Component
  1263. {
  1264. public:
  1265. WidgetsDemo()
  1266. {
  1267. setOpaque (true);
  1268. addAndMakeVisible (tabs);
  1269. }
  1270. void paint (Graphics& g) override
  1271. {
  1272. g.fillAll (Colours::white);
  1273. }
  1274. void resized() override
  1275. {
  1276. tabs.setBounds (getLocalBounds().reduced (4));
  1277. }
  1278. private:
  1279. DemoTabbedComponent tabs;
  1280. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WidgetsDemo)
  1281. };
  1282. // This static object will register this demo type in a global list of demos..
  1283. static JuceDemoType<WidgetsDemo> demo ("09 Components: Tabs & Widgets");