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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 (tc != nullptr && 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. initMissingItemButton();
  200. }
  201. Toolbar::~Toolbar()
  202. {
  203. items.clear();
  204. }
  205. void Toolbar::setVertical (const bool shouldBeVertical)
  206. {
  207. if (vertical != shouldBeVertical)
  208. {
  209. vertical = shouldBeVertical;
  210. resized();
  211. }
  212. }
  213. void Toolbar::clear()
  214. {
  215. items.clear();
  216. resized();
  217. }
  218. ToolbarItemComponent* Toolbar::createItem (ToolbarItemFactory& factory, const int itemId)
  219. {
  220. if (itemId == ToolbarItemFactory::separatorBarId) return new Spacer (itemId, 0.1f, true);
  221. if (itemId == ToolbarItemFactory::spacerId) return new Spacer (itemId, 0.5f, false);
  222. if (itemId == ToolbarItemFactory::flexibleSpacerId) return new Spacer (itemId, 0.0f, false);
  223. return factory.createItem (itemId);
  224. }
  225. void Toolbar::addItemInternal (ToolbarItemFactory& factory,
  226. const int itemId,
  227. const int insertIndex)
  228. {
  229. // An ID can't be zero - this might indicate a mistake somewhere?
  230. jassert (itemId != 0);
  231. if (auto* tc = createItem (factory, itemId))
  232. {
  233. #if JUCE_DEBUG
  234. Array<int> allowedIds;
  235. factory.getAllToolbarItemIds (allowedIds);
  236. // If your factory can create an item for a given ID, it must also return
  237. // that ID from its getAllToolbarItemIds() method!
  238. jassert (allowedIds.contains (itemId));
  239. #endif
  240. items.insert (insertIndex, tc);
  241. addAndMakeVisible (tc, insertIndex);
  242. }
  243. }
  244. void Toolbar::addItem (ToolbarItemFactory& factory, int itemId, int insertIndex)
  245. {
  246. addItemInternal (factory, itemId, insertIndex);
  247. resized();
  248. }
  249. void Toolbar::addDefaultItems (ToolbarItemFactory& factoryToUse)
  250. {
  251. Array<int> ids;
  252. factoryToUse.getDefaultItemSet (ids);
  253. clear();
  254. for (auto i : ids)
  255. addItemInternal (factoryToUse, i, -1);
  256. resized();
  257. }
  258. void Toolbar::removeToolbarItem (const int itemIndex)
  259. {
  260. items.remove (itemIndex);
  261. resized();
  262. }
  263. ToolbarItemComponent* Toolbar::removeAndReturnItem (const int itemIndex)
  264. {
  265. if (auto* tc = items.removeAndReturn (itemIndex))
  266. {
  267. removeChildComponent (tc);
  268. resized();
  269. return tc;
  270. }
  271. return nullptr;
  272. }
  273. int Toolbar::getNumItems() const noexcept
  274. {
  275. return items.size();
  276. }
  277. int Toolbar::getItemId (const int itemIndex) const noexcept
  278. {
  279. if (auto* tc = getItemComponent (itemIndex))
  280. return tc->getItemId();
  281. return 0;
  282. }
  283. ToolbarItemComponent* Toolbar::getItemComponent (const int itemIndex) const noexcept
  284. {
  285. return items[itemIndex];
  286. }
  287. ToolbarItemComponent* Toolbar::getNextActiveComponent (int index, const int delta) const
  288. {
  289. for (;;)
  290. {
  291. index += delta;
  292. if (auto* tc = getItemComponent (index))
  293. {
  294. if (tc->isActive)
  295. return tc;
  296. }
  297. else
  298. {
  299. return nullptr;
  300. }
  301. }
  302. }
  303. void Toolbar::setStyle (const ToolbarItemStyle& newStyle)
  304. {
  305. if (toolbarStyle != newStyle)
  306. {
  307. toolbarStyle = newStyle;
  308. updateAllItemPositions (false);
  309. }
  310. }
  311. String Toolbar::toString() const
  312. {
  313. String s ("TB:");
  314. for (int i = 0; i < getNumItems(); ++i)
  315. s << getItemId(i) << ' ';
  316. return s.trimEnd();
  317. }
  318. bool Toolbar::restoreFromString (ToolbarItemFactory& factoryToUse,
  319. const String& savedVersion)
  320. {
  321. if (! savedVersion.startsWith ("TB:"))
  322. return false;
  323. StringArray tokens;
  324. tokens.addTokens (savedVersion.substring (3), false);
  325. clear();
  326. for (auto& t : tokens)
  327. addItemInternal (factoryToUse, t.getIntValue(), -1);
  328. resized();
  329. return true;
  330. }
  331. void Toolbar::paint (Graphics& g)
  332. {
  333. getLookAndFeel().paintToolbarBackground (g, getWidth(), getHeight(), *this);
  334. }
  335. int Toolbar::getThickness() const noexcept
  336. {
  337. return vertical ? getWidth() : getHeight();
  338. }
  339. int Toolbar::getLength() const noexcept
  340. {
  341. return vertical ? getHeight() : getWidth();
  342. }
  343. void Toolbar::setEditingActive (const bool active)
  344. {
  345. if (isEditingActive != active)
  346. {
  347. isEditingActive = active;
  348. updateAllItemPositions (false);
  349. }
  350. }
  351. //==============================================================================
  352. void Toolbar::resized()
  353. {
  354. updateAllItemPositions (false);
  355. }
  356. void Toolbar::updateAllItemPositions (bool animate)
  357. {
  358. if (getWidth() > 0 && getHeight() > 0)
  359. {
  360. StretchableObjectResizer resizer;
  361. for (auto* tc : items)
  362. {
  363. tc->setEditingMode (isEditingActive ? ToolbarItemComponent::editableOnToolbar
  364. : ToolbarItemComponent::normalMode);
  365. tc->setStyle (toolbarStyle);
  366. auto* spacer = dynamic_cast<Spacer*> (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 (int i = 0; i < resizer.getNumItems(); ++i)
  384. totalLength += (int) resizer.getItemSize (i);
  385. const bool itemsOffTheEnd = totalLength > getLength();
  386. auto 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. auto maxLength = itemsOffTheEnd ? (vertical ? missingItemsButton->getY()
  397. : missingItemsButton->getX()) - 4
  398. : getLength();
  399. int pos = 0, activeIndex = 0;
  400. for (auto* tc : items)
  401. {
  402. if (tc->isActive)
  403. {
  404. auto size = (int) resizer.getItemSize (activeIndex++);
  405. Rectangle<int> newBounds;
  406. if (vertical)
  407. newBounds.setBounds (0, pos, getWidth(), size);
  408. else
  409. newBounds.setBounds (pos, 0, size, getHeight());
  410. auto& animator = Desktop::getInstance().getAnimator();
  411. if (animate)
  412. {
  413. animator.animateComponent (tc, newBounds, 1.0f, 200, false, 3.0, 0.0);
  414. }
  415. else
  416. {
  417. animator.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::initMissingItemButton()
  430. {
  431. if (missingItemsButton == nullptr)
  432. return;
  433. addChildComponent (*missingItemsButton);
  434. missingItemsButton->setAlwaysOnTop (true);
  435. missingItemsButton->onClick = [this] { showMissingItems(); };
  436. }
  437. void Toolbar::showMissingItems()
  438. {
  439. jassert (missingItemsButton->isShowing());
  440. if (missingItemsButton->isShowing())
  441. {
  442. PopupMenu m;
  443. auto comp = std::make_unique<MissingItemsComponent> (*this, getThickness());
  444. m.addCustomItem (1, std::move (comp), nullptr, TRANS ("Additional Items"));
  445. m.showMenuAsync (PopupMenu::Options().withTargetComponent (missingItemsButton.get()));
  446. }
  447. }
  448. //==============================================================================
  449. bool Toolbar::isInterestedInDragSource (const SourceDetails& dragSourceDetails)
  450. {
  451. return dragSourceDetails.description == toolbarDragDescriptor && isEditingActive;
  452. }
  453. void Toolbar::itemDragMove (const SourceDetails& dragSourceDetails)
  454. {
  455. if (auto* tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  456. {
  457. if (! items.contains (tc))
  458. {
  459. if (tc->getEditingMode() == ToolbarItemComponent::editableOnPalette)
  460. {
  461. if (auto* palette = tc->findParentComponentOfClass<ToolbarItemPalette>())
  462. palette->replaceComponent (*tc);
  463. }
  464. else
  465. {
  466. jassert (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar);
  467. }
  468. items.add (tc);
  469. addChildComponent (tc);
  470. updateAllItemPositions (true);
  471. }
  472. auto& animator = Desktop::getInstance().getAnimator();
  473. for (int i = getNumItems(); --i >= 0;)
  474. {
  475. auto currentIndex = items.indexOf (tc);
  476. auto newIndex = currentIndex;
  477. auto dragObjectLeft = vertical ? (dragSourceDetails.localPosition.getY() - tc->dragOffsetY)
  478. : (dragSourceDetails.localPosition.getX() - tc->dragOffsetX);
  479. auto dragObjectRight = dragObjectLeft + (vertical ? tc->getHeight() : tc->getWidth());
  480. auto current = animator.getComponentDestination (getChildComponent (newIndex));
  481. if (auto* prev = getNextActiveComponent (newIndex, -1))
  482. {
  483. auto previousPos = animator.getComponentDestination (prev);
  484. if (std::abs (dragObjectLeft - (vertical ? previousPos.getY() : previousPos.getX()))
  485. < std::abs (dragObjectRight - (vertical ? current.getBottom() : current.getRight())))
  486. {
  487. newIndex = getIndexOfChildComponent (prev);
  488. }
  489. }
  490. if (auto* next = getNextActiveComponent (newIndex, 1))
  491. {
  492. auto nextPos = animator.getComponentDestination (next);
  493. if (std::abs (dragObjectLeft - (vertical ? current.getY() : current.getX()))
  494. > std::abs (dragObjectRight - (vertical ? nextPos.getBottom() : nextPos.getRight())))
  495. {
  496. newIndex = getIndexOfChildComponent (next) + 1;
  497. }
  498. }
  499. if (newIndex == currentIndex)
  500. break;
  501. items.removeObject (tc, false);
  502. removeChildComponent (tc);
  503. addChildComponent (tc, newIndex);
  504. items.insert (newIndex, tc);
  505. updateAllItemPositions (true);
  506. }
  507. }
  508. }
  509. void Toolbar::itemDragExit (const SourceDetails& dragSourceDetails)
  510. {
  511. if (auto* tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  512. {
  513. if (isParentOf (tc))
  514. {
  515. items.removeObject (tc, false);
  516. removeChildComponent (tc);
  517. updateAllItemPositions (true);
  518. }
  519. }
  520. }
  521. void Toolbar::itemDropped (const SourceDetails& dragSourceDetails)
  522. {
  523. if (auto* tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  524. tc->setState (Button::buttonNormal);
  525. }
  526. void Toolbar::lookAndFeelChanged()
  527. {
  528. missingItemsButton.reset (getLookAndFeel().createToolbarMissingItemsButton (*this));
  529. initMissingItemButton();
  530. }
  531. void Toolbar::mouseDown (const MouseEvent&) {}
  532. //==============================================================================
  533. class Toolbar::CustomisationDialog : public DialogWindow
  534. {
  535. public:
  536. CustomisationDialog (ToolbarItemFactory& factory, Toolbar& bar, int optionFlags)
  537. : DialogWindow (TRANS("Add/remove items from toolbar"), Colours::white, true, true),
  538. toolbar (bar)
  539. {
  540. setContentOwned (new CustomiserPanel (factory, toolbar, optionFlags), true);
  541. setResizable (true, true);
  542. setResizeLimits (400, 300, 1500, 1000);
  543. positionNearBar();
  544. }
  545. ~CustomisationDialog() override
  546. {
  547. toolbar.setEditingActive (false);
  548. }
  549. void closeButtonPressed() override
  550. {
  551. setVisible (false);
  552. }
  553. bool canModalEventBeSentToComponent (const Component* comp) override
  554. {
  555. return toolbar.isParentOf (comp)
  556. || dynamic_cast<const ToolbarItemComponent::ItemDragAndDropOverlayComponent*> (comp) != nullptr;
  557. }
  558. void positionNearBar()
  559. {
  560. auto screenSize = toolbar.getParentMonitorArea();
  561. auto pos = toolbar.getScreenPosition();
  562. const int gap = 8;
  563. if (toolbar.isVertical())
  564. {
  565. if (pos.x > screenSize.getCentreX())
  566. pos.x -= getWidth() - gap;
  567. else
  568. pos.x += toolbar.getWidth() + gap;
  569. }
  570. else
  571. {
  572. pos.x += (toolbar.getWidth() - getWidth()) / 2;
  573. if (pos.y > screenSize.getCentreY())
  574. pos.y -= getHeight() - gap;
  575. else
  576. pos.y += toolbar.getHeight() + gap;
  577. }
  578. setTopLeftPosition (pos);
  579. }
  580. private:
  581. Toolbar& toolbar;
  582. class CustomiserPanel : public Component
  583. {
  584. public:
  585. CustomiserPanel (ToolbarItemFactory& tbf, Toolbar& bar, int optionFlags)
  586. : factory (tbf), toolbar (bar), palette (tbf, bar),
  587. instructions ({}, TRANS ("You can drag the items above and drop them onto a toolbar to add them.")
  588. + "\n\n"
  589. + TRANS ("Items on the toolbar can also be dragged around to change their order, or dragged off the edge to delete them.")),
  590. defaultButton (TRANS ("Restore to default set of items"))
  591. {
  592. addAndMakeVisible (palette);
  593. if ((optionFlags & (Toolbar::allowIconsOnlyChoice
  594. | Toolbar::allowIconsWithTextChoice
  595. | Toolbar::allowTextOnlyChoice)) != 0)
  596. {
  597. addAndMakeVisible (styleBox);
  598. styleBox.setEditableText (false);
  599. if ((optionFlags & Toolbar::allowIconsOnlyChoice) != 0) styleBox.addItem (TRANS("Show icons only"), 1);
  600. if ((optionFlags & Toolbar::allowIconsWithTextChoice) != 0) styleBox.addItem (TRANS("Show icons and descriptions"), 2);
  601. if ((optionFlags & Toolbar::allowTextOnlyChoice) != 0) styleBox.addItem (TRANS("Show descriptions only"), 3);
  602. int selectedStyle = 0;
  603. switch (bar.getStyle())
  604. {
  605. case Toolbar::iconsOnly: selectedStyle = 1; break;
  606. case Toolbar::iconsWithText: selectedStyle = 2; break;
  607. case Toolbar::textOnly: selectedStyle = 3; break;
  608. default: break;
  609. }
  610. styleBox.setSelectedId (selectedStyle);
  611. styleBox.onChange = [this] { updateStyle(); };
  612. }
  613. if ((optionFlags & Toolbar::showResetToDefaultsButton) != 0)
  614. {
  615. addAndMakeVisible (defaultButton);
  616. defaultButton.onClick = [this] { toolbar.addDefaultItems (factory); };
  617. }
  618. addAndMakeVisible (instructions);
  619. instructions.setFont (Font (13.0f));
  620. setSize (500, 300);
  621. }
  622. void updateStyle()
  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. default: break;
  630. }
  631. palette.resized(); // to make it update the styles
  632. }
  633. void paint (Graphics& g) override
  634. {
  635. Colour background;
  636. if (auto* dw = findParentComponentOfClass<DialogWindow>())
  637. background = dw->getBackgroundColour();
  638. g.setColour (background.contrasting().withAlpha (0.3f));
  639. g.fillRect (palette.getX(), palette.getBottom() - 1, palette.getWidth(), 1);
  640. }
  641. void resized() override
  642. {
  643. palette.setBounds (0, 0, getWidth(), getHeight() - 120);
  644. styleBox.setBounds (10, getHeight() - 110, 200, 22);
  645. defaultButton.changeWidthToFitText (22);
  646. defaultButton.setTopLeftPosition (240, getHeight() - 110);
  647. instructions.setBounds (10, getHeight() - 80, getWidth() - 20, 80);
  648. }
  649. private:
  650. ToolbarItemFactory& factory;
  651. Toolbar& toolbar;
  652. ToolbarItemPalette palette;
  653. Label instructions;
  654. ComboBox styleBox;
  655. TextButton defaultButton;
  656. };
  657. };
  658. void Toolbar::showCustomisationDialog (ToolbarItemFactory& factory, const int optionFlags)
  659. {
  660. setEditingActive (true);
  661. (new CustomisationDialog (factory, *this, optionFlags))
  662. ->enterModalState (true, nullptr, true);
  663. }
  664. //==============================================================================
  665. std::unique_ptr<AccessibilityHandler> Toolbar::createAccessibilityHandler()
  666. {
  667. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::group);
  668. }
  669. } // namespace juce