tb_widgets.cpp 63 KB

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