tb_widgets.cpp 47 KB

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