UIWidget.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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. AddRef();
  39. if (createWidget)
  40. {
  41. widget_ = new TBWidget();
  42. widget_->SetDelegate(this);
  43. GetSubsystem<UI>()->WrapWidget(this, widget_);
  44. }
  45. }
  46. UIWidget::~UIWidget()
  47. {
  48. }
  49. void UIWidget::SetIsFocusable(bool value)
  50. {
  51. if (!widget_)
  52. return;
  53. widget_->SetIsFocusable(value);
  54. }
  55. bool UIWidget::Load(const String& filename)
  56. {
  57. UI* ui = GetSubsystem<UI>();
  58. if (!ui->LoadResourceFile(widget_ , filename))
  59. return false;
  60. VariantMap eventData;
  61. eventData[WidgetLoaded::P_WIDGET] = this;
  62. SendEvent(E_WIDGETLOADED, eventData);
  63. return true;
  64. }
  65. UIPreferredSize* UIWidget::GetPreferredSize()
  66. {
  67. // error
  68. if (!widget_)
  69. return preferredSize_;
  70. preferredSize_->SetFromTBPreferredSize(widget_->GetPreferredSize());
  71. return preferredSize_;
  72. }
  73. UIWidget* UIWidget::GetWidget(const String& id)
  74. {
  75. if (!widget_)
  76. return 0;
  77. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  78. if (!child)
  79. return 0;
  80. UI* ui = GetSubsystem<UI>();
  81. return ui->WrapWidget(child);
  82. }
  83. void UIWidget::SetWidget(tb::TBWidget* widget)
  84. {
  85. widget_ = widget;
  86. widget_->SetDelegate(this);
  87. }
  88. /*
  89. enum SPECIAL_KEY
  90. {
  91. TB_KEY_UNDEFINED = 0,
  92. TB_KEY_UP, TB_KEY_DOWN, TB_KEY_LEFT, TB_KEY_RIGHT,
  93. TB_KEY_PAGE_UP, TB_KEY_PAGE_DOWN, TB_KEY_HOME, TB_KEY_END,
  94. TB_KEY_TAB, TB_KEY_BACKSPACE, TB_KEY_INSERT, TB_KEY_DELETE,
  95. TB_KEY_ENTER, TB_KEY_ESC,
  96. TB_KEY_F1, TB_KEY_F2, TB_KEY_F3, TB_KEY_F4, TB_KEY_F5, TB_KEY_F6,
  97. TB_KEY_F7, TB_KEY_F8, TB_KEY_F9, TB_KEY_F10, TB_KEY_F11, TB_KEY_F12
  98. };
  99. */
  100. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  101. {
  102. UI* ui = GetSubsystem<UI>();
  103. String refid;
  104. ui->GetTBIDString(ev.ref_id, refid);
  105. int key = ev.key;
  106. if (ev.special_key)
  107. {
  108. switch (ev.special_key)
  109. {
  110. case TB_KEY_ENTER:
  111. key = KEY_RETURN;
  112. break;
  113. case TB_KEY_BACKSPACE:
  114. key = KEY_BACKSPACE;
  115. break;
  116. case TB_KEY_DELETE:
  117. key = KEY_DELETE;
  118. break;
  119. case TB_KEY_DOWN:
  120. key = KEY_DOWN;
  121. break;
  122. case TB_KEY_UP:
  123. key = KEY_UP;
  124. break;
  125. case TB_KEY_LEFT:
  126. key = KEY_LEFT;
  127. break;
  128. case TB_KEY_RIGHT:
  129. key = KEY_RIGHT;
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. unsigned modifierKeys = 0;
  136. if( ev.special_key && TB_SHIFT)
  137. modifierKeys |= QUAL_SHIFT;
  138. if( ev.special_key && TB_CTRL)
  139. modifierKeys |= QUAL_CTRL;
  140. if( ev.special_key && TB_ALT)
  141. modifierKeys |= QUAL_ALT;
  142. using namespace WidgetEvent;
  143. data[P_HANDLER] = handler;
  144. data[P_TARGET] = target;
  145. data[P_TYPE] = (unsigned) ev.type;
  146. data[P_X] = ev.target_x;
  147. data[P_Y] = ev.target_y;
  148. data[P_DELTAX] = ev.delta_x;
  149. data[P_DELTAY] = ev.delta_y;
  150. data[P_COUNT] = ev.count;
  151. data[P_KEY] = key;
  152. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  153. data[P_MODIFIERKEYS] = modifierKeys;
  154. data[P_REFID] = refid;
  155. data[P_TOUCH] = (unsigned) ev.touch;
  156. }
  157. void UIWidget::OnDelete()
  158. {
  159. if (widget_)
  160. {
  161. // if we don't have a UI subsystem, we are exiting
  162. UI* ui = GetSubsystem<UI>();
  163. if (ui)
  164. ui->UnwrapWidget(widget_);
  165. }
  166. widget_ = 0;
  167. VariantMap eventData;
  168. eventData[WidgetDeleted::P_WIDGET] = this;
  169. SendEvent(E_WIDGETDELETED, eventData);
  170. UnsubscribeFromAllEvents();
  171. ReleaseRef();
  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. void UIWidget::SetFontId(tb::uint32 fontId)
  334. {
  335. if (!widget_)
  336. return;
  337. tb::TBFontDescription fd(widget_->GetFontDescription());
  338. fd.SetID(fontId);
  339. widget_->SetFontDescription(fd);
  340. }
  341. void UIWidget::SetFontSize(int size)
  342. {
  343. if (!widget_)
  344. return;
  345. tb::TBFontDescription fd(widget_->GetFontDescription());
  346. fd.SetSize(size);
  347. widget_->SetFontDescription(fd);
  348. }
  349. int UIWidget::GetFontSize()
  350. {
  351. if (!widget_)
  352. return 0;
  353. tb::TBFontDescription fd(widget_->GetFontDescription());
  354. return fd.GetSize();
  355. }
  356. void UIWidget::SetLayoutWidth(int width)
  357. {
  358. if (!widget_)
  359. return;
  360. tb::LayoutParams lp;
  361. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  362. if (oldLp)
  363. lp = *oldLp;
  364. lp.SetWidth(width);
  365. widget_->SetLayoutParams(lp);
  366. }
  367. int UIWidget::GetLayoutWidth()
  368. {
  369. if (!widget_)
  370. return tb::LayoutParams::UNSPECIFIED;
  371. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  372. if (!lp)
  373. return tb::LayoutParams::UNSPECIFIED;
  374. return lp->pref_w;
  375. }
  376. void UIWidget::SetLayoutHeight(int height)
  377. {
  378. if (!widget_)
  379. return;
  380. tb::LayoutParams lp;
  381. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  382. if (oldLp)
  383. lp = *oldLp;
  384. lp.SetHeight(height);
  385. widget_->SetLayoutParams(lp);
  386. }
  387. int UIWidget::GetLayoutHeight()
  388. {
  389. if (!widget_)
  390. return tb::LayoutParams::UNSPECIFIED;
  391. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  392. if (!lp)
  393. return tb::LayoutParams::UNSPECIFIED;
  394. return lp->pref_h;
  395. }
  396. void UIWidget::SetLayoutPrefWidth(int width)
  397. {
  398. if (!widget_)
  399. return;
  400. tb::LayoutParams lp;
  401. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  402. if (oldLp)
  403. lp = *oldLp;
  404. lp.pref_w = width;
  405. widget_->SetLayoutParams(lp);
  406. }
  407. int UIWidget::GetLayoutPrefWidth()
  408. {
  409. if (!widget_)
  410. return tb::LayoutParams::UNSPECIFIED;
  411. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  412. if (!lp)
  413. return tb::LayoutParams::UNSPECIFIED;
  414. return lp->pref_w;
  415. }
  416. void UIWidget::SetLayoutPrefHeight(int height)
  417. {
  418. if (!widget_)
  419. return;
  420. tb::LayoutParams lp;
  421. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  422. if (oldLp)
  423. lp = *oldLp;
  424. lp.pref_h = height;
  425. widget_->SetLayoutParams(lp);
  426. }
  427. int UIWidget::GetLayoutPrefHeight()
  428. {
  429. if (!widget_)
  430. return tb::LayoutParams::UNSPECIFIED;
  431. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  432. if (!lp)
  433. return tb::LayoutParams::UNSPECIFIED;
  434. return lp->pref_h;
  435. }
  436. void UIWidget::SetLayoutMinWidth(int width)
  437. {
  438. if (!widget_)
  439. return;
  440. tb::LayoutParams lp;
  441. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  442. if (oldLp)
  443. lp = *oldLp;
  444. lp.min_w = width;
  445. widget_->SetLayoutParams(lp);
  446. }
  447. int UIWidget::GetLayoutMinWidth()
  448. {
  449. if (!widget_)
  450. return tb::LayoutParams::UNSPECIFIED;
  451. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  452. if (!lp)
  453. return tb::LayoutParams::UNSPECIFIED;
  454. return lp->min_w;
  455. }
  456. void UIWidget::SetLayoutMinHeight(int height)
  457. {
  458. if (!widget_)
  459. return;
  460. tb::LayoutParams lp;
  461. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  462. if (oldLp)
  463. lp = *oldLp;
  464. lp.min_h = height;
  465. widget_->SetLayoutParams(lp);
  466. }
  467. int UIWidget::GetLayoutMinHeight()
  468. {
  469. if (!widget_)
  470. return tb::LayoutParams::UNSPECIFIED;
  471. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  472. if (!lp)
  473. return tb::LayoutParams::UNSPECIFIED;
  474. return lp->min_h;
  475. }
  476. void UIWidget::SetLayoutMaxWidth(int width)
  477. {
  478. if (!widget_)
  479. return;
  480. tb::LayoutParams lp;
  481. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  482. if (oldLp)
  483. lp = *oldLp;
  484. lp.max_w = width;
  485. widget_->SetLayoutParams(lp);
  486. }
  487. int UIWidget::GetLayoutMaxWidth()
  488. {
  489. if (!widget_)
  490. return tb::LayoutParams::UNSPECIFIED;
  491. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  492. if (!lp)
  493. return tb::LayoutParams::UNSPECIFIED;
  494. return lp->max_w;
  495. }
  496. void UIWidget::SetLayoutMaxHeight(int height)
  497. {
  498. if (!widget_)
  499. return;
  500. tb::LayoutParams lp;
  501. const tb::LayoutParams *oldLp(widget_->GetLayoutParams());
  502. if (oldLp)
  503. lp = *oldLp;
  504. lp.max_h = height;
  505. widget_->SetLayoutParams(lp);
  506. }
  507. int UIWidget::GetLayoutMaxHeight()
  508. {
  509. if (!widget_)
  510. return tb::LayoutParams::UNSPECIFIED;
  511. const tb::LayoutParams *lp(widget_->GetLayoutParams());
  512. if (!lp)
  513. return tb::LayoutParams::UNSPECIFIED;
  514. return lp->max_h;
  515. }
  516. void UIWidget::SetOpacity(float opacity)
  517. {
  518. if (!widget_)
  519. return;
  520. widget_->SetOpacity(opacity);
  521. }
  522. float UIWidget::GetOpacity()
  523. {
  524. if (!widget_)
  525. return -0.0f;
  526. return widget_->GetOpacity();
  527. }
  528. void UIWidget::SetAutoOpacity(float autoOpacity)
  529. {
  530. if (!widget_)
  531. return;
  532. if (autoOpacity == 0.0f)
  533. {
  534. widget_->SetOpacity(autoOpacity);
  535. widget_->SetVisibilility(tb::WIDGET_VISIBILITY_INVISIBLE);
  536. }
  537. else
  538. {
  539. widget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
  540. widget_->SetOpacity(autoOpacity);
  541. }
  542. }
  543. float UIWidget::GetAutoOpacity()
  544. {
  545. if (!widget_)
  546. return -0.0f;
  547. float autoOpacity(widget_->GetOpacity());
  548. if (autoOpacity == 0.0f)
  549. {
  550. if (widget_->GetVisibility() == tb::WIDGET_VISIBILITY_VISIBLE)
  551. return 0.0001f; // Don't say that it's completly invisible.
  552. }
  553. else
  554. {
  555. if (widget_->GetVisibility() != tb::WIDGET_VISIBILITY_VISIBLE)
  556. return 0.0f; // Say it's invisible.
  557. }
  558. return autoOpacity;
  559. }
  560. void UIWidget::DeleteAllChildren()
  561. {
  562. if (!widget_)
  563. return;
  564. widget_->DeleteAllChildren();
  565. }
  566. void UIWidget::SetSkinBg(const String& id)
  567. {
  568. if (!widget_)
  569. return;
  570. widget_->SetSkinBg(TBIDC(id.CString()));
  571. }
  572. void UIWidget::Remove()
  573. {
  574. if (!widget_ || !widget_->GetParent())
  575. return;
  576. widget_->GetParent()->RemoveChild(widget_);
  577. }
  578. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  579. {
  580. if (!widget_ || !child)
  581. return;
  582. TBWidget* childw = child->GetInternalWidget();
  583. if (!childw)
  584. return;
  585. widget_->RemoveChild(childw);
  586. if (cleanup)
  587. delete childw;
  588. }
  589. const String& UIWidget::GetId()
  590. {
  591. if (!widget_ || !widget_->GetID())
  592. {
  593. if (id_.Length())
  594. id_.Clear();
  595. return id_;
  596. }
  597. if (id_.Length())
  598. return id_;
  599. UI* ui = GetSubsystem<UI>();
  600. ui->GetTBIDString(widget_->GetID(), id_);
  601. return id_;
  602. }
  603. void UIWidget::SetId(const String& id)
  604. {
  605. if (!widget_)
  606. {
  607. if (id_.Length())
  608. id_.Clear();
  609. return;
  610. }
  611. id_ = id;
  612. widget_->SetID(TBIDC(id.CString()));
  613. }
  614. void UIWidget::SetState(UI_WIDGET_STATE state, bool on)
  615. {
  616. if (!widget_)
  617. return;
  618. widget_->SetState((WIDGET_STATE) state, on);
  619. }
  620. void UIWidget::SetFocus()
  621. {
  622. if (!widget_)
  623. return;
  624. widget_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  625. }
  626. bool UIWidget::GetFocus()
  627. {
  628. if (!widget_)
  629. return false;
  630. return widget_->GetIsFocused();
  631. }
  632. void UIWidget::SetFocusRecursive()
  633. {
  634. if (!widget_)
  635. return;
  636. widget_->SetFocusRecursive(WIDGET_FOCUS_REASON_UNKNOWN);
  637. }
  638. void UIWidget::SetVisibility(UI_WIDGET_VISIBILITY visibility)
  639. {
  640. if (!widget_)
  641. return;
  642. widget_->SetVisibilility((WIDGET_VISIBILITY) visibility);
  643. }
  644. UI_WIDGET_VISIBILITY UIWidget::GetVisibility()
  645. {
  646. if (!widget_)
  647. return UI_WIDGET_VISIBILITY_GONE;
  648. return (UI_WIDGET_VISIBILITY) widget_->GetVisibility();
  649. }
  650. UIWidget* UIWidget::GetFirstChild()
  651. {
  652. if (!widget_)
  653. return NULL;
  654. return GetSubsystem<UI>()->WrapWidget(widget_->GetFirstChild());
  655. }
  656. UIWidget* UIWidget::GetNext()
  657. {
  658. if (!widget_)
  659. return NULL;
  660. return GetSubsystem<UI>()->WrapWidget(widget_->GetNext());
  661. }
  662. void UIWidget::SetValue(double value)
  663. {
  664. if (!widget_)
  665. return;
  666. widget_->SetValueDouble(value);
  667. }
  668. double UIWidget::GetValue()
  669. {
  670. if (!widget_)
  671. return 0.0;
  672. return widget_->GetValueDouble();
  673. }
  674. void UIWidget::Enable()
  675. {
  676. if (!widget_)
  677. return;
  678. widget_->SetState(WIDGET_STATE_DISABLED, false);
  679. }
  680. void UIWidget::Disable()
  681. {
  682. if (!widget_)
  683. return;
  684. widget_->SetState(WIDGET_STATE_DISABLED, true);
  685. }
  686. bool UIWidget::GetState(UI_WIDGET_STATE state)
  687. {
  688. if (!widget_)
  689. return false;
  690. return widget_->GetState((WIDGET_STATE) state);
  691. }
  692. void UIWidget::SetStateRaw(UI_WIDGET_STATE state)
  693. {
  694. if (!widget_)
  695. return;
  696. widget_->SetStateRaw((WIDGET_STATE) state);
  697. }
  698. UI_WIDGET_STATE UIWidget::GetStateRaw()
  699. {
  700. if (!widget_)
  701. return UI_WIDGET_STATE_NONE;
  702. return (UI_WIDGET_STATE) widget_->GetStateRaw();
  703. }
  704. UIView* UIWidget::GetView()
  705. {
  706. if (!widget_)
  707. return 0;
  708. if (GetType() == UIView::GetTypeStatic())
  709. return (UIView*) this;
  710. TBWidget* tbw = widget_->GetParent();
  711. while(tbw)
  712. {
  713. TBWidgetDelegate* delegate = tbw->GetDelegate();
  714. if (delegate)
  715. {
  716. UIWidget* d = (UIWidget*) delegate;
  717. if (d->GetType() == UIView::GetTypeStatic())
  718. return (UIView*) d;
  719. }
  720. tbw = tbw->GetParent();
  721. }
  722. return 0;
  723. }
  724. void UIWidget::OnFocusChanged(bool focused)
  725. {
  726. using namespace WidgetFocusChanged;
  727. VariantMap eventData;
  728. eventData[P_WIDGET] = this;
  729. eventData[P_FOCUSED] = focused;
  730. SendEvent(E_WIDGETFOCUSCHANGED, eventData);
  731. }
  732. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  733. {
  734. UI* ui = GetSubsystem<UI>();
  735. if ((ev.type == EVENT_TYPE_CHANGED && !ui->GetBlockChangedEvents()) || ev.type == EVENT_TYPE_KEY_UP)
  736. {
  737. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  738. {
  739. VariantMap eventData;
  740. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  741. SendEvent(E_WIDGETEVENT, eventData);
  742. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  743. return true;
  744. }
  745. }
  746. else if (ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  747. {
  748. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  749. {
  750. VariantMap eventData;
  751. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  752. SendEvent(E_WIDGETEVENT, eventData);
  753. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  754. return true;
  755. }
  756. }
  757. else if (ev.type == EVENT_TYPE_POINTER_DOWN)
  758. {
  759. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  760. {
  761. VariantMap eventData;
  762. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  763. SendEvent(E_WIDGETEVENT, eventData);
  764. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  765. return true;
  766. }
  767. }
  768. else if (ev.type == EVENT_TYPE_SHORTCUT)
  769. {
  770. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  771. {
  772. VariantMap eventData;
  773. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  774. SendEvent(E_WIDGETEVENT, eventData);
  775. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  776. return true;
  777. }
  778. }
  779. else if (ev.type == EVENT_TYPE_TAB_CHANGED)
  780. {
  781. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  782. {
  783. VariantMap eventData;
  784. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  785. SendEvent(E_WIDGETEVENT, eventData);
  786. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  787. return true;
  788. }
  789. }
  790. else if (ev.type == EVENT_TYPE_CLICK)
  791. {
  792. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  793. {
  794. // popup menu
  795. if (JSGetHeapPtr())
  796. {
  797. VariantMap eventData;
  798. eventData[PopupMenuSelect::P_BUTTON] = this;
  799. String id;
  800. ui->GetTBIDString(ev.ref_id, id);
  801. eventData[PopupMenuSelect::P_REFID] = id;
  802. SendEvent(E_POPUPMENUSELECT, eventData);
  803. }
  804. return true;
  805. }
  806. else
  807. {
  808. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  809. {
  810. VariantMap eventData;
  811. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  812. SendEvent(E_WIDGETEVENT, eventData);
  813. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  814. return true;
  815. }
  816. }
  817. }
  818. if (ev.type == EVENT_TYPE_CUSTOM)
  819. {
  820. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  821. {
  822. VariantMap eventData;
  823. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  824. SendEvent(E_WIDGETEVENT, eventData);
  825. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  826. return true;
  827. }
  828. }
  829. return false;
  830. }
  831. bool UIWidget::GetCaptured()
  832. {
  833. if (!widget_)
  834. return false;
  835. return widget_->IsCaptured();
  836. }
  837. void UIWidget::SetCapturing(bool capturing)
  838. {
  839. if (!widget_)
  840. return;
  841. widget_->SetCapturing(capturing);
  842. }
  843. bool UIWidget::GetCapturing()
  844. {
  845. if (!widget_)
  846. return false;
  847. return widget_->GetCapturing();
  848. }
  849. void UIWidget::InvalidateLayout()
  850. {
  851. if (!widget_)
  852. return;
  853. widget_->InvalidateLayout(tb::TBWidget::INVALIDATE_LAYOUT_TARGET_ONLY);
  854. }
  855. void UIWidget::InvokeShortcut(const String& shortcut)
  856. {
  857. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  858. ev.ref_id = TBIDC(shortcut.CString());
  859. widget_->OnEvent(ev);
  860. }
  861. bool UIWidget::GetShortened()
  862. {
  863. if (!widget_)
  864. return false;
  865. return widget_->GetShortened();
  866. }
  867. void UIWidget::SetShortened(bool shortened)
  868. {
  869. if (!widget_)
  870. return;
  871. widget_->SetShortened(shortened);
  872. }
  873. String UIWidget::GetTooltip()
  874. {
  875. if (!widget_)
  876. return String::EMPTY;
  877. return widget_->GetTooltip().CStr();
  878. }
  879. void UIWidget::SetTooltip(const String& tooltip)
  880. {
  881. if (!widget_)
  882. return;
  883. widget_->SetTooltip(tooltip.CString());
  884. }
  885. }