The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. ToolbarItemPalette::ToolbarItemPalette (ToolbarItemFactory& factory_,
  18. Toolbar* const toolbar_)
  19. : factory (factory_),
  20. toolbar (toolbar_)
  21. {
  22. Component* const itemHolder = new Component();
  23. viewport.setViewedComponent (itemHolder);
  24. Array <int> allIds;
  25. factory.getAllToolbarItemIds (allIds);
  26. for (int i = 0; i < allIds.size(); ++i)
  27. addComponent (allIds.getUnchecked (i), -1);
  28. addAndMakeVisible (&viewport);
  29. }
  30. ToolbarItemPalette::~ToolbarItemPalette()
  31. {
  32. }
  33. //==============================================================================
  34. void ToolbarItemPalette::addComponent (const int itemId, const int index)
  35. {
  36. if (ToolbarItemComponent* const tc = Toolbar::createItem (factory, itemId))
  37. {
  38. items.insert (index, tc);
  39. viewport.getViewedComponent()->addAndMakeVisible (tc, index);
  40. tc->setEditingMode (ToolbarItemComponent::editableOnPalette);
  41. }
  42. else
  43. {
  44. jassertfalse;
  45. }
  46. }
  47. void ToolbarItemPalette::replaceComponent (ToolbarItemComponent* const comp)
  48. {
  49. const int index = items.indexOf (comp);
  50. jassert (index >= 0);
  51. items.removeObject (comp, false);
  52. addComponent (comp->getItemId(), index);
  53. resized();
  54. }
  55. void ToolbarItemPalette::resized()
  56. {
  57. viewport.setBoundsInset (BorderSize<int> (1));
  58. Component* const itemHolder = viewport.getViewedComponent();
  59. const int indent = 8;
  60. const int preferredWidth = viewport.getWidth() - viewport.getScrollBarThickness() - indent;
  61. const int height = toolbar->getThickness();
  62. int x = indent;
  63. int y = indent;
  64. int maxX = 0;
  65. for (int i = 0; i < items.size(); ++i)
  66. {
  67. ToolbarItemComponent* const tc = items.getUnchecked(i);
  68. tc->setStyle (toolbar->getStyle());
  69. int preferredSize = 1, minSize = 1, maxSize = 1;
  70. if (tc->getToolbarItemSizes (height, false, preferredSize, minSize, maxSize))
  71. {
  72. if (x + preferredSize > preferredWidth && x > indent)
  73. {
  74. x = indent;
  75. y += height;
  76. }
  77. tc->setBounds (x, y, preferredSize, height);
  78. x += preferredSize + 8;
  79. maxX = jmax (maxX, x);
  80. }
  81. }
  82. itemHolder->setSize (maxX, y + height + 8);
  83. }