tb_widgets.cpp 63 KB

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