tb_toggle_container.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_TOGGLE_CONTAINER_H
  6. #define TB_TOGGLE_CONTAINER_H
  7. #include "tb_widgets_common.h"
  8. namespace tb {
  9. /** TBToggleContainer is a widget that toggles a property when its value
  10. change between 0 and 1. TOGGLE specifies what property will toggle.
  11. This is useful f.ex to toggle a whole group of child widgets depending
  12. on the value of some other widget. By connecting the TBToggleContainer
  13. with a widget connection, this can happen completly automatically. */
  14. class TBToggleContainer : public TBWidget
  15. {
  16. public:
  17. // For safe typecasting
  18. TBOBJECT_SUBCLASS(TBToggleContainer, TBWidget);
  19. TBToggleContainer();
  20. /** Defines what should toggle when the value changes. */
  21. enum TOGGLE {
  22. TOGGLE_NOTHING, ///< Nothing happens (the default)
  23. TOGGLE_ENABLED, ///< Enabled/disabled state
  24. TOGGLE_OPACITY, ///< Opacity 1/0
  25. TOGGLE_EXPANDED ///< Expanded/collapsed (In parent axis direction)
  26. };
  27. /** Set what should toggle when the value changes. */
  28. void SetToggle(TOGGLE toggle);
  29. TOGGLE GetToggle() const { return m_toggle; }
  30. /** Set if the toggle state should be inverted. */
  31. void SetInvert(bool invert);
  32. bool GetInvert() const { return m_invert; }
  33. /** Get the current value, after checking the invert mode. */
  34. bool GetIsOn() const { return m_invert ? !m_value : !!m_value; }
  35. /** Set the value of this widget. 1 will turn on the toggle, 0 will turn it off (or
  36. the opposite if the invert mode is set). */
  37. virtual void SetValue(int value);
  38. virtual int GetValue() { return m_value; }
  39. virtual void OnInflate(const INFLATE_INFO &info);
  40. private:
  41. void UpdateInternal();
  42. TOGGLE m_toggle;
  43. bool m_invert;
  44. int m_value;
  45. };
  46. /** TBSectionHeader is just a thin wrapper for a TBButton that is in toggle
  47. mode with the skin TBSectionHeader by default. It is used as the clickable
  48. header in TBSection that toggles the section. */
  49. class TBSectionHeader : public TBButton
  50. {
  51. public:
  52. // For safe typecasting
  53. TBOBJECT_SUBCLASS(TBSectionHeader, TBButton);
  54. TBSectionHeader();
  55. virtual bool OnEvent(const TBWidgetEvent &ev);
  56. };
  57. /** TBSection is a widget with a header that when clicked toggles its children
  58. on and off (using a internal TBToggleContainer with TOGGLE_EXPANDED).
  59. The header is a TBSectionHeader.
  60. The skin names of the internal widgets are:
  61. TBSection - This widget itself.
  62. TBSection.layout - The layout that wraps the header and the container.
  63. TBSection.container - The toggle container with the children that expands/collapses.
  64. */
  65. class TBSection : public TBWidget
  66. {
  67. public:
  68. // For safe typecasting
  69. TBOBJECT_SUBCLASS(TBSection, TBWidget);
  70. TBSection();
  71. ~TBSection();
  72. TBLayout *GetLayout() { return &m_layout; }
  73. TBSectionHeader *GetHeader() { return &m_header; }
  74. TBToggleContainer *GetContainer() { return &m_toggle_container; }
  75. /** Set if the section should be scrolled into view after next layout. */
  76. void SetPendingScrollIntoView(bool pending_scroll) { m_pending_scroll = pending_scroll; }
  77. /** Set the text of the text field. */
  78. virtual bool SetText(const char *text) { return m_header.SetText(text); }
  79. virtual bool GetText(TBStr &text) { return m_header.GetText(text); }
  80. using TBWidget::GetText; ///< Make all versions in base class available.
  81. virtual void SetValue(int value);
  82. virtual int GetValue() { return m_toggle_container.GetValue(); }
  83. // ATOMIC BEGIN
  84. virtual void AddToggleChild(TBWidget *child, WIDGET_Z z = WIDGET_Z_TOP, WIDGET_INVOKE_INFO info = WIDGET_INVOKE_INFO_NORMAL);
  85. virtual void AddToggleChildRelative(TBWidget *child, WIDGET_Z_REL z, TBWidget *reference, WIDGET_INVOKE_INFO info = WIDGET_INVOKE_INFO_NORMAL);
  86. virtual void RemoveToggleChild(TBWidget* child);
  87. virtual void DeleteAllToggleChildren();
  88. virtual TBWidget* GetFirstToggleChild();
  89. virtual TBWidget* GetToggleWidgetById(const TBID &id);
  90. // ATOMIC END
  91. virtual TBWidget *GetContentRoot() { return m_toggle_container.GetContentRoot(); }
  92. virtual void OnProcessAfterChildren();
  93. virtual PreferredSize OnCalculatePreferredSize(const SizeConstraints &constraints);
  94. private:
  95. TBLayout m_layout;
  96. TBSectionHeader m_header;
  97. TBToggleContainer m_toggle_container;
  98. bool m_pending_scroll;
  99. // ATOMIC BEGIN
  100. TBLayout m_toggle_container_layout;
  101. // ATOMIC END
  102. };
  103. }; // namespace tb
  104. #endif // TB_TOGGLE_CONTAINER_H