tb_widgets.cpp 60 KB

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