tb_widgets.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  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. /// searches for specified widget ID from the top of the widget tree, returns the 1st one found.
  709. TBWidget *TBWidget::FindWidget ( TBID searchid )
  710. {
  711. TBWidget *rootwidget = GetParentRoot(true);
  712. if(rootwidget)
  713. return rootwidget->GetWidgetByID( searchid );
  714. return NULL;
  715. }
  716. /// return all of the widgets of the specified classname
  717. void TBWidget::SearchWidgetClass ( TBStr className, TBValue &results )
  718. {
  719. if ( className.Equals(GetClassName()) )
  720. {
  721. TBValue *new_val = results.GetArray()->AddValue();
  722. new_val->SetObject(this);
  723. }
  724. int num = numChildren();
  725. for (int ii = 0; ii < num; ii++)
  726. {
  727. TBWidget *child = GetChildFromIndex(ii);
  728. child->SearchWidgetClass( className, results);
  729. }
  730. }
  731. /// return all of the widgets of the specified id
  732. void TBWidget::SearchWidgetId ( TBID searchId, TBValue &results )
  733. {
  734. if ( searchId == GetID() )
  735. {
  736. TBValue *new_val = results.GetArray()->AddValue();
  737. new_val->SetObject(this);
  738. }
  739. int num = numChildren();
  740. for (int ii = 0; ii < num; ii++)
  741. {
  742. TBWidget *child = GetChildFromIndex(ii);
  743. child->SearchWidgetId( searchId, results);
  744. }
  745. }
  746. /// return all of the widgets with the specified text
  747. void TBWidget::SearchWidgetText ( TBStr searchText, TBValue &results )
  748. {
  749. if ( searchText.Equals(GetText()) )
  750. {
  751. TBValue *new_val = results.GetArray()->AddValue();
  752. new_val->SetObject(this);
  753. }
  754. int num = numChildren();
  755. for (int ii = 0; ii < num; ii++)
  756. {
  757. TBWidget *child = GetChildFromIndex(ii);
  758. child->SearchWidgetText( searchText, results);
  759. }
  760. }
  761. // ATOMIC END
  762. TBWidget *TBWidget::GetChildFromIndex(int index) const
  763. {
  764. int i = 0;
  765. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  766. if (i++ == index)
  767. return child;
  768. return nullptr;
  769. }
  770. int TBWidget::GetIndexFromChild(TBWidget *child) const
  771. {
  772. assert(child->GetParent() == this);
  773. int i = 0;
  774. for (TBWidget *tmp = GetFirstChild(); tmp; tmp = tmp->GetNext(), i++)
  775. if (tmp == child)
  776. return i;
  777. return -1; ///< Should not happen!
  778. }
  779. bool TBWidget::IsAncestorOf(TBWidget *other_widget) const
  780. {
  781. while (other_widget)
  782. {
  783. if (other_widget == this)
  784. return true;
  785. other_widget = other_widget->m_parent;
  786. }
  787. return false;
  788. }
  789. bool TBWidget::IsEventDestinationFor(TBWidget *other_widget) const
  790. {
  791. while (other_widget)
  792. {
  793. if (other_widget == this)
  794. return true;
  795. other_widget = other_widget->GetEventDestination();
  796. }
  797. return false;
  798. }
  799. TBWidget *TBWidget::GetParentRoot(bool view_root)
  800. {
  801. TBWidget *tmp = this;
  802. while (tmp->m_parent)
  803. {
  804. if (view_root && !tmp->m_parent->m_parent)
  805. {
  806. return tmp;
  807. }
  808. tmp = tmp->m_parent;
  809. }
  810. return tmp;
  811. }
  812. TBWindow *TBWidget::GetParentWindow()
  813. {
  814. TBWidget *tmp = this;
  815. while (tmp && !tmp->IsOfType<TBWindow>())
  816. tmp = tmp->m_parent;
  817. return static_cast<TBWindow *>(tmp);
  818. }
  819. void TBWidget::AddListener(TBWidgetListener *listener)
  820. {
  821. m_listeners.AddLast(listener);
  822. }
  823. void TBWidget::RemoveListener(TBWidgetListener *listener)
  824. {
  825. m_listeners.Remove(listener);
  826. }
  827. bool TBWidget::HasListener(TBWidgetListener *listener) const
  828. {
  829. return m_listeners.ContainsLink(listener);
  830. }
  831. void TBWidget::OnPaintChildren(const PaintProps &paint_props)
  832. {
  833. if (!m_children.GetFirst())
  834. return;
  835. // Translate renderer with child translation
  836. int child_translation_x, child_translation_y;
  837. GetChildTranslation(child_translation_x, child_translation_y);
  838. g_renderer->Translate(child_translation_x, child_translation_y);
  839. TBRect clip_rect = g_renderer->GetClipRect();
  840. // Invoke paint on all children that are in the current visible rect.
  841. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  842. {
  843. if (clip_rect.Intersects(child->m_rect))
  844. child->InvokePaint(paint_props);
  845. }
  846. // Invoke paint of overlay elements on all children that are in the current visible rect.
  847. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  848. {
  849. if (clip_rect.Intersects(child->m_rect) && child->GetVisibility() == WIDGET_VISIBILITY_VISIBLE)
  850. {
  851. TBSkinElement *skin_element = child->GetSkinBgElement();
  852. if (skin_element && skin_element->HasOverlayElements())
  853. {
  854. // Update the renderer with the widgets opacity
  855. WIDGET_STATE state = child->GetAutoState();
  856. float old_opacity = g_renderer->GetOpacity();
  857. float opacity = old_opacity * child->CalculateOpacityInternal(state, skin_element);
  858. if (opacity > 0)
  859. {
  860. g_renderer->SetOpacity(opacity);
  861. TBWidgetSkinConditionContext context(child);
  862. g_tb_skin->PaintSkinOverlay(child->m_rect, skin_element, static_cast<SKIN_STATE>(state), context);
  863. g_renderer->SetOpacity(old_opacity);
  864. }
  865. }
  866. }
  867. }
  868. // Draw generic focus skin if the focused widget is one of the children, and the skin
  869. // doesn't have a skin state for focus which would already be painted.
  870. if (focused_widget && focused_widget->m_parent == this)
  871. {
  872. TBWidgetSkinConditionContext context(focused_widget);
  873. TBSkinElement *skin_element = focused_widget->GetSkinBgElement();
  874. if (!skin_element || !skin_element->HasState(SKIN_STATE_FOCUSED, context))
  875. {
  876. WIDGET_STATE state = focused_widget->GetAutoState();
  877. if (state & SKIN_STATE_FOCUSED)
  878. g_tb_skin->PaintSkin(focused_widget->m_rect, TBIDC("generic_focus"), static_cast<SKIN_STATE>(state), context);
  879. }
  880. }
  881. g_renderer->Translate(-child_translation_x, -child_translation_y);
  882. }
  883. void TBWidget::OnResized(int old_w, int old_h)
  884. {
  885. // ATOMIC BEGIN So pure UIWidgets can handle resize
  886. if (GetDelegate()) { GetDelegate()->OnResized(old_w, old_h); }
  887. // ATOMIC END
  888. int dw = m_rect.w - old_w;
  889. int dh = m_rect.h - old_h;
  890. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  891. {
  892. if (child->GetVisibility() == WIDGET_VISIBILITY_GONE)
  893. continue;
  894. TBRect rect = child->m_rect;
  895. if ((child->m_gravity & WIDGET_GRAVITY_LEFT) && (child->m_gravity & WIDGET_GRAVITY_RIGHT))
  896. rect.w += dw;
  897. else if(child->m_gravity & WIDGET_GRAVITY_RIGHT)
  898. rect.x += dw;
  899. if ((child->m_gravity & WIDGET_GRAVITY_TOP) && (child->m_gravity & WIDGET_GRAVITY_BOTTOM))
  900. rect.h += dh;
  901. else if (child->m_gravity & WIDGET_GRAVITY_BOTTOM)
  902. rect.y += dh;
  903. child->SetRect(rect);
  904. }
  905. }
  906. void TBWidget::OnInflateChild(TBWidget *child)
  907. {
  908. if (child->GetVisibility() == WIDGET_VISIBILITY_GONE)
  909. return;
  910. // If the child pull towards only one edge (per axis), stick to that edge
  911. // and use the preferred size. Otherwise fill up all available space.
  912. TBRect padding_rect = GetPaddingRect();
  913. TBRect child_rect = padding_rect;
  914. WIDGET_GRAVITY gravity = child->GetGravity();
  915. bool fill_x = (gravity & WIDGET_GRAVITY_LEFT) && (gravity & WIDGET_GRAVITY_RIGHT);
  916. bool fill_y = (gravity & WIDGET_GRAVITY_TOP) && (gravity & WIDGET_GRAVITY_BOTTOM);
  917. if (!fill_x || !fill_y)
  918. {
  919. PreferredSize ps = child->GetPreferredSize();
  920. if (!fill_x)
  921. {
  922. child_rect.w = ps.pref_w;
  923. if (gravity & WIDGET_GRAVITY_RIGHT)
  924. child_rect.x = padding_rect.x + padding_rect.w - child_rect.w;
  925. }
  926. if (!fill_y)
  927. {
  928. child_rect.h = ps.pref_h;
  929. if (gravity & WIDGET_GRAVITY_BOTTOM)
  930. child_rect.y = padding_rect.y + padding_rect.h - child_rect.h;
  931. }
  932. }
  933. child->SetRect(child_rect);
  934. }
  935. TBRect TBWidget::GetPaddingRect()
  936. {
  937. TBRect padding_rect(0, 0, m_rect.w, m_rect.h);
  938. if (TBSkinElement *e = GetSkinBgElement())
  939. {
  940. padding_rect.x += e->padding_left;
  941. padding_rect.y += e->padding_top;
  942. padding_rect.w -= e->padding_left + e->padding_right;
  943. padding_rect.h -= e->padding_top + e->padding_bottom;
  944. }
  945. return padding_rect;
  946. }
  947. PreferredSize TBWidget::OnCalculatePreferredContentSize(const SizeConstraints &constraints)
  948. {
  949. // The default preferred size is calculated to satisfy the children
  950. // in the best way. Since this is the default, it's probably not a
  951. // layouting widget and children are resized purely by gravity.
  952. // Allow this widget a larger maximum if our gravity wants both ways,
  953. // otherwise don't grow more than the largest child.
  954. bool apply_max_w = !((m_gravity & WIDGET_GRAVITY_LEFT) && (m_gravity & WIDGET_GRAVITY_RIGHT));
  955. bool apply_max_h = !((m_gravity & WIDGET_GRAVITY_TOP) && (m_gravity & WIDGET_GRAVITY_BOTTOM));
  956. bool has_layouting_children = false;
  957. PreferredSize ps;
  958. TBSkinElement *bg_skin = GetSkinBgElement();
  959. int horizontal_padding = bg_skin ? bg_skin->padding_left + bg_skin->padding_right : 0;
  960. int vertical_padding = bg_skin ? bg_skin->padding_top + bg_skin->padding_bottom : 0;
  961. SizeConstraints inner_sc = constraints.ConstrainByPadding(horizontal_padding, vertical_padding);
  962. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  963. {
  964. if (child->GetVisibility() == WIDGET_VISIBILITY_GONE)
  965. continue;
  966. if (!has_layouting_children)
  967. {
  968. has_layouting_children = true;
  969. if (apply_max_w)
  970. ps.max_w = 0;
  971. if (apply_max_h)
  972. ps.max_h = 0;
  973. }
  974. PreferredSize child_ps = child->GetPreferredSize(inner_sc);
  975. ps.pref_w = MAX(ps.pref_w, child_ps.pref_w);
  976. ps.pref_h = MAX(ps.pref_h, child_ps.pref_h);
  977. ps.min_w = MAX(ps.min_w, child_ps.min_w);
  978. ps.min_h = MAX(ps.min_h, child_ps.min_h);
  979. if (apply_max_w)
  980. ps.max_w = MAX(ps.max_w, child_ps.max_w);
  981. if (apply_max_h)
  982. ps.max_h = MAX(ps.max_h, child_ps.max_h);
  983. ps.size_dependency |= child_ps.size_dependency;
  984. }
  985. return ps;
  986. }
  987. PreferredSize TBWidget::OnCalculatePreferredSize(const SizeConstraints &constraints)
  988. {
  989. PreferredSize ps = OnCalculatePreferredContentSize(constraints);
  990. assert(ps.pref_w >= ps.min_w);
  991. assert(ps.pref_h >= ps.min_h);
  992. if (TBSkinElement *e = GetSkinBgElement())
  993. {
  994. // Override the widgets preferences with skin attributes that has been specified.
  995. // If not set by the widget, calculate based on the intrinsic size of the skin.
  996. const int skin_intrinsic_w = e->GetIntrinsicWidth();
  997. if (e->GetPrefWidth() != SKIN_VALUE_NOT_SPECIFIED)
  998. ps.pref_w = e->GetPrefWidth();
  999. else if (ps.pref_w == 0 && skin_intrinsic_w != SKIN_VALUE_NOT_SPECIFIED)
  1000. ps.pref_w = skin_intrinsic_w;
  1001. else
  1002. {
  1003. // Grow by padding to get the preferred size from preferred content size.
  1004. ps.min_w += e->padding_left + e->padding_right;
  1005. ps.pref_w += e->padding_left + e->padding_right;
  1006. }
  1007. const int skin_intrinsic_h = e->GetIntrinsicHeight();
  1008. if (e->GetPrefHeight() != SKIN_VALUE_NOT_SPECIFIED)
  1009. ps.pref_h = e->GetPrefHeight();
  1010. else if (ps.pref_h == 0 && skin_intrinsic_h != SKIN_VALUE_NOT_SPECIFIED)
  1011. ps.pref_h = skin_intrinsic_h;
  1012. else
  1013. {
  1014. // Grow by padding to get the preferred size from preferred content size.
  1015. ps.min_h += e->padding_top + e->padding_bottom;
  1016. ps.pref_h += e->padding_top + e->padding_bottom;
  1017. }
  1018. if (e->GetMinWidth() != SKIN_VALUE_NOT_SPECIFIED)
  1019. ps.min_w = e->GetMinWidth();
  1020. else
  1021. ps.min_w = MAX(ps.min_w, e->GetIntrinsicMinWidth());
  1022. if (e->GetMinHeight() != SKIN_VALUE_NOT_SPECIFIED)
  1023. ps.min_h = e->GetMinHeight();
  1024. else
  1025. ps.min_h = MAX(ps.min_h, e->GetIntrinsicMinHeight());
  1026. if (e->GetMaxWidth() != SKIN_VALUE_NOT_SPECIFIED)
  1027. ps.max_w = e->GetMaxWidth();
  1028. else
  1029. ps.max_w += e->padding_left + e->padding_right;
  1030. if (e->GetMaxHeight() != SKIN_VALUE_NOT_SPECIFIED)
  1031. ps.max_h = e->GetMaxHeight();
  1032. else
  1033. ps.max_h += e->padding_top + e->padding_bottom;
  1034. // Sanitize result
  1035. ps.pref_w = MAX(ps.pref_w, ps.min_w);
  1036. ps.pref_h = MAX(ps.pref_h, ps.min_h);
  1037. }
  1038. return ps;
  1039. }
  1040. PreferredSize TBWidget::GetPreferredSize(const SizeConstraints &in_constraints)
  1041. {
  1042. SizeConstraints constraints(in_constraints);
  1043. if (m_layout_params)
  1044. constraints = constraints.ConstrainByLayoutParams(*m_layout_params);
  1045. // Returned cached result if valid and the constraints are the same.
  1046. if (m_packed.is_cached_ps_valid)
  1047. {
  1048. if (m_cached_sc == constraints ||
  1049. m_cached_ps.size_dependency == SIZE_DEP_NONE /*||
  1050. // FIX: These optimizations would probably be good. Keeping
  1051. // disabled for now because it needs testing.
  1052. // If *only* width depend on height, only the height matter
  1053. (m_cached_ps.size_dependency == SIZE_DEP_WIDTH_DEPEND_ON_HEIGHT &&
  1054. m_cached_sc.available_h == constraints.available_h) ||
  1055. // If *only* height depend on width, only the width matter
  1056. (m_cached_ps.size_dependency == SIZE_DEP_HEIGHT_DEPEND_ON_WIDTH &&
  1057. m_cached_sc.available_w == constraints.available_w)*/)
  1058. {
  1059. return m_cached_ps;
  1060. }
  1061. }
  1062. // Measure and save to cache
  1063. TB_IF_DEBUG_SETTING(LAYOUT_PS_DEBUGGING, last_measure_time = TBSystem::GetTimeMS());
  1064. m_packed.is_cached_ps_valid = 1;
  1065. m_cached_ps = OnCalculatePreferredSize(constraints);
  1066. m_cached_sc = constraints;
  1067. // Override the calculated ps with any specified layout parameter.
  1068. if (m_layout_params)
  1069. {
  1070. #define LP_OVERRIDE(param) if (m_layout_params->param != LayoutParams::UNSPECIFIED) \
  1071. m_cached_ps.param = m_layout_params->param;
  1072. LP_OVERRIDE(min_w);
  1073. LP_OVERRIDE(min_h);
  1074. LP_OVERRIDE(max_w);
  1075. LP_OVERRIDE(max_h);
  1076. LP_OVERRIDE(pref_w);
  1077. LP_OVERRIDE(pref_h);
  1078. // Sanitize results
  1079. m_cached_ps.max_w = MAX(m_cached_ps.max_w, m_cached_ps.min_w);
  1080. m_cached_ps.max_h = MAX(m_cached_ps.max_h, m_cached_ps.min_h);
  1081. m_cached_ps.pref_w = MAX(m_cached_ps.pref_w, m_cached_ps.min_w);
  1082. m_cached_ps.pref_h = MAX(m_cached_ps.pref_h, m_cached_ps.min_h);
  1083. }
  1084. return m_cached_ps;
  1085. }
  1086. void TBWidget::SetLayoutParams(const LayoutParams &lp)
  1087. {
  1088. if (!m_layout_params)
  1089. m_layout_params = new LayoutParams;
  1090. if (!m_layout_params)
  1091. return;
  1092. *m_layout_params = lp;
  1093. m_packed.is_cached_ps_valid = 0;
  1094. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  1095. }
  1096. void TBWidget::InvalidateLayout(INVALIDATE_LAYOUT il)
  1097. {
  1098. m_packed.is_cached_ps_valid = 0;
  1099. if (GetVisibility() == WIDGET_VISIBILITY_GONE)
  1100. return;
  1101. Invalidate();
  1102. if (il == INVALIDATE_LAYOUT_RECURSIVE && m_parent)
  1103. m_parent->InvalidateLayout(il);
  1104. }
  1105. void TBWidget::InvokeProcess()
  1106. {
  1107. InvokeSkinUpdatesInternal(false);
  1108. InvokeProcessInternal();
  1109. }
  1110. void TBWidget::InvokeSkinUpdatesInternal(bool force_update)
  1111. {
  1112. if (!update_skin_states && !force_update)
  1113. return;
  1114. update_skin_states = false;
  1115. // Check if the skin we get is different from what we expect. That might happen
  1116. // if the skin has some strong override dependant a condition that has changed.
  1117. // If that happens, call OnSkinChanged so the widget can react to that, and
  1118. // invalidate layout to apply new skin properties.
  1119. if (TBSkinElement *skin_elm = GetSkinBgElement())
  1120. {
  1121. if (skin_elm->id != m_skin_bg_expected)
  1122. {
  1123. OnSkinChanged();
  1124. m_skin_bg_expected = skin_elm->id;
  1125. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  1126. }
  1127. }
  1128. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  1129. child->InvokeSkinUpdatesInternal(true);
  1130. }
  1131. void TBWidget::InvokeProcessInternal()
  1132. {
  1133. OnProcess();
  1134. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  1135. child->InvokeProcessInternal();
  1136. OnProcessAfterChildren();
  1137. }
  1138. void TBWidget::InvokeProcessStates(bool force_update)
  1139. {
  1140. if (!update_widget_states && !force_update)
  1141. return;
  1142. update_widget_states = false;
  1143. OnProcessStates();
  1144. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  1145. child->InvokeProcessStates(true);
  1146. }
  1147. float TBWidget::CalculateOpacityInternal(WIDGET_STATE state, TBSkinElement *skin_element) const
  1148. {
  1149. float opacity = m_opacity;
  1150. if (skin_element)
  1151. opacity *= skin_element->opacity;
  1152. if (state & WIDGET_STATE_DISABLED)
  1153. if (m_disabledOpacity < 0.0f)
  1154. opacity *= g_tb_skin->GetDefaultDisabledOpacity();
  1155. else
  1156. opacity *= m_disabledOpacity;
  1157. return opacity;
  1158. }
  1159. void TBWidget::InvokePaint(const PaintProps &parent_paint_props)
  1160. {
  1161. // Don't paint invisible widgets
  1162. if (m_opacity == 0 || m_rect.IsEmpty() || GetVisibility() != WIDGET_VISIBILITY_VISIBLE)
  1163. return;
  1164. WIDGET_STATE state = GetAutoState();
  1165. TBSkinElement *skin_element = GetSkinBgElement();
  1166. // Multiply current opacity with widget opacity, skin opacity and state opacity.
  1167. float old_opacity = g_renderer->GetOpacity();
  1168. float opacity = old_opacity * CalculateOpacityInternal(state, skin_element);
  1169. if (opacity == 0)
  1170. return;
  1171. // FIX: This does not give the correct result! Must use a new render target!
  1172. g_renderer->SetOpacity(opacity);
  1173. int trns_x = m_rect.x, trns_y = m_rect.y;
  1174. g_renderer->Translate(trns_x, trns_y);
  1175. // Paint background skin
  1176. TBRect local_rect(0, 0, m_rect.w, m_rect.h);
  1177. TBWidgetSkinConditionContext context(this);
  1178. TBSkinElement *used_element = g_tb_skin->PaintSkin(local_rect, skin_element, static_cast<SKIN_STATE>(state), context);
  1179. assert(!!used_element == !!skin_element);
  1180. TB_IF_DEBUG_SETTING(LAYOUT_BOUNDS, g_renderer->DrawRect(local_rect, TBColor(255, 255, 255, 50)));
  1181. // Inherit properties from parent if not specified in the used skin for this widget.
  1182. PaintProps paint_props = parent_paint_props;
  1183. if (used_element && used_element->text_color != 0)
  1184. paint_props.text_color = used_element->text_color;
  1185. // Paint content
  1186. OnPaint(paint_props);
  1187. if (used_element)
  1188. g_renderer->Translate(used_element->content_ofs_x, used_element->content_ofs_y);
  1189. // Paint children
  1190. OnPaintChildren(paint_props);
  1191. #ifdef TB_RUNTIME_DEBUG_INFO
  1192. if (TB_DEBUG_SETTING(LAYOUT_PS_DEBUGGING))
  1193. {
  1194. // Layout debug painting. Paint recently layouted widgets with red and
  1195. // recently measured widgets with yellow.
  1196. // Invalidate to keep repainting until we've timed out (so it's removed).
  1197. const double debug_time = 300;
  1198. const double now = TBSystem::GetTimeMS();
  1199. if (now < last_layout_time + debug_time)
  1200. {
  1201. g_renderer->DrawRect(local_rect, TBColor(255, 30, 30, 200));
  1202. Invalidate();
  1203. }
  1204. if (now < last_measure_time + debug_time)
  1205. {
  1206. g_renderer->DrawRect(local_rect.Shrink(1, 1), TBColor(255, 255, 30, 200));
  1207. Invalidate();
  1208. }
  1209. }
  1210. #endif // TB_RUNTIME_DEBUG_INFO
  1211. if (used_element)
  1212. g_renderer->Translate(-used_element->content_ofs_x, -used_element->content_ofs_y);
  1213. g_renderer->Translate(-trns_x, -trns_y);
  1214. g_renderer->SetOpacity(old_opacity);
  1215. }
  1216. bool TBWidget::InvokeEvent(TBWidgetEvent &ev)
  1217. {
  1218. ev.target = this;
  1219. // First call the global listener about this event.
  1220. // Who knows, maybe some listener will block the event or cause us
  1221. // to be deleted.
  1222. TBWidgetSafePointer this_widget(this);
  1223. if (TBWidgetListener::InvokeWidgetInvokeEvent(this, ev))
  1224. return true;
  1225. if (!this_widget.Get())
  1226. return true; // We got removed so we actually handled this event.
  1227. if (ev.type == EVENT_TYPE_CHANGED)
  1228. {
  1229. InvalidateSkinStates();
  1230. m_connection.SyncFromWidget(this);
  1231. }
  1232. if (!this_widget.Get())
  1233. return true; // We got removed so we actually handled this event.
  1234. // Always update states after some event types.
  1235. switch (ev.type)
  1236. {
  1237. case EVENT_TYPE_CLICK:
  1238. case EVENT_TYPE_LONG_CLICK:
  1239. case EVENT_TYPE_CHANGED:
  1240. case EVENT_TYPE_KEY_DOWN:
  1241. case EVENT_TYPE_KEY_UP:
  1242. InvalidateStates();
  1243. default:
  1244. break;
  1245. };
  1246. // Call OnEvent on this widgets and travel up through its parents if not handled.
  1247. bool handled = false;
  1248. TBWidget *tmp = this;
  1249. while (tmp && !(handled = tmp->OnEvent(ev)))
  1250. tmp = tmp->GetEventDestination();
  1251. return handled;
  1252. }
  1253. void TBWidget::StartLongClickTimer(bool touch)
  1254. {
  1255. StopLongClickTimer();
  1256. m_long_click_timer = new TBLongClickTimer(this, touch);
  1257. }
  1258. void TBWidget::StopLongClickTimer()
  1259. {
  1260. if (!m_long_click_timer)
  1261. return;
  1262. delete m_long_click_timer;
  1263. m_long_click_timer = nullptr;
  1264. }
  1265. bool TBWidget::InvokePointerDown(int x, int y, int click_count, MODIFIER_KEYS modifierkeys, bool touch, int touchId)
  1266. {
  1267. TBWidget* down_widget = GetWidgetAt(x, y, true);
  1268. if (!captured_widget && down_widget && down_widget->needCapturing_)
  1269. {
  1270. SetCapturedWidget(down_widget);
  1271. SetHoveredWidget(captured_widget, touch);
  1272. //captured_button = button;
  1273. // Hide focus when we use the pointer, if it's not on the focused widget.
  1274. if (focused_widget != captured_widget)
  1275. SetAutoFocusState(false);
  1276. // Start long click timer. Only for touch events for now.
  1277. if (touch && captured_widget && captured_widget->GetWantLongClick())
  1278. captured_widget->StartLongClickTimer(touch);
  1279. // Get the closest parent window and bring it to the top
  1280. TBWindow *window = captured_widget ? captured_widget->GetParentWindow() : nullptr;
  1281. if (window)
  1282. window->Activate();
  1283. }
  1284. if (captured_widget)
  1285. {
  1286. // Check if there's any started scroller that should be stopped.
  1287. TBWidget *tmp = captured_widget;
  1288. while (tmp)
  1289. {
  1290. if (tmp->m_scroller && tmp->m_scroller->IsStarted())
  1291. {
  1292. // When we touch down to stop a scroller, we don't
  1293. // want the touch to end up causing a click.
  1294. cancel_click = true;
  1295. tmp->m_scroller->Stop();
  1296. break;
  1297. }
  1298. tmp = tmp->GetParent();
  1299. }
  1300. // Focus the captured widget or the closest
  1301. // focusable parent if it isn't focusable.
  1302. TBWidget *focus_target = captured_widget;
  1303. while (focus_target)
  1304. {
  1305. if (focus_target->SetFocus(WIDGET_FOCUS_REASON_POINTER))
  1306. break;
  1307. focus_target = focus_target->m_parent;
  1308. }
  1309. }
  1310. //save x and y to restore it later
  1311. int ox = x;
  1312. int oy = y;
  1313. //check only for captured_widget
  1314. if (captured_widget)
  1315. {
  1316. captured_widget->SetTouchId(touchId);
  1317. //converts x, y
  1318. captured_widget->ConvertFromRoot(x, y);
  1319. pointer_move_widget_x = pointer_down_widget_x = x;
  1320. pointer_move_widget_y = pointer_down_widget_y = y;
  1321. TBWidgetEvent ev(EVENT_TYPE_POINTER_DOWN, x, y, touch, modifierkeys);
  1322. ev.count = click_count;
  1323. captured_widget->InvokeEvent(ev);
  1324. }
  1325. //restore x and y coords, to use with down_widget
  1326. x = ox;
  1327. y = oy;
  1328. //if down_widget is captured and it's not a captured_widget
  1329. //then send an event, otherwise don't send it because POINTER_DOWN event will be sent twice
  1330. if (down_widget && down_widget != captured_widget)
  1331. {
  1332. //the same things that's done with captured_widget
  1333. down_widget->Invalidate();
  1334. down_widget->InvalidateSkinStates();
  1335. down_widget->OnCaptureChanged(true);
  1336. down_widget->SetTouchId(touchId);
  1337. //convert x, y
  1338. down_widget->ConvertFromRoot(x, y);
  1339. TBWidgetEvent ev(EVENT_TYPE_POINTER_DOWN, x, y, touch, modifierkeys);
  1340. ev.count = click_count;
  1341. down_widget->InvokeEvent(ev);
  1342. // Return true when captured instead of InvokeEvent result. If a widget is
  1343. // hit is more interesting for callers than if the event was handled or not.
  1344. return true;
  1345. }
  1346. return false;
  1347. }
  1348. bool TBWidget::InvokePointerUp(int x, int y, MODIFIER_KEYS modifierkeys, bool touch, int touchId)
  1349. {
  1350. //get down_widget before making handling captured_widget event to make sure that down_widget won't be changed
  1351. TBWidget* down_widget = GetWidgetAt(x, y, true);
  1352. //save x and y to restore it later
  1353. int ox = x;
  1354. int oy = y;
  1355. //save old_capture for later check, because captured_widget can be nullptr after releasing capture
  1356. TBWidget *old_capture = captured_widget;
  1357. if (captured_widget && captured_widget->touchId_ == touchId)
  1358. {
  1359. captured_widget->ConvertFromRoot(x, y);
  1360. TBWidgetEvent ev_up(EVENT_TYPE_POINTER_UP, x, y, touch, modifierkeys);
  1361. TBWidgetEvent ev_click(EVENT_TYPE_CLICK, x, y, touch, modifierkeys);
  1362. captured_widget->OnCaptureChanged(false);
  1363. // ATOMIC BEGIN
  1364. // Atomic's TurboBadger multitouch support is a hack, TurboBadger has since added better support
  1365. // https://github.com/AtomicGameEngine/AtomicGameEngine/issues/1206
  1366. captured_widget->InvokeEvent(ev_up);
  1367. if (!cancel_click && captured_widget && captured_widget->GetHitStatus(x, y))
  1368. {
  1369. if (captured_widget)
  1370. captured_widget->InvokeEvent(ev_click);
  1371. }
  1372. if (captured_widget)
  1373. captured_widget->ReleaseCapture();
  1374. // ATOMIC END
  1375. }
  1376. //restore x and y coords, to use with down_widget
  1377. x = ox;
  1378. y = oy;
  1379. //make sure that down_widget is not captured_widget to don't sent event twice
  1380. if (down_widget && down_widget->touchId_ == touchId && old_capture != down_widget)
  1381. {
  1382. down_widget->ConvertFromRoot(x, y);
  1383. TBWidgetEvent ev_up(EVENT_TYPE_POINTER_UP, x, y, touch, modifierkeys);
  1384. TBWidgetEvent ev_click(EVENT_TYPE_CLICK, x, y, touch, modifierkeys);
  1385. down_widget->OnCaptureChanged(false);
  1386. down_widget->InvokeEvent(ev_up);
  1387. if (!cancel_click && down_widget->GetHitStatus(x, y))
  1388. {
  1389. down_widget->InvokeEvent(ev_click);
  1390. }
  1391. //ReleaseCapture
  1392. down_widget->Invalidate();
  1393. down_widget->InvalidateSkinStates();
  1394. // Return true when captured instead of InvokeEvent result. If a widget is
  1395. // hit is more interesting for callers than if the event was handled or not.
  1396. return true;
  1397. }
  1398. return false;
  1399. }
  1400. void TBWidget::InvokeRightPointerDown(int x, int y, int click_count, MODIFIER_KEYS modifierkeys)
  1401. {
  1402. // note we're not capturing the widget as with normal pointer down
  1403. TBWidget* widget = GetWidgetAt(x, y, true);
  1404. if (!widget)
  1405. return;
  1406. TBWidgetEvent ev(EVENT_TYPE_RIGHT_POINTER_DOWN, x, y, false, modifierkeys);
  1407. ev.count = click_count;
  1408. widget->InvokeEvent(ev);
  1409. }
  1410. void TBWidget::InvokeRightPointerUp(int x, int y, MODIFIER_KEYS modifierkeys)
  1411. {
  1412. // note we're not capturing the widget as with normal pointer down
  1413. TBWidget* widget = GetWidgetAt(x, y, true);
  1414. if (!widget)
  1415. return;
  1416. TBWidgetEvent ev(EVENT_TYPE_RIGHT_POINTER_UP, x, y, false, modifierkeys);
  1417. ev.target = widget;
  1418. widget->InvokeEvent(ev);
  1419. }
  1420. void TBWidget::MaybeInvokeLongClickOrContextMenu(bool touch)
  1421. {
  1422. StopLongClickTimer();
  1423. if (captured_widget == this &&
  1424. !cancel_click &&
  1425. captured_widget->GetHitStatus(pointer_move_widget_x, pointer_move_widget_y))
  1426. {
  1427. // Invoke long click
  1428. TBWidgetEvent ev_long_click(EVENT_TYPE_LONG_CLICK, pointer_move_widget_x, pointer_move_widget_y, touch, TB_MODIFIER_NONE);
  1429. bool handled = captured_widget->InvokeEvent(ev_long_click);
  1430. if (!handled)
  1431. {
  1432. // Long click not handled so invoke a context menu event instead
  1433. TBWidgetEvent ev_context_menu(EVENT_TYPE_CONTEXT_MENU, pointer_move_widget_x, pointer_move_widget_y, touch, TB_MODIFIER_NONE);
  1434. handled = captured_widget->InvokeEvent(ev_context_menu);
  1435. }
  1436. // If any event was handled, suppress click when releasing pointer.
  1437. if (handled)
  1438. cancel_click = true;
  1439. }
  1440. }
  1441. void TBWidget::InvokePointerMove(int x, int y, MODIFIER_KEYS modifierkeys, bool touch, int touchId)
  1442. {
  1443. TBWidget* move = GetWidgetByTouchId(touchId);
  1444. if (move && move->IsCaptured())
  1445. {
  1446. int cx = x;
  1447. int cy = y;
  1448. move->ConvertFromRoot(cx, cy);
  1449. if (!move->GetHitStatus(cx, cy))
  1450. {
  1451. move->Invalidate();
  1452. move->InvalidateSkinStates();
  1453. move->OnCaptureChanged(false);
  1454. TBWidgetEvent ev_up(EVENT_TYPE_POINTER_UP, cx, cy, touch, TB_MODIFIER_NONE);
  1455. move->InvokeEvent(ev_up);
  1456. }
  1457. }
  1458. SetHoveredWidget(GetWidgetAt(x, y, true), touch);
  1459. TBWidget *target = captured_widget ? captured_widget : hovered_widget;
  1460. if (target)
  1461. {
  1462. target->ConvertFromRoot(x, y);
  1463. pointer_move_widget_x = x;
  1464. pointer_move_widget_y = y;
  1465. TBWidgetEvent ev(EVENT_TYPE_POINTER_MOVE, x, y, touch, modifierkeys);
  1466. if (target->InvokeEvent(ev))
  1467. return;
  1468. // The move event was not handled, so handle panning of scrollable widgets.
  1469. HandlePanningOnMove(x, y);
  1470. }
  1471. }
  1472. void TBWidget::HandlePanningOnMove(int x, int y)
  1473. {
  1474. if (!captured_widget)
  1475. return;
  1476. // Check pointer movement
  1477. const int dx = pointer_down_widget_x - x;
  1478. const int dy = pointer_down_widget_y - y;
  1479. const int threshold = TBSystem::GetPanThreshold();
  1480. const bool maybe_start_panning_x = ABS(dx) >= threshold;
  1481. const bool maybe_start_panning_y = ABS(dy) >= threshold;
  1482. // Do panning, or attempt starting panning (we don't know if any widget is scrollable yet)
  1483. if (captured_widget->m_packed.is_panning || maybe_start_panning_x || maybe_start_panning_y)
  1484. {
  1485. // The threshold is met for not invoking any long click
  1486. captured_widget->StopLongClickTimer();
  1487. int start_compensation_x = 0, start_compensation_y = 0;
  1488. if (!captured_widget->m_packed.is_panning)
  1489. {
  1490. // When we start panning, deduct the extra distance caused by the
  1491. // start threshold from the delta so we don't start with a sudden jump.
  1492. int extra = threshold - 1;
  1493. if (maybe_start_panning_x)
  1494. start_compensation_x = dx < 0 ? extra : -extra;
  1495. if (maybe_start_panning_y)
  1496. start_compensation_y = dy < 0 ? extra : -extra;
  1497. }
  1498. // Get any active scroller and feed it with pan actions.
  1499. TBScroller *scroller = captured_widget->GetReadyScroller(dx != 0, dy != 0);
  1500. if (!scroller)
  1501. return;
  1502. int old_translation_x = 0, old_translation_y = 0;
  1503. captured_widget->GetScrollRoot()->GetChildTranslation(old_translation_x, old_translation_y);
  1504. if (scroller->OnPan(dx + start_compensation_x, dy + start_compensation_y))
  1505. {
  1506. // Scroll delta changed, so we are now panning!
  1507. captured_widget->m_packed.is_panning = true;
  1508. cancel_click = true;
  1509. // If the captured widget (or its scroll root) has panned, we have to compensate the
  1510. // pointer down coordinates so we won't accumulate the difference the following pan.
  1511. int new_translation_x = 0, new_translation_y = 0;
  1512. captured_widget->GetScrollRoot()->GetChildTranslation(new_translation_x, new_translation_y);
  1513. pointer_down_widget_x += new_translation_x - old_translation_x + start_compensation_x;
  1514. pointer_down_widget_y += new_translation_y - old_translation_y + start_compensation_y;
  1515. }
  1516. }
  1517. }
  1518. void TBWidget::InvokePointerCancel()
  1519. {
  1520. if (captured_widget)
  1521. captured_widget->ReleaseCapture();
  1522. }
  1523. bool TBWidget::InvokeTouchDown(int x, int y, uint32 id, int click_count, MODIFIER_KEYS modifierkeys)
  1524. {
  1525. if (id == 0)
  1526. return InvokePointerDown(x, y, click_count, modifierkeys, true);
  1527. TOUCH_INFO *ti = NewTouchInfo(id);
  1528. if (!ti)
  1529. return false;
  1530. if (!ti->captured_widget)
  1531. ti->captured_widget = GetWidgetAt(x, y, true);
  1532. if (ti->captured_widget && !ti->captured_widget->GetState(WIDGET_STATE_DISABLED))
  1533. ti->hovered_widget = ti->captured_widget;
  1534. if (ti->captured_widget)
  1535. {
  1536. ti->captured_widget->ConvertFromRoot(x, y);
  1537. ti->move_widget_x = ti->down_widget_x = x;
  1538. ti->move_widget_y = ti->down_widget_y = y;
  1539. TBWidgetEvent ev(EVENT_TYPE_TOUCH_DOWN, x, y, true, modifierkeys);
  1540. ev.count = click_count;
  1541. ev.ref_id = id;
  1542. ti->captured_widget->InvokeEvent(ev);
  1543. return true;
  1544. }
  1545. return false;
  1546. }
  1547. bool TBWidget::InvokeTouchUp(int x, int y, uint32 id, MODIFIER_KEYS modifierkeys)
  1548. {
  1549. if (id == 0)
  1550. return InvokePointerUp(x, y, modifierkeys, true);
  1551. TOUCH_INFO *ti = GetTouchInfo(id);
  1552. if (ti && ti->captured_widget)
  1553. {
  1554. ti->captured_widget->ConvertFromRoot(x, y);
  1555. TBWidgetEvent ev(EVENT_TYPE_TOUCH_UP, x, y, true, modifierkeys);
  1556. ev.ref_id = id;
  1557. ti->captured_widget->InvokeEvent(ev);
  1558. DeleteTouchInfo(id);
  1559. return true;
  1560. }
  1561. return false;
  1562. }
  1563. void TBWidget::InvokeTouchMove(int x, int y, uint32 id, MODIFIER_KEYS modifierkeys)
  1564. {
  1565. if (id == 0)
  1566. return InvokePointerMove(x, y, modifierkeys, true);
  1567. TOUCH_INFO *ti = GetTouchInfo(id);
  1568. if (!ti)
  1569. return;
  1570. ti->hovered_widget = GetWidgetAt(x, y, true);
  1571. if (ti->captured_widget)
  1572. {
  1573. ti->captured_widget->ConvertFromRoot(x, y);
  1574. ti->move_widget_x = x;
  1575. ti->move_widget_y = y;
  1576. TBWidgetEvent ev(EVENT_TYPE_TOUCH_MOVE, x, y, true, modifierkeys);
  1577. ev.ref_id = id;
  1578. if (ti->captured_widget->InvokeEvent(ev))
  1579. return;
  1580. }
  1581. }
  1582. void TBWidget::InvokeTouchCancel(uint32 id)
  1583. {
  1584. if (id == 0)
  1585. return InvokePointerCancel();
  1586. TOUCH_INFO *ti = GetTouchInfo(id);
  1587. if (ti)
  1588. {
  1589. if (ti->captured_widget)
  1590. {
  1591. TBWidgetEvent ev(EVENT_TYPE_TOUCH_CANCEL, 0, 0, true);
  1592. ev.ref_id = id;
  1593. ti->captured_widget->InvokeEvent(ev);
  1594. }
  1595. DeleteTouchInfo(id);
  1596. }
  1597. }
  1598. void TBWidget::InvokeWheel(int x, int y, int delta_x, int delta_y, MODIFIER_KEYS modifierkeys)
  1599. {
  1600. SetHoveredWidget(GetWidgetAt(x, y, true), true);
  1601. TBWidget *target = captured_widget ? captured_widget : hovered_widget;
  1602. if (target)
  1603. {
  1604. target->ConvertFromRoot(x, y);
  1605. pointer_move_widget_x = x;
  1606. pointer_move_widget_y = y;
  1607. TBWidgetEvent ev(EVENT_TYPE_WHEEL, x, y, true, modifierkeys);
  1608. ev.delta_x = delta_x;
  1609. ev.delta_y = delta_y;
  1610. target->InvokeEvent(ev);
  1611. }
  1612. }
  1613. bool TBWidget::InvokeKey(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
  1614. {
  1615. bool handled = false;
  1616. if (focused_widget)
  1617. {
  1618. // ATOMIC: This may have some side effects for shortcuts we don't want?
  1619. if (focused_widget->GetDisabled())
  1620. return false;
  1621. // Emulate a click on the focused widget when pressing space or enter
  1622. if (!modifierkeys && focused_widget->GetClickByKey() &&
  1623. !focused_widget->GetDisabled() &&
  1624. !focused_widget->GetIsDying() &&
  1625. (special_key == TB_KEY_ENTER || key == ' '))
  1626. {
  1627. // Set the pressed state while the key is down, if it
  1628. // didn't already have the pressed state.
  1629. static bool check_pressed_state = true;
  1630. static bool had_pressed_state = false;
  1631. if (down && check_pressed_state)
  1632. {
  1633. had_pressed_state = focused_widget->GetState(WIDGET_STATE_PRESSED);
  1634. check_pressed_state = false;
  1635. }
  1636. if (!down)
  1637. check_pressed_state = true;
  1638. if (!had_pressed_state)
  1639. {
  1640. focused_widget->SetState(WIDGET_STATE_PRESSED, down);
  1641. focused_widget->m_packed.has_key_pressed_state = down;
  1642. }
  1643. // Invoke the click event
  1644. if (!down)
  1645. {
  1646. TBWidgetEvent ev(EVENT_TYPE_CLICK, m_rect.w / 2, m_rect.h / 2, true);
  1647. focused_widget->InvokeEvent(ev);
  1648. }
  1649. handled = true;
  1650. }
  1651. else
  1652. {
  1653. // Invoke the key event on the focused widget
  1654. TBWidgetEvent ev(down ? EVENT_TYPE_KEY_DOWN : EVENT_TYPE_KEY_UP);
  1655. ev.key = key;
  1656. ev.special_key = special_key;
  1657. ev.modifierkeys = modifierkeys;
  1658. handled = focused_widget->InvokeEvent(ev);
  1659. }
  1660. }
  1661. // Move focus between widgets
  1662. if (down && !handled && special_key == TB_KEY_TAB)
  1663. {
  1664. handled = MoveFocus(!(modifierkeys & TB_SHIFT));
  1665. // Show the focus when we move it by keyboard
  1666. if (handled)
  1667. SetAutoFocusState(true);
  1668. }
  1669. return handled;
  1670. }
  1671. void TBWidget::ReleaseCapture()
  1672. {
  1673. if (this == captured_widget)
  1674. SetCapturedWidget(nullptr);
  1675. }
  1676. void TBWidget::ConvertToRoot(int &x, int &y) const
  1677. {
  1678. const TBWidget *tmp = this;
  1679. while (tmp->m_parent)
  1680. {
  1681. x += tmp->m_rect.x;
  1682. y += tmp->m_rect.y;
  1683. tmp = tmp->m_parent;
  1684. if (tmp)
  1685. {
  1686. int child_translation_x, child_translation_y;
  1687. tmp->GetChildTranslation(child_translation_x, child_translation_y);
  1688. x += child_translation_x;
  1689. y += child_translation_y;
  1690. }
  1691. }
  1692. }
  1693. void TBWidget::ConvertFromRoot(int &x, int &y) const
  1694. {
  1695. const TBWidget *tmp = this;
  1696. while (tmp->m_parent)
  1697. {
  1698. x -= tmp->m_rect.x;
  1699. y -= tmp->m_rect.y;
  1700. tmp = tmp->m_parent;
  1701. if (tmp)
  1702. {
  1703. int child_translation_x, child_translation_y;
  1704. tmp->GetChildTranslation(child_translation_x, child_translation_y);
  1705. x -= child_translation_x;
  1706. y -= child_translation_y;
  1707. }
  1708. }
  1709. }
  1710. // static
  1711. void TBWidget::SetHoveredWidget(TBWidget *widget, bool touch)
  1712. {
  1713. if (TBWidget::hovered_widget == widget)
  1714. return;
  1715. if (widget && widget->GetState(WIDGET_STATE_DISABLED))
  1716. return;
  1717. // We may apply hover state automatically so the widget might need to be updated.
  1718. if (TBWidget::hovered_widget)
  1719. {
  1720. TBWidget::hovered_widget->Invalidate();
  1721. TBWidget::hovered_widget->InvalidateSkinStates();
  1722. }
  1723. TBWidget::hovered_widget = widget;
  1724. if (TBWidget::hovered_widget)
  1725. {
  1726. TBWidget::hovered_widget->Invalidate();
  1727. TBWidget::hovered_widget->InvalidateSkinStates();
  1728. // Cursor based movement should set hover state automatically, but touch
  1729. // events should not (since touch doesn't really move unless pressed).
  1730. TBWidget::hovered_widget->m_packed.no_automatic_hover_state = touch;
  1731. }
  1732. }
  1733. // static
  1734. void TBWidget::SetCapturedWidget(TBWidget *widget)
  1735. {
  1736. if (TBWidget::captured_widget == widget)
  1737. return;
  1738. if (widget && widget->GetState(WIDGET_STATE_DISABLED))
  1739. return;
  1740. if (TBWidget::captured_widget)
  1741. {
  1742. // Stop panning when capture change (most likely changing to nullptr because of InvokePointerUp)
  1743. // Notify any active scroller so it may begin scrolling.
  1744. if (TBScroller *scroller = TBWidget::captured_widget->FindStartedScroller())
  1745. {
  1746. if (TBWidget::captured_widget->m_packed.is_panning)
  1747. scroller->OnPanReleased();
  1748. else
  1749. scroller->Stop();
  1750. }
  1751. TBWidget::captured_widget->m_packed.is_panning = false;
  1752. // We apply pressed state automatically so the widget might need to be updated.
  1753. TBWidget::captured_widget->Invalidate();
  1754. TBWidget::captured_widget->InvalidateSkinStates();
  1755. TBWidget::captured_widget->StopLongClickTimer();
  1756. }
  1757. cancel_click = false;
  1758. TBWidget *old_capture = TBWidget::captured_widget;
  1759. TBWidget::captured_widget = widget;
  1760. if (old_capture)
  1761. old_capture->OnCaptureChanged(false);
  1762. if (TBWidget::captured_widget)
  1763. {
  1764. TBWidget::captured_widget->Invalidate();
  1765. TBWidget::captured_widget->InvalidateSkinStates();
  1766. TBWidget::captured_widget->OnCaptureChanged(true);
  1767. }
  1768. }
  1769. bool TBWidget::SetFontDescription(const TBFontDescription &font_desc)
  1770. {
  1771. if (m_font_desc == font_desc)
  1772. return true;
  1773. // Set the font description only if we have a matching font, or succeed creating one.
  1774. if (g_font_manager->HasFontFace(font_desc))
  1775. m_font_desc = font_desc;
  1776. else if (g_font_manager->CreateFontFace(font_desc))
  1777. m_font_desc = font_desc;
  1778. else
  1779. return false;
  1780. InvokeFontChanged();
  1781. return true;
  1782. }
  1783. void TBWidget::InvokeFontChanged()
  1784. {
  1785. OnFontChanged();
  1786. // Recurse to children that inherit the font
  1787. for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
  1788. if (child->m_font_desc.GetFontFaceID() == 0)
  1789. child->InvokeFontChanged();
  1790. }
  1791. TBFontDescription TBWidget::GetCalculatedFontDescription() const
  1792. {
  1793. const TBWidget *tmp = this;
  1794. while (tmp)
  1795. {
  1796. if (tmp->m_font_desc.GetFontFaceID() != 0)
  1797. return tmp->m_font_desc;
  1798. tmp = tmp->m_parent;
  1799. }
  1800. return g_font_manager->GetDefaultFontDescription();
  1801. }
  1802. TBFontFace *TBWidget::GetFont() const
  1803. {
  1804. return g_font_manager->GetFontFace(GetCalculatedFontDescription());
  1805. }
  1806. }; // namespace tb