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"
24 class WXDLLIMPEXP_FWD_ADV wxAnimation
;
26 class WXDLLIMPEXP_FWD_XML wxXmlNode
;
27 class WXDLLIMPEXP_FWD_XML wxXmlResource
;
29 class WXDLLIMPEXP_FWD_CORE wxXmlResourceHandler
;
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)
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.
40 class WXDLLIMPEXP_CORE wxXmlResourceHandlerImplBase
: public wxObject
44 wxXmlResourceHandlerImplBase(wxXmlResourceHandler
*handler
)
49 virtual ~wxXmlResourceHandlerImplBase() {}
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;
90 #if wxUSE_ANIMATIONCTRL
91 virtual wxAnimation
* GetAnimation(const wxString
& param
= wxT("animation")) = 0;
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;
104 virtual wxFileSystem
& GetCurFileSystem() = 0;
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;
110 wxXmlResourceHandler
* GetHandler() { return m_handler
; }
113 wxXmlResourceHandler
*m_handler
;
116 // Base class for all XRC handlers.
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.
123 // Also note that all the methods forwarding to GetImpl() are documented only
124 // in wxXmlResourceHandlerImpl in wx/xrc/xmlres.h to avoid duplication.
126 class WXDLLIMPEXP_CORE wxXmlResourceHandler
: public wxObject
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()
136 // This should be called exactly once.
137 void SetImpl(wxXmlResourceHandlerImplBase
* impl
)
139 wxASSERT_MSG( !m_impl
, wxS("Should be called exactly once") );
146 virtual ~wxXmlResourceHandler()
151 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
154 return GetImpl()->CreateResource(node
, parent
, instance
);
157 // This one is called from CreateResource after variables
159 virtual wxObject
*DoCreateResource() = 0;
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;
166 void SetParentResource(wxXmlResource
*res
)
172 // These methods are not forwarded to wxXmlResourceHandlerImpl because they
173 // are called from the derived classes ctors and so before SetImpl() can be
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
);
180 // Add styles common to all wxWindow-derived classes.
181 void AddWindowStyles();
184 // Everything else is simply forwarded to wxXmlResourceHandlerImpl.
185 void ReportError(wxXmlNode
*context
, const wxString
& message
)
187 return GetImpl()->ReportError(context
, message
);
189 void ReportError(const wxString
& message
)
191 return GetImpl()->ReportError(message
);
193 void ReportParamError(const wxString
& param
, const wxString
& message
)
195 return GetImpl()->ReportParamError(param
, message
);
198 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
) const
200 return GetImpl()->IsOfClass(node
, classname
);
202 wxString
GetNodeContent(const wxXmlNode
*node
)
204 return GetImpl()->GetNodeContent(node
);
206 bool HasParam(const wxString
& param
)
208 return GetImpl()->HasParam(param
);
211 wxXmlNode
*GetParamNode(const wxString
& param
)
213 return GetImpl()->GetParamNode(param
);
215 wxString
GetParamValue(const wxString
& param
)
217 return GetImpl()->GetParamValue(param
);
219 wxString
GetParamValue(const wxXmlNode
* node
)
221 return GetImpl()->GetParamValue(node
);
223 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0)
225 return GetImpl()->GetStyle(param
, defaults
);
227 wxString
GetText(const wxString
& param
, bool translate
= true)
229 return GetImpl()->GetText(param
, translate
);
233 return GetImpl()->GetID();
237 return GetImpl()->GetName();
239 bool GetBool(const wxString
& param
, bool defaultv
= false)
241 return GetImpl()->GetBool(param
, defaultv
);
243 long GetLong(const wxString
& param
, long defaultv
= 0)
245 return GetImpl()->GetLong(param
, defaultv
);
247 float GetFloat(const wxString
& param
, float defaultv
= 0)
249 return GetImpl()->GetFloat(param
, defaultv
);
251 wxColour
GetColour(const wxString
& param
,
252 const wxColour
& defaultv
= wxNullColour
)
254 return GetImpl()->GetColour(param
, defaultv
);
256 wxSize
GetSize(const wxString
& param
= wxT("size"),
257 wxWindow
*windowToUse
= NULL
)
259 return GetImpl()->GetSize(param
, windowToUse
);
261 wxPoint
GetPosition(const wxString
& param
= wxT("pos"))
263 return GetImpl()->GetPosition(param
);
265 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
266 wxWindow
*windowToUse
= NULL
)
268 return GetImpl()->GetDimension(param
, defaultv
, windowToUse
);
270 wxDirection
GetDirection(const wxString
& param
, wxDirection dir
= wxLEFT
)
272 return GetImpl()->GetDirection(param
, dir
);
274 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
275 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
276 wxSize size
= wxDefaultSize
)
278 return GetImpl()->GetBitmap(param
, defaultArtClient
, size
);
280 wxBitmap
GetBitmap(const wxXmlNode
* node
,
281 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
282 wxSize size
= wxDefaultSize
)
284 return GetImpl()->GetBitmap(node
, defaultArtClient
, size
);
286 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
287 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
288 wxSize size
= wxDefaultSize
)
290 return GetImpl()->GetIcon(param
, defaultArtClient
, size
);
292 wxIcon
GetIcon(const wxXmlNode
* node
,
293 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
294 wxSize size
= wxDefaultSize
)
296 return GetImpl()->GetIcon(node
, defaultArtClient
, size
);
298 wxIconBundle
GetIconBundle(const wxString
& param
,
299 const wxArtClient
& defaultArtClient
= wxART_OTHER
)
301 return GetImpl()->GetIconBundle(param
, defaultArtClient
);
303 wxImageList
*GetImageList(const wxString
& param
= wxT("imagelist"))
305 return GetImpl()->GetImageList(param
);
308 #if wxUSE_ANIMATIONCTRL
309 wxAnimation
* GetAnimation(const wxString
& param
= wxT("animation"))
311 return GetImpl()->GetAnimation(param
);
315 wxFont
GetFont(const wxString
& param
= wxT("font"),
316 wxWindow
* parent
= NULL
)
318 return GetImpl()->GetFont(param
, parent
);
320 bool GetBoolAttr(const wxString
& attr
, bool defaultv
)
322 return GetImpl()->GetBoolAttr(attr
, defaultv
);
324 void SetupWindow(wxWindow
*wnd
)
326 GetImpl()->SetupWindow(wnd
);
328 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false)
330 GetImpl()->CreateChildren(parent
, this_hnd_only
);
332 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
)
334 GetImpl()->CreateChildrenPrivately(parent
, rootnode
);
336 wxObject
*CreateResFromNode(wxXmlNode
*node
,
337 wxObject
*parent
, wxObject
*instance
= NULL
)
339 return GetImpl()->CreateResFromNode(node
, parent
, instance
);
343 wxFileSystem
& GetCurFileSystem()
345 return GetImpl()->GetCurFileSystem();
349 // Variables (filled by CreateResource)
352 wxObject
*m_parent
, *m_instance
;
353 wxWindow
*m_parentAsWindow
;
354 wxXmlResource
*m_resource
;
356 wxArrayString m_styleNames
;
357 wxArrayInt m_styleValues
;
359 friend class wxXmlResourceHandlerImpl
;
362 // This is supposed to never return NULL because SetImpl() should have been
364 wxXmlResourceHandlerImplBase
* GetImpl() const;
366 wxXmlResourceHandlerImplBase
*m_impl
;
368 wxDECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
);
373 #endif // _WX_XRC_XMLRESHANDLER_H_