From 3d20fa703244b1f6e087b4967c471210ebadb8d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Mon, 7 Apr 2003 22:16:27 +0000 Subject: [PATCH] XRC fixes for and relative file names git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@20068 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- contrib/include/wx/xrc/xmlres.h | 12 +++++- contrib/src/xrc/xmlres.cpp | 71 ++++++++++++++++++++++++--------- include/wx/xrc/xmlres.h | 12 +++++- src/xrc/xmlres.cpp | 71 ++++++++++++++++++++++++--------- 4 files changed, 126 insertions(+), 40 deletions(-) diff --git a/contrib/include/wx/xrc/xmlres.h b/contrib/include/wx/xrc/xmlres.h index 5f4a800261..ccf92157cf 100644 --- a/contrib/include/wx/xrc/xmlres.h +++ b/contrib/include/wx/xrc/xmlres.h @@ -240,8 +240,18 @@ protected: wxXmlNode *DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive); // Creates a resource from information in the given node. - wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL); + wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, + wxObject *instance = NULL); + // Creates a resource from information in the given node + // (Uses only 'handlerToUse' if != NULL) + // + // ATTENTION: Do *NOT* use this function, it will disappear in + // wxWindows 2.5.0! It exists *only* as a hack to preserve + // binary compatibility in 2.4.x branch. + wxObject *CreateResFromNode2(wxXmlNode *node, wxObject *parent, + wxObject *instance = NULL, + wxXmlResourceHandler *handlerToUse = NULL); private: long m_version; diff --git a/contrib/src/xrc/xmlres.cpp b/contrib/src/xrc/xmlres.cpp index 9a860e0ec5..4f87c3d848 100644 --- a/contrib/src/xrc/xmlres.cpp +++ b/contrib/src/xrc/xmlres.cpp @@ -24,6 +24,7 @@ #include "wx/frame.h" #include "wx/wfstream.h" #include "wx/filesys.h" +#include "wx/filename.h" #include "wx/log.h" #include "wx/intl.h" #include "wx/tokenzr.h" @@ -99,12 +100,29 @@ bool wxXmlResource::Load(const wxString& filemask) fnd = filemask; while (!!fnd) { -#if wxUSE_FILESYSTEM - if (filemask.Lower().Matches(wxT("*.zip")) || - filemask.Lower().Matches(wxT("*.xrs"))) + // NB: Load() accepts both filenames and URLs (should probably be + // changed to filenames only, but embedded resources currently + // rely on its ability to handle URLs - FIXME). This check + // serves as a quick way to determine whether found name is + // filename and not URL: + if (wxFileName::FileExists(fnd)) { - rt = rt && Load(fnd + wxT("#zip:*.xmlbin")); - rt = rt && Load(fnd + wxT("#zip:*.xrc")); + // Make the name absolute filename, because the app may + // change working directory later: + wxFileName fn(fnd); + if (fn.IsRelative()) + { + fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE); + fnd = fn.GetFullPath(); + } + } + +#if wxUSE_FILESYSTEM + if (fnd.Lower().Matches(wxT("*.zip")) || + fnd.Lower().Matches(wxT("*.xrs"))) + { + wxString url(wxFileSystem::FileNameToURL(fnd)); + rt = rt && Load(url + wxT("#zip:*.xrc")); } else #endif @@ -517,7 +535,15 @@ static void MergeNodes(wxXmlNode& dest, wxXmlNode& with) dest.SetContent(with.GetContent()); } -wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance) +wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, + wxObject *instance) +{ + return CreateResFromNode2(node, parent, instance); +} + +wxObject *wxXmlResource::CreateResFromNode2(wxXmlNode *node, wxObject *parent, + wxObject *instance, + wxXmlResourceHandler *handlerToUse) { if (node == NULL) return NULL; @@ -541,17 +567,26 @@ wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, wx } wxXmlResourceHandler *handler; - wxObject *ret; - wxNode * ND = m_handlers.GetFirst(); - while (ND) - { - handler = (wxXmlResourceHandler*)ND->GetData(); - if (node->GetName() == wxT("object") && handler->CanHandle(node)) + + if (handlerToUse) + { + if (handlerToUse->CanHandle(node)) { - ret = handler->CreateResource(node, parent, instance); - if (ret) return ret; + return handlerToUse->CreateResource(node, parent, instance); + } + } + else if (node->GetName() == wxT("object")) + { + wxNode *ND = m_handlers.GetFirst(); + while (ND) + { + handler = (wxXmlResourceHandler*)ND->GetData(); + if (handler->CanHandle(node)) + { + return handler->CreateResource(node, parent, instance); + } + ND = ND->GetNext(); } - ND = ND->GetNext(); } wxLogError(_("No handler found for XML node '%s', class '%s'!"), @@ -1084,10 +1119,8 @@ void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only) if (n->GetType() == wxXML_ELEMENT_NODE && (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref"))) { - if (this_hnd_only && CanHandle(n)) - CreateResource(n, parent, NULL); - else - m_resource->CreateResFromNode(n, parent, NULL); + m_resource->CreateResFromNode2(n, parent, NULL, + this_hnd_only ? this : NULL); } n = n->GetNext(); } diff --git a/include/wx/xrc/xmlres.h b/include/wx/xrc/xmlres.h index 5f4a800261..ccf92157cf 100644 --- a/include/wx/xrc/xmlres.h +++ b/include/wx/xrc/xmlres.h @@ -240,8 +240,18 @@ protected: wxXmlNode *DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive); // Creates a resource from information in the given node. - wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL); + wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, + wxObject *instance = NULL); + // Creates a resource from information in the given node + // (Uses only 'handlerToUse' if != NULL) + // + // ATTENTION: Do *NOT* use this function, it will disappear in + // wxWindows 2.5.0! It exists *only* as a hack to preserve + // binary compatibility in 2.4.x branch. + wxObject *CreateResFromNode2(wxXmlNode *node, wxObject *parent, + wxObject *instance = NULL, + wxXmlResourceHandler *handlerToUse = NULL); private: long m_version; diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index 9a860e0ec5..4f87c3d848 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -24,6 +24,7 @@ #include "wx/frame.h" #include "wx/wfstream.h" #include "wx/filesys.h" +#include "wx/filename.h" #include "wx/log.h" #include "wx/intl.h" #include "wx/tokenzr.h" @@ -99,12 +100,29 @@ bool wxXmlResource::Load(const wxString& filemask) fnd = filemask; while (!!fnd) { -#if wxUSE_FILESYSTEM - if (filemask.Lower().Matches(wxT("*.zip")) || - filemask.Lower().Matches(wxT("*.xrs"))) + // NB: Load() accepts both filenames and URLs (should probably be + // changed to filenames only, but embedded resources currently + // rely on its ability to handle URLs - FIXME). This check + // serves as a quick way to determine whether found name is + // filename and not URL: + if (wxFileName::FileExists(fnd)) { - rt = rt && Load(fnd + wxT("#zip:*.xmlbin")); - rt = rt && Load(fnd + wxT("#zip:*.xrc")); + // Make the name absolute filename, because the app may + // change working directory later: + wxFileName fn(fnd); + if (fn.IsRelative()) + { + fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE); + fnd = fn.GetFullPath(); + } + } + +#if wxUSE_FILESYSTEM + if (fnd.Lower().Matches(wxT("*.zip")) || + fnd.Lower().Matches(wxT("*.xrs"))) + { + wxString url(wxFileSystem::FileNameToURL(fnd)); + rt = rt && Load(url + wxT("#zip:*.xrc")); } else #endif @@ -517,7 +535,15 @@ static void MergeNodes(wxXmlNode& dest, wxXmlNode& with) dest.SetContent(with.GetContent()); } -wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance) +wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, + wxObject *instance) +{ + return CreateResFromNode2(node, parent, instance); +} + +wxObject *wxXmlResource::CreateResFromNode2(wxXmlNode *node, wxObject *parent, + wxObject *instance, + wxXmlResourceHandler *handlerToUse) { if (node == NULL) return NULL; @@ -541,17 +567,26 @@ wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, wx } wxXmlResourceHandler *handler; - wxObject *ret; - wxNode * ND = m_handlers.GetFirst(); - while (ND) - { - handler = (wxXmlResourceHandler*)ND->GetData(); - if (node->GetName() == wxT("object") && handler->CanHandle(node)) + + if (handlerToUse) + { + if (handlerToUse->CanHandle(node)) { - ret = handler->CreateResource(node, parent, instance); - if (ret) return ret; + return handlerToUse->CreateResource(node, parent, instance); + } + } + else if (node->GetName() == wxT("object")) + { + wxNode *ND = m_handlers.GetFirst(); + while (ND) + { + handler = (wxXmlResourceHandler*)ND->GetData(); + if (handler->CanHandle(node)) + { + return handler->CreateResource(node, parent, instance); + } + ND = ND->GetNext(); } - ND = ND->GetNext(); } wxLogError(_("No handler found for XML node '%s', class '%s'!"), @@ -1084,10 +1119,8 @@ void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only) if (n->GetType() == wxXML_ELEMENT_NODE && (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref"))) { - if (this_hnd_only && CanHandle(n)) - CreateResource(n, parent, NULL); - else - m_resource->CreateResFromNode(n, parent, NULL); + m_resource->CreateResFromNode2(n, parent, NULL, + this_hnd_only ? this : NULL); } n = n->GetNext(); }