tb_widgets.cpp 61 KB

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