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.

103 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. //==============================================================================
  21. /**
  22. A utility class for fitting a set of objects whose sizes can vary between
  23. a minimum and maximum size, into a space.
  24. This is a trickier algorithm than it would first seem, so I've put it in this
  25. class to allow it to be shared by various bits of code.
  26. To use it, create one of these objects, call addItem() to add the list of items
  27. you need, then call resizeToFit(), which will change all their sizes. You can
  28. then retrieve the new sizes with getItemSize() and getNumItems().
  29. It's currently used by the TableHeaderComponent for stretching out the table
  30. headings to fill the table's width.
  31. @tags{GUI}
  32. */
  33. class StretchableObjectResizer
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates an empty object resizer. */
  38. StretchableObjectResizer();
  39. /** Destructor. */
  40. ~StretchableObjectResizer();
  41. //==============================================================================
  42. /** Adds an item to the list.
  43. The order parameter lets you specify groups of items that are resized first when some
  44. space needs to be found. Those items with an order of 0 will be the first ones to be
  45. resized, and if that doesn't provide enough space to meet the requirements, the algorithm
  46. will then try resizing the items with an order of 1, then 2, and so on.
  47. */
  48. void addItem (double currentSize,
  49. double minSize,
  50. double maxSize,
  51. int order = 0);
  52. /** Resizes all the items to fit this amount of space.
  53. This will attempt to fit them in without exceeding each item's minimum and
  54. maximum sizes. In cases where none of the items can be expanded or enlarged any
  55. further, the final size may be greater or less than the size passed in.
  56. After calling this method, you can retrieve the new sizes with the getItemSize()
  57. method.
  58. */
  59. void resizeToFit (double targetSize);
  60. /** Returns the number of items that have been added. */
  61. int getNumItems() const noexcept { return items.size(); }
  62. /** Returns the size of one of the items. */
  63. double getItemSize (int index) const noexcept;
  64. private:
  65. //==============================================================================
  66. struct Item
  67. {
  68. double size;
  69. double minSize;
  70. double maxSize;
  71. int order;
  72. };
  73. Array<Item> items;
  74. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StretchableObjectResizer)
  75. };
  76. } // namespace juce