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/animate.h"
22 #include "wx/filesys.h"
23 #include "wx/imaglist.h"
25 class WXDLLIMPEXP_FWD_XML wxXmlNode
;
26 class WXDLLIMPEXP_FWD_XML wxXmlResource
;
28 class WXDLLIMPEXP_FWD_CORE wxXmlResourceHandler
;
30 // Helper macro used by the classes derived from wxXmlResourceHandler but also
31 // by wxXmlResourceHandler implementation itself.
32 #define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
34 // Abstract base class for the implementation object used by
35 // wxXmlResourceHandlerImpl. The real implementation is in
36 // wxXmlResourceHandlerImpl class in the "xrc" library while this class is in
37 // the "core" itself -- but it is so small that it doesn't matter.
39 class WXDLLIMPEXP_CORE wxXmlResourceHandlerImplBase
: public wxObject
43 wxXmlResourceHandlerImplBase(wxXmlResourceHandler
*handler
)
48 virtual ~wxXmlResourceHandlerImplBase() {}
50 virtual wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
51 wxObject
*instance
) = 0;
52 virtual bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
) const = 0;
53 virtual wxString
GetNodeContent(const wxXmlNode
*node
) = 0;
54 virtual bool HasParam(const wxString
& param
) = 0;
55 virtual wxXmlNode
*GetParamNode(const wxString
& param
) = 0;
56 virtual wxString
GetParamValue(const wxString
& param
) = 0;
57 virtual wxString
GetParamValue(const wxXmlNode
* node
) = 0;
58 virtual int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0) = 0;
59 virtual wxString
GetText(const wxString
& param
, bool translate
= true) = 0;
60 virtual int GetID() = 0;
61 virtual wxString
GetName() = 0;
62 virtual bool GetBool(const wxString
& param
, bool defaultv
= false) = 0;
63 virtual long GetLong(const wxString
& param
, long defaultv
= 0) = 0;
64 virtual float GetFloat(const wxString
& param
, float defaultv
= 0) = 0;
65 virtual wxColour
GetColour(const wxString
& param
,
66 const wxColour
& defaultv
= wxNullColour
) = 0;
67 virtual wxSize
GetSize(const wxString
& param
= wxT("size"),
68 wxWindow
*windowToUse
= NULL
) = 0;
69 virtual wxPoint
GetPosition(const wxString
& param
= wxT("pos")) = 0;
70 virtual wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
71 wxWindow
*windowToUse
= NULL
) = 0;
72 virtual wxDirection
GetDirection(const wxString
& param
, wxDirection dir
= wxLEFT
) = 0;
73 virtual wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
74 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
75 wxSize size
= wxDefaultSize
) = 0;
76 virtual wxBitmap
GetBitmap(const wxXmlNode
* node
,
77 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
78 wxSize size
= wxDefaultSize
) = 0;
79 virtual wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
80 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
81 wxSize size
= wxDefaultSize
) = 0;
82 virtual wxIcon
GetIcon(const wxXmlNode
* node
,
83 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
84 wxSize size
= wxDefaultSize
) = 0;
85 virtual wxIconBundle
GetIconBundle(const wxString
& param
,
86 const wxArtClient
& defaultArtClient
= wxART_OTHER
) = 0;
87 virtual wxImageList
*GetImageList(const wxString
& param
= wxT("imagelist")) = 0;
89 #if wxUSE_ANIMATIONCTRL
90 virtual wxAnimation
GetAnimation(const wxString
& param
= wxT("animation")) = 0;
93 virtual wxFont
GetFont(const wxString
& param
= wxT("font"), wxWindow
* parent
= NULL
) = 0;
94 virtual bool GetBoolAttr(const wxString
& attr
, bool defaultv
) = 0;
95 virtual void SetupWindow(wxWindow
*wnd
) = 0;
96 virtual void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false) = 0;
97 virtual void CreateChildrenPrivately(wxObject
*parent
,
98 wxXmlNode
*rootnode
= NULL
) = 0;
99 virtual wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
100 wxObject
*instance
= NULL
) = 0;
103 virtual wxFileSystem
& GetCurFileSystem() = 0;
105 virtual void ReportError(wxXmlNode
*context
, const wxString
& message
) = 0;
106 virtual void ReportError(const wxString
& message
) = 0;
107 virtual void ReportParamError(const wxString
& param
, const wxString
& message
) = 0;
109 wxXmlResourceHandler
* GetHandler() { return m_handler
; }
112 wxXmlResourceHandler
*m_handler
;
115 // Base class for all XRC handlers.
117 // Notice that this class is defined in the core library itself and so can be
118 // used as the base class by classes in any GUI library. However to actually be
119 // usable, it needs to be registered with wxXmlResource which implies linking
120 // the application with the xrc library.
122 // Also note that all the methods forwarding to GetImpl() are documented only
123 // in wxXmlResourceHandlerImpl in wx/xrc/xmlres.h to avoid duplication.
125 class WXDLLIMPEXP_CORE wxXmlResourceHandler
: public wxObject
128 // Constructor creates an unusable object, before anything can be done with
129 // it, SetImpl() needs to be called as done by wxXmlResource::AddHandler().
130 wxXmlResourceHandler()
135 // This should be called exactly once.
136 void SetImpl(wxXmlResourceHandlerImplBase
* impl
)
138 wxASSERT_MSG( !m_impl
, wxS("Should be called exactly once") );
145 virtual ~wxXmlResourceHandler()
150 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
,
153 return GetImpl()->CreateResource(node
, parent
, instance
);
156 // This one is called from CreateResource after variables
158 virtual wxObject
*DoCreateResource() = 0;
160 // Returns true if it understands this node and can create
161 // a resource from it, false otherwise.
162 virtual bool CanHandle(wxXmlNode
*node
) = 0;
165 void SetParentResource(wxXmlResource
*res
)
171 // These methods are not forwarded to wxXmlResourceHandlerImpl because they
172 // are called from the derived classes ctors and so before SetImpl() can be
175 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
176 // understood by this handler.
177 void AddStyle(const wxString
& name
, int value
);
179 // Add styles common to all wxWindow-derived classes.
180 void AddWindowStyles();
183 // Everything else is simply forwarded to wxXmlResourceHandlerImpl.
184 void ReportError(wxXmlNode
*context
, const wxString
& message
)
186 return GetImpl()->ReportError(context
, message
);
188 void ReportError(const wxString
& message
)
190 return GetImpl()->ReportError(message
);
192 void ReportParamError(const wxString
& param
, const wxString
& message
)
194 return GetImpl()->ReportParamError(param
, message
);
197 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
) const
199 return GetImpl()->IsOfClass(node
, classname
);
201 wxString
GetNodeContent(const wxXmlNode
*node
)
203 return GetImpl()->GetNodeContent(node
);
205 bool HasParam(const wxString
& param
)
207 return GetImpl()->HasParam(param
);
210 wxXmlNode
*GetParamNode(const wxString
& param
)
212 return GetImpl()->GetParamNode(param
);
214 wxString
GetParamValue(const wxString
& param
)
216 return GetImpl()->GetParamValue(param
);
218 wxString
GetParamValue(const wxXmlNode
* node
)
220 return GetImpl()->GetParamValue(node
);
222 int GetStyle(const wxString
& param
= wxT("style"), int defaults
= 0)
224 return GetImpl()->GetStyle(param
, defaults
);
226 wxString
GetText(const wxString
& param
, bool translate
= true)
228 return GetImpl()->GetText(param
, translate
);
232 return GetImpl()->GetID();
236 return GetImpl()->GetName();
238 bool GetBool(const wxString
& param
, bool defaultv
= false)
240 return GetImpl()->GetBool(param
, defaultv
);
242 long GetLong(const wxString
& param
, long defaultv
= 0)
244 return GetImpl()->GetLong(param
, defaultv
);
246 float GetFloat(const wxString
& param
, float defaultv
= 0)
248 return GetImpl()->GetFloat(param
, defaultv
);
250 wxColour
GetColour(const wxString
& param
,
251 const wxColour
& defaultv
= wxNullColour
)
253 return GetImpl()->GetColour(param
, defaultv
);
255 wxSize
GetSize(const wxString
& param
= wxT("size"),
256 wxWindow
*windowToUse
= NULL
)
258 return GetImpl()->GetSize(param
, windowToUse
);
260 wxPoint
GetPosition(const wxString
& param
= wxT("pos"))
262 return GetImpl()->GetPosition(param
);
264 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0,
265 wxWindow
*windowToUse
= NULL
)
267 return GetImpl()->GetDimension(param
, defaultv
, windowToUse
);
269 wxDirection
GetDirection(const wxString
& param
, wxDirection dir
= wxLEFT
)
271 return GetImpl()->GetDirection(param
, dir
);
273 wxBitmap
GetBitmap(const wxString
& param
= wxT("bitmap"),
274 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
275 wxSize size
= wxDefaultSize
)
277 return GetImpl()->GetBitmap(param
, defaultArtClient
, size
);
279 wxBitmap
GetBitmap(const wxXmlNode
* node
,
280 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
281 wxSize size
= wxDefaultSize
)
283 return GetImpl()->GetBitmap(node
, defaultArtClient
, size
);
285 wxIcon
GetIcon(const wxString
& param
= wxT("icon"),
286 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
287 wxSize size
= wxDefaultSize
)
289 return GetImpl()->GetIcon(param
, defaultArtClient
, size
);
291 wxIcon
GetIcon(const wxXmlNode
* node
,
292 const wxArtClient
& defaultArtClient
= wxART_OTHER
,
293 wxSize size
= wxDefaultSize
)
295 return GetImpl()->GetIcon(node
, defaultArtClient
, size
);
297 wxIconBundle
GetIconBundle(const wxString
& param
,
298 const wxArtClient
& defaultArtClient
= wxART_OTHER
)
300 return GetImpl()->GetIconBundle(param
, defaultArtClient
);
302 wxImageList
*GetImageList(const wxString
& param
= wxT("imagelist"))
304 return GetImpl()->GetImageList(param
);
307 #if wxUSE_ANIMATIONCTRL
308 wxAnimation
GetAnimation(const wxString
& param
= wxT("animation"))
310 return GetImpl()->GetAnimation(param
);
314 wxFont
GetFont(const wxString
& param
= wxT("font"),
315 wxWindow
* parent
= NULL
)
317 return GetImpl()->GetFont(param
, parent
);
319 bool GetBoolAttr(const wxString
& attr
, bool defaultv
)
321 return GetImpl()->GetBoolAttr(attr
, defaultv
);
323 void SetupWindow(wxWindow
*wnd
)
325 GetImpl()->SetupWindow(wnd
);
327 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= false)
329 GetImpl()->CreateChildren(parent
, this_hnd_only
);
331 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
)
333 GetImpl()->CreateChildrenPrivately(parent
, rootnode
);
335 wxObject
*CreateResFromNode(wxXmlNode
*node
,
336 wxObject
*parent
, wxObject
*instance
= NULL
)
338 return GetImpl()->CreateResFromNode(node
, parent
, instance
);
342 wxFileSystem
& GetCurFileSystem()
344 return GetImpl()->GetCurFileSystem();
348 // Variables (filled by CreateResource)
351 wxObject
*m_parent
, *m_instance
;
352 wxWindow
*m_parentAsWindow
;
353 wxXmlResource
*m_resource
;
355 wxArrayString m_styleNames
;
356 wxArrayInt m_styleValues
;
358 friend class wxXmlResourceHandlerImpl
;
361 // This is supposed to never return NULL because SetImpl() should have been
363 wxXmlResourceHandlerImplBase
* GetImpl() const;
365 wxXmlResourceHandlerImplBase
*m_impl
;
367 wxDECLARE_ABSTRACT_CLASS(wxXmlResourceHandler
);
372 #endif // _WX_XRC_XMLRESHANDLER_H_