UIWidget.cpp 23 KB

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