UIWidget.cpp 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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. 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. if (ui->UnwrapWidget(widget_))
  169. {
  170. widget_ = 0;
  171. ReleaseRef();
  172. return;
  173. }
  174. }
  175. }
  176. widget_ = 0;
  177. }
  178. void UIWidget::AddChildAfter(UIWidget* child, UIWidget* otherChild)
  179. {
  180. if (!widget_ || !child || !child->widget_ || !otherChild || !otherChild->widget_)
  181. return;
  182. widget_->AddChildRelative(child->widget_, tb::WIDGET_Z_REL_AFTER, otherChild->widget_);
  183. }
  184. void UIWidget::AddChildBefore(UIWidget* child, UIWidget* otherChild)
  185. {
  186. if (!widget_ || !child || !child->widget_ || !otherChild || !otherChild->widget_)
  187. return;
  188. widget_->AddChildRelative(child->widget_, tb::WIDGET_Z_REL_BEFORE, otherChild->widget_);
  189. }
  190. void UIWidget::AddChild(UIWidget* child)
  191. {
  192. if (!widget_ || !child || !child->widget_)
  193. return;
  194. widget_->AddChild(child->widget_);
  195. }
  196. void UIWidget::AddChildRelative(UIWidget* child, UI_WIDGET_Z_REL z, UIWidget* reference)
  197. {
  198. if (!widget_ || !child || !child->widget_ || !reference || !reference->widget_)
  199. return;
  200. widget_->AddChildRelative(child->widget_, (WIDGET_Z_REL) z, reference->widget_);
  201. }
  202. String UIWidget::GetText()
  203. {
  204. if (!widget_)
  205. return String::EMPTY;
  206. return widget_->GetText().CStr();
  207. }
  208. void UIWidget::SetText(const String& text)
  209. {
  210. if (!widget_)
  211. return;
  212. widget_->SetText(text.CString());
  213. }
  214. void UIWidget::SetGravity(UI_GRAVITY gravity)
  215. {
  216. if (!widget_)
  217. return;
  218. widget_->SetGravity((WIDGET_GRAVITY) gravity);
  219. }
  220. void UIWidget::SetAxis(UI_AXIS axis)
  221. {
  222. if (!widget_)
  223. return;
  224. widget_->SetAxis((AXIS) axis);
  225. }
  226. bool UIWidget::IsAncestorOf(UIWidget* widget)
  227. {
  228. if (!widget_ || !widget || !widget->widget_)
  229. return false;
  230. return widget_->IsAncestorOf(widget->widget_);
  231. }
  232. void UIWidget::SetPosition(int x, int y)
  233. {
  234. if (!widget_)
  235. return;
  236. widget_->SetPosition(TBPoint(x, y));
  237. }
  238. IntRect UIWidget::GetRect()
  239. {
  240. IntRect rect(0, 0, 0, 0);
  241. if (!widget_)
  242. return rect;
  243. tb::TBRect tbrect = widget_->GetRect();
  244. rect.top_ = tbrect.y;
  245. rect.left_ = tbrect.x;
  246. rect.right_ = tbrect.x + tbrect.w;
  247. rect.bottom_ = tbrect.y + tbrect.h;
  248. return rect;
  249. }
  250. void UIWidget::SetRect(IntRect rect)
  251. {
  252. if (!widget_)
  253. return;
  254. tb::TBRect tbrect;
  255. tbrect.y = rect.top_;
  256. tbrect.x = rect.left_;
  257. tbrect.w = rect.right_ - rect.left_;
  258. tbrect.h = rect.bottom_ - rect.top_;
  259. widget_->SetRect(tbrect);
  260. }
  261. void UIWidget::SetSize(int width, int height)
  262. {
  263. if (!widget_)
  264. return;
  265. widget_->SetSize(width, height);
  266. }
  267. void UIWidget::Invalidate()
  268. {
  269. if (!widget_)
  270. return;
  271. widget_->Invalidate();
  272. }
  273. void UIWidget::Center()
  274. {
  275. if (!widget_)
  276. return;
  277. TBRect rect = widget_->GetRect();
  278. TBWidget* root = widget_->GetParent();
  279. if (!root)
  280. {
  281. UI* ui = GetSubsystem<UI>();
  282. root = ui->GetRootWidget();
  283. }
  284. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  285. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  286. }
  287. UIWidget* UIWidget::GetParent()
  288. {
  289. if (!widget_)
  290. return 0;
  291. TBWidget* parent = widget_->GetParent();
  292. if (!parent)
  293. return 0;
  294. UI* ui = GetSubsystem<UI>();
  295. return ui->WrapWidget(parent);
  296. }
  297. UIWidget* UIWidget::GetContentRoot()
  298. {
  299. if (!widget_)
  300. return 0;
  301. TBWidget* root = widget_->GetContentRoot();
  302. if (!root)
  303. return 0;
  304. UI* ui = GetSubsystem<UI>();
  305. return ui->WrapWidget(root);
  306. }
  307. void UIWidget::Die()
  308. {
  309. if (!widget_)
  310. return;
  311. // clear delegate
  312. widget_->SetDelegate(NULL);
  313. // explictly die (can trigger an animation)
  314. widget_->Die();
  315. // call OnDelete, which unwraps the widget and does some bookkeeping
  316. OnDelete();
  317. }
  318. void UIWidget::SetLayoutParams(UILayoutParams* params)
  319. {
  320. if (!widget_)
  321. return;
  322. widget_->SetLayoutParams(*(params->GetTBLayoutParams()));
  323. }
  324. void UIWidget::SetFontDescription(UIFontDescription* fd)
  325. {
  326. if (!widget_)
  327. return;
  328. widget_->SetFontDescription(*(fd->GetTBFontDescription()));
  329. }
  330. void UIWidget::SetFontId(const String& fontId)
  331. {
  332. if (!widget_)
  333. return;
  334. tb::TBFontDescription fd(widget_->GetFontDescription());
  335. fd.SetID(fontId.CString());
  336. widget_->SetFontDescription(fd);
  337. }
  338. String UIWidget::GetFontId()
  339. {
  340. if (!widget_)
  341. return "";
  342. tb::TBFontDescription fd(widget_->GetFontDescription());
  343. if (!g_font_manager->HasFontFace(fd))
  344. {
  345. return "";
  346. }
  347. return g_font_manager->GetFontInfo(fd.GetID())->GetName();
  348. }
  349. void UIWidget::SetFontSize(int size)
  350. {
  351. if (!widget_)
  352. return;
  353. tb::TBFontDescription fd(widget_->GetFontDescription());
  354. fd.SetSize(size);
  355. widget_->SetFontDescription(fd);
  356. }
  357. int UIWidget::GetFontSize()
  358. {
  359. if (!widget_)
  360. return 0;
  361. tb::TBFontDescription fd(widget_->GetFontDescription());
  362. return fd.GetSize();
  363. }
  364. void UIWidget::SetLayoutWidth(int width)
  365. {
  366. if (!widget_)
  367. return;
  368. tb::LayoutParams lp;
  369. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  370. if (oldLp)
  371. lp = *oldLp;
  372. lp.SetWidth(width);
  373. widget_->SetLayoutParams(lp);
  374. }
  375. int UIWidget::GetLayoutWidth()
  376. {
  377. if (!widget_)
  378. return tb::LayoutParams::UNSPECIFIED;
  379. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  380. if (!lp)
  381. return tb::LayoutParams::UNSPECIFIED;
  382. return lp->pref_w;
  383. }
  384. void UIWidget::SetLayoutHeight(int height)
  385. {
  386. if (!widget_)
  387. return;
  388. tb::LayoutParams lp;
  389. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  390. if (oldLp)
  391. lp = *oldLp;
  392. lp.SetHeight(height);
  393. widget_->SetLayoutParams(lp);
  394. }
  395. int UIWidget::GetLayoutHeight()
  396. {
  397. if (!widget_)
  398. return tb::LayoutParams::UNSPECIFIED;
  399. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  400. if (!lp)
  401. return tb::LayoutParams::UNSPECIFIED;
  402. return lp->pref_h;
  403. }
  404. void UIWidget::SetLayoutPrefWidth(int width)
  405. {
  406. if (!widget_)
  407. return;
  408. tb::LayoutParams lp;
  409. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  410. if (oldLp)
  411. lp = *oldLp;
  412. lp.pref_w = width;
  413. widget_->SetLayoutParams(lp);
  414. }
  415. int UIWidget::GetLayoutPrefWidth()
  416. {
  417. if (!widget_)
  418. return tb::LayoutParams::UNSPECIFIED;
  419. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  420. if (!lp)
  421. return tb::LayoutParams::UNSPECIFIED;
  422. return lp->pref_w;
  423. }
  424. void UIWidget::SetLayoutPrefHeight(int height)
  425. {
  426. if (!widget_)
  427. return;
  428. tb::LayoutParams lp;
  429. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  430. if (oldLp)
  431. lp = *oldLp;
  432. lp.pref_h = height;
  433. widget_->SetLayoutParams(lp);
  434. }
  435. int UIWidget::GetLayoutPrefHeight()
  436. {
  437. if (!widget_)
  438. return tb::LayoutParams::UNSPECIFIED;
  439. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  440. if (!lp)
  441. return tb::LayoutParams::UNSPECIFIED;
  442. return lp->pref_h;
  443. }
  444. void UIWidget::SetLayoutMinWidth(int width)
  445. {
  446. if (!widget_)
  447. return;
  448. tb::LayoutParams lp;
  449. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  450. if (oldLp)
  451. lp = *oldLp;
  452. lp.min_w = width;
  453. widget_->SetLayoutParams(lp);
  454. }
  455. int UIWidget::GetLayoutMinWidth()
  456. {
  457. if (!widget_)
  458. return tb::LayoutParams::UNSPECIFIED;
  459. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  460. if (!lp)
  461. return tb::LayoutParams::UNSPECIFIED;
  462. return lp->min_w;
  463. }
  464. void UIWidget::SetLayoutMinHeight(int height)
  465. {
  466. if (!widget_)
  467. return;
  468. tb::LayoutParams lp;
  469. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  470. if (oldLp)
  471. lp = *oldLp;
  472. lp.min_h = height;
  473. widget_->SetLayoutParams(lp);
  474. }
  475. int UIWidget::GetLayoutMinHeight()
  476. {
  477. if (!widget_)
  478. return tb::LayoutParams::UNSPECIFIED;
  479. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  480. if (!lp)
  481. return tb::LayoutParams::UNSPECIFIED;
  482. return lp->min_h;
  483. }
  484. void UIWidget::SetLayoutMaxWidth(int width)
  485. {
  486. if (!widget_)
  487. return;
  488. tb::LayoutParams lp;
  489. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  490. if (oldLp)
  491. lp = *oldLp;
  492. lp.max_w = width;
  493. widget_->SetLayoutParams(lp);
  494. }
  495. int UIWidget::GetLayoutMaxWidth()
  496. {
  497. if (!widget_)
  498. return tb::LayoutParams::UNSPECIFIED;
  499. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  500. if (!lp)
  501. return tb::LayoutParams::UNSPECIFIED;
  502. return lp->max_w;
  503. }
  504. void UIWidget::SetLayoutMaxHeight(int height)
  505. {
  506. if (!widget_)
  507. return;
  508. tb::LayoutParams lp;
  509. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  510. if (oldLp)
  511. lp = *oldLp;
  512. lp.max_h = height;
  513. widget_->SetLayoutParams(lp);
  514. }
  515. int UIWidget::GetLayoutMaxHeight()
  516. {
  517. if (!widget_)
  518. return tb::LayoutParams::UNSPECIFIED;
  519. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  520. if (!lp)
  521. return tb::LayoutParams::UNSPECIFIED;
  522. return lp->max_h;
  523. }
  524. void UIWidget::SetOpacity(float opacity)
  525. {
  526. if (!widget_)
  527. return;
  528. widget_->SetOpacity(opacity);
  529. }
  530. float UIWidget::GetOpacity()
  531. {
  532. if (!widget_)
  533. return -0.0f;
  534. return widget_->GetOpacity();
  535. }
  536. void UIWidget::SetAutoOpacity(float autoOpacity)
  537. {
  538. if (!widget_)
  539. return;
  540. if (autoOpacity == 0.0f)
  541. {
  542. widget_->SetOpacity(autoOpacity);
  543. widget_->SetVisibilility(tb::WIDGET_VISIBILITY_INVISIBLE);
  544. }
  545. else
  546. {
  547. widget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
  548. widget_->SetOpacity(autoOpacity);
  549. }
  550. }
  551. float UIWidget::GetAutoOpacity()
  552. {
  553. if (!widget_)
  554. return -0.0f;
  555. float autoOpacity(widget_->GetOpacity());
  556. if (autoOpacity == 0.0f)
  557. {
  558. if (widget_->GetVisibility() == tb::WIDGET_VISIBILITY_VISIBLE)
  559. return 0.0001f; // Don't say that it's completly invisible.
  560. }
  561. else
  562. {
  563. if (widget_->GetVisibility() != tb::WIDGET_VISIBILITY_VISIBLE)
  564. return 0.0f; // Say it's invisible.
  565. }
  566. return autoOpacity;
  567. }
  568. void UIWidget::DeleteAllChildren()
  569. {
  570. if (!widget_)
  571. return;
  572. widget_->DeleteAllChildren();
  573. }
  574. void UIWidget::SetSkinBg(const String& id)
  575. {
  576. if (!widget_)
  577. return;
  578. widget_->SetSkinBg(TBIDC(id.CString()));
  579. }
  580. void UIWidget::Remove()
  581. {
  582. if (!widget_ || !widget_->GetParent())
  583. return;
  584. widget_->GetParent()->RemoveChild(widget_);
  585. }
  586. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  587. {
  588. if (!widget_ || !child)
  589. return;
  590. TBWidget* childw = child->GetInternalWidget();
  591. if (!childw)
  592. return;
  593. widget_->RemoveChild(childw);
  594. if (cleanup)
  595. delete childw;
  596. }
  597. const String& UIWidget::GetId()
  598. {
  599. if (!widget_ || !widget_->GetID())
  600. {
  601. if (id_.Length())
  602. id_.Clear();
  603. return id_;
  604. }
  605. if (id_.Length())
  606. return id_;
  607. UI* ui = GetSubsystem<UI>();
  608. ui->GetTBIDString(widget_->GetID(), id_);
  609. return id_;
  610. }
  611. void UIWidget::SetId(const String& id)
  612. {
  613. if (!widget_)
  614. {
  615. if (id_.Length())
  616. id_.Clear();
  617. return;
  618. }
  619. id_ = id;
  620. widget_->SetID(TBIDC(id.CString()));
  621. }
  622. void UIWidget::SetState(UI_WIDGET_STATE state, bool on)
  623. {
  624. if (!widget_)
  625. return;
  626. widget_->SetState((WIDGET_STATE) state, on);
  627. }
  628. void UIWidget::SetFocus()
  629. {
  630. if (!widget_)
  631. return;
  632. widget_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  633. }
  634. bool UIWidget::GetFocus()
  635. {
  636. if (!widget_)
  637. return false;
  638. return widget_->GetIsFocused();
  639. }
  640. void UIWidget::SetFocusRecursive()
  641. {
  642. if (!widget_)
  643. return;
  644. widget_->SetFocusRecursive(WIDGET_FOCUS_REASON_UNKNOWN);
  645. }
  646. void UIWidget::SetVisibility(UI_WIDGET_VISIBILITY visibility)
  647. {
  648. if (!widget_)
  649. return;
  650. widget_->SetVisibilility((WIDGET_VISIBILITY) visibility);
  651. }
  652. UI_WIDGET_VISIBILITY UIWidget::GetVisibility()
  653. {
  654. if (!widget_)
  655. return UI_WIDGET_VISIBILITY_GONE;
  656. return (UI_WIDGET_VISIBILITY) widget_->GetVisibility();
  657. }
  658. UIWidget* UIWidget::GetFirstChild()
  659. {
  660. if (!widget_)
  661. return NULL;
  662. return GetSubsystem<UI>()->WrapWidget(widget_->GetFirstChild());
  663. }
  664. UIWidget* UIWidget::GetNext()
  665. {
  666. if (!widget_)
  667. return NULL;
  668. return GetSubsystem<UI>()->WrapWidget(widget_->GetNext());
  669. }
  670. void UIWidget::SetValue(double value)
  671. {
  672. if (!widget_)
  673. return;
  674. widget_->SetValueDouble(value);
  675. }
  676. double UIWidget::GetValue()
  677. {
  678. if (!widget_)
  679. return 0.0;
  680. return widget_->GetValueDouble();
  681. }
  682. void UIWidget::Enable()
  683. {
  684. if (!widget_)
  685. return;
  686. widget_->SetState(WIDGET_STATE_DISABLED, false);
  687. }
  688. void UIWidget::Disable()
  689. {
  690. if (!widget_)
  691. return;
  692. widget_->SetState(WIDGET_STATE_DISABLED, true);
  693. }
  694. bool UIWidget::GetState(UI_WIDGET_STATE state)
  695. {
  696. if (!widget_)
  697. return false;
  698. return widget_->GetState((WIDGET_STATE) state);
  699. }
  700. void UIWidget::SetStateRaw(UI_WIDGET_STATE state)
  701. {
  702. if (!widget_)
  703. return;
  704. widget_->SetStateRaw((WIDGET_STATE) state);
  705. }
  706. UI_WIDGET_STATE UIWidget::GetStateRaw()
  707. {
  708. if (!widget_)
  709. return UI_WIDGET_STATE_NONE;
  710. return (UI_WIDGET_STATE) widget_->GetStateRaw();
  711. }
  712. UIView* UIWidget::GetView()
  713. {
  714. if (!widget_)
  715. return 0;
  716. if (GetType() == UIView::GetTypeStatic())
  717. return (UIView*) this;
  718. TBWidget* tbw = widget_->GetParent();
  719. while(tbw)
  720. {
  721. TBWidgetDelegate* delegate = tbw->GetDelegate();
  722. if (delegate)
  723. {
  724. UIWidget* d = (UIWidget*) delegate;
  725. if (d->GetType() == UIView::GetTypeStatic())
  726. return (UIView*) d;
  727. }
  728. tbw = tbw->GetParent();
  729. }
  730. return 0;
  731. }
  732. void UIWidget::OnFocusChanged(bool focused)
  733. {
  734. using namespace WidgetFocusChanged;
  735. VariantMap eventData;
  736. eventData[P_WIDGET] = this;
  737. eventData[P_FOCUSED] = focused;
  738. SendEvent(E_WIDGETFOCUSCHANGED, eventData);
  739. }
  740. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  741. {
  742. UI* ui = GetSubsystem<UI>();
  743. if ((ev.type == EVENT_TYPE_CHANGED && !ui->GetBlockChangedEvents()) || ev.type == EVENT_TYPE_KEY_UP)
  744. {
  745. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  746. {
  747. VariantMap eventData;
  748. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  749. SendEvent(E_WIDGETEVENT, eventData);
  750. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  751. return true;
  752. }
  753. }
  754. else if (ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  755. {
  756. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  757. {
  758. VariantMap eventData;
  759. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  760. SendEvent(E_WIDGETEVENT, eventData);
  761. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  762. return true;
  763. }
  764. }
  765. else if (ev.type == EVENT_TYPE_POINTER_DOWN)
  766. {
  767. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  768. {
  769. VariantMap eventData;
  770. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  771. SendEvent(E_WIDGETEVENT, eventData);
  772. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  773. return true;
  774. }
  775. }
  776. else if (ev.type == EVENT_TYPE_SHORTCUT)
  777. {
  778. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  779. {
  780. VariantMap eventData;
  781. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  782. SendEvent(E_WIDGETEVENT, eventData);
  783. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  784. return true;
  785. }
  786. }
  787. else if (ev.type == EVENT_TYPE_TAB_CHANGED)
  788. {
  789. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  790. {
  791. VariantMap eventData;
  792. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  793. SendEvent(E_WIDGETEVENT, eventData);
  794. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  795. return true;
  796. }
  797. }
  798. else if (ev.type == EVENT_TYPE_CLICK)
  799. {
  800. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  801. {
  802. // popup menu
  803. if (JSGetHeapPtr())
  804. {
  805. VariantMap eventData;
  806. eventData[PopupMenuSelect::P_BUTTON] = this;
  807. String id;
  808. ui->GetTBIDString(ev.ref_id, id);
  809. eventData[PopupMenuSelect::P_REFID] = id;
  810. SendEvent(E_POPUPMENUSELECT, eventData);
  811. }
  812. return true;
  813. }
  814. else
  815. {
  816. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  817. {
  818. VariantMap eventData;
  819. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  820. SendEvent(E_WIDGETEVENT, eventData);
  821. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  822. return true;
  823. }
  824. }
  825. }
  826. if (ev.type == EVENT_TYPE_CUSTOM)
  827. {
  828. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  829. {
  830. VariantMap eventData;
  831. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  832. SendEvent(E_WIDGETEVENT, eventData);
  833. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  834. return true;
  835. }
  836. }
  837. return false;
  838. }
  839. bool UIWidget::GetCaptured()
  840. {
  841. if (!widget_)
  842. return false;
  843. return widget_->IsCaptured();
  844. }
  845. void UIWidget::SetCapturing(bool capturing)
  846. {
  847. if (!widget_)
  848. return;
  849. widget_->SetCapturing(capturing);
  850. }
  851. bool UIWidget::GetCapturing()
  852. {
  853. if (!widget_)
  854. return false;
  855. return widget_->GetCapturing();
  856. }
  857. void UIWidget::InvalidateLayout()
  858. {
  859. if (!widget_)
  860. return;
  861. widget_->InvalidateLayout(tb::TBWidget::INVALIDATE_LAYOUT_TARGET_ONLY);
  862. }
  863. void UIWidget::InvokeShortcut(const String& shortcut)
  864. {
  865. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  866. ev.ref_id = TBIDC(shortcut.CString());
  867. widget_->OnEvent(ev);
  868. }
  869. bool UIWidget::GetShortened()
  870. {
  871. if (!widget_)
  872. return false;
  873. return widget_->GetShortened();
  874. }
  875. void UIWidget::SetShortened(bool shortened)
  876. {
  877. if (!widget_)
  878. return;
  879. widget_->SetShortened(shortened);
  880. }
  881. String UIWidget::GetTooltip()
  882. {
  883. if (!widget_)
  884. return String::EMPTY;
  885. return widget_->GetTooltip().CStr();
  886. }
  887. void UIWidget::SetTooltip(const String& tooltip)
  888. {
  889. if (!widget_)
  890. return;
  891. widget_->SetTooltip(tooltip.CString());
  892. }
  893. }