tb_widgets.cpp 60 KB

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