1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/palette.h"
22 #include "wx/dcmemory.h"
23 #include "wx/bitmap.h"
27 #include "wx/os2/private.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 #if !USE_SHARED_LIBRARIES
35 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
36 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
39 wxBitmapRefData::wxBitmapRefData()
47 m_selectedInto
= NULL
;
52 wxBitmapRefData::~wxBitmapRefData()
57 wxSprintf(buf
, wxT("Bitmap was deleted without selecting out of wxMemoryDC %lX."), (unsigned long) m_selectedInto
);
62 // TODO: DeleteObject((HBITMAP) m_hBitmap);
71 wxList
wxBitmap::sm_handlers
;
77 if ( wxTheBitmapList
)
78 wxTheBitmapList
->AddBitmap(this);
81 wxBitmap::wxBitmap(const wxBitmap
& bitmap
)
85 wxIcon *icon = wxDynamicCast(&bitmap, wxIcon);
88 HDC hdc = ::CreateCompatibleDC(NULL); // screen DC
89 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdc,
92 ::SelectObject(hdc, hbitmap);
93 ::DrawIcon(hdc, 0, 0, (HICON)icon->GetHICON());
97 SetHBITMAP((WXHBITMAP)hbitmap);
104 if ( wxTheBitmapList )
105 wxTheBitmapList->AddBitmap(this);
109 wxBitmap::~wxBitmap()
112 wxTheBitmapList
->DeleteObject(this);
115 bool wxBitmap::FreeResource(bool WXUNUSED(force
))
120 if (M_BITMAPDATA
->m_selectedInto
)
123 wxSprintf(buf
, wxT("Bitmap %lX was deleted without selecting out of wxMemoryDC %lX."), (unsigned long) this, (unsigned long) M_BITMAPDATA
->m_selectedInto
);
126 if (M_BITMAPDATA
->m_hBitmap
)
128 // TODO: DeleteObject((HBITMAP) M_BITMAPDATA->m_hBitmap);
130 M_BITMAPDATA
->m_hBitmap
= 0 ;
133 if (M_BITMAPDATA->m_bitmapPalette)
134 delete M_BITMAPDATA->m_bitmapPalette;
136 M_BITMAPDATA->m_bitmapPalette = NULL ;
143 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
145 m_refData
= new wxBitmapRefData
;
147 M_BITMAPDATA
->m_width
= the_width
;
148 M_BITMAPDATA
->m_height
= the_height
;
149 M_BITMAPDATA
->m_depth
= no_bits
;
150 M_BITMAPDATA
->m_numColors
= 0;
152 /* TODO: create the bitmap from data */
154 if ( wxTheBitmapList
)
155 wxTheBitmapList
->AddBitmap(this);
158 // Create from XPM data
159 wxBitmap::wxBitmap(char **data
, wxControl
*WXUNUSED(anItem
))
161 (void) Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
164 wxBitmap::wxBitmap(int w
, int h
, int d
)
166 (void)Create(w
, h
, d
);
168 if ( wxTheBitmapList
)
169 wxTheBitmapList
->AddBitmap(this);
172 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
174 (void) Create(data
, type
, width
, height
, depth
);
176 if ( wxTheBitmapList
)
177 wxTheBitmapList
->AddBitmap(this);
180 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
182 LoadFile(filename
, (int)type
);
184 if ( wxTheBitmapList
)
185 wxTheBitmapList
->AddBitmap(this);
188 bool wxBitmap::Create(int w
, int h
, int d
)
192 m_refData
= new wxBitmapRefData
;
194 M_BITMAPDATA
->m_width
= w
;
195 M_BITMAPDATA
->m_height
= h
;
196 M_BITMAPDATA
->m_depth
= d
;
198 /* TODO: create new bitmap */
200 return M_BITMAPDATA
->m_ok
;
203 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
207 m_refData
= new wxBitmapRefData
;
209 wxBitmapHandler
*handler
= FindHandler(type
);
211 if ( handler
== NULL
) {
212 wxLogWarning("no bitmap handler for type %d defined.", type
);
217 return handler
->LoadFile(this, filename
, type
, -1, -1);
220 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
224 m_refData
= new wxBitmapRefData
;
226 wxBitmapHandler
*handler
= FindHandler(type
);
228 if ( handler
== NULL
) {
229 wxLogWarning("no bitmap handler for type %d defined.", type
);
234 return handler
->Create(this, data
, type
, width
, height
, depth
);
237 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
239 wxBitmapHandler
*handler
= FindHandler(type
);
241 if ( handler
== NULL
) {
242 wxLogWarning("no bitmap handler for type %d defined.", type
);
247 return handler
->SaveFile(this, filename
, type
, palette
);
250 void wxBitmap::SetWidth(int w
)
253 m_refData
= new wxBitmapRefData
;
255 M_BITMAPDATA
->m_width
= w
;
258 void wxBitmap::SetHeight(int h
)
261 m_refData
= new wxBitmapRefData
;
263 M_BITMAPDATA
->m_height
= h
;
266 void wxBitmap::SetDepth(int d
)
269 m_refData
= new wxBitmapRefData
;
271 M_BITMAPDATA
->m_depth
= d
;
274 void wxBitmap::SetQuality(int q
)
277 m_refData
= new wxBitmapRefData
;
279 M_BITMAPDATA
->m_quality
= q
;
282 void wxBitmap::SetOk(bool isOk
)
285 m_refData
= new wxBitmapRefData
;
287 M_BITMAPDATA
->m_ok
= isOk
;
290 void wxBitmap::SetPalette(const wxPalette
& palette
)
293 m_refData
= new wxBitmapRefData
;
295 M_BITMAPDATA
->m_bitmapPalette
= palette
;
298 void wxBitmap::SetMask(wxMask
*mask
)
301 m_refData
= new wxBitmapRefData
;
303 M_BITMAPDATA
->m_bitmapMask
= mask
;
306 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
309 m_refData
= new wxBitmapRefData
;
311 M_BITMAPDATA
->m_hBitmap
= bmp
;
314 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
316 sm_handlers
.Append(handler
);
319 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
321 sm_handlers
.Insert(handler
);
324 bool wxBitmap::RemoveHandler(const wxString
& name
)
326 wxBitmapHandler
*handler
= FindHandler(name
);
329 sm_handlers
.DeleteObject(handler
);
336 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
338 wxNode
*node
= sm_handlers
.First();
341 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
342 if ( handler
->GetName() == name
)
349 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, long bitmapType
)
351 wxNode
*node
= sm_handlers
.First();
354 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
355 if ( handler
->GetExtension() == extension
&&
356 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
363 wxBitmapHandler
*wxBitmap::FindHandler(long bitmapType
)
365 wxNode
*node
= sm_handlers
.First();
368 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
369 if (handler
->GetType() == bitmapType
)
376 // Creates a bitmap that matches the device context, from
377 // an arbitray bitmap. At present, the original bitmap must have an
378 // associated palette. TODO: use a default palette if no palette exists.
379 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
380 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
382 wxBitmap
tmpBitmap(this->GetWidth(), this->GetHeight(), dc
.GetDepth());
386 HPALETTE hPal = (HPALETTE) NULL;
388 void *lpBits = (void*) NULL;
391 wxASSERT( this->GetPalette() && this->GetPalette()->Ok() && (this->GetPalette()->GetHPALETTE() != 0) );
393 tmpBitmap.SetPalette(this->GetPalette());
394 memDC.SelectObject(tmpBitmap);
395 memDC.SetPalette(this->GetPalette());
397 hPal = (HPALETTE) this->GetPalette()->GetHPALETTE();
399 if( this->GetPalette() && this->GetPalette()->Ok() && (this->GetPalette()->GetHPALETTE() != 0) )
401 tmpBitmap.SetPalette(* this->GetPalette());
402 memDC.SelectObject(tmpBitmap);
403 memDC.SetPalette(* this->GetPalette());
404 hPal = (HPALETTE) this->GetPalette()->GetHPALETTE();
408 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
410 palette.SetHPALETTE( (WXHPALETTE)hPal );
411 tmpBitmap.SetPalette( palette );
412 memDC.SelectObject(tmpBitmap);
413 memDC.SetPalette( palette );
416 // set the height negative because in a DIB the order of the lines is reversed
417 createDIB(this->GetWidth(), -this->GetHeight(), this->GetDepth(), hPal, &lpDib);
419 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
421 ::GetBitmapBits((HBITMAP)GetHBITMAP(), lpDib->bmiHeader.biSizeImage, lpBits);
423 ::SetDIBitsToDevice((HDC) memDC.GetHDC(), 0, 0, this->GetWidth(), this->GetHeight(),
424 0, 0, 0, this->GetHeight(), lpBits, lpDib, DIB_RGB_COLORS);
442 // Construct a mask from a bitmap and a colour indicating
443 // the transparent area
444 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
447 Create(bitmap
, colour
);
450 // Construct a mask from a bitmap and a palette index indicating
451 // the transparent area
452 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
455 Create(bitmap
, paletteIndex
);
458 // Construct a mask from a mono bitmap (copies the bitmap).
459 wxMask::wxMask(const wxBitmap
& bitmap
)
467 // TODO: delete mask bitmap
470 // Create a mask from a mono bitmap (copies the bitmap).
471 bool wxMask::Create(const wxBitmap
& bitmap
)
477 // Create a mask from a bitmap and a palette index indicating
478 // the transparent area
479 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
485 // Create a mask from a bitmap and a colour indicating
486 // the transparent area
487 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
497 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
499 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
504 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long type
,
505 int desiredWidth
, int desiredHeight
)
510 bool wxBitmapHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
519 /* TODO: bitmap handlers, a bit like this:
520 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
522 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
524 inline wxBMPResourceHandler()
526 m_name = "Windows bitmap resource";
528 m_type = wxBITMAP_TYPE_BMP_RESOURCE;
531 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
532 int desiredWidth, int desiredHeight);
534 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
537 void wxBitmap::CleanUpHandlers()
539 wxNode
*node
= sm_handlers
.First();
542 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
543 wxNode
*next
= node
->Next();
550 void wxBitmap::InitStandardHandlers()
552 /* TODO: initialize all standard bitmap or derive class handlers here.
553 AddHandler(new wxBMPResourceHandler);
554 AddHandler(new wxBMPFileHandler);
555 AddHandler(new wxXPMFileHandler);
556 AddHandler(new wxXPMDataHandler);
557 AddHandler(new wxICOResourceHandler);
558 AddHandler(new wxICOFileHandler);