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.

830 lines
26KB

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