tb_widgets.cpp 56 KB

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