tb_scroll_container.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_SCROLL_CONTAINER_H
  6. #define TB_SCROLL_CONTAINER_H
  7. #include "tb_widgets_common.h"
  8. namespace tb {
  9. enum SCROLL_MODE {
  10. SCROLL_MODE_X_Y, ///< X and Y always scroll-mode: xy
  11. SCROLL_MODE_Y, ///< Y always (X never) scroll-mode: y
  12. SCROLL_MODE_Y_AUTO, ///< Y auto (X never) scroll-mode: y-auto
  13. SCROLL_MODE_X_AUTO_Y_AUTO, ///< X auto, Y auto scroll-mode: auto
  14. SCROLL_MODE_OFF ///< X any Y never scroll-mode: off
  15. };
  16. /** TBScrollContainerRoot - Internal for TBScrollContainer */
  17. class TBScrollContainerRoot : public TBWidget
  18. {
  19. private: // May only be used by TBScrollContainer.
  20. friend class TBScrollContainer;
  21. TBScrollContainerRoot() {}
  22. public:
  23. virtual void OnPaintChildren(const PaintProps &paint_props);
  24. virtual void GetChildTranslation(int &x, int &y) const;
  25. };
  26. /** TBScrollBarVisibility - Helper for TBScrollContainer or any other scrollable
  27. container that needs to solve scrollbar visibility according to SCROLL_MODE. */
  28. class TBScrollBarVisibility
  29. {
  30. public:
  31. TBScrollBarVisibility () : x_on(false), y_on(false), visible_w(0), visible_h(0) {}
  32. static TBScrollBarVisibility Solve(SCROLL_MODE mode, int content_w, int content_h,
  33. int available_w, int available_h,
  34. int scrollbar_x_h, int scrollbar_y_w);
  35. static bool IsAlwaysOnX(SCROLL_MODE mode) { return mode == SCROLL_MODE_X_Y; }
  36. static bool IsAlwaysOnY(SCROLL_MODE mode) { return mode == SCROLL_MODE_X_Y || mode == SCROLL_MODE_Y; }
  37. public:
  38. bool x_on, y_on;
  39. int visible_w, visible_h;
  40. };
  41. /** TBScrollContainer - A container with scrollbars that can scroll its children. */
  42. class TBScrollContainer : public TBWidget
  43. {
  44. friend class TBScrollContainerRoot;
  45. public:
  46. // For safe typecasting
  47. TBOBJECT_SUBCLASS(TBScrollContainer, TBWidget);
  48. TBScrollContainer();
  49. ~TBScrollContainer();
  50. /** Set to true if the preferred size of this container should adapt to the preferred
  51. size of the content. This is disabled by default. */
  52. void SetAdaptToContentSize(bool adapt);
  53. bool GetAdaptToContentSize() { return m_adapt_to_content_size; }
  54. /** Set to true if the content should adapt to the available size of this container
  55. when it's larger than the preferred size. */
  56. void SetAdaptContentSize(bool adapt);
  57. bool GetAdaptContentSize() { return m_adapt_content_size; }
  58. void SetIgnoreScrollEvents(bool ignore) { m_ignore_scroll_events = ignore; }
  59. void SetScrollMode(SCROLL_MODE mode);
  60. SCROLL_MODE GetScrollMode() { return m_mode; }
  61. virtual void ScrollTo(int x, int y);
  62. virtual TBWidget::ScrollInfo GetScrollInfo();
  63. virtual TBWidget *GetScrollRoot() { return &m_root; }
  64. virtual void InvalidateLayout(INVALIDATE_LAYOUT il);
  65. virtual TBRect GetPaddingRect();
  66. virtual PreferredSize OnCalculatePreferredContentSize(const SizeConstraints &constraints);
  67. virtual void OnInflate(const INFLATE_INFO &info);
  68. virtual bool OnEvent(const TBWidgetEvent &ev);
  69. virtual void OnProcess();
  70. virtual void OnResized(int old_w, int old_h);
  71. virtual TBWidget *GetContentRoot() { return &m_root; }
  72. protected:
  73. TBScrollBar m_scrollbar_x;
  74. TBScrollBar m_scrollbar_y;
  75. TBScrollContainerRoot m_root;
  76. bool m_adapt_to_content_size;
  77. bool m_adapt_content_size;
  78. bool m_layout_is_invalid;
  79. bool m_ignore_scroll_events;
  80. SCROLL_MODE m_mode;
  81. void ValidateLayout(const SizeConstraints &constraints);
  82. };
  83. };
  84. #endif // TB_SCROLL_CONTAINER_H