]> git.saurik.com Git - wxWidgets.git/blobdiff - contrib/src/xrc/xmlres.cpp
forgot to commit project file for CW before
[wxWidgets.git] / contrib / src / xrc / xmlres.cpp
index e47ea3e254f621eae657ffe8fadd6414db64fd41..c29959f11905302f1fa82769666c9faed9711ef2 100644 (file)
@@ -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,11 +100,29 @@ bool wxXmlResource::Load(const wxString& filemask)
         fnd = filemask;
     while (!!fnd)
     {
+        // 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))
+        {
+            // 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
-        if (filemask.Lower().Matches(wxT("*.zip")) ||
-            filemask.Lower().Matches(wxT("*.xrs")))
+        if (fnd.Lower().Matches(wxT("*.zip")) ||
+            fnd.Lower().Matches(wxT("*.xrs")))
         {
-            rt = rt && Load(fnd + wxT("#zip:*.xrc"));
+            wxString url(wxFileSystem::FileNameToURL(fnd));
+            rt = rt && Load(url + wxT("#zip:*.xrc"));
         }
         else
 #endif
@@ -264,19 +283,21 @@ static void ProcessPlatformProperty(wxXmlNode *node)
             while (tkn.HasMoreTokens())
             {
                 s = tkn.GetNextToken();
-                if (
 #ifdef __WXMSW__
-                    s == wxString(wxT("win"))
-#elif defined(__UNIX__)
-                    s == wxString(wxT("unix"))
-#elif defined(__MAC__)
-                    s == wxString(wxT("mac"))
-#elif defined(__OS2__)
-                    s == wxString(wxT("os2"))
-#else
-                    FALSE
+                if (s == wxT("win")) isok = true;
 #endif
-              ) isok = TRUE;
+#ifdef __UNIX__
+                if (s == wxT("unix")) isok = true;
+#endif
+#ifdef __MAC__
+                if (s == wxT("mac")) isok = true;
+#endif
+#ifdef __OS2__
+                if (s == wxT("os2")) isok = true;
+#endif
+
+                if (isok)
+                    break;
             }
         }
 
@@ -516,7 +537,9 @@ 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,
+                                           wxXmlResourceHandler *handlerToUse)
 {
     if (node == NULL) return NULL;
 
@@ -540,17 +563,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'!"),
@@ -766,28 +798,7 @@ long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
 
 int wxXmlResourceHandler::GetID()
 {
-    wxString sid = GetName();
-    long num;
-
-    if (sid == wxT("-1")) return -1;
-    else if (sid.IsNumber() && sid.ToLong(&num)) return num;
-#define stdID(id) else if (sid == wxT(#id)) return id
-    stdID(wxID_OPEN); stdID(wxID_CLOSE); stdID(wxID_NEW);
-    stdID(wxID_SAVE); stdID(wxID_SAVEAS); stdID(wxID_REVERT);
-    stdID(wxID_EXIT); stdID(wxID_UNDO); stdID(wxID_REDO);
-    stdID(wxID_HELP); stdID(wxID_PRINT); stdID(wxID_PRINT_SETUP);
-    stdID(wxID_PREVIEW); stdID(wxID_ABOUT); stdID(wxID_HELP_CONTENTS);
-    stdID(wxID_HELP_COMMANDS); stdID(wxID_HELP_PROCEDURES);
-    stdID(wxID_CUT); stdID(wxID_COPY); stdID(wxID_PASTE);
-    stdID(wxID_CLEAR); stdID(wxID_FIND); stdID(wxID_DUPLICATE);
-    stdID(wxID_SELECTALL); stdID(wxID_OK); stdID(wxID_CANCEL);
-    stdID(wxID_APPLY); stdID(wxID_YES); stdID(wxID_NO);
-    stdID(wxID_STATIC); stdID(wxID_FORWARD); stdID(wxID_BACKWARD);
-    stdID(wxID_DEFAULT); stdID(wxID_MORE); stdID(wxID_SETUP);
-    stdID(wxID_RESET); stdID(wxID_HELP_CONTEXT);
-    stdID(wxID_CLOSE_ALL);
-#undef stdID
-    else return wxXmlResource::GetXRCID(sid);
+    return wxXmlResource::GetXRCID(GetName());
 }
 
 
@@ -857,7 +868,8 @@ wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
     wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
     if (fsfile == NULL)
     {
-        wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param.c_str());
+        wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
+                   name.c_str());
         return wxNullBitmap;
     }
     wxImage img(*(fsfile->GetStream()));
@@ -882,15 +894,8 @@ wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
                                      const wxArtClient& defaultArtClient,
                                      wxSize size)
 {
-#if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
     wxIcon icon;
     icon.CopyFromBitmap(GetBitmap(param, defaultArtClient, size));
-#else
-    wxIcon *iconpt;
-    wxBitmap bmppt = GetBitmap(param, size);
-    iconpt = (wxIcon*)(&bmppt);
-    wxIcon icon(*iconpt);
-#endif
     return icon;
 }
 
@@ -1111,10 +1116,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->CreateResFromNode(n, parent, NULL,
+                                          this_hnd_only ? this : NULL);
         }
         n = n->GetNext();
     }
@@ -1157,7 +1160,7 @@ struct XRCID_record
 
 static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
 
-/*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
+static int XRCID_Lookup(const wxChar *str_id, int value_if_not_found = -2)
 {
     static int XRCID_LastID = wxID_HIGHEST;
 
@@ -1185,20 +1188,30 @@ static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
     (*rec_var)->next = NULL;
 
     wxChar *end;
-    int asint = wxStrtol(str_id, &end, 10);
-    if (*str_id && *end == 0)
-    {
-        // if str_id was integer, keep it verbosely:
-        (*rec_var)->id = asint;
-    }
+    if (value_if_not_found != -2)
+        (*rec_var)->id = value_if_not_found;
     else
     {
-        (*rec_var)->id = ++XRCID_LastID;
+        int asint = wxStrtol(str_id, &end, 10);
+        if (*str_id && *end == 0)
+        {
+            // if str_id was integer, keep it verbosely:
+            (*rec_var)->id = asint;
+        }
+        else
+        {
+            (*rec_var)->id = ++XRCID_LastID;
+        }
     }
 
     return (*rec_var)->id;
 }
 
+/*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
+{
+    return XRCID_Lookup(str_id);
+}
+
 
 static void CleanXRCID_Record(XRCID_record *rec)
 {
@@ -1213,11 +1226,32 @@ static void CleanXRCID_Record(XRCID_record *rec)
 static void CleanXRCID_Records()
 {
     for (int i = 0; i < XRCID_TABLE_SIZE; i++)
+    {
         CleanXRCID_Record(XRCID_Records[i]);
+        XRCID_Records[i] = NULL;
+    }
 }
 
-
-
+static void AddStdXRCID_Records()
+{
+#define stdID(id) XRCID_Lookup(wxT(#id), id)
+    stdID(-1);
+    stdID(wxID_OPEN); stdID(wxID_CLOSE); stdID(wxID_NEW);
+    stdID(wxID_SAVE); stdID(wxID_SAVEAS); stdID(wxID_REVERT);
+    stdID(wxID_EXIT); stdID(wxID_UNDO); stdID(wxID_REDO);
+    stdID(wxID_HELP); stdID(wxID_PRINT); stdID(wxID_PRINT_SETUP);
+    stdID(wxID_PREVIEW); stdID(wxID_ABOUT); stdID(wxID_HELP_CONTENTS);
+    stdID(wxID_HELP_COMMANDS); stdID(wxID_HELP_PROCEDURES);
+    stdID(wxID_CUT); stdID(wxID_COPY); stdID(wxID_PASTE);
+    stdID(wxID_CLEAR); stdID(wxID_FIND); stdID(wxID_DUPLICATE);
+    stdID(wxID_SELECTALL); stdID(wxID_OK); stdID(wxID_CANCEL);
+    stdID(wxID_APPLY); stdID(wxID_YES); stdID(wxID_NO);
+    stdID(wxID_STATIC); stdID(wxID_FORWARD); stdID(wxID_BACKWARD);
+    stdID(wxID_DEFAULT); stdID(wxID_MORE); stdID(wxID_SETUP);
+    stdID(wxID_RESET); stdID(wxID_HELP_CONTEXT);
+    stdID(wxID_CLOSE_ALL);
+#undef stdID
+}
 
 
 
@@ -1232,6 +1266,7 @@ public:
     wxXmlResourceModule() {}
     bool OnInit()
     {
+        AddStdXRCID_Records();
         wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
         return TRUE;
     }
@@ -1254,5 +1289,3 @@ void wxXmlInitResourceModule()
     module->Init();
     wxModule::RegisterModule(module);
 }
-
-