]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/image.cpp
Add missing WXK constants for the control keys
[wxWidgets.git] / src / common / image.cpp
index 62597542fd95dfaf2c759b93a8664c498486ccfd..2c61998e6d3c226b9822dcd4750d60a75f2b61a9 100644 (file)
@@ -2235,10 +2235,105 @@ bool wxImage::HasOption(const wxString& name) const
 // image I/O
 // ----------------------------------------------------------------------------
 
 // image I/O
 // ----------------------------------------------------------------------------
 
-bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
-                        wxBitmapType WXUNUSED_UNLESS_STREAMS(type),
+// Under Windows we can load wxImage not only from files but also from
+// resources.
+#if defined(__WINDOWS__) && wxUSE_WXDIB && wxUSE_IMAGE
+    #define HAS_LOAD_FROM_RESOURCE
+#endif
+
+#ifdef HAS_LOAD_FROM_RESOURCE
+
+#include "wx/msw/dib.h"
+#include "wx/msw/private.h"
+
+static wxImage LoadImageFromResource(const wxString &name, wxBitmapType type)
+{
+    AutoHBITMAP
+        hBitmap,
+        hMask;
+
+    if ( type == wxBITMAP_TYPE_BMP_RESOURCE )
+    {
+        hBitmap.Init( ::LoadBitmap(wxGetInstance(), name.t_str()) );
+
+        if ( !hBitmap )
+        {
+            wxLogError(_("Failed to load bitmap \"%s\" from resources."), name);
+        }
+    }
+    else if ( type == wxBITMAP_TYPE_ICO_RESOURCE )
+    {
+        const HICON hIcon = ::LoadIcon(wxGetInstance(), name.t_str());
+
+        if ( !hIcon )
+        {
+            wxLogError(_("Failed to load icon \"%s\" from resources."), name);
+        }
+        else
+        {
+            ICONINFO info;
+            if ( !::GetIconInfo(hIcon, &info) )
+            {
+                wxLogLastError(wxT("GetIconInfo"));
+                return wxImage();
+            }
+
+            hBitmap.Init(info.hbmColor);
+            hMask.Init(info.hbmMask);
+        }
+    }
+    else if ( type == wxBITMAP_TYPE_CUR_RESOURCE )
+    {
+        wxLogDebug(wxS("Loading cursors from resources is not implemented."));
+    }
+    else
+    {
+        wxFAIL_MSG(wxS("Invalid bitmap resource type."));
+    }
+
+    if ( !hBitmap )
+        return wxImage();
+
+    wxImage image = wxDIB(hBitmap).ConvertToImage();
+    if ( hMask )
+    {
+        const wxImage mask = wxDIB(hMask).ConvertToImage();
+        image.SetMaskFromImage(mask, 255, 255, 255);
+    }
+    else
+    {
+        // Light gray colour is a default mask
+        image.SetMaskColour(0xc0, 0xc0, 0xc0);
+    }
+
+    // We could have already loaded alpha from the resources, but if not,
+    // initialize it now using the mask.
+    if ( !image.HasAlpha() )
+        image.InitAlpha();
+
+    return image;
+}
+
+#endif // HAS_LOAD_FROM_RESOURCE
+
+bool wxImage::LoadFile( const wxString& filename,
+                        wxBitmapType type,
                         int WXUNUSED_UNLESS_STREAMS(index) )
 {
                         int WXUNUSED_UNLESS_STREAMS(index) )
 {
+#ifdef HAS_LOAD_FROM_RESOURCE
+    if (   type == wxBITMAP_TYPE_BMP_RESOURCE
+        || type == wxBITMAP_TYPE_ICO_RESOURCE
+        || type == wxBITMAP_TYPE_CUR_RESOURCE)
+    {
+        const wxImage image = ::LoadImageFromResource(filename, type);
+        if ( image.IsOk() )
+        {
+            *this = image;
+            return true;
+        }
+    }
+#endif // HAS_LOAD_FROM_RESOURCE
+
 #if HAS_FILE_STREAMS
     wxImageFileInputStream stream(filename);
     if ( stream.IsOk() )
 #if HAS_FILE_STREAMS
     wxImageFileInputStream stream(filename);
     if ( stream.IsOk() )
@@ -2576,6 +2671,7 @@ bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const
     if ( !handler )
     {
         wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
     if ( !handler )
     {
         wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
+        return false;
     }
 
     return DoSave(*handler, stream);
     }
 
     return DoSave(*handler, stream);