Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_widgets_reader.h
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 
6 #ifndef TBWIDGETS_READER_H
7 #define TBWIDGETS_READER_H
8 
9 #include "tb_linklist.h"
10 #include "tb_widgets.h"
11 #include <iostream>
12 
13 namespace tb {
14 
15 class TBWidgetsReader;
16 class TBWidgetFactory;
17 class TBWidget;
18 class TBNode;
19 
21 struct INFLATE_INFO {
23  : reader(reader), target(target), node(node) , sync_type(sync_type) {}
24  TBWidgetsReader *reader;
25 
32 };
33 
35 class TBWidgetFactory : public TBLinkOf<TBWidgetFactory>
36 {
37 public:
38  TBWidgetFactory(const char *name, TBValue::TYPE s);
39 
41  virtual TBWidget *Create(INFLATE_INFO *info) = 0;
42 
43  void Register();
44 public:
45  const char *name;
46  TBValue::TYPE sync_type;
47  TBWidgetFactory *next_registered_wf;
48 };
49 
65 #define TB_WIDGET_FACTORY(classname, sync_type, add_child_z) \
66  class classname##WidgetFactory : public TBWidgetFactory \
67  { \
68  public: \
69  classname##WidgetFactory() \
70  : TBWidgetFactory(#classname, sync_type) { Register(); } \
71  virtual TBWidget *Create(INFLATE_INFO *info) \
72  { \
73  classname *widget = new classname(); \
74  if (widget) { \
75  if (widget->m_sync_type != sync_type) std::cerr << #classname "ERROR\n"; \
76  widget->GetContentRoot()->SetZInflate(add_child_z); \
77  ReadCustomProps(widget, info); \
78  } \
79  return widget; \
80  } \
81  void ReadCustomProps(classname *widget, INFLATE_INFO *info); \
82  }; \
83  static classname##WidgetFactory classname##_wf; \
84  void classname##WidgetFactory::ReadCustomProps(classname *widget, INFLATE_INFO *info)
85 
141 {
142 public:
143  static TBWidgetsReader *Create();
144  ~TBWidgetsReader();
145 
149  bool AddFactory(TBWidgetFactory *wf) { factories.AddLast(wf); return true; }
150  void RemoveFactory(TBWidgetFactory *wf) { factories.Remove(wf); }
151 
153  static void SetIDFromNode(TBID &id, TBNode *node);
154 
155  bool LoadFile(TBWidget *target, const TBStr & filename);
156  bool LoadFormatted(TBWidget *target, const char *format, ...) TB_POST_FORMAT(3,4);
157  bool LoadData(TBWidget *target, const char *data);
158  bool LoadData(TBWidget *target, const char *data, int data_len);
159  void LoadNodeTree(TBWidget *target, TBNode *node);
160 
161  bool DumpFile(TBWidget *source, const TBStr & filename);
162  bool DumpData(TBWidget *source, TBStr & data);
163 
164 private:
165  bool Init();
166  bool CreateWidget(TBWidget *target, TBNode *node);
167  bool CreateNode(TBNode *target, TBWidget *widget);
168  TBLinkListOf<TBWidgetFactory> factories;
169 };
170 
171 } // namespace tb
172 
173 #endif // TBWIDGETS_READER_H
bool AddFactory(TBWidgetFactory *wf)
Add a widget factory.
Definition: tb_widgets_reader.h:149
TBWidgetsReader parse a resource file (or buffer) into a TBNode tree, and turns it into a hierarchy o...
Definition: tb_widgets_reader.h:140
TBWidgetFactory creates a widget from a TBNode.
Definition: tb_widgets_reader.h:35
TYPE
The current type of the value.
Definition: tb_value.h:64
The base TBWidget class.
Definition: tb_widgets.h:418
TBNode * node
The node containing properties.
Definition: tb_widgets_reader.h:29
static void SetIDFromNode(TBID &id, TBNode *node)
Set the id from the given node.
Definition: tb_widgets_reader.cpp:897
INFLATE_INFO contains info passed to TBWidget::OnInflate during resource loading. ...
Definition: tb_widgets_reader.h:21
virtual TBWidget * Create(INFLATE_INFO *info)=0
Create and return the new widget or nullptr on out of memory.
TBWidget * target
The widget that that will be parent to the inflated widget.
Definition: tb_widgets_reader.h:27
TBNode is a tree node with a string name and a value (TBValue).
Definition: tb_node_tree.h:30
TBValue::TYPE sync_type
The data type that should be synchronized through TBWidgetValue.
Definition: tb_widgets_reader.h:31