xmlreshandler.h 13 KB

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