tb_tab_container.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_tab_container.h"
  6. #include <assert.h>
  7. namespace tb {
  8. // == TBTabLayout =======================================================================
  9. void TBTabLayout::OnChildAdded(TBWidget *child)
  10. {
  11. if (TBButton *button = TBSafeCast<TBButton>(child))
  12. {
  13. button->SetSqueezable(true);
  14. button->SetSkinBg(TBIDC("TBTabContainer.tab"));
  15. button->SetID(TBIDC("tab"));
  16. }
  17. }
  18. PreferredSize TBTabLayout::OnCalculatePreferredContentSize(const SizeConstraints &constraints)
  19. {
  20. PreferredSize ps = TBLayout::OnCalculatePreferredContentSize(constraints);
  21. // Make sure the number of tabs doesn't grow parents.
  22. // It is only the content that should do that. The tabs
  23. // will scroll anyway.
  24. if (GetAxis() == AXIS_X)
  25. ps.min_w = MIN(ps.min_w, 1);
  26. else
  27. ps.min_h = MIN(ps.min_h, 1);
  28. return ps;
  29. }
  30. // == TBTabContainer ====================================================================
  31. TBTabContainer::TBTabContainer()
  32. : m_need_page_update(true)
  33. , m_current_page(-1)
  34. , m_align(TB_ALIGN_TOP)
  35. {
  36. AddChild(&m_root_layout);
  37. // Put the tab layout on top of the content in Z order so their skin can make
  38. // a seamless overlap over the border. Control which side they are layouted
  39. // to by calling SetLayoutOrder.
  40. m_root_layout.AddChild(&m_content_root);
  41. m_root_layout.AddChild(&m_tab_layout);
  42. m_root_layout.SetAxis(AXIS_Y);
  43. m_root_layout.SetGravity(WIDGET_GRAVITY_ALL);
  44. m_root_layout.SetLayoutDistribution(LAYOUT_DISTRIBUTION_AVAILABLE);
  45. m_root_layout.SetLayoutOrder(LAYOUT_ORDER_TOP_TO_BOTTOM);
  46. m_root_layout.SetSkinBg(TBIDC("TBTabContainer.rootlayout"));
  47. m_root_layout.SetID("root_layout");
  48. m_tab_layout.SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_CENTER);
  49. m_tab_layout.SetSkinBg(TBIDC("TBTabContainer.tablayout_x"));
  50. m_tab_layout.SetLayoutPosition(LAYOUT_POSITION_RIGHT_BOTTOM);
  51. m_tab_layout.SetID("tab_layout");
  52. m_content_root.SetGravity(WIDGET_GRAVITY_ALL);
  53. m_content_root.SetSkinBg(TBIDC("TBTabContainer.container"));
  54. m_content_root.SetID("content_root");
  55. }
  56. TBTabContainer::~TBTabContainer()
  57. {
  58. m_root_layout.RemoveChild(&m_content_root);
  59. m_root_layout.RemoveChild(&m_tab_layout);
  60. RemoveChild(&m_root_layout);
  61. }
  62. void TBTabContainer::SetAxis(AXIS axis)
  63. {
  64. m_root_layout.SetAxis(axis);
  65. m_tab_layout.SetAxis(axis == AXIS_X ? AXIS_Y : AXIS_X);
  66. m_tab_layout.SetSkinBg(axis == AXIS_X ? TBIDC("TBTabContainer.tablayout_y") :
  67. TBIDC("TBTabContainer.tablayout_x"));
  68. }
  69. void TBTabContainer::SetValue(int index)
  70. {
  71. if (index == m_current_page || !GetNumPages())
  72. return;
  73. m_current_page = index;
  74. // Update the pages visibility and tabs pressed value.
  75. index = 0;
  76. TBWidget *page = m_content_root.GetFirstChild();
  77. TBWidget *tab = m_tab_layout.GetFirstChild();
  78. for ( ; page && tab; page = page->GetNext(), tab = tab->GetNext(), index++)
  79. {
  80. bool active = index == m_current_page;
  81. page->SetVisibilility(active ? WIDGET_VISIBILITY_VISIBLE : WIDGET_VISIBILITY_INVISIBLE);
  82. tab->SetValue(active ? 1 : 0);
  83. if (active)
  84. {
  85. if (page->GetVisibility() == WIDGET_VISIBILITY_GONE)
  86. continue;
  87. TBRect contentRect = m_content_root.GetRect();
  88. TBSkinElement* skin = m_content_root.GetSkinBgElement();
  89. contentRect = contentRect.Shrink(skin->padding_left, skin->padding_top, skin->padding_right, skin->padding_bottom);
  90. TBRect pageRect = page->GetRect();
  91. if (contentRect.w != pageRect.w || contentRect.h != pageRect.h)
  92. {
  93. contentRect.x = skin->padding_left;
  94. contentRect.y = skin->padding_right;
  95. page->SetRect(contentRect);
  96. }
  97. }
  98. }
  99. TBWidgetEvent ev(EVENT_TYPE_TAB_CHANGED);
  100. InvokeEvent(ev);
  101. }
  102. int TBTabContainer::GetNumPages()
  103. {
  104. int count = 0;
  105. for (TBWidget *tab = m_tab_layout.GetFirstChild(); tab; tab = tab->GetNext())
  106. count++;
  107. if (!count)
  108. m_current_page = -1;
  109. return count;
  110. }
  111. TBWidget *TBTabContainer::GetCurrentPageWidget() const
  112. {
  113. if (m_current_page == -1)
  114. return nullptr;
  115. return m_content_root.GetChildFromIndex(m_current_page);
  116. }
  117. void TBTabContainer::SetAlignment(TB_ALIGN align)
  118. {
  119. bool horizontal = (align == TB_ALIGN_TOP || align == TB_ALIGN_BOTTOM);
  120. bool reverse = (align == TB_ALIGN_TOP || align == TB_ALIGN_LEFT);
  121. SetAxis(horizontal ? AXIS_Y : AXIS_X);
  122. m_root_layout.SetLayoutOrder(reverse ? LAYOUT_ORDER_TOP_TO_BOTTOM : LAYOUT_ORDER_BOTTOM_TO_TOP);
  123. m_tab_layout.SetLayoutPosition(reverse ? LAYOUT_POSITION_RIGHT_BOTTOM : LAYOUT_POSITION_LEFT_TOP);
  124. m_align = align;
  125. }
  126. bool TBTabContainer::OnEvent(const TBWidgetEvent &ev)
  127. {
  128. if ((ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN) &&
  129. ev.target->GetID() == TBIDC("tab") &&
  130. ev.target->GetParent() == &m_tab_layout)
  131. {
  132. int clicked_index = m_tab_layout.GetIndexFromChild(ev.target);
  133. SetValue(clicked_index);
  134. return true;
  135. }
  136. return false;
  137. }
  138. void TBTabContainer::OnProcess()
  139. {
  140. if (m_need_page_update)
  141. {
  142. m_need_page_update = false;
  143. // Force update value
  144. int current_page = m_current_page;
  145. m_current_page = -1;
  146. SetValue(current_page);
  147. }
  148. }
  149. }; // namespace tb