tb_layout.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_LAYOUT_H
  6. #define TB_LAYOUT_H
  7. #include "tb_widgets.h"
  8. namespace tb {
  9. /** This means the spacing should be the default, read from the skin. */
  10. #define SPACING_FROM_SKIN TB_INVALID_DIMENSION
  11. /** Specifies which height widgets in a AXIS_X layout should have,
  12. or which width widgets in a AXIS_Y layout should have.
  13. No matter what, it will still prioritize minimum and maximum for each widget. */
  14. enum LAYOUT_SIZE {
  15. LAYOUT_SIZE_GRAVITY, ///< Sizes depend on the gravity for each widget. (If the widget pulls
  16. ///< towards both directions, it should grow to all available space)
  17. LAYOUT_SIZE_PREFERRED, ///< Size will be the preferred so each widget may be sized differently.
  18. LAYOUT_SIZE_AVAILABLE ///< Size should grow to all available space
  19. };
  20. /** Specifies which y position widgets in a AXIS_X layout should have,
  21. or which x position widgets in a AXIS_Y layout should have. */
  22. enum LAYOUT_POSITION {
  23. LAYOUT_POSITION_CENTER, ///< Position is centered
  24. LAYOUT_POSITION_LEFT_TOP, ///< Position is to the left for AXIS_Y layout and top for AXIS_X layout.
  25. LAYOUT_POSITION_RIGHT_BOTTOM, ///< Position is to the right for AXIS_Y layout and bottom for AXIS_X layout.
  26. LAYOUT_POSITION_GRAVITY, ///< Position depend on the gravity for each widget. (If the widget pulls
  27. ///< towards both directions, it will be centered)
  28. };
  29. /** Specifies which width widgets in a AXIS_X layout should have,
  30. or which height widgets in a AXIS_Y layout should have. */
  31. enum LAYOUT_DISTRIBUTION {
  32. LAYOUT_DISTRIBUTION_PREFERRED, ///< Size will be the preferred so each widget may be sized differently.
  33. LAYOUT_DISTRIBUTION_AVAILABLE, ///< Size should grow to all available space
  34. LAYOUT_DISTRIBUTION_GRAVITY ///< Sizes depend on the gravity for each widget. (If the widget pulls
  35. ///< towards both directions, it should grow to all available space)
  36. };
  37. /** Specifies how widgets should be moved horizontally in a AXIS_X
  38. layout (or vertically in a AXIS_Y layout) if there is extra space
  39. available. */
  40. enum LAYOUT_DISTRIBUTION_POSITION {
  41. LAYOUT_DISTRIBUTION_POSITION_CENTER,
  42. LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP,
  43. LAYOUT_DISTRIBUTION_POSITION_RIGHT_BOTTOM
  44. };
  45. /** Layout order parameter for TBLayout::SetLayoutOrder. */
  46. enum LAYOUT_ORDER {
  47. LAYOUT_ORDER_BOTTOM_TO_TOP, ///< From bottom to top widget (default creation order).
  48. LAYOUT_ORDER_TOP_TO_BOTTOM ///< From top to bottom widget.
  49. };
  50. /** Specifies what happens when there is not enough room for the layout, even
  51. when all the children have been shrunk to their minimum size. */
  52. enum LAYOUT_OVERFLOW {
  53. LAYOUT_OVERFLOW_CLIP,
  54. LAYOUT_OVERFLOW_SCROLL
  55. //LAYOUT_OVERFLOW_WRAP
  56. };
  57. /** TBLayout layouts its children along the given axis.
  58. Each widgets size depend on its preferred size (See TBWidget::GetPreferredSize),
  59. gravity, and the specified layout settings (See SetLayoutSize, SetLayoutPosition
  60. SetLayoutOverflow, SetLayoutDistribution, SetLayoutDistributionPosition), and
  61. the available size.
  62. Each widget is also separated by the specified spacing (See SetSpacing).
  63. */
  64. class TBLayout : public TBWidget
  65. {
  66. public:
  67. // For safe typecasting
  68. TBOBJECT_SUBCLASS(TBLayout, TBWidget);
  69. TBLayout(AXIS axis = AXIS_X);
  70. /** Set along which axis the content should be layouted */
  71. virtual void SetAxis(AXIS axis);
  72. virtual AXIS GetAxis() const { return m_axis; }
  73. /** Set the spacing between widgets in this layout. Setting the default (SPACING_FROM_SKIN)
  74. will make it use the spacing specified in the skin. */
  75. void SetSpacing(int spacing);
  76. int GetSpacing() const { return m_spacing; }
  77. /** Set the overflow scroll. If there is not enough room for all children in this layout,
  78. it can scroll in the axis it's laid out. It does so automatically by wheel or panning also
  79. for other LAYOUT_OVERFLOW than LAYOUT_OVERFLOW_SCROLL. */
  80. void SetOverflowScroll(int overflow_scroll);
  81. int GetOverflowScroll() const { return m_overflow_scroll; }
  82. /** Set if a fadeout should be painter where the layout overflows or not. */
  83. void SetPaintOverflowFadeout(bool paint_fadeout) { m_packed.paint_overflow_fadeout = paint_fadeout; }
  84. /** Set the layout size mode. See LAYOUT_SIZE. */
  85. void SetLayoutSize(LAYOUT_SIZE size);
  86. /** Set the layout position mode. See LAYOUT_POSITION. */
  87. void SetLayoutPosition(LAYOUT_POSITION pos);
  88. /** Set the layout size mode. See LAYOUT_OVERFLOW. */
  89. void SetLayoutOverflow(LAYOUT_OVERFLOW overflow);
  90. /** Set the layout distribution mode. See LAYOUT_DISTRIBUTION. */
  91. void SetLayoutDistribution(LAYOUT_DISTRIBUTION distribution);
  92. /** Set the layout distribution position mode. See LAYOUT_DISTRIBUTION_POSITION. */
  93. void SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION distribution_pos);
  94. /** Set the layout order. The default is LAYOUT_ORDER_BOTTOM_TO_TOP, which begins
  95. from bottom to top (default creation order). */
  96. void SetLayoutOrder(LAYOUT_ORDER order);
  97. virtual void InvalidateLayout(INVALIDATE_LAYOUT il);
  98. virtual PreferredSize OnCalculatePreferredContentSize(const SizeConstraints &constraints);
  99. virtual void OnInflate(const INFLATE_INFO &info);
  100. virtual bool OnEvent(const TBWidgetEvent &ev);
  101. virtual void OnPaintChildren(const PaintProps &paint_props);
  102. virtual void OnProcess();
  103. virtual void OnResized(int old_w, int old_h);
  104. virtual void OnInflateChild(TBWidget *child);
  105. virtual void GetChildTranslation(int &x, int &y) const;
  106. virtual void ScrollTo(int x, int y);
  107. virtual TBWidget::ScrollInfo GetScrollInfo();
  108. // ATOMIC BEGIN
  109. /// A different way of setting up a layout, using a series of characters to program the main 5 layout fields
  110. /// character [0] = 'X' or 'Y' for UI_AXIS = X(D), Y
  111. /// character [1] = 'A'|'G'|'P' for UI_LAYOUT_SIZE = Available, Gravity(D), Perferred
  112. /// character [2] = 'C'|'G'|'L'|'R' for UI_LAYOUT_POSITION = Center(D), Gravity, LeftTop, RightBottom
  113. /// character [3] = 'A'|'G'|'P' for UI_LAYOUT_DISTRIBUTION = Available, Gravity, Perferred(D)
  114. /// character [4] = 'C'|'L'|'R' for UI_LAYOUT_DISTRIBUTION_POSITION, Center(D), LeftTop, RightBottom
  115. /// A '-' character in any field will not program that entry.
  116. /// Any character used that is not an allowed characters or a '-' will result in the default setting to be used.
  117. /// Any text in the string above character 5 is ignored.
  118. /// If the input string is less than 5 characters it will not program any of the fields.
  119. void SetLayoutConfig ( const char *settings );
  120. // ATOMIC END
  121. protected:
  122. AXIS m_axis;
  123. int m_spacing;
  124. int m_overflow;
  125. int m_overflow_scroll;
  126. union {
  127. struct {
  128. uint32 layout_is_invalid : 1;
  129. uint32 layout_mode_size : 4;
  130. uint32 layout_mode_pos : 4;
  131. uint32 layout_mode_overflow : 4;
  132. uint32 layout_mode_dist : 4;
  133. uint32 layout_mode_dist_pos : 4;
  134. uint32 mode_reverse_order : 1;
  135. uint32 paint_overflow_fadeout : 1;
  136. } m_packed;
  137. uint32 m_packed_init;
  138. };
  139. void ValidateLayout(const SizeConstraints &constraints, PreferredSize *calculate_ps = nullptr);
  140. bool QualifyForExpansion(WIDGET_GRAVITY gravity) const;
  141. int GetWantedHeight(WIDGET_GRAVITY gravity, const PreferredSize &ps, int available_height) const;
  142. TBWidget *GetNextNonCollapsedWidget(TBWidget *child) const;
  143. int GetTrailingSpace(TBWidget *child, int spacing) const;
  144. int CalculateSpacing();
  145. TBWidget *GetFirstInLayoutOrder() const;
  146. TBWidget *GetNextInLayoutOrder(TBWidget *child) const;
  147. };
  148. };
  149. #endif // TB_LAYOUT_H