tb_widgets.cpp 47 KB

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