tb_widgets.cpp 48 KB

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