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.5KB

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