tb_tab_container.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // ATOMIC BEGIN
  8. #include "tb_node_tree.h"
  9. #include "tb_widgets_reader.h"
  10. #include "tb_atomic_widgets.h"
  11. // ATOMIC END
  12. namespace tb {
  13. // == TBTabLayout =======================================================================
  14. void TBTabLayout::OnChildAdded(TBWidget *child)
  15. {
  16. if (TBButton *button = TBSafeCast<TBButton>(child))
  17. {
  18. button->SetSqueezable(true);
  19. button->SetSkinBg(TBIDC("TBTabContainer.tab"));
  20. button->SetID(TBIDC("tab"));
  21. }
  22. }
  23. void TBTabLayout::OnChildRemove(TBWidget *child)
  24. {
  25. int index = GetIndexFromChild(child);
  26. if (index < tabContainer_->m_current_page)
  27. --tabContainer_->m_current_page;
  28. }
  29. PreferredSize TBTabLayout::OnCalculatePreferredContentSize(const SizeConstraints &constraints)
  30. {
  31. PreferredSize ps = TBLayout::OnCalculatePreferredContentSize(constraints);
  32. // Make sure the number of tabs doesn't grow parents.
  33. // It is only the content that should do that. The tabs
  34. // will scroll anyway.
  35. if (GetAxis() == AXIS_X)
  36. ps.min_w = MIN(ps.min_w, 1);
  37. else
  38. ps.min_h = MIN(ps.min_h, 1);
  39. return ps;
  40. }
  41. // == TBTabContainer ====================================================================
  42. TBTabContainer::TBTabContainer()
  43. : m_need_page_update(true)
  44. , m_current_page(-1)
  45. , m_align(TB_ALIGN_TOP)
  46. , m_tab_layout(this)
  47. {
  48. AddChild(&m_root_layout);
  49. // Put the tab layout on top of the content in Z order so their skin can make
  50. // a seamless overlap over the border. Control which side they are layouted
  51. // to by calling SetLayoutOrder.
  52. m_root_layout.AddChild(&m_content_root);
  53. m_root_layout.AddChild(&m_tab_layout);
  54. m_root_layout.SetAxis(AXIS_Y);
  55. m_root_layout.SetGravity(WIDGET_GRAVITY_ALL);
  56. m_root_layout.SetLayoutDistribution(LAYOUT_DISTRIBUTION_AVAILABLE);
  57. m_root_layout.SetLayoutOrder(LAYOUT_ORDER_TOP_TO_BOTTOM);
  58. m_root_layout.SetSkinBg(TBIDC("TBTabContainer.rootlayout"));
  59. m_root_layout.SetID("root_layout");
  60. m_tab_layout.SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_CENTER);
  61. m_tab_layout.SetSkinBg(TBIDC("TBTabContainer.tablayout_x"));
  62. m_tab_layout.SetLayoutPosition(LAYOUT_POSITION_RIGHT_BOTTOM);
  63. m_tab_layout.SetID("tab_layout");
  64. m_content_root.SetGravity(WIDGET_GRAVITY_ALL);
  65. m_content_root.SetSkinBg(TBIDC("TBTabContainer.container"));
  66. m_content_root.SetID("content_root");
  67. }
  68. TBTabContainer::~TBTabContainer()
  69. {
  70. m_root_layout.RemoveChild(&m_content_root);
  71. m_root_layout.RemoveChild(&m_tab_layout);
  72. RemoveChild(&m_root_layout);
  73. }
  74. void TBTabContainer::SetAxis(AXIS axis)
  75. {
  76. m_root_layout.SetAxis(axis);
  77. m_tab_layout.SetAxis(axis == AXIS_X ? AXIS_Y : AXIS_X);
  78. m_tab_layout.SetSkinBg(axis == AXIS_X ? TBIDC("TBTabContainer.tablayout_y") :
  79. TBIDC("TBTabContainer.tablayout_x"));
  80. }
  81. void TBTabContainer::SetValue(int index)
  82. {
  83. if (index == m_current_page || !GetNumPages())
  84. return;
  85. m_current_page = index;
  86. // Update the pages visibility and tabs pressed value.
  87. index = 0;
  88. TBWidget *page = m_content_root.GetFirstChild();
  89. TBWidget *tab = m_tab_layout.GetFirstChild();
  90. for ( ; page && tab; page = page->GetNext(), tab = tab->GetNext(), index++)
  91. {
  92. bool active = index == m_current_page;
  93. page->SetVisibilility(active ? WIDGET_VISIBILITY_VISIBLE : WIDGET_VISIBILITY_INVISIBLE);
  94. tab->SetValue(active ? 1 : 0);
  95. if (active)
  96. {
  97. if (page->GetVisibility() == WIDGET_VISIBILITY_GONE)
  98. continue;
  99. TBRect contentRect = m_content_root.GetRect();
  100. TBSkinElement* skin = m_content_root.GetSkinBgElement();
  101. contentRect = contentRect.Shrink(skin->padding_left, skin->padding_top, skin->padding_right, skin->padding_bottom);
  102. TBRect pageRect = page->GetRect();
  103. if (contentRect.w != pageRect.w || contentRect.h != pageRect.h)
  104. {
  105. contentRect.x = skin->padding_left;
  106. contentRect.y = skin->padding_right;
  107. page->SetRect(contentRect);
  108. }
  109. }
  110. }
  111. TBWidgetEvent ev(EVENT_TYPE_TAB_CHANGED);
  112. InvokeEvent(ev);
  113. }
  114. int TBTabContainer::GetNumPages()
  115. {
  116. int count = 0;
  117. for (TBWidget *tab = m_tab_layout.GetFirstChild(); tab; tab = tab->GetNext())
  118. count++;
  119. if (!count)
  120. m_current_page = -1;
  121. return count;
  122. }
  123. TBWidget *TBTabContainer::GetCurrentPageWidget() const
  124. {
  125. if (m_current_page == -1)
  126. return nullptr;
  127. return m_content_root.GetChildFromIndex(m_current_page);
  128. }
  129. void TBTabContainer::SetAlignment(TB_ALIGN align)
  130. {
  131. bool horizontal = (align == TB_ALIGN_TOP || align == TB_ALIGN_BOTTOM);
  132. bool reverse = (align == TB_ALIGN_TOP || align == TB_ALIGN_LEFT);
  133. SetAxis(horizontal ? AXIS_Y : AXIS_X);
  134. m_root_layout.SetLayoutOrder(reverse ? LAYOUT_ORDER_TOP_TO_BOTTOM : LAYOUT_ORDER_BOTTOM_TO_TOP);
  135. m_tab_layout.SetLayoutPosition(reverse ? LAYOUT_POSITION_RIGHT_BOTTOM : LAYOUT_POSITION_LEFT_TOP);
  136. m_align = align;
  137. }
  138. bool TBTabContainer::OnEvent(const TBWidgetEvent &ev)
  139. {
  140. if ((ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN) &&
  141. ev.target->GetID() == TBIDC("tab") &&
  142. ev.target->GetParent() == &m_tab_layout)
  143. {
  144. int clicked_index = m_tab_layout.GetIndexFromChild(ev.target);
  145. SetValue(clicked_index);
  146. return true;
  147. }
  148. return false;
  149. }
  150. void TBTabContainer::OnProcess()
  151. {
  152. if (m_need_page_update)
  153. {
  154. m_need_page_update = false;
  155. // Force update value
  156. int current_page = m_current_page;
  157. m_current_page = -1;
  158. SetValue(current_page);
  159. }
  160. }
  161. // ATOMIC BEGIN
  162. /// takes the contents of a DockWindow into a tab in a tabcontainer
  163. bool TBTabContainer::DockFromWindow ( TBStr windowTitle )
  164. {
  165. int nn = 0;
  166. TBLinkListOf<TBWidget>::Iterator mx = GetParentRoot(true)->GetIteratorForward();
  167. while (TBWidget *mxw = mx.GetAndStep())
  168. {
  169. if ( mxw->GetText().Equals(windowTitle) )
  170. {
  171. TBDockWindow *dw1 = TBSafeCast<TBDockWindow>( mxw );
  172. if (dw1)
  173. dw1->Dock ( this );
  174. return true;
  175. }
  176. nn++;
  177. }
  178. return false;
  179. }
  180. /// undocks the page into a window with the tab name, and removes the tab
  181. void TBTabContainer::UndockPage ( int page )
  182. {
  183. TBWidget *mytab = GetTabLayout()->GetChildFromIndex(page); // find the offending tab
  184. TBWidget *mypage = GetContentRoot()->GetChildFromIndex(page); // find the offending page (layout)
  185. if (mytab == NULL || mypage == NULL ) return; // nobody home
  186. TBStr tabstr = mytab->GetText(); //get the name/text of the tab
  187. GetTabLayout()->RemoveChild(mytab); // remove, delete the tab[page] button
  188. mytab->Die(); // and get rid of it
  189. GetContentRoot()->RemoveChild(mypage); // remove the pagewidget[page] from the content root (without deleting it, hopefully)
  190. TBDockWindow *mywindow = new TBDockWindow(tabstr, mypage); // create an undock window.
  191. mywindow->SetDockOrigin( GetID() ); //tell it to redock here
  192. mywindow->Show(GetParentRoot(true));
  193. Invalidate(); // and tell everyone the party is over.
  194. }
  195. // ATOMIC END
  196. }; // namespace tb