tb_widgets.cpp 56 KB

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