tb_widgets.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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_widgets.h"
  6. #include "tb_window.h"
  7. #include "tb_widgets_listener.h"
  8. #include "tb_renderer.h"
  9. #include "tb_widgets_common.h"
  10. #include "tb_widget_skin_condition_context.h"
  11. #include "tb_system.h"
  12. #include "tb_scroller.h"
  13. #include "tb_font_renderer.h"
  14. #include <assert.h>
  15. #ifdef TB_ALWAYS_SHOW_EDIT_FOCUS
  16. #include "tb_editfield.h"
  17. #endif // TB_ALWAYS_SHOW_EDIT_FOCUS
  18. namespace tb {
  19. //static data
  20. TBWidget *TBWidget::hovered_widget = nullptr;
  21. TBWidget *TBWidget::captured_widget = nullptr;
  22. TBWidget *TBWidget::focused_widget = nullptr;
  23. int TBWidget::pointer_down_widget_x = 0;
  24. int TBWidget::pointer_down_widget_y = 0;
  25. int TBWidget::pointer_move_widget_x = 0;
  26. int TBWidget::pointer_move_widget_y = 0;
  27. bool TBWidget::cancel_click = false;
  28. bool TBWidget::update_widget_states = true;
  29. bool TBWidget::update_skin_states = true;
  30. bool TBWidget::show_focus_state = false;
  31. // == TBLongClickTimer ==================================================================
  32. /** One shot timer for long click event */
  33. class TBLongClickTimer : private TBMessageHandler
  34. {
  35. public:
  36. TBLongClickTimer(TBWidget *widget, bool touch) : m_widget(widget), m_touch(touch)
  37. {
  38. PostMessageDelayed(TBIDC("TBLongClickTimer"), nullptr, TBSystem::GetLongClickDelayMS());
  39. }
  40. virtual void OnMessageReceived(TBMessage *msg)
  41. {
  42. assert(msg->message == TBIDC("TBLongClickTimer"));
  43. m_widget->MaybeInvokeLongClickOrContextMenu(m_touch);
  44. }
  45. private:
  46. TBWidget *m_widget;
  47. bool m_touch;
  48. };
  49. // == TBWidget::PaintProps ==============================================================
  50. TBWidget::PaintProps::PaintProps()
  51. {
  52. // Set the default properties, used for the root widgets
  53. // calling InvokePaint. The base values for all inheritance.
  54. text_color = g_tb_skin->GetDefaultTextColor();
  55. }
  56. // == TBWidget ==========================================================================
  57. TBWidget::TBWidget()
  58. : m_parent(nullptr)
  59. , m_opacity(1.f)
  60. , m_state(WIDGET_STATE_NONE)
  61. , m_gravity(WIDGET_GRAVITY_DEFAULT)
  62. , m_layout_params(nullptr)
  63. , m_scroller(nullptr)
  64. , m_long_click_timer(nullptr)
  65. , m_delegate(nullptr)
  66. , m_packed_init(0)
  67. {
  68. #ifdef TB_RUNTIME_DEBUG_INFO
  69. last_measure_time = 0;
  70. last_layout_time = 0;
  71. #endif // TB_RUNTIME_DEBUG_INFO
  72. }
  73. TBWidget::~TBWidget()
  74. {
  75. assert(!m_parent); ///< A widget must be removed from parent before deleted
  76. m_packed.is_dying = true;
  77. if (this == hovered_widget)
  78. hovered_widget = nullptr;
  79. if (this == captured_widget)
  80. captured_widget = nullptr;
  81. if (this == focused_widget)
  82. focused_widget = nullptr;
  83. if (m_delegate)
  84. m_delegate->OnDelete();
  85. TBWidgetListener::InvokeWidgetDelete(this);
  86. DeleteAllChildren();
  87. delete m_scroller;
  88. delete m_layout_params;
  89. StopLongClickTimer();
  90. assert(!m_listeners.HasLinks()); // There's still listeners added to this widget!
  91. }
  92. void TBWidget::SetRect(const TBRect &rect)
  93. {
  94. if (m_rect.Equals(rect))
  95. return;
  96. TBRect old_rect = m_rect;
  97. m_rect = rect;
  98. if (old_rect.w != m_rect.w || old_rect.h != m_rect.h)
  99. OnResized(old_rect.w, old_rect.h);
  100. Invalidate();
  101. }
  102. void TBWidget::Invalidate()
  103. {
  104. if (!GetVisibilityCombined() && !m_rect.IsEmpty())
  105. return;
  106. TBWidget *tmp = this;
  107. while (tmp)
  108. {
  109. tmp->OnInvalid();
  110. tmp = tmp->m_parent;
  111. }
  112. }
  113. void TBWidget::InvalidateStates()
  114. {
  115. update_widget_states = true;
  116. InvalidateSkinStates();
  117. }
  118. void TBWidget::InvalidateSkinStates()
  119. {
  120. update_skin_states = true;
  121. }
  122. void TBWidget::Die()
  123. {
  124. if (m_packed.is_dying)
  125. return;
  126. m_packed.is_dying = true;
  127. OnDie();
  128. if (!TBWidgetListener::InvokeWidgetDying(this))
  129. {
  130. // No one was interested, so die immediately.
  131. if (m_parent)
  132. m_parent->RemoveChild(this);
  133. delete this;
  134. }
  135. }
  136. TBWidget *TBWidget::GetWidgetByIDInternal(const TBID &id, const TB_TYPE_ID type_id)
  137. {
  138. if (m_id == id && (!type_id || IsOfTypeId(type_id)))
  139. return this;
  140. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  141. {
  142. if (TBWidget *sub_child = child->GetWidgetByIDInternal(id, type_id))
  143. return sub_child;
  144. }
  145. return nullptr;
  146. }
  147. TBStr TBWidget::GetTextByID(const TBID &id)
  148. {
  149. if (TBWidget *widget = GetWidgetByID(id))
  150. return widget->GetText();
  151. return "";
  152. }
  153. int TBWidget::GetValueByID(const TBID &id)
  154. {
  155. if (TBWidget *widget = GetWidgetByID(id))
  156. return widget->GetValue();
  157. return 0;
  158. }
  159. void TBWidget::SetID(const TBID &id)
  160. {
  161. m_id = id;
  162. InvalidateSkinStates();
  163. }
  164. void TBWidget::SetStateRaw(WIDGET_STATE state)
  165. {
  166. if (m_state == state)
  167. return;
  168. m_state = state;
  169. Invalidate();
  170. InvalidateSkinStates();
  171. }
  172. void TBWidget::SetState(WIDGET_STATE state, bool on)
  173. {
  174. SetStateRaw(on ? m_state | state : m_state & ~state);
  175. }
  176. WIDGET_STATE TBWidget::GetAutoState() const
  177. {
  178. WIDGET_STATE state = m_state;
  179. bool add_pressed_state = !cancel_click && this == captured_widget && this == hovered_widget;
  180. if (add_pressed_state)
  181. state |= WIDGET_STATE_PRESSED;
  182. if (this == hovered_widget && (!m_packed.no_automatic_hover_state || add_pressed_state))
  183. state |= WIDGET_STATE_HOVERED;
  184. if (this == focused_widget && show_focus_state)
  185. state |= WIDGET_STATE_FOCUSED;
  186. #ifdef TB_ALWAYS_SHOW_EDIT_FOCUS
  187. else if (this == focused_widget && IsOfType<TBEditField>())
  188. state |= WIDGET_STATE_FOCUSED;
  189. #endif
  190. return state;
  191. }
  192. //static
  193. void TBWidget::SetAutoFocusState(bool on)
  194. {
  195. if (show_focus_state == on)
  196. return;
  197. show_focus_state = on;
  198. if (focused_widget)
  199. focused_widget->Invalidate();
  200. }
  201. void TBWidget::SetOpacity(float opacity)
  202. {
  203. opacity = Clamp(opacity, 0.f, 1.f);
  204. if (m_opacity == opacity)
  205. return;
  206. if (opacity == 0) // Invalidate after setting opacity 0 will do nothing.
  207. Invalidate();
  208. m_opacity = opacity;
  209. Invalidate();
  210. }
  211. void TBWidget::SetVisibilility(WIDGET_VISIBILITY vis)
  212. {
  213. if (m_packed.visibility == vis)
  214. return;
  215. // Invalidate after making it invisible will do nothing.
  216. if (vis != WIDGET_VISIBILITY_VISIBLE)
  217. Invalidate();
  218. if (vis == WIDGET_VISIBILITY_GONE)
  219. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  220. WIDGET_VISIBILITY old_vis = GetVisibility();
  221. m_packed.visibility = vis;
  222. Invalidate();
  223. if (old_vis == WIDGET_VISIBILITY_GONE)
  224. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  225. OnVisibilityChanged();
  226. }
  227. WIDGET_VISIBILITY TBWidget::GetVisibility() const
  228. {
  229. return static_cast<WIDGET_VISIBILITY>(m_packed.visibility);
  230. }
  231. bool TBWidget::GetVisibilityCombined() const
  232. {
  233. const TBWidget *tmp = this;
  234. while (tmp)
  235. {
  236. if (tmp->GetOpacity() == 0 || tmp->GetVisibility() != WIDGET_VISIBILITY_VISIBLE)
  237. return false;
  238. tmp = tmp->m_parent;
  239. }
  240. return true;
  241. }
  242. bool TBWidget::GetDisabled() const
  243. {
  244. const TBWidget *tmp = this;
  245. while (tmp)
  246. {
  247. if (tmp->GetState(WIDGET_STATE_DISABLED))
  248. return true;
  249. tmp = tmp->m_parent;
  250. }
  251. return false;
  252. }
  253. void TBWidget::AddChild(TBWidget *child, WIDGET_Z z, WIDGET_INVOKE_INFO info)
  254. {
  255. AddChildRelative(child, z == WIDGET_Z_TOP ? WIDGET_Z_REL_AFTER : WIDGET_Z_REL_BEFORE, nullptr, info);
  256. }
  257. void TBWidget::AddChildRelative(TBWidget *child, WIDGET_Z_REL z, TBWidget *reference, WIDGET_INVOKE_INFO info)
  258. {
  259. assert(!child->m_parent);
  260. child->m_parent = this;
  261. if (reference)
  262. {
  263. if (z == WIDGET_Z_REL_BEFORE)
  264. m_children.AddBefore(child, reference);
  265. else
  266. m_children.AddAfter(child, reference);
  267. }
  268. else // If there is no reference widget, before means first and after means last.
  269. {
  270. if (z == WIDGET_Z_REL_BEFORE)
  271. m_children.AddFirst(child);
  272. else
  273. m_children.AddLast(child);
  274. }
  275. if (info == WIDGET_INVOKE_INFO_NORMAL)
  276. {
  277. OnChildAdded(child);
  278. child->OnAdded();
  279. TBWidgetListener::InvokeWidgetAdded(this, child);
  280. }
  281. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  282. Invalidate();
  283. InvalidateSkinStates();
  284. }
  285. void TBWidget::RemoveChild(TBWidget *child, WIDGET_INVOKE_INFO info)
  286. {
  287. assert(child->m_parent);
  288. if (info == WIDGET_INVOKE_INFO_NORMAL)
  289. {
  290. // If we're not being deleted and delete the focused widget, try
  291. // to keep the focus in this widget by moving it to the next widget.
  292. if (!m_packed.is_dying && child == focused_widget)
  293. m_parent->MoveFocus(true);
  294. OnChildRemove(child);
  295. child->OnRemove();
  296. TBWidgetListener::InvokeWidgetRemove(this, child);
  297. }
  298. m_children.Remove(child);
  299. child->m_parent = nullptr;
  300. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  301. Invalidate();
  302. InvalidateSkinStates();
  303. }
  304. void TBWidget::DeleteAllChildren()
  305. {
  306. while (TBWidget *child = GetFirstChild())
  307. {
  308. RemoveChild(child);
  309. delete child;
  310. }
  311. }
  312. void TBWidget::SetZ(WIDGET_Z z)
  313. {
  314. if (!m_parent)
  315. return;
  316. if (z == WIDGET_Z_TOP && this == m_parent->m_children.GetLast())
  317. return; // Already at the top
  318. if (z == WIDGET_Z_BOTTOM && this == m_parent->m_children.GetFirst())
  319. return; // Already at the top
  320. TBWidget *parent = m_parent;
  321. parent->RemoveChild(this, WIDGET_INVOKE_INFO_NO_CALLBACKS);
  322. parent->AddChild(this, z, WIDGET_INVOKE_INFO_NO_CALLBACKS);
  323. }
  324. void TBWidget::SetGravity(WIDGET_GRAVITY g)
  325. {
  326. if (m_gravity == g)
  327. return;
  328. m_gravity = g;
  329. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  330. }
  331. void TBWidget::SetSkinBg(const TBID &skin_bg, WIDGET_INVOKE_INFO info)
  332. {
  333. if (skin_bg == m_skin_bg)
  334. return;
  335. // Set the skin and m_skin_bg_expected. During InvokeProcess, we will detect
  336. // if any widget get a different element due to conditions and strong override.
  337. // If that happens, OnSkinChanged will be called and m_skin_bg_expected updated to
  338. // match that override.
  339. m_skin_bg = skin_bg;
  340. m_skin_bg_expected = skin_bg;
  341. Invalidate();
  342. InvalidateSkinStates();
  343. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  344. if (info == WIDGET_INVOKE_INFO_NORMAL)
  345. OnSkinChanged();
  346. }
  347. TBSkinElement *TBWidget::GetSkinBgElement()
  348. {
  349. TBWidgetSkinConditionContext context(this);
  350. WIDGET_STATE state = GetAutoState();
  351. return g_tb_skin->GetSkinElementStrongOverride(m_skin_bg, static_cast<SKIN_STATE>(state), context);
  352. }
  353. TBWidget *TBWidget::FindScrollableWidget(bool scroll_x, bool scroll_y)
  354. {
  355. TBWidget *candidate = this;
  356. while (candidate)
  357. {
  358. ScrollInfo scroll_info = candidate->GetScrollInfo();
  359. if ((scroll_x && scroll_info.CanScrollX()) ||
  360. (scroll_y && scroll_info.CanScrollY()))
  361. return candidate;
  362. candidate = candidate->GetParent();
  363. }
  364. return nullptr;
  365. }
  366. TBScroller *TBWidget::FindStartedScroller()
  367. {
  368. TBWidget *candidate = this;
  369. while (candidate)
  370. {
  371. if (candidate->m_scroller && candidate->m_scroller->IsStarted())
  372. return candidate->m_scroller;
  373. candidate = candidate->GetParent();
  374. }
  375. return nullptr;
  376. }
  377. TBScroller *TBWidget::GetReadyScroller(bool scroll_x, bool scroll_y)
  378. {
  379. if (TBScroller *scroller = FindStartedScroller())
  380. return scroller;
  381. // We didn't have any active scroller, so create one for the nearest scrollable parent.
  382. if (TBWidget *scrollable_widget = FindScrollableWidget(scroll_x, scroll_y))
  383. return scrollable_widget->GetScroller();
  384. return nullptr;
  385. }
  386. TBScroller *TBWidget::GetScroller()
  387. {
  388. if (!m_scroller)
  389. m_scroller = new TBScroller(this);
  390. return m_scroller;
  391. }
  392. void TBWidget::ScrollToSmooth(int x, int y)
  393. {
  394. ScrollInfo info = GetScrollInfo();
  395. int dx = x - info.x;
  396. int dy = y - info.y;
  397. if (TBScroller *scroller = GetReadyScroller(dx != 0, dy != 0))
  398. scroller->OnScrollBy(dx, dy, false);
  399. }
  400. void TBWidget::ScrollBySmooth(int dx, int dy)
  401. {
  402. // Clip the values to the scroll limits, so we don't
  403. // scroll any parents.
  404. //int x = CLAMP(info.x + dx, info.min_x, info.max_x);
  405. //int y = CLAMP(info.y + dy, info.min_y, info.max_y);
  406. //dx = x - info.x;
  407. //dy = y - info.y;
  408. if (!dx && !dy)
  409. return;
  410. if (TBScroller *scroller = GetReadyScroller(dx != 0, dy != 0))
  411. scroller->OnScrollBy(dx, dy, true);
  412. }
  413. void TBWidget::ScrollBy(int dx, int dy)
  414. {
  415. ScrollInfo info = GetScrollInfo();
  416. ScrollTo(info.x + dx, info.y + dy);
  417. }
  418. void TBWidget::ScrollByRecursive(int &dx, int &dy)
  419. {
  420. TBWidget *tmp = this;
  421. while (tmp)
  422. {
  423. ScrollInfo old_info = tmp->GetScrollInfo();
  424. tmp->ScrollTo(old_info.x + dx, old_info.y + dy);
  425. ScrollInfo new_info = tmp->GetScrollInfo();
  426. dx -= new_info.x - old_info.x;
  427. dy -= new_info.y - old_info.y;
  428. if (!dx && !dy)
  429. break;
  430. tmp = tmp->m_parent;
  431. }
  432. }
  433. void TBWidget::ScrollIntoViewRecursive()
  434. {
  435. TBRect scroll_to_rect = m_rect;
  436. TBWidget *tmp = this;
  437. while (tmp->m_parent)
  438. {
  439. tmp->m_parent->ScrollIntoView(scroll_to_rect);
  440. scroll_to_rect.x += tmp->m_parent->m_rect.x;
  441. scroll_to_rect.y += tmp->m_parent->m_rect.y;
  442. tmp = tmp->m_parent;
  443. }
  444. }
  445. void TBWidget::ScrollIntoView(const TBRect &rect)
  446. {
  447. const ScrollInfo info = GetScrollInfo();
  448. int new_x = info.x;
  449. int new_y = info.y;
  450. const TBRect visible_rect = GetPaddingRect().Offset(info.x, info.y);
  451. if (rect.y <= visible_rect.y)
  452. new_y = rect.y;
  453. else if (rect.y + rect.h > visible_rect.y + visible_rect.h)
  454. new_y = rect.y + rect.h - visible_rect.h;
  455. if (rect.x <= visible_rect.x)
  456. new_x = rect.x;
  457. else if (rect.x + rect.w > visible_rect.x + visible_rect.w)
  458. new_x = rect.x + rect.w - visible_rect.w;
  459. ScrollTo(new_x, new_y);
  460. }
  461. bool TBWidget::SetFocus(WIDGET_FOCUS_REASON reason, WIDGET_INVOKE_INFO info)
  462. {
  463. if (focused_widget == this)
  464. return true;
  465. if (GetDisabled() || !GetIsFocusable() || !GetVisibilityCombined() || GetIsDying())
  466. return false;
  467. // Update windows last focus
  468. TBWindow *window = GetParentWindow();
  469. if (window)
  470. {
  471. window->SetLastFocus(this);
  472. // If not active, just return. We should get focus when the window is activated.
  473. // Exception for windows that doesn't activate. They may contain focusable widgets.
  474. if (!window->IsActive() && (window->GetSettings() & WINDOW_SETTINGS_CAN_ACTIVATE))
  475. return true;
  476. }
  477. if (focused_widget)
  478. {
  479. focused_widget->Invalidate();
  480. focused_widget->InvalidateSkinStates();
  481. }
  482. TBWidgetSafePointer old_focus(focused_widget);
  483. focused_widget = this;
  484. Invalidate();
  485. InvalidateSkinStates();
  486. if (reason == WIDGET_FOCUS_REASON_NAVIGATION)
  487. ScrollIntoViewRecursive();
  488. if (info == WIDGET_INVOKE_INFO_NORMAL)
  489. {
  490. // A lot of weird bugs could happen if people mess with focus from OnFocusChanged.
  491. // Take some precaution and detect if it change again after OnFocusChanged(false).
  492. if (TBWidget *old = old_focus.Get())
  493. {
  494. // The currently focused widget still has the pressed state set by the emulated click
  495. // (By keyboard), so unset it before we unfocus it so it's not stuck in pressed state.
  496. if (old->m_packed.has_key_pressed_state)
  497. {
  498. old->SetState(WIDGET_STATE_PRESSED, false);
  499. old->m_packed.has_key_pressed_state = false;
  500. }
  501. old->OnFocusChanged(false);
  502. }
  503. if (old_focus.Get())
  504. TBWidgetListener::InvokeWidgetFocusChanged(old_focus.Get(), false);
  505. if (focused_widget && focused_widget == this)
  506. focused_widget->OnFocusChanged(true);
  507. if (focused_widget && focused_widget == this)
  508. TBWidgetListener::InvokeWidgetFocusChanged(focused_widget, true);
  509. }
  510. return true;
  511. }
  512. bool TBWidget::SetFocusRecursive(WIDGET_FOCUS_REASON reason)
  513. {
  514. // Search for a child widget that accepts focus
  515. TBWidget *child = GetFirstChild();
  516. while (child)
  517. {
  518. if (child->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN))
  519. return true;
  520. child = child->GetNextDeep(this);
  521. }
  522. return false;
  523. }
  524. bool TBWidget::MoveFocus(bool forward)
  525. {
  526. TBWidget *origin = focused_widget;
  527. if (!origin)
  528. origin = this;
  529. TBWidget *root = origin->GetParentWindow();
  530. if (!root)
  531. root = origin->GetParentRoot();
  532. TBWidget *current = origin;
  533. while (current)
  534. {
  535. current = forward ? current->GetNextDeep() : current->GetPrevDeep();
  536. // Wrap around if we reach the end/beginning
  537. if (!current || !root->IsAncestorOf(current))
  538. current = forward ? root->GetFirstChild() : root->GetLastLeaf();
  539. // Break if we reached the origin again (we're not finding anything else)
  540. if (current == origin)
  541. break;
  542. // Try to focus what we found
  543. if (current && current->SetFocus(WIDGET_FOCUS_REASON_NAVIGATION))
  544. return true;
  545. }
  546. return false;
  547. }
  548. TBWidget *TBWidget::GetNextDeep(const TBWidget *bounding_ancestor) const
  549. {
  550. if (m_children.GetFirst())
  551. return GetFirstChild();
  552. for (const TBWidget *widget = this; widget != bounding_ancestor; widget = widget->m_parent)
  553. if (widget->next)
  554. return widget->GetNext();
  555. return nullptr;
  556. }
  557. TBWidget *TBWidget::GetPrevDeep() const
  558. {
  559. if (!prev)
  560. return m_parent;
  561. TBWidget *widget = GetPrev();
  562. while (widget->m_children.GetLast())
  563. widget = widget->GetLastChild();
  564. return widget;
  565. }
  566. TBWidget *TBWidget::GetLastLeaf() const
  567. {
  568. if (TBWidget *widget = GetLastChild())
  569. {
  570. while (widget->GetLastChild())
  571. widget = widget->GetLastChild();
  572. return widget;
  573. }
  574. return nullptr;
  575. }
  576. bool TBWidget::GetIsInteractable() const
  577. {
  578. return !(m_opacity == 0 || GetIgnoreInput() || GetState(WIDGET_STATE_DISABLED) ||
  579. GetIsDying() || GetVisibility() != WIDGET_VISIBILITY_VISIBLE);
  580. }
  581. WIDGET_HIT_STATUS TBWidget::GetHitStatus(int x, int y)
  582. {
  583. if (!GetIsInteractable())
  584. return WIDGET_HIT_STATUS_NO_HIT;
  585. return x >= 0 && y >= 0 && x < m_rect.w && y < m_rect.h ? WIDGET_HIT_STATUS_HIT : WIDGET_HIT_STATUS_NO_HIT;
  586. }
  587. TBWidget *TBWidget::GetWidgetAt(int x, int y, bool include_children) const
  588. {
  589. int child_translation_x, child_translation_y;
  590. GetChildTranslation(child_translation_x, child_translation_y);
  591. x -= child_translation_x;
  592. y -= child_translation_y;
  593. TBWidget *tmp = GetFirstChild();
  594. TBWidget *last_match = nullptr;
  595. while (tmp)
  596. {
  597. WIDGET_HIT_STATUS hit_status = tmp->GetHitStatus(x - tmp->m_rect.x, y - tmp->m_rect.y);
  598. if (hit_status)
  599. {
  600. if (include_children && hit_status != WIDGET_HIT_STATUS_HIT_NO_CHILDREN)
  601. {
  602. last_match = tmp->GetWidgetAt(x - tmp->m_rect.x, y - tmp->m_rect.y, include_children);
  603. if (!last_match)
  604. last_match = tmp;
  605. }
  606. else
  607. last_match = tmp;
  608. }
  609. tmp = tmp->GetNext();
  610. }
  611. return last_match;
  612. }
  613. TBWidget *TBWidget::GetChildFromIndex(int index) const
  614. {
  615. int i = 0;
  616. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  617. if (i++ == index)
  618. return child;
  619. return nullptr;
  620. }
  621. int TBWidget::GetIndexFromChild(TBWidget *child) const
  622. {
  623. assert(child->GetParent() == this);
  624. int i = 0;
  625. for (TBWidget *tmp = GetFirstChild(); tmp; tmp = tmp->GetNext(), i++)
  626. if (tmp == child)
  627. return i;
  628. return -1; ///< Should not happen!
  629. }
  630. bool TBWidget::IsAncestorOf(TBWidget *other_widget) const
  631. {
  632. while (other_widget)
  633. {
  634. if (other_widget == this)
  635. return true;
  636. other_widget = other_widget->m_parent;
  637. }
  638. return false;
  639. }
  640. bool TBWidget::IsEventDestinationFor(TBWidget *other_widget) const
  641. {
  642. while (other_widget)
  643. {
  644. if (other_widget == this)
  645. return true;
  646. other_widget = other_widget->GetEventDestination();
  647. }
  648. return false;
  649. }
  650. TBWidget *TBWidget::GetParentRoot()
  651. {
  652. TBWidget *tmp = this;
  653. while (tmp->m_parent)
  654. tmp = tmp->m_parent;
  655. return tmp;
  656. }
  657. TBWindow *TBWidget::GetParentWindow()
  658. {
  659. TBWidget *tmp = this;
  660. while (tmp && !tmp->IsOfType<TBWindow>())
  661. tmp = tmp->m_parent;
  662. return static_cast<TBWindow *>(tmp);
  663. }
  664. void TBWidget::AddListener(TBWidgetListener *listener)
  665. {
  666. m_listeners.AddLast(listener);
  667. }
  668. void TBWidget::RemoveListener(TBWidgetListener *listener)
  669. {
  670. m_listeners.Remove(listener);
  671. }
  672. bool TBWidget::HasListener(TBWidgetListener *listener) const
  673. {
  674. return m_listeners.ContainsLink(listener);
  675. }
  676. void TBWidget::OnPaintChildren(const PaintProps &paint_props)
  677. {
  678. if (!m_children.GetFirst())
  679. return;
  680. // Translate renderer with child translation
  681. int child_translation_x, child_translation_y;
  682. GetChildTranslation(child_translation_x, child_translation_y);
  683. g_renderer->Translate(child_translation_x, child_translation_y);
  684. TBRect clip_rect = g_renderer->GetClipRect();
  685. // Invoke paint on all children that are in the current visible rect.
  686. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  687. {
  688. if (clip_rect.Intersects(child->m_rect))
  689. child->InvokePaint(paint_props);
  690. }
  691. // Invoke paint of overlay elements on all children that are in the current visible rect.
  692. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  693. {
  694. if (clip_rect.Intersects(child->m_rect) && child->GetVisibility() == WIDGET_VISIBILITY_VISIBLE)
  695. {
  696. TBSkinElement *skin_element = child->GetSkinBgElement();
  697. if (skin_element && skin_element->HasOverlayElements())
  698. {
  699. // Update the renderer with the widgets opacity
  700. WIDGET_STATE state = child->GetAutoState();
  701. float old_opacity = g_renderer->GetOpacity();
  702. float opacity = old_opacity * child->CalculateOpacityInternal(state, skin_element);
  703. if (opacity > 0)
  704. {
  705. g_renderer->SetOpacity(opacity);
  706. TBWidgetSkinConditionContext context(child);
  707. g_tb_skin->PaintSkinOverlay(child->m_rect, skin_element, static_cast<SKIN_STATE>(state), context);
  708. g_renderer->SetOpacity(old_opacity);
  709. }
  710. }
  711. }
  712. }
  713. // Draw generic focus skin if the focused widget is one of the children, and the skin
  714. // doesn't have a skin state for focus which would already be painted.
  715. if (focused_widget && focused_widget->m_parent == this)
  716. {
  717. TBWidgetSkinConditionContext context(focused_widget);
  718. TBSkinElement *skin_element = focused_widget->GetSkinBgElement();
  719. if (!skin_element || !skin_element->HasState(SKIN_STATE_FOCUSED, context))
  720. {
  721. WIDGET_STATE state = focused_widget->GetAutoState();
  722. if (state & SKIN_STATE_FOCUSED)
  723. g_tb_skin->PaintSkin(focused_widget->m_rect, TBIDC("generic_focus"), static_cast<SKIN_STATE>(state), context);
  724. }
  725. }
  726. g_renderer->Translate(-child_translation_x, -child_translation_y);
  727. }
  728. void TBWidget::OnResized(int old_w, int old_h)
  729. {
  730. int dw = m_rect.w - old_w;
  731. int dh = m_rect.h - old_h;
  732. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  733. {
  734. if (child->GetVisibility() == WIDGET_VISIBILITY_GONE)
  735. continue;
  736. TBRect rect = child->m_rect;
  737. if ((child->m_gravity & WIDGET_GRAVITY_LEFT) && (child->m_gravity & WIDGET_GRAVITY_RIGHT))
  738. rect.w += dw;
  739. else if(child->m_gravity & WIDGET_GRAVITY_RIGHT)
  740. rect.x += dw;
  741. if ((child->m_gravity & WIDGET_GRAVITY_TOP) && (child->m_gravity & WIDGET_GRAVITY_BOTTOM))
  742. rect.h += dh;
  743. else if (child->m_gravity & WIDGET_GRAVITY_BOTTOM)
  744. rect.y += dh;
  745. child->SetRect(rect);
  746. }
  747. }
  748. void TBWidget::OnInflateChild(TBWidget *child)
  749. {
  750. if (child->GetVisibility() == WIDGET_VISIBILITY_GONE)
  751. return;
  752. // If the child pull towards only one edge (per axis), stick to that edge
  753. // and use the preferred size. Otherwise fill up all available space.
  754. TBRect padding_rect = GetPaddingRect();
  755. TBRect child_rect = padding_rect;
  756. WIDGET_GRAVITY gravity = child->GetGravity();
  757. bool fill_x = (gravity & WIDGET_GRAVITY_LEFT) && (gravity & WIDGET_GRAVITY_RIGHT);
  758. bool fill_y = (gravity & WIDGET_GRAVITY_TOP) && (gravity & WIDGET_GRAVITY_BOTTOM);
  759. if (!fill_x || !fill_y)
  760. {
  761. PreferredSize ps = child->GetPreferredSize();
  762. if (!fill_x)
  763. {
  764. child_rect.w = ps.pref_w;
  765. if (gravity & WIDGET_GRAVITY_RIGHT)
  766. child_rect.x = padding_rect.x + padding_rect.w - child_rect.w;
  767. }
  768. if (!fill_y)
  769. {
  770. child_rect.h = ps.pref_h;
  771. if (gravity & WIDGET_GRAVITY_BOTTOM)
  772. child_rect.y = padding_rect.y + padding_rect.h - child_rect.h;
  773. }
  774. }
  775. child->SetRect(child_rect);
  776. }
  777. TBRect TBWidget::GetPaddingRect()
  778. {
  779. TBRect padding_rect(0, 0, m_rect.w, m_rect.h);
  780. if (TBSkinElement *e = GetSkinBgElement())
  781. {
  782. padding_rect.x += e->padding_left;
  783. padding_rect.y += e->padding_top;
  784. padding_rect.w -= e->padding_left + e->padding_right;
  785. padding_rect.h -= e->padding_top + e->padding_bottom;
  786. }
  787. return padding_rect;
  788. }
  789. PreferredSize TBWidget::OnCalculatePreferredContentSize(const SizeConstraints &constraints)
  790. {
  791. // The default preferred size is calculated to satisfy the children
  792. // in the best way. Since this is the default, it's probably not a
  793. // layouting widget and children are resized purely by gravity.
  794. // Allow this widget a larger maximum if our gravity wants both ways,
  795. // otherwise don't grow more than the largest child.
  796. bool apply_max_w = !((m_gravity & WIDGET_GRAVITY_LEFT) && (m_gravity & WIDGET_GRAVITY_RIGHT));
  797. bool apply_max_h = !((m_gravity & WIDGET_GRAVITY_TOP) && (m_gravity & WIDGET_GRAVITY_BOTTOM));
  798. bool has_layouting_children = false;
  799. PreferredSize ps;
  800. TBSkinElement *bg_skin = GetSkinBgElement();
  801. int horizontal_padding = bg_skin ? bg_skin->padding_left + bg_skin->padding_right : 0;
  802. int vertical_padding = bg_skin ? bg_skin->padding_top + bg_skin->padding_bottom : 0;
  803. SizeConstraints inner_sc = constraints.ConstrainByPadding(horizontal_padding, vertical_padding);
  804. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  805. {
  806. if (child->GetVisibility() == WIDGET_VISIBILITY_GONE)
  807. continue;
  808. if (!has_layouting_children)
  809. {
  810. has_layouting_children = true;
  811. if (apply_max_w)
  812. ps.max_w = 0;
  813. if (apply_max_h)
  814. ps.max_h = 0;
  815. }
  816. PreferredSize child_ps = child->GetPreferredSize(inner_sc);
  817. ps.pref_w = MAX(ps.pref_w, child_ps.pref_w);
  818. ps.pref_h = MAX(ps.pref_h, child_ps.pref_h);
  819. ps.min_w = MAX(ps.min_w, child_ps.min_w);
  820. ps.min_h = MAX(ps.min_h, child_ps.min_h);
  821. if (apply_max_w)
  822. ps.max_w = MAX(ps.max_w, child_ps.max_w);
  823. if (apply_max_h)
  824. ps.max_h = MAX(ps.max_h, child_ps.max_h);
  825. ps.size_dependency |= child_ps.size_dependency;
  826. }
  827. return ps;
  828. }
  829. PreferredSize TBWidget::OnCalculatePreferredSize(const SizeConstraints &constraints)
  830. {
  831. PreferredSize ps = OnCalculatePreferredContentSize(constraints);
  832. assert(ps.pref_w >= ps.min_w);
  833. assert(ps.pref_h >= ps.min_h);
  834. if (TBSkinElement *e = GetSkinBgElement())
  835. {
  836. // Override the widgets preferences with skin attributes that has been specified.
  837. // If not set by the widget, calculate based on the intrinsic size of the skin.
  838. const int skin_intrinsic_w = e->GetIntrinsicWidth();
  839. if (e->GetPrefWidth() != SKIN_VALUE_NOT_SPECIFIED)
  840. ps.pref_w = e->GetPrefWidth();
  841. else if (ps.pref_w == 0 && skin_intrinsic_w != SKIN_VALUE_NOT_SPECIFIED)
  842. ps.pref_w = skin_intrinsic_w;
  843. else
  844. {
  845. // Grow by padding to get the preferred size from preferred content size.
  846. ps.min_w += e->padding_left + e->padding_right;
  847. ps.pref_w += e->padding_left + e->padding_right;
  848. }
  849. const int skin_intrinsic_h = e->GetIntrinsicHeight();
  850. if (e->GetPrefHeight() != SKIN_VALUE_NOT_SPECIFIED)
  851. ps.pref_h = e->GetPrefHeight();
  852. else if (ps.pref_h == 0 && skin_intrinsic_h != SKIN_VALUE_NOT_SPECIFIED)
  853. ps.pref_h = skin_intrinsic_h;
  854. else
  855. {
  856. // Grow by padding to get the preferred size from preferred content size.
  857. ps.min_h += e->padding_top + e->padding_bottom;
  858. ps.pref_h += e->padding_top + e->padding_bottom;
  859. }
  860. if (e->GetMinWidth() != SKIN_VALUE_NOT_SPECIFIED)
  861. ps.min_w = e->GetMinWidth();
  862. else
  863. ps.min_w = MAX(ps.min_w, e->GetIntrinsicMinWidth());
  864. if (e->GetMinHeight() != SKIN_VALUE_NOT_SPECIFIED)
  865. ps.min_h = e->GetMinHeight();
  866. else
  867. ps.min_h = MAX(ps.min_h, e->GetIntrinsicMinHeight());
  868. if (e->GetMaxWidth() != SKIN_VALUE_NOT_SPECIFIED)
  869. ps.max_w = e->GetMaxWidth();
  870. else
  871. ps.max_w += e->padding_left + e->padding_right;
  872. if (e->GetMaxHeight() != SKIN_VALUE_NOT_SPECIFIED)
  873. ps.max_h = e->GetMaxHeight();
  874. else
  875. ps.max_h += e->padding_top + e->padding_bottom;
  876. // Sanitize result
  877. ps.pref_w = MAX(ps.pref_w, ps.min_w);
  878. ps.pref_h = MAX(ps.pref_h, ps.min_h);
  879. }
  880. return ps;
  881. }
  882. PreferredSize TBWidget::GetPreferredSize(const SizeConstraints &in_constraints)
  883. {
  884. SizeConstraints constraints(in_constraints);
  885. if (m_layout_params)
  886. constraints = constraints.ConstrainByLayoutParams(*m_layout_params);
  887. // Returned cached result if valid and the constraints are the same.
  888. if (m_packed.is_cached_ps_valid)
  889. {
  890. if (m_cached_sc == constraints ||
  891. m_cached_ps.size_dependency == SIZE_DEP_NONE /*||
  892. // FIX: These optimizations would probably be good. Keeping
  893. // disabled for now because it needs testing.
  894. // If *only* width depend on height, only the height matter
  895. (m_cached_ps.size_dependency == SIZE_DEP_WIDTH_DEPEND_ON_HEIGHT &&
  896. m_cached_sc.available_h == constraints.available_h) ||
  897. // If *only* height depend on width, only the width matter
  898. (m_cached_ps.size_dependency == SIZE_DEP_HEIGHT_DEPEND_ON_WIDTH &&
  899. m_cached_sc.available_w == constraints.available_w)*/)
  900. {
  901. return m_cached_ps;
  902. }
  903. }
  904. // Measure and save to cache
  905. TB_IF_DEBUG_SETTING(LAYOUT_PS_DEBUGGING, last_measure_time = TBSystem::GetTimeMS());
  906. m_packed.is_cached_ps_valid = 1;
  907. m_cached_ps = OnCalculatePreferredSize(constraints);
  908. m_cached_sc = constraints;
  909. // Override the calculated ps with any specified layout parameter.
  910. if (m_layout_params)
  911. {
  912. #define LP_OVERRIDE(param) if (m_layout_params->param != LayoutParams::UNSPECIFIED) \
  913. m_cached_ps.param = m_layout_params->param;
  914. LP_OVERRIDE(min_w);
  915. LP_OVERRIDE(min_h);
  916. LP_OVERRIDE(max_w);
  917. LP_OVERRIDE(max_h);
  918. LP_OVERRIDE(pref_w);
  919. LP_OVERRIDE(pref_h);
  920. // Sanitize results
  921. m_cached_ps.max_w = MAX(m_cached_ps.max_w, m_cached_ps.min_w);
  922. m_cached_ps.max_h = MAX(m_cached_ps.max_h, m_cached_ps.min_h);
  923. m_cached_ps.pref_w = MAX(m_cached_ps.pref_w, m_cached_ps.min_w);
  924. m_cached_ps.pref_h = MAX(m_cached_ps.pref_h, m_cached_ps.min_h);
  925. }
  926. return m_cached_ps;
  927. }
  928. void TBWidget::SetLayoutParams(const LayoutParams &lp)
  929. {
  930. if (!m_layout_params)
  931. m_layout_params = new LayoutParams;
  932. if (!m_layout_params)
  933. return;
  934. *m_layout_params = lp;
  935. m_packed.is_cached_ps_valid = 0;
  936. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  937. }
  938. void TBWidget::InvalidateLayout(INVALIDATE_LAYOUT il)
  939. {
  940. m_packed.is_cached_ps_valid = 0;
  941. if (GetVisibility() == WIDGET_VISIBILITY_GONE)
  942. return;
  943. Invalidate();
  944. if (il == INVALIDATE_LAYOUT_RECURSIVE && m_parent)
  945. m_parent->InvalidateLayout(il);
  946. }
  947. void TBWidget::InvokeProcess()
  948. {
  949. InvokeSkinUpdatesInternal(false);
  950. InvokeProcessInternal();
  951. }
  952. void TBWidget::InvokeSkinUpdatesInternal(bool force_update)
  953. {
  954. if (!update_skin_states && !force_update)
  955. return;
  956. update_skin_states = false;
  957. // Check if the skin we get is different from what we expect. That might happen
  958. // if the skin has some strong override dependant a condition that has changed.
  959. // If that happens, call OnSkinChanged so the widget can react to that, and
  960. // invalidate layout to apply new skin properties.
  961. if (TBSkinElement *skin_elm = GetSkinBgElement())
  962. {
  963. if (skin_elm->id != m_skin_bg_expected)
  964. {
  965. OnSkinChanged();
  966. m_skin_bg_expected = skin_elm->id;
  967. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  968. }
  969. }
  970. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  971. child->InvokeSkinUpdatesInternal(true);
  972. }
  973. void TBWidget::InvokeProcessInternal()
  974. {
  975. OnProcess();
  976. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  977. child->InvokeProcessInternal();
  978. OnProcessAfterChildren();
  979. }
  980. void TBWidget::InvokeProcessStates(bool force_update)
  981. {
  982. if (!update_widget_states && !force_update)
  983. return;
  984. update_widget_states = false;
  985. OnProcessStates();
  986. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  987. child->InvokeProcessStates(true);
  988. }
  989. float TBWidget::CalculateOpacityInternal(WIDGET_STATE state, TBSkinElement *skin_element) const
  990. {
  991. float opacity = m_opacity;
  992. if (skin_element)
  993. opacity *= skin_element->opacity;
  994. if (state & WIDGET_STATE_DISABLED)
  995. opacity *= g_tb_skin->GetDefaultDisabledOpacity();
  996. return opacity;
  997. }
  998. void TBWidget::InvokePaint(const PaintProps &parent_paint_props)
  999. {
  1000. // Don't paint invisible widgets
  1001. if (m_opacity == 0 || m_rect.IsEmpty() || GetVisibility() != WIDGET_VISIBILITY_VISIBLE)
  1002. return;
  1003. WIDGET_STATE state = GetAutoState();
  1004. TBSkinElement *skin_element = GetSkinBgElement();
  1005. // Multiply current opacity with widget opacity, skin opacity and state opacity.
  1006. float old_opacity = g_renderer->GetOpacity();
  1007. float opacity = old_opacity * CalculateOpacityInternal(state, skin_element);
  1008. if (opacity == 0)
  1009. return;
  1010. // FIX: This does not give the correct result! Must use a new render target!
  1011. g_renderer->SetOpacity(opacity);
  1012. int trns_x = m_rect.x, trns_y = m_rect.y;
  1013. g_renderer->Translate(trns_x, trns_y);
  1014. // Paint background skin
  1015. TBRect local_rect(0, 0, m_rect.w, m_rect.h);
  1016. TBWidgetSkinConditionContext context(this);
  1017. TBSkinElement *used_element = g_tb_skin->PaintSkin(local_rect, skin_element, static_cast<SKIN_STATE>(state), context);
  1018. assert(!!used_element == !!skin_element);
  1019. TB_IF_DEBUG_SETTING(LAYOUT_BOUNDS, g_renderer->DrawRect(local_rect, TBColor(255, 255, 255, 50)));
  1020. // Inherit properties from parent if not specified in the used skin for this widget.
  1021. PaintProps paint_props = parent_paint_props;
  1022. if (used_element && used_element->text_color != 0)
  1023. paint_props.text_color = used_element->text_color;
  1024. // Paint content
  1025. OnPaint(paint_props);
  1026. if (used_element)
  1027. g_renderer->Translate(used_element->content_ofs_x, used_element->content_ofs_y);
  1028. // Paint children
  1029. OnPaintChildren(paint_props);
  1030. #ifdef TB_RUNTIME_DEBUG_INFO
  1031. if (TB_DEBUG_SETTING(LAYOUT_PS_DEBUGGING))
  1032. {
  1033. // Layout debug painting. Paint recently layouted widgets with red and
  1034. // recently measured widgets with yellow.
  1035. // Invalidate to keep repainting until we've timed out (so it's removed).
  1036. const double debug_time = 300;
  1037. const double now = TBSystem::GetTimeMS();
  1038. if (now < last_layout_time + debug_time)
  1039. {
  1040. g_renderer->DrawRect(local_rect, TBColor(255, 30, 30, 200));
  1041. Invalidate();
  1042. }
  1043. if (now < last_measure_time + debug_time)
  1044. {
  1045. g_renderer->DrawRect(local_rect.Shrink(1, 1), TBColor(255, 255, 30, 200));
  1046. Invalidate();
  1047. }
  1048. }
  1049. #endif // TB_RUNTIME_DEBUG_INFO
  1050. if (used_element)
  1051. g_renderer->Translate(-used_element->content_ofs_x, -used_element->content_ofs_y);
  1052. g_renderer->Translate(-trns_x, -trns_y);
  1053. g_renderer->SetOpacity(old_opacity);
  1054. }
  1055. bool TBWidget::InvokeEvent(TBWidgetEvent &ev)
  1056. {
  1057. ev.target = this;
  1058. // First call the global listener about this event.
  1059. // Who knows, maybe some listener will block the event or cause us
  1060. // to be deleted.
  1061. TBWidgetSafePointer this_widget(this);
  1062. if (TBWidgetListener::InvokeWidgetInvokeEvent(this, ev))
  1063. return true;
  1064. if (!this_widget.Get())
  1065. return true; // We got removed so we actually handled this event.
  1066. if (ev.type == EVENT_TYPE_CHANGED)
  1067. {
  1068. InvalidateSkinStates();
  1069. m_connection.SyncFromWidget(this);
  1070. }
  1071. if (!this_widget.Get())
  1072. return true; // We got removed so we actually handled this event.
  1073. // Always update states after some event types.
  1074. switch (ev.type)
  1075. {
  1076. case EVENT_TYPE_CLICK:
  1077. case EVENT_TYPE_LONG_CLICK:
  1078. case EVENT_TYPE_CHANGED:
  1079. case EVENT_TYPE_KEY_DOWN:
  1080. case EVENT_TYPE_KEY_UP:
  1081. InvalidateStates();
  1082. default:
  1083. break;
  1084. };
  1085. // Call OnEvent on this widgets and travel up through its parents if not handled.
  1086. bool handled = false;
  1087. TBWidget *tmp = this;
  1088. while (tmp && !(handled = tmp->OnEvent(ev)))
  1089. tmp = tmp->GetEventDestination();
  1090. return handled;
  1091. }
  1092. void TBWidget::StartLongClickTimer(bool touch)
  1093. {
  1094. StopLongClickTimer();
  1095. m_long_click_timer = new TBLongClickTimer(this, touch);
  1096. }
  1097. void TBWidget::StopLongClickTimer()
  1098. {
  1099. if (!m_long_click_timer)
  1100. return;
  1101. delete m_long_click_timer;
  1102. m_long_click_timer = nullptr;
  1103. }
  1104. void TBWidget::InvokePointerDown(int x, int y, int click_count, MODIFIER_KEYS modifierkeys, bool touch)
  1105. {
  1106. if (!captured_widget)
  1107. {
  1108. SetCapturedWidget(GetWidgetAt(x, y, true));
  1109. SetHoveredWidget(captured_widget, touch);
  1110. //captured_button = button;
  1111. // Hide focus when we use the pointer, if it's not on the focused widget.
  1112. if (focused_widget != captured_widget)
  1113. SetAutoFocusState(false);
  1114. // Start long click timer. Only for touch events for now.
  1115. if (touch && captured_widget && captured_widget->GetWantLongClick())
  1116. captured_widget->StartLongClickTimer(touch);
  1117. // Get the closest parent window and bring it to the top
  1118. TBWindow *window = captured_widget ? captured_widget->GetParentWindow() : nullptr;
  1119. if (window)
  1120. window->Activate();
  1121. }
  1122. if (captured_widget)
  1123. {
  1124. // Check if there's any started scroller that should be stopped.
  1125. TBWidget *tmp = captured_widget;
  1126. while (tmp)
  1127. {
  1128. if (tmp->m_scroller && tmp->m_scroller->IsStarted())
  1129. {
  1130. // When we touch down to stop a scroller, we don't
  1131. // want the touch to end up causing a click.
  1132. cancel_click = true;
  1133. tmp->m_scroller->Stop();
  1134. break;
  1135. }
  1136. tmp = tmp->GetParent();
  1137. }
  1138. // Focus the captured widget or the closest
  1139. // focusable parent if it isn't focusable.
  1140. TBWidget *focus_target = captured_widget;
  1141. while (focus_target)
  1142. {
  1143. if (focus_target->SetFocus(WIDGET_FOCUS_REASON_POINTER))
  1144. break;
  1145. focus_target = focus_target->m_parent;
  1146. }
  1147. }
  1148. if (captured_widget)
  1149. {
  1150. captured_widget->ConvertFromRoot(x, y);
  1151. pointer_move_widget_x = pointer_down_widget_x = x;
  1152. pointer_move_widget_y = pointer_down_widget_y = y;
  1153. TBWidgetEvent ev(EVENT_TYPE_POINTER_DOWN, x, y, touch, modifierkeys);
  1154. ev.count = click_count;
  1155. captured_widget->InvokeEvent(ev);
  1156. }
  1157. }
  1158. void TBWidget::InvokePointerUp(int x, int y, MODIFIER_KEYS modifierkeys, bool touch)
  1159. {
  1160. if (captured_widget)
  1161. {
  1162. captured_widget->ConvertFromRoot(x, y);
  1163. TBWidgetEvent ev_up(EVENT_TYPE_POINTER_UP, x, y, touch, modifierkeys);
  1164. TBWidgetEvent ev_click(EVENT_TYPE_CLICK, x, y, touch, modifierkeys);
  1165. captured_widget->InvokeEvent(ev_up);
  1166. if (!cancel_click && captured_widget && captured_widget->GetHitStatus(x, y))
  1167. captured_widget->InvokeEvent(ev_click);
  1168. if (captured_widget) // && button == captured_button
  1169. captured_widget->ReleaseCapture();
  1170. }
  1171. }
  1172. void TBWidget::InvokeRightPointerDown(int x, int y, int click_count, MODIFIER_KEYS modifierkeys)
  1173. {
  1174. // note we're not capturing the widget as with normal pointer down
  1175. TBWidget* widget = GetWidgetAt(x, y, true);
  1176. if (!widget)
  1177. return;
  1178. TBWidgetEvent ev(EVENT_TYPE_RIGHT_POINTER_DOWN, x, y, false, modifierkeys);
  1179. ev.count = click_count;
  1180. widget->InvokeEvent(ev);
  1181. }
  1182. void TBWidget::InvokeRightPointerUp(int x, int y, MODIFIER_KEYS modifierkeys)
  1183. {
  1184. // note we're not capturing the widget as with normal pointer down
  1185. TBWidget* widget = GetWidgetAt(x, y, true);
  1186. if (!widget)
  1187. return;
  1188. TBWidgetEvent ev(EVENT_TYPE_RIGHT_POINTER_UP, x, y, false, modifierkeys);
  1189. ev.target = widget;
  1190. widget->InvokeEvent(ev);
  1191. }
  1192. void TBWidget::MaybeInvokeLongClickOrContextMenu(bool touch)
  1193. {
  1194. StopLongClickTimer();
  1195. if (captured_widget == this &&
  1196. !cancel_click &&
  1197. captured_widget->GetHitStatus(pointer_move_widget_x, pointer_move_widget_y))
  1198. {
  1199. // Invoke long click
  1200. TBWidgetEvent ev_long_click(EVENT_TYPE_LONG_CLICK, pointer_move_widget_x, pointer_move_widget_y, touch, TB_MODIFIER_NONE);
  1201. bool handled = captured_widget->InvokeEvent(ev_long_click);
  1202. if (!handled)
  1203. {
  1204. // Long click not handled so invoke a context menu event instead
  1205. TBWidgetEvent ev_context_menu(EVENT_TYPE_CONTEXT_MENU, pointer_move_widget_x, pointer_move_widget_y, touch, TB_MODIFIER_NONE);
  1206. handled = captured_widget->InvokeEvent(ev_context_menu);
  1207. }
  1208. // If any event was handled, suppress click when releasing pointer.
  1209. if (handled)
  1210. cancel_click = true;
  1211. }
  1212. }
  1213. void TBWidget::InvokePointerMove(int x, int y, MODIFIER_KEYS modifierkeys, bool touch)
  1214. {
  1215. SetHoveredWidget(GetWidgetAt(x, y, true), touch);
  1216. TBWidget *target = captured_widget ? captured_widget : hovered_widget;
  1217. if (target)
  1218. {
  1219. target->ConvertFromRoot(x, y);
  1220. pointer_move_widget_x = x;
  1221. pointer_move_widget_y = y;
  1222. TBWidgetEvent ev(EVENT_TYPE_POINTER_MOVE, x, y, touch, modifierkeys);
  1223. if (target->InvokeEvent(ev))
  1224. return;
  1225. // The move event was not handled, so handle panning of scrollable widgets.
  1226. HandlePanningOnMove(x, y);
  1227. }
  1228. }
  1229. void TBWidget::HandlePanningOnMove(int x, int y)
  1230. {
  1231. if (!captured_widget)
  1232. return;
  1233. // Check pointer movement
  1234. const int dx = pointer_down_widget_x - x;
  1235. const int dy = pointer_down_widget_y - y;
  1236. const int threshold = TBSystem::GetPanThreshold();
  1237. const bool maybe_start_panning_x = ABS(dx) >= threshold;
  1238. const bool maybe_start_panning_y = ABS(dy) >= threshold;
  1239. // Do panning, or attempt starting panning (we don't know if any widget is scrollable yet)
  1240. if (captured_widget->m_packed.is_panning || maybe_start_panning_x || maybe_start_panning_y)
  1241. {
  1242. // The threshold is met for not invoking any long click
  1243. captured_widget->StopLongClickTimer();
  1244. int start_compensation_x = 0, start_compensation_y = 0;
  1245. if (!captured_widget->m_packed.is_panning)
  1246. {
  1247. // When we start panning, deduct the extra distance caused by the
  1248. // start threshold from the delta so we don't start with a sudden jump.
  1249. int extra = threshold - 1;
  1250. if (maybe_start_panning_x)
  1251. start_compensation_x = dx < 0 ? extra : -extra;
  1252. if (maybe_start_panning_y)
  1253. start_compensation_y = dy < 0 ? extra : -extra;
  1254. }
  1255. // Get any active scroller and feed it with pan actions.
  1256. TBScroller *scroller = captured_widget->GetReadyScroller(dx != 0, dy != 0);
  1257. if (!scroller)
  1258. return;
  1259. int old_translation_x = 0, old_translation_y = 0;
  1260. captured_widget->GetScrollRoot()->GetChildTranslation(old_translation_x, old_translation_y);
  1261. if (scroller->OnPan(dx + start_compensation_x, dy + start_compensation_y))
  1262. {
  1263. // Scroll delta changed, so we are now panning!
  1264. captured_widget->m_packed.is_panning = true;
  1265. cancel_click = true;
  1266. // If the captured widget (or its scroll root) has panned, we have to compensate the
  1267. // pointer down coordinates so we won't accumulate the difference the following pan.
  1268. int new_translation_x = 0, new_translation_y = 0;
  1269. captured_widget->GetScrollRoot()->GetChildTranslation(new_translation_x, new_translation_y);
  1270. pointer_down_widget_x += new_translation_x - old_translation_x + start_compensation_x;
  1271. pointer_down_widget_y += new_translation_y - old_translation_y + start_compensation_y;
  1272. }
  1273. }
  1274. }
  1275. void TBWidget::InvokeWheel(int x, int y, int delta_x, int delta_y, MODIFIER_KEYS modifierkeys)
  1276. {
  1277. SetHoveredWidget(GetWidgetAt(x, y, true), true);
  1278. TBWidget *target = captured_widget ? captured_widget : hovered_widget;
  1279. if (target)
  1280. {
  1281. target->ConvertFromRoot(x, y);
  1282. pointer_move_widget_x = x;
  1283. pointer_move_widget_y = y;
  1284. TBWidgetEvent ev(EVENT_TYPE_WHEEL, x, y, true, modifierkeys);
  1285. ev.delta_x = delta_x;
  1286. ev.delta_y = delta_y;
  1287. target->InvokeEvent(ev);
  1288. }
  1289. }
  1290. bool TBWidget::InvokeKey(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
  1291. {
  1292. bool handled = false;
  1293. if (focused_widget)
  1294. {
  1295. // ATOMIC: This may have some side effects for shortcuts we don't want?
  1296. if (focused_widget->GetDisabled())
  1297. return false;
  1298. // Emulate a click on the focused widget when pressing space or enter
  1299. if (!modifierkeys && focused_widget->GetClickByKey() &&
  1300. !focused_widget->GetDisabled() &&
  1301. !focused_widget->GetIsDying() &&
  1302. (special_key == TB_KEY_ENTER || key == ' '))
  1303. {
  1304. // Set the pressed state while the key is down, if it
  1305. // didn't already have the pressed state.
  1306. static bool check_pressed_state = true;
  1307. static bool had_pressed_state = false;
  1308. if (down && check_pressed_state)
  1309. {
  1310. had_pressed_state = focused_widget->GetState(WIDGET_STATE_PRESSED);
  1311. check_pressed_state = false;
  1312. }
  1313. if (!down)
  1314. check_pressed_state = true;
  1315. if (!had_pressed_state)
  1316. {
  1317. focused_widget->SetState(WIDGET_STATE_PRESSED, down);
  1318. focused_widget->m_packed.has_key_pressed_state = down;
  1319. }
  1320. // Invoke the click event
  1321. if (!down)
  1322. {
  1323. TBWidgetEvent ev(EVENT_TYPE_CLICK, m_rect.w / 2, m_rect.h / 2, true);
  1324. focused_widget->InvokeEvent(ev);
  1325. }
  1326. handled = true;
  1327. }
  1328. else
  1329. {
  1330. // Invoke the key event on the focused widget
  1331. TBWidgetEvent ev(down ? EVENT_TYPE_KEY_DOWN : EVENT_TYPE_KEY_UP);
  1332. ev.key = key;
  1333. ev.special_key = special_key;
  1334. ev.modifierkeys = modifierkeys;
  1335. handled = focused_widget->InvokeEvent(ev);
  1336. }
  1337. }
  1338. // Move focus between widgets
  1339. if (down && !handled && special_key == TB_KEY_TAB)
  1340. {
  1341. handled = MoveFocus(!(modifierkeys & TB_SHIFT));
  1342. // Show the focus when we move it by keyboard
  1343. if (handled)
  1344. SetAutoFocusState(true);
  1345. }
  1346. return handled;
  1347. }
  1348. void TBWidget::ReleaseCapture()
  1349. {
  1350. if (this == captured_widget)
  1351. SetCapturedWidget(nullptr);
  1352. }
  1353. void TBWidget::ConvertToRoot(int &x, int &y) const
  1354. {
  1355. const TBWidget *tmp = this;
  1356. while (tmp->m_parent)
  1357. {
  1358. x += tmp->m_rect.x;
  1359. y += tmp->m_rect.y;
  1360. tmp = tmp->m_parent;
  1361. if (tmp)
  1362. {
  1363. int child_translation_x, child_translation_y;
  1364. tmp->GetChildTranslation(child_translation_x, child_translation_y);
  1365. x += child_translation_x;
  1366. y += child_translation_y;
  1367. }
  1368. }
  1369. }
  1370. void TBWidget::ConvertFromRoot(int &x, int &y) const
  1371. {
  1372. const TBWidget *tmp = this;
  1373. while (tmp->m_parent)
  1374. {
  1375. x -= tmp->m_rect.x;
  1376. y -= tmp->m_rect.y;
  1377. tmp = tmp->m_parent;
  1378. if (tmp)
  1379. {
  1380. int child_translation_x, child_translation_y;
  1381. tmp->GetChildTranslation(child_translation_x, child_translation_y);
  1382. x -= child_translation_x;
  1383. y -= child_translation_y;
  1384. }
  1385. }
  1386. }
  1387. // static
  1388. void TBWidget::SetHoveredWidget(TBWidget *widget, bool touch)
  1389. {
  1390. if (TBWidget::hovered_widget == widget)
  1391. return;
  1392. if (widget && widget->GetState(WIDGET_STATE_DISABLED))
  1393. return;
  1394. // We may apply hover state automatically so the widget might need to be updated.
  1395. if (TBWidget::hovered_widget)
  1396. {
  1397. TBWidget::hovered_widget->Invalidate();
  1398. TBWidget::hovered_widget->InvalidateSkinStates();
  1399. }
  1400. TBWidget::hovered_widget = widget;
  1401. if (TBWidget::hovered_widget)
  1402. {
  1403. TBWidget::hovered_widget->Invalidate();
  1404. TBWidget::hovered_widget->InvalidateSkinStates();
  1405. // Cursor based movement should set hover state automatically, but touch
  1406. // events should not (since touch doesn't really move unless pressed).
  1407. TBWidget::hovered_widget->m_packed.no_automatic_hover_state = touch;
  1408. }
  1409. }
  1410. // static
  1411. void TBWidget::SetCapturedWidget(TBWidget *widget)
  1412. {
  1413. if (TBWidget::captured_widget == widget)
  1414. return;
  1415. if (widget && widget->GetState(WIDGET_STATE_DISABLED))
  1416. return;
  1417. if (TBWidget::captured_widget)
  1418. {
  1419. // Stop panning when capture change (most likely changing to nullptr because of InvokePointerUp)
  1420. // Notify any active scroller so it may begin scrolling.
  1421. if (TBScroller *scroller = TBWidget::captured_widget->FindStartedScroller())
  1422. {
  1423. if (TBWidget::captured_widget->m_packed.is_panning)
  1424. scroller->OnPanReleased();
  1425. else
  1426. scroller->Stop();
  1427. }
  1428. TBWidget::captured_widget->m_packed.is_panning = false;
  1429. // We apply pressed state automatically so the widget might need to be updated.
  1430. TBWidget::captured_widget->Invalidate();
  1431. TBWidget::captured_widget->InvalidateSkinStates();
  1432. TBWidget::captured_widget->StopLongClickTimer();
  1433. }
  1434. cancel_click = false;
  1435. TBWidget *old_capture = TBWidget::captured_widget;
  1436. TBWidget::captured_widget = widget;
  1437. if (old_capture)
  1438. old_capture->OnCaptureChanged(false);
  1439. if (TBWidget::captured_widget)
  1440. {
  1441. TBWidget::captured_widget->Invalidate();
  1442. TBWidget::captured_widget->InvalidateSkinStates();
  1443. TBWidget::captured_widget->OnCaptureChanged(true);
  1444. }
  1445. }
  1446. bool TBWidget::SetFontDescription(const TBFontDescription &font_desc)
  1447. {
  1448. if (m_font_desc == font_desc)
  1449. return true;
  1450. // Set the font description only if we have a matching font, or succeed creating one.
  1451. if (g_font_manager->HasFontFace(font_desc))
  1452. m_font_desc = font_desc;
  1453. else if (g_font_manager->CreateFontFace(font_desc))
  1454. m_font_desc = font_desc;
  1455. else
  1456. return false;
  1457. InvokeFontChanged();
  1458. return true;
  1459. }
  1460. void TBWidget::InvokeFontChanged()
  1461. {
  1462. OnFontChanged();
  1463. // Recurse to children that inherit the font
  1464. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  1465. if (child->m_font_desc.GetFontFaceID() == 0)
  1466. child->InvokeFontChanged();
  1467. }
  1468. TBFontDescription TBWidget::GetCalculatedFontDescription() const
  1469. {
  1470. const TBWidget *tmp = this;
  1471. while (tmp)
  1472. {
  1473. if (tmp->m_font_desc.GetFontFaceID() != 0)
  1474. return tmp->m_font_desc;
  1475. tmp = tmp->m_parent;
  1476. }
  1477. return g_font_manager->GetDefaultFontDescription();
  1478. }
  1479. TBFontFace *TBWidget::GetFont() const
  1480. {
  1481. return g_font_manager->GetFontFace(GetCalculatedFontDescription());
  1482. }
  1483. }; // namespace tb