tb_widgets_reader.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef TBWIDGETS_READER_H
  6. #define TBWIDGETS_READER_H
  7. #include "tb_linklist.h"
  8. #include "tb_widgets.h"
  9. namespace tb {
  10. class TBWidgetsReader;
  11. class TBWidgetFactory;
  12. class TBWidget;
  13. class TBNode;
  14. /** INFLATE_INFO contains info passed to TBWidget::OnInflate during resource loading. */
  15. struct INFLATE_INFO {
  16. INFLATE_INFO(TBWidgetsReader *reader, TBWidget *target, TBNode *node, TBValue::TYPE sync_type)
  17. : reader(reader), target(target), node(node), sync_type(sync_type) {}
  18. TBWidgetsReader *reader;
  19. /** The widget that that will be parent to the inflated widget. */
  20. TBWidget *target;
  21. /** The node containing properties. */
  22. TBNode *node;
  23. /** The data type that should be synchronized through TBWidgetValue. */
  24. TBValue::TYPE sync_type;
  25. };
  26. /** TBWidgetFactory creates a widget from a TBNode. */
  27. class TBWidgetFactory : public TBLinkOf<TBWidgetFactory>
  28. {
  29. public:
  30. TBWidgetFactory(const char *name, TBValue::TYPE sync_type);
  31. /** Create and return the new widget or nullptr on out of memory. */
  32. virtual TBWidget *Create(INFLATE_INFO *info) = 0;
  33. void Register();
  34. public:
  35. const char *name;
  36. TBValue::TYPE sync_type;
  37. TBWidgetFactory *next_registered_wf;
  38. };
  39. /** This macro creates a new TBWidgetFactory for the given class name so it can
  40. be created from resources (using TBWidgetsReader).
  41. classname - The name of the class.
  42. sync_type - The data type that should be synchronized through TBWidgetValue.
  43. add_child_z - The order in which children should be added to it by default.
  44. It should be followed by an empty block (may eventually be removed).
  45. Reading custom properties from resources can be done by overriding
  46. TBWidget::OnInflate.
  47. Example:
  48. TB_WIDGET_FACTORY(MyWidget, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  49. */
  50. #define TB_WIDGET_FACTORY(classname, sync_type, add_child_z) \
  51. class classname##WidgetFactory : public TBWidgetFactory \
  52. { \
  53. public: \
  54. classname##WidgetFactory() \
  55. : TBWidgetFactory(#classname, sync_type) { Register(); } \
  56. virtual TBWidget *Create(INFLATE_INFO *info) \
  57. { \
  58. classname *widget = new classname(); \
  59. if (widget) { \
  60. widget->GetContentRoot()->SetZInflate(add_child_z); \
  61. ReadCustomProps(widget, info); \
  62. } \
  63. return widget; \
  64. } \
  65. void ReadCustomProps(classname *widget, INFLATE_INFO *info); \
  66. }; \
  67. static classname##WidgetFactory classname##_wf; \
  68. void classname##WidgetFactory::ReadCustomProps(classname *widget, INFLATE_INFO *info)
  69. /**
  70. TBWidgetsReader parse a resource file (or buffer) into a TBNode tree,
  71. and turn it into a hierarchy of widgets. It can create all types of widgets
  72. that have a registered factory (TBWidgetFactory). All core widgets have
  73. a factory by default, and you can also add your own.
  74. Values may be looked up from any existing TBNodeRefTree using the syntax
  75. "@treename>noderequest". In addition to this, strings will be looked up
  76. from the global TBLanguage by using the syntax "@stringid"
  77. Branches may be included or not depending on the value of a TBNodeRefTree
  78. node, using "@if @treename>noderequest" and optionally a following "@else".
  79. Branches may be included from TBNodeRefTree using
  80. "@include @treename>noderequest", or included from nodes specified previosly
  81. in the same tree using "@include noderequest".
  82. Files can be included by using the syntax "@file filename".
  83. Each factory may have its own set of properties, but a set of generic
  84. properties is always supported on all widgets. Those are:
  85. Resource name: TBWidget property: Values:
  86. id TBWidget::m_id TBID (string or int)
  87. group-id TBWidget::m_group_id TBID (string or int)
  88. value TBWidget::SetValue integer
  89. data TBWidget::m_data integer
  90. is-group-root TBWidget::SetIsGroupRoot boolean
  91. is-focusable TBWidget::SetIsFocusable boolean
  92. want-long-click TBWidget::SetWantLongClick boolean
  93. ignore-input TBWidget::SetIgnoreInput boolean
  94. opacity TBWidget::SetOpacity float (0 - 1)
  95. disabledOpacity TBWidget::SetDisabledOpacity float (0 - 1)
  96. text TBWidget::SetText string
  97. connection TBWidget::Connect string
  98. axis TBWidget::SetAxis x or y
  99. gravity TBWidget::SetGravity string (combination of left, top, right, bottom, or all)
  100. visibility TBWidget::SetVisibility string (visible, invisible, gone)
  101. state TBWidget::SetState string (disabled)
  102. skin TBWidget::SetSkinBg TBID (string or int)
  103. rect TBWidget::SetRect 4 integers (x, y, width, height)
  104. lp>width TBWidget::SetLayoutParams dimension
  105. lp>min-width TBWidget::SetLayoutParams dimension
  106. lp>max-width TBWidget::SetLayoutParams dimension
  107. lp>pref-width TBWidget::SetLayoutParams dimension
  108. lp>height TBWidget::SetLayoutParams dimension
  109. lp>min-height TBWidget::SetLayoutParams dimension
  110. lp>max-height TBWidget::SetLayoutParams dimension
  111. lp>pref-height TBWidget::SetLayoutParams dimension
  112. autofocus The TBWidget will be focused automatically the first time its TBWindow is activated.
  113. font>name Font name
  114. font>size Font size
  115. */
  116. class TBWidgetsReader
  117. {
  118. public:
  119. static TBWidgetsReader *Create();
  120. ~TBWidgetsReader();
  121. /** Add a widget factory. Does not take ownership of the factory.
  122. The easiest way to add factories for custom widget types, is using the
  123. TB_WIDGET_FACTORY macro that automatically register it during startup. */
  124. bool AddFactory(TBWidgetFactory *wf) { factories.AddLast(wf); return true; }
  125. void RemoveFactory(TBWidgetFactory *wf) { factories.Remove(wf); }
  126. /** Set the id from the given node. */
  127. static void SetIDFromNode(TBID &id, TBNode *node);
  128. bool LoadFile(TBWidget *target, const char *filename);
  129. bool LoadData(TBWidget *target, const char *data);
  130. bool LoadData(TBWidget *target, const char *data, int data_len);
  131. void LoadNodeTree(TBWidget *target, TBNode *node);
  132. private:
  133. bool Init();
  134. bool CreateWidget(TBWidget *target, TBNode *node);
  135. TBLinkListOf<TBWidgetFactory> factories;
  136. };
  137. }; // namespace tb
  138. #endif // TBWIDGETS_READER_H