/////////////////////////////////////////////////////////////////////////////
// Name: bitmap.cpp
// Purpose: wxBitmap
-// Author: AUTHOR
+// Author: David Webster
// Modified by:
-// Created: ??/??/98
+// Created: 08/08/99
// RCS-ID: $Id$
-// Copyright: (c) AUTHOR
+// Copyright: (c) David Webster
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
-#pragma implementation "bitmap.h"
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#ifndef WX_PRECOMP
+ #include <stdio.h>
+
+ #include "wx/list.h"
+ #include "wx/utils.h"
+ #include "wx/app.h"
+ #include "wx/palette.h"
+ #include "wx/dcmemory.h"
+ #include "wx/bitmap.h"
+ #include "wx/icon.h"
#endif
-#include "wx/setup.h"
-#include "wx/utils.h"
-#include "wx/palette.h"
-#include "wx/bitmap.h"
-#include "wx/icon.h"
+#include "wx/os2/private.h"
#include "wx/log.h"
+//#include "wx/msw/dib.h"
+#include "wx/image.h"
+
+// ----------------------------------------------------------------------------
+// macros
+// ----------------------------------------------------------------------------
+
#if !USE_SHARED_LIBRARIES
IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
+
+IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
#endif
+// ============================================================================
+// implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// wxBitmapRefData
+// ----------------------------------------------------------------------------
+
wxBitmapRefData::wxBitmapRefData()
{
- m_ok = FALSE;
- m_width = 0;
- m_height = 0;
- m_depth = 0;
- m_quality = 0;
- m_numColors = 0;
- m_bitmapMask = NULL;
+ m_nQuality = 0;
+ m_pSelectedInto = NULL;
+ m_nNumColors = 0;
+ m_pBitmapMask = NULL;
}
-wxBitmapRefData::~wxBitmapRefData()
+void wxBitmapRefData::Free()
{
- /*
- * TODO: delete the bitmap data here.
- */
+ wxASSERT_MSG( !m_pSelectedInto,
+ wxT("deleting bitmap still selected into wxMemoryDC") );
- if (m_bitmapMask)
- delete m_bitmapMask;
- m_bitmapMask = NULL;
+ if (m_hBitmap)
+ {
+ if ( !::GpiDeleteBitmap((HBITMAP)m_hBitmap) )
+ {
+ wxLogLastError("GpiDeleteBitmap(hbitmap)");
+ }
+ }
+
+ delete m_pBitmapMask;
+ m_pBitmapMask = NULL;
}
-wxList wxBitmap::sm_handlers;
+// ----------------------------------------------------------------------------
+// wxBitmap creation
+// ----------------------------------------------------------------------------
-wxBitmap::wxBitmap()
+// this function should be called from all wxBitmap ctors
+void wxBitmap::Init()
{
- m_refData = NULL;
+ // m_refData = NULL; done in the base class ctor
- if ( wxTheBitmapList )
+ if (wxTheBitmapList)
wxTheBitmapList->AddBitmap(this);
}
-wxBitmap::~wxBitmap()
+bool wxBitmap::CopyFromIconOrCursor(
+ const wxGDIImage& rIcon
+)
{
- if (wxTheBitmapList)
- wxTheBitmapList->DeleteObject(this);
-}
+ wxBitmapRefData* pRefData = new wxBitmapRefData;
-wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
-{
- m_refData = new wxBitmapRefData;
+ m_refData = pRefData;
- M_BITMAPDATA->m_width = the_width ;
- M_BITMAPDATA->m_height = the_height ;
- M_BITMAPDATA->m_depth = no_bits ;
- M_BITMAPDATA->m_numColors = 0;
+ refData->m_width = rIcon.GetWidth();
+ refData->m_height = rIcon.GetHeight();
+ refData->m_depth = wxDisplayDepth();
- /* TODO: create the bitmap from data */
+ refData->m_hBitmap = (WXHBITMAP)rIcon.GetHandle();
+ // no mask???
+ refData->m_bitmapMask = new wxMask();
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
+#if WXWIN_COMPATIBILITY_2
+ refData->m_ok = TRUE;
+#endif // WXWIN_COMPATIBILITY_2
+
+ return(TRUE);
}
-wxBitmap::wxBitmap(int w, int h, int d)
+bool wxBitmap::CopyFromCursor(
+ const wxCursor& rCursor
+)
{
- (void)Create(w, h, d);
+ UnRef();
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
+ if (!rCursor.Ok())
+ return(FALSE);
+ return CopyFromIconOrCursor(wxGDIImage)rCursor);
}
-wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
+bool wxBitmap::CopyFromIcon(
+ const wxIcon& rIcon
+)
{
- (void) Create(data, type, width, height, depth);
+ UnRef();
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
-}
+ if (!rIcon.Ok())
+ return(FALSE);
-wxBitmap::wxBitmap(const wxString& filename, long type)
-{
- LoadFile(filename, (int)type);
+#if WXWIN_COMPATIBILITY_2
+ refData->m_ok = TRUE;
+#endif // WXWIN_COMPATIBILITY_2
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
+ return CopyFromIconOrCursor(icon);
}
-/* TODO: maybe allow creation from XPM
-// Create from data
-wxBitmap::wxBitmap(const char **data)
+wxBitmap::~wxBitmap()
{
- (void) Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
+ if (wxTheBitmapList)
+ wxTheBitmapList->DeleteObject(this);
}
-*/
-bool wxBitmap::Create(int w, int h, int d)
-{
- UnRef();
+wxBitmap::wxBitmap(
+ const char zBits[]
+, int nTheWidth
+, int nTheHeight
+, int nNoBits
+)
+{
+ Init();
+
+ wxBitmapRefData* pRefData = new wxBitmapRefData;
+ BITMAPINFOHEADER2 vHeader;
+ BITMAPINFO2 vInfo;
+ HDC hDc;
+ HPS hPs;
+ DEVOPENSTRUCT vDop = { NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+ SIZEL vSize = {0, 0};
+
+ wxAssert(vHabmain != NULL);
+
+ hDc = ::DevOpenDC(vHabmain, OD_MEMORY, (PSZ)"*", 1L, (PDEVOPENDATA)&vDop, 0L);
+
+ vHeader.cbFix = sizeof(vHeader);
+ vHeader.cx = (USHORT)nTheWidth;
+ vHeader.cy = (USHORT)nTheHeight;
+ vHeader.cPlanes = 1L;
+ vHeader.cBitCount = nNoBits;
+ vHeader.ulCompression = BCA_UNCOMP;
+ vHeader.cxResolution = 0;
+ vHeader.cyResolution = 0;
+ vHeader.cclrUsed = 0;
+ vHeader.cclrImportant = 0;
+ vHeader.usUnits = BRU_METRIC;
+ vHeader.usRecording = BRA_BOTTOMUP;
+ vHeader.usRendering = BRH_NOTHALFTONED;
+ vHeader.cSize1 = 0;
+ vHeader.cSize2 = 0;
+ vHeader.ulColorEncoding = 0;
+ vHeader.ulIdentifier = 0;
+
+ hPs = ::GpiCreatePS(habMain, hdc, &vSize, GPIA_ASSOC | PU_PELS);
+ if (hPs == 0)
+ {
+ wxLogLastError("GpiCreatePS Failure");
+ }
- m_refData = new wxBitmapRefData;
+ m_hDc = hDc;
+ m_hPs = hPs;
- M_BITMAPDATA->m_width = w;
- M_BITMAPDATA->m_height = h;
- M_BITMAPDATA->m_depth = d;
+ m_refData = pRefData;
- /* TODO: create new bitmap */
+ refData->m_width = nTheWidth;
+ refData->m_height = nTheHeight;
+ refData->m_depth = nNoBits;
+ refData->m_numColors = 0;
+ refData->m_selectedInto = NULL;
- return M_BITMAPDATA->m_ok;
-}
+ HBITMAP hBmp = ::GpiCreateBitmap(hPs, &vHeader, 0L, NULL, &vInfo);
+ if ( !hbmp )
+ {
+ wxLogLastError("CreateBitmap");
+ }
+ SetHBITMAP((WXHBITMAP)hbmp);
+}
+
+// Create from XPM data
+wxBitmap::wxBitmap(
+ char** ppData
+, wxControl* WXUNUSED(pAnItem))
+{
+ Init();
+
+F (void)Create( (void *)ppData
+ ,wxBITMAP_TYPE_XPM_DATA
+ ,0
+ ,0
+ ,0
+ );
+}
+
+wxBitmap::wxBitmap(
+ int nW
+, int nH
+, int nD
+)
+{
+ Init();
+
+ (void)Create( nW
+ ,nH
+ ,nD
+ );
+}
+
+wxBitmap::wxBitmap(
+ void* pData
+, long lType
+, int nWidth
+, int nHeight
+, int nDepth
+)
+{
+ Init();
+
+ (void)Create( pData
+ ,lType
+ ,nWidth
+ ,nHeight
+ ,nDepth
+ );
+}
+
+wxBitmap::wxBitmap(
+ const wxString& rFilename
+, long lType
+)
+{
+ Init();
+
+ LoadFile( rFilename
+ ,(int)lType
+ );
+}
+
+bool wxBitmap::Create(
+ int nW
+, int nH
+, int nD
+)
+{
+ HBITMAP hBmp;
+ BITMAPINFOHEADER2 vHeader;
+ BITMAPINFO2 vInfo;
+ HPS hpsScreen;
+ HDC hdcScreen;
+ DEVOPENSTRUCT vDop = { NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+ SIZEL vSize = {0, 0};
+ LONG lBitCount;
+
+ wxAssert(vHabmain != NULL);
+
+ hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
+ hdcScreen = ::GpiQueryDevice(hpsScreen);
+ ::DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, &lBitCount);
+
+ vHeader.cbFix = sizeof(vHeader);
+ vHeader.cx = (USHORT)nW;
+ vHeader.cy = (USHORT)nH;
+ vHeader.cPlanes = (USHORT)nD;
+ vHeader.cBitCount = lBitCount;
+ vHeader.ulCompression = BCA_UNCOMP;
+ vHeader.cxResolution = 0;
+ vHeader.cyResolution = 0;
+ vHeader.cclrUsed = 0;
+ vHeader.cclrImportant = 0;
+ vHeader.usUnits = BRU_METRIC;
+ vHeader.usRecording = BRA_BOTTOMUP;
+ vHeader.usRendering = BRH_NOTHALFTONED;
+ vHeader.cSize1 = 0;
+ vHeader.cSize2 = 0;
+ vHeader.ulColorEncoding = 0;
+ vHeader.ulIdentifier = 0;
-bool wxBitmap::LoadFile(const wxString& filename, long type)
-{
- UnRef();
+ UnRef();
m_refData = new wxBitmapRefData;
- wxBitmapHandler *handler = FindHandler(type);
-
- if ( handler == NULL ) {
- wxLogWarning("no bitmap handler for type %d defined.", type);
+ GetBitmapData()->m_width = nW;
+ GetBitmapData()->m_height = nH;
+ GetBitmapData()->m_depth = nD;
- return FALSE;
+ if (nD > 0)
+ {
+ hBmp = ::GpiCreateBitmap(hpsScreen, &vHeader, 0L, NULL, &vInfo);
+ if (!hBmp)
+ {
+ wxLogLastError("CreateBitmap");
+ }
+ }
+ else
+ {
+ LONG lPlanes;
+
+ ::DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, &lPlanes);
+ hBmp = ::GpiCreateBitmap(hpsScreen, &vHeader, 0L, NULL, &vInfo);
+ if (!hBmp)
+ {
+ wxLogLastError("CreateBitmap");
+ }
+ GetBitmapData()->m_depth = wxDisplayDepth();
}
+ SetHBITMAP((WXHBITMAP)hBmp);
- return handler->LoadFile(this, filename, type, -1, -1);
+#if WXWIN_COMPATIBILITY_2
+ GetBitmapData()->m_bOk = hBmp != 0;
+#endif // WXWIN_COMPATIBILITY_2
+
+ return Ok();
}
-bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
+bool wxBitmap::LoadFile(
+ const wxString& rFilename
+, long lType
+)
{
UnRef();
- m_refData = new wxBitmapRefData;
-
- wxBitmapHandler *handler = FindHandler(type);
+ wxBitmapHandler* pHandler = wxDynamicCast( FindHandler(lType)
+ ,wxBitmapHandler
+ );
- if ( handler == NULL ) {
- wxLogWarning("no bitmap handler for type %d defined.", type);
+ if (pHandler)
+ {
+ m_refData = new wxBitmapRefData;
- return FALSE;
+ return(pHandler->LoadFile( this
+ ,rFilename
+ ,lType
+ , -1
+ , -1
+ );
}
+ else
+ {
+ wxImage vImage;
+
+ if (!vImage.LoadFile(rFilename, lType) || !image.Ok() )
+ return(FALSE);
- return handler->Create(this, data, type, width, height, depth);
+ *this = vImage.ConvertToBitmap();
+
+ return(TRUE);
+ }
}
-bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette)
+bool wxBitmap::Create(
+ void* pData
+, long lType
+, int nWidth
+, int nHeight
+, int nDepth
+)
{
- wxBitmapHandler *handler = FindHandler(type);
+ UnRef();
- if ( handler == NULL ) {
- wxLogWarning("no bitmap handler for type %d defined.", type);
+ wxBitmapHandler* pHandler = wxDynamicCast( FindHandler(lType)
+ ,wxBitmapHandler
+ );
- return FALSE;
- }
+ if (!pHandler)
+ {
+ wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
+ "type %d defined."), type);
- return handler->SaveFile(this, filename, type, palette);
-}
+ return(FALSE);
+ }
-void wxBitmap::SetWidth(int w)
-{
- if (!M_BITMAPDATA)
- m_refData = new wxBitmapRefData;
+ m_refData = new wxBitmapRefData;
- M_BITMAPDATA->m_width = w;
+ return(handler->Create( this
+ ,pData
+ ,lType
+ ,nWidth
+ ,nHeight
+ ,nDepth
+ ));
}
-void wxBitmap::SetHeight(int h)
+bool wxBitmap::SaveFile(
+ const wxString& rFilename
+, int lType
+, const wxPalette* pPalette
+)
{
- if (!M_BITMAPDATA)
- m_refData = new wxBitmapRefData;
+ wxBitmapHandler* pHandler = wxDynamicCast( FindHandler(lType)
+ ,wxBitmapHandler
+ );
- M_BITMAPDATA->m_height = h;
-}
+ if (pHandler)
+ {
+ return pHandler->SaveFile( this
+ ,rFilename
+ ,lType
+ ,pPalette
+ );
+ }
+ else
+ {
+ // FIXME what about palette? shouldn't we use it?
+ wxImage vImage(*this);
-void wxBitmap::SetDepth(int d)
-{
- if (!M_BITMAPDATA)
- m_refData = new wxBitmapRefData;
+ if (!vImage.Ok())
+ return(FALSE);
- M_BITMAPDATA->m_depth = d;
+ return(vImage.SaveFile( rFilename
+ ,lType
+ ));
+ }
}
-void wxBitmap::SetQuality(int q)
-{
- if (!M_BITMAPDATA)
- m_refData = new wxBitmapRefData;
-
- M_BITMAPDATA->m_quality = q;
-}
+// ----------------------------------------------------------------------------
+// wxBitmap accessors
+// ----------------------------------------------------------------------------
-void wxBitmap::SetOk(bool isOk)
+void wxBitmap::SetQuality(
+ int nQ
+)
{
- if (!M_BITMAPDATA)
- m_refData = new wxBitmapRefData;
+ EnsureHasData();
- M_BITMAPDATA->m_ok = isOk;
+ GetBitmapData()->m_nQuality = nQ;
}
-void wxBitmap::SetPalette(const wxPalette& palette)
+#if WXWIN_COMPATIBILITY_2
+void wxBitmap::SetOk(
+ bool bOk
+)
{
- if (!M_BITMAPDATA)
- m_refData = new wxBitmapRefData;
+ EnsureHasData();
- M_BITMAPDATA->m_bitmapPalette = palette ;
+ GetBitmapData()->m_bOk = bOk;
}
+#endif // WXWIN_COMPATIBILITY_2
-void wxBitmap::SetMask(wxMask *mask)
+void wxBitmap::SetPalette(
+ const wxPalette& rPalette
+)
{
- if (!M_BITMAPDATA)
- m_refData = new wxBitmapRefData;
+ EnsureHasData();
- M_BITMAPDATA->m_bitmapMask = mask ;
+ GetBitmapData()->m_vBitmapPalette = rPalette;
}
-void wxBitmap::AddHandler(wxBitmapHandler *handler)
+void wxBitmap::SetMask(
+ wxMask* pMask
+)
{
- sm_handlers.Append(handler);
-}
+ EnsureHasData();
-void wxBitmap::InsertHandler(wxBitmapHandler *handler)
-{
- sm_handlers.Insert(handler);
+ GetBitmapData()->m_pBitmapMask = pMask;
}
-bool wxBitmap::RemoveHandler(const wxString& name)
+// Will try something for OS/2 but not really sure how close
+// to the msw intent this is.
+wxBitmap wxBitmap::GetBitmapForDC(
+ wxDC& rDc
+) const
{
- wxBitmapHandler *handler = FindHandler(name);
- if ( handler )
- {
- sm_handlers.DeleteObject(handler);
- return TRUE;
- }
- else
- return FALSE;
-}
+ wxMemoryDC vMemDC;
+ wxBitmap vTmpBitmap( this->GetWidth()
+ ,this->GetHeight()
+ ,rDc.GetDepth()
+ );
+ HPS hMemoryPS;
+ HPS hPs;
+ POINTL vPoint[4];
+ SIZEL vSize = {0,0}
-wxBitmapHandler *wxBitmap::FindHandler(const wxString& name)
-{
- wxNode *node = sm_handlers.First();
- while ( node )
- {
- wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
- if ( handler->GetName() == name )
- return handler;
- node = node->Next();
- }
- return NULL;
-}
+ hMemoryPS = ::GpiCreatePS(habMain, (HDC)vMemDC.m_hDc, &vSize, PU_PELS | GPIT_MICRO | GPI_ASSOC);
+ hPs = ::GpiCreatePS(habMain, (HDC)rDc.m_hDc, &vSize, PU_PELS | GPIT_MICRO | GPI_ASSOC);
-wxBitmapHandler *wxBitmap::FindHandler(const wxString& extension, long bitmapType)
-{
- wxNode *node = sm_handlers.First();
- while ( node )
- {
- wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
- if ( handler->GetExtension() == extension &&
- (bitmapType == -1 || handler->GetType() == bitmapType) )
- return handler;
- node = node->Next();
- }
- return NULL;
-}
+ // TODO: Set the points
-wxBitmapHandler *wxBitmap::FindHandler(long bitmapType)
-{
- wxNode *node = sm_handlers.First();
- while ( node )
- {
- wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
- if (handler->GetType() == bitmapType)
- return handler;
- node = node->Next();
- }
- return NULL;
+ rDc.m_oldBitmap = (WXHBITMAP)::GpiSetBitMap(hPs, (HBITMAP)vTmpBitmap.GetHBITMAP());
+ :GpiBitBlt(hPs, hMemoryPS, 4L, vPoint, &vSize, PU_PELS | GPI_ASSOC);
+
+ return tmpBitmap;
}
-/*
- * wxMask
- */
+// ----------------------------------------------------------------------------
+// wxMask
+// ----------------------------------------------------------------------------
wxMask::wxMask()
{
-/* TODO
- m_maskBitmap = 0;
-*/
+ m_hMaskBitmap = 0;
}
// Construct a mask from a bitmap and a colour indicating
// the transparent area
-wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
+wxMask::wxMask(
+ const wxBitmap& rBitmap
+, const wxColour& rColour
+)
{
-/* TODO
- m_maskBitmap = 0;
-*/
- Create(bitmap, colour);
+ m_hMaskBitmap = 0;
+ Create( rBitmap
+ ,rColour
+ );
}
// Construct a mask from a bitmap and a palette index indicating
// the transparent area
-wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
+wxMask::wxMask(
+ const wxBitmap& rBitmap
+, int nPaletteIndex
+)
{
-/* TODO
- m_maskBitmap = 0;
-*/
-
- Create(bitmap, paletteIndex);
+ m_hMaskBitmap = 0;
+ Create( rBitmap
+ ,nPaletteIndex
+ );
}
// Construct a mask from a mono bitmap (copies the bitmap).
-wxMask::wxMask(const wxBitmap& bitmap)
+wxMask::wxMask(
+ const wxBitmap& rBitmap
+)
{
-/* TODO
- m_maskBitmap = 0;
-*/
-
- Create(bitmap);
+ m_hMaskBitmap = 0;
+ Create(rBitmap);
}
wxMask::~wxMask()
{
-// TODO: delete mask bitmap
+ if (m_hMaskBitmap)
+ ::GpiDeleteBitmap((HBITMAP)m_hMaskBitmap);
}
// Create a mask from a mono bitmap (copies the bitmap).
bool wxMask::Create(const wxBitmap& bitmap)
{
-// TODO
- return FALSE;
+ if ( m_maskBitmap )
+ {
+ ::DeleteObject((HBITMAP) m_maskBitmap);
+ m_maskBitmap = 0;
+ }
+ if (!bitmap.Ok() || bitmap.GetDepth() != 1)
+ {
+ return FALSE;
+ }
+ m_maskBitmap = (WXHBITMAP) CreateBitmap(
+ bitmap.GetWidth(),
+ bitmap.GetHeight(),
+ 1, 1, 0
+ );
+ HDC srcDC = CreateCompatibleDC(0);
+ SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
+ HDC destDC = CreateCompatibleDC(0);
+ SelectObject(destDC, (HBITMAP) m_maskBitmap);
+ BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
+ SelectObject(srcDC, 0);
+ DeleteDC(srcDC);
+ SelectObject(destDC, 0);
+ DeleteDC(destDC);
+ return TRUE;
}
// Create a mask from a bitmap and a palette index indicating
// the transparent area
bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
{
-// TODO
+ if ( m_maskBitmap )
+ {
+ ::DeleteObject((HBITMAP) m_maskBitmap);
+ m_maskBitmap = 0;
+ }
+ if (bitmap.Ok() && bitmap.GetPalette()->Ok())
+ {
+ unsigned char red, green, blue;
+ if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
+ {
+ wxColour transparentColour(red, green, blue);
+ return Create(bitmap, transparentColour);
+ }
+ }
return FALSE;
}
// the transparent area
bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
{
-// TODO
- return FALSE;
+ if ( m_maskBitmap )
+ {
+ ::DeleteObject((HBITMAP) m_maskBitmap);
+ m_maskBitmap = 0;
+ }
+ if (!bitmap.Ok())
+ {
+ return FALSE;
+ }
+
+ // scan the bitmap for the transparent colour and set
+ // the corresponding pixels in the mask to BLACK and
+ // the rest to WHITE
+ COLORREF maskColour = RGB(colour.Red(), colour.Green(), colour.Blue());
+ m_maskBitmap = (WXHBITMAP) ::CreateBitmap(
+ bitmap.GetWidth(),
+ bitmap.GetHeight(),
+ 1, 1, 0
+ );
+ HDC srcDC = ::CreateCompatibleDC(0);
+ ::SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
+ HDC destDC = ::CreateCompatibleDC(0);
+ ::SelectObject(destDC, (HBITMAP) m_maskBitmap);
+
+ // this is not very efficient, but I can't think
+ // of a better way of doing it
+ for (int w = 0; w < bitmap.GetWidth(); w++)
+ {
+ for (int h = 0; h < bitmap.GetHeight(); h++)
+ {
+ COLORREF col = GetPixel(srcDC, w, h);
+ if (col == maskColour)
+ {
+ ::SetPixel(destDC, w, h, RGB(0, 0, 0));
+ }
+ else
+ {
+ ::SetPixel(destDC, w, h, RGB(255, 255, 255));
+ }
+ }
+ }
+ ::SelectObject(srcDC, 0);
+ ::DeleteDC(srcDC);
+ ::SelectObject(destDC, 0);
+ ::DeleteDC(destDC);
+ return TRUE;
}
-/*
- * wxBitmapHandler
- */
+// ----------------------------------------------------------------------------
+// wxBitmapHandler
+// ----------------------------------------------------------------------------
-IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
+bool wxBitmapHandler::Create(wxGDIImage *image,
+ void *data,
+ long flags,
+ int width, int height, int depth)
+{
+ wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
-bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
+ return bitmap ? Create(bitmap, data, width, height, depth) : FALSE;
+}
+
+bool wxBitmapHandler::Load(wxGDIImage *image,
+ const wxString& name,
+ long flags,
+ int width, int height)
+{
+ wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
+
+ return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
+}
+
+bool wxBitmapHandler::Save(wxGDIImage *image,
+ const wxString& name,
+ int type)
+{
+ wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
+
+ return bitmap ? SaveFile(bitmap, name, type) : FALSE;
+}
+
+bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
+ void *WXUNUSED(data),
+ long WXUNUSED(type),
+ int WXUNUSED(width),
+ int WXUNUSED(height),
+ int WXUNUSED(depth))
{
return FALSE;
}
-bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long type,
- int desiredWidth, int desiredHeight)
+bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
+ const wxString& WXUNUSED(name),
+ long WXUNUSED(type),
+ int WXUNUSED(desiredWidth),
+ int WXUNUSED(desiredHeight))
{
return FALSE;
}
-bool wxBitmapHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
+bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
+ const wxString& WXUNUSED(name),
+ int WXUNUSED(type),
+ const wxPalette *WXUNUSED(palette))
{
return FALSE;
}
-/*
- * Standard handlers
- */
+// ----------------------------------------------------------------------------
+// DIB functions
+// ----------------------------------------------------------------------------
-/* TODO: bitmap handlers, a bit like this:
-class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
+bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
+ HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
{
- DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
-public:
- inline wxBMPResourceHandler()
- {
- m_name = "Windows bitmap resource";
- m_extension = "";
- m_type = wxBITMAP_TYPE_BMP_RESOURCE;
- };
+ unsigned long i, headerSize;
+ LPBITMAPINFO lpDIBheader = NULL;
+ LPPALETTEENTRY lpPe = NULL;
- virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
- int desiredWidth, int desiredHeight);
-};
-IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
-*/
-void wxBitmap::CleanUpHandlers()
-{
- wxNode *node = sm_handlers.First();
- while ( node )
- {
- wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
- wxNode *next = node->Next();
- delete handler;
- delete node;
- node = next;
- }
-}
+ // Allocate space for a DIB header
+ headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
+ lpDIBheader = (BITMAPINFO *) malloc(headerSize);
+ lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
-void wxBitmap::InitStandardHandlers()
-{
-/* TODO: initialize all standard bitmap or derive class handlers here.
- AddHandler(new wxBMPResourceHandler);
- AddHandler(new wxBMPFileHandler);
- AddHandler(new wxXPMFileHandler);
- AddHandler(new wxXPMDataHandler);
- AddHandler(new wxICOResourceHandler);
- AddHandler(new wxICOFileHandler);
-*/
+ GetPaletteEntries(hPal, 0, 256, lpPe);
+
+ memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
+
+ // Fill in the static parts of the DIB header
+ lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
+ lpDIBheader->bmiHeader.biWidth = xSize;
+ lpDIBheader->bmiHeader.biHeight = ySize;
+ lpDIBheader->bmiHeader.biPlanes = 1;
+
+ // this value must be 1, 4, 8 or 24 so PixelDepth can only be
+ lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
+ lpDIBheader->bmiHeader.biCompression = BI_RGB;
+ lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
+ lpDIBheader->bmiHeader.biClrUsed = 256;
+
+
+ // Initialize the DIB palette
+ for (i = 0; i < 256; i++) {
+ lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
+ lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
+ lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
+ lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
+ }
+
+ *lpDIBHeader = lpDIBheader;
+
+ return TRUE;
}
-void wxBitmap::SetHBITMAP(WXHBITMAP bmp)
+void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
{
- if (!M_BITMAPDATA)
- m_refData = new wxBitmapRefData;
-
- M_BITMAPDATA->m_hBitmap = bmp;
+ free(lpDIBHeader);
}
+