Audio plugin host https://kx.studio/carla
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.

juce_Toolbar.cpp 25KB

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