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.

812 lines
25KB

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