1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/bitmap.h
3 // Purpose: wxBitmap class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/msw/gdiimage.h"
16 #include "wx/gdicmn.h"
17 #include "wx/palette.h"
19 class WXDLLEXPORT wxBitmap
;
20 class WXDLLEXPORT wxBitmapHandler
;
21 class WXDLLEXPORT wxBitmapRefData
;
22 class WXDLLEXPORT wxControl
;
23 class WXDLLEXPORT wxCursor
;
24 class WXDLLEXPORT wxDC
;
26 class WXDLLEXPORT wxDIB
;
28 class WXDLLEXPORT wxIcon
;
29 class WXDLLEXPORT wxImage
;
30 class WXDLLEXPORT wxMask
;
31 class WXDLLEXPORT wxPalette
;
32 class WXDLLEXPORT wxPixelDataBase
;
34 // ----------------------------------------------------------------------------
35 // wxBitmap: a mono or colour bitmap
36 // ----------------------------------------------------------------------------
38 class WXDLLEXPORT wxBitmap
: public wxGDIImage
41 // default ctor creates an invalid bitmap, you must Create() it later
42 wxBitmap() { Init(); }
45 wxBitmap(const wxBitmap
& bitmap
) : wxGDIImage(bitmap
) { Init(); Ref(bitmap
); }
47 // Initialize with raw data
48 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
50 // Initialize with XPM data
51 wxBitmap(const char **data
) { CreateFromXpm(data
); }
52 wxBitmap(char **data
) { CreateFromXpm((const char **)data
); }
54 // Load a file or resource
55 wxBitmap(const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_BMP_RESOURCE
);
57 // New constructor for generalised creation from data
58 wxBitmap(void *data
, long type
, int width
, int height
, int depth
= 1);
60 // Create a new, uninitialized bitmap of the given size and depth (if it
61 // is omitted, will create a bitmap compatible with the display)
63 // NB: this ctor will create a DIB for 24 and 32bpp bitmaps, use ctor
64 // taking a DC argument if you want to force using DDB in this case
65 wxBitmap(int width
, int height
, int depth
= -1);
67 // Create a bitmap compatible with the given DC
68 wxBitmap(int width
, int height
, const wxDC
& dc
);
70 #if wxUSE_IMAGE && wxUSE_WXDIB
71 // Convert from wxImage
72 wxBitmap(const wxImage
& image
, int depth
= -1)
73 { (void)CreateFromImage(image
, depth
); }
75 // Create a DDB compatible with the given DC from wxImage
76 wxBitmap(const wxImage
& image
, const wxDC
& dc
)
77 { (void)CreateFromImage(image
, dc
); }
80 // we must have this, otherwise icons are silently copied into bitmaps using
81 // the copy ctor but the resulting bitmap is invalid!
82 wxBitmap(const wxIcon
& icon
) { Init(); CopyFromIcon(icon
); }
84 wxBitmap
& operator=(const wxBitmap
& bitmap
)
86 if ( m_refData
!= bitmap
.m_refData
)
91 wxBitmap
& operator=(const wxIcon
& icon
)
93 (void)CopyFromIcon(icon
);
98 wxBitmap
& operator=(const wxCursor
& cursor
)
100 (void)CopyFromCursor(cursor
);
107 #if wxUSE_IMAGE && wxUSE_WXDIB
108 wxImage
ConvertToImage() const;
109 #endif // wxUSE_IMAGE
111 // get the given part of bitmap
112 wxBitmap
GetSubBitmap( const wxRect
& rect
) const;
114 // copies the contents and mask of the given (colour) icon to the bitmap
115 bool CopyFromIcon(const wxIcon
& icon
);
117 // copies the contents and mask of the given cursor to the bitmap
118 bool CopyFromCursor(const wxCursor
& cursor
);
121 // copies from a device independent bitmap
122 bool CopyFromDIB(const wxDIB
& dib
);
125 virtual bool Create(int width
, int height
, int depth
= -1);
126 virtual bool Create(int width
, int height
, const wxDC
& dc
);
127 virtual bool Create(void *data
, long type
, int width
, int height
, int depth
= 1);
128 virtual bool LoadFile(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
129 virtual bool SaveFile(const wxString
& name
, int type
, const wxPalette
*cmap
= NULL
);
131 wxBitmapRefData
*GetBitmapData() const
132 { return (wxBitmapRefData
*)m_refData
; }
134 // raw bitmap access support functions
135 void *GetRawData(wxPixelDataBase
& data
, int bpp
);
136 void UngetRawData(wxPixelDataBase
& data
);
139 wxPalette
* GetPalette() const;
140 void SetPalette(const wxPalette
& palette
);
141 #endif // wxUSE_PALETTE
143 wxMask
*GetMask() const;
144 wxBitmap
GetMaskBitmap() const;
145 void SetMask(wxMask
*mask
);
147 bool operator==(const wxBitmap
& bitmap
) const { return m_refData
== bitmap
.m_refData
; }
148 bool operator!=(const wxBitmap
& bitmap
) const { return m_refData
!= bitmap
.m_refData
; }
150 // these functions are internal and shouldn't be used, they risk to
151 // disappear in the future
152 bool HasAlpha() const;
155 #if WXWIN_COMPATIBILITY_2_4
156 // these functions do nothing and are only there for backwards
158 wxDEPRECATED( int GetQuality() const );
159 wxDEPRECATED( void SetQuality(int quality
) );
160 #endif // WXWIN_COMPATIBILITY_2_4
162 // implementation only from now on
163 // -------------------------------
166 void SetHBITMAP(WXHBITMAP bmp
) { SetHandle((WXHANDLE
)bmp
); }
167 WXHBITMAP
GetHBITMAP() const { return (WXHBITMAP
)GetHandle(); }
170 void SetSelectedInto(wxDC
*dc
);
171 wxDC
*GetSelectedInto() const;
172 #endif // __WXDEBUG__
175 // common part of all ctors
178 virtual wxGDIImageRefData
*CreateData() const;
179 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
181 // creates the bitmap from XPM data, supposed to be called from ctor
182 bool CreateFromXpm(const char **bits
);
184 // creates an uninitialized bitmap, called from Create()s above
185 bool DoCreate(int w
, int h
, int depth
, WXHDC hdc
);
187 #if wxUSE_IMAGE && wxUSE_WXDIB
188 // creates the bitmap from wxImage, supposed to be called from ctor
189 bool CreateFromImage(const wxImage
& image
, int depth
);
191 // creates a DDB from wxImage, supposed to be called from ctor
192 bool CreateFromImage(const wxImage
& image
, const wxDC
& dc
);
194 // common part of the 2 methods above (hdc may be 0)
195 bool CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
);
196 #endif // wxUSE_IMAGE
200 // common part of CopyFromIcon/CopyFromCursor for Win32
201 bool CopyFromIconOrCursor(const wxGDIImage
& icon
);
204 DECLARE_DYNAMIC_CLASS(wxBitmap
)
207 // ----------------------------------------------------------------------------
208 // wxMask: a mono bitmap used for drawing bitmaps transparently.
209 // ----------------------------------------------------------------------------
211 class WXDLLEXPORT wxMask
: public wxObject
216 // Construct a mask from a bitmap and a colour indicating the transparent
218 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
220 // Construct a mask from a bitmap and a palette index indicating the
222 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
224 // Construct a mask from a mono bitmap (copies the bitmap).
225 wxMask(const wxBitmap
& bitmap
);
227 // construct a mask from the givne bitmap handle
228 wxMask(WXHBITMAP hbmp
) { m_maskBitmap
= hbmp
; }
232 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
233 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
234 bool Create(const wxBitmap
& bitmap
);
237 WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
238 void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
241 WXHBITMAP m_maskBitmap
;
243 DECLARE_DYNAMIC_CLASS(wxMask
)
246 // ----------------------------------------------------------------------------
247 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
248 // ----------------------------------------------------------------------------
250 class WXDLLEXPORT wxBitmapHandler
: public wxGDIImageHandler
253 wxBitmapHandler() { m_type
= wxBITMAP_TYPE_INVALID
; }
254 wxBitmapHandler(const wxString
& name
, const wxString
& ext
, long type
)
255 : wxGDIImageHandler(name
, ext
, type
)
259 // keep wxBitmapHandler derived from wxGDIImageHandler compatible with the
260 // old class which worked only with bitmaps
261 virtual bool Create(wxBitmap
*bitmap
,
264 int width
, int height
, int depth
= 1);
265 virtual bool LoadFile(wxBitmap
*bitmap
,
266 const wxString
& name
,
268 int desiredWidth
, int desiredHeight
);
269 virtual bool SaveFile(wxBitmap
*bitmap
,
270 const wxString
& name
,
272 const wxPalette
*palette
= NULL
);
274 virtual bool Create(wxGDIImage
*image
,
277 int width
, int height
, int depth
= 1);
278 virtual bool Load(wxGDIImage
*image
,
279 const wxString
& name
,
281 int desiredWidth
, int desiredHeight
);
282 virtual bool Save(wxGDIImage
*image
,
283 const wxString
& name
,
287 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)