1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Wrappers for the XML based Resource system
7 // Created: 4-June-2001
9 // Copyright: (c) 2001 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
18 #include "pyistream.h"
19 #include "wx/xrc/xml.h"
20 #include "wx/xrc/xmlres.h"
21 #include <wx/filesys.h>
22 #include <wx/fs_mem.h>
25 //---------------------------------------------------------------------------
28 %include my_typemaps.i
39 //---------------------------------------------------------------------------
41 // Put some wx default wxChar* values into wxStrings.
42 static const wxString wxPyEmptyString(wxT(""));
43 static const wxString wxPyUTF8String(wxT("UTF-8"));
44 static const wxString wxPyStyleString(wxT("style"));
45 static const wxString wxPySizeString(wxT("size"));
46 static const wxString wxPyPosString(wxT("pos"));
47 static const wxString wxPyBitmapString(wxT("bitmap"));
48 static const wxString wxPyIconString(wxT("icon"));
49 static const wxString wxPyFontString(wxT("font"));
51 //---------------------------------------------------------------------------
53 enum wxXmlResourceFlags
56 wxXRC_NO_SUBCLASSING = 2
60 // This class holds XML resources from one or more .xml files
61 // (or derived forms, either binary or zipped -- see manual for
64 class wxXmlResource : public wxObject
68 // Flags: wxXRC_USE_LOCALE
69 // translatable strings will be translated via _()
70 // wxXRC_NO_SUBCLASSING
71 // subclass property of object nodes will be ignored
72 // (useful for previews in XRC editors)
73 wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
74 %name(wxEmptyXmlResource) wxXmlResource(int flags = wxXRC_USE_LOCALE);
75 %pragma(python) addtomethod = "__init__:self.InitAllHandlers()"
76 %pragma(python) addtomethod = "wxEmptyXmlResource:val.InitAllHandlers()"
81 // Loads resources from XML files that match given filemask.
82 // This method understands VFS (see filesys.h).
83 bool Load(const wxString& filemask);
86 bool LoadFromString(const wxString& data) {
87 static int s_memFileIdx = 0;
89 // Check for memory FS. If not present, load the handler:
90 wxMemoryFSHandler::AddFile(wxT("XRC_resource/dummy_file"),
93 wxFSFile *f = fsys.OpenFile(wxT("memory:XRC_resource/dummy_file"));
94 wxMemoryFSHandler::RemoveFile(wxT("XRC_resource/dummy_file"));
98 wxFileSystem::AddHandler(new wxMemoryFSHandler);
100 // Now put the resource data into the memory FS
101 wxString filename(wxT("XRC_resource/data_string_"));
102 filename << s_memFileIdx;
104 wxMemoryFSHandler::AddFile(filename, data);
106 // Load the "file" into the resource object
107 bool retval = self->Load(wxT("memory:") + filename );
113 // Initialize handlers for all supported controls/windows. This will
114 // make the executable quite big because it forces linking against
115 // most of wxWin library
116 void InitAllHandlers();
118 // Initialize only specific handler (or custom handler). Convention says
119 // that handler name is equal to control's name plus 'XmlHandler', e.g.
120 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler
121 // (xmlres) can create include file that contains initialization code for
122 // all controls used within the resource.
123 void AddHandler(wxPyXmlResourceHandler *handler);
125 // Add a new handler at the begining of the handler list
126 void InsertHandler(wxPyXmlResourceHandler *handler);
128 // Removes all handlers
129 void ClearHandlers();
131 // Loads menu from resource. Returns NULL on failure.
132 wxMenu *LoadMenu(const wxString& name);
134 // Loads menubar from resource. Returns NULL on failure.
135 wxMenuBar *LoadMenuBar(const wxString& name);
136 %name(LoadMenuBarOnFrame) wxMenuBar *LoadMenuBar(wxWindow *parent, const wxString& name);
140 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
142 // Loads dialog. dlg points to parent window (if any). Second form
143 // is used to finish creation of already existing instance (main reason
144 // for this is that you may want to use derived class with new event table)
145 // Example (typical usage):
147 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
149 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
150 %name(LoadOnDialog)bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
152 // Loads panel. panel points to parent window (if any). Second form
153 // is used to finish creation of already existing instance.
154 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
155 %name(LoadOnPanel)bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
157 // Load a frame's contents from a resource
158 wxFrame *LoadFrame(wxWindow* parent, const wxString& name);
159 %name(LoadOnFrame)bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
161 // Load an object from the resource specifying both the resource name and
162 // the classname. This lets you load nonstandard container windows.
163 wxObject *LoadObject(wxWindow *parent, const wxString& name,
164 const wxString& classname);
166 // Load an object from the resource specifying both the resource name and
167 // the classname. This form lets you finish the creation of an existing
169 %name(LoadOnObject)bool LoadObject(wxObject *instance, wxWindow *parent, const wxString& name,
170 const wxString& classname);
172 // Loads bitmap or icon resource from file:
173 wxBitmap LoadBitmap(const wxString& name);
174 wxIcon LoadIcon(const wxString& name);
176 // Attaches unknown control into given panel/window/dialog:
177 // (unknown controls are used in conjunction with <object class="unknown">)
178 bool AttachUnknownControl(const wxString& name, wxWindow *control,
179 wxWindow *parent = NULL);
181 // Returns numeric ID that is equivalent to string id used in XML
182 // resource. To be used in event tables
183 // Macro XMLID is provided for convenience
184 static int GetXRCID(const wxString& str_id);
186 // Returns version info (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a)
187 long GetVersion() const;
189 // Compares resources version to argument. Returns -1 if resources version
190 // is less than the argument, +1 if greater and 0 if they equal.
191 int CompareVersion(int major, int minor, int release, int revision) const;
194 // Gets global resources object or create one if none exists
195 static wxXmlResource *Get();
196 // Sets global resources object and returns pointer to previous one (may be NULL).
197 static wxXmlResource *Set(wxXmlResource *res);
201 //----------------------------------------------------------------------
203 %pragma(python) code = "
205 return wxXmlResource_GetXRCID(str_id)
207 def XRCCTRL(window, str_id, *ignoreargs):
208 return window.FindWindowById(XRCID(str_id))
214 //----------------------------------------------------------------------
215 //----------------------------------------------------------------------
216 // In order to provide wrappers for wxXmlResourceHandler we need to also
217 // provide the classes for representing and parsing XML.
220 // Represents XML node type.
223 // note: values are synchronized with xmlElementType from libxml
225 wxXML_ATTRIBUTE_NODE,
227 wxXML_CDATA_SECTION_NODE,
228 wxXML_ENTITY_REF_NODE,
233 wxXML_DOCUMENT_TYPE_NODE,
234 wxXML_DOCUMENT_FRAG_NODE,
236 wxXML_HTML_DOCUMENT_NODE
241 // Represents node property(ies).
242 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
243 // "hello.gif" and "id" is property with value "3".
247 wxXmlProperty(const wxString& name = wxPyEmptyString,
248 const wxString& value = wxPyEmptyString,
249 wxXmlProperty *next = NULL);
251 wxString GetName() const;
252 wxString GetValue() const;
253 wxXmlProperty *GetNext() const;
255 void SetName(const wxString& name);
256 void SetValue(const wxString& value);
257 void SetNext(wxXmlProperty *next);
263 // Represents node in XML document. Node has name and may have content
264 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
265 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
266 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
267 // with content="hi").
269 // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
270 // (default is UTF-8).
274 wxXmlNode(wxXmlNode *parent = NULL,
275 wxXmlNodeType type = 0,
276 const wxString& name = wxPyEmptyString,
277 const wxString& content = wxPyEmptyString,
278 wxXmlProperty *props = NULL,
279 wxXmlNode *next = NULL);
283 // user-friendly creation:
284 %name(wxXmlNodeEasy)wxXmlNode(wxXmlNodeType type, const wxString& name,
285 const wxString& content = wxPyEmptyString);
287 void AddChild(wxXmlNode *child);
288 void InsertChild(wxXmlNode *child, wxXmlNode *before_node);
289 bool RemoveChild(wxXmlNode *child);
290 void AddProperty(wxXmlProperty *prop);
291 %name(AddPropertyName)void AddProperty(const wxString& name, const wxString& value);
292 bool DeleteProperty(const wxString& name);
295 wxXmlNodeType GetType() const;
296 wxString GetName() const;
297 wxString GetContent() const;
299 wxXmlNode *GetParent() const;
300 wxXmlNode *GetNext() const;
301 wxXmlNode *GetChildren() const;
303 wxXmlProperty *GetProperties() const;
304 wxString GetPropVal(const wxString& propName,
305 const wxString& defaultVal) const;
306 bool HasProp(const wxString& propName) const;
308 void SetType(wxXmlNodeType type);
309 void SetName(const wxString& name);
310 void SetContent(const wxString& con);
312 void SetParent(wxXmlNode *parent);
313 void SetNext(wxXmlNode *next);
314 void SetChildren(wxXmlNode *child);
316 void SetProperties(wxXmlProperty *prop);
321 // This class holds XML data/document as parsed by XML parser.
322 class wxXmlDocument : public wxObject
325 wxXmlDocument(const wxString& filename,
326 const wxString& encoding = wxPyUTF8String);
327 %name(wxXmlDocumentFromStream)wxXmlDocument(wxInputStream& stream,
328 const wxString& encoding = wxPyUTF8String);
329 %name(wxEmptyXmlDocument)wxXmlDocument();
334 // Parses .xml file and loads data. Returns TRUE on success, FALSE
336 bool Load(const wxString& filename,
337 const wxString& encoding = wxPyUTF8String);
338 %name(LoadFromStream)bool Load(wxInputStream& stream,
339 const wxString& encoding = wxPyUTF8String);
341 // Saves document as .xml file.
342 bool Save(const wxString& filename) const;
343 %name(SaveToStream)bool Save(wxOutputStream& stream) const;
347 // Returns root node of the document.
348 wxXmlNode *GetRoot() const;
350 // Returns version of document (may be empty).
351 wxString GetVersion() const;
353 // Returns encoding of document (may be empty).
354 // Note: this is the encoding original file was saved in, *not* the
355 // encoding of in-memory representation!
356 wxString GetFileEncoding() const;
358 // Write-access methods:
359 void SetRoot(wxXmlNode *node);
360 void SetVersion(const wxString& version);
361 void SetFileEncoding(const wxString& encoding);
364 // Returns encoding of in-memory representation of the document (same
365 // as passed to Load or ctor, defaults to UTF-8). NB: this is
366 // meaningless in Unicode build where data are stored as wchar_t*
367 wxString GetEncoding() {
369 return wxPyEmptyString;
371 return self->GetEncoding();
378 //----------------------------------------------------------------------
379 // And now for wxXmlResourceHandler...
383 %{ // C++ version of Python aware wxXmlResourceHandler, for the pure virtual
384 // callbacks, as well as to make some protected things public so they can
386 class wxPyXmlResourceHandler : public wxXmlResourceHandler {
388 wxPyXmlResourceHandler() : wxXmlResourceHandler() {}
389 //~wxPyXmlResourceHandler();
391 // Base class virtuals
393 DEC_PYCALLBACK_OBJECT__pure(DoCreateResource);
394 DEC_PYCALLBACK_BOOL_NODE_pure(CanHandle);
396 // wxObject* DoCreateResource() {
397 // wxObject* rv = NULL;
398 // wxPyBeginBlockThreads();
399 // if (wxPyCBH_findCallback(m_myInst, "DoCreateResource")) {
401 // ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
403 // SWIG_GetPtrObj(ro, (void **)&rv, "_wxObject_p");
407 // wxPyEndBlockThreads();
411 // bool CanHandle(wxXmlNode* a) {
413 // wxPyBeginBlockThreads();
414 // if (wxPyCBH_findCallback(m_myInst, "CanHandle")) {
415 // PyObject* obj = wxPyConstructObject((void*)a, "wxXmlNode", 0);
416 // rv = wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", obj));
419 // wxPyEndBlockThreads();
425 // accessors for protected members
427 wxXmlResource* GetResource() { return m_resource; }
428 wxXmlNode* GetNode() { return m_node; }
429 wxString GetClass() { return m_class; }
430 wxObject* GetParent() { return m_parent; }
431 wxObject* GetInstance() { return m_instance; }
432 wxWindow* GetParentAsWindow() { return m_parentAsWindow; }
433 wxWindow* GetInstanceAsWindow() { return m_instanceAsWindow; }
436 // turn some protected methods into public via delegation
438 bool IsOfClass(wxXmlNode *node, const wxString& classname)
439 { return wxXmlResourceHandler::IsOfClass(node, classname); }
441 wxString GetNodeContent(wxXmlNode *node)
442 { return wxXmlResourceHandler::GetNodeContent(node); }
444 bool HasParam(const wxString& param)
445 { return wxXmlResourceHandler::HasParam(param); }
447 wxXmlNode *GetParamNode(const wxString& param)
448 { return wxXmlResourceHandler::GetParamNode(param); }
450 wxString GetParamValue(const wxString& param)
451 { return wxXmlResourceHandler::GetParamValue(param); }
453 void AddStyle(const wxString& name, int value)
454 { wxXmlResourceHandler::AddStyle(name, value); }
456 void AddWindowStyles()
457 { wxXmlResourceHandler::AddWindowStyles(); }
459 int GetStyle(const wxString& param = wxT("style"), int defaults = 0)
460 { return wxXmlResourceHandler::GetStyle(param, defaults); }
462 wxString GetText(const wxString& param, bool translate = TRUE)
463 { return wxXmlResourceHandler::GetText(param, translate); }
466 { return wxXmlResourceHandler::GetID(); }
469 { return wxXmlResourceHandler::GetName(); }
471 bool GetBool(const wxString& param, bool defaultv = FALSE)
472 { return wxXmlResourceHandler::GetBool(param, defaultv); }
474 long GetLong( const wxString& param, long defaultv = 0 )
475 { return wxXmlResourceHandler::GetLong(param, defaultv); }
477 wxColour GetColour(const wxString& param)
478 { return wxXmlResourceHandler::GetColour(param); }
480 wxSize GetSize(const wxString& param = wxT("size"))
481 { return wxXmlResourceHandler::GetSize(param); }
483 wxPoint GetPosition(const wxString& param = wxT("pos"))
484 { return wxXmlResourceHandler::GetPosition(param); }
486 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0)
487 { return wxXmlResourceHandler::GetDimension(param, defaultv); }
489 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
490 const wxArtClient& defaultArtClient = wxART_OTHER,
491 wxSize size = wxDefaultSize)
492 { return wxXmlResourceHandler::GetBitmap(param, defaultArtClient, size); }
494 wxIcon GetIcon(const wxString& param = wxT("icon"),
495 const wxArtClient& defaultArtClient = wxART_OTHER,
496 wxSize size = wxDefaultSize)
497 { return wxXmlResourceHandler::GetIcon(param, defaultArtClient, size); }
499 wxFont GetFont(const wxString& param = wxT("font"))
500 { return wxXmlResourceHandler::GetFont(param); }
502 void SetupWindow(wxWindow *wnd)
503 { wxXmlResourceHandler::SetupWindow(wnd); }
505 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE)
506 { wxXmlResourceHandler::CreateChildren(parent, this_hnd_only); }
508 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL)
509 { wxXmlResourceHandler::CreateChildrenPrivately(parent, rootnode); }
511 wxObject *CreateResFromNode(wxXmlNode *node,
512 wxObject *parent, wxObject *instance = NULL)
513 { return wxXmlResourceHandler::CreateResFromNode(node, parent, instance); }
515 wxFileSystem& GetCurFileSystem()
516 { return wxXmlResourceHandler::GetCurFileSystem(); }
522 IMP_PYCALLBACK_OBJECT__pure(wxPyXmlResourceHandler, wxXmlResourceHandler, DoCreateResource);
523 IMP_PYCALLBACK_BOOL_NODE_pure(wxPyXmlResourceHandler, wxXmlResourceHandler, CanHandle);
528 //----------------------------------------------------------------------
529 // Now the version that will be SWIGged.
532 %name(wxXmlResourceHandler) class wxPyXmlResourceHandler : public wxObject {
534 wxPyXmlResourceHandler() : wxXmlResourceHandler() {}
535 //~wxPyXmlResourceHandler();
537 void _setCallbackInfo(PyObject* self, PyObject* _class);
538 %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxXmlResourceHandler)"
542 // Creates an object (menu, dialog, control, ...) from an XML node.
543 // Should check for validity.
544 // parent is a higher-level object (usually window, dialog or panel)
545 // that is often neccessary to create the resource.
546 // If instance is non-NULL it should not create a new instance via 'new' but
547 // should rather use this one, and call its Create method.
548 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
551 // Sets the parent resource.
552 void SetParentResource(wxXmlResource *res);
555 wxXmlResource* GetResource() { return m_resource; }
556 wxXmlNode* GetNode() { return m_node; }
557 wxString GetClass() { return m_class; }
558 wxObject* GetParent() { return m_parent; }
559 wxObject* GetInstance() { return m_instance; }
560 wxWindow* GetParentAsWindow() { return m_parentAsWindow; }
561 wxWindow* GetInstanceAsWindow() { return m_instanceAsWindow; }
564 // Returns true if the node has a property class equal to classname,
565 // e.g. <object class="wxDialog">.
566 bool IsOfClass(wxXmlNode *node, const wxString& classname);
568 // Gets node content from wxXML_ENTITY_NODE
569 // The problem is, <tag>content<tag> is represented as
570 // wxXML_ENTITY_NODE name="tag", content=""
571 // |-- wxXML_TEXT_NODE or
572 // wxXML_CDATA_SECTION_NODE name="" content="content"
573 wxString GetNodeContent(wxXmlNode *node);
575 // Check to see if a parameter exists.
576 bool HasParam(const wxString& param);
578 // Finds the node or returns NULL.
579 wxXmlNode *GetParamNode(const wxString& param);
581 // Finds the parameter value or returns the empty string.
582 wxString GetParamValue(const wxString& param);
584 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
585 // understood by this handler.
586 void AddStyle(const wxString& name, int value);
588 // Add styles common to all wxWindow-derived classes.
589 void AddWindowStyles();
591 // Gets style flags from text in form "flag | flag2| flag3 |..."
592 // Only understads flags added with AddStyle
593 int GetStyle(const wxString& param = wxPyStyleString, int defaults = 0);
595 // Gets text from param and does some conversions:
596 // - replaces \n, \r, \t by respective chars (according to C syntax)
597 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
598 // - calls wxGetTranslations (unless disabled in wxXmlResource)
599 wxString GetText(const wxString& param, bool translate = TRUE);
601 // Returns the XRCID.
604 // Returns the resource name.
607 // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE).
608 bool GetBool(const wxString& param, bool defaultv = FALSE);
610 // Gets the integer value from the parameter.
611 long GetLong( const wxString& param, long defaultv = 0 );
613 // Gets colour in HTML syntax (#RRGGBB).
614 wxColour GetColour(const wxString& param);
616 // Gets the size (may be in dialog units).
617 wxSize GetSize(const wxString& param = wxPySizeString);
619 // Gets the position (may be in dialog units).
620 wxPoint GetPosition(const wxString& param = wxPyPosString);
622 // Gets a dimension (may be in dialog units).
623 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
626 wxBitmap GetBitmap(const wxString& param = wxPyBitmapString,
627 const wxArtClient& defaultArtClient = wxART_OTHER,
628 wxSize size = wxDefaultSize);
631 wxIcon GetIcon(const wxString& param = wxPyIconString,
632 const wxArtClient& defaultArtClient = wxART_OTHER,
633 wxSize size = wxDefaultSize);
636 wxFont GetFont(const wxString& param = wxPyFontString);
638 // Sets common window options.
639 void SetupWindow(wxWindow *wnd);
642 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
645 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
647 // Creates a resource from a node.
648 wxObject *CreateResFromNode(wxXmlNode *node,
649 wxObject *parent, wxObject *instance = NULL);
652 wxFileSystem& GetCurFileSystem();
656 //----------------------------------------------------------------------
657 //----------------------------------------------------------------------
661 wxClassInfo::CleanUpClasses();
662 wxClassInfo::InitializeClasses();
664 wxXmlInitResourceModule();
665 wxXmlResource::Get()->InitAllHandlers();
669 //----------------------------------------------------------------------
670 // This file gets appended to the shadow class file.
671 //----------------------------------------------------------------------
673 %pragma(python) include="_xrcextras.py";