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 wxMask
;
29 class WXDLLIMPEXP_FWD_CORE wxPalette
;
30 class WXDLLIMPEXP_FWD_CORE wxPixelDataBase
;
32 // What kind of transparency should a bitmap copied from an icon or cursor
34 enum wxBitmapTransparency
36 wxBitmapTransparency_Auto
, // default: copy alpha if the source has it
37 wxBitmapTransparency_None
, // never create alpha
38 wxBitmapTransparency_Always
// always use alpha
41 // ----------------------------------------------------------------------------
42 // wxBitmap: a mono or colour bitmap
43 // NOTE: for wxMSW we don't use the wxBitmapBase base class declared in bitmap.h!
44 // ----------------------------------------------------------------------------
46 class WXDLLIMPEXP_CORE wxBitmap
: public wxGDIImage
,
47 public wxBitmapHelpers
50 // default ctor creates an invalid bitmap, you must Create() it later
53 // Initialize with raw data
54 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
56 // Initialize with XPM data
57 wxBitmap(const char* const* data
);
61 *this = wxBitmap(const_cast<const char* const*>(data
));
65 // Load a file or resource
66 wxBitmap(const wxString
& name
, wxBitmapType type
= wxBITMAP_DEFAULT_TYPE
);
68 // New constructor for generalised creation from data
69 wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
= 1);
71 // Create a new, uninitialized bitmap of the given size and depth (if it
72 // is omitted, will create a bitmap compatible with the display)
74 // NB: this ctor will create a DIB for 24 and 32bpp bitmaps, use ctor
75 // taking a DC argument if you want to force using DDB in this case
76 wxBitmap(int width
, int height
, int depth
= -1) { (void)Create(width
, height
, depth
); }
77 wxBitmap(const wxSize
& sz
, int depth
= -1) { (void)Create(sz
, depth
); }
79 // Create a bitmap compatible with the given DC
80 wxBitmap(int width
, int height
, const wxDC
& dc
);
83 // Convert from wxImage
84 wxBitmap(const wxImage
& image
, int depth
= -1)
85 { (void)CreateFromImage(image
, depth
); }
87 // Create a DDB compatible with the given DC from wxImage
88 wxBitmap(const wxImage
& image
, const wxDC
& dc
)
89 { (void)CreateFromImage(image
, dc
); }
92 // we must have this, otherwise icons are silently copied into bitmaps using
93 // the copy ctor but the resulting bitmap is invalid!
94 wxBitmap(const wxIcon
& icon
,
95 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
)
97 CopyFromIcon(icon
, transp
);
100 wxBitmap
& operator=(const wxIcon
& icon
)
102 (void)CopyFromIcon(icon
);
107 wxBitmap
& operator=(const wxCursor
& cursor
)
109 (void)CopyFromCursor(cursor
);
117 wxImage
ConvertToImage() const;
118 wxBitmap
ConvertToDisabled(unsigned char brightness
= 255) const;
119 #endif // wxUSE_IMAGE
121 // get the given part of bitmap
122 wxBitmap
GetSubBitmap( const wxRect
& rect
) const;
124 // NB: This should not be called from user code. It is for wx internal
126 wxBitmap
GetSubBitmapOfHDC( const wxRect
& rect
, WXHDC hdc
) const;
128 // copies the contents and mask of the given (colour) icon to the bitmap
129 bool CopyFromIcon(const wxIcon
& icon
,
130 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
);
132 // copies the contents and mask of the given cursor to the bitmap
133 bool CopyFromCursor(const wxCursor
& cursor
,
134 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
);
137 // copies from a device independent bitmap
138 bool CopyFromDIB(const wxDIB
& dib
);
141 virtual bool Create(int width
, int height
, int depth
= wxBITMAP_SCREEN_DEPTH
);
142 virtual bool Create(const wxSize
& sz
, int depth
= wxBITMAP_SCREEN_DEPTH
)
143 { return Create(sz
.GetWidth(), sz
.GetHeight(), depth
); }
145 virtual bool Create(int width
, int height
, const wxDC
& dc
);
146 virtual bool Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
= 1);
147 virtual bool CreateScaled(int w
, int h
, int d
, double logicalScale
)
148 { return Create(w
*logicalScale
,h
*logicalScale
,d
); }
150 virtual bool LoadFile(const wxString
& name
, wxBitmapType type
= wxBITMAP_DEFAULT_TYPE
);
151 virtual bool SaveFile(const wxString
& name
, wxBitmapType type
, const wxPalette
*cmap
= NULL
) const;
153 wxBitmapRefData
*GetBitmapData() const
154 { return (wxBitmapRefData
*)m_refData
; }
156 // raw bitmap access support functions
157 void *GetRawData(wxPixelDataBase
& data
, int bpp
);
158 void UngetRawData(wxPixelDataBase
& data
);
161 wxPalette
* GetPalette() const;
162 void SetPalette(const wxPalette
& palette
);
163 #endif // wxUSE_PALETTE
165 wxMask
*GetMask() const;
166 void SetMask(wxMask
*mask
);
168 // these functions are internal and shouldn't be used, they risk to
169 // disappear in the future
170 bool HasAlpha() const;
173 // support for scaled bitmaps
174 virtual double GetScaleFactor() const { return 1.0; }
175 virtual double GetScaledWidth() const { return GetWidth() / GetScaleFactor(); }
176 virtual double GetScaledHeight() const { return GetHeight() / GetScaleFactor(); }
177 virtual wxSize
GetScaledSize() const
178 { return wxSize(GetScaledWidth(), GetScaledHeight()); }
180 // implementation only from now on
181 // -------------------------------
184 void SetHBITMAP(WXHBITMAP bmp
) { SetHandle((WXHANDLE
)bmp
); }
185 WXHBITMAP
GetHBITMAP() const { return (WXHBITMAP
)GetHandle(); }
187 void SetSelectedInto(wxDC
*dc
);
188 wxDC
*GetSelectedInto() const;
191 virtual wxGDIImageRefData
*CreateData() const;
192 virtual wxGDIRefData
*CloneGDIRefData(const wxGDIRefData
*data
) const;
194 // creates an uninitialized bitmap, called from Create()s above
195 bool DoCreate(int w
, int h
, int depth
, WXHDC hdc
);
198 // creates the bitmap from wxImage, supposed to be called from ctor
199 bool CreateFromImage(const wxImage
& image
, int depth
);
201 // creates a DDB from wxImage, supposed to be called from ctor
202 bool CreateFromImage(const wxImage
& image
, const wxDC
& dc
);
204 // common part of the 2 methods above (hdc may be 0)
205 bool CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
);
206 #endif // wxUSE_IMAGE
209 // common part of CopyFromIcon/CopyFromCursor for Win32
211 CopyFromIconOrCursor(const wxGDIImage
& icon
,
212 wxBitmapTransparency transp
= wxBitmapTransparency_Auto
);
215 DECLARE_DYNAMIC_CLASS(wxBitmap
)
218 // ----------------------------------------------------------------------------
219 // wxMask: a mono bitmap used for drawing bitmaps transparently.
220 // ----------------------------------------------------------------------------
222 class WXDLLIMPEXP_CORE wxMask
: public wxObject
228 wxMask(const wxMask
&mask
);
230 // Construct a mask from a bitmap and a colour indicating the transparent
232 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
234 // Construct a mask from a bitmap and a palette index indicating the
236 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
238 // Construct a mask from a mono bitmap (copies the bitmap).
239 wxMask(const wxBitmap
& bitmap
);
241 // construct a mask from the givne bitmap handle
242 wxMask(WXHBITMAP hbmp
) { m_maskBitmap
= hbmp
; }
246 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
247 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
248 bool Create(const wxBitmap
& bitmap
);
250 wxBitmap
GetBitmap() const;
253 WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
254 void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
257 WXHBITMAP m_maskBitmap
;
259 DECLARE_DYNAMIC_CLASS(wxMask
)
263 // ----------------------------------------------------------------------------
264 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
265 // NOTE: for wxMSW we don't use the wxBitmapHandler class declared in bitmap.h!
266 // ----------------------------------------------------------------------------
268 class WXDLLIMPEXP_CORE wxBitmapHandler
: public wxGDIImageHandler
271 wxBitmapHandler() { }
272 wxBitmapHandler(const wxString
& name
, const wxString
& ext
, wxBitmapType type
)
273 : wxGDIImageHandler(name
, ext
, type
) { }
275 // implement wxGDIImageHandler's pure virtuals:
277 virtual bool Create(wxGDIImage
*image
,
280 int width
, int height
, int depth
= 1);
281 virtual bool Load(wxGDIImage
*image
,
282 const wxString
& name
,
284 int desiredWidth
, int desiredHeight
);
285 virtual bool Save(const wxGDIImage
*image
,
286 const wxString
& name
,
287 wxBitmapType type
) const;
290 // make wxBitmapHandler compatible with the wxBitmapHandler interface
291 // declared in bitmap.h, even if it's derived from wxGDIImageHandler:
293 virtual bool Create(wxBitmap
*bitmap
,
296 int width
, int height
, int depth
= 1);
297 virtual bool LoadFile(wxBitmap
*bitmap
,
298 const wxString
& name
,
300 int desiredWidth
, int desiredHeight
);
301 virtual bool SaveFile(const wxBitmap
*bitmap
,
302 const wxString
& name
,
304 const wxPalette
*palette
= NULL
) const;
307 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)