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/xml/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"));
52 class wxPyXmlSubclassFactory;
54 //---------------------------------------------------------------------------
56 enum wxXmlResourceFlags
59 wxXRC_NO_SUBCLASSING = 2
63 // This class holds XML resources from one or more .xml files
64 // (or derived forms, either binary or zipped -- see manual for
67 class wxXmlResource : public wxObject
71 // Flags: wxXRC_USE_LOCALE
72 // translatable strings will be translated via _()
73 // wxXRC_NO_SUBCLASSING
74 // subclass property of object nodes will be ignored
75 // (useful for previews in XRC editors)
76 wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
77 %name(wxEmptyXmlResource) wxXmlResource(int flags = wxXRC_USE_LOCALE);
78 %pragma(python) addtomethod = "__init__:self.InitAllHandlers()"
79 %pragma(python) addtomethod = "wxEmptyXmlResource:val.InitAllHandlers()"
84 // Loads resources from XML files that match given filemask.
85 // This method understands VFS (see filesys.h).
86 bool Load(const wxString& filemask);
89 bool LoadFromString(const wxString& data) {
90 static int s_memFileIdx = 0;
92 // Check for memory FS. If not present, load the handler:
93 wxMemoryFSHandler::AddFile(wxT("XRC_resource/dummy_file"),
96 wxFSFile *f = fsys.OpenFile(wxT("memory:XRC_resource/dummy_file"));
97 wxMemoryFSHandler::RemoveFile(wxT("XRC_resource/dummy_file"));
101 wxFileSystem::AddHandler(new wxMemoryFSHandler);
103 // Now put the resource data into the memory FS
104 wxString filename(wxT("XRC_resource/data_string_"));
105 filename << s_memFileIdx;
107 wxMemoryFSHandler::AddFile(filename, data);
109 // Load the "file" into the resource object
110 bool retval = self->Load(wxT("memory:") + filename );
116 // Initialize handlers for all supported controls/windows. This will
117 // make the executable quite big because it forces linking against
118 // most of wxWin library
119 void InitAllHandlers();
121 // Initialize only specific handler (or custom handler). Convention says
122 // that handler name is equal to control's name plus 'XmlHandler', e.g.
123 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler
124 // (xmlres) can create include file that contains initialization code for
125 // all controls used within the resource.
126 void AddHandler(wxPyXmlResourceHandler *handler);
128 // Add a new handler at the begining of the handler list
129 void InsertHandler(wxPyXmlResourceHandler *handler);
131 // Removes all handlers
132 void ClearHandlers();
134 // Registers subclasses factory for use in XRC. This function is not meant
135 // for public use, please see the comment above wxXmlSubclassFactory
137 static void AddSubclassFactory(wxPyXmlSubclassFactory *factory);
140 // Loads menu from resource. Returns NULL on failure.
141 wxMenu *LoadMenu(const wxString& name);
143 // Loads menubar from resource. Returns NULL on failure.
144 wxMenuBar *LoadMenuBar(const wxString& name);
145 %name(LoadMenuBarOnFrame) wxMenuBar *LoadMenuBar(wxWindow *parent, const wxString& name);
149 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
151 // Loads dialog. dlg points to parent window (if any). Second form
152 // is used to finish creation of already existing instance (main reason
153 // for this is that you may want to use derived class with new event table)
154 // Example (typical usage):
156 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
158 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
159 %name(LoadOnDialog)bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
161 // Loads panel. panel points to parent window (if any). Second form
162 // is used to finish creation of already existing instance.
163 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
164 %name(LoadOnPanel)bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
166 // Load a frame's contents from a resource
167 wxFrame *LoadFrame(wxWindow* parent, const wxString& name);
168 %name(LoadOnFrame)bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
170 // Load an object from the resource specifying both the resource name and
171 // the classname. This lets you load nonstandard container windows.
172 wxObject *LoadObject(wxWindow *parent, const wxString& name,
173 const wxString& classname);
175 // Load an object from the resource specifying both the resource name and
176 // the classname. This form lets you finish the creation of an existing
178 %name(LoadOnObject)bool LoadObject(wxObject *instance, wxWindow *parent, const wxString& name,
179 const wxString& classname);
181 // Loads bitmap or icon resource from file:
182 wxBitmap LoadBitmap(const wxString& name);
183 wxIcon LoadIcon(const wxString& name);
185 // Attaches unknown control into given panel/window/dialog:
186 // (unknown controls are used in conjunction with <object class="unknown">)
187 bool AttachUnknownControl(const wxString& name, wxWindow *control,
188 wxWindow *parent = NULL);
190 // Returns numeric ID that is equivalent to string id used in XML
191 // resource. To be used in event tables
192 // Macro XMLID is provided for convenience
193 static int GetXRCID(const wxString& str_id);
195 // Returns version info (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a)
196 long GetVersion() const;
198 // Compares resources version to argument. Returns -1 if resources version
199 // is less than the argument, +1 if greater and 0 if they equal.
200 int CompareVersion(int major, int minor, int release, int revision) const;
203 // Gets global resources object or create one if none exists
204 static wxXmlResource *Get();
205 // Sets global resources object and returns pointer to previous one (may be NULL).
206 static wxXmlResource *Set(wxXmlResource *res);
208 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
211 // Set flags after construction.
212 void SetFlags(int flags) { m_flags = flags; }
216 //----------------------------------------------------------------------
218 %pragma(python) code = "
220 return wxXmlResource_GetXRCID(str_id)
222 def XRCCTRL(window, str_id, *ignoreargs):
223 return window.FindWindowById(XRCID(str_id))
229 //----------------------------------------------------------------------
230 // wxXmlSubclassFactory
234 class wxPyXmlSubclassFactory : public wxXmlSubclassFactory
237 wxPyXmlSubclassFactory() {}
238 DEC_PYCALLBACK_OBJECT_STRING_pure(Create);
242 IMP_PYCALLBACK_OBJECT_STRING_pure(wxPyXmlSubclassFactory, wxXmlSubclassFactory, Create);
246 %name(wxXmlSubclassFactory)class wxPyXmlSubclassFactory {
248 wxPyXmlSubclassFactory();
250 void _setCallbackInfo(PyObject* self, PyObject* _class);
251 %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxXmlSubclassFactory)"
255 //----------------------------------------------------------------------
256 // In order to provide wrappers for wxXmlResourceHandler we need to also
257 // provide the classes for representing and parsing XML.
260 // Represents XML node type.
263 // note: values are synchronized with xmlElementType from libxml
265 wxXML_ATTRIBUTE_NODE,
267 wxXML_CDATA_SECTION_NODE,
268 wxXML_ENTITY_REF_NODE,
273 wxXML_DOCUMENT_TYPE_NODE,
274 wxXML_DOCUMENT_FRAG_NODE,
276 wxXML_HTML_DOCUMENT_NODE
281 // Represents node property(ies).
282 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
283 // "hello.gif" and "id" is property with value "3".
287 wxXmlProperty(const wxString& name = wxPyEmptyString,
288 const wxString& value = wxPyEmptyString,
289 wxXmlProperty *next = NULL);
291 wxString GetName() const;
292 wxString GetValue() const;
293 wxXmlProperty *GetNext() const;
295 void SetName(const wxString& name);
296 void SetValue(const wxString& value);
297 void SetNext(wxXmlProperty *next);
303 // Represents node in XML document. Node has name and may have content
304 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
305 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
306 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
307 // with content="hi").
309 // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
310 // (default is UTF-8).
314 wxXmlNode(wxXmlNode *parent = NULL,
315 wxXmlNodeType type = 0,
316 const wxString& name = wxPyEmptyString,
317 const wxString& content = wxPyEmptyString,
318 wxXmlProperty *props = NULL,
319 wxXmlNode *next = NULL);
323 // user-friendly creation:
324 %name(wxXmlNodeEasy)wxXmlNode(wxXmlNodeType type, const wxString& name,
325 const wxString& content = wxPyEmptyString);
327 void AddChild(wxXmlNode *child);
328 void InsertChild(wxXmlNode *child, wxXmlNode *before_node);
329 bool RemoveChild(wxXmlNode *child);
330 void AddProperty(wxXmlProperty *prop);
331 %name(AddPropertyName)void AddProperty(const wxString& name, const wxString& value);
332 bool DeleteProperty(const wxString& name);
335 wxXmlNodeType GetType() const;
336 wxString GetName() const;
337 wxString GetContent() const;
339 wxXmlNode *GetParent() const;
340 wxXmlNode *GetNext() const;
341 wxXmlNode *GetChildren() const;
343 wxXmlProperty *GetProperties() const;
344 wxString GetPropVal(const wxString& propName,
345 const wxString& defaultVal) const;
346 bool HasProp(const wxString& propName) const;
348 void SetType(wxXmlNodeType type);
349 void SetName(const wxString& name);
350 void SetContent(const wxString& con);
352 void SetParent(wxXmlNode *parent);
353 void SetNext(wxXmlNode *next);
354 void SetChildren(wxXmlNode *child);
356 void SetProperties(wxXmlProperty *prop);
361 // This class holds XML data/document as parsed by XML parser.
362 class wxXmlDocument : public wxObject
365 wxXmlDocument(const wxString& filename,
366 const wxString& encoding = wxPyUTF8String);
367 %name(wxXmlDocumentFromStream)wxXmlDocument(wxInputStream& stream,
368 const wxString& encoding = wxPyUTF8String);
369 %name(wxEmptyXmlDocument)wxXmlDocument();
374 // Parses .xml file and loads data. Returns TRUE on success, FALSE
376 bool Load(const wxString& filename,
377 const wxString& encoding = wxPyUTF8String);
378 %name(LoadFromStream)bool Load(wxInputStream& stream,
379 const wxString& encoding = wxPyUTF8String);
381 // Saves document as .xml file.
382 bool Save(const wxString& filename) const;
383 %name(SaveToStream)bool Save(wxOutputStream& stream) const;
387 // Returns root node of the document.
388 wxXmlNode *GetRoot() const;
390 // Returns version of document (may be empty).
391 wxString GetVersion() const;
393 // Returns encoding of document (may be empty).
394 // Note: this is the encoding original file was saved in, *not* the
395 // encoding of in-memory representation!
396 wxString GetFileEncoding() const;
398 // Write-access methods:
399 void SetRoot(wxXmlNode *node);
400 void SetVersion(const wxString& version);
401 void SetFileEncoding(const wxString& encoding);
404 // Returns encoding of in-memory representation of the document (same
405 // as passed to Load or ctor, defaults to UTF-8). NB: this is
406 // meaningless in Unicode build where data are stored as wchar_t*
407 wxString GetEncoding() {
409 return wxPyEmptyString;
411 return self->GetEncoding();
414 void SetEncoding(const wxString& enc) {
418 self->SetEncoding(enc);
425 //----------------------------------------------------------------------
426 // And now for wxXmlResourceHandler...
430 %{ // C++ version of Python aware wxXmlResourceHandler, for the pure virtual
431 // callbacks, as well as to make some protected things public so they can
433 class wxPyXmlResourceHandler : public wxXmlResourceHandler {
435 wxPyXmlResourceHandler() : wxXmlResourceHandler() {}
436 //~wxPyXmlResourceHandler();
438 // Base class virtuals
440 DEC_PYCALLBACK_OBJECT__pure(DoCreateResource);
441 DEC_PYCALLBACK_BOOL_NODE_pure(CanHandle);
444 // accessors for protected members
446 wxXmlResource* GetResource() { return m_resource; }
447 wxXmlNode* GetNode() { return m_node; }
448 wxString GetClass() { return m_class; }
449 wxObject* GetParent() { return m_parent; }
450 wxObject* GetInstance() { return m_instance; }
451 wxWindow* GetParentAsWindow() { return m_parentAsWindow; }
452 wxWindow* GetInstanceAsWindow() { return m_instanceAsWindow; }
455 // turn some protected methods into public via delegation
457 bool IsOfClass(wxXmlNode *node, const wxString& classname)
458 { return wxXmlResourceHandler::IsOfClass(node, classname); }
460 wxString GetNodeContent(wxXmlNode *node)
461 { return wxXmlResourceHandler::GetNodeContent(node); }
463 bool HasParam(const wxString& param)
464 { return wxXmlResourceHandler::HasParam(param); }
466 wxXmlNode *GetParamNode(const wxString& param)
467 { return wxXmlResourceHandler::GetParamNode(param); }
469 wxString GetParamValue(const wxString& param)
470 { return wxXmlResourceHandler::GetParamValue(param); }
472 void AddStyle(const wxString& name, int value)
473 { wxXmlResourceHandler::AddStyle(name, value); }
475 void AddWindowStyles()
476 { wxXmlResourceHandler::AddWindowStyles(); }
478 int GetStyle(const wxString& param = wxT("style"), int defaults = 0)
479 { return wxXmlResourceHandler::GetStyle(param, defaults); }
481 wxString GetText(const wxString& param, bool translate = TRUE)
482 { return wxXmlResourceHandler::GetText(param, translate); }
485 { return wxXmlResourceHandler::GetID(); }
488 { return wxXmlResourceHandler::GetName(); }
490 bool GetBool(const wxString& param, bool defaultv = FALSE)
491 { return wxXmlResourceHandler::GetBool(param, defaultv); }
493 long GetLong( const wxString& param, long defaultv = 0 )
494 { return wxXmlResourceHandler::GetLong(param, defaultv); }
496 wxColour GetColour(const wxString& param)
497 { return wxXmlResourceHandler::GetColour(param); }
499 wxSize GetSize(const wxString& param = wxT("size"))
500 { return wxXmlResourceHandler::GetSize(param); }
502 wxPoint GetPosition(const wxString& param = wxT("pos"))
503 { return wxXmlResourceHandler::GetPosition(param); }
505 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0)
506 { return wxXmlResourceHandler::GetDimension(param, defaultv); }
508 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
509 const wxArtClient& defaultArtClient = wxART_OTHER,
510 wxSize size = wxDefaultSize)
511 { return wxXmlResourceHandler::GetBitmap(param, defaultArtClient, size); }
513 wxIcon GetIcon(const wxString& param = wxT("icon"),
514 const wxArtClient& defaultArtClient = wxART_OTHER,
515 wxSize size = wxDefaultSize)
516 { return wxXmlResourceHandler::GetIcon(param, defaultArtClient, size); }
518 wxFont GetFont(const wxString& param = wxT("font"))
519 { return wxXmlResourceHandler::GetFont(param); }
521 void SetupWindow(wxWindow *wnd)
522 { wxXmlResourceHandler::SetupWindow(wnd); }
524 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE)
525 { wxXmlResourceHandler::CreateChildren(parent, this_hnd_only); }
527 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL)
528 { wxXmlResourceHandler::CreateChildrenPrivately(parent, rootnode); }
530 wxObject *CreateResFromNode(wxXmlNode *node,
531 wxObject *parent, wxObject *instance = NULL)
532 { return wxXmlResourceHandler::CreateResFromNode(node, parent, instance); }
534 wxFileSystem& GetCurFileSystem()
535 { return wxXmlResourceHandler::GetCurFileSystem(); }
541 IMP_PYCALLBACK_OBJECT__pure(wxPyXmlResourceHandler, wxXmlResourceHandler, DoCreateResource);
542 IMP_PYCALLBACK_BOOL_NODE_pure(wxPyXmlResourceHandler, wxXmlResourceHandler, CanHandle);
547 //----------------------------------------------------------------------
548 // Now the version that will be SWIGged.
551 %name(wxXmlResourceHandler) class wxPyXmlResourceHandler : public wxObject {
553 wxPyXmlResourceHandler() : wxXmlResourceHandler() {}
554 //~wxPyXmlResourceHandler();
556 void _setCallbackInfo(PyObject* self, PyObject* _class);
557 %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxXmlResourceHandler)"
561 // Creates an object (menu, dialog, control, ...) from an XML node.
562 // Should check for validity.
563 // parent is a higher-level object (usually window, dialog or panel)
564 // that is often neccessary to create the resource.
565 // If instance is non-NULL it should not create a new instance via 'new' but
566 // should rather use this one, and call its Create method.
567 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
570 // Sets the parent resource.
571 void SetParentResource(wxXmlResource *res);
574 wxXmlResource* GetResource() { return m_resource; }
575 wxXmlNode* GetNode() { return m_node; }
576 wxString GetClass() { return m_class; }
577 wxObject* GetParent() { return m_parent; }
578 wxObject* GetInstance() { return m_instance; }
579 wxWindow* GetParentAsWindow() { return m_parentAsWindow; }
580 wxWindow* GetInstanceAsWindow() { return m_instanceAsWindow; }
583 // Returns true if the node has a property class equal to classname,
584 // e.g. <object class="wxDialog">.
585 bool IsOfClass(wxXmlNode *node, const wxString& classname);
587 // Gets node content from wxXML_ENTITY_NODE
588 // The problem is, <tag>content<tag> is represented as
589 // wxXML_ENTITY_NODE name="tag", content=""
590 // |-- wxXML_TEXT_NODE or
591 // wxXML_CDATA_SECTION_NODE name="" content="content"
592 wxString GetNodeContent(wxXmlNode *node);
594 // Check to see if a parameter exists.
595 bool HasParam(const wxString& param);
597 // Finds the node or returns NULL.
598 wxXmlNode *GetParamNode(const wxString& param);
600 // Finds the parameter value or returns the empty string.
601 wxString GetParamValue(const wxString& param);
603 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
604 // understood by this handler.
605 void AddStyle(const wxString& name, int value);
607 // Add styles common to all wxWindow-derived classes.
608 void AddWindowStyles();
610 // Gets style flags from text in form "flag | flag2| flag3 |..."
611 // Only understads flags added with AddStyle
612 int GetStyle(const wxString& param = wxPyStyleString, int defaults = 0);
614 // Gets text from param and does some conversions:
615 // - replaces \n, \r, \t by respective chars (according to C syntax)
616 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
617 // - calls wxGetTranslations (unless disabled in wxXmlResource)
618 wxString GetText(const wxString& param, bool translate = TRUE);
620 // Returns the XRCID.
623 // Returns the resource name.
626 // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE).
627 bool GetBool(const wxString& param, bool defaultv = FALSE);
629 // Gets the integer value from the parameter.
630 long GetLong( const wxString& param, long defaultv = 0 );
632 // Gets colour in HTML syntax (#RRGGBB).
633 wxColour GetColour(const wxString& param);
635 // Gets the size (may be in dialog units).
636 wxSize GetSize(const wxString& param = wxPySizeString);
638 // Gets the position (may be in dialog units).
639 wxPoint GetPosition(const wxString& param = wxPyPosString);
641 // Gets a dimension (may be in dialog units).
642 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
645 wxBitmap GetBitmap(const wxString& param = wxPyBitmapString,
646 const wxArtClient& defaultArtClient = wxART_OTHER,
647 wxSize size = wxDefaultSize);
650 wxIcon GetIcon(const wxString& param = wxPyIconString,
651 const wxArtClient& defaultArtClient = wxART_OTHER,
652 wxSize size = wxDefaultSize);
655 wxFont GetFont(const wxString& param = wxPyFontString);
657 // Sets common window options.
658 void SetupWindow(wxWindow *wnd);
661 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
664 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
666 // Creates a resource from a node.
667 wxObject *CreateResFromNode(wxXmlNode *node,
668 wxObject *parent, wxObject *instance = NULL);
671 wxFileSystem& GetCurFileSystem();
675 //----------------------------------------------------------------------
676 //----------------------------------------------------------------------
680 wxXmlInitResourceModule();
681 wxXmlResource::Get()->InitAllHandlers();
685 //----------------------------------------------------------------------
686 // This file gets appended to the shadow class file.
687 //----------------------------------------------------------------------
689 %pragma(python) include="_xrcextras.py";