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