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.

1555 lines
57KB

  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. else if (rowNumber % 2)
  624. g.fillAll (Colour (0xffeeeeee));
  625. }
  626. // This is overloaded from TableListBoxModel, and must paint any cells that aren't using custom
  627. // components.
  628. void paintCell (Graphics& g, int rowNumber, int columnId,
  629. int width, int height, bool /*rowIsSelected*/) override
  630. {
  631. g.setColour (Colours::black);
  632. g.setFont (font);
  633. const XmlElement* rowElement = dataList->getChildElement (rowNumber);
  634. if (rowElement != 0)
  635. {
  636. const String text (rowElement->getStringAttribute (getAttributeNameForColumnId (columnId)));
  637. g.drawText (text, 2, 0, width - 4, height, Justification::centredLeft, true);
  638. }
  639. g.setColour (Colours::black.withAlpha (0.2f));
  640. g.fillRect (width - 1, 0, 1, height);
  641. }
  642. // This is overloaded from TableListBoxModel, and tells us that the user has clicked a table header
  643. // to change the sort order.
  644. void sortOrderChanged (int newSortColumnId, bool isForwards) override
  645. {
  646. if (newSortColumnId != 0)
  647. {
  648. DemoDataSorter sorter (getAttributeNameForColumnId (newSortColumnId), isForwards);
  649. dataList->sortChildElements (sorter);
  650. table.updateContent();
  651. }
  652. }
  653. // This is overloaded from TableListBoxModel, and must update any custom components that we're using
  654. Component* refreshComponentForCell (int rowNumber, int columnId, bool /*isRowSelected*/,
  655. Component* existingComponentToUpdate) override
  656. {
  657. if (columnId == 1 || columnId == 7) // The ID and Length columns do not have a custom component
  658. {
  659. jassert (existingComponentToUpdate == 0);
  660. return 0;
  661. }
  662. else if (columnId == 5) // For the ratings column, we return the custom combobox component
  663. {
  664. RatingColumnCustomComponent* ratingsBox = (RatingColumnCustomComponent*) existingComponentToUpdate;
  665. // If an existing component is being passed-in for updating, we'll re-use it, but
  666. // if not, we'll have to create one.
  667. if (ratingsBox == 0)
  668. ratingsBox = new RatingColumnCustomComponent (*this);
  669. ratingsBox->setRowAndColumn (rowNumber, columnId);
  670. return ratingsBox;
  671. }
  672. else // The other columns are editable text columns, for which we use the custom Label component
  673. {
  674. EditableTextCustomComponent* textLabel = (EditableTextCustomComponent*) existingComponentToUpdate;
  675. // same as above...
  676. if (textLabel == 0)
  677. textLabel = new EditableTextCustomComponent (*this);
  678. textLabel->setRowAndColumn (rowNumber, columnId);
  679. return textLabel;
  680. }
  681. }
  682. // This is overloaded from TableListBoxModel, and should choose the best width for the specified
  683. // column.
  684. int getColumnAutoSizeWidth (int columnId) override
  685. {
  686. if (columnId == 5)
  687. return 100; // (this is the ratings column, containing a custom combobox component)
  688. int widest = 32;
  689. // find the widest bit of text in this column..
  690. for (int i = getNumRows(); --i >= 0;)
  691. {
  692. const XmlElement* rowElement = dataList->getChildElement (i);
  693. if (rowElement != 0)
  694. {
  695. const String text (rowElement->getStringAttribute (getAttributeNameForColumnId (columnId)));
  696. widest = jmax (widest, font.getStringWidth (text));
  697. }
  698. }
  699. return widest + 8;
  700. }
  701. // A couple of quick methods to set and get cell values when the user changes them
  702. int getRating (const int rowNumber) const
  703. {
  704. return dataList->getChildElement (rowNumber)->getIntAttribute ("Rating");
  705. }
  706. void setRating (const int rowNumber, const int newRating)
  707. {
  708. dataList->getChildElement (rowNumber)->setAttribute ("Rating", newRating);
  709. }
  710. String getText (const int columnNumber, const int rowNumber) const
  711. {
  712. return dataList->getChildElement (rowNumber)->getStringAttribute ( getAttributeNameForColumnId(columnNumber));
  713. }
  714. void setText (const int columnNumber, const int rowNumber, const String& newText)
  715. {
  716. const String& columnName = table.getHeader().getColumnName (columnNumber);
  717. dataList->getChildElement (rowNumber)->setAttribute (columnName, newText);
  718. }
  719. //==============================================================================
  720. void resized() override
  721. {
  722. // position our table with a gap around its edge
  723. table.setBoundsInset (BorderSize<int> (8));
  724. }
  725. private:
  726. TableListBox table; // the table component itself
  727. Font font;
  728. ScopedPointer<XmlElement> demoData; // This is the XML document loaded from the embedded file "demo table data.xml"
  729. XmlElement* columnList; // A pointer to the sub-node of demoData that contains the list of columns
  730. XmlElement* dataList; // A pointer to the sub-node of demoData that contains the list of data rows
  731. int numRows; // The number of rows of data we've got
  732. //==============================================================================
  733. // This is a custom Label component, which we use for the table's editable text columns.
  734. class EditableTextCustomComponent : public Label
  735. {
  736. public:
  737. EditableTextCustomComponent (TableDemoComponent& owner_)
  738. : owner (owner_)
  739. {
  740. // double click to edit the label text; single click handled below
  741. setEditable (false, true, false);
  742. setColour (textColourId, Colours::black);
  743. }
  744. void mouseDown (const MouseEvent& event) override
  745. {
  746. // single click on the label should simply select the row
  747. owner.table.selectRowsBasedOnModifierKeys (row, event.mods, false);
  748. Label::mouseDown (event);
  749. }
  750. void textWasEdited() override
  751. {
  752. owner.setText (columnId, row, getText());
  753. }
  754. // Our demo code will call this when we may need to update our contents
  755. void setRowAndColumn (const int newRow, const int newColumn)
  756. {
  757. row = newRow;
  758. columnId = newColumn;
  759. setText (owner.getText(columnId, row), dontSendNotification);
  760. }
  761. private:
  762. TableDemoComponent& owner;
  763. int row, columnId;
  764. };
  765. //==============================================================================
  766. // This is a custom component containing a combo box, which we're going to put inside
  767. // our table's "rating" column.
  768. class RatingColumnCustomComponent : public Component,
  769. public ComboBoxListener
  770. {
  771. public:
  772. RatingColumnCustomComponent (TableDemoComponent& owner_)
  773. : owner (owner_)
  774. {
  775. // just put a combo box inside this component
  776. addAndMakeVisible (comboBox);
  777. comboBox.addItem ("fab", 1);
  778. comboBox.addItem ("groovy", 2);
  779. comboBox.addItem ("hep", 3);
  780. comboBox.addItem ("mad for it", 4);
  781. comboBox.addItem ("neat", 5);
  782. comboBox.addItem ("swingin", 6);
  783. comboBox.addItem ("wild", 7);
  784. // when the combo is changed, we'll get a callback.
  785. comboBox.addListener (this);
  786. comboBox.setWantsKeyboardFocus (false);
  787. }
  788. void resized() override
  789. {
  790. comboBox.setBoundsInset (BorderSize<int> (2));
  791. }
  792. // Our demo code will call this when we may need to update our contents
  793. void setRowAndColumn (const int newRow, const int newColumn)
  794. {
  795. row = newRow;
  796. columnId = newColumn;
  797. comboBox.setSelectedId (owner.getRating (row), dontSendNotification);
  798. }
  799. void comboBoxChanged (ComboBox* /*comboBoxThatHasChanged*/) override
  800. {
  801. owner.setRating (row, comboBox.getSelectedId());
  802. }
  803. private:
  804. TableDemoComponent& owner;
  805. ComboBox comboBox;
  806. int row, columnId;
  807. };
  808. //==============================================================================
  809. // A comparator used to sort our data when the user clicks a column header
  810. class DemoDataSorter
  811. {
  812. public:
  813. DemoDataSorter (const String attributeToSort_, bool forwards)
  814. : attributeToSort (attributeToSort_),
  815. direction (forwards ? 1 : -1)
  816. {
  817. }
  818. int compareElements (XmlElement* first, XmlElement* second) const
  819. {
  820. int result = first->getStringAttribute (attributeToSort)
  821. .compareNatural (second->getStringAttribute (attributeToSort));
  822. if (result == 0)
  823. result = first->getStringAttribute ("ID")
  824. .compareNatural (second->getStringAttribute ("ID"));
  825. return direction * result;
  826. }
  827. private:
  828. String attributeToSort;
  829. int direction;
  830. };
  831. //==============================================================================
  832. // this loads the embedded database XML file into memory
  833. void loadData()
  834. {
  835. XmlDocument dataDoc (String ((const char*) BinaryData::demo_table_data_xml));
  836. demoData = dataDoc.getDocumentElement();
  837. dataList = demoData->getChildByName ("DATA");
  838. columnList = demoData->getChildByName ("COLUMNS");
  839. numRows = dataList->getNumChildElements();
  840. }
  841. // (a utility method to search our XML for the attribute that matches a column ID)
  842. String getAttributeNameForColumnId (const int columnId) const
  843. {
  844. forEachXmlChildElement (*columnList, columnXml)
  845. {
  846. if (columnXml->getIntAttribute ("columnId") == columnId)
  847. return columnXml->getStringAttribute ("name");
  848. }
  849. return String::empty;
  850. }
  851. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableDemoComponent)
  852. };
  853. //==============================================================================
  854. class DragAndDropDemo : public Component,
  855. public DragAndDropContainer
  856. {
  857. public:
  858. DragAndDropDemo()
  859. : sourceListBox ("D+D source", nullptr)
  860. {
  861. setName ("Drag-and-Drop");
  862. sourceListBox.setModel (&sourceModel);
  863. sourceListBox.setMultipleSelectionEnabled (true);
  864. addAndMakeVisible (sourceListBox);
  865. addAndMakeVisible (target);
  866. }
  867. void resized() override
  868. {
  869. Rectangle<int> r (getLocalBounds().reduced (8));
  870. sourceListBox.setBounds (r.withSize (250, 180));
  871. target.setBounds (r.removeFromBottom (150).removeFromRight (250));
  872. }
  873. private:
  874. //==============================================================================
  875. struct SourceItemListboxContents : public ListBoxModel
  876. {
  877. // The following methods implement the necessary virtual functions from ListBoxModel,
  878. // telling the listbox how many rows there are, painting them, etc.
  879. int getNumRows() override
  880. {
  881. return 30;
  882. }
  883. void paintListBoxItem (int rowNumber, Graphics& g,
  884. int width, int height, bool rowIsSelected) override
  885. {
  886. if (rowIsSelected)
  887. g.fillAll (Colours::lightblue);
  888. g.setColour (Colours::black);
  889. g.setFont (height * 0.7f);
  890. g.drawText ("Draggable Thing #" + String (rowNumber + 1),
  891. 5, 0, width, height,
  892. Justification::centredLeft, true);
  893. }
  894. var getDragSourceDescription (const SparseSet<int>& selectedRows) override
  895. {
  896. // for our drag description, we'll just make a comma-separated list of the selected row
  897. // numbers - this will be picked up by the drag target and displayed in its box.
  898. StringArray rows;
  899. for (int i = 0; i < selectedRows.size(); ++i)
  900. rows.add (String (selectedRows[i] + 1));
  901. return rows.joinIntoString (", ");
  902. }
  903. };
  904. //==============================================================================
  905. // and this is a component that can have things dropped onto it..
  906. class DragAndDropDemoTarget : public Component,
  907. public DragAndDropTarget,
  908. public FileDragAndDropTarget,
  909. public TextDragAndDropTarget
  910. {
  911. public:
  912. DragAndDropDemoTarget()
  913. : message ("Drag-and-drop some rows from the top-left box onto this component!\n\n"
  914. "You can also drag-and-drop files and text from other apps"),
  915. somethingIsBeingDraggedOver (false)
  916. {
  917. }
  918. void paint (Graphics& g) override
  919. {
  920. g.fillAll (Colours::green.withAlpha (0.2f));
  921. // draw a red line around the comp if the user's currently dragging something over it..
  922. if (somethingIsBeingDraggedOver)
  923. {
  924. g.setColour (Colours::red);
  925. g.drawRect (getLocalBounds(), 3);
  926. }
  927. g.setColour (Colours::black);
  928. g.setFont (14.0f);
  929. g.drawFittedText (message, getLocalBounds().reduced (10, 0), Justification::centred, 4);
  930. }
  931. //==============================================================================
  932. // These methods implement the DragAndDropTarget interface, and allow our component
  933. // to accept drag-and-drop of objects from other Juce components..
  934. bool isInterestedInDragSource (const SourceDetails& /*dragSourceDetails*/) override
  935. {
  936. // normally you'd check the sourceDescription value to see if it's the
  937. // sort of object that you're interested in before returning true, but for
  938. // the demo, we'll say yes to anything..
  939. return true;
  940. }
  941. void itemDragEnter (const SourceDetails& /*dragSourceDetails*/) override
  942. {
  943. somethingIsBeingDraggedOver = true;
  944. repaint();
  945. }
  946. void itemDragMove (const SourceDetails& /*dragSourceDetails*/) override
  947. {
  948. }
  949. void itemDragExit (const SourceDetails& /*dragSourceDetails*/) override
  950. {
  951. somethingIsBeingDraggedOver = false;
  952. repaint();
  953. }
  954. void itemDropped (const SourceDetails& dragSourceDetails) override
  955. {
  956. message = "Items dropped: " + dragSourceDetails.description.toString();
  957. somethingIsBeingDraggedOver = false;
  958. repaint();
  959. }
  960. //==============================================================================
  961. // These methods implement the FileDragAndDropTarget interface, and allow our component
  962. // to accept drag-and-drop of files..
  963. bool isInterestedInFileDrag (const StringArray& /*files*/) override
  964. {
  965. // normally you'd check these files to see if they're something that you're
  966. // interested in before returning true, but for the demo, we'll say yes to anything..
  967. return true;
  968. }
  969. void fileDragEnter (const StringArray& /*files*/, int /*x*/, int /*y*/) override
  970. {
  971. somethingIsBeingDraggedOver = true;
  972. repaint();
  973. }
  974. void fileDragMove (const StringArray& /*files*/, int /*x*/, int /*y*/) override
  975. {
  976. }
  977. void fileDragExit (const StringArray& /*files*/) override
  978. {
  979. somethingIsBeingDraggedOver = false;
  980. repaint();
  981. }
  982. void filesDropped (const StringArray& files, int /*x*/, int /*y*/) override
  983. {
  984. message = "Files dropped: " + files.joinIntoString ("\n");
  985. somethingIsBeingDraggedOver = false;
  986. repaint();
  987. }
  988. //==============================================================================
  989. // These methods implement the TextDragAndDropTarget interface, and allow our component
  990. // to accept drag-and-drop of text..
  991. bool isInterestedInTextDrag (const String& /*text*/) override
  992. {
  993. return true;
  994. }
  995. void textDragEnter (const String& /*text*/, int /*x*/, int /*y*/) override
  996. {
  997. somethingIsBeingDraggedOver = true;
  998. repaint();
  999. }
  1000. void textDragMove (const String& /*text*/, int /*x*/, int /*y*/) override
  1001. {
  1002. }
  1003. void textDragExit (const String& /*text*/) override
  1004. {
  1005. somethingIsBeingDraggedOver = false;
  1006. repaint();
  1007. }
  1008. void textDropped (const String& text, int /*x*/, int /*y*/) override
  1009. {
  1010. message = "Text dropped:\n" + text;
  1011. somethingIsBeingDraggedOver = false;
  1012. repaint();
  1013. }
  1014. private:
  1015. String message;
  1016. bool somethingIsBeingDraggedOver;
  1017. };
  1018. //==============================================================================
  1019. ListBox sourceListBox;
  1020. SourceItemListboxContents sourceModel;
  1021. DragAndDropDemoTarget target;
  1022. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragAndDropDemo)
  1023. };
  1024. //==============================================================================
  1025. class MenusDemo : public Component,
  1026. public MenuBarModel,
  1027. private Button::Listener
  1028. {
  1029. public:
  1030. MenusDemo()
  1031. {
  1032. addAndMakeVisible (menuBar = new MenuBarComponent (this));
  1033. popupButton.setButtonText ("Show Popup Menu");
  1034. popupButton.setTriggeredOnMouseDown (true);
  1035. popupButton.addListener (this);
  1036. addAndMakeVisible (popupButton);
  1037. }
  1038. ~MenusDemo()
  1039. {
  1040. #if JUCE_MAC
  1041. MenuBarModel::setMacMainMenu (nullptr);
  1042. #endif
  1043. PopupMenu::dismissAllActiveMenus();
  1044. popupButton.removeListener (this);
  1045. }
  1046. void resized() override
  1047. {
  1048. Rectangle<int> area (getLocalBounds());
  1049. menuBar->setBounds (area.removeFromTop (LookAndFeel::getDefaultLookAndFeel().getDefaultMenuBarHeight()));
  1050. area.removeFromTop (20);
  1051. area = area.removeFromTop (33);
  1052. popupButton.setBounds (area.removeFromLeft (200).reduced (5));
  1053. }
  1054. //==============================================================================
  1055. StringArray getMenuBarNames() override
  1056. {
  1057. const char* const names[] = { "Demo", "Look-and-feel", "Tabs", "Misc", nullptr };
  1058. return StringArray (names);
  1059. }
  1060. PopupMenu getMenuForIndex (int menuIndex, const String& /*menuName*/) override
  1061. {
  1062. ApplicationCommandManager* commandManager = &MainAppWindow::getApplicationCommandManager();
  1063. PopupMenu menu;
  1064. if (menuIndex == 0)
  1065. {
  1066. menu.addCommandItem (commandManager, MainAppWindow::showPreviousDemo);
  1067. menu.addCommandItem (commandManager, MainAppWindow::showNextDemo);
  1068. menu.addSeparator();
  1069. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::quit);
  1070. }
  1071. else if (menuIndex == 1)
  1072. {
  1073. menu.addCommandItem (commandManager, MainAppWindow::useLookAndFeelV1);
  1074. menu.addCommandItem (commandManager, MainAppWindow::useLookAndFeelV2);
  1075. menu.addCommandItem (commandManager, MainAppWindow::useLookAndFeelV3);
  1076. menu.addSeparator();
  1077. menu.addCommandItem (commandManager, MainAppWindow::useNativeTitleBar);
  1078. #if JUCE_MAC
  1079. menu.addItem (6000, "Use Native Menu Bar");
  1080. #endif
  1081. #if ! JUCE_LINUX
  1082. menu.addCommandItem (commandManager, MainAppWindow::goToKioskMode);
  1083. #endif
  1084. if (MainAppWindow* mainWindow = MainAppWindow::getMainAppWindow())
  1085. {
  1086. StringArray engines (mainWindow->getRenderingEngines());
  1087. if (engines.size() > 1)
  1088. {
  1089. menu.addSeparator();
  1090. for (int i = 0; i < engines.size(); ++i)
  1091. menu.addCommandItem (commandManager, MainAppWindow::renderingEngineOne + i);
  1092. }
  1093. }
  1094. }
  1095. else if (menuIndex == 2)
  1096. {
  1097. if (TabbedComponent* tabs = findParentComponentOfClass<TabbedComponent>())
  1098. {
  1099. menu.addItem (3000, "Tabs at Top", true, tabs->getOrientation() == TabbedButtonBar::TabsAtTop);
  1100. menu.addItem (3001, "Tabs at Bottom", true, tabs->getOrientation() == TabbedButtonBar::TabsAtBottom);
  1101. menu.addItem (3002, "Tabs on Left", true, tabs->getOrientation() == TabbedButtonBar::TabsAtLeft);
  1102. menu.addItem (3003, "Tabs on Right", true, tabs->getOrientation() == TabbedButtonBar::TabsAtRight);
  1103. }
  1104. }
  1105. else if (menuIndex == 3)
  1106. {
  1107. return getDummyPopupMenu();
  1108. }
  1109. return menu;
  1110. }
  1111. void menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/) override
  1112. {
  1113. // most of our menu items are invoked automatically as commands, but we can handle the
  1114. // other special cases here..
  1115. if (menuItemID == 6000)
  1116. {
  1117. #if JUCE_MAC
  1118. if (MenuBarModel::getMacMainMenu() != nullptr)
  1119. {
  1120. MenuBarModel::setMacMainMenu (nullptr);
  1121. menuBar->setModel (this);
  1122. }
  1123. else
  1124. {
  1125. menuBar->setModel (nullptr);
  1126. MenuBarModel::setMacMainMenu (this);
  1127. }
  1128. #endif
  1129. }
  1130. else if (menuItemID >= 3000 && menuItemID <= 3003)
  1131. {
  1132. if (TabbedComponent* tabs = findParentComponentOfClass<TabbedComponent>())
  1133. {
  1134. TabbedButtonBar::Orientation o = TabbedButtonBar::TabsAtTop;
  1135. if (menuItemID == 3001) o = TabbedButtonBar::TabsAtBottom;
  1136. if (menuItemID == 3002) o = TabbedButtonBar::TabsAtLeft;
  1137. if (menuItemID == 3003) o = TabbedButtonBar::TabsAtRight;
  1138. tabs->setOrientation (o);
  1139. }
  1140. }
  1141. }
  1142. private:
  1143. TextButton popupButton;
  1144. ScopedPointer<MenuBarComponent> menuBar;
  1145. PopupMenu getDummyPopupMenu()
  1146. {
  1147. PopupMenu m;
  1148. m.addItem (1, "Normal item");
  1149. m.addItem (2, "Disabled item", false);
  1150. m.addItem (3, "Ticked item", true, true);
  1151. m.addColouredItem (4, "Coloured item", Colours::green);
  1152. m.addSeparator();
  1153. m.addCustomItem (5, new CustomMenuComponent());
  1154. m.addSeparator();
  1155. for (int i = 0; i < 8; ++i)
  1156. {
  1157. PopupMenu subMenu;
  1158. for (int s = 0; s < 8; ++s)
  1159. {
  1160. PopupMenu subSubMenu;
  1161. for (int item = 0; item < 8; ++item)
  1162. subSubMenu.addItem (1000 + (i * s * item), "Item " + String (item + 1));
  1163. subMenu.addSubMenu ("Sub-sub menu " + String (s + 1), subSubMenu);
  1164. }
  1165. m.addSubMenu ("Sub menu " + String (i + 1), subMenu);
  1166. }
  1167. return m;
  1168. }
  1169. //==============================================================================
  1170. void buttonClicked (Button* button) override
  1171. {
  1172. if (button == &popupButton)
  1173. getDummyPopupMenu().showMenuAsync (PopupMenu::Options().withTargetComponent (&popupButton), nullptr);
  1174. }
  1175. //==============================================================================
  1176. class CustomMenuComponent : public PopupMenu::CustomComponent,
  1177. private Timer
  1178. {
  1179. public:
  1180. CustomMenuComponent()
  1181. {
  1182. // set off a timer to move a blob around on this component every
  1183. // 300 milliseconds - see the timerCallback() method.
  1184. startTimer (300);
  1185. }
  1186. void getIdealSize (int& idealWidth, int& idealHeight) override
  1187. {
  1188. // tells the menu how big we'd like to be..
  1189. idealWidth = 200;
  1190. idealHeight = 60;
  1191. }
  1192. void paint (Graphics& g) override
  1193. {
  1194. g.fillAll (Colours::yellow.withAlpha (0.3f));
  1195. g.setColour (Colours::pink);
  1196. g.fillEllipse (blobPosition);
  1197. g.setFont (Font (14.0f, Font::italic));
  1198. g.setColour (Colours::black);
  1199. g.drawFittedText ("This is a customised menu item (also demonstrating the Timer class)...",
  1200. getLocalBounds().reduced (4, 0),
  1201. Justification::centred, 3);
  1202. }
  1203. private:
  1204. void timerCallback() override
  1205. {
  1206. Random random;
  1207. blobPosition.setBounds ((float) random.nextInt (getWidth()),
  1208. (float) random.nextInt (getHeight()),
  1209. 40.0f, 30.0f);
  1210. repaint();
  1211. }
  1212. Rectangle<float> blobPosition;
  1213. };
  1214. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MenusDemo)
  1215. };
  1216. //==============================================================================
  1217. class DemoTabbedComponent : public TabbedComponent
  1218. {
  1219. public:
  1220. DemoTabbedComponent()
  1221. : TabbedComponent (TabbedButtonBar::TabsAtTop)
  1222. {
  1223. addTab ("Menus", getRandomTabBackgroundColour(), new MenusDemo(), true);
  1224. addTab ("Buttons", getRandomTabBackgroundColour(), new ButtonsPage(), true);
  1225. addTab ("Sliders", getRandomTabBackgroundColour(), new SlidersPage(), true);
  1226. addTab ("Toolbars", getRandomTabBackgroundColour(), new ToolbarDemoComp(), true);
  1227. addTab ("Misc", getRandomTabBackgroundColour(), new MiscPage(), true);
  1228. addTab ("Tables", getRandomTabBackgroundColour(), new TableDemoComponent(), true);
  1229. addTab ("Drag & Drop", getRandomTabBackgroundColour(), new DragAndDropDemo(), true);
  1230. getTabbedButtonBar().getTabButton (5)->setExtraComponent (new CustomTabButton(), TabBarButton::afterText);
  1231. }
  1232. static Colour getRandomTabBackgroundColour()
  1233. {
  1234. return Colour (Random::getSystemRandom().nextFloat(), 0.1f, 0.97f, 1.0f);
  1235. }
  1236. // This is a small star button that is put inside one of the tabs. You can
  1237. // use this technique to create things like "close tab" buttons, etc.
  1238. class CustomTabButton : public Component
  1239. {
  1240. public:
  1241. CustomTabButton()
  1242. {
  1243. setSize (20, 20);
  1244. }
  1245. void paint (Graphics& g) override
  1246. {
  1247. Path star;
  1248. star.addStar (Point<float>(), 7, 1.0f, 2.0f);
  1249. g.setColour (Colours::green);
  1250. g.fillPath (star, star.getTransformToScaleToFit (getLocalBounds().reduced (2).toFloat(), true));
  1251. }
  1252. void mouseDown (const MouseEvent&) override
  1253. {
  1254. showBubbleMessage (this,
  1255. "This is a custom tab component\n"
  1256. "\n"
  1257. "You can use these to implement things like close-buttons "
  1258. "or status displays for your tabs.");
  1259. }
  1260. };
  1261. };
  1262. //==============================================================================
  1263. class WidgetsDemo : public Component
  1264. {
  1265. public:
  1266. WidgetsDemo()
  1267. {
  1268. setOpaque (true);
  1269. addAndMakeVisible (tabs);
  1270. }
  1271. void paint (Graphics& g) override
  1272. {
  1273. g.fillAll (Colours::white);
  1274. }
  1275. void resized() override
  1276. {
  1277. tabs.setBounds (getLocalBounds().reduced (4));
  1278. }
  1279. private:
  1280. DemoTabbedComponent tabs;
  1281. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WidgetsDemo);
  1282. };
  1283. // This static object will register this demo type in a global list of demos..
  1284. static JuceDemoType<WidgetsDemo> demo ("09 Components: Tabs & Widgets");