tb_widgets_reader.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_widgets_reader.h"
  6. #include "tb_widgets_common.h"
  7. #include "tb_scroll_container.h"
  8. #include "tb_tab_container.h"
  9. #include "tb_select.h"
  10. #include "tb_inline_select.h"
  11. #include "tb_editfield.h"
  12. #include "tb_node_tree.h"
  13. #include "tb_font_renderer.h"
  14. #include "tb_toggle_container.h"
  15. #include "image/tb_image_widget.h"
  16. namespace tb {
  17. TB_WIDGET_FACTORY(TBWidget, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  18. void TBWidget::OnInflate(const INFLATE_INFO &info)
  19. {
  20. TBWidgetsReader::SetIDFromNode(GetID(), info.node->GetNode("id"));
  21. TBWidgetsReader::SetIDFromNode(GetGroupID(), info.node->GetNode("group-id"));
  22. if (info.sync_type == TBValue::TYPE_FLOAT)
  23. SetValueDouble(info.node->GetValueFloat("value", 0));
  24. else
  25. SetValue(info.node->GetValueInt("value", 0));
  26. if (TBNode *data_node = info.node->GetNode("data"))
  27. data.Copy(data_node->GetValue());
  28. SetIsGroupRoot(info.node->GetValueInt("is-group-root", GetIsGroupRoot()) ? true : false);
  29. SetIsFocusable(info.node->GetValueInt("is-focusable", GetIsFocusable()) ? true : false);
  30. SetWantLongClick(info.node->GetValueInt("want-long-click", GetWantLongClick()) ? true : false);
  31. SetIgnoreInput(info.node->GetValueInt("ignore-input", GetIgnoreInput()) ? true : false);
  32. SetOpacity(info.node->GetValueFloat("opacity", GetOpacity()));
  33. SetDisabledOpacity(info.node->GetValueFloat("disabled-Opacity", GetDisabledOpacity()));
  34. if (const char *text = info.node->GetValueString("text", nullptr))
  35. SetText(text);
  36. if (const char *connection = info.node->GetValueStringRaw("connection", nullptr))
  37. {
  38. // If we already have a widget value with this name, just connect to it and the widget will
  39. // adjust its value to it. Otherwise create a new widget value, and give it the value we
  40. // got from the resource.
  41. if (TBWidgetValue *value = g_value_group.GetValue(connection))
  42. Connect(value);
  43. else if (TBWidgetValue *value = g_value_group.CreateValueIfNeeded(connection, info.sync_type))
  44. {
  45. value->SetFromWidget(this);
  46. Connect(value);
  47. }
  48. }
  49. if (const char *axis = info.node->GetValueString("axis", nullptr))
  50. SetAxis(*axis == 'x' ? AXIS_X : AXIS_Y);
  51. if (const char *gravity = info.node->GetValueString("gravity", nullptr))
  52. {
  53. WIDGET_GRAVITY g = WIDGET_GRAVITY_NONE;
  54. if (strstr(gravity, "left")) g |= WIDGET_GRAVITY_LEFT;
  55. if (strstr(gravity, "top")) g |= WIDGET_GRAVITY_TOP;
  56. if (strstr(gravity, "right")) g |= WIDGET_GRAVITY_RIGHT;
  57. if (strstr(gravity, "bottom")) g |= WIDGET_GRAVITY_BOTTOM;
  58. if (strstr(gravity, "all")) g |= WIDGET_GRAVITY_ALL;
  59. if (!(g & WIDGET_GRAVITY_LEFT_RIGHT))
  60. g |= WIDGET_GRAVITY_LEFT;
  61. if (!(g & WIDGET_GRAVITY_TOP_BOTTOM))
  62. g |= WIDGET_GRAVITY_TOP;
  63. SetGravity(g);
  64. }
  65. if (const char *visibility = info.node->GetValueString("visibility", nullptr))
  66. {
  67. if (!strcmp(visibility, "visible")) SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  68. else if (!strcmp(visibility, "invisible")) SetVisibilility(WIDGET_VISIBILITY_INVISIBLE);
  69. else if (!strcmp(visibility, "gone")) SetVisibilility(WIDGET_VISIBILITY_GONE);
  70. }
  71. if (const char *state = info.node->GetValueString("state", nullptr))
  72. {
  73. if (strstr(state, "disabled"))
  74. SetState(WIDGET_STATE_DISABLED, true);
  75. }
  76. if (const char *skin = info.node->GetValueString("skin", nullptr))
  77. {
  78. SetSkinBg(skin);
  79. }
  80. if (TBNode *lp = info.node->GetNode("lp"))
  81. {
  82. LayoutParams layout_params;
  83. if (GetLayoutParams())
  84. layout_params = *GetLayoutParams();
  85. const TBDimensionConverter *dc = g_tb_skin->GetDimensionConverter();
  86. if (const char *str = lp->GetValueString("width", nullptr))
  87. layout_params.SetWidth(dc->GetPxFromString(str, LayoutParams::UNSPECIFIED));
  88. if (const char *str = lp->GetValueString("height", nullptr))
  89. layout_params.SetHeight(dc->GetPxFromString(str, LayoutParams::UNSPECIFIED));
  90. if (const char *str = lp->GetValueString("min-width", nullptr))
  91. layout_params.min_w = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED);
  92. if (const char *str = lp->GetValueString("max-width", nullptr))
  93. layout_params.max_w = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED);
  94. if (const char *str = lp->GetValueString("pref-width", nullptr))
  95. layout_params.pref_w = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED);
  96. if (const char *str = lp->GetValueString("min-height", nullptr))
  97. layout_params.min_h = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED);
  98. if (const char *str = lp->GetValueString("max-height", nullptr))
  99. layout_params.max_h = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED);
  100. if (const char *str = lp->GetValueString("pref-height", nullptr))
  101. layout_params.pref_h = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED);
  102. SetLayoutParams(layout_params);
  103. }
  104. // Add the new widget to the hiearchy if not already added.
  105. if (!GetParent())
  106. info.target->AddChild(this, info.target->GetZInflate());
  107. // Read the font now when the widget is in the hiearchy so inheritance works.
  108. if (TBNode *font = info.node->GetNode("font"))
  109. {
  110. TBFontDescription fd = GetCalculatedFontDescription();
  111. if (const char *size = font->GetValueString("size", nullptr))
  112. {
  113. int new_size = g_tb_skin->GetDimensionConverter()->GetPxFromString(size, fd.GetSize());
  114. fd.SetSize(new_size);
  115. }
  116. if (const char *name = font->GetValueString("name", nullptr))
  117. fd.SetID(name);
  118. SetFontDescription(fd);
  119. }
  120. info.target->OnInflateChild(this);
  121. if (TBNode *rect_node = info.node->GetNode("rect"))
  122. {
  123. const TBDimensionConverter *dc = g_tb_skin->GetDimensionConverter();
  124. TBValue &val = rect_node->GetValue();
  125. if (val.GetArrayLength() == 4)
  126. {
  127. SetRect(TBRect(dc->GetPxFromValue(val.GetArray()->GetValue(0), 0),
  128. dc->GetPxFromValue(val.GetArray()->GetValue(1), 0),
  129. dc->GetPxFromValue(val.GetArray()->GetValue(2), 0),
  130. dc->GetPxFromValue(val.GetArray()->GetValue(3), 0)));
  131. }
  132. }
  133. if (const char *tooltip = info.node->GetValueString("tooltip", nullptr))
  134. SetTooltip(tooltip);
  135. }
  136. TB_WIDGET_FACTORY(TBButton, TBValue::TYPE_NULL, WIDGET_Z_BOTTOM) {}
  137. void TBButton::OnInflate(const INFLATE_INFO &info)
  138. {
  139. SetToggleMode(info.node->GetValueInt("toggle-mode", GetToggleMode()) ? true : false);
  140. // ATOMIC BEGIN
  141. SetURL(info.node->GetValueString("url", ""));
  142. SetSqueezable(info.node->GetValueInt("squeezable", GetSqueezable()) ? true : false);
  143. // ATOMIC END
  144. TBWidget::OnInflate(info);
  145. }
  146. TB_WIDGET_FACTORY(TBInlineSelect, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  147. void TBInlineSelect::OnInflate(const INFLATE_INFO &info)
  148. {
  149. int min = info.node->GetValueInt("min", GetMinValue());
  150. int max = info.node->GetValueInt("max", GetMaxValue());
  151. SetLimits(min, max);
  152. // ATOMIC BEGIN
  153. double m_stepsize = (double)info.node->GetValueFloat("stepsize", (float)GetStepSize());
  154. // ATOMIC END
  155. TBWidget::OnInflate(info);
  156. }
  157. TB_WIDGET_FACTORY(TBClickLabel, TBValue::TYPE_STRING, WIDGET_Z_BOTTOM) {}
  158. TB_WIDGET_FACTORY(TBEditField, TBValue::TYPE_STRING, WIDGET_Z_TOP) {}
  159. void TBEditField::OnInflate(const INFLATE_INFO &info)
  160. {
  161. SetMultiline(info.node->GetValueInt("multiline", 0) ? true : false);
  162. SetStyling(info.node->GetValueInt("styling", 0) ? true : false);
  163. SetReadOnly(info.node->GetValueInt("readonly", 0) ? true : false);
  164. SetWrapping(info.node->GetValueInt("wrap", GetWrapping()) ? true : false);
  165. SetAdaptToContentSize(info.node->GetValueInt("adapt-to-content", GetAdaptToContentSize()) ? true : false);
  166. if (const char *virtual_width = info.node->GetValueString("virtual-width", nullptr))
  167. SetVirtualWidth(g_tb_skin->GetDimensionConverter()->GetPxFromString(virtual_width, GetVirtualWidth()));
  168. if (const char *text = info.node->GetValueString("placeholder", nullptr))
  169. SetPlaceholderText(text);
  170. if (const char *type = info.node->GetValueString("type", nullptr))
  171. {
  172. if (stristr(type, "text")) SetEditType(EDIT_TYPE_TEXT);
  173. else if (stristr(type, "search")) SetEditType(EDIT_TYPE_SEARCH);
  174. else if (stristr(type, "password")) SetEditType(EDIT_TYPE_PASSWORD);
  175. else if (stristr(type, "email")) SetEditType(EDIT_TYPE_EMAIL);
  176. else if (stristr(type, "phone")) SetEditType(EDIT_TYPE_PHONE);
  177. else if (stristr(type, "url")) SetEditType(EDIT_TYPE_URL);
  178. else if (stristr(type, "number")) SetEditType(EDIT_TYPE_NUMBER);
  179. }
  180. if (const char *text_align = info.node->GetValueString("text-align", nullptr))
  181. {
  182. if (!strcmp(text_align, "left")) SetTextAlign(TB_TEXT_ALIGN_LEFT);
  183. else if (!strcmp(text_align, "center")) SetTextAlign(TB_TEXT_ALIGN_CENTER);
  184. else if (!strcmp(text_align, "right")) SetTextAlign(TB_TEXT_ALIGN_RIGHT);
  185. }
  186. TBWidget::OnInflate(info);
  187. }
  188. TB_WIDGET_FACTORY(TBLayout, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  189. void TBLayout::OnInflate(const INFLATE_INFO &info)
  190. {
  191. if (const char *spacing = info.node->GetValueString("spacing", nullptr))
  192. SetSpacing(g_tb_skin->GetDimensionConverter()->GetPxFromString(spacing, SPACING_FROM_SKIN));
  193. SetGravity(WIDGET_GRAVITY_ALL);
  194. if (const char *size = info.node->GetValueString("size", nullptr))
  195. {
  196. LAYOUT_SIZE ls = LAYOUT_SIZE_PREFERRED;
  197. if (strstr(size, "available"))
  198. ls = LAYOUT_SIZE_AVAILABLE;
  199. else if (strstr(size, "gravity"))
  200. ls = LAYOUT_SIZE_GRAVITY;
  201. SetLayoutSize(ls);
  202. }
  203. if (const char *pos = info.node->GetValueString("position", nullptr))
  204. {
  205. LAYOUT_POSITION lp = LAYOUT_POSITION_CENTER;
  206. if (strstr(pos, "left") || strstr(pos, "top"))
  207. lp = LAYOUT_POSITION_LEFT_TOP;
  208. else if (strstr(pos, "right") || strstr(pos, "bottom"))
  209. lp = LAYOUT_POSITION_RIGHT_BOTTOM;
  210. else if (strstr(pos, "gravity"))
  211. lp = LAYOUT_POSITION_GRAVITY;
  212. SetLayoutPosition(lp);
  213. }
  214. if (const char *pos = info.node->GetValueString("overflow", nullptr))
  215. {
  216. LAYOUT_OVERFLOW lo = LAYOUT_OVERFLOW_CLIP;
  217. if (strstr(pos, "scroll"))
  218. lo = LAYOUT_OVERFLOW_SCROLL;
  219. SetLayoutOverflow(lo);
  220. }
  221. if (const char *dist = info.node->GetValueString("distribution", nullptr))
  222. {
  223. LAYOUT_DISTRIBUTION ld = LAYOUT_DISTRIBUTION_PREFERRED;
  224. if (strstr(dist, "available"))
  225. ld = LAYOUT_DISTRIBUTION_AVAILABLE;
  226. else if (strstr(dist, "gravity"))
  227. ld = LAYOUT_DISTRIBUTION_GRAVITY;
  228. SetLayoutDistribution(ld);
  229. }
  230. if (const char *dist = info.node->GetValueString("distribution-position", nullptr))
  231. {
  232. LAYOUT_DISTRIBUTION_POSITION ld = LAYOUT_DISTRIBUTION_POSITION_CENTER;
  233. if (strstr(dist, "left") || strstr(dist, "top"))
  234. ld = LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP;
  235. else if (strstr(dist, "right") || strstr(dist, "bottom"))
  236. ld = LAYOUT_DISTRIBUTION_POSITION_RIGHT_BOTTOM;
  237. SetLayoutDistributionPosition(ld);
  238. }
  239. // ATOMIC BEGIN
  240. if (const char *layoutConfig = info.node->GetValueString("config", nullptr))
  241. SetLayoutConfig(layoutConfig);
  242. // ATOMIC END
  243. TBWidget::OnInflate(info);
  244. }
  245. TB_WIDGET_FACTORY(TBScrollContainer, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  246. void TBScrollContainer::OnInflate(const INFLATE_INFO &info)
  247. {
  248. SetGravity(WIDGET_GRAVITY_ALL);
  249. SetAdaptContentSize(info.node->GetValueInt("adapt-content", GetAdaptContentSize()) ? true : false);
  250. SetAdaptToContentSize(info.node->GetValueInt("adapt-to-content", GetAdaptToContentSize()) ? true : false);
  251. if (const char *mode = info.node->GetValueString("scroll-mode", nullptr))
  252. {
  253. if (!strcmp(mode, "xy")) SetScrollMode(SCROLL_MODE_X_Y);
  254. if (!strcmp(mode, "y")) SetScrollMode(SCROLL_MODE_Y);
  255. if (!strcmp(mode, "y-auto")) SetScrollMode(SCROLL_MODE_Y_AUTO);
  256. if (!strcmp(mode, "auto")) SetScrollMode(SCROLL_MODE_X_AUTO_Y_AUTO);
  257. if (!strcmp(mode, "off")) SetScrollMode(SCROLL_MODE_OFF);
  258. }
  259. TBWidget::OnInflate(info);
  260. }
  261. TB_WIDGET_FACTORY(TBTabContainer, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  262. void TBTabContainer::OnInflate(const INFLATE_INFO &info)
  263. {
  264. TBWidget::OnInflate(info);
  265. if (const char *align = info.node->GetValueString("align", nullptr))
  266. {
  267. if (!strcmp(align, "left")) SetAlignment(TB_ALIGN_LEFT);
  268. else if (!strcmp(align, "top")) SetAlignment(TB_ALIGN_TOP);
  269. else if (!strcmp(align, "right")) SetAlignment(TB_ALIGN_RIGHT);
  270. else if (!strcmp(align, "bottom")) SetAlignment(TB_ALIGN_BOTTOM);
  271. }
  272. // Allow additional attributes to be specified for the "tabs", "content" and "root" layouts by
  273. // calling OnInflate.
  274. if (TBNode *tabs = info.node->GetNode("tabs"))
  275. {
  276. // Inflate the tabs widgets into the tab layout.
  277. TBLayout *tab_layout = GetTabLayout();
  278. info.reader->LoadNodeTree(tab_layout, tabs);
  279. INFLATE_INFO inflate_info(info.reader, tab_layout->GetContentRoot(), tabs, TBValue::TYPE_NULL);
  280. tab_layout->OnInflate(inflate_info);
  281. }
  282. if (TBNode *tabs = info.node->GetNode("content"))
  283. {
  284. INFLATE_INFO inflate_info(info.reader, GetContentRoot(), tabs, TBValue::TYPE_NULL);
  285. GetContentRoot()->OnInflate(inflate_info);
  286. }
  287. if (TBNode *tabs = info.node->GetNode("root"))
  288. {
  289. INFLATE_INFO inflate_info(info.reader, &m_root_layout, tabs, TBValue::TYPE_NULL);
  290. m_root_layout.OnInflate(inflate_info);
  291. }
  292. }
  293. TB_WIDGET_FACTORY(TBScrollBar, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  294. void TBScrollBar::OnInflate(const INFLATE_INFO &info)
  295. {
  296. const char *axis = info.node->GetValueString("axis", "x");
  297. SetAxis(*axis == 'x' ? AXIS_X : AXIS_Y);
  298. SetGravity(*axis == 'x' ? WIDGET_GRAVITY_LEFT_RIGHT : WIDGET_GRAVITY_TOP_BOTTOM);
  299. // ATOMIC BEGIN
  300. double min = (double)info.node->GetValueFloat("min", (float)GetMinValue());
  301. double max = (double)info.node->GetValueFloat("max", (float)GetMaxValue());
  302. double thumb = (double)info.node->GetValueFloat("thumb", (float)GetVisible()); // use thumb, visible is already used.
  303. SetLimits(min, max, thumb);
  304. // ATOMIC END
  305. TBWidget::OnInflate(info);
  306. }
  307. TB_WIDGET_FACTORY(TBSlider, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  308. void TBSlider::OnInflate(const INFLATE_INFO &info)
  309. {
  310. const char *axis = info.node->GetValueString("axis", "x");
  311. SetAxis(*axis == 'x' ? AXIS_X : AXIS_Y);
  312. SetGravity(*axis == 'x' ? WIDGET_GRAVITY_LEFT_RIGHT : WIDGET_GRAVITY_TOP_BOTTOM);
  313. double min = (double)info.node->GetValueFloat("min", (float)GetMinValue());
  314. double max = (double)info.node->GetValueFloat("max", (float)GetMaxValue());
  315. SetLimits(min, max);
  316. TBWidget::OnInflate(info);
  317. }
  318. void ReadItems(TBNode *node, TBGenericStringItemSource *target_source)
  319. {
  320. // If there is a items node, loop through all its children and add
  321. // items to the target item source.
  322. if (TBNode *items = node->GetNode("items"))
  323. {
  324. for (TBNode *n = items->GetFirstChild(); n; n = n->GetNext())
  325. {
  326. if (strcmp(n->GetName(), "item") != 0)
  327. continue;
  328. const char *item_str = n->GetValueString("text", "");
  329. TBID item_id;
  330. if (TBNode *n_id = n->GetNode("id"))
  331. TBWidgetsReader::SetIDFromNode(item_id, n_id);
  332. TBGenericStringItem *item = new TBGenericStringItem(item_str, item_id);
  333. if (!item || !target_source->AddItem(item))
  334. {
  335. // Out of memory
  336. delete item;
  337. break;
  338. }
  339. }
  340. }
  341. }
  342. TB_WIDGET_FACTORY(TBSelectList, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  343. void TBSelectList::OnInflate(const INFLATE_INFO &info)
  344. {
  345. // Read items (if there is any) into the default source
  346. ReadItems(info.node, GetDefaultSource());
  347. TBWidget::OnInflate(info);
  348. }
  349. TB_WIDGET_FACTORY(TBSelectDropdown, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  350. void TBSelectDropdown::OnInflate(const INFLATE_INFO &info)
  351. {
  352. // Read items (if there is any) into the default source
  353. ReadItems(info.node, GetDefaultSource());
  354. TBWidget::OnInflate(info);
  355. }
  356. TB_WIDGET_FACTORY(TBCheckBox, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  357. TB_WIDGET_FACTORY(TBRadioButton, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  358. TB_WIDGET_FACTORY(TBTextField, TBValue::TYPE_STRING, WIDGET_Z_TOP) {}
  359. void TBTextField::OnInflate(const INFLATE_INFO &info)
  360. {
  361. if (const char *text_align = info.node->GetValueString("text-align", nullptr))
  362. {
  363. if (!strcmp(text_align, "left")) SetTextAlign(TB_TEXT_ALIGN_LEFT);
  364. else if (!strcmp(text_align, "center")) SetTextAlign(TB_TEXT_ALIGN_CENTER);
  365. else if (!strcmp(text_align, "right")) SetTextAlign(TB_TEXT_ALIGN_RIGHT);
  366. }
  367. //ATOMIC BEGIN
  368. SetSqueezable(info.node->GetValueInt("squeezable", GetSqueezable()) ? true : false);
  369. //ATOMIC END
  370. TBWidget::OnInflate(info);
  371. }
  372. TB_WIDGET_FACTORY(TBSkinImage, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  373. TB_WIDGET_FACTORY(TBSeparator, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  374. TB_WIDGET_FACTORY(TBProgressSpinner, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  375. TB_WIDGET_FACTORY(TBContainer, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  376. TB_WIDGET_FACTORY(TBSectionHeader, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  377. TB_WIDGET_FACTORY(TBSection, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  378. TB_WIDGET_FACTORY(TBToggleContainer, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  379. void TBToggleContainer::OnInflate(const INFLATE_INFO &info)
  380. {
  381. if (const char *toggle = info.node->GetValueString("toggle", nullptr))
  382. {
  383. if (stristr(toggle, "enabled")) SetToggle(TBToggleContainer::TOGGLE_ENABLED);
  384. else if (stristr(toggle, "opacity")) SetToggle(TBToggleContainer::TOGGLE_OPACITY);
  385. else if (stristr(toggle, "expanded")) SetToggle(TBToggleContainer::TOGGLE_EXPANDED);
  386. }
  387. SetInvert(info.node->GetValueInt("invert", GetInvert()) ? true : false);
  388. TBWidget::OnInflate(info);
  389. }
  390. #ifdef TB_IMAGE
  391. TB_WIDGET_FACTORY(TBImageWidget, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  392. void TBImageWidget::OnInflate(const INFLATE_INFO &info)
  393. {
  394. if (const char *filename = info.node->GetValueString("filename", nullptr))
  395. SetImage(filename);
  396. TBWidget::OnInflate(info);
  397. }
  398. #endif // TB_IMAGE
  399. // == TBWidgetFactory ===================================
  400. // We can't use a linked list object since we don't know if its constructor
  401. // would run before of after any widget factory constructor that add itself
  402. // to it. Using a manual one way link list is very simple.
  403. TBWidgetFactory *g_registered_factories = nullptr;
  404. TBWidgetFactory::TBWidgetFactory(const char *name, TBValue::TYPE sync_type)
  405. : name(name)
  406. , sync_type(sync_type)
  407. , next_registered_wf(nullptr)
  408. {
  409. }
  410. void TBWidgetFactory::Register()
  411. {
  412. next_registered_wf = g_registered_factories;
  413. g_registered_factories = this;
  414. }
  415. // == TBWidgetsReader ===================================
  416. TBWidgetsReader *TBWidgetsReader::Create()
  417. {
  418. TBWidgetsReader *w_reader = new TBWidgetsReader;
  419. if (!w_reader || !w_reader->Init())
  420. {
  421. delete w_reader;
  422. return nullptr;
  423. }
  424. return w_reader;
  425. }
  426. bool TBWidgetsReader::Init()
  427. {
  428. for (TBWidgetFactory *wf = g_registered_factories; wf; wf = wf->next_registered_wf)
  429. if (!AddFactory(wf))
  430. return false;
  431. return true;
  432. }
  433. TBWidgetsReader::~TBWidgetsReader()
  434. {
  435. }
  436. bool TBWidgetsReader::LoadFile(TBWidget *target, const char *filename)
  437. {
  438. TBNode node;
  439. if (!node.ReadFile(filename))
  440. return false;
  441. LoadNodeTree(target, &node);
  442. return true;
  443. }
  444. bool TBWidgetsReader::LoadData(TBWidget *target, const char *data)
  445. {
  446. TBNode node;
  447. node.ReadData(data);
  448. LoadNodeTree(target, &node);
  449. return true;
  450. }
  451. bool TBWidgetsReader::LoadData(TBWidget *target, const char *data, int data_len)
  452. {
  453. TBNode node;
  454. node.ReadData(data, data_len);
  455. LoadNodeTree(target, &node);
  456. return true;
  457. }
  458. void TBWidgetsReader::LoadNodeTree(TBWidget *target, TBNode *node)
  459. {
  460. // Iterate through all nodes and create widgets
  461. for (TBNode *child = node->GetFirstChild(); child; child = child->GetNext())
  462. CreateWidget(target, child);
  463. }
  464. void TBWidgetsReader::SetIDFromNode(TBID &id, TBNode *node)
  465. {
  466. if (!node)
  467. return;
  468. if (node->GetValue().IsString())
  469. id.Set(node->GetValue().GetString());
  470. else
  471. id.Set(node->GetValue().GetInt());
  472. }
  473. bool TBWidgetsReader::CreateWidget(TBWidget *target, TBNode *node)
  474. {
  475. // Find a widget creator from the node name
  476. TBWidgetFactory *wc = nullptr;
  477. for (wc = factories.GetFirst(); wc; wc = wc->GetNext())
  478. if (strcmp(node->GetName(), wc->name) == 0)
  479. break;
  480. if (!wc)
  481. return false;
  482. // Create the widget
  483. INFLATE_INFO info(this, target->GetContentRoot(), node, wc->sync_type);
  484. TBWidget *new_widget = wc->Create(&info);
  485. if (!new_widget)
  486. return false;
  487. // Read properties and add i to the hierarchy.
  488. new_widget->OnInflate(info);
  489. // If this assert is trigged, you probably forgot to call TBWidget::OnInflate from an overridden version.
  490. assert(new_widget->GetParent());
  491. // Iterate through all nodes and create widgets
  492. for (TBNode *n = node->GetFirstChild(); n; n = n->GetNext())
  493. CreateWidget(new_widget, n);
  494. if (node->GetValueInt("autofocus", 0))
  495. new_widget->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  496. return true;
  497. }
  498. }; // namespace tb