UIWidget.cpp 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../IO/Log.h"
  23. #include "../Input/InputEvents.h"
  24. #include "UIEvents.h"
  25. #include "UI.h"
  26. #include "UIWidget.h"
  27. #include "UILayout.h"
  28. #include "UIFontDescription.h"
  29. #include "UIView.h"
  30. using namespace tb;
  31. namespace Atomic
  32. {
  33. UIWidget::UIWidget(Context* context, bool createWidget) : Object(context),
  34. widget_(0),
  35. preferredSize_(new UIPreferredSize()),
  36. multiTouch_(false)
  37. {
  38. if (createWidget)
  39. {
  40. widget_ = new TBWidget();
  41. widget_->SetDelegate(this);
  42. GetSubsystem<UI>()->WrapWidget(this, widget_);
  43. }
  44. }
  45. UIWidget::~UIWidget()
  46. {
  47. }
  48. void UIWidget::SetIsFocusable(bool value)
  49. {
  50. if (!widget_)
  51. return;
  52. widget_->SetIsFocusable(value);
  53. }
  54. bool UIWidget::Load(const String& filename)
  55. {
  56. UI* ui = GetSubsystem<UI>();
  57. if (!ui->LoadResourceFile(widget_ , filename))
  58. return false;
  59. VariantMap eventData;
  60. eventData[WidgetLoaded::P_WIDGET] = this;
  61. SendEvent(E_WIDGETLOADED, eventData);
  62. return true;
  63. }
  64. UIPreferredSize* UIWidget::GetPreferredSize()
  65. {
  66. // error
  67. if (!widget_)
  68. return preferredSize_;
  69. preferredSize_->SetFromTBPreferredSize(widget_->GetPreferredSize());
  70. return preferredSize_;
  71. }
  72. UIWidget* UIWidget::GetWidget(const String& id)
  73. {
  74. if (!widget_)
  75. return 0;
  76. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  77. if (!child)
  78. return 0;
  79. UI* ui = GetSubsystem<UI>();
  80. return ui->WrapWidget(child);
  81. }
  82. void UIWidget::SetWidget(tb::TBWidget* widget)
  83. {
  84. widget_ = widget;
  85. widget_->SetDelegate(this);
  86. }
  87. /*
  88. enum SPECIAL_KEY
  89. {
  90. TB_KEY_UNDEFINED = 0,
  91. TB_KEY_UP, TB_KEY_DOWN, TB_KEY_LEFT, TB_KEY_RIGHT,
  92. TB_KEY_PAGE_UP, TB_KEY_PAGE_DOWN, TB_KEY_HOME, TB_KEY_END,
  93. TB_KEY_TAB, TB_KEY_BACKSPACE, TB_KEY_INSERT, TB_KEY_DELETE,
  94. TB_KEY_ENTER, TB_KEY_ESC,
  95. TB_KEY_F1, TB_KEY_F2, TB_KEY_F3, TB_KEY_F4, TB_KEY_F5, TB_KEY_F6,
  96. TB_KEY_F7, TB_KEY_F8, TB_KEY_F9, TB_KEY_F10, TB_KEY_F11, TB_KEY_F12
  97. };
  98. */
  99. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  100. {
  101. UI* ui = GetSubsystem<UI>();
  102. String refid;
  103. ui->GetTBIDString(ev.ref_id, refid);
  104. int key = ev.key;
  105. if (ev.special_key)
  106. {
  107. switch (ev.special_key)
  108. {
  109. case TB_KEY_ENTER:
  110. key = KEY_RETURN;
  111. break;
  112. case TB_KEY_BACKSPACE:
  113. key = KEY_BACKSPACE;
  114. break;
  115. case TB_KEY_DELETE:
  116. key = KEY_DELETE;
  117. break;
  118. case TB_KEY_DOWN:
  119. key = KEY_DOWN;
  120. break;
  121. case TB_KEY_UP:
  122. key = KEY_UP;
  123. break;
  124. case TB_KEY_LEFT:
  125. key = KEY_LEFT;
  126. break;
  127. case TB_KEY_RIGHT:
  128. key = KEY_RIGHT;
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. unsigned modifierKeys = 0;
  135. if( ev.special_key && TB_SHIFT)
  136. modifierKeys |= QUAL_SHIFT;
  137. if( ev.special_key && TB_CTRL)
  138. modifierKeys |= QUAL_CTRL;
  139. if( ev.special_key && TB_ALT)
  140. modifierKeys |= QUAL_ALT;
  141. using namespace WidgetEvent;
  142. data[P_HANDLER] = handler;
  143. data[P_TARGET] = target;
  144. data[P_TYPE] = (unsigned) ev.type;
  145. data[P_X] = ev.target_x;
  146. data[P_Y] = ev.target_y;
  147. data[P_DELTAX] = ev.delta_x;
  148. data[P_DELTAY] = ev.delta_y;
  149. data[P_COUNT] = ev.count;
  150. data[P_KEY] = key;
  151. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  152. data[P_MODIFIERKEYS] = modifierKeys;
  153. data[P_REFID] = refid;
  154. data[P_TOUCH] = (unsigned) ev.touch;
  155. }
  156. void UIWidget::OnDelete()
  157. {
  158. VariantMap eventData;
  159. eventData[WidgetDeleted::P_WIDGET] = this;
  160. this->SendEvent(E_WIDGETDELETED, eventData);
  161. UnsubscribeFromAllEvents();
  162. if (widget_)
  163. {
  164. // if we don't have a UI subsystem, we are exiting
  165. UI* ui = GetSubsystem<UI>();
  166. if (ui)
  167. {
  168. ui->UnwrapWidget(widget_);
  169. }
  170. }
  171. widget_ = 0;
  172. }
  173. void UIWidget::AddChildAfter(UIWidget* child, UIWidget* otherChild)
  174. {
  175. if (!widget_ || !child || !child->widget_ || !otherChild || !otherChild->widget_)
  176. return;
  177. widget_->AddChildRelative(child->widget_, tb::WIDGET_Z_REL_AFTER, otherChild->widget_);
  178. }
  179. void UIWidget::AddChildBefore(UIWidget* child, UIWidget* otherChild)
  180. {
  181. if (!widget_ || !child || !child->widget_ || !otherChild || !otherChild->widget_)
  182. return;
  183. widget_->AddChildRelative(child->widget_, tb::WIDGET_Z_REL_BEFORE, otherChild->widget_);
  184. }
  185. void UIWidget::AddChild(UIWidget* child)
  186. {
  187. if (!widget_ || !child || !child->widget_)
  188. return;
  189. widget_->AddChild(child->widget_);
  190. }
  191. void UIWidget::AddChildRelative(UIWidget* child, UI_WIDGET_Z_REL z, UIWidget* reference)
  192. {
  193. if (!widget_ || !child || !child->widget_ || !reference || !reference->widget_)
  194. return;
  195. widget_->AddChildRelative(child->widget_, (WIDGET_Z_REL) z, reference->widget_);
  196. }
  197. String UIWidget::GetText()
  198. {
  199. if (!widget_)
  200. return String::EMPTY;
  201. return widget_->GetText().CStr();
  202. }
  203. void UIWidget::SetText(const String& text)
  204. {
  205. if (!widget_)
  206. return;
  207. widget_->SetText(text.CString());
  208. }
  209. void UIWidget::SetGravity(UI_GRAVITY gravity)
  210. {
  211. if (!widget_)
  212. return;
  213. widget_->SetGravity((WIDGET_GRAVITY) gravity);
  214. }
  215. void UIWidget::SetAxis(UI_AXIS axis)
  216. {
  217. if (!widget_)
  218. return;
  219. widget_->SetAxis((AXIS) axis);
  220. }
  221. bool UIWidget::IsAncestorOf(UIWidget* widget)
  222. {
  223. if (!widget_ || !widget || !widget->widget_)
  224. return false;
  225. return widget_->IsAncestorOf(widget->widget_);
  226. }
  227. void UIWidget::SetPosition(int x, int y)
  228. {
  229. if (!widget_)
  230. return;
  231. widget_->SetPosition(TBPoint(x, y));
  232. }
  233. IntRect UIWidget::GetRect()
  234. {
  235. IntRect rect(0, 0, 0, 0);
  236. if (!widget_)
  237. return rect;
  238. tb::TBRect tbrect = widget_->GetRect();
  239. rect.top_ = tbrect.y;
  240. rect.left_ = tbrect.x;
  241. rect.right_ = tbrect.x + tbrect.w;
  242. rect.bottom_ = tbrect.y + tbrect.h;
  243. return rect;
  244. }
  245. void UIWidget::SetRect(IntRect rect)
  246. {
  247. if (!widget_)
  248. return;
  249. tb::TBRect tbrect;
  250. tbrect.y = rect.top_;
  251. tbrect.x = rect.left_;
  252. tbrect.w = rect.right_ - rect.left_;
  253. tbrect.h = rect.bottom_ - rect.top_;
  254. widget_->SetRect(tbrect);
  255. }
  256. void UIWidget::SetSize(int width, int height)
  257. {
  258. if (!widget_)
  259. return;
  260. widget_->SetSize(width, height);
  261. }
  262. void UIWidget::Invalidate()
  263. {
  264. if (!widget_)
  265. return;
  266. widget_->Invalidate();
  267. }
  268. void UIWidget::Center()
  269. {
  270. if (!widget_)
  271. return;
  272. TBRect rect = widget_->GetRect();
  273. TBWidget* root = widget_->GetParent();
  274. if (!root)
  275. {
  276. UI* ui = GetSubsystem<UI>();
  277. root = ui->GetRootWidget();
  278. }
  279. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  280. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  281. }
  282. UIWidget* UIWidget::GetParent()
  283. {
  284. if (!widget_)
  285. return 0;
  286. TBWidget* parent = widget_->GetParent();
  287. if (!parent)
  288. return 0;
  289. UI* ui = GetSubsystem<UI>();
  290. return ui->WrapWidget(parent);
  291. }
  292. UIWidget* UIWidget::GetContentRoot()
  293. {
  294. if (!widget_)
  295. return 0;
  296. TBWidget* root = widget_->GetContentRoot();
  297. if (!root)
  298. return 0;
  299. UI* ui = GetSubsystem<UI>();
  300. return ui->WrapWidget(root);
  301. }
  302. void UIWidget::Die()
  303. {
  304. if (!widget_)
  305. return;
  306. // clear delegate
  307. widget_->SetDelegate(NULL);
  308. // explictly die (can trigger an animation)
  309. widget_->Die();
  310. // call OnDelete, which unwraps the widget and does some bookkeeping
  311. OnDelete();
  312. }
  313. void UIWidget::SetLayoutParams(UILayoutParams* params)
  314. {
  315. if (!widget_)
  316. return;
  317. widget_->SetLayoutParams(*(params->GetTBLayoutParams()));
  318. }
  319. void UIWidget::SetFontDescription(UIFontDescription* fd)
  320. {
  321. if (!widget_)
  322. return;
  323. widget_->SetFontDescription(*(fd->GetTBFontDescription()));
  324. }
  325. void UIWidget::SetFontId(const String& fontId)
  326. {
  327. if (!widget_)
  328. return;
  329. tb::TBFontDescription fd(widget_->GetFontDescription());
  330. fd.SetID(fontId.CString());
  331. widget_->SetFontDescription(fd);
  332. }
  333. String UIWidget::GetFontId()
  334. {
  335. if (!widget_)
  336. return "";
  337. tb::TBFontDescription fd(widget_->GetFontDescription());
  338. if (!g_font_manager->HasFontFace(fd))
  339. {
  340. return "";
  341. }
  342. return g_font_manager->GetFontInfo(fd.GetID())->GetName();
  343. }
  344. void UIWidget::SetFontSize(int size)
  345. {
  346. if (!widget_)
  347. return;
  348. tb::TBFontDescription fd(widget_->GetFontDescription());
  349. fd.SetSize(size);
  350. widget_->SetFontDescription(fd);
  351. }
  352. int UIWidget::GetFontSize()
  353. {
  354. if (!widget_)
  355. return 0;
  356. tb::TBFontDescription fd(widget_->GetFontDescription());
  357. return fd.GetSize();
  358. }
  359. void UIWidget::SetLayoutWidth(int width)
  360. {
  361. if (!widget_)
  362. return;
  363. tb::LayoutParams lp;
  364. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  365. if (oldLp)
  366. lp = *oldLp;
  367. lp.SetWidth(width);
  368. widget_->SetLayoutParams(lp);
  369. }
  370. int UIWidget::GetLayoutWidth()
  371. {
  372. if (!widget_)
  373. return tb::LayoutParams::UNSPECIFIED;
  374. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  375. if (!lp)
  376. return tb::LayoutParams::UNSPECIFIED;
  377. return lp->pref_w;
  378. }
  379. void UIWidget::SetLayoutHeight(int height)
  380. {
  381. if (!widget_)
  382. return;
  383. tb::LayoutParams lp;
  384. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  385. if (oldLp)
  386. lp = *oldLp;
  387. lp.SetHeight(height);
  388. widget_->SetLayoutParams(lp);
  389. }
  390. int UIWidget::GetLayoutHeight()
  391. {
  392. if (!widget_)
  393. return tb::LayoutParams::UNSPECIFIED;
  394. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  395. if (!lp)
  396. return tb::LayoutParams::UNSPECIFIED;
  397. return lp->pref_h;
  398. }
  399. void UIWidget::SetLayoutPrefWidth(int width)
  400. {
  401. if (!widget_)
  402. return;
  403. tb::LayoutParams lp;
  404. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  405. if (oldLp)
  406. lp = *oldLp;
  407. lp.pref_w = width;
  408. widget_->SetLayoutParams(lp);
  409. }
  410. int UIWidget::GetLayoutPrefWidth()
  411. {
  412. if (!widget_)
  413. return tb::LayoutParams::UNSPECIFIED;
  414. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  415. if (!lp)
  416. return tb::LayoutParams::UNSPECIFIED;
  417. return lp->pref_w;
  418. }
  419. void UIWidget::SetLayoutPrefHeight(int height)
  420. {
  421. if (!widget_)
  422. return;
  423. tb::LayoutParams lp;
  424. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  425. if (oldLp)
  426. lp = *oldLp;
  427. lp.pref_h = height;
  428. widget_->SetLayoutParams(lp);
  429. }
  430. int UIWidget::GetLayoutPrefHeight()
  431. {
  432. if (!widget_)
  433. return tb::LayoutParams::UNSPECIFIED;
  434. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  435. if (!lp)
  436. return tb::LayoutParams::UNSPECIFIED;
  437. return lp->pref_h;
  438. }
  439. void UIWidget::SetLayoutMinWidth(int width)
  440. {
  441. if (!widget_)
  442. return;
  443. tb::LayoutParams lp;
  444. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  445. if (oldLp)
  446. lp = *oldLp;
  447. lp.min_w = width;
  448. widget_->SetLayoutParams(lp);
  449. }
  450. int UIWidget::GetLayoutMinWidth()
  451. {
  452. if (!widget_)
  453. return tb::LayoutParams::UNSPECIFIED;
  454. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  455. if (!lp)
  456. return tb::LayoutParams::UNSPECIFIED;
  457. return lp->min_w;
  458. }
  459. void UIWidget::SetLayoutMinHeight(int height)
  460. {
  461. if (!widget_)
  462. return;
  463. tb::LayoutParams lp;
  464. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  465. if (oldLp)
  466. lp = *oldLp;
  467. lp.min_h = height;
  468. widget_->SetLayoutParams(lp);
  469. }
  470. int UIWidget::GetLayoutMinHeight()
  471. {
  472. if (!widget_)
  473. return tb::LayoutParams::UNSPECIFIED;
  474. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  475. if (!lp)
  476. return tb::LayoutParams::UNSPECIFIED;
  477. return lp->min_h;
  478. }
  479. void UIWidget::SetLayoutMaxWidth(int width)
  480. {
  481. if (!widget_)
  482. return;
  483. tb::LayoutParams lp;
  484. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  485. if (oldLp)
  486. lp = *oldLp;
  487. lp.max_w = width;
  488. widget_->SetLayoutParams(lp);
  489. }
  490. int UIWidget::GetLayoutMaxWidth()
  491. {
  492. if (!widget_)
  493. return tb::LayoutParams::UNSPECIFIED;
  494. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  495. if (!lp)
  496. return tb::LayoutParams::UNSPECIFIED;
  497. return lp->max_w;
  498. }
  499. void UIWidget::SetLayoutMaxHeight(int height)
  500. {
  501. if (!widget_)
  502. return;
  503. tb::LayoutParams lp;
  504. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  505. if (oldLp)
  506. lp = *oldLp;
  507. lp.max_h = height;
  508. widget_->SetLayoutParams(lp);
  509. }
  510. int UIWidget::GetLayoutMaxHeight()
  511. {
  512. if (!widget_)
  513. return tb::LayoutParams::UNSPECIFIED;
  514. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  515. if (!lp)
  516. return tb::LayoutParams::UNSPECIFIED;
  517. return lp->max_h;
  518. }
  519. void UIWidget::SetOpacity(float opacity)
  520. {
  521. if (!widget_)
  522. return;
  523. widget_->SetOpacity(opacity);
  524. }
  525. float UIWidget::GetOpacity()
  526. {
  527. if (!widget_)
  528. return -0.0f;
  529. return widget_->GetOpacity();
  530. }
  531. void UIWidget::SetAutoOpacity(float autoOpacity)
  532. {
  533. if (!widget_)
  534. return;
  535. if (autoOpacity == 0.0f)
  536. {
  537. widget_->SetOpacity(autoOpacity);
  538. widget_->SetVisibilility(tb::WIDGET_VISIBILITY_INVISIBLE);
  539. }
  540. else
  541. {
  542. widget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
  543. widget_->SetOpacity(autoOpacity);
  544. }
  545. }
  546. float UIWidget::GetAutoOpacity()
  547. {
  548. if (!widget_)
  549. return -0.0f;
  550. float autoOpacity(widget_->GetOpacity());
  551. if (autoOpacity == 0.0f)
  552. {
  553. if (widget_->GetVisibility() == tb::WIDGET_VISIBILITY_VISIBLE)
  554. return 0.0001f; // Don't say that it's completly invisible.
  555. }
  556. else
  557. {
  558. if (widget_->GetVisibility() != tb::WIDGET_VISIBILITY_VISIBLE)
  559. return 0.0f; // Say it's invisible.
  560. }
  561. return autoOpacity;
  562. }
  563. void UIWidget::DeleteAllChildren()
  564. {
  565. if (!widget_)
  566. return;
  567. widget_->DeleteAllChildren();
  568. }
  569. void UIWidget::SetSkinBg(const String& id)
  570. {
  571. if (!widget_)
  572. return;
  573. widget_->SetSkinBg(TBIDC(id.CString()));
  574. }
  575. void UIWidget::Remove()
  576. {
  577. if (!widget_ || !widget_->GetParent())
  578. return;
  579. widget_->GetParent()->RemoveChild(widget_);
  580. }
  581. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  582. {
  583. if (!widget_ || !child)
  584. return;
  585. TBWidget* childw = child->GetInternalWidget();
  586. if (!childw)
  587. return;
  588. widget_->RemoveChild(childw);
  589. if (cleanup)
  590. delete childw;
  591. }
  592. const String& UIWidget::GetId()
  593. {
  594. if (!widget_ || !widget_->GetID())
  595. {
  596. if (id_.Length())
  597. id_.Clear();
  598. return id_;
  599. }
  600. if (id_.Length())
  601. return id_;
  602. UI* ui = GetSubsystem<UI>();
  603. ui->GetTBIDString(widget_->GetID(), id_);
  604. return id_;
  605. }
  606. void UIWidget::SetId(const String& id)
  607. {
  608. if (!widget_)
  609. {
  610. if (id_.Length())
  611. id_.Clear();
  612. return;
  613. }
  614. id_ = id;
  615. widget_->SetID(TBIDC(id.CString()));
  616. }
  617. void UIWidget::SetState(UI_WIDGET_STATE state, bool on)
  618. {
  619. if (!widget_)
  620. return;
  621. widget_->SetState((WIDGET_STATE) state, on);
  622. }
  623. void UIWidget::SetFocus()
  624. {
  625. if (!widget_)
  626. return;
  627. widget_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  628. }
  629. bool UIWidget::GetFocus()
  630. {
  631. if (!widget_)
  632. return false;
  633. return widget_->GetIsFocused();
  634. }
  635. void UIWidget::SetFocusRecursive()
  636. {
  637. if (!widget_)
  638. return;
  639. widget_->SetFocusRecursive(WIDGET_FOCUS_REASON_UNKNOWN);
  640. }
  641. void UIWidget::SetVisibility(UI_WIDGET_VISIBILITY visibility)
  642. {
  643. if (!widget_)
  644. return;
  645. widget_->SetVisibilility((WIDGET_VISIBILITY) visibility);
  646. }
  647. UI_WIDGET_VISIBILITY UIWidget::GetVisibility()
  648. {
  649. if (!widget_)
  650. return UI_WIDGET_VISIBILITY_GONE;
  651. return (UI_WIDGET_VISIBILITY) widget_->GetVisibility();
  652. }
  653. UIWidget* UIWidget::GetFirstChild()
  654. {
  655. if (!widget_)
  656. return NULL;
  657. return GetSubsystem<UI>()->WrapWidget(widget_->GetFirstChild());
  658. }
  659. UIWidget* UIWidget::GetNext()
  660. {
  661. if (!widget_)
  662. return NULL;
  663. return GetSubsystem<UI>()->WrapWidget(widget_->GetNext());
  664. }
  665. void UIWidget::SetValue(double value)
  666. {
  667. if (!widget_)
  668. return;
  669. widget_->SetValueDouble(value);
  670. }
  671. double UIWidget::GetValue()
  672. {
  673. if (!widget_)
  674. return 0.0;
  675. return widget_->GetValueDouble();
  676. }
  677. void UIWidget::Enable()
  678. {
  679. if (!widget_)
  680. return;
  681. widget_->SetState(WIDGET_STATE_DISABLED, false);
  682. }
  683. void UIWidget::Disable()
  684. {
  685. if (!widget_)
  686. return;
  687. widget_->SetState(WIDGET_STATE_DISABLED, true);
  688. }
  689. bool UIWidget::GetState(UI_WIDGET_STATE state)
  690. {
  691. if (!widget_)
  692. return false;
  693. return widget_->GetState((WIDGET_STATE) state);
  694. }
  695. void UIWidget::SetStateRaw(UI_WIDGET_STATE state)
  696. {
  697. if (!widget_)
  698. return;
  699. widget_->SetStateRaw((WIDGET_STATE) state);
  700. }
  701. UI_WIDGET_STATE UIWidget::GetStateRaw()
  702. {
  703. if (!widget_)
  704. return UI_WIDGET_STATE_NONE;
  705. return (UI_WIDGET_STATE) widget_->GetStateRaw();
  706. }
  707. UIView* UIWidget::GetView()
  708. {
  709. if (!widget_)
  710. return 0;
  711. if (GetType() == UIView::GetTypeStatic())
  712. return (UIView*) this;
  713. TBWidget* tbw = widget_->GetParent();
  714. while(tbw)
  715. {
  716. TBWidgetDelegate* delegate = tbw->GetDelegate();
  717. if (delegate)
  718. {
  719. UIWidget* d = (UIWidget*) delegate;
  720. if (d->GetType() == UIView::GetTypeStatic())
  721. return (UIView*) d;
  722. }
  723. tbw = tbw->GetParent();
  724. }
  725. return 0;
  726. }
  727. void UIWidget::OnFocusChanged(bool focused)
  728. {
  729. using namespace WidgetFocusChanged;
  730. VariantMap eventData;
  731. eventData[P_WIDGET] = this;
  732. eventData[P_FOCUSED] = focused;
  733. SendEvent(E_WIDGETFOCUSCHANGED, eventData);
  734. }
  735. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  736. {
  737. UI* ui = GetSubsystem<UI>();
  738. if ((ev.type == EVENT_TYPE_CHANGED && !ui->GetBlockChangedEvents()) || ev.type == EVENT_TYPE_KEY_UP)
  739. {
  740. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  741. {
  742. VariantMap eventData;
  743. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  744. SendEvent(E_WIDGETEVENT, eventData);
  745. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  746. return true;
  747. }
  748. }
  749. else if (ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  750. {
  751. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  752. {
  753. VariantMap eventData;
  754. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  755. SendEvent(E_WIDGETEVENT, eventData);
  756. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  757. return true;
  758. }
  759. }
  760. else if (ev.type == EVENT_TYPE_POINTER_DOWN)
  761. {
  762. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  763. {
  764. VariantMap eventData;
  765. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  766. SendEvent(E_WIDGETEVENT, eventData);
  767. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  768. return true;
  769. }
  770. }
  771. else if (ev.type == EVENT_TYPE_SHORTCUT)
  772. {
  773. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  774. {
  775. VariantMap eventData;
  776. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  777. SendEvent(E_WIDGETEVENT, eventData);
  778. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  779. return true;
  780. }
  781. }
  782. else if (ev.type == EVENT_TYPE_TAB_CHANGED)
  783. {
  784. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  785. {
  786. VariantMap eventData;
  787. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  788. SendEvent(E_WIDGETEVENT, eventData);
  789. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  790. return true;
  791. }
  792. }
  793. else if (ev.type == EVENT_TYPE_CLICK)
  794. {
  795. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  796. {
  797. // popup menu
  798. if (JSGetHeapPtr())
  799. {
  800. VariantMap eventData;
  801. eventData[PopupMenuSelect::P_BUTTON] = this;
  802. String id;
  803. ui->GetTBIDString(ev.ref_id, id);
  804. eventData[PopupMenuSelect::P_REFID] = id;
  805. SendEvent(E_POPUPMENUSELECT, eventData);
  806. }
  807. return true;
  808. }
  809. else
  810. {
  811. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  812. {
  813. VariantMap eventData;
  814. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  815. SendEvent(E_WIDGETEVENT, eventData);
  816. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  817. return true;
  818. }
  819. }
  820. }
  821. if (ev.type == EVENT_TYPE_CUSTOM)
  822. {
  823. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  824. {
  825. VariantMap eventData;
  826. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  827. SendEvent(E_WIDGETEVENT, eventData);
  828. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  829. return true;
  830. }
  831. }
  832. return false;
  833. }
  834. bool UIWidget::GetCaptured()
  835. {
  836. if (!widget_)
  837. return false;
  838. return widget_->IsCaptured();
  839. }
  840. void UIWidget::SetCapturing(bool capturing)
  841. {
  842. if (!widget_)
  843. return;
  844. widget_->SetCapturing(capturing);
  845. }
  846. bool UIWidget::GetCapturing()
  847. {
  848. if (!widget_)
  849. return false;
  850. return widget_->GetCapturing();
  851. }
  852. void UIWidget::InvalidateLayout()
  853. {
  854. if (!widget_)
  855. return;
  856. widget_->InvalidateLayout(tb::TBWidget::INVALIDATE_LAYOUT_TARGET_ONLY);
  857. }
  858. void UIWidget::InvokeShortcut(const String& shortcut)
  859. {
  860. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  861. ev.ref_id = TBIDC(shortcut.CString());
  862. widget_->OnEvent(ev);
  863. }
  864. bool UIWidget::GetShortened()
  865. {
  866. if (!widget_)
  867. return false;
  868. return widget_->GetShortened();
  869. }
  870. void UIWidget::SetShortened(bool shortened)
  871. {
  872. if (!widget_)
  873. return;
  874. widget_->SetShortened(shortened);
  875. }
  876. String UIWidget::GetTooltip()
  877. {
  878. if (!widget_)
  879. return String::EMPTY;
  880. return widget_->GetTooltip().CStr();
  881. }
  882. void UIWidget::SetTooltip(const String& tooltip)
  883. {
  884. if (!widget_)
  885. return;
  886. widget_->SetTooltip(tooltip.CString());
  887. }
  888. IntVector2 UIWidget::ConvertToRoot(const IntVector2 position) const
  889. {
  890. IntVector2 result = position;
  891. if (widget_)
  892. widget_->ConvertToRoot(result.x_, result.y_);
  893. return result;
  894. }
  895. IntVector2 UIWidget::ConvertFromRoot(const IntVector2 position) const
  896. {
  897. IntVector2 result = position;
  898. if (widget_)
  899. widget_->ConvertFromRoot(result.x_, result.y_);
  900. return result;
  901. }
  902. }