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);
199 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
204 //----------------------------------------------------------------------
206 %pragma(python) code = "
208 return wxXmlResource_GetXRCID(str_id)
210 def XRCCTRL(window, str_id, *ignoreargs):
211 return window.FindWindowById(XRCID(str_id))
217 //----------------------------------------------------------------------
218 //----------------------------------------------------------------------
219 // In order to provide wrappers for wxXmlResourceHandler we need to also
220 // provide the classes for representing and parsing XML.
223 // Represents XML node type.
226 // note: values are synchronized with xmlElementType from libxml
228 wxXML_ATTRIBUTE_NODE,
230 wxXML_CDATA_SECTION_NODE,
231 wxXML_ENTITY_REF_NODE,
236 wxXML_DOCUMENT_TYPE_NODE,
237 wxXML_DOCUMENT_FRAG_NODE,
239 wxXML_HTML_DOCUMENT_NODE
244 // Represents node property(ies).
245 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
246 // "hello.gif" and "id" is property with value "3".
250 wxXmlProperty(const wxString& name = wxPyEmptyString,
251 const wxString& value = wxPyEmptyString,
252 wxXmlProperty *next = NULL);
254 wxString GetName() const;
255 wxString GetValue() const;
256 wxXmlProperty *GetNext() const;
258 void SetName(const wxString& name);
259 void SetValue(const wxString& value);
260 void SetNext(wxXmlProperty *next);
266 // Represents node in XML document. Node has name and may have content
267 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
268 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
269 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
270 // with content="hi").
272 // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
273 // (default is UTF-8).
277 wxXmlNode(wxXmlNode *parent = NULL,
278 wxXmlNodeType type = 0,
279 const wxString& name = wxPyEmptyString,
280 const wxString& content = wxPyEmptyString,
281 wxXmlProperty *props = NULL,
282 wxXmlNode *next = NULL);
286 // user-friendly creation:
287 %name(wxXmlNodeEasy)wxXmlNode(wxXmlNodeType type, const wxString& name,
288 const wxString& content = wxPyEmptyString);
290 void AddChild(wxXmlNode *child);
291 void InsertChild(wxXmlNode *child, wxXmlNode *before_node);
292 bool RemoveChild(wxXmlNode *child);
293 void AddProperty(wxXmlProperty *prop);
294 %name(AddPropertyName)void AddProperty(const wxString& name, const wxString& value);
295 bool DeleteProperty(const wxString& name);
298 wxXmlNodeType GetType() const;
299 wxString GetName() const;
300 wxString GetContent() const;
302 wxXmlNode *GetParent() const;
303 wxXmlNode *GetNext() const;
304 wxXmlNode *GetChildren() const;
306 wxXmlProperty *GetProperties() const;
307 wxString GetPropVal(const wxString& propName,
308 const wxString& defaultVal) const;
309 bool HasProp(const wxString& propName) const;
311 void SetType(wxXmlNodeType type);
312 void SetName(const wxString& name);
313 void SetContent(const wxString& con);
315 void SetParent(wxXmlNode *parent);
316 void SetNext(wxXmlNode *next);
317 void SetChildren(wxXmlNode *child);
319 void SetProperties(wxXmlProperty *prop);
324 // This class holds XML data/document as parsed by XML parser.
325 class wxXmlDocument : public wxObject
328 wxXmlDocument(const wxString& filename,
329 const wxString& encoding = wxPyUTF8String);
330 %name(wxXmlDocumentFromStream)wxXmlDocument(wxInputStream& stream,
331 const wxString& encoding = wxPyUTF8String);
332 %name(wxEmptyXmlDocument)wxXmlDocument();
337 // Parses .xml file and loads data. Returns TRUE on success, FALSE
339 bool Load(const wxString& filename,
340 const wxString& encoding = wxPyUTF8String);
341 %name(LoadFromStream)bool Load(wxInputStream& stream,
342 const wxString& encoding = wxPyUTF8String);
344 // Saves document as .xml file.
345 bool Save(const wxString& filename) const;
346 %name(SaveToStream)bool Save(wxOutputStream& stream) const;
350 // Returns root node of the document.
351 wxXmlNode *GetRoot() const;
353 // Returns version of document (may be empty).
354 wxString GetVersion() const;
356 // Returns encoding of document (may be empty).
357 // Note: this is the encoding original file was saved in, *not* the
358 // encoding of in-memory representation!
359 wxString GetFileEncoding() const;
361 // Write-access methods:
362 void SetRoot(wxXmlNode *node);
363 void SetVersion(const wxString& version);
364 void SetFileEncoding(const wxString& encoding);
367 // Returns encoding of in-memory representation of the document (same
368 // as passed to Load or ctor, defaults to UTF-8). NB: this is
369 // meaningless in Unicode build where data are stored as wchar_t*
370 wxString GetEncoding() {
372 return wxPyEmptyString;
374 return self->GetEncoding();
381 //----------------------------------------------------------------------
382 // And now for wxXmlResourceHandler...
386 %{ // C++ version of Python aware wxXmlResourceHandler, for the pure virtual
387 // callbacks, as well as to make some protected things public so they can
389 class wxPyXmlResourceHandler : public wxXmlResourceHandler {
391 wxPyXmlResourceHandler() : wxXmlResourceHandler() {}
392 //~wxPyXmlResourceHandler();
394 // Base class virtuals
396 DEC_PYCALLBACK_OBJECT__pure(DoCreateResource);
397 DEC_PYCALLBACK_BOOL_NODE_pure(CanHandle);
399 // wxObject* DoCreateResource() {
400 // wxObject* rv = NULL;
401 // wxPyBeginBlockThreads();
402 // if (wxPyCBH_findCallback(m_myInst, "DoCreateResource")) {
404 // ro = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("()"));
406 // SWIG_GetPtrObj(ro, (void **)&rv, "_wxObject_p");
410 // wxPyEndBlockThreads();
414 // bool CanHandle(wxXmlNode* a) {
416 // wxPyBeginBlockThreads();
417 // if (wxPyCBH_findCallback(m_myInst, "CanHandle")) {
418 // PyObject* obj = wxPyConstructObject((void*)a, "wxXmlNode", 0);
419 // rv = wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", obj));
422 // wxPyEndBlockThreads();
428 // accessors for protected members
430 wxXmlResource* GetResource() { return m_resource; }
431 wxXmlNode* GetNode() { return m_node; }
432 wxString GetClass() { return m_class; }
433 wxObject* GetParent() { return m_parent; }
434 wxObject* GetInstance() { return m_instance; }
435 wxWindow* GetParentAsWindow() { return m_parentAsWindow; }
436 wxWindow* GetInstanceAsWindow() { return m_instanceAsWindow; }
439 // turn some protected methods into public via delegation
441 bool IsOfClass(wxXmlNode *node, const wxString& classname)
442 { return wxXmlResourceHandler::IsOfClass(node, classname); }
444 wxString GetNodeContent(wxXmlNode *node)
445 { return wxXmlResourceHandler::GetNodeContent(node); }
447 bool HasParam(const wxString& param)
448 { return wxXmlResourceHandler::HasParam(param); }
450 wxXmlNode *GetParamNode(const wxString& param)
451 { return wxXmlResourceHandler::GetParamNode(param); }
453 wxString GetParamValue(const wxString& param)
454 { return wxXmlResourceHandler::GetParamValue(param); }
456 void AddStyle(const wxString& name, int value)
457 { wxXmlResourceHandler::AddStyle(name, value); }
459 void AddWindowStyles()
460 { wxXmlResourceHandler::AddWindowStyles(); }
462 int GetStyle(const wxString& param = wxT("style"), int defaults = 0)
463 { return wxXmlResourceHandler::GetStyle(param, defaults); }
465 wxString GetText(const wxString& param, bool translate = TRUE)
466 { return wxXmlResourceHandler::GetText(param, translate); }
469 { return wxXmlResourceHandler::GetID(); }
472 { return wxXmlResourceHandler::GetName(); }
474 bool GetBool(const wxString& param, bool defaultv = FALSE)
475 { return wxXmlResourceHandler::GetBool(param, defaultv); }
477 long GetLong( const wxString& param, long defaultv = 0 )
478 { return wxXmlResourceHandler::GetLong(param, defaultv); }
480 wxColour GetColour(const wxString& param)
481 { return wxXmlResourceHandler::GetColour(param); }
483 wxSize GetSize(const wxString& param = wxT("size"))
484 { return wxXmlResourceHandler::GetSize(param); }
486 wxPoint GetPosition(const wxString& param = wxT("pos"))
487 { return wxXmlResourceHandler::GetPosition(param); }
489 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0)
490 { return wxXmlResourceHandler::GetDimension(param, defaultv); }
492 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
493 const wxArtClient& defaultArtClient = wxART_OTHER,
494 wxSize size = wxDefaultSize)
495 { return wxXmlResourceHandler::GetBitmap(param, defaultArtClient, size); }
497 wxIcon GetIcon(const wxString& param = wxT("icon"),
498 const wxArtClient& defaultArtClient = wxART_OTHER,
499 wxSize size = wxDefaultSize)
500 { return wxXmlResourceHandler::GetIcon(param, defaultArtClient, size); }
502 wxFont GetFont(const wxString& param = wxT("font"))
503 { return wxXmlResourceHandler::GetFont(param); }
505 void SetupWindow(wxWindow *wnd)
506 { wxXmlResourceHandler::SetupWindow(wnd); }
508 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE)
509 { wxXmlResourceHandler::CreateChildren(parent, this_hnd_only); }
511 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL)
512 { wxXmlResourceHandler::CreateChildrenPrivately(parent, rootnode); }
514 wxObject *CreateResFromNode(wxXmlNode *node,
515 wxObject *parent, wxObject *instance = NULL)
516 { return wxXmlResourceHandler::CreateResFromNode(node, parent, instance); }
518 wxFileSystem& GetCurFileSystem()
519 { return wxXmlResourceHandler::GetCurFileSystem(); }
525 IMP_PYCALLBACK_OBJECT__pure(wxPyXmlResourceHandler, wxXmlResourceHandler, DoCreateResource);
526 IMP_PYCALLBACK_BOOL_NODE_pure(wxPyXmlResourceHandler, wxXmlResourceHandler, CanHandle);
531 //----------------------------------------------------------------------
532 // Now the version that will be SWIGged.
535 %name(wxXmlResourceHandler) class wxPyXmlResourceHandler : public wxObject {
537 wxPyXmlResourceHandler() : wxXmlResourceHandler() {}
538 //~wxPyXmlResourceHandler();
540 void _setCallbackInfo(PyObject* self, PyObject* _class);
541 %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxXmlResourceHandler)"
545 // Creates an object (menu, dialog, control, ...) from an XML node.
546 // Should check for validity.
547 // parent is a higher-level object (usually window, dialog or panel)
548 // that is often neccessary to create the resource.
549 // If instance is non-NULL it should not create a new instance via 'new' but
550 // should rather use this one, and call its Create method.
551 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
554 // Sets the parent resource.
555 void SetParentResource(wxXmlResource *res);
558 wxXmlResource* GetResource() { return m_resource; }
559 wxXmlNode* GetNode() { return m_node; }
560 wxString GetClass() { return m_class; }
561 wxObject* GetParent() { return m_parent; }
562 wxObject* GetInstance() { return m_instance; }
563 wxWindow* GetParentAsWindow() { return m_parentAsWindow; }
564 wxWindow* GetInstanceAsWindow() { return m_instanceAsWindow; }
567 // Returns true if the node has a property class equal to classname,
568 // e.g. <object class="wxDialog">.
569 bool IsOfClass(wxXmlNode *node, const wxString& classname);
571 // Gets node content from wxXML_ENTITY_NODE
572 // The problem is, <tag>content<tag> is represented as
573 // wxXML_ENTITY_NODE name="tag", content=""
574 // |-- wxXML_TEXT_NODE or
575 // wxXML_CDATA_SECTION_NODE name="" content="content"
576 wxString GetNodeContent(wxXmlNode *node);
578 // Check to see if a parameter exists.
579 bool HasParam(const wxString& param);
581 // Finds the node or returns NULL.
582 wxXmlNode *GetParamNode(const wxString& param);
584 // Finds the parameter value or returns the empty string.
585 wxString GetParamValue(const wxString& param);
587 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
588 // understood by this handler.
589 void AddStyle(const wxString& name, int value);
591 // Add styles common to all wxWindow-derived classes.
592 void AddWindowStyles();
594 // Gets style flags from text in form "flag | flag2| flag3 |..."
595 // Only understads flags added with AddStyle
596 int GetStyle(const wxString& param = wxPyStyleString, int defaults = 0);
598 // Gets text from param and does some conversions:
599 // - replaces \n, \r, \t by respective chars (according to C syntax)
600 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
601 // - calls wxGetTranslations (unless disabled in wxXmlResource)
602 wxString GetText(const wxString& param, bool translate = TRUE);
604 // Returns the XRCID.
607 // Returns the resource name.
610 // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE).
611 bool GetBool(const wxString& param, bool defaultv = FALSE);
613 // Gets the integer value from the parameter.
614 long GetLong( const wxString& param, long defaultv = 0 );
616 // Gets colour in HTML syntax (#RRGGBB).
617 wxColour GetColour(const wxString& param);
619 // Gets the size (may be in dialog units).
620 wxSize GetSize(const wxString& param = wxPySizeString);
622 // Gets the position (may be in dialog units).
623 wxPoint GetPosition(const wxString& param = wxPyPosString);
625 // Gets a dimension (may be in dialog units).
626 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
629 wxBitmap GetBitmap(const wxString& param = wxPyBitmapString,
630 const wxArtClient& defaultArtClient = wxART_OTHER,
631 wxSize size = wxDefaultSize);
634 wxIcon GetIcon(const wxString& param = wxPyIconString,
635 const wxArtClient& defaultArtClient = wxART_OTHER,
636 wxSize size = wxDefaultSize);
639 wxFont GetFont(const wxString& param = wxPyFontString);
641 // Sets common window options.
642 void SetupWindow(wxWindow *wnd);
645 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
648 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
650 // Creates a resource from a node.
651 wxObject *CreateResFromNode(wxXmlNode *node,
652 wxObject *parent, wxObject *instance = NULL);
655 wxFileSystem& GetCurFileSystem();
659 //----------------------------------------------------------------------
660 //----------------------------------------------------------------------
664 wxClassInfo::CleanUpClasses();
665 wxClassInfo::InitializeClasses();
667 wxXmlInitResourceModule();
668 wxXmlResource::Get()->InitAllHandlers();
672 //----------------------------------------------------------------------
673 // This file gets appended to the shadow class file.
674 //----------------------------------------------------------------------
676 %pragma(python) include="_xrcextras.py";