X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cd834e7b5a80dc0b0b9782de8606bdb401824e95..bfdc04a9bb95e6865570d59192718052b6229221:/wxPython/contrib/xrc/xrc.i?ds=sidebyside diff --git a/wxPython/contrib/xrc/xrc.i b/wxPython/contrib/xrc/xrc.i index 6783f33e8d..f2be7c4107 100644 --- a/wxPython/contrib/xrc/xrc.i +++ b/wxPython/contrib/xrc/xrc.i @@ -15,6 +15,8 @@ %{ #include "wxPython.h" +#include "pyistream.h" +#include "wx/xml/xml.h" #include "wx/xrc/xmlres.h" #include #include @@ -31,6 +33,23 @@ %extern events.i %extern controls.i +%extern streams.i + + +//--------------------------------------------------------------------------- +%{ + // Put some wx default wxChar* values into wxStrings. + static const wxString wxPyEmptyString(wxT("")); + static const wxString wxPyUTF8String(wxT("UTF-8")); + static const wxString wxPyStyleString(wxT("style")); + static const wxString wxPySizeString(wxT("size")); + static const wxString wxPyPosString(wxT("pos")); + static const wxString wxPyBitmapString(wxT("bitmap")); + static const wxString wxPyIconString(wxT("icon")); + static const wxString wxPyFontString(wxT("font")); +%} + +class wxPyXmlSubclassFactory; //--------------------------------------------------------------------------- @@ -104,11 +123,20 @@ public: // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler // (xmlres) can create include file that contains initialization code for // all controls used within the resource. - void AddHandler(wxXmlResourceHandler *handler); + void AddHandler(wxPyXmlResourceHandler *handler); + + // Add a new handler at the begining of the handler list + void InsertHandler(wxPyXmlResourceHandler *handler); // Removes all handlers void ClearHandlers(); + // Registers subclasses factory for use in XRC. This function is not meant + // for public use, please see the comment above wxXmlSubclassFactory + // definition. + static void AddSubclassFactory(wxPyXmlSubclassFactory *factory); + + // Loads menu from resource. Returns NULL on failure. wxMenu *LoadMenu(const wxString& name); @@ -135,9 +163,22 @@ public: wxPanel *LoadPanel(wxWindow *parent, const wxString& name); %name(LoadOnPanel)bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name); - bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name); + // Load a frame's contents from a resource + wxFrame *LoadFrame(wxWindow* parent, const wxString& name); + %name(LoadOnFrame)bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name); + + // Load an object from the resource specifying both the resource name and + // the classname. This lets you load nonstandard container windows. + wxObject *LoadObject(wxWindow *parent, const wxString& name, + const wxString& classname); - // Loads bitmap or icon resource from file: + // Load an object from the resource specifying both the resource name and + // the classname. This form lets you finish the creation of an existing + // instance. + %name(LoadOnObject)bool LoadObject(wxObject *instance, wxWindow *parent, const wxString& name, + const wxString& classname); + + // Loads bitmap or icon resource from file: wxBitmap LoadBitmap(const wxString& name); wxIcon LoadIcon(const wxString& name); @@ -164,6 +205,12 @@ public: // Sets global resources object and returns pointer to previous one (may be NULL). static wxXmlResource *Set(wxXmlResource *res); + // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING. + int GetFlags(); + + // Set flags after construction. + void SetFlags(int flags) { m_flags = flags; } + }; //---------------------------------------------------------------------- @@ -180,15 +227,455 @@ XMLCTRL = XRCCTRL " //---------------------------------------------------------------------- +// wxXmlSubclassFactory + + +%{ +class wxPyXmlSubclassFactory : public wxXmlSubclassFactory +{ +public: + wxPyXmlSubclassFactory() {} + DEC_PYCALLBACK_OBJECT_STRING_pure(Create); + PYPRIVATE; +}; + +IMP_PYCALLBACK_OBJECT_STRING_pure(wxPyXmlSubclassFactory, wxXmlSubclassFactory, Create); +%} + + +%name(wxXmlSubclassFactory)class wxPyXmlSubclassFactory { +public: + wxPyXmlSubclassFactory(); + + void _setCallbackInfo(PyObject* self, PyObject* _class); + %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxXmlSubclassFactory)" +}; -// TODO: Add wxXmlResourceHandler and allow it to be derived from. //---------------------------------------------------------------------- +// In order to provide wrappers for wxXmlResourceHandler we need to also +// provide the classes for representing and parsing XML. -%init %{ - wxClassInfo::CleanUpClasses(); - wxClassInfo::InitializeClasses(); +// Represents XML node type. +enum wxXmlNodeType +{ + // note: values are synchronized with xmlElementType from libxml + wxXML_ELEMENT_NODE, + wxXML_ATTRIBUTE_NODE, + wxXML_TEXT_NODE, + wxXML_CDATA_SECTION_NODE, + wxXML_ENTITY_REF_NODE, + wxXML_ENTITY_NODE, + wxXML_PI_NODE, + wxXML_COMMENT_NODE, + wxXML_DOCUMENT_NODE, + wxXML_DOCUMENT_TYPE_NODE, + wxXML_DOCUMENT_FRAG_NODE, + wxXML_NOTATION_NODE, + wxXML_HTML_DOCUMENT_NODE +}; + + + +// Represents node property(ies). +// Example: in "src" is property with value +// "hello.gif" and "id" is property with value "3". +class wxXmlProperty +{ +public: + wxXmlProperty(const wxString& name = wxPyEmptyString, + const wxString& value = wxPyEmptyString, + wxXmlProperty *next = NULL); + + wxString GetName() const; + wxString GetValue() const; + wxXmlProperty *GetNext() const; + + void SetName(const wxString& name); + void SetValue(const wxString& value); + void SetNext(wxXmlProperty *next); +}; + + + + +// Represents node in XML document. Node has name and may have content +// and properties. Most common node types are wxXML_TEXT_NODE (name and props +// are irrelevant) and wxXML_ELEMENT_NODE (e.g. in hi there is +// element with name="title", irrelevant content and one child (wxXML_TEXT_NODE +// with content="hi"). +// +// If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load +// (default is UTF-8). +class wxXmlNode +{ +public: + wxXmlNode(wxXmlNode *parent = NULL, + wxXmlNodeType type = 0, + const wxString& name = wxPyEmptyString, + const wxString& content = wxPyEmptyString, + wxXmlProperty *props = NULL, + wxXmlNode *next = NULL); + ~wxXmlNode(); + + + // user-friendly creation: + %name(wxXmlNodeEasy)wxXmlNode(wxXmlNodeType type, const wxString& name, + const wxString& content = wxPyEmptyString); + + void AddChild(wxXmlNode *child); + void InsertChild(wxXmlNode *child, wxXmlNode *before_node); + bool RemoveChild(wxXmlNode *child); + void AddProperty(wxXmlProperty *prop); + %name(AddPropertyName)void AddProperty(const wxString& name, const wxString& value); + bool DeleteProperty(const wxString& name); + + // access methods: + wxXmlNodeType GetType() const; + wxString GetName() const; + wxString GetContent() const; + + wxXmlNode *GetParent() const; + wxXmlNode *GetNext() const; + wxXmlNode *GetChildren() const; + + wxXmlProperty *GetProperties() const; + wxString GetPropVal(const wxString& propName, + const wxString& defaultVal) const; + bool HasProp(const wxString& propName) const; + + void SetType(wxXmlNodeType type); + void SetName(const wxString& name); + void SetContent(const wxString& con); + + void SetParent(wxXmlNode *parent); + void SetNext(wxXmlNode *next); + void SetChildren(wxXmlNode *child); + + void SetProperties(wxXmlProperty *prop); +}; + + + +// This class holds XML data/document as parsed by XML parser. +class wxXmlDocument : public wxObject +{ +public: + wxXmlDocument(const wxString& filename, + const wxString& encoding = wxPyUTF8String); + %name(wxXmlDocumentFromStream)wxXmlDocument(wxInputStream& stream, + const wxString& encoding = wxPyUTF8String); + %name(wxEmptyXmlDocument)wxXmlDocument(); + + ~wxXmlDocument(); + + + // Parses .xml file and loads data. Returns TRUE on success, FALSE + // otherwise. + bool Load(const wxString& filename, + const wxString& encoding = wxPyUTF8String); + %name(LoadFromStream)bool Load(wxInputStream& stream, + const wxString& encoding = wxPyUTF8String); + + // Saves document as .xml file. + bool Save(const wxString& filename) const; + %name(SaveToStream)bool Save(wxOutputStream& stream) const; + + bool IsOk() const; + + // Returns root node of the document. + wxXmlNode *GetRoot() const; + + // Returns version of document (may be empty). + wxString GetVersion() const; + + // Returns encoding of document (may be empty). + // Note: this is the encoding original file was saved in, *not* the + // encoding of in-memory representation! + wxString GetFileEncoding() const; + + // Write-access methods: + void SetRoot(wxXmlNode *node); + void SetVersion(const wxString& version); + void SetFileEncoding(const wxString& encoding); + + %addmethods { + // Returns encoding of in-memory representation of the document (same + // as passed to Load or ctor, defaults to UTF-8). NB: this is + // meaningless in Unicode build where data are stored as wchar_t* + wxString GetEncoding() { + #if wxUSE_UNICODE + return wxPyEmptyString; + #else + return self->GetEncoding(); + #endif + } + void SetEncoding(const wxString& enc) { + #if wxUSE_UNICODE + // do nothing + #else + self->SetEncoding(enc); + #endif + } + } +}; + + +//---------------------------------------------------------------------- +// And now for wxXmlResourceHandler... + + + +%{ // C++ version of Python aware wxXmlResourceHandler, for the pure virtual + // callbacks, as well as to make some protected things public so they can + // be wrapped. +class wxPyXmlResourceHandler : public wxXmlResourceHandler { +public: + wxPyXmlResourceHandler() : wxXmlResourceHandler() {} + //~wxPyXmlResourceHandler(); + + // Base class virtuals + + DEC_PYCALLBACK_OBJECT__pure(DoCreateResource); + DEC_PYCALLBACK_BOOL_NODE_pure(CanHandle); + + + // accessors for protected members + + wxXmlResource* GetResource() { return m_resource; } + wxXmlNode* GetNode() { return m_node; } + wxString GetClass() { return m_class; } + wxObject* GetParent() { return m_parent; } + wxObject* GetInstance() { return m_instance; } + wxWindow* GetParentAsWindow() { return m_parentAsWindow; } + wxWindow* GetInstanceAsWindow() { return m_instanceAsWindow; } + + + // turn some protected methods into public via delegation + + bool IsOfClass(wxXmlNode *node, const wxString& classname) + { return wxXmlResourceHandler::IsOfClass(node, classname); } + + wxString GetNodeContent(wxXmlNode *node) + { return wxXmlResourceHandler::GetNodeContent(node); } + + bool HasParam(const wxString& param) + { return wxXmlResourceHandler::HasParam(param); } + + wxXmlNode *GetParamNode(const wxString& param) + { return wxXmlResourceHandler::GetParamNode(param); } + + wxString GetParamValue(const wxString& param) + { return wxXmlResourceHandler::GetParamValue(param); } + + void AddStyle(const wxString& name, int value) + { wxXmlResourceHandler::AddStyle(name, value); } + + void AddWindowStyles() + { wxXmlResourceHandler::AddWindowStyles(); } + + int GetStyle(const wxString& param = wxT("style"), int defaults = 0) + { return wxXmlResourceHandler::GetStyle(param, defaults); } + + wxString GetText(const wxString& param, bool translate = TRUE) + { return wxXmlResourceHandler::GetText(param, translate); } + + int GetID() + { return wxXmlResourceHandler::GetID(); } + + wxString GetName() + { return wxXmlResourceHandler::GetName(); } + + bool GetBool(const wxString& param, bool defaultv = FALSE) + { return wxXmlResourceHandler::GetBool(param, defaultv); } + + long GetLong( const wxString& param, long defaultv = 0 ) + { return wxXmlResourceHandler::GetLong(param, defaultv); } + + wxColour GetColour(const wxString& param) + { return wxXmlResourceHandler::GetColour(param); } + + wxSize GetSize(const wxString& param = wxT("size")) + { return wxXmlResourceHandler::GetSize(param); } + + wxPoint GetPosition(const wxString& param = wxT("pos")) + { return wxXmlResourceHandler::GetPosition(param); } + + wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0) + { return wxXmlResourceHandler::GetDimension(param, defaultv); } + + wxBitmap GetBitmap(const wxString& param = wxT("bitmap"), + const wxArtClient& defaultArtClient = wxART_OTHER, + wxSize size = wxDefaultSize) + { return wxXmlResourceHandler::GetBitmap(param, defaultArtClient, size); } + + wxIcon GetIcon(const wxString& param = wxT("icon"), + const wxArtClient& defaultArtClient = wxART_OTHER, + wxSize size = wxDefaultSize) + { return wxXmlResourceHandler::GetIcon(param, defaultArtClient, size); } + + wxFont GetFont(const wxString& param = wxT("font")) + { return wxXmlResourceHandler::GetFont(param); } + + void SetupWindow(wxWindow *wnd) + { wxXmlResourceHandler::SetupWindow(wnd); } + + void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE) + { wxXmlResourceHandler::CreateChildren(parent, this_hnd_only); } + + void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL) + { wxXmlResourceHandler::CreateChildrenPrivately(parent, rootnode); } + + wxObject *CreateResFromNode(wxXmlNode *node, + wxObject *parent, wxObject *instance = NULL) + { return wxXmlResourceHandler::CreateResFromNode(node, parent, instance); } + + wxFileSystem& GetCurFileSystem() + { return wxXmlResourceHandler::GetCurFileSystem(); } + + + PYPRIVATE; +}; + +IMP_PYCALLBACK_OBJECT__pure(wxPyXmlResourceHandler, wxXmlResourceHandler, DoCreateResource); +IMP_PYCALLBACK_BOOL_NODE_pure(wxPyXmlResourceHandler, wxXmlResourceHandler, CanHandle); + +%} + + +//---------------------------------------------------------------------- +// Now the version that will be SWIGged. + + +%name(wxXmlResourceHandler) class wxPyXmlResourceHandler : public wxObject { +public: + wxPyXmlResourceHandler() : wxXmlResourceHandler() {} + //~wxPyXmlResourceHandler(); + + void _setCallbackInfo(PyObject* self, PyObject* _class); + %pragma(python) addtomethod = "__init__:self._setCallbackInfo(self, wxXmlResourceHandler)" + + + + // Creates an object (menu, dialog, control, ...) from an XML node. + // Should check for validity. + // parent is a higher-level object (usually window, dialog or panel) + // that is often neccessary to create the resource. + // If instance is non-NULL it should not create a new instance via 'new' but + // should rather use this one, and call its Create method. + wxObject *CreateResource(wxXmlNode *node, wxObject *parent, + wxObject *instance); + + // Sets the parent resource. + void SetParentResource(wxXmlResource *res); + + + wxXmlResource* GetResource() { return m_resource; } + wxXmlNode* GetNode() { return m_node; } + wxString GetClass() { return m_class; } + wxObject* GetParent() { return m_parent; } + wxObject* GetInstance() { return m_instance; } + wxWindow* GetParentAsWindow() { return m_parentAsWindow; } + wxWindow* GetInstanceAsWindow() { return m_instanceAsWindow; } + + + // Returns true if the node has a property class equal to classname, + // e.g. . + bool IsOfClass(wxXmlNode *node, const wxString& classname); + + // Gets node content from wxXML_ENTITY_NODE + // The problem is, content is represented as + // wxXML_ENTITY_NODE name="tag", content="" + // |-- wxXML_TEXT_NODE or + // wxXML_CDATA_SECTION_NODE name="" content="content" + wxString GetNodeContent(wxXmlNode *node); + + // Check to see if a parameter exists. + bool HasParam(const wxString& param); + + // Finds the node or returns NULL. + wxXmlNode *GetParamNode(const wxString& param); + + // Finds the parameter value or returns the empty string. + wxString GetParamValue(const wxString& param); + + // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags + // understood by this handler. + void AddStyle(const wxString& name, int value); + + // Add styles common to all wxWindow-derived classes. + void AddWindowStyles(); + + // Gets style flags from text in form "flag | flag2| flag3 |..." + // Only understads flags added with AddStyle + int GetStyle(const wxString& param = wxPyStyleString, int defaults = 0); + + // Gets text from param and does some conversions: + // - replaces \n, \r, \t by respective chars (according to C syntax) + // - replaces _ by & and __ by _ (needed for _File => &File because of XML) + // - calls wxGetTranslations (unless disabled in wxXmlResource) + wxString GetText(const wxString& param, bool translate = TRUE); + + // Returns the XRCID. + int GetID(); + + // Returns the resource name. + wxString GetName(); + + // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE). + bool GetBool(const wxString& param, bool defaultv = FALSE); + + // Gets the integer value from the parameter. + long GetLong( const wxString& param, long defaultv = 0 ); + + // Gets colour in HTML syntax (#RRGGBB). + wxColour GetColour(const wxString& param); + + // Gets the size (may be in dialog units). + wxSize GetSize(const wxString& param = wxPySizeString); + + // Gets the position (may be in dialog units). + wxPoint GetPosition(const wxString& param = wxPyPosString); + + // Gets a dimension (may be in dialog units). + wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0); + + // Gets a bitmap. + wxBitmap GetBitmap(const wxString& param = wxPyBitmapString, + const wxArtClient& defaultArtClient = wxART_OTHER, + wxSize size = wxDefaultSize); + + // Gets an icon. + wxIcon GetIcon(const wxString& param = wxPyIconString, + const wxArtClient& defaultArtClient = wxART_OTHER, + wxSize size = wxDefaultSize); + + // Gets a font. + wxFont GetFont(const wxString& param = wxPyFontString); + + // Sets common window options. + void SetupWindow(wxWindow *wnd); + + // Creates children. + void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE); + + // Helper function. + void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL); + + // Creates a resource from a node. + wxObject *CreateResFromNode(wxXmlNode *node, + wxObject *parent, wxObject *instance = NULL); + + // helper + wxFileSystem& GetCurFileSystem(); +}; + + +//---------------------------------------------------------------------- +//---------------------------------------------------------------------- + +%init %{ wxXmlInitResourceModule(); wxXmlResource::Get()->InitAllHandlers();