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"
17 #include "wx/palette.h"
19 class WXDLLIMPEXP_FWD_CORE wxBitmap
;
20 class WXDLLIMPEXP_FWD_CORE wxBitmapHandler
;
21 class WXDLLIMPEXP_FWD_CORE wxBitmapRefData
;
22 class WXDLLIMPEXP_FWD_CORE wxControl
;
23 class WXDLLIMPEXP_FWD_CORE wxCursor
;
24 class WXDLLIMPEXP_FWD_CORE wxDC
;
26 class WXDLLIMPEXP_FWD_CORE wxDIB
;
28 class WXDLLIMPEXP_FWD_CORE wxIcon
;
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 // NOTE: for wxMSW we don't use the wxBitmapBase base class declared in bitmap.h!
45 // ----------------------------------------------------------------------------
47 class WXDLLIMPEXP_CORE wxBitmap
: public wxGDIImage
,
48 public wxBitmapHelpers
51 // default ctor creates an invalid bitmap, you must Create() it later
54 // Initialize with raw data
55 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
57 // Initialize with XPM data
58 wxBitmap(const char* const* data
);
62 *this = wxBitmap(const_cast<const char* const*>(data
));
66 // Load a file or resource
67 wxBitmap(const wxString
& name
, wxBitmapType type
= wxBITMAP_DEFAULT_TYPE
);
69 // New constructor for generalised creation from data
70 wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
= 1);
72 // Create a new, uninitialized bitmap of the given size and depth (if it
73 // is omitted, will create a bitmap compatible with the display)
75 // NB: this ctor will create a DIB for 24 and 32bpp bitmaps, use ctor
76 // taking a DC argument if you want to force using DDB in this case
77 wxBitmap(int width
, int height
, int depth
= -1) { (void)Create(width
, height
, depth
); }
78 wxBitmap(const wxSize
& sz
, int depth
= -1) { (void)Create(sz
, depth
); }
80 // Create a bitmap compatible with the given DC
81 wxBitmap(int width
, int height
, const wxDC
& dc
);
84 // Convert from wxImage
85 wxBitmap(const wxImage
& image
, int depth
= -1)
86 { (void)CreateFromImage(image
, depth
); }
88 // Create a DDB compatible with the given DC from wxImage
89 wxBitmap(const wxImage
& image
, const wxDC
& dc
)
90 { (void)CreateFromImage(image
, dc
); }
93 // we must have this, otherwise icons are silently copied into bitmaps using
94 // the copy ctor but the resulting bitmap is invalid!
95 wxBitmap(const wxIcon
& icon
,
96 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
)
98 CopyFromIcon(icon
, transp
);
101 wxBitmap
& operator=(const wxIcon
& icon
)
103 (void)CopyFromIcon(icon
);
108 wxBitmap
& operator=(const wxCursor
& cursor
)
110 (void)CopyFromCursor(cursor
);
118 wxImage
ConvertToImage() const;
119 wxBitmap
ConvertToDisabled(unsigned char brightness
= 255) const;
120 #endif // wxUSE_IMAGE
122 // get the given part of bitmap
123 wxBitmap
GetSubBitmap( const wxRect
& rect
) const;
125 // NB: This should not be called from user code. It is for wx internal
127 wxBitmap
GetSubBitmapOfHDC( const wxRect
& rect
, WXHDC hdc
) const;
129 // copies the contents and mask of the given (colour) icon to the bitmap
130 bool CopyFromIcon(const wxIcon
& icon
,
131 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
);
133 // copies the contents and mask of the given cursor to the bitmap
134 bool CopyFromCursor(const wxCursor
& cursor
,
135 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
);
138 // copies from a device independent bitmap
139 bool CopyFromDIB(const wxDIB
& dib
);
142 virtual bool Create(int width
, int height
, int depth
= wxBITMAP_SCREEN_DEPTH
);
143 virtual bool Create(const wxSize
& sz
, int depth
= wxBITMAP_SCREEN_DEPTH
)
144 { return Create(sz
.GetWidth(), sz
.GetHeight(), depth
); }
146 virtual bool Create(int width
, int height
, const wxDC
& dc
);
147 virtual bool Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
= 1);
148 virtual bool CreateScaled(int w
, int h
, int d
, double logicalScale
)
149 { return Create(wxRound(w
*logicalScale
), wxRound(h
*logicalScale
), d
); }
151 virtual bool LoadFile(const wxString
& name
, wxBitmapType type
= wxBITMAP_DEFAULT_TYPE
);
152 virtual bool SaveFile(const wxString
& name
, wxBitmapType type
, const wxPalette
*cmap
= NULL
) const;
154 wxBitmapRefData
*GetBitmapData() const
155 { return (wxBitmapRefData
*)m_refData
; }
157 // raw bitmap access support functions
158 void *GetRawData(wxPixelDataBase
& data
, int bpp
);
159 void UngetRawData(wxPixelDataBase
& data
);
162 wxPalette
* GetPalette() const;
163 void SetPalette(const wxPalette
& palette
);
164 #endif // wxUSE_PALETTE
166 wxMask
*GetMask() const;
167 void SetMask(wxMask
*mask
);
169 // these functions are internal and shouldn't be used, they risk to
170 // disappear in the future
171 bool HasAlpha() const;
174 // support for scaled bitmaps
175 virtual double GetScaleFactor() const { return 1.0; }
176 virtual double GetScaledWidth() const { return GetWidth() / GetScaleFactor(); }
177 virtual double GetScaledHeight() const { return GetHeight() / GetScaleFactor(); }
178 virtual wxSize
GetScaledSize() const
179 { return wxSize(wxRound(GetScaledWidth()), wxRound(GetScaledHeight())); }
181 // implementation only from now on
182 // -------------------------------
185 void SetHBITMAP(WXHBITMAP bmp
) { SetHandle((WXHANDLE
)bmp
); }
186 WXHBITMAP
GetHBITMAP() const { return (WXHBITMAP
)GetHandle(); }
188 void SetSelectedInto(wxDC
*dc
);
189 wxDC
*GetSelectedInto() const;
192 virtual wxGDIImageRefData
*CreateData() const;
193 virtual wxGDIRefData
*CloneGDIRefData(const wxGDIRefData
*data
) const;
195 // creates an uninitialized bitmap, called from Create()s above
196 bool DoCreate(int w
, int h
, int depth
, WXHDC hdc
);
199 // creates the bitmap from wxImage, supposed to be called from ctor
200 bool CreateFromImage(const wxImage
& image
, int depth
);
202 // creates a DDB from wxImage, supposed to be called from ctor
203 bool CreateFromImage(const wxImage
& image
, const wxDC
& dc
);
205 // common part of the 2 methods above (hdc may be 0)
206 bool CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
);
207 #endif // wxUSE_IMAGE
210 // common part of CopyFromIcon/CopyFromCursor for Win32
212 CopyFromIconOrCursor(const wxGDIImage
& icon
,
213 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
);
216 DECLARE_DYNAMIC_CLASS(wxBitmap
)
219 // ----------------------------------------------------------------------------
220 // wxMask: a mono bitmap used for drawing bitmaps transparently.
221 // ----------------------------------------------------------------------------
223 class WXDLLIMPEXP_CORE wxMask
: public wxObject
229 wxMask(const wxMask
&mask
);
231 // Construct a mask from a bitmap and a colour indicating the transparent
233 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
235 // Construct a mask from a bitmap and a palette index indicating the
237 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
239 // Construct a mask from a mono bitmap (copies the bitmap).
240 wxMask(const wxBitmap
& bitmap
);
242 // construct a mask from the givne bitmap handle
243 wxMask(WXHBITMAP hbmp
) { m_maskBitmap
= hbmp
; }
247 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
248 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
249 bool Create(const wxBitmap
& bitmap
);
251 wxBitmap
GetBitmap() const;
254 WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
255 void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
258 WXHBITMAP m_maskBitmap
;
260 DECLARE_DYNAMIC_CLASS(wxMask
)
264 // ----------------------------------------------------------------------------
265 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
266 // NOTE: for wxMSW we don't use the wxBitmapHandler class declared in bitmap.h!
267 // ----------------------------------------------------------------------------
269 class WXDLLIMPEXP_CORE wxBitmapHandler
: public wxGDIImageHandler
272 wxBitmapHandler() { }
273 wxBitmapHandler(const wxString
& name
, const wxString
& ext
, wxBitmapType type
)
274 : wxGDIImageHandler(name
, ext
, type
) { }
276 // implement wxGDIImageHandler's pure virtuals:
278 virtual bool Create(wxGDIImage
*image
,
281 int width
, int height
, int depth
= 1);
282 virtual bool Load(wxGDIImage
*image
,
283 const wxString
& name
,
285 int desiredWidth
, int desiredHeight
);
286 virtual bool Save(const wxGDIImage
*image
,
287 const wxString
& name
,
288 wxBitmapType type
) const;
291 // make wxBitmapHandler compatible with the wxBitmapHandler interface
292 // declared in bitmap.h, even if it's derived from wxGDIImageHandler:
294 virtual bool Create(wxBitmap
*bitmap
,
297 int width
, int height
, int depth
= 1);
298 virtual bool LoadFile(wxBitmap
*bitmap
,
299 const wxString
& name
,
301 int desiredWidth
, int desiredHeight
);
302 virtual bool SaveFile(const wxBitmap
*bitmap
,
303 const wxString
& name
,
305 const wxPalette
*palette
= NULL
) const;
308 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)