tb_window.cpp 7.4 KB

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