tb_widgets.cpp 47 KB

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