tb_widgets.cpp 47 KB

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