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

9 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. const char* const Toolbar::toolbarDragDescriptor = "_toolbarItem_";
  18. //==============================================================================
  19. class Toolbar::Spacer : public ToolbarItemComponent
  20. {
  21. public:
  22. Spacer (const int itemId_, const float fixedSize_, const bool drawBar_)
  23. : ToolbarItemComponent (itemId_, String::empty, false),
  24. fixedSize (fixedSize_),
  25. drawBar (drawBar_)
  26. {
  27. }
  28. bool getToolbarItemSizes (int toolbarThickness, bool /*isToolbarVertical*/,
  29. int& preferredSize, int& minSize, int& maxSize) override
  30. {
  31. if (fixedSize <= 0)
  32. {
  33. preferredSize = toolbarThickness * 2;
  34. minSize = 4;
  35. maxSize = 32768;
  36. }
  37. else
  38. {
  39. maxSize = roundToInt (toolbarThickness * fixedSize);
  40. minSize = drawBar ? maxSize : jmin (4, maxSize);
  41. preferredSize = maxSize;
  42. if (getEditingMode() == editableOnPalette)
  43. preferredSize = maxSize = toolbarThickness / (drawBar ? 3 : 2);
  44. }
  45. return true;
  46. }
  47. void paintButtonArea (Graphics&, int, int, bool, bool) override
  48. {
  49. }
  50. void contentAreaChanged (const Rectangle<int>&) override
  51. {
  52. }
  53. int getResizeOrder() const noexcept
  54. {
  55. return fixedSize <= 0 ? 0 : 1;
  56. }
  57. void paint (Graphics& g) override
  58. {
  59. const int w = getWidth();
  60. const int h = getHeight();
  61. if (drawBar)
  62. {
  63. g.setColour (findColour (Toolbar::separatorColourId, true));
  64. const float thickness = 0.2f;
  65. if (isToolbarVertical())
  66. g.fillRect (w * 0.1f, h * (0.5f - thickness * 0.5f), w * 0.8f, h * thickness);
  67. else
  68. g.fillRect (w * (0.5f - thickness * 0.5f), h * 0.1f, w * thickness, h * 0.8f);
  69. }
  70. if (getEditingMode() != normalMode && ! drawBar)
  71. {
  72. g.setColour (findColour (Toolbar::separatorColourId, true));
  73. const int indentX = jmin (2, (w - 3) / 2);
  74. const int indentY = jmin (2, (h - 3) / 2);
  75. g.drawRect (indentX, indentY, w - indentX * 2, h - indentY * 2, 1);
  76. if (fixedSize <= 0)
  77. {
  78. float x1, y1, x2, y2, x3, y3, x4, y4, hw, hl;
  79. if (isToolbarVertical())
  80. {
  81. x1 = w * 0.5f;
  82. y1 = h * 0.4f;
  83. x2 = x1;
  84. y2 = indentX * 2.0f;
  85. x3 = x1;
  86. y3 = h * 0.6f;
  87. x4 = x1;
  88. y4 = h - y2;
  89. hw = w * 0.15f;
  90. hl = w * 0.2f;
  91. }
  92. else
  93. {
  94. x1 = w * 0.4f;
  95. y1 = h * 0.5f;
  96. x2 = indentX * 2.0f;
  97. y2 = y1;
  98. x3 = w * 0.6f;
  99. y3 = y1;
  100. x4 = w - x2;
  101. y4 = y1;
  102. hw = h * 0.15f;
  103. hl = h * 0.2f;
  104. }
  105. Path p;
  106. p.addArrow (Line<float> (x1, y1, x2, y2), 1.5f, hw, hl);
  107. p.addArrow (Line<float> (x3, y3, x4, y4), 1.5f, hw, hl);
  108. g.fillPath (p);
  109. }
  110. }
  111. }
  112. private:
  113. const float fixedSize;
  114. const bool drawBar;
  115. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Spacer)
  116. };
  117. //==============================================================================
  118. class Toolbar::MissingItemsComponent : public PopupMenu::CustomComponent
  119. {
  120. public:
  121. MissingItemsComponent (Toolbar& bar, const int h)
  122. : PopupMenu::CustomComponent (true),
  123. owner (&bar),
  124. height (h)
  125. {
  126. for (int i = bar.items.size(); --i >= 0;)
  127. {
  128. ToolbarItemComponent* const tc = bar.items.getUnchecked(i);
  129. if (dynamic_cast<Spacer*> (tc) == nullptr && ! tc->isVisible())
  130. {
  131. oldIndexes.insert (0, i);
  132. addAndMakeVisible (tc, 0);
  133. }
  134. }
  135. layout (400);
  136. }
  137. ~MissingItemsComponent()
  138. {
  139. if (owner != nullptr)
  140. {
  141. for (int i = 0; i < getNumChildComponents(); ++i)
  142. {
  143. if (ToolbarItemComponent* const tc = dynamic_cast<ToolbarItemComponent*> (getChildComponent (i)))
  144. {
  145. tc->setVisible (false);
  146. const int index = oldIndexes.remove (i);
  147. owner->addChildComponent (tc, index);
  148. --i;
  149. }
  150. }
  151. owner->resized();
  152. }
  153. }
  154. void layout (const int preferredWidth)
  155. {
  156. const int indent = 8;
  157. int x = indent;
  158. int y = indent;
  159. int maxX = 0;
  160. for (int i = 0; i < getNumChildComponents(); ++i)
  161. {
  162. if (ToolbarItemComponent* const tc = dynamic_cast<ToolbarItemComponent*> (getChildComponent (i)))
  163. {
  164. int preferredSize = 1, minSize = 1, maxSize = 1;
  165. if (tc->getToolbarItemSizes (height, false, preferredSize, minSize, maxSize))
  166. {
  167. if (x + preferredSize > preferredWidth && x > indent)
  168. {
  169. x = indent;
  170. y += height;
  171. }
  172. tc->setBounds (x, y, preferredSize, height);
  173. x += preferredSize;
  174. maxX = jmax (maxX, x);
  175. }
  176. }
  177. }
  178. setSize (maxX + 8, y + height + 8);
  179. }
  180. void getIdealSize (int& idealWidth, int& idealHeight) override
  181. {
  182. idealWidth = getWidth();
  183. idealHeight = getHeight();
  184. }
  185. private:
  186. Component::SafePointer<Toolbar> owner;
  187. const int height;
  188. Array<int> oldIndexes;
  189. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MissingItemsComponent)
  190. };
  191. //==============================================================================
  192. Toolbar::Toolbar()
  193. : vertical (false),
  194. isEditingActive (false),
  195. toolbarStyle (Toolbar::iconsOnly)
  196. {
  197. addChildComponent (missingItemsButton = getLookAndFeel().createToolbarMissingItemsButton (*this));
  198. missingItemsButton->setAlwaysOnTop (true);
  199. missingItemsButton->addListener (this);
  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 (ToolbarItemComponent* const 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,
  245. const int itemId,
  246. const 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 (int i = 0; i < ids.size(); ++i)
  257. addItemInternal (factoryToUse, ids.getUnchecked (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 (ToolbarItemComponent* const 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 (ToolbarItemComponent* const 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 (ToolbarItemComponent* const 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 (int i = 0; i < tokens.size(); ++i)
  329. addItemInternal (factoryToUse, tokens[i].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 (const bool animate)
  359. {
  360. if (getWidth() > 0 && getHeight() > 0)
  361. {
  362. StretchableObjectResizer resizer;
  363. for (int i = 0; i < items.size(); ++i)
  364. {
  365. ToolbarItemComponent* const tc = items.getUnchecked(i);
  366. tc->setEditingMode (isEditingActive ? ToolbarItemComponent::editableOnToolbar
  367. : ToolbarItemComponent::normalMode);
  368. tc->setStyle (toolbarStyle);
  369. Spacer* const 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. const int 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. const int maxLength = itemsOffTheEnd ? (vertical ? missingItemsButton->getY()
  400. : missingItemsButton->getX()) - 4
  401. : getLength();
  402. int pos = 0, activeIndex = 0;
  403. for (int i = 0; i < items.size(); ++i)
  404. {
  405. ToolbarItemComponent* const tc = items.getUnchecked(i);
  406. if (tc->isActive)
  407. {
  408. const int size = (int) resizer.getItemSize (activeIndex++);
  409. Rectangle<int> newBounds;
  410. if (vertical)
  411. newBounds.setBounds (0, pos, getWidth(), size);
  412. else
  413. newBounds.setBounds (pos, 0, size, getHeight());
  414. ComponentAnimator& animator = Desktop::getInstance().getAnimator();
  415. if (animate)
  416. {
  417. animator.animateComponent (tc, newBounds, 1.0f, 200, false, 3.0, 0.0);
  418. }
  419. else
  420. {
  421. animator.cancelAnimation (tc, false);
  422. tc->setBounds (newBounds);
  423. }
  424. pos += size;
  425. tc->setVisible (pos <= maxLength
  426. && ((! tc->isBeingDragged)
  427. || tc->getEditingMode() == ToolbarItemComponent::editableOnPalette));
  428. }
  429. }
  430. }
  431. }
  432. //==============================================================================
  433. void Toolbar::buttonClicked (Button*)
  434. {
  435. jassert (missingItemsButton->isShowing());
  436. if (missingItemsButton->isShowing())
  437. {
  438. PopupMenu m;
  439. m.addCustomItem (1, new MissingItemsComponent (*this, getThickness()));
  440. m.showMenuAsync (PopupMenu::Options().withTargetComponent (missingItemsButton), nullptr);
  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 (ToolbarItemComponent* const tc = dynamic_cast<ToolbarItemComponent*> (dragSourceDetails.sourceComponent.get()))
  451. {
  452. if (! items.contains (tc))
  453. {
  454. if (tc->getEditingMode() == ToolbarItemComponent::editableOnPalette)
  455. {
  456. if (ToolbarItemPalette* const 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. ComponentAnimator& animator = Desktop::getInstance().getAnimator();
  468. for (int i = getNumItems(); --i >= 0;)
  469. {
  470. const int currentIndex = items.indexOf (tc);
  471. int newIndex = currentIndex;
  472. const int dragObjectLeft = vertical ? (dragSourceDetails.localPosition.getY() - tc->dragOffsetY)
  473. : (dragSourceDetails.localPosition.getX() - tc->dragOffsetX);
  474. const int dragObjectRight = dragObjectLeft + (vertical ? tc->getHeight() : tc->getWidth());
  475. const Rectangle<int> current (animator.getComponentDestination (getChildComponent (newIndex)));
  476. if (ToolbarItemComponent* const prev = getNextActiveComponent (newIndex, -1))
  477. {
  478. const Rectangle<int> 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 (ToolbarItemComponent* const next = getNextActiveComponent (newIndex, 1))
  486. {
  487. const Rectangle<int> 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 (ToolbarItemComponent* const 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 (ToolbarItemComponent* const 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()
  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. const Rectangle<int> screenSize (toolbar.getParentMonitorArea());
  551. Point<int> 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. private ComboBoxListener, // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  574. private ButtonListener
  575. {
  576. public:
  577. CustomiserPanel (ToolbarItemFactory& tbf, Toolbar& bar, int optionFlags)
  578. : factory (tbf), toolbar (bar), palette (tbf, bar),
  579. instructions (String::empty, TRANS ("You can drag the items above and drop them onto a toolbar to add them.")
  580. + "\n\n"
  581. + TRANS ("Items on the toolbar can also be dragged around to change their order, or dragged off the edge to delete them.")),
  582. defaultButton (TRANS ("Restore to default set of items"))
  583. {
  584. addAndMakeVisible (palette);
  585. if ((optionFlags & (Toolbar::allowIconsOnlyChoice
  586. | Toolbar::allowIconsWithTextChoice
  587. | Toolbar::allowTextOnlyChoice)) != 0)
  588. {
  589. addAndMakeVisible (styleBox);
  590. styleBox.setEditableText (false);
  591. if ((optionFlags & Toolbar::allowIconsOnlyChoice) != 0) styleBox.addItem (TRANS("Show icons only"), 1);
  592. if ((optionFlags & Toolbar::allowIconsWithTextChoice) != 0) styleBox.addItem (TRANS("Show icons and descriptions"), 2);
  593. if ((optionFlags & Toolbar::allowTextOnlyChoice) != 0) styleBox.addItem (TRANS("Show descriptions only"), 3);
  594. int selectedStyle = 0;
  595. switch (bar.getStyle())
  596. {
  597. case Toolbar::iconsOnly: selectedStyle = 1; break;
  598. case Toolbar::iconsWithText: selectedStyle = 2; break;
  599. case Toolbar::textOnly: selectedStyle = 3; break;
  600. }
  601. styleBox.setSelectedId (selectedStyle);
  602. styleBox.addListener (this);
  603. }
  604. if ((optionFlags & Toolbar::showResetToDefaultsButton) != 0)
  605. {
  606. addAndMakeVisible (defaultButton);
  607. defaultButton.addListener (this);
  608. }
  609. addAndMakeVisible (instructions);
  610. instructions.setFont (Font (13.0f));
  611. setSize (500, 300);
  612. }
  613. void comboBoxChanged (ComboBox*) override
  614. {
  615. switch (styleBox.getSelectedId())
  616. {
  617. case 1: toolbar.setStyle (Toolbar::iconsOnly); break;
  618. case 2: toolbar.setStyle (Toolbar::iconsWithText); break;
  619. case 3: toolbar.setStyle (Toolbar::textOnly); break;
  620. }
  621. palette.resized(); // to make it update the styles
  622. }
  623. void buttonClicked (Button*) override
  624. {
  625. toolbar.addDefaultItems (factory);
  626. }
  627. void paint (Graphics& g) override
  628. {
  629. Colour background;
  630. if (DialogWindow* const dw = findParentComponentOfClass<DialogWindow>())
  631. background = dw->getBackgroundColour();
  632. g.setColour (background.contrasting().withAlpha (0.3f));
  633. g.fillRect (palette.getX(), palette.getBottom() - 1, palette.getWidth(), 1);
  634. }
  635. void resized() override
  636. {
  637. palette.setBounds (0, 0, getWidth(), getHeight() - 120);
  638. styleBox.setBounds (10, getHeight() - 110, 200, 22);
  639. defaultButton.changeWidthToFitText (22);
  640. defaultButton.setTopLeftPosition (240, getHeight() - 110);
  641. instructions.setBounds (10, getHeight() - 80, getWidth() - 20, 80);
  642. }
  643. private:
  644. ToolbarItemFactory& factory;
  645. Toolbar& toolbar;
  646. ToolbarItemPalette palette;
  647. Label instructions;
  648. ComboBox styleBox;
  649. TextButton defaultButton;
  650. };
  651. };
  652. void Toolbar::showCustomisationDialog (ToolbarItemFactory& factory, const int optionFlags)
  653. {
  654. setEditingActive (true);
  655. (new CustomisationDialog (factory, *this, optionFlags))
  656. ->enterModalState (true, nullptr, true);
  657. }