use wxBitmapToHICON/CURSOR()

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@19721 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2003-03-23 01:48:15 +00:00
parent c11bf84234
commit 7fd328a34d
2 changed files with 29 additions and 187 deletions

View File

@ -29,7 +29,6 @@
#endif
#ifndef WX_PRECOMP
#include "wx/list.h"
#include "wx/utils.h"
#include "wx/app.h"
#include "wx/bitmap.h"
@ -38,21 +37,10 @@
#include "wx/settings.h"
#endif
#include "wx/ptr_scpd.h"
#include "wx/module.h"
#include "wx/image.h"
#include "wx/msw/private.h"
#ifndef __WXMICROWIN__
#include "wx/msw/dib.h"
#endif
#if wxUSE_RESOURCE_LOADING_IN_MSW
#include "wx/msw/curico.h"
#include "wx/msw/curicop.h"
#endif
// define functions missing in MicroWin
#ifdef __WXMICROWIN__
static inline void DestroyCursor(HCURSOR) { }
@ -123,13 +111,6 @@ public:
}
};
// ----------------------------------------------------------------------------
// other types
// ----------------------------------------------------------------------------
wxDECLARE_SCOPED_ARRAY(unsigned char, ByteArray);
wxDEFINE_SCOPED_ARRAY(unsigned char, ByteArray);
// ============================================================================
// implementation
// ============================================================================
@ -195,96 +176,26 @@ wxCursor::wxCursor(const wxImage& image)
const int w = wxCursorRefData::GetStandardWidth();
const int h = wxCursorRefData::GetStandardHeight();
wxImage image32 = image.Scale(w, h);
const int hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
const int hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
const int imagebitcount = (w*h)/8;
ByteArray bits(new unsigned char [imagebitcount]),
maskBits(new unsigned char [imagebitcount]);
int i, j, i8;
unsigned char c, cMask;
const unsigned char * const rgbBits = image32.GetData();
// first create the XOR mask
for ( i = 0; i < imagebitcount; i++ )
{
bits[i] = 0;
i8 = i * 8;
// unlike gtk, the pixels go in the opposite order in the bytes
cMask = 128;
for ( j = 0; j < 8; j++ )
{
// possible overflow if we do the summation first ?
c = rgbBits[(i8+j)*3]/3 +
rgbBits[(i8+j)*3+1]/3 +
rgbBits[(i8+j)*3+2]/3;
// if average value is > mid grey
if ( c > 127 )
bits[i] = bits[i] | cMask;
cMask = cMask / 2;
}
}
// now the AND one
if ( image32.HasMask() )
{
unsigned char r = image32.GetMaskRed(),
g = image32.GetMaskGreen(),
b = image32.GetMaskBlue();
for ( i = 0; i < imagebitcount; i++ )
{
maskBits[i] = 0x0;
i8 = i * 8;
cMask = 128;
for ( j = 0; j < 8; j++ )
{
if ( rgbBits[(i8+j)*3] == r &&
rgbBits[(i8+j)*3+1] == g &&
rgbBits[(i8+j)*3+2] == b )
{
maskBits[i] = maskBits[i] | cMask;
}
cMask = cMask / 2;
}
}
}
else // no mask in the image
{
memset(maskBits.get(), 0, sizeof(unsigned char)*imagebitcount);
}
// determine where should the cursors hot spot be
int hotSpotX = image32.GetOptionInt(wxCUR_HOTSPOT_X);
int hotSpotY = image32.GetOptionInt(wxCUR_HOTSPOT_Y);
if (hotSpotX < 0 || hotSpotX >= w)
hotSpotX = 0;
if (hotSpotY < 0 || hotSpotY >= h)
hotSpotY = 0;
// do create cursor now
HCURSOR hcursor = ::CreateCursor
(
wxGetInstance(),
hotSpotX, hotSpotY,
w, h,
/* AND */ maskBits.get(),
/* XOR */ bits.get()
);
wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < w &&
hotSpotY >= 0 && hotSpotY < h,
_T("invalid cursor hot spot coordinates") );
HCURSOR hcursor = wxBitmapToHCURSOR
(
wxBitmap(image.Scale(w, h)),
hotSpotX,
hotSpotY
);
if ( !hcursor )
{
wxLogLastError(_T("CreateCursor"));
}
else
{
m_refData = new wxCursorRefData(hcursor, true /* delete it */);
wxLogWarning(_("Failed to create cursor."));
return;
}
m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
}
wxCursor::wxCursor(const char WXUNUSED(bits)[],
@ -327,36 +238,23 @@ wxCursor::wxCursor(const wxString& filename,
hcursor = ::LoadCursorFromFile(filename);
break;
#if wxUSE_RESOURCE_LOADING_IN_MSW
case wxBITMAP_TYPE_ICO:
hcursor = IconToCursor((wxChar *)filename.c_str(),
wxGetInstance(),
hotSpotX, hotSpotY,
NULL, NULL);
hcursor = wxBitmapToHCURSOR
(
wxIcon(filename, wxBITMAP_TYPE_ICO),
hotSpotX,
hotSpotY
);
break;
case wxBITMAP_TYPE_BMP:
{
HBITMAP hBitmap = 0;
HPALETTE hPalette = 0;
if ( wxReadDIB((wxChar *)filename.c_str(), &hBitmap, &hPalette) )
{
if (hPalette)
DeleteObject(hPalette);
POINT pt;
pt.x = hotSpotX;
pt.y = hotSpotY;
hcursor = MakeCursorFromBitmap(wxGetInstance(), hBitmap, &pt);
DeleteObject(hBitmap);
}
else
{
hcursor = NULL;
}
}
hcursor = wxBitmapToHCURSOR
(
wxBitmap(filename, wxBITMAP_TYPE_BMP),
hotSpotX,
hotSpotY
);
break;
#endif // wxUSE_RESOURCE_LOADING_IN_MSW
default:
wxFAIL_MSG( _T("unknown cursor resource type") );

View File

@ -40,11 +40,6 @@
#include "wx/msw/private.h"
#if wxUSE_RESOURCE_LOADING_IN_MSW
#include "wx/msw/curico.h"
#include "wx/msw/curicop.h"
#endif
// ----------------------------------------------------------------------------
// wxWin macros
// ----------------------------------------------------------------------------
@ -97,36 +92,7 @@ wxIcon::~wxIcon()
void wxIcon::CopyFromBitmap(const wxBitmap& bmp)
{
#ifndef __WXMICROWIN__
#ifdef __WIN32__
wxMask *mask = bmp.GetMask();
if ( !mask )
{
// we must have a mask for an icon, so even if it's probably incorrect,
// do create it (grey is the "standard" transparent colour)
mask = new wxMask(bmp, *wxLIGHT_GREY);
}
ICONINFO iconInfo;
iconInfo.fIcon = TRUE; // we want an icon, not a cursor
iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
iconInfo.hbmColor = GetHbitmapOf(bmp);
// black out the transparent area to preserve background colour, because
// Windows blits the original bitmap using SRCINVERT (XOR) after applying
// the mask to the dest rect.
{
MemoryHDC dcSrc, dcDst;
SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
selectBitmap(dcDst, iconInfo.hbmColor);
if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
dcSrc, 0, 0, SRCAND) )
{
wxLogLastError(_T("BitBlt"));
}
}
HICON hicon = ::CreateIconIndirect(&iconInfo);
HICON hicon = wxBitmapToHICON(bmp);
if ( !hicon )
{
wxLogLastError(wxT("CreateIconIndirect"));
@ -136,29 +102,7 @@ void wxIcon::CopyFromBitmap(const wxBitmap& bmp)
SetHICON((WXHICON)hicon);
SetSize(bmp.GetWidth(), bmp.GetHeight());
}
if ( !bmp.GetMask() )
{
// we created the mask, now delete it
delete mask;
}
// delete the inverted mask bitmap we created as well
::DeleteObject(iconInfo.hbmMask);
#else // Win16
/*
// This probably doesn't work.
HBITMAP hBitmap = (HBITMAP) bmp.GetHBITMAP();
HICON hIcon = MakeIconFromBitmap((HINSTANCE) wxGetInstance(), hBitmap);
if (hIcon)
{
SetHICON((WXHICON)hIcon);
SetSize(bmp.GetWidth(), bmp.GetHeight());
}
*/
wxFAIL_MSG("Bitmap to icon conversion (including use of XPMs for icons) not implemented");
#endif // Win32/16
#endif
#endif // __WXMICROWIN__
}
void wxIcon::CreateIconFromXpm(const char **data)