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.

115 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. ToolbarItemPalette::ToolbarItemPalette (ToolbarItemFactory& factory_,
  21. Toolbar* const toolbar_)
  22. : factory (factory_),
  23. toolbar (toolbar_)
  24. {
  25. Component* const itemHolder = new Component();
  26. viewport.setViewedComponent (itemHolder);
  27. Array <int> allIds;
  28. factory.getAllToolbarItemIds (allIds);
  29. for (int i = 0; i < allIds.size(); ++i)
  30. addComponent (allIds.getUnchecked (i), -1);
  31. addAndMakeVisible (&viewport);
  32. }
  33. ToolbarItemPalette::~ToolbarItemPalette()
  34. {
  35. }
  36. //==============================================================================
  37. void ToolbarItemPalette::addComponent (const int itemId, const int index)
  38. {
  39. ToolbarItemComponent* const tc = Toolbar::createItem (factory, itemId);
  40. jassert (tc != nullptr);
  41. if (tc != nullptr)
  42. {
  43. items.insert (index, tc);
  44. viewport.getViewedComponent()->addAndMakeVisible (tc, index);
  45. tc->setEditingMode (ToolbarItemComponent::editableOnPalette);
  46. }
  47. }
  48. void ToolbarItemPalette::replaceComponent (ToolbarItemComponent* const comp)
  49. {
  50. const int index = items.indexOf (comp);
  51. jassert (index >= 0);
  52. items.removeObject (comp, false);
  53. addComponent (comp->getItemId(), index);
  54. resized();
  55. }
  56. void ToolbarItemPalette::resized()
  57. {
  58. viewport.setBoundsInset (BorderSize<int> (1));
  59. Component* const itemHolder = viewport.getViewedComponent();
  60. const int indent = 8;
  61. const int preferredWidth = viewport.getWidth() - viewport.getScrollBarThickness() - indent;
  62. const int height = toolbar->getThickness();
  63. int x = indent;
  64. int y = indent;
  65. int maxX = 0;
  66. for (int i = 0; i < items.size(); ++i)
  67. {
  68. ToolbarItemComponent* const tc = items.getUnchecked(i);
  69. tc->setStyle (toolbar->getStyle());
  70. int preferredSize = 1, minSize = 1, maxSize = 1;
  71. if (tc->getToolbarItemSizes (height, false, preferredSize, minSize, maxSize))
  72. {
  73. if (x + preferredSize > preferredWidth && x > indent)
  74. {
  75. x = indent;
  76. y += height;
  77. }
  78. tc->setBounds (x, y, preferredSize, height);
  79. x += preferredSize + 8;
  80. maxX = jmax (maxX, x);
  81. }
  82. }
  83. itemHolder->setSize (maxX, y + height + 8);
  84. }
  85. END_JUCE_NAMESPACE