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 m_parentAsWindow
= NULL
;
143 // This should be called exactly once.
144 void SetImpl(wxXmlResourceHandlerImplBase
* impl
)
146 wxASSERT_MSG( !m_impl
, wxS("Should be called exactly once") );
153 virtual ~wxXmlResourceHandler()
158 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
161 return GetImpl()->CreateResource(node
, parent
, instance
);
164 // This one is called from CreateResource after variables
166 virtual wxObject
*DoCreateResource() = 0;
168 // Returns true if it understands this node and can create
169 // a resource from it, false otherwise.
170 virtual bool CanHandle(wxXmlNode
*node
) = 0;
173 void SetParentResource(wxXmlResource
*res
)
179 // These methods are not forwarded to wxXmlResourceHandlerImpl because they
180 // are called from the derived classes ctors and so before SetImpl() can be
183 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
184 // understood by this handler.
185 void AddStyle(const wxString
& name
, int value
);
187 // Add styles common to all wxWindow-derived classes.
188 void AddWindowStyles();
191 // Everything else is simply forwarded to wxXmlResourceHandlerImpl.
192 void ReportError(wxXmlNode
*context
, const wxString
& message
)
194 GetImpl()->ReportError(context
, message
);
196 void ReportError(const wxString
& message
)
198 GetImpl()->ReportError(message
);
200 void ReportParamError(const wxString
& param
, const wxString
& message
)
202 GetImpl()->ReportParamError(param
, message
);
205 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
) const
207 return GetImpl()->IsOfClass(node
, classname
);
209 wxString
GetNodeContent(const wxXmlNode
*node
)
211 return GetImpl()->GetNodeContent(node
);
213 bool HasParam(const wxString
& param
)
215 return GetImpl()->HasParam(param
);
218 wxXmlNode
*GetParamNode(const wxString
& param
)
220 return GetImpl()->GetParamNode(param
);
222 wxString
GetParamValue(const wxString
& param
)
224 return GetImpl()->GetParamValue(param
);
226 wxString
GetParamValue(const wxXmlNode
* node
)
228 return GetImpl()->GetParamValue(node
);
230 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0)
232 return GetImpl()->GetStyle(param
, defaults
);
234 wxString
GetText(const wxString
& param
, bool translate
= true)
236 return GetImpl()->GetText(param
, translate
);
240 return GetImpl()->GetID();
244 return GetImpl()->GetName();
246 bool GetBool(const wxString
& param
, bool defaultv
= false)
248 return GetImpl()->GetBool(param
, defaultv
);
250 long GetLong(const wxString
& param
, long defaultv
= 0)
252 return GetImpl()->GetLong(param
, defaultv
);
254 float GetFloat(const wxString
& param
, float defaultv
= 0)
256 return GetImpl()->GetFloat(param
, defaultv
);
258 wxColour
GetColour(const wxString
& param
,
259 const wxColour
& defaultv
= wxNullColour
)
261 return GetImpl()->GetColour(param
, defaultv
);
263 wxSize
GetSize(const wxString
& param
= wxT("size"),
264 wxWindow
*windowToUse
= NULL
)
266 return GetImpl()->GetSize(param
, windowToUse
);
268 wxPoint
GetPosition(const wxString
& param
= wxT("pos"))
270 return GetImpl()->GetPosition(param
);
272 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
273 wxWindow
*windowToUse
= NULL
)
275 return GetImpl()->GetDimension(param
, defaultv
, windowToUse
);
277 wxDirection
GetDirection(const wxString
& param
, wxDirection dir
= wxLEFT
)
279 return GetImpl()->GetDirection(param
, dir
);
281 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
282 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
283 wxSize size
= wxDefaultSize
)
285 return GetImpl()->GetBitmap(param
, defaultArtClient
, size
);
287 wxBitmap
GetBitmap(const wxXmlNode
* node
,
288 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
289 wxSize size
= wxDefaultSize
)
291 return GetImpl()->GetBitmap(node
, defaultArtClient
, size
);
293 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
294 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
295 wxSize size
= wxDefaultSize
)
297 return GetImpl()->GetIcon(param
, defaultArtClient
, size
);
299 wxIcon
GetIcon(const wxXmlNode
* node
,
300 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
301 wxSize size
= wxDefaultSize
)
303 return GetImpl()->GetIcon(node
, defaultArtClient
, size
);
305 wxIconBundle
GetIconBundle(const wxString
& param
,
306 const wxArtClient
& defaultArtClient
= wxART_OTHER
)
308 return GetImpl()->GetIconBundle(param
, defaultArtClient
);
310 wxImageList
*GetImageList(const wxString
& param
= wxT("imagelist"))
312 return GetImpl()->GetImageList(param
);
315 #if wxUSE_ANIMATIONCTRL
316 wxAnimation
* GetAnimation(const wxString
& param
= wxT("animation"))
318 return GetImpl()->GetAnimation(param
);
322 wxFont
GetFont(const wxString
& param
= wxT("font"),
323 wxWindow
* parent
= NULL
)
325 return GetImpl()->GetFont(param
, parent
);
327 bool GetBoolAttr(const wxString
& attr
, bool defaultv
)
329 return GetImpl()->GetBoolAttr(attr
, defaultv
);
331 void SetupWindow(wxWindow
*wnd
)
333 GetImpl()->SetupWindow(wnd
);
335 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false)
337 GetImpl()->CreateChildren(parent
, this_hnd_only
);
339 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
)
341 GetImpl()->CreateChildrenPrivately(parent
, rootnode
);
343 wxObject
*CreateResFromNode(wxXmlNode
*node
,
344 wxObject
*parent
, wxObject
*instance
= NULL
)
346 return GetImpl()->CreateResFromNode(node
, parent
, instance
);
350 wxFileSystem
& GetCurFileSystem()
352 return GetImpl()->GetCurFileSystem();
356 // Variables (filled by CreateResource)
359 wxObject
*m_parent
, *m_instance
;
360 wxWindow
*m_parentAsWindow
;
361 wxXmlResource
*m_resource
;
363 // provide method access to those member variables
364 wxXmlResource
* GetResource() const { return m_resource
; }
365 wxXmlNode
* GetNode() const { return m_node
; }
366 wxString
GetClass() const { return m_class
; }
367 wxObject
* GetParent() const { return m_parent
; }
368 wxObject
* GetInstance() const { return m_instance
; }
369 wxWindow
* GetParentAsWindow() const { return m_parentAsWindow
; }
372 wxArrayString m_styleNames
;
373 wxArrayInt m_styleValues
;
375 friend class wxXmlResourceHandlerImpl
;
378 // This is supposed to never return NULL because SetImpl() should have been
380 wxXmlResourceHandlerImplBase
* GetImpl() const;
382 wxXmlResourceHandlerImplBase
*m_impl
;
384 wxDECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
);
389 #endif // _WX_XRC_XMLRESHANDLER_H_