1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/xrc/xmlreshandler.cpp
3 // Purpose: XML resource handler
4 // Author: Steven Lamerton
7 // Copyright: (c) 2011 Steven Lamerton
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_XRC_XMLRESHANDLER_H_
12 #define _WX_XRC_XMLRESHANDLER_H_
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"
25 class WXDLLIMPEXP_FWD_ADV wxAnimation
;
27 class WXDLLIMPEXP_FWD_XML wxXmlNode
;
28 class WXDLLIMPEXP_FWD_XML wxXmlResource
;
30 class WXDLLIMPEXP_FWD_CORE wxXmlResourceHandler
;
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)
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.
41 class WXDLLIMPEXP_CORE wxXmlResourceHandlerImplBase
: public wxObject
45 wxXmlResourceHandlerImplBase(wxXmlResourceHandler
*handler
)
50 virtual ~wxXmlResourceHandlerImplBase() {}
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;
91 #if wxUSE_ANIMATIONCTRL
92 virtual wxAnimation
* GetAnimation(const wxString
& param
= wxT("animation")) = 0;
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;
105 virtual wxFileSystem
& GetCurFileSystem() = 0;
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;
111 wxXmlResourceHandler
* GetHandler() { return m_handler
; }
114 wxXmlResourceHandler
*m_handler
;
117 // Base class for all XRC handlers.
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.
124 // Also note that all the methods forwarding to GetImpl() are documented only
125 // in wxXmlResourceHandlerImpl in wx/xrc/xmlres.h to avoid duplication.
127 class WXDLLIMPEXP_CORE wxXmlResourceHandler
: public wxObject
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()
137 // This should be called exactly once.
138 void SetImpl(wxXmlResourceHandlerImplBase
* impl
)
140 wxASSERT_MSG( !m_impl
, wxS("Should be called exactly once") );
147 virtual ~wxXmlResourceHandler()
152 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
155 return GetImpl()->CreateResource(node
, parent
, instance
);
158 // This one is called from CreateResource after variables
160 virtual wxObject
*DoCreateResource() = 0;
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;
167 void SetParentResource(wxXmlResource
*res
)
173 // These methods are not forwarded to wxXmlResourceHandlerImpl because they
174 // are called from the derived classes ctors and so before SetImpl() can be
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
);
181 // Add styles common to all wxWindow-derived classes.
182 void AddWindowStyles();
185 // Everything else is simply forwarded to wxXmlResourceHandlerImpl.
186 void ReportError(wxXmlNode
*context
, const wxString
& message
)
188 GetImpl()->ReportError(context
, message
);
190 void ReportError(const wxString
& message
)
192 GetImpl()->ReportError(message
);
194 void ReportParamError(const wxString
& param
, const wxString
& message
)
196 GetImpl()->ReportParamError(param
, message
);
199 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
) const
201 return GetImpl()->IsOfClass(node
, classname
);
203 wxString
GetNodeContent(const wxXmlNode
*node
)
205 return GetImpl()->GetNodeContent(node
);
207 bool HasParam(const wxString
& param
)
209 return GetImpl()->HasParam(param
);
212 wxXmlNode
*GetParamNode(const wxString
& param
)
214 return GetImpl()->GetParamNode(param
);
216 wxString
GetParamValue(const wxString
& param
)
218 return GetImpl()->GetParamValue(param
);
220 wxString
GetParamValue(const wxXmlNode
* node
)
222 return GetImpl()->GetParamValue(node
);
224 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0)
226 return GetImpl()->GetStyle(param
, defaults
);
228 wxString
GetText(const wxString
& param
, bool translate
= true)
230 return GetImpl()->GetText(param
, translate
);
234 return GetImpl()->GetID();
238 return GetImpl()->GetName();
240 bool GetBool(const wxString
& param
, bool defaultv
= false)
242 return GetImpl()->GetBool(param
, defaultv
);
244 long GetLong(const wxString
& param
, long defaultv
= 0)
246 return GetImpl()->GetLong(param
, defaultv
);
248 float GetFloat(const wxString
& param
, float defaultv
= 0)
250 return GetImpl()->GetFloat(param
, defaultv
);
252 wxColour
GetColour(const wxString
& param
,
253 const wxColour
& defaultv
= wxNullColour
)
255 return GetImpl()->GetColour(param
, defaultv
);
257 wxSize
GetSize(const wxString
& param
= wxT("size"),
258 wxWindow
*windowToUse
= NULL
)
260 return GetImpl()->GetSize(param
, windowToUse
);
262 wxPoint
GetPosition(const wxString
& param
= wxT("pos"))
264 return GetImpl()->GetPosition(param
);
266 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
267 wxWindow
*windowToUse
= NULL
)
269 return GetImpl()->GetDimension(param
, defaultv
, windowToUse
);
271 wxDirection
GetDirection(const wxString
& param
, wxDirection dir
= wxLEFT
)
273 return GetImpl()->GetDirection(param
, dir
);
275 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
276 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
277 wxSize size
= wxDefaultSize
)
279 return GetImpl()->GetBitmap(param
, defaultArtClient
, size
);
281 wxBitmap
GetBitmap(const wxXmlNode
* node
,
282 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
283 wxSize size
= wxDefaultSize
)
285 return GetImpl()->GetBitmap(node
, defaultArtClient
, size
);
287 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
288 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
289 wxSize size
= wxDefaultSize
)
291 return GetImpl()->GetIcon(param
, defaultArtClient
, size
);
293 wxIcon
GetIcon(const wxXmlNode
* node
,
294 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
295 wxSize size
= wxDefaultSize
)
297 return GetImpl()->GetIcon(node
, defaultArtClient
, size
);
299 wxIconBundle
GetIconBundle(const wxString
& param
,
300 const wxArtClient
& defaultArtClient
= wxART_OTHER
)
302 return GetImpl()->GetIconBundle(param
, defaultArtClient
);
304 wxImageList
*GetImageList(const wxString
& param
= wxT("imagelist"))
306 return GetImpl()->GetImageList(param
);
309 #if wxUSE_ANIMATIONCTRL
310 wxAnimation
* GetAnimation(const wxString
& param
= wxT("animation"))
312 return GetImpl()->GetAnimation(param
);
316 wxFont
GetFont(const wxString
& param
= wxT("font"),
317 wxWindow
* parent
= NULL
)
319 return GetImpl()->GetFont(param
, parent
);
321 bool GetBoolAttr(const wxString
& attr
, bool defaultv
)
323 return GetImpl()->GetBoolAttr(attr
, defaultv
);
325 void SetupWindow(wxWindow
*wnd
)
327 GetImpl()->SetupWindow(wnd
);
329 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false)
331 GetImpl()->CreateChildren(parent
, this_hnd_only
);
333 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
)
335 GetImpl()->CreateChildrenPrivately(parent
, rootnode
);
337 wxObject
*CreateResFromNode(wxXmlNode
*node
,
338 wxObject
*parent
, wxObject
*instance
= NULL
)
340 return GetImpl()->CreateResFromNode(node
, parent
, instance
);
344 wxFileSystem
& GetCurFileSystem()
346 return GetImpl()->GetCurFileSystem();
350 // Variables (filled by CreateResource)
353 wxObject
*m_parent
, *m_instance
;
354 wxWindow
*m_parentAsWindow
;
355 wxXmlResource
*m_resource
;
357 // provide method access to those member variables
358 wxXmlResource
* GetResource() const { return m_resource
; }
359 wxXmlNode
* GetNode() const { return m_node
; }
360 wxString
GetClass() const { return m_class
; }
361 wxObject
* GetParent() const { return m_parent
; }
362 wxObject
* GetInstance() const { return m_instance
; }
363 wxWindow
* GetParentAsWindow() const { return m_parentAsWindow
; }
366 wxArrayString m_styleNames
;
367 wxArrayInt m_styleValues
;
369 friend class wxXmlResourceHandlerImpl
;
372 // This is supposed to never return NULL because SetImpl() should have been
374 wxXmlResourceHandlerImplBase
* GetImpl() const;
376 wxXmlResourceHandlerImplBase
*m_impl
;
378 wxDECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
);
383 #endif // _WX_XRC_XMLRESHANDLER_H_