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.

96 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. A utility class for fitting a set of objects whose sizes can vary between
  18. a minimum and maximum size, into a space.
  19. This is a trickier algorithm than it would first seem, so I've put it in this
  20. class to allow it to be shared by various bits of code.
  21. To use it, create one of these objects, call addItem() to add the list of items
  22. you need, then call resizeToFit(), which will change all their sizes. You can
  23. then retrieve the new sizes with getItemSize() and getNumItems().
  24. It's currently used by the TableHeaderComponent for stretching out the table
  25. headings to fill the table's width.
  26. @tags{GUI}
  27. */
  28. class StretchableObjectResizer
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an empty object resizer. */
  33. StretchableObjectResizer();
  34. /** Destructor. */
  35. ~StretchableObjectResizer();
  36. //==============================================================================
  37. /** Adds an item to the list.
  38. The order parameter lets you specify groups of items that are resized first when some
  39. space needs to be found. Those items with an order of 0 will be the first ones to be
  40. resized, and if that doesn't provide enough space to meet the requirements, the algorithm
  41. will then try resizing the items with an order of 1, then 2, and so on.
  42. */
  43. void addItem (double currentSize,
  44. double minSize,
  45. double maxSize,
  46. int order = 0);
  47. /** Resizes all the items to fit this amount of space.
  48. This will attempt to fit them in without exceeding each item's minimum and
  49. maximum sizes. In cases where none of the items can be expanded or enlarged any
  50. further, the final size may be greater or less than the size passed in.
  51. After calling this method, you can retrieve the new sizes with the getItemSize()
  52. method.
  53. */
  54. void resizeToFit (double targetSize);
  55. /** Returns the number of items that have been added. */
  56. int getNumItems() const noexcept { return items.size(); }
  57. /** Returns the size of one of the items. */
  58. double getItemSize (int index) const noexcept;
  59. private:
  60. //==============================================================================
  61. struct Item
  62. {
  63. double size;
  64. double minSize;
  65. double maxSize;
  66. int order;
  67. };
  68. Array<Item> items;
  69. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StretchableObjectResizer)
  70. };
  71. } // namespace juce