]> git.saurik.com Git - wxWidgets.git/blob - include/wx/xrc/xmlreshandler.h
Include wx/xml/xml.h from the XRC handlers that need it.
[wxWidgets.git] / include / wx / xrc / xmlreshandler.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/xrc/xmlreshandler.cpp
3 // Purpose: XML resource handler
4 // Author: Steven Lamerton
5 // Created: 2011/01/26
6 // RCS-ID: $id$
7 // Copyright: (c) 2011 Steven Lamerton
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_XRC_XMLRESHANDLER_H_
12 #define _WX_XRC_XMLRESHANDLER_H_
13
14 #include "wx/defs.h"
15
16 #if wxUSE_XRC
17
18 #include "wx/string.h"
19 #include "wx/artprov.h"
20 #include "wx/colour.h"
21 #include "wx/animate.h"
22 #include "wx/filesys.h"
23 #include "wx/imaglist.h"
24
25 class WXDLLIMPEXP_FWD_XML wxXmlNode;
26 class WXDLLIMPEXP_FWD_XML wxXmlResource;
27
28 class WXDLLIMPEXP_FWD_CORE wxXmlResourceHandler;
29
30 // Helper macro used by the classes derived from wxXmlResourceHandler but also
31 // by wxXmlResourceHandler implementation itself.
32 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
33
34 // Abstract base class for the implementation object used by
35 // wxXmlResourceHandlerImpl. The real implementation is in
36 // wxXmlResourceHandlerImpl class in the "xrc" library while this class is in
37 // the "core" itself -- but it is so small that it doesn't matter.
38
39 class WXDLLIMPEXP_CORE wxXmlResourceHandlerImplBase : public wxObject
40 {
41 public:
42 // Constructor.
43 wxXmlResourceHandlerImplBase(wxXmlResourceHandler *handler)
44 : m_handler(handler)
45 {}
46
47 // Destructor.
48 virtual ~wxXmlResourceHandlerImplBase() {}
49
50 virtual wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
51 wxObject *instance) = 0;
52 virtual bool IsOfClass(wxXmlNode *node, const wxString& classname) const = 0;
53 virtual wxString GetNodeContent(const wxXmlNode *node) = 0;
54 virtual bool HasParam(const wxString& param) = 0;
55 virtual wxXmlNode *GetParamNode(const wxString& param) = 0;
56 virtual wxString GetParamValue(const wxString& param) = 0;
57 virtual wxString GetParamValue(const wxXmlNode* node) = 0;
58 virtual int GetStyle(const wxString& param = wxT("style"), int defaults = 0) = 0;
59 virtual wxString GetText(const wxString& param, bool translate = true) = 0;
60 virtual int GetID() = 0;
61 virtual wxString GetName() = 0;
62 virtual bool GetBool(const wxString& param, bool defaultv = false) = 0;
63 virtual long GetLong(const wxString& param, long defaultv = 0) = 0;
64 virtual float GetFloat(const wxString& param, float defaultv = 0) = 0;
65 virtual wxColour GetColour(const wxString& param,
66 const wxColour& defaultv = wxNullColour) = 0;
67 virtual wxSize GetSize(const wxString& param = wxT("size"),
68 wxWindow *windowToUse = NULL) = 0;
69 virtual wxPoint GetPosition(const wxString& param = wxT("pos")) = 0;
70 virtual wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0,
71 wxWindow *windowToUse = NULL) = 0;
72 virtual wxDirection GetDirection(const wxString& param, wxDirection dir = wxLEFT) = 0;
73 virtual wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
74 const wxArtClient& defaultArtClient = wxART_OTHER,
75 wxSize size = wxDefaultSize) = 0;
76 virtual wxBitmap GetBitmap(const wxXmlNode* node,
77 const wxArtClient& defaultArtClient = wxART_OTHER,
78 wxSize size = wxDefaultSize) = 0;
79 virtual wxIcon GetIcon(const wxString& param = wxT("icon"),
80 const wxArtClient& defaultArtClient = wxART_OTHER,
81 wxSize size = wxDefaultSize) = 0;
82 virtual wxIcon GetIcon(const wxXmlNode* node,
83 const wxArtClient& defaultArtClient = wxART_OTHER,
84 wxSize size = wxDefaultSize) = 0;
85 virtual wxIconBundle GetIconBundle(const wxString& param,
86 const wxArtClient& defaultArtClient = wxART_OTHER) = 0;
87 virtual wxImageList *GetImageList(const wxString& param = wxT("imagelist")) = 0;
88
89 #if wxUSE_ANIMATIONCTRL
90 virtual wxAnimation GetAnimation(const wxString& param = wxT("animation")) = 0;
91 #endif
92
93 virtual wxFont GetFont(const wxString& param = wxT("font"), wxWindow* parent = NULL) = 0;
94 virtual bool GetBoolAttr(const wxString& attr, bool defaultv) = 0;
95 virtual void SetupWindow(wxWindow *wnd) = 0;
96 virtual void CreateChildren(wxObject *parent, bool this_hnd_only = false) = 0;
97 virtual void CreateChildrenPrivately(wxObject *parent,
98 wxXmlNode *rootnode = NULL) = 0;
99 virtual wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent,
100 wxObject *instance = NULL) = 0;
101
102 #if wxUSE_FILESYSTEM
103 virtual wxFileSystem& GetCurFileSystem() = 0;
104 #endif
105 virtual void ReportError(wxXmlNode *context, const wxString& message) = 0;
106 virtual void ReportError(const wxString& message) = 0;
107 virtual void ReportParamError(const wxString& param, const wxString& message) = 0;
108
109 wxXmlResourceHandler* GetHandler() { return m_handler; }
110
111 protected:
112 wxXmlResourceHandler *m_handler;
113 };
114
115 // Base class for all XRC handlers.
116 //
117 // Notice that this class is defined in the core library itself and so can be
118 // used as the base class by classes in any GUI library. However to actually be
119 // usable, it needs to be registered with wxXmlResource which implies linking
120 // the application with the xrc library.
121 //
122 // Also note that all the methods forwarding to GetImpl() are documented only
123 // in wxXmlResourceHandlerImpl in wx/xrc/xmlres.h to avoid duplication.
124
125 class WXDLLIMPEXP_CORE wxXmlResourceHandler : public wxObject
126 {
127 public:
128 // Constructor creates an unusable object, before anything can be done with
129 // it, SetImpl() needs to be called as done by wxXmlResource::AddHandler().
130 wxXmlResourceHandler()
131 {
132 m_impl = NULL;
133 }
134
135 // This should be called exactly once.
136 void SetImpl(wxXmlResourceHandlerImplBase* impl)
137 {
138 wxASSERT_MSG( !m_impl, wxS("Should be called exactly once") );
139
140 m_impl = impl;
141 }
142
143
144 // Destructor.
145 virtual ~wxXmlResourceHandler()
146 {
147 delete m_impl;
148 }
149
150 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
151 wxObject *instance)
152 {
153 return GetImpl()->CreateResource(node, parent, instance);
154 }
155
156 // This one is called from CreateResource after variables
157 // were filled.
158 virtual wxObject *DoCreateResource() = 0;
159
160 // Returns true if it understands this node and can create
161 // a resource from it, false otherwise.
162 virtual bool CanHandle(wxXmlNode *node) = 0;
163
164
165 void SetParentResource(wxXmlResource *res)
166 {
167 m_resource = res;
168 }
169
170
171 // These methods are not forwarded to wxXmlResourceHandlerImpl because they
172 // are called from the derived classes ctors and so before SetImpl() can be
173 // called.
174
175 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
176 // understood by this handler.
177 void AddStyle(const wxString& name, int value);
178
179 // Add styles common to all wxWindow-derived classes.
180 void AddWindowStyles();
181
182 protected:
183 // Everything else is simply forwarded to wxXmlResourceHandlerImpl.
184 void ReportError(wxXmlNode *context, const wxString& message)
185 {
186 return GetImpl()->ReportError(context, message);
187 }
188 void ReportError(const wxString& message)
189 {
190 return GetImpl()->ReportError(message);
191 }
192 void ReportParamError(const wxString& param, const wxString& message)
193 {
194 return GetImpl()->ReportParamError(param, message);
195 }
196
197 bool IsOfClass(wxXmlNode *node, const wxString& classname) const
198 {
199 return GetImpl()->IsOfClass(node, classname);
200 }
201 wxString GetNodeContent(const wxXmlNode *node)
202 {
203 return GetImpl()->GetNodeContent(node);
204 }
205 bool HasParam(const wxString& param)
206 {
207 return GetImpl()->HasParam(param);
208 }
209
210 wxXmlNode *GetParamNode(const wxString& param)
211 {
212 return GetImpl()->GetParamNode(param);
213 }
214 wxString GetParamValue(const wxString& param)
215 {
216 return GetImpl()->GetParamValue(param);
217 }
218 wxString GetParamValue(const wxXmlNode* node)
219 {
220 return GetImpl()->GetParamValue(node);
221 }
222 int GetStyle(const wxString& param = wxT("style"), int defaults = 0)
223 {
224 return GetImpl()->GetStyle(param, defaults);
225 }
226 wxString GetText(const wxString& param, bool translate = true)
227 {
228 return GetImpl()->GetText(param, translate);
229 }
230 int GetID() const
231 {
232 return GetImpl()->GetID();
233 }
234 wxString GetName()
235 {
236 return GetImpl()->GetName();
237 }
238 bool GetBool(const wxString& param, bool defaultv = false)
239 {
240 return GetImpl()->GetBool(param, defaultv);
241 }
242 long GetLong(const wxString& param, long defaultv = 0)
243 {
244 return GetImpl()->GetLong(param, defaultv);
245 }
246 float GetFloat(const wxString& param, float defaultv = 0)
247 {
248 return GetImpl()->GetFloat(param, defaultv);
249 }
250 wxColour GetColour(const wxString& param,
251 const wxColour& defaultv = wxNullColour)
252 {
253 return GetImpl()->GetColour(param, defaultv);
254 }
255 wxSize GetSize(const wxString& param = wxT("size"),
256 wxWindow *windowToUse = NULL)
257 {
258 return GetImpl()->GetSize(param, windowToUse);
259 }
260 wxPoint GetPosition(const wxString& param = wxT("pos"))
261 {
262 return GetImpl()->GetPosition(param);
263 }
264 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0,
265 wxWindow *windowToUse = NULL)
266 {
267 return GetImpl()->GetDimension(param, defaultv, windowToUse);
268 }
269 wxDirection GetDirection(const wxString& param, wxDirection dir = wxLEFT)
270 {
271 return GetImpl()->GetDirection(param, dir);
272 }
273 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
274 const wxArtClient& defaultArtClient = wxART_OTHER,
275 wxSize size = wxDefaultSize)
276 {
277 return GetImpl()->GetBitmap(param, defaultArtClient, size);
278 }
279 wxBitmap GetBitmap(const wxXmlNode* node,
280 const wxArtClient& defaultArtClient = wxART_OTHER,
281 wxSize size = wxDefaultSize)
282 {
283 return GetImpl()->GetBitmap(node, defaultArtClient, size);
284 }
285 wxIcon GetIcon(const wxString& param = wxT("icon"),
286 const wxArtClient& defaultArtClient = wxART_OTHER,
287 wxSize size = wxDefaultSize)
288 {
289 return GetImpl()->GetIcon(param, defaultArtClient, size);
290 }
291 wxIcon GetIcon(const wxXmlNode* node,
292 const wxArtClient& defaultArtClient = wxART_OTHER,
293 wxSize size = wxDefaultSize)
294 {
295 return GetImpl()->GetIcon(node, defaultArtClient, size);
296 }
297 wxIconBundle GetIconBundle(const wxString& param,
298 const wxArtClient& defaultArtClient = wxART_OTHER)
299 {
300 return GetImpl()->GetIconBundle(param, defaultArtClient);
301 }
302 wxImageList *GetImageList(const wxString& param = wxT("imagelist"))
303 {
304 return GetImpl()->GetImageList(param);
305 }
306
307 #if wxUSE_ANIMATIONCTRL
308 wxAnimation GetAnimation(const wxString& param = wxT("animation"))
309 {
310 return GetImpl()->GetAnimation(param);
311 }
312 #endif
313
314 wxFont GetFont(const wxString& param = wxT("font"),
315 wxWindow* parent = NULL)
316 {
317 return GetImpl()->GetFont(param, parent);
318 }
319 bool GetBoolAttr(const wxString& attr, bool defaultv)
320 {
321 return GetImpl()->GetBoolAttr(attr, defaultv);
322 }
323 void SetupWindow(wxWindow *wnd)
324 {
325 GetImpl()->SetupWindow(wnd);
326 }
327 void CreateChildren(wxObject *parent, bool this_hnd_only = false)
328 {
329 GetImpl()->CreateChildren(parent, this_hnd_only);
330 }
331 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL)
332 {
333 GetImpl()->CreateChildrenPrivately(parent, rootnode);
334 }
335 wxObject *CreateResFromNode(wxXmlNode *node,
336 wxObject *parent, wxObject *instance = NULL)
337 {
338 return GetImpl()->CreateResFromNode(node, parent, instance);
339 }
340
341 #if wxUSE_FILESYSTEM
342 wxFileSystem& GetCurFileSystem()
343 {
344 return GetImpl()->GetCurFileSystem();
345 }
346 #endif
347
348 // Variables (filled by CreateResource)
349 wxXmlNode *m_node;
350 wxString m_class;
351 wxObject *m_parent, *m_instance;
352 wxWindow *m_parentAsWindow;
353 wxXmlResource *m_resource;
354
355 wxArrayString m_styleNames;
356 wxArrayInt m_styleValues;
357
358 friend class wxXmlResourceHandlerImpl;
359
360 private:
361 // This is supposed to never return NULL because SetImpl() should have been
362 // called.
363 wxXmlResourceHandlerImplBase* GetImpl() const;
364
365 wxXmlResourceHandlerImplBase *m_impl;
366
367 wxDECLARE_ABSTRACT_CLASS(wxXmlResourceHandler);
368 };
369
370 #endif // wxUSE_XRC
371
372 #endif // _WX_XRC_XMLRESHANDLER_H_