]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxLoadUserResource() overload not copying the resource data.
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 26 Apr 2010 16:53:21 +0000 (16:53 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 26 Apr 2010 16:53:21 +0000 (16:53 +0000)
The existing wxLoadUserResource() copies the resource data which is often
unnecessary. Add another overload which just returns the pointer directly to
the resource data.

Also move the function into base from core as it can be useful for the console
applications as well.

Finally, define wxUserResourceStr used by this function only in the same file
where the function itself is defined instead of datacmn.cpp.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64150 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/utils.h
src/common/datacmn.cpp
src/msw/utils.cpp
src/msw/utilsgui.cpp

index 38c59e5f6a74e826374c96200037e32ae0dfa8eb..61520916af70995d933743779c5c2f015296062e 100644 (file)
@@ -676,10 +676,31 @@ public:
 void WXDLLIMPEXP_CORE wxGetMousePosition( int* x, int* y );
 
 // MSW only: get user-defined resource from the .res file.
-// Returns NULL or newly-allocated memory, so use delete[] to clean up.
 #ifdef __WXMSW__
-    extern WXDLLIMPEXP_CORE const wxChar* wxUserResourceStr;
-    WXDLLIMPEXP_CORE char* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr, int* pLen = NULL);
+    // default resource type for wxLoadUserResource()
+    extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxUserResourceStr;
+
+    // Return the pointer to the resource data. This pointer is read-only, use
+    // the overload below if you need to modify the data.
+    //
+    // Returns true on success, false on failure. Doesn't log an error message
+    // if the resource is not found (because this could be expected) but does
+    // log one if any other error occurs.
+    WXDLLIMPEXP_BASE bool
+    wxLoadUserResource(const void **outData,
+                       size_t *outLen,
+                       const wxString& resourceName,
+                       const wxString& resourceType = wxUserResourceStr);
+
+    // This function allocates a new buffer and makes a copy of the resource
+    // data, remember to delete[] the buffer. And avoid using it entirely if
+    // the overload above can be used.
+    //
+    // Returns NULL on failure.
+    WXDLLIMPEXP_BASE char*
+    wxLoadUserResource(const wxString& resourceName,
+                       const wxString& resourceType = wxUserResourceStr,
+                       int* pLen = NULL);
 #endif // MSW
 
 // ----------------------------------------------------------------------------
index 10baf573200099561343d4b00c768e8c91435a23..bb6a533fbe3ebdd2902d5c5ceec542754e826802 100644 (file)
@@ -78,6 +78,3 @@ extern WXDLLEXPORT_DATA(const char) wxDirDialogNameStr[] = "wxDirCtrl";
 extern WXDLLEXPORT_DATA(const char) wxDirDialogDefaultFolderStr[] = "/";
 
 extern WXDLLEXPORT_DATA(const char) wxFileDialogNameStr[] = "filedlg";
-#if defined(__WXMSW__) || defined(__OS2__)
-WXDLLEXPORT_DATA(const wxChar *) wxUserResourceStr = wxT("TEXT");
-#endif
index fd36d634b193ebfe00c10280fb17f66e97cd5c79..2958f75788cecb009c0ac87bebec596c1325d224 100644 (file)
@@ -118,6 +118,8 @@ static const wxChar WX_SECTION[] = wxT("wxWindows");
 static const wxChar eUSERNAME[]  = wxT("UserName");
 #endif
 
+WXDLLIMPEXP_DATA_BASE(const wxChar *) wxUserResourceStr = wxT("TEXT");
+
 // ============================================================================
 // implementation
 // ============================================================================
@@ -1075,6 +1077,67 @@ bool wxIsDebuggerRunning()
 #endif
 }
 
+// ----------------------------------------------------------------------------
+// working with MSW resources
+// ----------------------------------------------------------------------------
+
+bool
+wxLoadUserResource(const void **outData,
+                   size_t *outLen,
+                   const wxString& resourceName,
+                   const wxString& resourceType)
+{
+    wxCHECK_MSG( outData && outLen, false, "output pointers can't be NULL" );
+
+    HRSRC hResource = ::FindResource(wxGetInstance(),
+                                     resourceName.wx_str(),
+                                     resourceType.wx_str());
+    if ( !hResource )
+        return false;
+
+    HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
+    if ( !hData )
+    {
+        wxLogSysError(_("Failed to load resource \"%s\"."), resourceName);
+        return false;
+    }
+
+    *outData = ::LockResource(hData);
+    if ( !*outData )
+    {
+        wxLogSysError(_("Failed to lock resource \"%s\"."), resourceName);
+        return false;
+    }
+
+    *outLen = ::SizeofResource(wxGetInstance(), hResource);
+
+    // Notice that we do not need to call neither UnlockResource() (which is
+    // obsolete in Win32) nor GlobalFree() (resources are freed on process
+    // termination only)
+
+    return true;
+}
+
+char *
+wxLoadUserResource(const wxString& resourceName,
+                   const wxString& resourceType,
+                   int* pLen)
+{
+    const void *data;
+    size_t len;
+    if ( !wxLoadUserResource(&data, &len, resourceName, resourceType) )
+        return NULL;
+
+    char *s = new char[len + 1];
+    memcpy(s, data, len);
+    s[len] = '\0'; // NUL-terminate in case the resource itself wasn't
+
+    if (pLen)
+      *pLen = len;
+
+    return s;
+}
+
 // ----------------------------------------------------------------------------
 // OS version
 // ----------------------------------------------------------------------------
index 68ca45db20cafbe0293087787bc83e2fe4c5238d..f85d206b05c1381dd947e82188774e697e322726 100644 (file)
@@ -104,48 +104,6 @@ bool wxCheckForInterrupt(wxWindow *wnd)
     return true;
 }
 
-// MSW only: get user-defined resource from the .res file.
-// Returns NULL or newly-allocated memory, so use delete[] to clean up.
-
-#ifndef __WXMICROWIN__
-char *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType, int* pLen /* = NULL */)
-{
-    HRSRC hResource = ::FindResource(wxGetInstance(),
-                                     resourceName.wx_str(),
-                                     resourceType.wx_str());
-    if ( hResource == 0 )
-        return NULL;
-
-    HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
-    if ( hData == 0 )
-        return NULL;
-
-    void *theText = ::LockResource(hData);
-    if ( !theText )
-        return NULL;
-
-    // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
-    // so we need to find the length of the resource.
-    int len = ::SizeofResource(wxGetInstance(), hResource);
-    char *s = new char[len + 1];
-    memcpy(s, theText, len);
-    s[len] = '\0'; // NUL-terminate in case the resource itself wasn't
-
-    // Obsolete in WIN32
-#ifndef __WIN32__
-    UnlockResource(hData);
-#endif
-
-    // No need??
-    //  GlobalFree(hData);
-
-    if (pLen)
-      *pLen = len;
-
-    return s;
-}
-#endif // __WXMICROWIN__
-
 // ----------------------------------------------------------------------------
 // get display info
 // ----------------------------------------------------------------------------