juce_StretchableObjectResizer.h 3.6 KB

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