tb_widgets.cpp 56 KB

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