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.

814 lines
25KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. const char* const Toolbar::toolbarDragDescriptor = "_toolbarItem_";
  20. //==============================================================================
  21. class Toolbar::Spacer : public ToolbarItemComponent
  22. {
  23. public:
  24. Spacer (const int itemId_, const float fixedSize_, const bool drawBar_)
  25. : ToolbarItemComponent (itemId_, String(), false),
  26. fixedSize (fixedSize_),
  27. drawBar (drawBar_)
  28. {
  29. setWantsKeyboardFocus (false);
  30. }
  31. bool getToolbarItemSizes (int toolbarThickness, bool /*isToolbarVertical*/,
  32. int& preferredSize, int& minSize, int& maxSize) override
  33. {
  34. if (fixedSize <= 0)
  35. {
  36. preferredSize = toolbarThickness * 2;
  37. minSize = 4;
  38. maxSize = 32768;
  39. }
  40. else
  41. {
  42. maxSize = roundToInt (toolbarThickness * fixedSize);
  43. minSize = drawBar ? maxSize : jmin (4, maxSize);
  44. preferredSize = maxSize;
  45. if (getEditingMode() == editableOnPalette)
  46. preferredSize = maxSize = toolbarThickness / (drawBar ? 3 : 2);
  47. }
  48. return true;
  49. }
  50. void paintButtonArea (Graphics&, int, int, bool, bool) override
  51. {
  52. }
  53. void contentAreaChanged (const Rectangle<int>&) override
  54. {
  55. }
  56. int getResizeOrder() const noexcept
  57. {
  58. return fixedSize <= 0 ? 0 : 1;
  59. }
  60. void paint (Graphics& g) override
  61. {
  62. const int w = getWidth();
  63. const int h = getHeight();
  64. if (drawBar)
  65. {
  66. g.setColour (findColour (Toolbar::separatorColourId, true));
  67. const float thickness = 0.2f;
  68. if (isToolbarVertical())
  69. g.fillRect (w * 0.1f, h * (0.5f - thickness * 0.5f), w * 0.8f, h * thickness);
  70. else
  71. g.fillRect (w * (0.5f - thickness * 0.5f), h * 0.1f, w * thickness, h * 0.8f);
  72. }
  73. if (getEditingMode() != normalMode && ! drawBar)
  74. {
  75. g.setColour (findColour (Toolbar::separatorColourId, true));
  76. const int indentX = jmin (2, (w - 3) / 2);
  77. const int indentY = jmin (2, (h - 3) / 2);
  78. g.drawRect (indentX, indentY, w - indentX * 2, h - indentY * 2, 1);
  79. if (fixedSize <= 0)
  80. {
  81. float x1, y1, x2, y2, x3, y3, x4, y4, hw, hl;
  82. if (isToolbarVertical())
  83. {
  84. x1 = w * 0.5f;
  85. y1 = h * 0.4f;
  86. x2 = x1;
  87. y2 = indentX * 2.0f;
  88. x3 = x1;
  89. y3 = h * 0.6f;
  90. x4 = x1;
  91. y4 = h - y2;
  92. hw = w * 0.15f;
  93. hl = w * 0.2f;
  94. }
  95. else
  96. {
  97. x1 = w * 0.4f;
  98. y1 = h * 0.5f;
  99. x2 = indentX * 2.0f;
  100. y2 = y1;
  101. x3 = w * 0.6f;
  102. y3 = y1;
  103. x4 = w - x2;
  104. y4 = y1;
  105. hw = h * 0.15f;
  106. hl = h * 0.2f;
  107. }
  108. Path p;
  109. p.addArrow (Line<float> (x1, y1, x2, y2), 1.5f, hw, hl);
  110. p.addArrow (Line<float> (x3, y3, x4, y4), 1.5f, hw, hl);
  111. g.fillPath (p);
  112. }
  113. }
  114. }
  115. private:
  116. const float fixedSize;
  117. const bool drawBar;
  118. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Spacer)
  119. };
  120. //==============================================================================
  121. class Toolbar::MissingItemsComponent : public PopupMenu::CustomComponent
  122. {
  123. public:
  124. MissingItemsComponent (Toolbar& bar, const int h)
  125. : PopupMenu::CustomComponent (true),
  126. owner (&bar),
  127. height (h)
  128. {
  129. for (int i = bar.items.size(); --i >= 0;)
  130. {
  131. ToolbarItemComponent* const tc = bar.items.getUnchecked(i);
  132. if (dynamic_cast<Spacer*> (tc) == nullptr && ! tc->isVisible())
  133. {
  134. oldIndexes.insert (0, i);
  135. addAndMakeVisible (tc, 0);
  136. }
  137. }
  138. layout (400);
  139. }
  140. ~MissingItemsComponent()
  141. {
  142. if (owner != nullptr)
  143. {
  144. for (int i = 0; i < getNumChildComponents(); ++i)
  145. {
  146. if (ToolbarItemComponent* const tc = dynamic_cast<ToolbarItemComponent*> (getChildComponent (i)))
  147. {
  148. tc->setVisible (false);
  149. const int index = oldIndexes.removeAndReturn (i);
  150. owner->addChildComponent (tc, index);
  151. --i;
  152. }
  153. }
  154. owner->resized();
  155. }
  156. }
  157. void layout (const int preferredWidth)
  158. {
  159. const int indent = 8;
  160. int x = indent;
  161. int y = indent;
  162. int maxX = 0;
  163. for (int i = 0; i < getNumChildComponents(); ++i)
  164. {
  165. if (ToolbarItemComponent* const tc = dynamic_cast<ToolbarItemComponent*> (getChildComponent (i)))
  166. {
  167. int preferredSize = 1, minSize = 1, maxSize = 1;
  168. if (tc->getToolbarItemSizes (height, false, preferredSize, minSize, maxSize))
  169. {
  170. if (x + preferredSize > preferredWidth && x > indent)
  171. {
  172. x = indent;
  173. y += height;
  174. }
  175. tc->setBounds (x, y, preferredSize, height);
  176. x += preferredSize;
  177. maxX = jmax (maxX, x);
  178. }
  179. }
  180. }
  181. setSize (maxX + 8, y + height + 8);
  182. }
  183. void getIdealSize (int& idealWidth, int& idealHeight) override
  184. {
  185. idealWidth = getWidth();
  186. idealHeight = getHeight();
  187. }
  188. private:
  189. Component::SafePointer<Toolbar> owner;
  190. const int height;
  191. Array<int> oldIndexes;
  192. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MissingItemsComponent)
  193. };
  194. //==============================================================================
  195. Toolbar::Toolbar()
  196. : vertical (false),
  197. isEditingActive (false),
  198. toolbarStyle (Toolbar::iconsOnly)
  199. {
  200. addChildComponent (missingItemsButton = getLookAndFeel().createToolbarMissingItemsButton (*this));
  201. missingItemsButton->setAlwaysOnTop (true);
  202. missingItemsButton->addListener (this);
  203. }
  204. Toolbar::~Toolbar()
  205. {
  206. items.clear();
  207. }
  208. void Toolbar::setVertical (const bool shouldBeVertical)
  209. {
  210. if (vertical != shouldBeVertical)
  211. {
  212. vertical = shouldBeVertical;
  213. resized();
  214. }
  215. }
  216. void Toolbar::clear()
  217. {
  218. items.clear();
  219. resized();
  220. }
  221. ToolbarItemComponent* Toolbar::createItem (ToolbarItemFactory& factory, const int itemId)
  222. {
  223. if (itemId == ToolbarItemFactory::separatorBarId) return new Spacer (itemId, 0.1f, true);
  224. if (itemId == ToolbarItemFactory::spacerId) return new Spacer (itemId, 0.5f, false);
  225. if (itemId == ToolbarItemFactory::flexibleSpacerId) return new Spacer (itemId, 0.0f, false);
  226. return factory.createItem (itemId);
  227. }
  228. void Toolbar::addItemInternal (ToolbarItemFactory& factory,
  229. const int itemId,
  230. const int insertIndex)
  231. {
  232. // An ID can't be zero - this might indicate a mistake somewhere?
  233. jassert (itemId != 0);
  234. if (ToolbarItemComponent* const tc = createItem (factory, itemId))
  235. {
  236. #if JUCE_DEBUG
  237. Array<int> allowedIds;
  238. factory.getAllToolbarItemIds (allowedIds);
  239. // If your factory can create an item for a given ID, it must also return
  240. // that ID from its getAllToolbarItemIds() method!
  241. jassert (allowedIds.contains (itemId));
  242. #endif
  243. items.insert (insertIndex, tc);
  244. addAndMakeVisible (tc, insertIndex);
  245. }
  246. }
  247. void Toolbar::addItem (ToolbarItemFactory& factory,
  248. const int itemId,
  249. const int insertIndex)
  250. {
  251. addItemInternal (factory, itemId, insertIndex);
  252. resized();
  253. }
  254. void Toolbar::addDefaultItems (ToolbarItemFactory& factoryToUse)
  255. {
  256. Array<int> ids;
  257. factoryToUse.getDefaultItemSet (ids);
  258. clear();
  259. for (int i = 0; i < ids.size(); ++i)
  260. addItemInternal (factoryToUse, ids.getUnchecked (i), -1);
  261. resized();
  262. }
  263. void Toolbar::removeToolbarItem (const int itemIndex)
  264. {
  265. items.remove (itemIndex);
  266. resized();
  267. }
  268. ToolbarItemComponent* Toolbar::removeAndReturnItem (const int itemIndex)
  269. {
  270. if (ToolbarItemComponent* const tc = items.removeAndReturn (itemIndex))
  271. {
  272. removeChildComponent (tc);
  273. resized();
  274. return tc;
  275. }
  276. return nullptr;
  277. }
  278. int Toolbar::getNumItems() const noexcept
  279. {
  280. return items.size();
  281. }
  282. int Toolbar::getItemId (const int itemIndex) const noexcept
  283. {
  284. if (ToolbarItemComponent* const tc = getItemComponent (itemIndex))
  285. return tc->getItemId();
  286. return 0;
  287. }
  288. ToolbarItemComponent* Toolbar::getItemComponent (const int itemIndex) const noexcept
  289. {
  290. return items [itemIndex];
  291. }
  292. ToolbarItemComponent* Toolbar::getNextActiveComponent (int index, const int delta) const
  293. {
  294. for (;;)
  295. {
  296. index += delta;
  297. if (ToolbarItemComponent* const tc = getItemComponent (index))
  298. {
  299. if (tc->isActive)
  300. return tc;
  301. }
  302. else
  303. {
  304. return nullptr;
  305. }
  306. }
  307. }
  308. void Toolbar::setStyle (const ToolbarItemStyle& newStyle)
  309. {
  310. if (toolbarStyle != newStyle)
  311. {
  312. toolbarStyle = newStyle;
  313. updateAllItemPositions (false);
  314. }
  315. }
  316. String Toolbar::toString() const
  317. {
  318. String s ("TB:");
  319. for (int i = 0; i < getNumItems(); ++i)
  320. s << getItemId(i) << ' ';
  321. return s.trimEnd();
  322. }
  323. bool Toolbar::restoreFromString (ToolbarItemFactory& factoryToUse,
  324. const String& savedVersion)
  325. {
  326. if (! savedVersion.startsWith ("TB:"))
  327. return false;
  328. StringArray tokens;
  329. tokens.addTokens (savedVersion.substring (3), false);
  330. clear();
  331. for (int i = 0; i < tokens.size(); ++i)
  332. addItemInternal (factoryToUse, tokens[i].getIntValue(), -1);
  333. resized();
  334. return true;
  335. }
  336. void Toolbar::paint (Graphics& g)
  337. {
  338. getLookAndFeel().paintToolbarBackground (g, getWidth(), getHeight(), *this);
  339. }
  340. int Toolbar::getThickness() const noexcept
  341. {
  342. return vertical ? getWidth() : getHeight();
  343. }
  344. int Toolbar::getLength() const noexcept
  345. {
  346. return vertical ? getHeight() : getWidth();
  347. }
  348. void Toolbar::setEditingActive (const bool active)
  349. {
  350. if (isEditingActive != active)
  351. {
  352. isEditingActive = active;
  353. updateAllItemPositions (false);
  354. }
  355. }
  356. //==============================================================================
  357. void Toolbar::resized()
  358. {
  359. updateAllItemPositions (false);
  360. }
  361. void Toolbar::updateAllItemPositions (const bool animate)
  362. {
  363. if (getWidth() > 0 && getHeight() > 0)
  364. {
  365. StretchableObjectResizer resizer;
  366. for (int i = 0; i < items.size(); ++i)
  367. {
  368. ToolbarItemComponent* const tc = items.getUnchecked(i);
  369. tc->setEditingMode (isEditingActive ? ToolbarItemComponent::editableOnToolbar
  370. : ToolbarItemComponent::normalMode);
  371. tc->setStyle (toolbarStyle);
  372. Spacer* const spacer = dynamic_cast<Spacer*> (tc);
  373. int preferredSize = 1, minSize = 1, maxSize = 1;
  374. if (tc->getToolbarItemSizes (getThickness(), isVertical(),
  375. preferredSize, minSize, maxSize))
  376. {
  377. tc->isActive = true;
  378. resizer.addItem (preferredSize, minSize, maxSize,
  379. spacer != nullptr ? spacer->getResizeOrder() : 2);
  380. }
  381. else
  382. {
  383. tc->isActive = false;
  384. tc->setVisible (false);
  385. }
  386. }
  387. resizer.resizeToFit (getLength());
  388. int totalLength = 0;
  389. for (int i = 0; i < resizer.getNumItems(); ++i)
  390. totalLength += (int) resizer.getItemSize (i);
  391. const bool itemsOffTheEnd = totalLength > getLength();
  392. const int extrasButtonSize = getThickness() / 2;
  393. missingItemsButton->setSize (extrasButtonSize, extrasButtonSize);
  394. missingItemsButton->setVisible (itemsOffTheEnd);
  395. missingItemsButton->setEnabled (! isEditingActive);
  396. if (vertical)
  397. missingItemsButton->setCentrePosition (getWidth() / 2,
  398. getHeight() - 4 - extrasButtonSize / 2);
  399. else
  400. missingItemsButton->setCentrePosition (getWidth() - 4 - extrasButtonSize / 2,
  401. getHeight() / 2);
  402. const int maxLength = itemsOffTheEnd ? (vertical ? missingItemsButton->getY()
  403. : missingItemsButton->getX()) - 4
  404. : getLength();
  405. int pos = 0, activeIndex = 0;
  406. for (int i = 0; i < items.size(); ++i)
  407. {
  408. ToolbarItemComponent* const tc = items.getUnchecked(i);
  409. if (tc->isActive)
  410. {
  411. const int size = (int) resizer.getItemSize (activeIndex++);
  412. Rectangle<int> newBounds;
  413. if (vertical)
  414. newBounds.setBounds (0, pos, getWidth(), size);
  415. else
  416. newBounds.setBounds (pos, 0, size, getHeight());
  417. ComponentAnimator& animator = Desktop::getInstance().getAnimator();
  418. if (animate)
  419. {
  420. animator.animateComponent (tc, newBounds, 1.0f, 200, false, 3.0, 0.0);
  421. }
  422. else
  423. {
  424. animator.cancelAnimation (tc, false);
  425. tc->setBounds (newBounds);
  426. }
  427. pos += size;
  428. tc->setVisible (pos <= maxLength
  429. && ((! tc->isBeingDragged)
  430. || tc->getEditingMode() == ToolbarItemComponent::editableOnPalette));
  431. }
  432. }
  433. }
  434. }
  435. //==============================================================================
  436. void Toolbar::buttonClicked (Button*)
  437. {
  438. jassert (missingItemsButton->isShowing());
  439. if (missingItemsButton->isShowing())
  440. {
  441. PopupMenu m;
  442. m.addCustomItem (1, new MissingItemsComponent (*this, getThickness()));
  443. m.showMenuAsync (PopupMenu::Options().withTargetComponent (missingItemsButton), nullptr);
  444. }
  445. }
  446. //==============================================================================
  447. bool Toolbar::isInterestedInDragSource (const SourceDetails& dragSourceDetails)
  448. {
  449. return dragSourceDetails.description == toolbarDragDescriptor && isEditingActive;
  450. }
  451. void Toolbar::itemDragMove (const SourceDetails& dragSourceDetails)
  452. {
  453. if (ToolbarItemComponent* const tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  454. {
  455. if (! items.contains (tc))
  456. {
  457. if (tc->getEditingMode() == ToolbarItemComponent::editableOnPalette)
  458. {
  459. if (ToolbarItemPalette* const palette = tc->findParentComponentOfClass<ToolbarItemPalette>())
  460. palette->replaceComponent (*tc);
  461. }
  462. else
  463. {
  464. jassert (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar);
  465. }
  466. items.add (tc);
  467. addChildComponent (tc);
  468. updateAllItemPositions (true);
  469. }
  470. ComponentAnimator& animator = Desktop::getInstance().getAnimator();
  471. for (int i = getNumItems(); --i >= 0;)
  472. {
  473. const int currentIndex = items.indexOf (tc);
  474. int newIndex = currentIndex;
  475. const int dragObjectLeft = vertical ? (dragSourceDetails.localPosition.getY() - tc->dragOffsetY)
  476. : (dragSourceDetails.localPosition.getX() - tc->dragOffsetX);
  477. const int dragObjectRight = dragObjectLeft + (vertical ? tc->getHeight() : tc->getWidth());
  478. const Rectangle<int> current (animator.getComponentDestination (getChildComponent (newIndex)));
  479. if (ToolbarItemComponent* const prev = getNextActiveComponent (newIndex, -1))
  480. {
  481. const Rectangle<int> previousPos (animator.getComponentDestination (prev));
  482. if (std::abs (dragObjectLeft - (vertical ? previousPos.getY() : previousPos.getX()))
  483. < std::abs (dragObjectRight - (vertical ? current.getBottom() : current.getRight())))
  484. {
  485. newIndex = getIndexOfChildComponent (prev);
  486. }
  487. }
  488. if (ToolbarItemComponent* const next = getNextActiveComponent (newIndex, 1))
  489. {
  490. const Rectangle<int> nextPos (animator.getComponentDestination (next));
  491. if (std::abs (dragObjectLeft - (vertical ? current.getY() : current.getX()))
  492. > std::abs (dragObjectRight - (vertical ? nextPos.getBottom() : nextPos.getRight())))
  493. {
  494. newIndex = getIndexOfChildComponent (next) + 1;
  495. }
  496. }
  497. if (newIndex == currentIndex)
  498. break;
  499. items.removeObject (tc, false);
  500. removeChildComponent (tc);
  501. addChildComponent (tc, newIndex);
  502. items.insert (newIndex, tc);
  503. updateAllItemPositions (true);
  504. }
  505. }
  506. }
  507. void Toolbar::itemDragExit (const SourceDetails& dragSourceDetails)
  508. {
  509. if (ToolbarItemComponent* const tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  510. {
  511. if (isParentOf (tc))
  512. {
  513. items.removeObject (tc, false);
  514. removeChildComponent (tc);
  515. updateAllItemPositions (true);
  516. }
  517. }
  518. }
  519. void Toolbar::itemDropped (const SourceDetails& dragSourceDetails)
  520. {
  521. if (ToolbarItemComponent* const tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  522. tc->setState (Button::buttonNormal);
  523. }
  524. void Toolbar::mouseDown (const MouseEvent&) {}
  525. //==============================================================================
  526. class Toolbar::CustomisationDialog : public DialogWindow
  527. {
  528. public:
  529. CustomisationDialog (ToolbarItemFactory& factory, Toolbar& bar, int optionFlags)
  530. : DialogWindow (TRANS("Add/remove items from toolbar"), Colours::white, true, true),
  531. toolbar (bar)
  532. {
  533. setContentOwned (new CustomiserPanel (factory, toolbar, optionFlags), true);
  534. setResizable (true, true);
  535. setResizeLimits (400, 300, 1500, 1000);
  536. positionNearBar();
  537. }
  538. ~CustomisationDialog()
  539. {
  540. toolbar.setEditingActive (false);
  541. }
  542. void closeButtonPressed() override
  543. {
  544. setVisible (false);
  545. }
  546. bool canModalEventBeSentToComponent (const Component* comp) override
  547. {
  548. return toolbar.isParentOf (comp)
  549. || dynamic_cast<const ToolbarItemComponent::ItemDragAndDropOverlayComponent*> (comp) != nullptr;
  550. }
  551. void positionNearBar()
  552. {
  553. const Rectangle<int> screenSize (toolbar.getParentMonitorArea());
  554. Point<int> pos (toolbar.getScreenPosition());
  555. const int gap = 8;
  556. if (toolbar.isVertical())
  557. {
  558. if (pos.x > screenSize.getCentreX())
  559. pos.x -= getWidth() - gap;
  560. else
  561. pos.x += toolbar.getWidth() + gap;
  562. }
  563. else
  564. {
  565. pos.x += (toolbar.getWidth() - getWidth()) / 2;
  566. if (pos.y > screenSize.getCentreY())
  567. pos.y -= getHeight() - gap;
  568. else
  569. pos.y += toolbar.getHeight() + gap;
  570. }
  571. setTopLeftPosition (pos);
  572. }
  573. private:
  574. Toolbar& toolbar;
  575. class CustomiserPanel : public Component,
  576. private ComboBoxListener, // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  577. private ButtonListener
  578. {
  579. public:
  580. CustomiserPanel (ToolbarItemFactory& tbf, Toolbar& bar, int optionFlags)
  581. : factory (tbf), toolbar (bar), palette (tbf, bar),
  582. instructions (String(), TRANS ("You can drag the items above and drop them onto a toolbar to add them.")
  583. + "\n\n"
  584. + TRANS ("Items on the toolbar can also be dragged around to change their order, or dragged off the edge to delete them.")),
  585. defaultButton (TRANS ("Restore to default set of items"))
  586. {
  587. addAndMakeVisible (palette);
  588. if ((optionFlags & (Toolbar::allowIconsOnlyChoice
  589. | Toolbar::allowIconsWithTextChoice
  590. | Toolbar::allowTextOnlyChoice)) != 0)
  591. {
  592. addAndMakeVisible (styleBox);
  593. styleBox.setEditableText (false);
  594. if ((optionFlags & Toolbar::allowIconsOnlyChoice) != 0) styleBox.addItem (TRANS("Show icons only"), 1);
  595. if ((optionFlags & Toolbar::allowIconsWithTextChoice) != 0) styleBox.addItem (TRANS("Show icons and descriptions"), 2);
  596. if ((optionFlags & Toolbar::allowTextOnlyChoice) != 0) styleBox.addItem (TRANS("Show descriptions only"), 3);
  597. int selectedStyle = 0;
  598. switch (bar.getStyle())
  599. {
  600. case Toolbar::iconsOnly: selectedStyle = 1; break;
  601. case Toolbar::iconsWithText: selectedStyle = 2; break;
  602. case Toolbar::textOnly: selectedStyle = 3; break;
  603. }
  604. styleBox.setSelectedId (selectedStyle);
  605. styleBox.addListener (this);
  606. }
  607. if ((optionFlags & Toolbar::showResetToDefaultsButton) != 0)
  608. {
  609. addAndMakeVisible (defaultButton);
  610. defaultButton.addListener (this);
  611. }
  612. addAndMakeVisible (instructions);
  613. instructions.setFont (Font (13.0f));
  614. setSize (500, 300);
  615. }
  616. void comboBoxChanged (ComboBox*) override
  617. {
  618. switch (styleBox.getSelectedId())
  619. {
  620. case 1: toolbar.setStyle (Toolbar::iconsOnly); break;
  621. case 2: toolbar.setStyle (Toolbar::iconsWithText); break;
  622. case 3: toolbar.setStyle (Toolbar::textOnly); break;
  623. }
  624. palette.resized(); // to make it update the styles
  625. }
  626. void buttonClicked (Button*) override
  627. {
  628. toolbar.addDefaultItems (factory);
  629. }
  630. void paint (Graphics& g) override
  631. {
  632. Colour background;
  633. if (DialogWindow* const dw = findParentComponentOfClass<DialogWindow>())
  634. background = dw->getBackgroundColour();
  635. g.setColour (background.contrasting().withAlpha (0.3f));
  636. g.fillRect (palette.getX(), palette.getBottom() - 1, palette.getWidth(), 1);
  637. }
  638. void resized() override
  639. {
  640. palette.setBounds (0, 0, getWidth(), getHeight() - 120);
  641. styleBox.setBounds (10, getHeight() - 110, 200, 22);
  642. defaultButton.changeWidthToFitText (22);
  643. defaultButton.setTopLeftPosition (240, getHeight() - 110);
  644. instructions.setBounds (10, getHeight() - 80, getWidth() - 20, 80);
  645. }
  646. private:
  647. ToolbarItemFactory& factory;
  648. Toolbar& toolbar;
  649. ToolbarItemPalette palette;
  650. Label instructions;
  651. ComboBox styleBox;
  652. TextButton defaultButton;
  653. };
  654. };
  655. void Toolbar::showCustomisationDialog (ToolbarItemFactory& factory, const int optionFlags)
  656. {
  657. setEditingActive (true);
  658. (new CustomisationDialog (factory, *this, optionFlags))
  659. ->enterModalState (true, nullptr, true);
  660. }