tb_widgets.cpp 56 KB

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