tb_widgets_reader.cpp 19 KB

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