tb_toggle_container.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "tb_toggle_container.h"
  6. #include "tb_widgets_reader.h"
  7. #include "tb_node_tree.h"
  8. namespace tb {
  9. // == TBSectionHeader =====================================
  10. TBSectionHeader::TBSectionHeader()
  11. {
  12. SetSkinBg(TBIDC("TBSectionHeader"));
  13. SetGravity(WIDGET_GRAVITY_LEFT | WIDGET_GRAVITY_RIGHT);
  14. SetToggleMode(true);
  15. }
  16. bool TBSectionHeader::OnEvent(const TBWidgetEvent &ev)
  17. {
  18. if (ev.target == this && ev.type == EVENT_TYPE_CHANGED && GetParent()->GetParent())
  19. {
  20. if (TBSection *section = TBSafeCast<TBSection>(GetParent()->GetParent()))
  21. {
  22. section->GetContainer()->SetValue(GetValue());
  23. // Try to scroll the container into view when expanded
  24. section->SetPendingScrollIntoView(GetValue() ? true : false);
  25. }
  26. }
  27. return TBButton::OnEvent(ev);
  28. }
  29. // == TBSectionHeader =====================================
  30. TBSection::TBSection()
  31. : m_pending_scroll(false)
  32. {
  33. SetGravity(WIDGET_GRAVITY_LEFT | WIDGET_GRAVITY_RIGHT);
  34. SetSkinBg(TBIDC("TBSection"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  35. m_layout.SetSkinBg(TBIDC("TBSection.layout"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  36. m_toggle_container.SetSkinBg(TBIDC("TBSection.container"));
  37. m_toggle_container.SetToggle(TBToggleContainer::TOGGLE_EXPANDED);
  38. m_toggle_container.SetGravity(WIDGET_GRAVITY_ALL);
  39. m_layout.SetAxis(AXIS_Y);
  40. m_layout.SetGravity(WIDGET_GRAVITY_ALL);
  41. m_layout.SetLayoutSize(LAYOUT_SIZE_AVAILABLE);
  42. AddChild(&m_layout);
  43. m_layout.AddChild(&m_header);
  44. m_layout.AddChild(&m_toggle_container);
  45. }
  46. TBSection::~TBSection()
  47. {
  48. m_layout.RemoveChild(&m_toggle_container);
  49. m_layout.RemoveChild(&m_header);
  50. RemoveChild(&m_layout);
  51. }
  52. void TBSection::SetValue(int value)
  53. {
  54. m_header.SetValue(value);
  55. m_toggle_container.SetValue(value);
  56. }
  57. void TBSection::OnProcessAfterChildren()
  58. {
  59. if (m_pending_scroll)
  60. {
  61. m_pending_scroll = false;
  62. ScrollIntoViewRecursive();
  63. }
  64. }
  65. PreferredSize TBSection::OnCalculatePreferredSize(const SizeConstraints &constraints)
  66. {
  67. PreferredSize ps = TBWidget::OnCalculatePreferredContentSize(constraints);
  68. // We should not grow larger than we are, when there's extra space available.
  69. ps.max_h = ps.pref_h;
  70. return ps;
  71. }
  72. // == TBToggleContainer ===================================
  73. TBToggleContainer::TBToggleContainer()
  74. : m_toggle(TOGGLE_NOTHING)
  75. , m_invert(false)
  76. , m_value(0)
  77. {
  78. SetSkinBg(TBIDC("TBToggleContainer"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  79. }
  80. void TBToggleContainer::SetToggle(TOGGLE toggle)
  81. {
  82. if (toggle == m_toggle)
  83. return;
  84. if (m_toggle == TOGGLE_EXPANDED)
  85. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  86. m_toggle = toggle;
  87. UpdateInternal();
  88. }
  89. void TBToggleContainer::SetInvert(bool invert)
  90. {
  91. if (invert == m_invert)
  92. return;
  93. m_invert = invert;
  94. UpdateInternal();
  95. }
  96. void TBToggleContainer::SetValue(int value)
  97. {
  98. if (value == m_value)
  99. return;
  100. m_value = value;
  101. UpdateInternal();
  102. InvalidateSkinStates();
  103. }
  104. void TBToggleContainer::UpdateInternal()
  105. {
  106. bool on = GetIsOn();
  107. switch (m_toggle)
  108. {
  109. case TOGGLE_NOTHING:
  110. break;
  111. case TOGGLE_ENABLED:
  112. SetState(WIDGET_STATE_DISABLED, !on);
  113. break;
  114. case TOGGLE_OPACITY:
  115. SetOpacity(on ? 1.f : 0);
  116. break;
  117. case TOGGLE_EXPANDED:
  118. SetVisibilility(on ? WIDGET_VISIBILITY_VISIBLE : WIDGET_VISIBILITY_GONE);
  119. // Also disable when collapsed so tab focus skips the children.
  120. SetState(WIDGET_STATE_DISABLED, !on);
  121. break;
  122. };
  123. }
  124. }; // namespace tb