UIWidget.cpp 26 KB

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