Fix warnings about implicit float or double to int conversions in wxMSW.
[wxWidgets.git] / include / wx / msw / bitmap.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/bitmap.h
3 // Purpose: wxBitmap class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_BITMAP_H_
13 #define _WX_BITMAP_H_
14
15 #include "wx/msw/gdiimage.h"
16 #include "wx/math.h"
17 #include "wx/palette.h"
18
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;
25 #if wxUSE_WXDIB
26 class WXDLLIMPEXP_FWD_CORE wxDIB;
27 #endif
28 class WXDLLIMPEXP_FWD_CORE wxIcon;
29 class WXDLLIMPEXP_FWD_CORE wxMask;
30 class WXDLLIMPEXP_FWD_CORE wxPalette;
31 class WXDLLIMPEXP_FWD_CORE wxPixelDataBase;
32
33 // What kind of transparency should a bitmap copied from an icon or cursor
34 // have?
35 enum wxBitmapTransparency
36 {
37 wxBitmapTransparency_Auto, // default: copy alpha if the source has it
38 wxBitmapTransparency_None, // never create alpha
39 wxBitmapTransparency_Always // always use alpha
40 };
41
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 // ----------------------------------------------------------------------------
46
47 class WXDLLIMPEXP_CORE wxBitmap : public wxGDIImage,
48 public wxBitmapHelpers
49 {
50 public:
51 // default ctor creates an invalid bitmap, you must Create() it later
52 wxBitmap() { }
53
54 // Initialize with raw data
55 wxBitmap(const char bits[], int width, int height, int depth = 1);
56
57 // Initialize with XPM data
58 wxBitmap(const char* const* data);
59 #ifdef wxNEEDS_CHARPP
60 wxBitmap(char** data)
61 {
62 *this = wxBitmap(const_cast<const char* const*>(data));
63 }
64 #endif
65
66 // Load a file or resource
67 wxBitmap(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
68
69 // New constructor for generalised creation from data
70 wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth = 1);
71
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)
74 //
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); }
79
80 // Create a bitmap compatible with the given DC
81 wxBitmap(int width, int height, const wxDC& dc);
82
83 #if wxUSE_IMAGE
84 // Convert from wxImage
85 wxBitmap(const wxImage& image, int depth = -1)
86 { (void)CreateFromImage(image, depth); }
87
88 // Create a DDB compatible with the given DC from wxImage
89 wxBitmap(const wxImage& image, const wxDC& dc)
90 { (void)CreateFromImage(image, dc); }
91 #endif // wxUSE_IMAGE
92
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)
97 {
98 CopyFromIcon(icon, transp);
99 }
100
101 wxBitmap& operator=(const wxIcon& icon)
102 {
103 (void)CopyFromIcon(icon);
104
105 return *this;
106 }
107
108 wxBitmap& operator=(const wxCursor& cursor)
109 {
110 (void)CopyFromCursor(cursor);
111
112 return *this;
113 }
114
115 virtual ~wxBitmap();
116
117 #if wxUSE_IMAGE
118 wxImage ConvertToImage() const;
119 wxBitmap ConvertToDisabled(unsigned char brightness = 255) const;
120 #endif // wxUSE_IMAGE
121
122 // get the given part of bitmap
123 wxBitmap GetSubBitmap( const wxRect& rect ) const;
124
125 // NB: This should not be called from user code. It is for wx internal
126 // use only.
127 wxBitmap GetSubBitmapOfHDC( const wxRect& rect, WXHDC hdc ) const;
128
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);
132
133 // copies the contents and mask of the given cursor to the bitmap
134 bool CopyFromCursor(const wxCursor& cursor,
135 wxBitmapTransparency transp = wxBitmapTransparency_Auto);
136
137 #if wxUSE_WXDIB
138 // copies from a device independent bitmap
139 bool CopyFromDIB(const wxDIB& dib);
140 #endif
141
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); }
145
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); }
150
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;
153
154 wxBitmapRefData *GetBitmapData() const
155 { return (wxBitmapRefData *)m_refData; }
156
157 // raw bitmap access support functions
158 void *GetRawData(wxPixelDataBase& data, int bpp);
159 void UngetRawData(wxPixelDataBase& data);
160
161 #if wxUSE_PALETTE
162 wxPalette* GetPalette() const;
163 void SetPalette(const wxPalette& palette);
164 #endif // wxUSE_PALETTE
165
166 wxMask *GetMask() const;
167 void SetMask(wxMask *mask);
168
169 // these functions are internal and shouldn't be used, they risk to
170 // disappear in the future
171 bool HasAlpha() const;
172 void UseAlpha();
173
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())); }
180
181 // implementation only from now on
182 // -------------------------------
183
184 public:
185 void SetHBITMAP(WXHBITMAP bmp) { SetHandle((WXHANDLE)bmp); }
186 WXHBITMAP GetHBITMAP() const { return (WXHBITMAP)GetHandle(); }
187
188 void SetSelectedInto(wxDC *dc);
189 wxDC *GetSelectedInto() const;
190
191 protected:
192 virtual wxGDIImageRefData *CreateData() const;
193 virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
194
195 // creates an uninitialized bitmap, called from Create()s above
196 bool DoCreate(int w, int h, int depth, WXHDC hdc);
197
198 #if wxUSE_IMAGE
199 // creates the bitmap from wxImage, supposed to be called from ctor
200 bool CreateFromImage(const wxImage& image, int depth);
201
202 // creates a DDB from wxImage, supposed to be called from ctor
203 bool CreateFromImage(const wxImage& image, const wxDC& dc);
204
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
208
209 private:
210 // common part of CopyFromIcon/CopyFromCursor for Win32
211 bool
212 CopyFromIconOrCursor(const wxGDIImage& icon,
213 wxBitmapTransparency transp = wxBitmapTransparency_Auto);
214
215
216 DECLARE_DYNAMIC_CLASS(wxBitmap)
217 };
218
219 // ----------------------------------------------------------------------------
220 // wxMask: a mono bitmap used for drawing bitmaps transparently.
221 // ----------------------------------------------------------------------------
222
223 class WXDLLIMPEXP_CORE wxMask : public wxObject
224 {
225 public:
226 wxMask();
227
228 // Copy constructor
229 wxMask(const wxMask &mask);
230
231 // Construct a mask from a bitmap and a colour indicating the transparent
232 // area
233 wxMask(const wxBitmap& bitmap, const wxColour& colour);
234
235 // Construct a mask from a bitmap and a palette index indicating the
236 // transparent area
237 wxMask(const wxBitmap& bitmap, int paletteIndex);
238
239 // Construct a mask from a mono bitmap (copies the bitmap).
240 wxMask(const wxBitmap& bitmap);
241
242 // construct a mask from the givne bitmap handle
243 wxMask(WXHBITMAP hbmp) { m_maskBitmap = hbmp; }
244
245 virtual ~wxMask();
246
247 bool Create(const wxBitmap& bitmap, const wxColour& colour);
248 bool Create(const wxBitmap& bitmap, int paletteIndex);
249 bool Create(const wxBitmap& bitmap);
250
251 wxBitmap GetBitmap() const;
252
253 // Implementation
254 WXHBITMAP GetMaskBitmap() const { return m_maskBitmap; }
255 void SetMaskBitmap(WXHBITMAP bmp) { m_maskBitmap = bmp; }
256
257 protected:
258 WXHBITMAP m_maskBitmap;
259
260 DECLARE_DYNAMIC_CLASS(wxMask)
261 };
262
263
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 // ----------------------------------------------------------------------------
268
269 class WXDLLIMPEXP_CORE wxBitmapHandler : public wxGDIImageHandler
270 {
271 public:
272 wxBitmapHandler() { }
273 wxBitmapHandler(const wxString& name, const wxString& ext, wxBitmapType type)
274 : wxGDIImageHandler(name, ext, type) { }
275
276 // implement wxGDIImageHandler's pure virtuals:
277
278 virtual bool Create(wxGDIImage *image,
279 const void* data,
280 wxBitmapType type,
281 int width, int height, int depth = 1);
282 virtual bool Load(wxGDIImage *image,
283 const wxString& name,
284 wxBitmapType type,
285 int desiredWidth, int desiredHeight);
286 virtual bool Save(const wxGDIImage *image,
287 const wxString& name,
288 wxBitmapType type) const;
289
290
291 // make wxBitmapHandler compatible with the wxBitmapHandler interface
292 // declared in bitmap.h, even if it's derived from wxGDIImageHandler:
293
294 virtual bool Create(wxBitmap *bitmap,
295 const void* data,
296 wxBitmapType type,
297 int width, int height, int depth = 1);
298 virtual bool LoadFile(wxBitmap *bitmap,
299 const wxString& name,
300 wxBitmapType type,
301 int desiredWidth, int desiredHeight);
302 virtual bool SaveFile(const wxBitmap *bitmap,
303 const wxString& name,
304 wxBitmapType type,
305 const wxPalette *palette = NULL) const;
306
307 private:
308 DECLARE_DYNAMIC_CLASS(wxBitmapHandler)
309 };
310
311 #endif
312 // _WX_BITMAP_H_