added singleton interface to wxXmlResource, replaces wxTheXmlResource

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13618 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2002-01-17 23:41:53 +00:00
parent d9ade1df2f
commit 824e8eaacc
8 changed files with 82 additions and 36 deletions

View File

@@ -101,7 +101,7 @@ public:
wxXmlResource(int flags = wxXRC_USE_LOCALE); wxXmlResource(int flags = wxXRC_USE_LOCALE);
wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE); wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
~wxXmlResource(); ~wxXmlResource();
// 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).
bool Load(const wxString& filemask); bool Load(const wxString& filemask);
@@ -172,6 +172,13 @@ public:
int CompareVersion(int major, int minor, int release, int revision) const int CompareVersion(int major, int minor, int release, int revision) const
{ return GetVersion() - { return GetVersion() -
(major*256*256*256 + minor*256*256 + release*256 + revision); } (major*256*256*256 + minor*256*256 + release*256 + revision); }
// Singleton accessors:
// Gets global resources object or create one if none exists
static wxXmlResource *Get();
// Sets global resources object and returns pointer to previous one (may be NULL).
static wxXmlResource *Set(wxXmlResource *res);
protected: protected:
// Scans resources list for unloaded files and loads them. Also reloads // Scans resources list for unloaded files and loads them. Also reloads
@@ -199,11 +206,14 @@ private:
#endif #endif
friend class wxXmlResourceHandler; friend class wxXmlResourceHandler;
// singleton instance:
static wxXmlResource *ms_instance;
}; };
// Global instance of resource class. For your convenience. // This is here only for backward compatibility. Do NOT use!!
extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource; #define wxTheXmlResource wxXmlResource::Get()
// This macro translates string identifier (as used in XML resource, // This macro translates string identifier (as used in XML resource,
// e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by

View File

@@ -111,8 +111,8 @@ IMPLEMENT_APP(MyApp)
bool MyApp::OnInit() bool MyApp::OnInit()
{ {
wxImage::AddHandler(new wxGIFHandler); wxImage::AddHandler(new wxGIFHandler);
wxTheXmlResource->InitAllHandlers(); wxXmlResource::Get()->InitAllHandlers();
wxTheXmlResource->Load("rc/resource.xrc"); wxXmlResource::Get()->Load("rc/resource.xrc");
MyFrame *frame = new MyFrame("XML resources demo", MyFrame *frame = new MyFrame("XML resources demo",
wxPoint(50, 50), wxSize(450, 340)); wxPoint(50, 50), wxSize(450, 340));
@@ -130,8 +130,8 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
{ {
SetIcon(wxICON(appicon)); SetIcon(wxICON(appicon));
SetMenuBar(wxTheXmlResource->LoadMenuBar("mainmenu")); SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
SetToolBar(wxTheXmlResource->LoadToolBar(this, "toolbar")); SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
} }
@@ -155,7 +155,7 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
{ {
wxDialog dlg; wxDialog dlg;
wxTheXmlResource->LoadDialog(&dlg, this, "dlg1"); wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
dlg.ShowModal(); dlg.ShowModal();
} }
@@ -163,6 +163,6 @@ void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
{ {
wxDialog dlg; wxDialog dlg;
wxTheXmlResource->LoadDialog(&dlg, this, "dlg2"); wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
dlg.ShowModal(); dlg.ShowModal();
} }

View File

@@ -40,6 +40,22 @@
WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords); WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords);
wxXmlResource *wxXmlResource::ms_instance = NULL;
/*static*/ wxXmlResource *wxXmlResource::Get()
{
if ( !ms_instance )
ms_instance = new wxXmlResource;
return ms_instance;
}
/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
{
wxXmlResource *old = ms_instance;
ms_instance = res;
return old;
}
wxXmlResource::wxXmlResource(int flags) wxXmlResource::wxXmlResource(int flags)
{ {
m_handlers.DeleteContents(TRUE); m_handlers.DeleteContents(TRUE);
@@ -1085,21 +1101,18 @@ static void CleanXMLID_Records()
// --------------- module and globals ----------------------------- // --------------- module and globals -----------------------------
static wxXmlResource gs_XmlResource;
wxXmlResource *wxTheXmlResource = &gs_XmlResource;
class wxXmlResourceModule: public wxModule class wxXmlResourceModule: public wxModule
{ {
DECLARE_DYNAMIC_CLASS(wxXmlResourceModule) DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
public: public:
wxXmlResourceModule() {} wxXmlResourceModule() {}
bool OnInit() {return TRUE;} bool OnInit()
{
return TRUE;
}
void OnExit() void OnExit()
{ {
wxTheXmlResource->ClearHandlers(); delete wxXmlResource::Get();
CleanXMLID_Records(); CleanXMLID_Records();
} }
}; };

View File

@@ -392,7 +392,7 @@ void " + parFuncname + "()\n\
{ {
wxString name, ext, path; wxString name, ext, path;
wxSplitPath(parFiles[i], &path, &name, &ext); wxSplitPath(parFiles[i], &path, &name, &ext);
file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" + file.Write(" wxXmlResource::Get()->Load(\"memory:xml_resource/" +
name + ".xrc" + "\");\n"); name + ".xrc" + "\");\n");
} }

View File

@@ -101,7 +101,7 @@ public:
wxXmlResource(int flags = wxXRC_USE_LOCALE); wxXmlResource(int flags = wxXRC_USE_LOCALE);
wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE); wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
~wxXmlResource(); ~wxXmlResource();
// 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).
bool Load(const wxString& filemask); bool Load(const wxString& filemask);
@@ -172,6 +172,13 @@ public:
int CompareVersion(int major, int minor, int release, int revision) const int CompareVersion(int major, int minor, int release, int revision) const
{ return GetVersion() - { return GetVersion() -
(major*256*256*256 + minor*256*256 + release*256 + revision); } (major*256*256*256 + minor*256*256 + release*256 + revision); }
// Singleton accessors:
// Gets global resources object or create one if none exists
static wxXmlResource *Get();
// Sets global resources object and returns pointer to previous one (may be NULL).
static wxXmlResource *Set(wxXmlResource *res);
protected: protected:
// Scans resources list for unloaded files and loads them. Also reloads // Scans resources list for unloaded files and loads them. Also reloads
@@ -199,11 +206,14 @@ private:
#endif #endif
friend class wxXmlResourceHandler; friend class wxXmlResourceHandler;
// singleton instance:
static wxXmlResource *ms_instance;
}; };
// Global instance of resource class. For your convenience. // This is here only for backward compatibility. Do NOT use!!
extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource; #define wxTheXmlResource wxXmlResource::Get()
// This macro translates string identifier (as used in XML resource, // This macro translates string identifier (as used in XML resource,
// e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by

View File

@@ -111,8 +111,8 @@ IMPLEMENT_APP(MyApp)
bool MyApp::OnInit() bool MyApp::OnInit()
{ {
wxImage::AddHandler(new wxGIFHandler); wxImage::AddHandler(new wxGIFHandler);
wxTheXmlResource->InitAllHandlers(); wxXmlResource::Get()->InitAllHandlers();
wxTheXmlResource->Load("rc/resource.xrc"); wxXmlResource::Get()->Load("rc/resource.xrc");
MyFrame *frame = new MyFrame("XML resources demo", MyFrame *frame = new MyFrame("XML resources demo",
wxPoint(50, 50), wxSize(450, 340)); wxPoint(50, 50), wxSize(450, 340));
@@ -130,8 +130,8 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
{ {
SetIcon(wxICON(appicon)); SetIcon(wxICON(appicon));
SetMenuBar(wxTheXmlResource->LoadMenuBar("mainmenu")); SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
SetToolBar(wxTheXmlResource->LoadToolBar(this, "toolbar")); SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
} }
@@ -155,7 +155,7 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
{ {
wxDialog dlg; wxDialog dlg;
wxTheXmlResource->LoadDialog(&dlg, this, "dlg1"); wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
dlg.ShowModal(); dlg.ShowModal();
} }
@@ -163,6 +163,6 @@ void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
{ {
wxDialog dlg; wxDialog dlg;
wxTheXmlResource->LoadDialog(&dlg, this, "dlg2"); wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
dlg.ShowModal(); dlg.ShowModal();
} }

View File

@@ -40,6 +40,22 @@
WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords); WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords);
wxXmlResource *wxXmlResource::ms_instance = NULL;
/*static*/ wxXmlResource *wxXmlResource::Get()
{
if ( !ms_instance )
ms_instance = new wxXmlResource;
return ms_instance;
}
/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
{
wxXmlResource *old = ms_instance;
ms_instance = res;
return old;
}
wxXmlResource::wxXmlResource(int flags) wxXmlResource::wxXmlResource(int flags)
{ {
m_handlers.DeleteContents(TRUE); m_handlers.DeleteContents(TRUE);
@@ -1085,21 +1101,18 @@ static void CleanXMLID_Records()
// --------------- module and globals ----------------------------- // --------------- module and globals -----------------------------
static wxXmlResource gs_XmlResource;
wxXmlResource *wxTheXmlResource = &gs_XmlResource;
class wxXmlResourceModule: public wxModule class wxXmlResourceModule: public wxModule
{ {
DECLARE_DYNAMIC_CLASS(wxXmlResourceModule) DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
public: public:
wxXmlResourceModule() {} wxXmlResourceModule() {}
bool OnInit() {return TRUE;} bool OnInit()
{
return TRUE;
}
void OnExit() void OnExit()
{ {
wxTheXmlResource->ClearHandlers(); delete wxXmlResource::Get();
CleanXMLID_Records(); CleanXMLID_Records();
} }
}; };

View File

@@ -392,7 +392,7 @@ void " + parFuncname + "()\n\
{ {
wxString name, ext, path; wxString name, ext, path;
wxSplitPath(parFiles[i], &path, &name, &ext); wxSplitPath(parFiles[i], &path, &name, &ext);
file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" + file.Write(" wxXmlResource::Get()->Load(\"memory:xml_resource/" +
name + ".xrc" + "\");\n"); name + ".xrc" + "\");\n");
} }