UIWidget.cpp 23 KB

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