tb_window.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_window.h"
  6. #include <assert.h>
  7. namespace tb {
  8. // == TBWindow ==========================================================================
  9. TBWindow::TBWindow()
  10. : m_settings(WINDOW_SETTINGS_DEFAULT)
  11. {
  12. SetSkinBg(TBIDC("TBWindow"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  13. AddChild(&m_mover);
  14. AddChild(&m_resizer);
  15. m_mover.SetSkinBg(TBIDC("TBWindow.mover"));
  16. m_mover.AddChild(&m_textfield);
  17. m_textfield.SetIgnoreInput(true);
  18. m_mover.AddChild(&m_close_button);
  19. m_close_button.SetSkinBg(TBIDC("TBWindow.close"));
  20. m_close_button.SetIsFocusable(false);
  21. m_close_button.SetID(TBIDC("TBWindow.close"));
  22. SetIsGroupRoot(true);
  23. }
  24. TBWindow::~TBWindow()
  25. {
  26. if (m_resizer.GetParent()) RemoveChild(&m_resizer);
  27. if (m_mover.GetParent()) RemoveChild(&m_mover);
  28. if (m_close_button.GetParent()) m_mover.RemoveChild(&m_close_button);
  29. m_mover.RemoveChild(&m_textfield);
  30. }
  31. TBRect TBWindow::GetResizeToFitContentRect(RESIZE_FIT fit)
  32. {
  33. PreferredSize ps = GetPreferredSize();
  34. int new_w = ps.pref_w;
  35. int new_h = ps.pref_h;
  36. if (fit == RESIZE_FIT_MINIMAL)
  37. {
  38. new_w = ps.min_w;
  39. new_h = ps.min_h;
  40. }
  41. else if (fit == RESIZE_FIT_CURRENT_OR_NEEDED)
  42. {
  43. new_w = CLAMP(GetRect().w, ps.min_w, ps.max_w);
  44. new_h = CLAMP(GetRect().h, ps.min_h, ps.max_h);
  45. }
  46. if (GetParent())
  47. {
  48. new_w = MIN(new_w, GetParent()->GetRect().w);
  49. new_h = MIN(new_h, GetParent()->GetRect().h);
  50. }
  51. return TBRect(GetRect().x, GetRect().y, new_w, new_h);
  52. }
  53. void TBWindow::ResizeToFitContent(RESIZE_FIT fit)
  54. {
  55. SetRect(GetResizeToFitContentRect(fit));
  56. }
  57. void TBWindow::Close()
  58. {
  59. Die();
  60. }
  61. bool TBWindow::IsActive() const
  62. {
  63. return GetState(WIDGET_STATE_SELECTED);
  64. }
  65. TBWindow *TBWindow::GetTopMostOtherWindow(bool only_activable_windows)
  66. {
  67. TBWindow *other_window = nullptr;
  68. TBWidget *sibling = GetParent()->GetLastChild();
  69. while (sibling && !other_window)
  70. {
  71. if (sibling != this)
  72. other_window = TBSafeCast<TBWindow>(sibling);
  73. if (only_activable_windows && other_window && !(other_window->m_settings & WINDOW_SETTINGS_CAN_ACTIVATE))
  74. other_window = nullptr;
  75. sibling = sibling->GetPrev();
  76. }
  77. return other_window;
  78. }
  79. void TBWindow::Activate()
  80. {
  81. if (!GetParent() || !(m_settings & WINDOW_SETTINGS_CAN_ACTIVATE))
  82. return;
  83. if (IsActive())
  84. {
  85. // Already active, but we may still have lost focus,
  86. // so ensure it comes back to us.
  87. EnsureFocus();
  88. return;
  89. }
  90. // Deactivate currently active window
  91. TBWindow *active_window = GetTopMostOtherWindow(true);
  92. if (active_window)
  93. active_window->DeActivate();
  94. // Activate this window
  95. SetZ(WIDGET_Z_TOP);
  96. SetWindowActiveState(true);
  97. EnsureFocus();
  98. }
  99. bool TBWindow::EnsureFocus()
  100. {
  101. // If we already have focus, we're done.
  102. if (focused_widget && IsAncestorOf(focused_widget))
  103. return true;
  104. // Focus last focused widget (if we have one)
  105. bool success = false;
  106. if (m_last_focus.Get())
  107. success = m_last_focus.Get()->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  108. // We didn't have one or failed, so try focus any child.
  109. if (!success)
  110. success = SetFocusRecursive(WIDGET_FOCUS_REASON_UNKNOWN);
  111. return success;
  112. }
  113. void TBWindow::DeActivate()
  114. {
  115. if (!IsActive())
  116. return;
  117. SetWindowActiveState(false);
  118. }
  119. void TBWindow::SetWindowActiveState(bool active)
  120. {
  121. SetState(WIDGET_STATE_SELECTED, active);
  122. m_mover.SetState(WIDGET_STATE_SELECTED, active);
  123. }
  124. void TBWindow::SetSettings(WINDOW_SETTINGS settings)
  125. {
  126. if (settings == m_settings)
  127. return;
  128. m_settings = settings;
  129. if (settings & WINDOW_SETTINGS_TITLEBAR)
  130. {
  131. if (!m_mover.GetParent())
  132. AddChild(&m_mover);
  133. }
  134. else if (!(settings & WINDOW_SETTINGS_TITLEBAR))
  135. {
  136. if (m_mover.GetParent())
  137. RemoveChild(&m_mover);
  138. }
  139. if (settings & WINDOW_SETTINGS_RESIZABLE)
  140. {
  141. if (!m_resizer.GetParent())
  142. AddChild(&m_resizer);
  143. }
  144. else if (!(settings & WINDOW_SETTINGS_RESIZABLE))
  145. {
  146. if (m_resizer.GetParent())
  147. RemoveChild(&m_resizer);
  148. }
  149. if (settings & WINDOW_SETTINGS_CLOSE_BUTTON)
  150. {
  151. if (!m_close_button.GetParent())
  152. m_mover.AddChild(&m_close_button);
  153. }
  154. else if (!(settings & WINDOW_SETTINGS_CLOSE_BUTTON))
  155. {
  156. if (m_close_button.GetParent())
  157. m_mover.RemoveChild(&m_close_button);
  158. }
  159. // FIX: invalidate layout / resize window!
  160. Invalidate();
  161. }
  162. int TBWindow::GetTitleHeight()
  163. {
  164. if (m_settings & WINDOW_SETTINGS_TITLEBAR)
  165. return m_mover.GetPreferredSize().pref_h;
  166. return 0;
  167. }
  168. TBRect TBWindow::GetPaddingRect()
  169. {
  170. TBRect padding_rect = TBWidget::GetPaddingRect();
  171. int title_height = GetTitleHeight();
  172. padding_rect.y += title_height;
  173. padding_rect.h -= title_height;
  174. return padding_rect;
  175. }
  176. PreferredSize TBWindow::OnCalculatePreferredSize(const SizeConstraints &constraints)
  177. {
  178. PreferredSize ps = OnCalculatePreferredContentSize(constraints);
  179. // Add window skin padding
  180. if (TBSkinElement *e = GetSkinBgElement())
  181. {
  182. ps.min_w += e->padding_left + e->padding_right;
  183. ps.pref_w += e->padding_left + e->padding_right;
  184. ps.min_h += e->padding_top + e->padding_bottom;
  185. ps.pref_h += e->padding_top + e->padding_bottom;
  186. }
  187. // Add window title bar height
  188. int title_height = GetTitleHeight();
  189. ps.min_h += title_height;
  190. ps.pref_h += title_height;
  191. return ps;
  192. }
  193. bool TBWindow::OnEvent(const TBWidgetEvent &ev)
  194. {
  195. if (ev.target == &m_close_button)
  196. {
  197. if (ev.type == EVENT_TYPE_CLICK)
  198. Close();
  199. return true;
  200. }
  201. return TBWidget::OnEvent(ev);
  202. }
  203. void TBWindow::OnAdded()
  204. {
  205. // If we was added last, call Activate to update status etc.
  206. if (GetParent()->GetLastChild() == this)
  207. Activate();
  208. }
  209. void TBWindow::OnRemove()
  210. {
  211. DeActivate();
  212. // Active the top most other window
  213. if (TBWindow *active_window = GetTopMostOtherWindow(true))
  214. active_window->Activate();
  215. }
  216. void TBWindow::OnChildAdded(TBWidget *child)
  217. {
  218. m_resizer.SetZ(WIDGET_Z_TOP);
  219. }
  220. void TBWindow::OnResized(int old_w, int old_h)
  221. {
  222. // Apply gravity on children
  223. TBWidget::OnResized(old_w, old_h);
  224. // Manually move our own decoration children
  225. // FIX: Put a layout in the TBMover so we can add things there nicely.
  226. int title_height = GetTitleHeight();
  227. m_mover.SetRect(TBRect(0, 0, GetRect().w, title_height));
  228. PreferredSize ps = m_resizer.GetPreferredSize();
  229. m_resizer.SetRect(TBRect(GetRect().w - ps.pref_w, GetRect().h - ps.pref_h, ps.pref_w, ps.pref_h));
  230. TBRect mover_rect = m_mover.GetPaddingRect();
  231. int button_size = mover_rect.h;
  232. m_close_button.SetRect(TBRect(mover_rect.x + mover_rect.w - button_size, mover_rect.y, button_size, button_size));
  233. if (m_settings & WINDOW_SETTINGS_CLOSE_BUTTON)
  234. mover_rect.w -= button_size;
  235. m_textfield.SetRect(mover_rect);
  236. }
  237. }; // namespace tb