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