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_ToolbarItemPalette.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. ToolbarItemPalette::ToolbarItemPalette (ToolbarItemFactory& tbf, Toolbar& bar)
  21. : factory (tbf), toolbar (bar)
  22. {
  23. auto* itemHolder = new Component();
  24. viewport.setViewedComponent (itemHolder);
  25. Array<int> allIds;
  26. factory.getAllToolbarItemIds (allIds);
  27. for (auto& i : allIds)
  28. addComponent (i, -1);
  29. addAndMakeVisible (viewport);
  30. }
  31. ToolbarItemPalette::~ToolbarItemPalette()
  32. {
  33. }
  34. //==============================================================================
  35. void ToolbarItemPalette::addComponent (const int itemId, const int index)
  36. {
  37. if (auto* tc = Toolbar::createItem (factory, itemId))
  38. {
  39. items.insert (index, tc);
  40. viewport.getViewedComponent()->addAndMakeVisible (tc, index);
  41. tc->setEditingMode (ToolbarItemComponent::editableOnPalette);
  42. }
  43. else
  44. {
  45. jassertfalse;
  46. }
  47. }
  48. void ToolbarItemPalette::replaceComponent (ToolbarItemComponent& comp)
  49. {
  50. auto 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. auto* itemHolder = viewport.getViewedComponent();
  60. const int indent = 8;
  61. const int preferredWidth = viewport.getWidth() - viewport.getScrollBarThickness() - indent;
  62. const int height = toolbar.getThickness();
  63. auto x = indent;
  64. auto y = indent;
  65. int maxX = 0;
  66. for (auto* tc : items)
  67. {
  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. }
  84. } // namespace juce