juce_Toolbar.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. namespace juce
  20. {
  21. const char* const Toolbar::toolbarDragDescriptor = "_toolbarItem_";
  22. //==============================================================================
  23. class Toolbar::Spacer : public ToolbarItemComponent
  24. {
  25. public:
  26. Spacer (int itemID, float sizeToUse, bool shouldDrawBar)
  27. : ToolbarItemComponent (itemID, {}, false),
  28. fixedSize (sizeToUse),
  29. drawBar (shouldDrawBar)
  30. {
  31. setWantsKeyboardFocus (false);
  32. }
  33. bool getToolbarItemSizes (int toolbarThickness, bool /*isToolbarVertical*/,
  34. int& preferredSize, int& minSize, int& maxSize) override
  35. {
  36. if (fixedSize <= 0)
  37. {
  38. preferredSize = toolbarThickness * 2;
  39. minSize = 4;
  40. maxSize = 32768;
  41. }
  42. else
  43. {
  44. maxSize = roundToInt (toolbarThickness * fixedSize);
  45. minSize = drawBar ? maxSize : jmin (4, maxSize);
  46. preferredSize = maxSize;
  47. if (getEditingMode() == editableOnPalette)
  48. preferredSize = maxSize = toolbarThickness / (drawBar ? 3 : 2);
  49. }
  50. return true;
  51. }
  52. void paintButtonArea (Graphics&, int, int, bool, bool) override
  53. {
  54. }
  55. void contentAreaChanged (const Rectangle<int>&) override
  56. {
  57. }
  58. int getResizeOrder() const noexcept
  59. {
  60. return fixedSize <= 0 ? 0 : 1;
  61. }
  62. void paint (Graphics& g) override
  63. {
  64. auto w = getWidth();
  65. auto h = getHeight();
  66. if (drawBar)
  67. {
  68. g.setColour (findColour (Toolbar::separatorColourId, true));
  69. auto thickness = 0.2f;
  70. if (isToolbarVertical())
  71. g.fillRect (w * 0.1f, h * (0.5f - thickness * 0.5f), w * 0.8f, h * thickness);
  72. else
  73. g.fillRect (w * (0.5f - thickness * 0.5f), h * 0.1f, w * thickness, h * 0.8f);
  74. }
  75. if (getEditingMode() != normalMode && ! drawBar)
  76. {
  77. g.setColour (findColour (Toolbar::separatorColourId, true));
  78. auto indentX = jmin (2, (w - 3) / 2);
  79. auto indentY = jmin (2, (h - 3) / 2);
  80. g.drawRect (indentX, indentY, w - indentX * 2, h - indentY * 2, 1);
  81. if (fixedSize <= 0)
  82. {
  83. float x1, y1, x2, y2, x3, y3, x4, y4, hw, hl;
  84. if (isToolbarVertical())
  85. {
  86. x1 = w * 0.5f;
  87. y1 = h * 0.4f;
  88. x2 = x1;
  89. y2 = indentX * 2.0f;
  90. x3 = x1;
  91. y3 = h * 0.6f;
  92. x4 = x1;
  93. y4 = h - y2;
  94. hw = w * 0.15f;
  95. hl = w * 0.2f;
  96. }
  97. else
  98. {
  99. x1 = w * 0.4f;
  100. y1 = h * 0.5f;
  101. x2 = indentX * 2.0f;
  102. y2 = y1;
  103. x3 = w * 0.6f;
  104. y3 = y1;
  105. x4 = w - x2;
  106. y4 = y1;
  107. hw = h * 0.15f;
  108. hl = h * 0.2f;
  109. }
  110. Path p;
  111. p.addArrow ({ x1, y1, x2, y2 }, 1.5f, hw, hl);
  112. p.addArrow ({ x3, y3, x4, y4 }, 1.5f, hw, hl);
  113. g.fillPath (p);
  114. }
  115. }
  116. }
  117. private:
  118. const float fixedSize;
  119. const bool drawBar;
  120. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Spacer)
  121. };
  122. //==============================================================================
  123. class Toolbar::MissingItemsComponent : public PopupMenu::CustomComponent
  124. {
  125. public:
  126. MissingItemsComponent (Toolbar& bar, int h)
  127. : PopupMenu::CustomComponent (true),
  128. owner (&bar),
  129. height (h)
  130. {
  131. for (int i = bar.items.size(); --i >= 0;)
  132. {
  133. auto* tc = bar.items.getUnchecked(i);
  134. if (dynamic_cast<Spacer*> (tc) == nullptr && ! tc->isVisible())
  135. {
  136. oldIndexes.insert (0, i);
  137. addAndMakeVisible (tc, 0);
  138. }
  139. }
  140. layout (400);
  141. }
  142. ~MissingItemsComponent() override
  143. {
  144. if (owner != nullptr)
  145. {
  146. for (int i = 0; i < getNumChildComponents(); ++i)
  147. {
  148. if (auto* tc = dynamic_cast<ToolbarItemComponent*> (getChildComponent (i)))
  149. {
  150. tc->setVisible (false);
  151. auto index = oldIndexes.removeAndReturn (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. auto x = indent;
  163. auto y = indent;
  164. int maxX = 0;
  165. for (auto* c : getChildren())
  166. {
  167. if (auto* tc = dynamic_cast<ToolbarItemComponent*> (c))
  168. {
  169. int preferredSize = 1, minSize = 1, maxSize = 1;
  170. if (tc->getToolbarItemSizes (height, false, preferredSize, minSize, maxSize))
  171. {
  172. if (x + preferredSize > preferredWidth && x > indent)
  173. {
  174. x = indent;
  175. y += height;
  176. }
  177. tc->setBounds (x, y, preferredSize, height);
  178. x += preferredSize;
  179. maxX = jmax (maxX, x);
  180. }
  181. }
  182. }
  183. setSize (maxX + 8, y + height + 8);
  184. }
  185. void getIdealSize (int& idealWidth, int& idealHeight) override
  186. {
  187. idealWidth = getWidth();
  188. idealHeight = getHeight();
  189. }
  190. private:
  191. Component::SafePointer<Toolbar> owner;
  192. const int height;
  193. Array<int> oldIndexes;
  194. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MissingItemsComponent)
  195. };
  196. //==============================================================================
  197. Toolbar::Toolbar()
  198. {
  199. missingItemsButton.reset (getLookAndFeel().createToolbarMissingItemsButton (*this));
  200. addChildComponent (missingItemsButton.get());
  201. missingItemsButton->setAlwaysOnTop (true);
  202. missingItemsButton->onClick = [this] { showMissingItems(); };
  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 (auto* 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, int itemId, 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 (auto i : ids)
  258. addItemInternal (factoryToUse, 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 (auto* 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 (auto* 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 (auto* 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 (auto& t : tokens)
  330. addItemInternal (factoryToUse, t.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 (bool animate)
  360. {
  361. if (getWidth() > 0 && getHeight() > 0)
  362. {
  363. StretchableObjectResizer resizer;
  364. for (auto* tc : items)
  365. {
  366. tc->setEditingMode (isEditingActive ? ToolbarItemComponent::editableOnToolbar
  367. : ToolbarItemComponent::normalMode);
  368. tc->setStyle (toolbarStyle);
  369. auto* spacer = dynamic_cast<Spacer*> (tc);
  370. int preferredSize = 1, minSize = 1, maxSize = 1;
  371. if (tc->getToolbarItemSizes (getThickness(), isVertical(),
  372. preferredSize, minSize, maxSize))
  373. {
  374. tc->isActive = true;
  375. resizer.addItem (preferredSize, minSize, maxSize,
  376. spacer != nullptr ? spacer->getResizeOrder() : 2);
  377. }
  378. else
  379. {
  380. tc->isActive = false;
  381. tc->setVisible (false);
  382. }
  383. }
  384. resizer.resizeToFit (getLength());
  385. int totalLength = 0;
  386. for (int i = 0; i < resizer.getNumItems(); ++i)
  387. totalLength += (int) resizer.getItemSize (i);
  388. const bool itemsOffTheEnd = totalLength > getLength();
  389. auto extrasButtonSize = getThickness() / 2;
  390. missingItemsButton->setSize (extrasButtonSize, extrasButtonSize);
  391. missingItemsButton->setVisible (itemsOffTheEnd);
  392. missingItemsButton->setEnabled (! isEditingActive);
  393. if (vertical)
  394. missingItemsButton->setCentrePosition (getWidth() / 2,
  395. getHeight() - 4 - extrasButtonSize / 2);
  396. else
  397. missingItemsButton->setCentrePosition (getWidth() - 4 - extrasButtonSize / 2,
  398. getHeight() / 2);
  399. auto maxLength = itemsOffTheEnd ? (vertical ? missingItemsButton->getY()
  400. : missingItemsButton->getX()) - 4
  401. : getLength();
  402. int pos = 0, activeIndex = 0;
  403. for (auto* tc : items)
  404. {
  405. if (tc->isActive)
  406. {
  407. auto size = (int) resizer.getItemSize (activeIndex++);
  408. Rectangle<int> newBounds;
  409. if (vertical)
  410. newBounds.setBounds (0, pos, getWidth(), size);
  411. else
  412. newBounds.setBounds (pos, 0, size, getHeight());
  413. auto& animator = Desktop::getInstance().getAnimator();
  414. if (animate)
  415. {
  416. animator.animateComponent (tc, newBounds, 1.0f, 200, false, 3.0, 0.0);
  417. }
  418. else
  419. {
  420. animator.cancelAnimation (tc, false);
  421. tc->setBounds (newBounds);
  422. }
  423. pos += size;
  424. tc->setVisible (pos <= maxLength
  425. && ((! tc->isBeingDragged)
  426. || tc->getEditingMode() == ToolbarItemComponent::editableOnPalette));
  427. }
  428. }
  429. }
  430. }
  431. //==============================================================================
  432. void Toolbar::showMissingItems()
  433. {
  434. jassert (missingItemsButton->isShowing());
  435. if (missingItemsButton->isShowing())
  436. {
  437. PopupMenu m;
  438. auto comp = std::make_unique<MissingItemsComponent> (*this, getThickness());
  439. m.addCustomItem (1, std::move (comp));
  440. m.showMenuAsync (PopupMenu::Options().withTargetComponent (missingItemsButton.get()));
  441. }
  442. }
  443. //==============================================================================
  444. bool Toolbar::isInterestedInDragSource (const SourceDetails& dragSourceDetails)
  445. {
  446. return dragSourceDetails.description == toolbarDragDescriptor && isEditingActive;
  447. }
  448. void Toolbar::itemDragMove (const SourceDetails& dragSourceDetails)
  449. {
  450. if (auto* tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  451. {
  452. if (! items.contains (tc))
  453. {
  454. if (tc->getEditingMode() == ToolbarItemComponent::editableOnPalette)
  455. {
  456. if (auto* palette = tc->findParentComponentOfClass<ToolbarItemPalette>())
  457. palette->replaceComponent (*tc);
  458. }
  459. else
  460. {
  461. jassert (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar);
  462. }
  463. items.add (tc);
  464. addChildComponent (tc);
  465. updateAllItemPositions (true);
  466. }
  467. auto& animator = Desktop::getInstance().getAnimator();
  468. for (int i = getNumItems(); --i >= 0;)
  469. {
  470. auto currentIndex = items.indexOf (tc);
  471. auto newIndex = currentIndex;
  472. auto dragObjectLeft = vertical ? (dragSourceDetails.localPosition.getY() - tc->dragOffsetY)
  473. : (dragSourceDetails.localPosition.getX() - tc->dragOffsetX);
  474. auto dragObjectRight = dragObjectLeft + (vertical ? tc->getHeight() : tc->getWidth());
  475. auto current = animator.getComponentDestination (getChildComponent (newIndex));
  476. if (auto* prev = getNextActiveComponent (newIndex, -1))
  477. {
  478. auto previousPos = animator.getComponentDestination (prev);
  479. if (std::abs (dragObjectLeft - (vertical ? previousPos.getY() : previousPos.getX()))
  480. < std::abs (dragObjectRight - (vertical ? current.getBottom() : current.getRight())))
  481. {
  482. newIndex = getIndexOfChildComponent (prev);
  483. }
  484. }
  485. if (auto* next = getNextActiveComponent (newIndex, 1))
  486. {
  487. auto nextPos = animator.getComponentDestination (next);
  488. if (std::abs (dragObjectLeft - (vertical ? current.getY() : current.getX()))
  489. > std::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. if (auto* tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  507. {
  508. if (isParentOf (tc))
  509. {
  510. items.removeObject (tc, false);
  511. removeChildComponent (tc);
  512. updateAllItemPositions (true);
  513. }
  514. }
  515. }
  516. void Toolbar::itemDropped (const SourceDetails& dragSourceDetails)
  517. {
  518. if (auto* tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  519. tc->setState (Button::buttonNormal);
  520. }
  521. void Toolbar::mouseDown (const MouseEvent&) {}
  522. //==============================================================================
  523. class Toolbar::CustomisationDialog : public DialogWindow
  524. {
  525. public:
  526. CustomisationDialog (ToolbarItemFactory& factory, Toolbar& bar, int optionFlags)
  527. : DialogWindow (TRANS("Add/remove items from toolbar"), Colours::white, true, true),
  528. toolbar (bar)
  529. {
  530. setContentOwned (new CustomiserPanel (factory, toolbar, optionFlags), true);
  531. setResizable (true, true);
  532. setResizeLimits (400, 300, 1500, 1000);
  533. positionNearBar();
  534. }
  535. ~CustomisationDialog() override
  536. {
  537. toolbar.setEditingActive (false);
  538. }
  539. void closeButtonPressed() override
  540. {
  541. setVisible (false);
  542. }
  543. bool canModalEventBeSentToComponent (const Component* comp) override
  544. {
  545. return toolbar.isParentOf (comp)
  546. || dynamic_cast<const ToolbarItemComponent::ItemDragAndDropOverlayComponent*> (comp) != nullptr;
  547. }
  548. void positionNearBar()
  549. {
  550. auto screenSize = toolbar.getParentMonitorArea();
  551. auto pos = toolbar.getScreenPosition();
  552. const int gap = 8;
  553. if (toolbar.isVertical())
  554. {
  555. if (pos.x > screenSize.getCentreX())
  556. pos.x -= getWidth() - gap;
  557. else
  558. pos.x += toolbar.getWidth() + gap;
  559. }
  560. else
  561. {
  562. pos.x += (toolbar.getWidth() - getWidth()) / 2;
  563. if (pos.y > screenSize.getCentreY())
  564. pos.y -= getHeight() - gap;
  565. else
  566. pos.y += toolbar.getHeight() + gap;
  567. }
  568. setTopLeftPosition (pos);
  569. }
  570. private:
  571. Toolbar& toolbar;
  572. class CustomiserPanel : public Component
  573. {
  574. public:
  575. CustomiserPanel (ToolbarItemFactory& tbf, Toolbar& bar, int optionFlags)
  576. : factory (tbf), toolbar (bar), palette (tbf, bar),
  577. instructions ({}, TRANS ("You can drag the items above and drop them onto a toolbar to add them.")
  578. + "\n\n"
  579. + TRANS ("Items on the toolbar can also be dragged around to change their order, or dragged off the edge to delete them.")),
  580. defaultButton (TRANS ("Restore to default set of items"))
  581. {
  582. addAndMakeVisible (palette);
  583. if ((optionFlags & (Toolbar::allowIconsOnlyChoice
  584. | Toolbar::allowIconsWithTextChoice
  585. | Toolbar::allowTextOnlyChoice)) != 0)
  586. {
  587. addAndMakeVisible (styleBox);
  588. styleBox.setEditableText (false);
  589. if ((optionFlags & Toolbar::allowIconsOnlyChoice) != 0) styleBox.addItem (TRANS("Show icons only"), 1);
  590. if ((optionFlags & Toolbar::allowIconsWithTextChoice) != 0) styleBox.addItem (TRANS("Show icons and descriptions"), 2);
  591. if ((optionFlags & Toolbar::allowTextOnlyChoice) != 0) styleBox.addItem (TRANS("Show descriptions only"), 3);
  592. int selectedStyle = 0;
  593. switch (bar.getStyle())
  594. {
  595. case Toolbar::iconsOnly: selectedStyle = 1; break;
  596. case Toolbar::iconsWithText: selectedStyle = 2; break;
  597. case Toolbar::textOnly: selectedStyle = 3; break;
  598. }
  599. styleBox.setSelectedId (selectedStyle);
  600. styleBox.onChange = [this] { updateStyle(); };
  601. }
  602. if ((optionFlags & Toolbar::showResetToDefaultsButton) != 0)
  603. {
  604. addAndMakeVisible (defaultButton);
  605. defaultButton.onClick = [this] { toolbar.addDefaultItems (factory); };
  606. }
  607. addAndMakeVisible (instructions);
  608. instructions.setFont (Font (13.0f));
  609. setSize (500, 300);
  610. }
  611. void updateStyle()
  612. {
  613. switch (styleBox.getSelectedId())
  614. {
  615. case 1: toolbar.setStyle (Toolbar::iconsOnly); break;
  616. case 2: toolbar.setStyle (Toolbar::iconsWithText); break;
  617. case 3: toolbar.setStyle (Toolbar::textOnly); break;
  618. }
  619. palette.resized(); // to make it update the styles
  620. }
  621. void paint (Graphics& g) override
  622. {
  623. Colour background;
  624. if (auto* dw = findParentComponentOfClass<DialogWindow>())
  625. background = dw->getBackgroundColour();
  626. g.setColour (background.contrasting().withAlpha (0.3f));
  627. g.fillRect (palette.getX(), palette.getBottom() - 1, palette.getWidth(), 1);
  628. }
  629. void resized() override
  630. {
  631. palette.setBounds (0, 0, getWidth(), getHeight() - 120);
  632. styleBox.setBounds (10, getHeight() - 110, 200, 22);
  633. defaultButton.changeWidthToFitText (22);
  634. defaultButton.setTopLeftPosition (240, getHeight() - 110);
  635. instructions.setBounds (10, getHeight() - 80, getWidth() - 20, 80);
  636. }
  637. private:
  638. ToolbarItemFactory& factory;
  639. Toolbar& toolbar;
  640. ToolbarItemPalette palette;
  641. Label instructions;
  642. ComboBox styleBox;
  643. TextButton defaultButton;
  644. };
  645. };
  646. void Toolbar::showCustomisationDialog (ToolbarItemFactory& factory, const int optionFlags)
  647. {
  648. setEditingActive (true);
  649. (new CustomisationDialog (factory, *this, optionFlags))
  650. ->enterModalState (true, nullptr, true);
  651. }
  652. } // namespace juce