added wxXmlResource::Unload() (replaces patch 1178853)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@34839 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2005-07-13 18:01:43 +00:00
parent 2148dc99a9
commit 60fd818a78
4 changed files with 135 additions and 23 deletions

View File

@ -10,6 +10,10 @@ All:
- Fixed wxScopeGuard to work with VC++, documented it. - Fixed wxScopeGuard to work with VC++, documented it.
- Fixed proxy handling in wxURL. - Fixed proxy handling in wxURL.
All (GUI):
- Added wxXmlResource::Unload()
wxMSW: wxMSW:
- Fixed multiline tooltips handling. - Fixed multiline tooltips handling.

View File

@ -32,6 +32,7 @@ enum wxXmlResourceFlags
\latexignore{\rtfignore{\wxheading{Members}}} \latexignore{\rtfignore{\wxheading{Members}}}
\membersection{wxXmlResource::wxXmlResource}\label{wxxmlresourcector} \membersection{wxXmlResource::wxXmlResource}\label{wxxmlresourcector}
\func{}{wxXmlResource}{\param{const wxString\& }{filemask}, \param{int }{flags = wxXRC\_USE\_LOCALE}} \func{}{wxXmlResource}{\param{const wxString\& }{filemask}, \param{int }{flags = wxXRC\_USE\_LOCALE}}
@ -55,12 +56,14 @@ wxXRC\_NO\_SUBCLASSING: subclass property of object nodes will be ignored
XRC files from being reloaded from disk in case they have been modified there XRC files from being reloaded from disk in case they have been modified there
since being last loaded (may slightly speed up loading them).} since being last loaded (may slightly speed up loading them).}
\membersection{wxXmlResource::\destruct{wxXmlResource}}\label{wxxmlresourcedtor} \membersection{wxXmlResource::\destruct{wxXmlResource}}\label{wxxmlresourcedtor}
\func{}{\destruct{wxXmlResource}}{\void} \func{}{\destruct{wxXmlResource}}{\void}
Destructor. Destructor.
\membersection{wxXmlResource::AddHandler}\label{wxxmlresourceaddhandler} \membersection{wxXmlResource::AddHandler}\label{wxxmlresourceaddhandler}
\func{void}{AddHandler}{\param{wxXmlResourceHandler* }{handler}} \func{void}{AddHandler}{\param{wxXmlResourceHandler* }{handler}}
@ -71,6 +74,7 @@ wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource compiler
(wxxrc) can create include file that contains initialization code for (wxxrc) can create include file that contains initialization code for
all controls used within the resource. all controls used within the resource.
\membersection{wxXmlResource::AttachUnknownControl}\label{wxxmlresourceattachunknowncontrol} \membersection{wxXmlResource::AttachUnknownControl}\label{wxxmlresourceattachunknowncontrol}
\func{bool}{AttachUnknownControl}{\param{const wxString\& }{name}, \param{wxWindow* }{control}, \param{wxWindow* }{parent = NULL}} \func{bool}{AttachUnknownControl}{\param{const wxString\& }{name}, \param{wxWindow* }{control}, \param{wxWindow* }{parent = NULL}}
@ -78,12 +82,14 @@ all controls used within the resource.
Attaches an unknown control to the given panel/window/dialog. Attaches an unknown control to the given panel/window/dialog.
Unknown controls are used in conjunction with <object class="unknown">. Unknown controls are used in conjunction with <object class="unknown">.
\membersection{wxXmlResource::ClearHandlers}\label{wxxmlresourceclearhandlers} \membersection{wxXmlResource::ClearHandlers}\label{wxxmlresourceclearhandlers}
\func{void}{ClearHandlers}{\void} \func{void}{ClearHandlers}{\void}
Removes all handlers. Removes all handlers.
\membersection{wxXmlResource::CompareVersion}\label{wxxmlresourcecompareversion} \membersection{wxXmlResource::CompareVersion}\label{wxxmlresourcecompareversion}
\constfunc{int}{CompareVersion}{\param{int }{major}, \param{int }{minor}, \param{int }{release}, \param{int }{revision}} \constfunc{int}{CompareVersion}{\param{int }{major}, \param{int }{minor}, \param{int }{release}, \param{int }{revision}}
@ -91,24 +97,28 @@ Removes all handlers.
Compares the XRC version to the argument. Returns -1 if the XRC version Compares the XRC version to the argument. Returns -1 if the XRC version
is less than the argument, +1 if greater, and 0 if they equal. is less than the argument, +1 if greater, and 0 if they equal.
\membersection{wxXmlResource::Get}\label{wxxmlresourceget} \membersection{wxXmlResource::Get}\label{wxxmlresourceget}
\func{wxXmlResource*}{Get}{\void} \func{wxXmlResource*}{Get}{\void}
Gets the global resources object or creates one if none exists. Gets the global resources object or creates one if none exists.
\membersection{wxXmlResource::GetFlags}\label{wxxmlresourcegetflags} \membersection{wxXmlResource::GetFlags}\label{wxxmlresourcegetflags}
\func{int}{GetFlags}{\void} \func{int}{GetFlags}{\void}
Returns flags, which may be a bitlist of wxXRC\_USE\_LOCALE and wxXRC\_NO\_SUBCLASSING. Returns flags, which may be a bitlist of wxXRC\_USE\_LOCALE and wxXRC\_NO\_SUBCLASSING.
\membersection{wxXmlResource::GetVersion}\label{wxxmlresourcegetversion} \membersection{wxXmlResource::GetVersion}\label{wxxmlresourcegetversion}
\constfunc{long}{GetVersion}{\void} \constfunc{long}{GetVersion}{\void}
Returns version information (a.b.c.d = d+ 256*c + 256\textasciicircum2*b + 256\textasciitilde3*a). Returns version information (a.b.c.d = d+ 256*c + 256\textasciicircum2*b + 256\textasciitilde3*a).
\membersection{wxXmlResource::GetXRCID}\label{wxxmlresourcegetxmlid} \membersection{wxXmlResource::GetXRCID}\label{wxxmlresourcegetxmlid}
\func{int}{GetXRCID}{\param{const wxChar* }{str\_id}} \func{int}{GetXRCID}{\param{const wxChar* }{str\_id}}
@ -117,6 +127,7 @@ Returns a numeric ID that is equivalent to the string ID used in an XML
resource. To be used in event tables. resource. To be used in event tables.
The macro {\tt XRCID(name)} is provided for convenience. The macro {\tt XRCID(name)} is provided for convenience.
\membersection{wxXmlResource::InitAllHandlers}\label{wxxmlresourceinitallhandlers} \membersection{wxXmlResource::InitAllHandlers}\label{wxxmlresourceinitallhandlers}
\func{void}{InitAllHandlers}{\void} \func{void}{InitAllHandlers}{\void}
@ -125,6 +136,7 @@ Initializes handlers for all supported controls/windows. This will
make the executable quite big because it forces linking against make the executable quite big because it forces linking against
most of the wxWidgets library. most of the wxWidgets library.
\membersection{wxXmlResource::Load}\label{wxxmlresourceload} \membersection{wxXmlResource::Load}\label{wxxmlresourceload}
\func{bool}{Load}{\param{const wxString\& }{filemask}} \func{bool}{Load}{\param{const wxString\& }{filemask}}
@ -132,12 +144,14 @@ most of the wxWidgets library.
Loads resources from XML files that match given filemask. Loads resources from XML files that match given filemask.
This method understands VFS (see filesys.h). This method understands VFS (see filesys.h).
\membersection{wxXmlResource::LoadBitmap}\label{wxxmlresourceloadbitmap} \membersection{wxXmlResource::LoadBitmap}\label{wxxmlresourceloadbitmap}
\func{wxBitmap}{LoadBitmap}{\param{const wxString\& }{name}} \func{wxBitmap}{LoadBitmap}{\param{const wxString\& }{name}}
Loads a bitmap resource from a file. Loads a bitmap resource from a file.
\membersection{wxXmlResource::LoadDialog}\label{wxxmlresourceloaddialog} \membersection{wxXmlResource::LoadDialog}\label{wxxmlresourceloaddialog}
\func{wxDialog*}{LoadDialog}{\param{wxWindow* }{parent}, \param{const wxString\& }{name}} \func{wxDialog*}{LoadDialog}{\param{wxWindow* }{parent}, \param{const wxString\& }{name}}
@ -159,24 +173,28 @@ Example:
dlg->ShowModal(); dlg->ShowModal();
\end{verbatim} \end{verbatim}
\membersection{wxXmlResource::LoadFrame}\label{wxxmlresourceloadframe} \membersection{wxXmlResource::LoadFrame}\label{wxxmlresourceloadframe}
\func{bool}{LoadFrame}{\param{wxFrame* }{frame}, \param{wxWindow* }{parent}, \param{const wxString\& }{name}} \func{bool}{LoadFrame}{\param{wxFrame* }{frame}, \param{wxWindow* }{parent}, \param{const wxString\& }{name}}
Loads a frame. Loads a frame.
\membersection{wxXmlResource::LoadIcon}\label{wxxmlresourceloadicon} \membersection{wxXmlResource::LoadIcon}\label{wxxmlresourceloadicon}
\func{wxIcon}{LoadIcon}{\param{const wxString\& }{name}} \func{wxIcon}{LoadIcon}{\param{const wxString\& }{name}}
Loads an icon resource from a file. Loads an icon resource from a file.
\membersection{wxXmlResource::LoadMenu}\label{wxxmlresourceloadmenu} \membersection{wxXmlResource::LoadMenu}\label{wxxmlresourceloadmenu}
\func{wxMenu*}{LoadMenu}{\param{const wxString\& }{name}} \func{wxMenu*}{LoadMenu}{\param{const wxString\& }{name}}
Loads menu from resource. Returns NULL on failure. Loads menu from resource. Returns NULL on failure.
\membersection{wxXmlResource::LoadMenuBar}\label{wxxmlresourceloadmenubar} \membersection{wxXmlResource::LoadMenuBar}\label{wxxmlresourceloadmenubar}
\func{wxMenuBar*}{LoadMenuBar}{\param{wxWindow* }{parent}, \param{const wxString\& }{name}} \func{wxMenuBar*}{LoadMenuBar}{\param{wxWindow* }{parent}, \param{const wxString\& }{name}}
@ -187,6 +205,7 @@ Loads a menubar from resource. Returns NULL on failure.
Loads a menubar from resource. Returns NULL on failure. Loads a menubar from resource. Returns NULL on failure.
\membersection{wxXmlResource::LoadPanel}\label{wxxmlresourceloadpanel} \membersection{wxXmlResource::LoadPanel}\label{wxxmlresourceloadpanel}
\func{wxPanel*}{LoadPanel}{\param{wxWindow* }{parent}, \param{const wxString\& }{name}} \func{wxPanel*}{LoadPanel}{\param{wxWindow* }{parent}, \param{const wxString\& }{name}}
@ -198,21 +217,35 @@ Loads a panel. {\it panel} points to parent window (if any).
Loads a panel. {\it panel} points to parent window (if any). This form Loads a panel. {\it panel} points to parent window (if any). This form
is used to finish creation of an already existing instance. is used to finish creation of an already existing instance.
\membersection{wxXmlResource::LoadToolBar}\label{wxxmlresourceloadtoolbar} \membersection{wxXmlResource::LoadToolBar}\label{wxxmlresourceloadtoolbar}
\func{wxToolBar*}{LoadToolBar}{\param{wxWindow* }{parent}, \param{const wxString\& }{name}} \func{wxToolBar*}{LoadToolBar}{\param{wxWindow* }{parent}, \param{const wxString\& }{name}}
Loads a toolbar. Loads a toolbar.
\membersection{wxXmlResource::Set}\label{wxxmlresourceset} \membersection{wxXmlResource::Set}\label{wxxmlresourceset}
\func{wxXmlResource*}{Set}{\param{wxXmlResource* }{res}} \func{wxXmlResource*}{Set}{\param{wxXmlResource* }{res}}
Sets the global resources object and returns a pointer to the previous one (may be NULL). Sets the global resources object and returns a pointer to the previous one (may be NULL).
\membersection{wxXmlResource::SetFlags}\label{wxxmlresourcesetflags} \membersection{wxXmlResource::SetFlags}\label{wxxmlresourcesetflags}
\func{void}{SetFlags}{\param{int }{flags}} \func{void}{SetFlags}{\param{int }{flags}}
Sets flags (bitlist of wxXRC\_USE\_LOCALE and wxXRC\_NO\_SUBCLASSING). Sets flags (bitlist of wxXRC\_USE\_LOCALE and wxXRC\_NO\_SUBCLASSING).
\membersection{wxXmlResource::Unload}\label{wxxmlresourceunload}
\func{bool}{Unload}{\param{const wxString\& }{filename}}
This function unloads a resource previously loaded by
\helpref{Load()}{wxxmlresourceload}.
Returns \true if the resource was successfully unloaded and \false if it hasn't
been found in the list of loaded resources.

View File

@ -124,6 +124,9 @@ public:
// This method understands VFS (see filesys.h). // This method understands VFS (see filesys.h).
bool Load(const wxString& filemask); bool Load(const wxString& filemask);
// Unload resource from the given XML file (wildcards not allowed)
bool Unload(const wxString& filename);
// Initialize handlers for all supported controls/windows. This will // Initialize handlers for all supported controls/windows. This will
// make the executable quite big because it forces linking against // make the executable quite big because it forces linking against
// most of the wxWidgets library. // most of the wxWidgets library.
@ -250,6 +253,17 @@ protected:
wxObject *instance = NULL, wxObject *instance = NULL,
wxXmlResourceHandler *handlerToUse = NULL); wxXmlResourceHandler *handlerToUse = NULL);
// Helper of Load() and Unload(): returns the URL corresponding to the
// given file if it's indeed a file, otherwise returns the original string
// unmodified
static wxString ConvertFileNameToURL(const wxString& filename);
// loading resources from archives is impossible without wxFileSystem
#if wxUSE_FILESYSTEM
// Another helper: detect if the filename is a ZIP or XRS file
static bool IsArchive(const wxString& filename);
#endif // wxUSE_FILESYSTEM
private: private:
long m_version; long m_version;

View File

@ -84,6 +84,46 @@ wxXmlResource::~wxXmlResource()
} }
/* static */
wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename)
{
wxString fnd(filename);
// NB: as Load() and Unload() accept both filenames and URLs (should
// probably be changed to filenames only, but embedded resources
// currently rely on its ability to handle URLs - FIXME) we need to
// determine whether found name is filename and not URL and this is the
// fastest/simplest way to do it
if (wxFileName::FileExists(fnd))
{
// Make the name absolute filename, because the app may
// change working directory later:
wxFileName fn(fnd);
if (fn.IsRelative())
{
fn.MakeAbsolute();
fnd = fn.GetFullPath();
}
#if wxUSE_FILESYSTEM
fnd = wxFileSystem::FileNameToURL(fnd);
#endif
}
return fnd;
}
#if wxUSE_FILESYSTEM
/* static */
bool wxXmlResource::IsArchive(const wxString& filename)
{
const wxString fnd = filename.Lower();
return fnd.Matches(wxT("*.zip")) || fnd.Matches(wxT("*.xrs"));
}
#endif // wxUSE_FILESYSTEM
bool wxXmlResource::Load(const wxString& filemask) bool wxXmlResource::Load(const wxString& filemask)
{ {
wxString fnd; wxString fnd;
@ -105,34 +145,15 @@ bool wxXmlResource::Load(const wxString& filemask)
fnd = filemask; fnd = filemask;
while (!fnd.empty()) while (!fnd.empty())
{ {
// NB: Load() accepts both filenames and URLs (should probably be fnd = ConvertFileNameToURL(fnd);
// 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))
{
// Make the name absolute filename, because the app may
// change working directory later:
wxFileName fn(fnd);
if (fn.IsRelative())
{
fn.MakeAbsolute();
fnd = fn.GetFullPath();
}
#if wxUSE_FILESYSTEM
fnd = wxFileSystem::FileNameToURL(fnd);
#endif
}
#if wxUSE_FILESYSTEM #if wxUSE_FILESYSTEM
if (fnd.Lower().Matches(wxT("*.zip")) || if ( IsArchive(fnd) )
fnd.Lower().Matches(wxT("*.xrs")))
{ {
rt = rt && Load(fnd + wxT("#zip:*.xrc")); rt = rt && Load(fnd + wxT("#zip:*.xrc"));
} }
else else // a single resource URL
#endif #endif // wxUSE_FILESYSTEM
{ {
drec = new wxXmlResourceDataRecord; drec = new wxXmlResourceDataRecord;
drec->File = fnd; drec->File = fnd;
@ -149,6 +170,46 @@ bool wxXmlResource::Load(const wxString& filemask)
return rt && UpdateResources(); return rt && UpdateResources();
} }
bool wxXmlResource::Unload(const wxString& filename)
{
wxASSERT_MSG( !wxIsWild(filename),
_T("wildcards not supported by wxXmlResource::Unload()") );
wxString fnd = ConvertFileNameToURL(filename);
#if wxUSE_FILESYSTEM
const bool isArchive = IsArchive(fnd);
if ( isArchive )
fnd += _T("#zip:");
#endif // wxUSE_FILESYSTEM
bool unloaded = false;
const size_t count = m_data.GetCount();
for ( size_t i = 0; i < count; i++ )
{
#if wxUSE_FILESYSTEM
if ( isArchive )
{
if ( m_data[i].File.StartsWith(fnd) )
unloaded = true;
// don't break from the loop, we can have other matching files
}
else // a single resource URL
#endif // wxUSE_FILESYSTEM
{
if ( m_data[i].File == fnd )
{
m_data.RemoveAt(i);
unloaded = true;
// no sense in continuing, there is only one file with this URL
break;
}
}
}
return unloaded;
}
IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject) IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject)