UIWidget.cpp 26 KB

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