UITabContainer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_widgets.h>
  23. #include <TurboBadger/tb_widgets_common.h>
  24. #include <TurboBadger/tb_tab_container.h>
  25. #include "UI.h"
  26. #include "UIEvents.h"
  27. #include "UITabContainer.h"
  28. #include "UILayout.h"
  29. #include "UIButton.h"
  30. using namespace tb;
  31. namespace Atomic
  32. {
  33. UITabContainer::UITabContainer(Context* context, bool createWidget) : UIWidget(context, false)
  34. {
  35. if (createWidget)
  36. {
  37. widget_ = new TBTabContainer();
  38. widget_->SetDelegate(this);
  39. GetSubsystem<UI>()->WrapWidget(this, widget_);
  40. }
  41. }
  42. UITabContainer::~UITabContainer()
  43. {
  44. }
  45. UIWidget* UITabContainer::GetCurrentPageWidget()
  46. {
  47. if (!widget_)
  48. return 0;
  49. TBWidget* w = ((TBTabContainer*)widget_)->GetCurrentPageWidget();
  50. return GetSubsystem<UI>()->WrapWidget(w);
  51. }
  52. int UITabContainer::GetNumPages()
  53. {
  54. if (!widget_)
  55. return 0;
  56. return ((TBTabContainer*)widget_)->GetNumPages();
  57. }
  58. UILayout* UITabContainer::GetTabLayout()
  59. {
  60. if (!widget_)
  61. return 0;
  62. TBLayout* layout = ((TBTabContainer*)widget_)->GetTabLayout();
  63. if (!layout)
  64. return 0;
  65. UI* ui = GetSubsystem<UI>();
  66. return (UILayout*) ui->WrapWidget(layout);
  67. }
  68. void UITabContainer::SetCurrentPage(int page)
  69. {
  70. if (!widget_)
  71. return;
  72. ((TBTabContainer*)widget_)->SetCurrentPage(page);
  73. }
  74. bool UITabContainer::OnEvent(const tb::TBWidgetEvent &ev)
  75. {
  76. return UIWidget::OnEvent(ev);
  77. }
  78. /// returns the current page number
  79. int UITabContainer::GetCurrentPage()
  80. {
  81. if (!widget_)
  82. return 0;
  83. return ((TBTabContainer*)widget_)->GetCurrentPage();
  84. }
  85. /// deletes a tab + page, returns true if successful
  86. bool UITabContainer::DeletePage( int page )
  87. {
  88. if (!widget_ || page < 0 || page > GetNumPages() - 1)
  89. return false;
  90. UILayout *uil = GetTabLayout();
  91. if (uil)
  92. {
  93. UIWidget* mytab = NULL;
  94. int nn=0;
  95. for (UIWidget *child = uil->GetFirstChild(); child; child = child->GetNext())
  96. if (nn++ == page)
  97. mytab = child;
  98. if (mytab)
  99. {
  100. mytab->UnsubscribeFromAllEvents();
  101. uil->RemoveChild( mytab, true );
  102. }
  103. }
  104. UIWidget *pages = GetContentRoot();
  105. if (pages)
  106. {
  107. UIWidget* mypage = NULL;
  108. int nn=0;
  109. for (UIWidget *child = pages->GetFirstChild(); child; child = child->GetNext())
  110. if (nn++ == page)
  111. mypage = child;
  112. if (mypage)
  113. {
  114. mypage->UnsubscribeFromAllEvents();
  115. pages->RemoveChild( mypage, true );
  116. }
  117. }
  118. Invalidate();
  119. // tab container "feature", can not set it to the page number that was removed.
  120. int num = 0;
  121. if ( page - 1 > 0 ) num = page - 1;
  122. SetCurrentPage(num);
  123. return true;
  124. }
  125. /// adds a tab + page from layout file
  126. void UITabContainer::AddTabPageFile ( const String &tabname, const String &layoutFile, bool selecttab )
  127. {
  128. UIButton* button = new UIButton(context_);
  129. button->SetText(tabname);
  130. button->SetId(tabname);
  131. UILayout *uil = GetTabLayout();
  132. if (uil && button)
  133. uil->AddChild(button);
  134. UILayout* layout = new UILayout(context_);
  135. layout->SetAxis(UI_AXIS_Y);
  136. layout->SetLayoutSize(UI_LAYOUT_SIZE_AVAILABLE);
  137. layout->SetLayoutPosition(UI_LAYOUT_POSITION_GRAVITY);
  138. layout->SetLayoutDistribution(UI_LAYOUT_DISTRIBUTION_AVAILABLE);
  139. layout->Load (layoutFile);
  140. UIWidget *pages = GetContentRoot();
  141. if (pages && layout)
  142. pages->AddChild(layout);
  143. Invalidate();
  144. if (selecttab)
  145. SetCurrentPage( GetNumPages() -1 );
  146. }
  147. /// adds a tab + page widget(s)
  148. void UITabContainer::AddTabPageWidget ( const String &tabname, UIWidget *widget, bool selecttab )
  149. {
  150. UIButton* button = new UIButton(context_);
  151. button->SetText(tabname);
  152. button->SetId(tabname);
  153. UILayout *uil = GetTabLayout();
  154. if (uil && button)
  155. uil->AddChild(button);
  156. UILayout* layout = new UILayout(context_);
  157. layout->SetAxis(UI_AXIS_Y);
  158. layout->SetLayoutSize(UI_LAYOUT_SIZE_AVAILABLE);
  159. layout->SetLayoutPosition(UI_LAYOUT_POSITION_GRAVITY);
  160. layout->SetLayoutDistribution(UI_LAYOUT_DISTRIBUTION_AVAILABLE);
  161. layout->AddChild(widget);
  162. UIWidget *pages = GetContentRoot();
  163. if (pages && layout)
  164. pages->AddChild(layout);
  165. Invalidate();
  166. if (selecttab)
  167. SetCurrentPage( GetNumPages() -1 );
  168. }
  169. /// undocks the page into a window with the tab name, and removes the tab
  170. void UITabContainer::UndockPage ( int page )
  171. {
  172. if (!widget_)
  173. return;
  174. ((TBTabContainer*)widget_)->UndockPage(page);
  175. // tab container "feature", can not set it to the page number that was removed.
  176. int num = 0;
  177. if ( page - 1 > 0 ) num = page - 1;
  178. SetCurrentPage(num);
  179. }
  180. /// docks content from a UIDockWindow with specified title
  181. bool UITabContainer::DockWindow ( String windowTitle )
  182. {
  183. if (!widget_)
  184. return false;
  185. bool done = ((TBTabContainer*)widget_)->DockFromWindow(windowTitle.CString());
  186. if (done)
  187. SetCurrentPage( GetNumPages() -1 );
  188. return done;
  189. }
  190. }