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/palette.h"
18 class WXDLLIMPEXP_FWD_CORE wxBitmap
;
19 class WXDLLIMPEXP_FWD_CORE wxBitmapHandler
;
20 class WXDLLIMPEXP_FWD_CORE wxBitmapRefData
;
21 class WXDLLIMPEXP_FWD_CORE wxControl
;
22 class WXDLLIMPEXP_FWD_CORE wxCursor
;
23 class WXDLLIMPEXP_FWD_CORE wxDC
;
25 class WXDLLIMPEXP_FWD_CORE wxDIB
;
27 class WXDLLIMPEXP_FWD_CORE wxIcon
;
28 class WXDLLIMPEXP_FWD_CORE wxImage
;
29 class WXDLLIMPEXP_FWD_CORE wxMask
;
30 class WXDLLIMPEXP_FWD_CORE wxPalette
;
31 class WXDLLIMPEXP_FWD_CORE wxPixelDataBase
;
33 // What kind of transparency should a bitmap copied from an icon or cursor
35 enum wxBitmapTransparency
37 wxBitmapTransparency_Auto
, // default: copy alpha if the source has it
38 wxBitmapTransparency_None
, // never create alpha
39 wxBitmapTransparency_Always
// always use alpha
42 // ----------------------------------------------------------------------------
43 // wxBitmap: a mono or colour bitmap
44 // ----------------------------------------------------------------------------
46 class WXDLLEXPORT wxBitmap
: public wxGDIImage
49 // default ctor creates an invalid bitmap, you must Create() it later
52 // Initialize with raw data
53 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
55 // Initialize with XPM data
56 wxBitmap(const char* const* data
);
60 *this = wxBitmap(wx_const_cast(const char* const*, data
));
64 // Load a file or resource
65 wxBitmap(const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_BMP_RESOURCE
);
67 // New constructor for generalised creation from data
68 wxBitmap(const void* data
, long type
, int width
, int height
, int depth
= 1);
70 // Create a new, uninitialized bitmap of the given size and depth (if it
71 // is omitted, will create a bitmap compatible with the display)
73 // NB: this ctor will create a DIB for 24 and 32bpp bitmaps, use ctor
74 // taking a DC argument if you want to force using DDB in this case
75 wxBitmap(int width
, int height
, int depth
= -1);
77 // Create a bitmap compatible with the given DC
78 wxBitmap(int width
, int height
, const wxDC
& dc
);
81 // Convert from wxImage
82 wxBitmap(const wxImage
& image
, int depth
= -1)
83 { (void)CreateFromImage(image
, depth
); }
85 // Create a DDB compatible with the given DC from wxImage
86 wxBitmap(const wxImage
& image
, const wxDC
& dc
)
87 { (void)CreateFromImage(image
, dc
); }
90 // we must have this, otherwise icons are silently copied into bitmaps using
91 // the copy ctor but the resulting bitmap is invalid!
92 wxBitmap(const wxIcon
& icon
,
93 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
)
95 CopyFromIcon(icon
, transp
);
98 wxBitmap
& operator=(const wxIcon
& icon
)
100 (void)CopyFromIcon(icon
);
105 wxBitmap
& operator=(const wxCursor
& cursor
)
107 (void)CopyFromCursor(cursor
);
115 wxImage
ConvertToImage() const;
116 #endif // wxUSE_IMAGE
118 // get the given part of bitmap
119 wxBitmap
GetSubBitmap( const wxRect
& rect
) const;
121 // NB: This should not be called from user code. It is for wx internal
123 wxBitmap
GetSubBitmapOfHDC( const wxRect
& rect
, WXHDC hdc
) const;
125 // copies the contents and mask of the given (colour) icon to the bitmap
126 bool CopyFromIcon(const wxIcon
& icon
,
127 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
);
129 // copies the contents and mask of the given cursor to the bitmap
130 bool CopyFromCursor(const wxCursor
& cursor
,
131 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
);
134 // copies from a device independent bitmap
135 bool CopyFromDIB(const wxDIB
& dib
);
138 virtual bool Create(int width
, int height
, int depth
= -1);
139 virtual bool Create(int width
, int height
, const wxDC
& dc
);
140 virtual bool Create(const void* data
, long type
, int width
, int height
, int depth
= 1);
141 virtual bool LoadFile(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
142 virtual bool SaveFile(const wxString
& name
, int type
, const wxPalette
*cmap
= NULL
);
144 wxBitmapRefData
*GetBitmapData() const
145 { return (wxBitmapRefData
*)m_refData
; }
147 // raw bitmap access support functions
148 void *GetRawData(wxPixelDataBase
& data
, int bpp
);
149 void UngetRawData(wxPixelDataBase
& data
);
152 wxPalette
* GetPalette() const;
153 void SetPalette(const wxPalette
& palette
);
154 #endif // wxUSE_PALETTE
156 wxMask
*GetMask() const;
157 wxBitmap
GetMaskBitmap() const;
158 void SetMask(wxMask
*mask
);
160 // these functions are internal and shouldn't be used, they risk to
161 // disappear in the future
162 bool HasAlpha() const;
165 // implementation only from now on
166 // -------------------------------
169 void SetHBITMAP(WXHBITMAP bmp
) { SetHandle((WXHANDLE
)bmp
); }
170 WXHBITMAP
GetHBITMAP() const { return (WXHBITMAP
)GetHandle(); }
173 void SetSelectedInto(wxDC
*dc
);
174 wxDC
*GetSelectedInto() const;
175 #endif // __WXDEBUG__
178 virtual wxGDIImageRefData
*CreateData() const;
179 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
181 // creates an uninitialized bitmap, called from Create()s above
182 bool DoCreate(int w
, int h
, int depth
, WXHDC hdc
);
185 // creates the bitmap from wxImage, supposed to be called from ctor
186 bool CreateFromImage(const wxImage
& image
, int depth
);
188 // creates a DDB from wxImage, supposed to be called from ctor
189 bool CreateFromImage(const wxImage
& image
, const wxDC
& dc
);
191 // common part of the 2 methods above (hdc may be 0)
192 bool CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
);
193 #endif // wxUSE_IMAGE
196 // common part of CopyFromIcon/CopyFromCursor for Win32
198 CopyFromIconOrCursor(const wxGDIImage
& icon
,
199 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
);
202 DECLARE_DYNAMIC_CLASS(wxBitmap
)
205 // ----------------------------------------------------------------------------
206 // wxMask: a mono bitmap used for drawing bitmaps transparently.
207 // ----------------------------------------------------------------------------
209 class WXDLLEXPORT wxMask
: public wxObject
215 wxMask(const wxMask
&mask
);
217 // Construct a mask from a bitmap and a colour indicating the transparent
219 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
221 // Construct a mask from a bitmap and a palette index indicating the
223 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
225 // Construct a mask from a mono bitmap (copies the bitmap).
226 wxMask(const wxBitmap
& bitmap
);
228 // construct a mask from the givne bitmap handle
229 wxMask(WXHBITMAP hbmp
) { m_maskBitmap
= hbmp
; }
233 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
234 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
235 bool Create(const wxBitmap
& bitmap
);
238 WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
239 void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
242 WXHBITMAP m_maskBitmap
;
244 DECLARE_DYNAMIC_CLASS(wxMask
)
247 // ----------------------------------------------------------------------------
248 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
249 // ----------------------------------------------------------------------------
251 class WXDLLEXPORT wxBitmapHandler
: public wxGDIImageHandler
254 wxBitmapHandler() { }
255 wxBitmapHandler(const wxString
& name
, const wxString
& ext
, long type
)
256 : wxGDIImageHandler(name
, ext
, type
)
260 // keep wxBitmapHandler derived from wxGDIImageHandler compatible with the
261 // old class which worked only with bitmaps
262 virtual bool Create(wxBitmap
*bitmap
,
265 int width
, int height
, int depth
= 1);
266 virtual bool LoadFile(wxBitmap
*bitmap
,
267 const wxString
& name
,
269 int desiredWidth
, int desiredHeight
);
270 virtual bool SaveFile(wxBitmap
*bitmap
,
271 const wxString
& name
,
273 const wxPalette
*palette
= NULL
);
275 virtual bool Create(wxGDIImage
*image
,
278 int width
, int height
, int depth
= 1);
279 virtual bool Load(wxGDIImage
*image
,
280 const wxString
& name
,
282 int desiredWidth
, int desiredHeight
);
283 virtual bool Save(wxGDIImage
*image
,
284 const wxString
& name
,
288 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)