tb_widgets.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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. TBWidget *current = origin;
  548. while (current)
  549. {
  550. current = forward ? current->GetNextDeep() : current->GetPrevDeep();
  551. // Wrap around if we reach the end/beginning
  552. if (!current || !root->IsAncestorOf(current))
  553. current = forward ? root->GetFirstChild() : root->GetLastLeaf();
  554. // Break if we reached the origin again (we're not finding anything else)
  555. if (current == origin)
  556. break;
  557. // Try to focus what we found
  558. if (current && current->SetFocus(WIDGET_FOCUS_REASON_NAVIGATION))
  559. return true;
  560. }
  561. return false;
  562. }
  563. TBWidget *TBWidget::GetNextDeep(const TBWidget *bounding_ancestor) const
  564. {
  565. if (m_children.GetFirst())
  566. return GetFirstChild();
  567. for (const TBWidget *widget = this; widget != bounding_ancestor; widget = widget->m_parent)
  568. if (widget->next)
  569. return widget->GetNext();
  570. return nullptr;
  571. }
  572. TBWidget *TBWidget::GetPrevDeep() const
  573. {
  574. if (!prev)
  575. return m_parent;
  576. TBWidget *widget = GetPrev();
  577. while (widget->m_children.GetLast())
  578. widget = widget->GetLastChild();
  579. return widget;
  580. }
  581. TBWidget *TBWidget::GetLastLeaf() const
  582. {
  583. if (TBWidget *widget = GetLastChild())
  584. {
  585. while (widget->GetLastChild())
  586. widget = widget->GetLastChild();
  587. return widget;
  588. }
  589. return nullptr;
  590. }
  591. bool TBWidget::GetIsInteractable() const
  592. {
  593. return !(m_opacity == 0 || GetIgnoreInput() || GetState(WIDGET_STATE_DISABLED) ||
  594. GetIsDying() || GetVisibility() != WIDGET_VISIBILITY_VISIBLE);
  595. }
  596. WIDGET_HIT_STATUS TBWidget::GetHitStatus(int x, int y)
  597. {
  598. if (!GetIsInteractable())
  599. return WIDGET_HIT_STATUS_NO_HIT;
  600. return x >= 0 && y >= 0 && x < m_rect.w && y < m_rect.h ? WIDGET_HIT_STATUS_HIT : WIDGET_HIT_STATUS_NO_HIT;
  601. }
  602. TBWidget *TBWidget::GetWidgetAt(int x, int y, bool include_children) const
  603. {
  604. int child_translation_x, child_translation_y;
  605. GetChildTranslation(child_translation_x, child_translation_y);
  606. x -= child_translation_x;
  607. y -= child_translation_y;
  608. TBWidget *tmp = GetFirstChild();
  609. TBWidget *last_match = nullptr;
  610. while (tmp)
  611. {
  612. WIDGET_HIT_STATUS hit_status = tmp->GetHitStatus(x - tmp->m_rect.x, y - tmp->m_rect.y);
  613. if (hit_status)
  614. {
  615. if (include_children && hit_status != WIDGET_HIT_STATUS_HIT_NO_CHILDREN)
  616. {
  617. last_match = tmp->GetWidgetAt(x - tmp->m_rect.x, y - tmp->m_rect.y, include_children);
  618. if (!last_match)
  619. last_match = tmp;
  620. }
  621. else
  622. last_match = tmp;
  623. }
  624. tmp = tmp->GetNext();
  625. }
  626. return last_match;
  627. }
  628. TBWidget *TBWidget::GetChildFromIndex(int index) const
  629. {
  630. int i = 0;
  631. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  632. if (i++ == index)
  633. return child;
  634. return nullptr;
  635. }
  636. int TBWidget::GetIndexFromChild(TBWidget *child) const
  637. {
  638. assert(child->GetParent() == this);
  639. int i = 0;
  640. for (TBWidget *tmp = GetFirstChild(); tmp; tmp = tmp->GetNext(), i++)
  641. if (tmp == child)
  642. return i;
  643. return -1; ///< Should not happen!
  644. }
  645. bool TBWidget::IsAncestorOf(TBWidget *other_widget) const
  646. {
  647. while (other_widget)
  648. {
  649. if (other_widget == this)
  650. return true;
  651. other_widget = other_widget->m_parent;
  652. }
  653. return false;
  654. }
  655. bool TBWidget::IsEventDestinationFor(TBWidget *other_widget) const
  656. {
  657. while (other_widget)
  658. {
  659. if (other_widget == this)
  660. return true;
  661. other_widget = other_widget->GetEventDestination();
  662. }
  663. return false;
  664. }
  665. TBWidget *TBWidget::GetParentRoot()
  666. {
  667. TBWidget *tmp = this;
  668. while (tmp->m_parent)
  669. tmp = tmp->m_parent;
  670. return tmp;
  671. }
  672. TBWindow *TBWidget::GetParentWindow()
  673. {
  674. TBWidget *tmp = this;
  675. while (tmp && !tmp->IsOfType<TBWindow>())
  676. tmp = tmp->m_parent;
  677. return static_cast<TBWindow *>(tmp);
  678. }
  679. void TBWidget::AddListener(TBWidgetListener *listener)
  680. {
  681. m_listeners.AddLast(listener);
  682. }
  683. void TBWidget::RemoveListener(TBWidgetListener *listener)
  684. {
  685. m_listeners.Remove(listener);
  686. }
  687. bool TBWidget::HasListener(TBWidgetListener *listener) const
  688. {
  689. return m_listeners.ContainsLink(listener);
  690. }
  691. void TBWidget::OnPaintChildren(const PaintProps &paint_props)
  692. {
  693. if (!m_children.GetFirst())
  694. return;
  695. // Translate renderer with child translation
  696. int child_translation_x, child_translation_y;
  697. GetChildTranslation(child_translation_x, child_translation_y);
  698. g_renderer->Translate(child_translation_x, child_translation_y);
  699. TBRect clip_rect = g_renderer->GetClipRect();
  700. // Invoke paint on all children that are in the current visible rect.
  701. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  702. {
  703. if (clip_rect.Intersects(child->m_rect))
  704. child->InvokePaint(paint_props);
  705. }
  706. // Invoke paint of overlay elements on all children that are in the current visible rect.
  707. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  708. {
  709. if (clip_rect.Intersects(child->m_rect) && child->GetVisibility() == WIDGET_VISIBILITY_VISIBLE)
  710. {
  711. TBSkinElement *skin_element = child->GetSkinBgElement();
  712. if (skin_element && skin_element->HasOverlayElements())
  713. {
  714. // Update the renderer with the widgets opacity
  715. WIDGET_STATE state = child->GetAutoState();
  716. float old_opacity = g_renderer->GetOpacity();
  717. float opacity = old_opacity * child->CalculateOpacityInternal(state, skin_element);
  718. if (opacity > 0)
  719. {
  720. g_renderer->SetOpacity(opacity);
  721. TBWidgetSkinConditionContext context(child);
  722. g_tb_skin->PaintSkinOverlay(child->m_rect, skin_element, static_cast<SKIN_STATE>(state), context);
  723. g_renderer->SetOpacity(old_opacity);
  724. }
  725. }
  726. }
  727. }
  728. // Draw generic focus skin if the focused widget is one of the children, and the skin
  729. // doesn't have a skin state for focus which would already be painted.
  730. if (focused_widget && focused_widget->m_parent == this)
  731. {
  732. TBWidgetSkinConditionContext context(focused_widget);
  733. TBSkinElement *skin_element = focused_widget->GetSkinBgElement();
  734. if (!skin_element || !skin_element->HasState(SKIN_STATE_FOCUSED, context))
  735. {
  736. WIDGET_STATE state = focused_widget->GetAutoState();
  737. if (state & SKIN_STATE_FOCUSED)
  738. g_tb_skin->PaintSkin(focused_widget->m_rect, TBIDC("generic_focus"), static_cast<SKIN_STATE>(state), context);
  739. }
  740. }
  741. g_renderer->Translate(-child_translation_x, -child_translation_y);
  742. }
  743. void TBWidget::OnResized(int old_w, int old_h)
  744. {
  745. int dw = m_rect.w - old_w;
  746. int dh = m_rect.h - old_h;
  747. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  748. {
  749. if (child->GetVisibility() == WIDGET_VISIBILITY_GONE)
  750. continue;
  751. TBRect rect = child->m_rect;
  752. if ((child->m_gravity & WIDGET_GRAVITY_LEFT) && (child->m_gravity & WIDGET_GRAVITY_RIGHT))
  753. rect.w += dw;
  754. else if(child->m_gravity & WIDGET_GRAVITY_RIGHT)
  755. rect.x += dw;
  756. if ((child->m_gravity & WIDGET_GRAVITY_TOP) && (child->m_gravity & WIDGET_GRAVITY_BOTTOM))
  757. rect.h += dh;
  758. else if (child->m_gravity & WIDGET_GRAVITY_BOTTOM)
  759. rect.y += dh;
  760. child->SetRect(rect);
  761. }
  762. }
  763. void TBWidget::OnInflateChild(TBWidget *child)
  764. {
  765. if (child->GetVisibility() == WIDGET_VISIBILITY_GONE)
  766. return;
  767. // If the child pull towards only one edge (per axis), stick to that edge
  768. // and use the preferred size. Otherwise fill up all available space.
  769. TBRect padding_rect = GetPaddingRect();
  770. TBRect child_rect = padding_rect;
  771. WIDGET_GRAVITY gravity = child->GetGravity();
  772. bool fill_x = (gravity & WIDGET_GRAVITY_LEFT) && (gravity & WIDGET_GRAVITY_RIGHT);
  773. bool fill_y = (gravity & WIDGET_GRAVITY_TOP) && (gravity & WIDGET_GRAVITY_BOTTOM);
  774. if (!fill_x || !fill_y)
  775. {
  776. PreferredSize ps = child->GetPreferredSize();
  777. if (!fill_x)
  778. {
  779. child_rect.w = ps.pref_w;
  780. if (gravity & WIDGET_GRAVITY_RIGHT)
  781. child_rect.x = padding_rect.x + padding_rect.w - child_rect.w;
  782. }
  783. if (!fill_y)
  784. {
  785. child_rect.h = ps.pref_h;
  786. if (gravity & WIDGET_GRAVITY_BOTTOM)
  787. child_rect.y = padding_rect.y + padding_rect.h - child_rect.h;
  788. }
  789. }
  790. child->SetRect(child_rect);
  791. }
  792. TBRect TBWidget::GetPaddingRect()
  793. {
  794. TBRect padding_rect(0, 0, m_rect.w, m_rect.h);
  795. if (TBSkinElement *e = GetSkinBgElement())
  796. {
  797. padding_rect.x += e->padding_left;
  798. padding_rect.y += e->padding_top;
  799. padding_rect.w -= e->padding_left + e->padding_right;
  800. padding_rect.h -= e->padding_top + e->padding_bottom;
  801. }
  802. return padding_rect;
  803. }
  804. PreferredSize TBWidget::OnCalculatePreferredContentSize(const SizeConstraints &constraints)
  805. {
  806. // The default preferred size is calculated to satisfy the children
  807. // in the best way. Since this is the default, it's probably not a
  808. // layouting widget and children are resized purely by gravity.
  809. // Allow this widget a larger maximum if our gravity wants both ways,
  810. // otherwise don't grow more than the largest child.
  811. bool apply_max_w = !((m_gravity & WIDGET_GRAVITY_LEFT) && (m_gravity & WIDGET_GRAVITY_RIGHT));
  812. bool apply_max_h = !((m_gravity & WIDGET_GRAVITY_TOP) && (m_gravity & WIDGET_GRAVITY_BOTTOM));
  813. bool has_layouting_children = false;
  814. PreferredSize ps;
  815. TBSkinElement *bg_skin = GetSkinBgElement();
  816. int horizontal_padding = bg_skin ? bg_skin->padding_left + bg_skin->padding_right : 0;
  817. int vertical_padding = bg_skin ? bg_skin->padding_top + bg_skin->padding_bottom : 0;
  818. SizeConstraints inner_sc = constraints.ConstrainByPadding(horizontal_padding, vertical_padding);
  819. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  820. {
  821. if (child->GetVisibility() == WIDGET_VISIBILITY_GONE)
  822. continue;
  823. if (!has_layouting_children)
  824. {
  825. has_layouting_children = true;
  826. if (apply_max_w)
  827. ps.max_w = 0;
  828. if (apply_max_h)
  829. ps.max_h = 0;
  830. }
  831. PreferredSize child_ps = child->GetPreferredSize(inner_sc);
  832. ps.pref_w = MAX(ps.pref_w, child_ps.pref_w);
  833. ps.pref_h = MAX(ps.pref_h, child_ps.pref_h);
  834. ps.min_w = MAX(ps.min_w, child_ps.min_w);
  835. ps.min_h = MAX(ps.min_h, child_ps.min_h);
  836. if (apply_max_w)
  837. ps.max_w = MAX(ps.max_w, child_ps.max_w);
  838. if (apply_max_h)
  839. ps.max_h = MAX(ps.max_h, child_ps.max_h);
  840. ps.size_dependency |= child_ps.size_dependency;
  841. }
  842. return ps;
  843. }
  844. PreferredSize TBWidget::OnCalculatePreferredSize(const SizeConstraints &constraints)
  845. {
  846. PreferredSize ps = OnCalculatePreferredContentSize(constraints);
  847. assert(ps.pref_w >= ps.min_w);
  848. assert(ps.pref_h >= ps.min_h);
  849. if (TBSkinElement *e = GetSkinBgElement())
  850. {
  851. // Override the widgets preferences with skin attributes that has been specified.
  852. // If not set by the widget, calculate based on the intrinsic size of the skin.
  853. const int skin_intrinsic_w = e->GetIntrinsicWidth();
  854. if (e->GetPrefWidth() != SKIN_VALUE_NOT_SPECIFIED)
  855. ps.pref_w = e->GetPrefWidth();
  856. else if (ps.pref_w == 0 && skin_intrinsic_w != SKIN_VALUE_NOT_SPECIFIED)
  857. ps.pref_w = skin_intrinsic_w;
  858. else
  859. {
  860. // Grow by padding to get the preferred size from preferred content size.
  861. ps.min_w += e->padding_left + e->padding_right;
  862. ps.pref_w += e->padding_left + e->padding_right;
  863. }
  864. const int skin_intrinsic_h = e->GetIntrinsicHeight();
  865. if (e->GetPrefHeight() != SKIN_VALUE_NOT_SPECIFIED)
  866. ps.pref_h = e->GetPrefHeight();
  867. else if (ps.pref_h == 0 && skin_intrinsic_h != SKIN_VALUE_NOT_SPECIFIED)
  868. ps.pref_h = skin_intrinsic_h;
  869. else
  870. {
  871. // Grow by padding to get the preferred size from preferred content size.
  872. ps.min_h += e->padding_top + e->padding_bottom;
  873. ps.pref_h += e->padding_top + e->padding_bottom;
  874. }
  875. if (e->GetMinWidth() != SKIN_VALUE_NOT_SPECIFIED)
  876. ps.min_w = e->GetMinWidth();
  877. else
  878. ps.min_w = MAX(ps.min_w, e->GetIntrinsicMinWidth());
  879. if (e->GetMinHeight() != SKIN_VALUE_NOT_SPECIFIED)
  880. ps.min_h = e->GetMinHeight();
  881. else
  882. ps.min_h = MAX(ps.min_h, e->GetIntrinsicMinHeight());
  883. if (e->GetMaxWidth() != SKIN_VALUE_NOT_SPECIFIED)
  884. ps.max_w = e->GetMaxWidth();
  885. else
  886. ps.max_w += e->padding_left + e->padding_right;
  887. if (e->GetMaxHeight() != SKIN_VALUE_NOT_SPECIFIED)
  888. ps.max_h = e->GetMaxHeight();
  889. else
  890. ps.max_h += e->padding_top + e->padding_bottom;
  891. // Sanitize result
  892. ps.pref_w = MAX(ps.pref_w, ps.min_w);
  893. ps.pref_h = MAX(ps.pref_h, ps.min_h);
  894. }
  895. return ps;
  896. }
  897. PreferredSize TBWidget::GetPreferredSize(const SizeConstraints &in_constraints)
  898. {
  899. SizeConstraints constraints(in_constraints);
  900. if (m_layout_params)
  901. constraints = constraints.ConstrainByLayoutParams(*m_layout_params);
  902. // Returned cached result if valid and the constraints are the same.
  903. if (m_packed.is_cached_ps_valid)
  904. {
  905. if (m_cached_sc == constraints ||
  906. m_cached_ps.size_dependency == SIZE_DEP_NONE /*||
  907. // FIX: These optimizations would probably be good. Keeping
  908. // disabled for now because it needs testing.
  909. // If *only* width depend on height, only the height matter
  910. (m_cached_ps.size_dependency == SIZE_DEP_WIDTH_DEPEND_ON_HEIGHT &&
  911. m_cached_sc.available_h == constraints.available_h) ||
  912. // If *only* height depend on width, only the width matter
  913. (m_cached_ps.size_dependency == SIZE_DEP_HEIGHT_DEPEND_ON_WIDTH &&
  914. m_cached_sc.available_w == constraints.available_w)*/)
  915. {
  916. return m_cached_ps;
  917. }
  918. }
  919. // Measure and save to cache
  920. TB_IF_DEBUG_SETTING(LAYOUT_PS_DEBUGGING, last_measure_time = TBSystem::GetTimeMS());
  921. m_packed.is_cached_ps_valid = 1;
  922. m_cached_ps = OnCalculatePreferredSize(constraints);
  923. m_cached_sc = constraints;
  924. // Override the calculated ps with any specified layout parameter.
  925. if (m_layout_params)
  926. {
  927. #define LP_OVERRIDE(param) if (m_layout_params->param != LayoutParams::UNSPECIFIED) \
  928. m_cached_ps.param = m_layout_params->param;
  929. LP_OVERRIDE(min_w);
  930. LP_OVERRIDE(min_h);
  931. LP_OVERRIDE(max_w);
  932. LP_OVERRIDE(max_h);
  933. LP_OVERRIDE(pref_w);
  934. LP_OVERRIDE(pref_h);
  935. // Sanitize results
  936. m_cached_ps.max_w = MAX(m_cached_ps.max_w, m_cached_ps.min_w);
  937. m_cached_ps.max_h = MAX(m_cached_ps.max_h, m_cached_ps.min_h);
  938. m_cached_ps.pref_w = MAX(m_cached_ps.pref_w, m_cached_ps.min_w);
  939. m_cached_ps.pref_h = MAX(m_cached_ps.pref_h, m_cached_ps.min_h);
  940. }
  941. return m_cached_ps;
  942. }
  943. void TBWidget::SetLayoutParams(const LayoutParams &lp)
  944. {
  945. if (!m_layout_params)
  946. m_layout_params = new LayoutParams;
  947. if (!m_layout_params)
  948. return;
  949. *m_layout_params = lp;
  950. m_packed.is_cached_ps_valid = 0;
  951. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  952. }
  953. void TBWidget::InvalidateLayout(INVALIDATE_LAYOUT il)
  954. {
  955. m_packed.is_cached_ps_valid = 0;
  956. if (GetVisibility() == WIDGET_VISIBILITY_GONE)
  957. return;
  958. Invalidate();
  959. if (il == INVALIDATE_LAYOUT_RECURSIVE && m_parent)
  960. m_parent->InvalidateLayout(il);
  961. }
  962. void TBWidget::InvokeProcess()
  963. {
  964. InvokeSkinUpdatesInternal(false);
  965. InvokeProcessInternal();
  966. }
  967. void TBWidget::InvokeSkinUpdatesInternal(bool force_update)
  968. {
  969. if (!update_skin_states && !force_update)
  970. return;
  971. update_skin_states = false;
  972. // Check if the skin we get is different from what we expect. That might happen
  973. // if the skin has some strong override dependant a condition that has changed.
  974. // If that happens, call OnSkinChanged so the widget can react to that, and
  975. // invalidate layout to apply new skin properties.
  976. if (TBSkinElement *skin_elm = GetSkinBgElement())
  977. {
  978. if (skin_elm->id != m_skin_bg_expected)
  979. {
  980. OnSkinChanged();
  981. m_skin_bg_expected = skin_elm->id;
  982. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  983. }
  984. }
  985. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  986. child->InvokeSkinUpdatesInternal(true);
  987. }
  988. void TBWidget::InvokeProcessInternal()
  989. {
  990. OnProcess();
  991. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  992. child->InvokeProcessInternal();
  993. OnProcessAfterChildren();
  994. }
  995. void TBWidget::InvokeProcessStates(bool force_update)
  996. {
  997. if (!update_widget_states && !force_update)
  998. return;
  999. update_widget_states = false;
  1000. OnProcessStates();
  1001. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  1002. child->InvokeProcessStates(true);
  1003. }
  1004. float TBWidget::CalculateOpacityInternal(WIDGET_STATE state, TBSkinElement *skin_element) const
  1005. {
  1006. float opacity = m_opacity;
  1007. if (skin_element)
  1008. opacity *= skin_element->opacity;
  1009. if (state & WIDGET_STATE_DISABLED)
  1010. opacity *= g_tb_skin->GetDefaultDisabledOpacity();
  1011. return opacity;
  1012. }
  1013. void TBWidget::InvokePaint(const PaintProps &parent_paint_props)
  1014. {
  1015. // Don't paint invisible widgets
  1016. if (m_opacity == 0 || m_rect.IsEmpty() || GetVisibility() != WIDGET_VISIBILITY_VISIBLE)
  1017. return;
  1018. WIDGET_STATE state = GetAutoState();
  1019. TBSkinElement *skin_element = GetSkinBgElement();
  1020. // Multiply current opacity with widget opacity, skin opacity and state opacity.
  1021. float old_opacity = g_renderer->GetOpacity();
  1022. float opacity = old_opacity * CalculateOpacityInternal(state, skin_element);
  1023. if (opacity == 0)
  1024. return;
  1025. // FIX: This does not give the correct result! Must use a new render target!
  1026. g_renderer->SetOpacity(opacity);
  1027. int trns_x = m_rect.x, trns_y = m_rect.y;
  1028. g_renderer->Translate(trns_x, trns_y);
  1029. // Paint background skin
  1030. TBRect local_rect(0, 0, m_rect.w, m_rect.h);
  1031. TBWidgetSkinConditionContext context(this);
  1032. TBSkinElement *used_element = g_tb_skin->PaintSkin(local_rect, skin_element, static_cast<SKIN_STATE>(state), context);
  1033. assert(!!used_element == !!skin_element);
  1034. TB_IF_DEBUG_SETTING(LAYOUT_BOUNDS, g_renderer->DrawRect(local_rect, TBColor(255, 255, 255, 50)));
  1035. // Inherit properties from parent if not specified in the used skin for this widget.
  1036. PaintProps paint_props = parent_paint_props;
  1037. if (used_element && used_element->text_color != 0)
  1038. paint_props.text_color = used_element->text_color;
  1039. // Paint content
  1040. OnPaint(paint_props);
  1041. if (used_element)
  1042. g_renderer->Translate(used_element->content_ofs_x, used_element->content_ofs_y);
  1043. // Paint children
  1044. OnPaintChildren(paint_props);
  1045. #ifdef TB_RUNTIME_DEBUG_INFO
  1046. if (TB_DEBUG_SETTING(LAYOUT_PS_DEBUGGING))
  1047. {
  1048. // Layout debug painting. Paint recently layouted widgets with red and
  1049. // recently measured widgets with yellow.
  1050. // Invalidate to keep repainting until we've timed out (so it's removed).
  1051. const double debug_time = 300;
  1052. const double now = TBSystem::GetTimeMS();
  1053. if (now < last_layout_time + debug_time)
  1054. {
  1055. g_renderer->DrawRect(local_rect, TBColor(255, 30, 30, 200));
  1056. Invalidate();
  1057. }
  1058. if (now < last_measure_time + debug_time)
  1059. {
  1060. g_renderer->DrawRect(local_rect.Shrink(1, 1), TBColor(255, 255, 30, 200));
  1061. Invalidate();
  1062. }
  1063. }
  1064. #endif // TB_RUNTIME_DEBUG_INFO
  1065. if (used_element)
  1066. g_renderer->Translate(-used_element->content_ofs_x, -used_element->content_ofs_y);
  1067. g_renderer->Translate(-trns_x, -trns_y);
  1068. g_renderer->SetOpacity(old_opacity);
  1069. }
  1070. bool TBWidget::InvokeEvent(TBWidgetEvent &ev)
  1071. {
  1072. ev.target = this;
  1073. // First call the global listener about this event.
  1074. // Who knows, maybe some listener will block the event or cause us
  1075. // to be deleted.
  1076. TBWidgetSafePointer this_widget(this);
  1077. if (TBWidgetListener::InvokeWidgetInvokeEvent(this, ev))
  1078. return true;
  1079. if (!this_widget.Get())
  1080. return true; // We got removed so we actually handled this event.
  1081. if (ev.type == EVENT_TYPE_CHANGED)
  1082. {
  1083. InvalidateSkinStates();
  1084. m_connection.SyncFromWidget(this);
  1085. }
  1086. if (!this_widget.Get())
  1087. return true; // We got removed so we actually handled this event.
  1088. // Always update states after some event types.
  1089. switch (ev.type)
  1090. {
  1091. case EVENT_TYPE_CLICK:
  1092. case EVENT_TYPE_LONG_CLICK:
  1093. case EVENT_TYPE_CHANGED:
  1094. case EVENT_TYPE_KEY_DOWN:
  1095. case EVENT_TYPE_KEY_UP:
  1096. InvalidateStates();
  1097. default:
  1098. break;
  1099. };
  1100. // Call OnEvent on this widgets and travel up through its parents if not handled.
  1101. bool handled = false;
  1102. TBWidget *tmp = this;
  1103. while (tmp && !(handled = tmp->OnEvent(ev)))
  1104. tmp = tmp->GetEventDestination();
  1105. return handled;
  1106. }
  1107. void TBWidget::StartLongClickTimer(bool touch)
  1108. {
  1109. StopLongClickTimer();
  1110. m_long_click_timer = new TBLongClickTimer(this, touch);
  1111. }
  1112. void TBWidget::StopLongClickTimer()
  1113. {
  1114. if (!m_long_click_timer)
  1115. return;
  1116. delete m_long_click_timer;
  1117. m_long_click_timer = nullptr;
  1118. }
  1119. void TBWidget::InvokePointerDown(int x, int y, int click_count, MODIFIER_KEYS modifierkeys, bool touch, int touchId)
  1120. {
  1121. TBWidget* down_widget = GetWidgetAt(x, y, true);
  1122. if (!captured_widget && down_widget && down_widget->needCapturing_)
  1123. {
  1124. SetCapturedWidget(down_widget);
  1125. SetHoveredWidget(captured_widget, touch);
  1126. //captured_button = button;
  1127. // Hide focus when we use the pointer, if it's not on the focused widget.
  1128. if (focused_widget != captured_widget)
  1129. SetAutoFocusState(false);
  1130. // Start long click timer. Only for touch events for now.
  1131. if (touch && captured_widget && captured_widget->GetWantLongClick())
  1132. captured_widget->StartLongClickTimer(touch);
  1133. // Get the closest parent window and bring it to the top
  1134. TBWindow *window = captured_widget ? captured_widget->GetParentWindow() : nullptr;
  1135. if (window)
  1136. window->Activate();
  1137. }
  1138. if (captured_widget)
  1139. {
  1140. // Check if there's any started scroller that should be stopped.
  1141. TBWidget *tmp = captured_widget;
  1142. while (tmp)
  1143. {
  1144. if (tmp->m_scroller && tmp->m_scroller->IsStarted())
  1145. {
  1146. // When we touch down to stop a scroller, we don't
  1147. // want the touch to end up causing a click.
  1148. cancel_click = true;
  1149. tmp->m_scroller->Stop();
  1150. break;
  1151. }
  1152. tmp = tmp->GetParent();
  1153. }
  1154. // Focus the captured widget or the closest
  1155. // focusable parent if it isn't focusable.
  1156. TBWidget *focus_target = captured_widget;
  1157. while (focus_target)
  1158. {
  1159. if (focus_target->SetFocus(WIDGET_FOCUS_REASON_POINTER))
  1160. break;
  1161. focus_target = focus_target->m_parent;
  1162. }
  1163. }
  1164. //save x and y to restore it later
  1165. int ox = x;
  1166. int oy = y;
  1167. //check only for captured_widget
  1168. if (captured_widget)
  1169. {
  1170. captured_widget->SetTouchId(touchId);
  1171. //converts x, y
  1172. captured_widget->ConvertFromRoot(x, y);
  1173. pointer_move_widget_x = pointer_down_widget_x = x;
  1174. pointer_move_widget_y = pointer_down_widget_y = y;
  1175. TBWidgetEvent ev(EVENT_TYPE_POINTER_DOWN, x, y, touch, modifierkeys);
  1176. ev.count = click_count;
  1177. captured_widget->InvokeEvent(ev);
  1178. }
  1179. //restore x and y coords, to use with down_widget
  1180. x = ox;
  1181. y = oy;
  1182. //if down_widget is captured and it's not a captured_widget
  1183. //then send an event, otherwise don't send it because POINTER_DOWN event will be sent twice
  1184. if (down_widget && down_widget != captured_widget)
  1185. {
  1186. //the same things that's done with captured_widget
  1187. down_widget->Invalidate();
  1188. down_widget->InvalidateSkinStates();
  1189. down_widget->OnCaptureChanged(true);
  1190. down_widget->SetTouchId(touchId);
  1191. //convert x, y
  1192. down_widget->ConvertFromRoot(x, y);
  1193. TBWidgetEvent ev(EVENT_TYPE_POINTER_DOWN, x, y, touch, modifierkeys);
  1194. ev.count = click_count;
  1195. down_widget->InvokeEvent(ev);
  1196. }
  1197. }
  1198. void TBWidget::InvokePointerUp(int x, int y, MODIFIER_KEYS modifierkeys, bool touch, int touchId)
  1199. {
  1200. //save x and y to restore it later
  1201. int ox = x;
  1202. int oy = y;
  1203. //save old_capture for later check, because captured_widget can be nullptr after releasing capture
  1204. TBWidget *old_capture = captured_widget;
  1205. if (captured_widget && captured_widget->touchId_ == touchId)
  1206. {
  1207. captured_widget->ConvertFromRoot(x, y);
  1208. TBWidgetEvent ev_up(EVENT_TYPE_POINTER_UP, x, y, touch, modifierkeys);
  1209. TBWidgetEvent ev_click(EVENT_TYPE_CLICK, x, y, touch, modifierkeys);
  1210. captured_widget->InvokeEvent(ev_up);
  1211. if (!cancel_click && captured_widget->GetHitStatus(x, y))
  1212. {
  1213. captured_widget->InvokeEvent(ev_click);
  1214. }
  1215. captured_widget->ReleaseCapture();
  1216. }
  1217. //restore x and y coords, to use with down_widget
  1218. x = ox;
  1219. y = oy;
  1220. TBWidget* down_widget = GetWidgetAt(x, y, true);
  1221. //make sure that down_widget is not captured_widget to don't sent event twice
  1222. if (down_widget && down_widget->touchId_ == touchId && old_capture != down_widget)
  1223. {
  1224. down_widget->ConvertFromRoot(x, y);
  1225. TBWidgetEvent ev_up(EVENT_TYPE_POINTER_UP, x, y, touch, modifierkeys);
  1226. TBWidgetEvent ev_click(EVENT_TYPE_CLICK, x, y, touch, modifierkeys);
  1227. down_widget->InvokeEvent(ev_up);
  1228. if (!cancel_click && down_widget->GetHitStatus(x, y))
  1229. {
  1230. down_widget->InvokeEvent(ev_click);
  1231. }
  1232. //ReleaseCapture
  1233. down_widget->Invalidate();
  1234. down_widget->InvalidateSkinStates();
  1235. down_widget->OnCaptureChanged(false);
  1236. }
  1237. }
  1238. void TBWidget::InvokeRightPointerDown(int x, int y, int click_count, MODIFIER_KEYS modifierkeys)
  1239. {
  1240. // note we're not capturing the widget as with normal pointer down
  1241. TBWidget* widget = GetWidgetAt(x, y, true);
  1242. if (!widget)
  1243. return;
  1244. TBWidgetEvent ev(EVENT_TYPE_RIGHT_POINTER_DOWN, x, y, false, modifierkeys);
  1245. ev.count = click_count;
  1246. widget->InvokeEvent(ev);
  1247. }
  1248. void TBWidget::InvokeRightPointerUp(int x, int y, MODIFIER_KEYS modifierkeys)
  1249. {
  1250. // note we're not capturing the widget as with normal pointer down
  1251. TBWidget* widget = GetWidgetAt(x, y, true);
  1252. if (!widget)
  1253. return;
  1254. TBWidgetEvent ev(EVENT_TYPE_RIGHT_POINTER_UP, x, y, false, modifierkeys);
  1255. ev.target = widget;
  1256. widget->InvokeEvent(ev);
  1257. }
  1258. void TBWidget::MaybeInvokeLongClickOrContextMenu(bool touch)
  1259. {
  1260. StopLongClickTimer();
  1261. if (captured_widget == this &&
  1262. !cancel_click &&
  1263. captured_widget->GetHitStatus(pointer_move_widget_x, pointer_move_widget_y))
  1264. {
  1265. // Invoke long click
  1266. TBWidgetEvent ev_long_click(EVENT_TYPE_LONG_CLICK, pointer_move_widget_x, pointer_move_widget_y, touch, TB_MODIFIER_NONE);
  1267. bool handled = captured_widget->InvokeEvent(ev_long_click);
  1268. if (!handled)
  1269. {
  1270. // Long click not handled so invoke a context menu event instead
  1271. TBWidgetEvent ev_context_menu(EVENT_TYPE_CONTEXT_MENU, pointer_move_widget_x, pointer_move_widget_y, touch, TB_MODIFIER_NONE);
  1272. handled = captured_widget->InvokeEvent(ev_context_menu);
  1273. }
  1274. // If any event was handled, suppress click when releasing pointer.
  1275. if (handled)
  1276. cancel_click = true;
  1277. }
  1278. }
  1279. void TBWidget::InvokePointerMove(int x, int y, MODIFIER_KEYS modifierkeys, bool touch, int touchId)
  1280. {
  1281. TBWidget* move = GetWidgetByTouchId(touchId);
  1282. if (move && move->IsCaptured())
  1283. {
  1284. int cx = x;
  1285. int cy = y;
  1286. move->ConvertFromRoot(cx, cy);
  1287. if (!move->GetHitStatus(cx, cy))
  1288. {
  1289. move->Invalidate();
  1290. move->InvalidateSkinStates();
  1291. move->OnCaptureChanged(false);
  1292. TBWidgetEvent ev_up(EVENT_TYPE_POINTER_UP, cx, cy, touch, TB_MODIFIER_NONE);
  1293. move->InvokeEvent(ev_up);
  1294. }
  1295. }
  1296. SetHoveredWidget(GetWidgetAt(x, y, true), touch);
  1297. TBWidget *target = captured_widget ? captured_widget : hovered_widget;
  1298. if (target)
  1299. {
  1300. target->ConvertFromRoot(x, y);
  1301. pointer_move_widget_x = x;
  1302. pointer_move_widget_y = y;
  1303. TBWidgetEvent ev(EVENT_TYPE_POINTER_MOVE, x, y, touch, modifierkeys);
  1304. if (target->InvokeEvent(ev))
  1305. return;
  1306. // The move event was not handled, so handle panning of scrollable widgets.
  1307. HandlePanningOnMove(x, y);
  1308. }
  1309. }
  1310. void TBWidget::HandlePanningOnMove(int x, int y)
  1311. {
  1312. if (!captured_widget)
  1313. return;
  1314. // Check pointer movement
  1315. const int dx = pointer_down_widget_x - x;
  1316. const int dy = pointer_down_widget_y - y;
  1317. const int threshold = TBSystem::GetPanThreshold();
  1318. const bool maybe_start_panning_x = ABS(dx) >= threshold;
  1319. const bool maybe_start_panning_y = ABS(dy) >= threshold;
  1320. // Do panning, or attempt starting panning (we don't know if any widget is scrollable yet)
  1321. if (captured_widget->m_packed.is_panning || maybe_start_panning_x || maybe_start_panning_y)
  1322. {
  1323. // The threshold is met for not invoking any long click
  1324. captured_widget->StopLongClickTimer();
  1325. int start_compensation_x = 0, start_compensation_y = 0;
  1326. if (!captured_widget->m_packed.is_panning)
  1327. {
  1328. // When we start panning, deduct the extra distance caused by the
  1329. // start threshold from the delta so we don't start with a sudden jump.
  1330. int extra = threshold - 1;
  1331. if (maybe_start_panning_x)
  1332. start_compensation_x = dx < 0 ? extra : -extra;
  1333. if (maybe_start_panning_y)
  1334. start_compensation_y = dy < 0 ? extra : -extra;
  1335. }
  1336. // Get any active scroller and feed it with pan actions.
  1337. TBScroller *scroller = captured_widget->GetReadyScroller(dx != 0, dy != 0);
  1338. if (!scroller)
  1339. return;
  1340. int old_translation_x = 0, old_translation_y = 0;
  1341. captured_widget->GetScrollRoot()->GetChildTranslation(old_translation_x, old_translation_y);
  1342. if (scroller->OnPan(dx + start_compensation_x, dy + start_compensation_y))
  1343. {
  1344. // Scroll delta changed, so we are now panning!
  1345. captured_widget->m_packed.is_panning = true;
  1346. cancel_click = true;
  1347. // If the captured widget (or its scroll root) has panned, we have to compensate the
  1348. // pointer down coordinates so we won't accumulate the difference the following pan.
  1349. int new_translation_x = 0, new_translation_y = 0;
  1350. captured_widget->GetScrollRoot()->GetChildTranslation(new_translation_x, new_translation_y);
  1351. pointer_down_widget_x += new_translation_x - old_translation_x + start_compensation_x;
  1352. pointer_down_widget_y += new_translation_y - old_translation_y + start_compensation_y;
  1353. }
  1354. }
  1355. }
  1356. void TBWidget::InvokeWheel(int x, int y, int delta_x, int delta_y, MODIFIER_KEYS modifierkeys)
  1357. {
  1358. SetHoveredWidget(GetWidgetAt(x, y, true), true);
  1359. TBWidget *target = captured_widget ? captured_widget : hovered_widget;
  1360. if (target)
  1361. {
  1362. target->ConvertFromRoot(x, y);
  1363. pointer_move_widget_x = x;
  1364. pointer_move_widget_y = y;
  1365. TBWidgetEvent ev(EVENT_TYPE_WHEEL, x, y, true, modifierkeys);
  1366. ev.delta_x = delta_x;
  1367. ev.delta_y = delta_y;
  1368. target->InvokeEvent(ev);
  1369. }
  1370. }
  1371. bool TBWidget::InvokeKey(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
  1372. {
  1373. bool handled = false;
  1374. if (focused_widget)
  1375. {
  1376. // ATOMIC: This may have some side effects for shortcuts we don't want?
  1377. if (focused_widget->GetDisabled())
  1378. return false;
  1379. // Emulate a click on the focused widget when pressing space or enter
  1380. if (!modifierkeys && focused_widget->GetClickByKey() &&
  1381. !focused_widget->GetDisabled() &&
  1382. !focused_widget->GetIsDying() &&
  1383. (special_key == TB_KEY_ENTER || key == ' '))
  1384. {
  1385. // Set the pressed state while the key is down, if it
  1386. // didn't already have the pressed state.
  1387. static bool check_pressed_state = true;
  1388. static bool had_pressed_state = false;
  1389. if (down && check_pressed_state)
  1390. {
  1391. had_pressed_state = focused_widget->GetState(WIDGET_STATE_PRESSED);
  1392. check_pressed_state = false;
  1393. }
  1394. if (!down)
  1395. check_pressed_state = true;
  1396. if (!had_pressed_state)
  1397. {
  1398. focused_widget->SetState(WIDGET_STATE_PRESSED, down);
  1399. focused_widget->m_packed.has_key_pressed_state = down;
  1400. }
  1401. // Invoke the click event
  1402. if (!down)
  1403. {
  1404. TBWidgetEvent ev(EVENT_TYPE_CLICK, m_rect.w / 2, m_rect.h / 2, true);
  1405. focused_widget->InvokeEvent(ev);
  1406. }
  1407. handled = true;
  1408. }
  1409. else
  1410. {
  1411. // Invoke the key event on the focused widget
  1412. TBWidgetEvent ev(down ? EVENT_TYPE_KEY_DOWN : EVENT_TYPE_KEY_UP);
  1413. ev.key = key;
  1414. ev.special_key = special_key;
  1415. ev.modifierkeys = modifierkeys;
  1416. handled = focused_widget->InvokeEvent(ev);
  1417. }
  1418. }
  1419. // Move focus between widgets
  1420. if (down && !handled && special_key == TB_KEY_TAB)
  1421. {
  1422. handled = MoveFocus(!(modifierkeys & TB_SHIFT));
  1423. // Show the focus when we move it by keyboard
  1424. if (handled)
  1425. SetAutoFocusState(true);
  1426. }
  1427. return handled;
  1428. }
  1429. void TBWidget::ReleaseCapture()
  1430. {
  1431. if (this == captured_widget)
  1432. SetCapturedWidget(nullptr);
  1433. }
  1434. void TBWidget::ConvertToRoot(int &x, int &y) const
  1435. {
  1436. const TBWidget *tmp = this;
  1437. while (tmp->m_parent)
  1438. {
  1439. x += tmp->m_rect.x;
  1440. y += tmp->m_rect.y;
  1441. tmp = tmp->m_parent;
  1442. if (tmp)
  1443. {
  1444. int child_translation_x, child_translation_y;
  1445. tmp->GetChildTranslation(child_translation_x, child_translation_y);
  1446. x += child_translation_x;
  1447. y += child_translation_y;
  1448. }
  1449. }
  1450. }
  1451. void TBWidget::ConvertFromRoot(int &x, int &y) const
  1452. {
  1453. const TBWidget *tmp = this;
  1454. while (tmp->m_parent)
  1455. {
  1456. x -= tmp->m_rect.x;
  1457. y -= tmp->m_rect.y;
  1458. tmp = tmp->m_parent;
  1459. if (tmp)
  1460. {
  1461. int child_translation_x, child_translation_y;
  1462. tmp->GetChildTranslation(child_translation_x, child_translation_y);
  1463. x -= child_translation_x;
  1464. y -= child_translation_y;
  1465. }
  1466. }
  1467. }
  1468. // static
  1469. void TBWidget::SetHoveredWidget(TBWidget *widget, bool touch)
  1470. {
  1471. if (TBWidget::hovered_widget == widget)
  1472. return;
  1473. if (widget && widget->GetState(WIDGET_STATE_DISABLED))
  1474. return;
  1475. // We may apply hover state automatically so the widget might need to be updated.
  1476. if (TBWidget::hovered_widget)
  1477. {
  1478. TBWidget::hovered_widget->Invalidate();
  1479. TBWidget::hovered_widget->InvalidateSkinStates();
  1480. }
  1481. TBWidget::hovered_widget = widget;
  1482. if (TBWidget::hovered_widget)
  1483. {
  1484. TBWidget::hovered_widget->Invalidate();
  1485. TBWidget::hovered_widget->InvalidateSkinStates();
  1486. // Cursor based movement should set hover state automatically, but touch
  1487. // events should not (since touch doesn't really move unless pressed).
  1488. TBWidget::hovered_widget->m_packed.no_automatic_hover_state = touch;
  1489. }
  1490. }
  1491. // static
  1492. void TBWidget::SetCapturedWidget(TBWidget *widget)
  1493. {
  1494. if (TBWidget::captured_widget == widget)
  1495. return;
  1496. if (widget && widget->GetState(WIDGET_STATE_DISABLED))
  1497. return;
  1498. if (TBWidget::captured_widget)
  1499. {
  1500. // Stop panning when capture change (most likely changing to nullptr because of InvokePointerUp)
  1501. // Notify any active scroller so it may begin scrolling.
  1502. if (TBScroller *scroller = TBWidget::captured_widget->FindStartedScroller())
  1503. {
  1504. if (TBWidget::captured_widget->m_packed.is_panning)
  1505. scroller->OnPanReleased();
  1506. else
  1507. scroller->Stop();
  1508. }
  1509. TBWidget::captured_widget->m_packed.is_panning = false;
  1510. // We apply pressed state automatically so the widget might need to be updated.
  1511. TBWidget::captured_widget->Invalidate();
  1512. TBWidget::captured_widget->InvalidateSkinStates();
  1513. TBWidget::captured_widget->StopLongClickTimer();
  1514. }
  1515. cancel_click = false;
  1516. TBWidget *old_capture = TBWidget::captured_widget;
  1517. TBWidget::captured_widget = widget;
  1518. if (old_capture)
  1519. old_capture->OnCaptureChanged(false);
  1520. if (TBWidget::captured_widget)
  1521. {
  1522. TBWidget::captured_widget->Invalidate();
  1523. TBWidget::captured_widget->InvalidateSkinStates();
  1524. TBWidget::captured_widget->OnCaptureChanged(true);
  1525. }
  1526. }
  1527. bool TBWidget::SetFontDescription(const TBFontDescription &font_desc)
  1528. {
  1529. if (m_font_desc == font_desc)
  1530. return true;
  1531. // Set the font description only if we have a matching font, or succeed creating one.
  1532. if (g_font_manager->HasFontFace(font_desc))
  1533. m_font_desc = font_desc;
  1534. else if (g_font_manager->CreateFontFace(font_desc))
  1535. m_font_desc = font_desc;
  1536. else
  1537. return false;
  1538. InvokeFontChanged();
  1539. return true;
  1540. }
  1541. void TBWidget::InvokeFontChanged()
  1542. {
  1543. OnFontChanged();
  1544. // Recurse to children that inherit the font
  1545. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  1546. if (child->m_font_desc.GetFontFaceID() == 0)
  1547. child->InvokeFontChanged();
  1548. }
  1549. TBFontDescription TBWidget::GetCalculatedFontDescription() const
  1550. {
  1551. const TBWidget *tmp = this;
  1552. while (tmp)
  1553. {
  1554. if (tmp->m_font_desc.GetFontFaceID() != 0)
  1555. return tmp->m_font_desc;
  1556. tmp = tmp->m_parent;
  1557. }
  1558. return g_font_manager->GetDefaultFontDescription();
  1559. }
  1560. TBFontFace *TBWidget::GetFont() const
  1561. {
  1562. return g_font_manager->GetFontFace(GetCalculatedFontDescription());
  1563. }
  1564. }; // namespace tb