]> git.saurik.com Git - wxWidgets.git/commitdiff
added singleton interface to wxXmlResource, replaces wxTheXmlResource
authorVáclav Slavík <vslavik@fastmail.fm>
Thu, 17 Jan 2002 23:41:53 +0000 (23:41 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Thu, 17 Jan 2002 23:41:53 +0000 (23:41 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13618 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

contrib/include/wx/xrc/xmlres.h
contrib/samples/xrc/xrcdemo.cpp
contrib/src/xrc/xmlres.cpp
contrib/utils/wxrc/wxrc.cpp
include/wx/xrc/xmlres.h
samples/xrc/xrcdemo.cpp
src/xrc/xmlres.cpp
utils/wxrc/wxrc.cpp

index 01f70f294b2a77e31d84d01983767ee7a3ee218b..8566b230f79e4c8b0a0518fe9d3020e63c5bc3d8 100644 (file)
@@ -101,7 +101,7 @@ public:
     wxXmlResource(int flags = wxXRC_USE_LOCALE);
     wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
     ~wxXmlResource();
-
+    
     // Loads resources from XML files that match given filemask.
     // This method understands VFS (see filesys.h).
     bool Load(const wxString& filemask);
@@ -172,6 +172,13 @@ public:
     int CompareVersion(int major, int minor, int release, int revision) const
         { return GetVersion() -
                  (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:
     // Scans resources list for unloaded files and loads them. Also reloads
@@ -199,11 +206,14 @@ private:
 #endif
 
     friend class wxXmlResourceHandler;
+    
+    // singleton instance:
+    static wxXmlResource *ms_instance;
 };
 
 
-// Global instance of resource class. For your convenience.
-extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource;
+// This is here only for backward compatibility. Do NOT use!!
+#define wxTheXmlResource  wxXmlResource::Get()
 
 // This macro translates string identifier (as used in XML resource,
 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
index be7bb1b41c48a798ee0c8c67beebb4ddb54b63df..91cc6b70a8602a668ec73ebd77190ff7511add28 100644 (file)
@@ -111,8 +111,8 @@ IMPLEMENT_APP(MyApp)
 bool MyApp::OnInit()
 {
     wxImage::AddHandler(new wxGIFHandler);
-    wxTheXmlResource->InitAllHandlers();
-    wxTheXmlResource->Load("rc/resource.xrc");
+    wxXmlResource::Get()->InitAllHandlers();
+    wxXmlResource::Get()->Load("rc/resource.xrc");
 
     MyFrame *frame = new MyFrame("XML resources demo",
                                  wxPoint(50, 50), wxSize(450, 340));
@@ -130,8 +130,8 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
 {
     SetIcon(wxICON(appicon));
 
-    SetMenuBar(wxTheXmlResource->LoadMenuBar("mainmenu"));
-    SetToolBar(wxTheXmlResource->LoadToolBar(this, "toolbar"));
+    SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
+    SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
 }
 
 
@@ -155,7 +155,7 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
 {
     wxDialog dlg;
-    wxTheXmlResource->LoadDialog(&dlg, this, "dlg1");
+    wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
     dlg.ShowModal();
 }
 
@@ -163,6 +163,6 @@ void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
 {
     wxDialog dlg;
-    wxTheXmlResource->LoadDialog(&dlg, this, "dlg2");
+    wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
     dlg.ShowModal();
 }
index a802efe57c67771eeb428a4b55402200bf8e4f04..649a55320da7f7c0da729ae7f707fb3cb1a3aa12 100644 (file)
 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)
 {
     m_handlers.DeleteContents(TRUE);
@@ -1085,21 +1101,18 @@ static void CleanXMLID_Records()
 
 // --------------- module and globals -----------------------------
 
-
-static wxXmlResource gs_XmlResource;
-
-wxXmlResource *wxTheXmlResource = &gs_XmlResource;
-
-
 class wxXmlResourceModule: public wxModule
 {
 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
 public:
     wxXmlResourceModule() {}
-    bool OnInit() {return TRUE;}
+    bool OnInit()
+    {
+        return TRUE;
+    }
     void OnExit()
     {
-        wxTheXmlResource->ClearHandlers();
+        delete wxXmlResource::Get();
         CleanXMLID_Records();
     }
 };
index 5741a87450cd0a8a039a2082862bbb5a10285e0c..9acaeef5e462b1cce142a8a9336f6543dd5116b2 100644 (file)
@@ -392,7 +392,7 @@ void " + parFuncname + "()\n\
     {
         wxString name, ext, path;
         wxSplitPath(parFiles[i], &path, &name, &ext);
-        file.Write("    wxTheXmlResource->Load(\"memory:xml_resource/" + 
+        file.Write("    wxXmlResource::Get()->Load(\"memory:xml_resource/" + 
                    name + ".xrc" + "\");\n");
     }
     
index 01f70f294b2a77e31d84d01983767ee7a3ee218b..8566b230f79e4c8b0a0518fe9d3020e63c5bc3d8 100644 (file)
@@ -101,7 +101,7 @@ public:
     wxXmlResource(int flags = wxXRC_USE_LOCALE);
     wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
     ~wxXmlResource();
-
+    
     // Loads resources from XML files that match given filemask.
     // This method understands VFS (see filesys.h).
     bool Load(const wxString& filemask);
@@ -172,6 +172,13 @@ public:
     int CompareVersion(int major, int minor, int release, int revision) const
         { return GetVersion() -
                  (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:
     // Scans resources list for unloaded files and loads them. Also reloads
@@ -199,11 +206,14 @@ private:
 #endif
 
     friend class wxXmlResourceHandler;
+    
+    // singleton instance:
+    static wxXmlResource *ms_instance;
 };
 
 
-// Global instance of resource class. For your convenience.
-extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource;
+// This is here only for backward compatibility. Do NOT use!!
+#define wxTheXmlResource  wxXmlResource::Get()
 
 // This macro translates string identifier (as used in XML resource,
 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
index be7bb1b41c48a798ee0c8c67beebb4ddb54b63df..91cc6b70a8602a668ec73ebd77190ff7511add28 100644 (file)
@@ -111,8 +111,8 @@ IMPLEMENT_APP(MyApp)
 bool MyApp::OnInit()
 {
     wxImage::AddHandler(new wxGIFHandler);
-    wxTheXmlResource->InitAllHandlers();
-    wxTheXmlResource->Load("rc/resource.xrc");
+    wxXmlResource::Get()->InitAllHandlers();
+    wxXmlResource::Get()->Load("rc/resource.xrc");
 
     MyFrame *frame = new MyFrame("XML resources demo",
                                  wxPoint(50, 50), wxSize(450, 340));
@@ -130,8 +130,8 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
 {
     SetIcon(wxICON(appicon));
 
-    SetMenuBar(wxTheXmlResource->LoadMenuBar("mainmenu"));
-    SetToolBar(wxTheXmlResource->LoadToolBar(this, "toolbar"));
+    SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
+    SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
 }
 
 
@@ -155,7 +155,7 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
 void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
 {
     wxDialog dlg;
-    wxTheXmlResource->LoadDialog(&dlg, this, "dlg1");
+    wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
     dlg.ShowModal();
 }
 
@@ -163,6 +163,6 @@ void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
 void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
 {
     wxDialog dlg;
-    wxTheXmlResource->LoadDialog(&dlg, this, "dlg2");
+    wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
     dlg.ShowModal();
 }
index a802efe57c67771eeb428a4b55402200bf8e4f04..649a55320da7f7c0da729ae7f707fb3cb1a3aa12 100644 (file)
 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)
 {
     m_handlers.DeleteContents(TRUE);
@@ -1085,21 +1101,18 @@ static void CleanXMLID_Records()
 
 // --------------- module and globals -----------------------------
 
-
-static wxXmlResource gs_XmlResource;
-
-wxXmlResource *wxTheXmlResource = &gs_XmlResource;
-
-
 class wxXmlResourceModule: public wxModule
 {
 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
 public:
     wxXmlResourceModule() {}
-    bool OnInit() {return TRUE;}
+    bool OnInit()
+    {
+        return TRUE;
+    }
     void OnExit()
     {
-        wxTheXmlResource->ClearHandlers();
+        delete wxXmlResource::Get();
         CleanXMLID_Records();
     }
 };
index 5741a87450cd0a8a039a2082862bbb5a10285e0c..9acaeef5e462b1cce142a8a9336f6543dd5116b2 100644 (file)
@@ -392,7 +392,7 @@ void " + parFuncname + "()\n\
     {
         wxString name, ext, path;
         wxSplitPath(parFiles[i], &path, &name, &ext);
-        file.Write("    wxTheXmlResource->Load(\"memory:xml_resource/" + 
+        file.Write("    wxXmlResource::Get()->Load(\"memory:xml_resource/" + 
                    name + ".xrc" + "\");\n");
     }