add wxSize overloads to wxBitmap ctors and to wxBitmap::Create
[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/palette.h"
17
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;
24 #if wxUSE_WXDIB
25 class WXDLLIMPEXP_FWD_CORE wxDIB;
26 #endif
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;
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 {
49 public:
50 // default ctor creates an invalid bitmap, you must Create() it later
51 wxBitmap() { }
52
53 // Initialize with raw data
54 wxBitmap(const char bits[], int width, int height, int depth = 1);
55
56 // Initialize with XPM data
57 wxBitmap(const char* const* data);
58 #ifdef wxNEEDS_CHARPP
59 wxBitmap(char** data)
60 {
61 *this = wxBitmap(const_cast<const char* const*>(data));
62 }
63 #endif
64
65 // Load a file or resource
66 wxBitmap(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
67
68 // New constructor for generalised creation from data
69 wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth = 1);
70
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)
73 //
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); }
78
79 // Create a bitmap compatible with the given DC
80 wxBitmap(int width, int height, const wxDC& dc);
81
82 #if wxUSE_IMAGE
83 // Convert from wxImage
84 wxBitmap(const wxImage& image, int depth = -1)
85 { (void)CreateFromImage(image, depth); }
86
87 // Create a DDB compatible with the given DC from wxImage
88 wxBitmap(const wxImage& image, const wxDC& dc)
89 { (void)CreateFromImage(image, dc); }
90 #endif // wxUSE_IMAGE
91
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)
96 {
97 CopyFromIcon(icon, transp);
98 }
99
100 wxBitmap& operator=(const wxIcon& icon)
101 {
102 (void)CopyFromIcon(icon);
103
104 return *this;
105 }
106
107 wxBitmap& operator=(const wxCursor& cursor)
108 {
109 (void)CopyFromCursor(cursor);
110
111 return *this;
112 }
113
114 virtual ~wxBitmap();
115
116 #if wxUSE_IMAGE
117 wxImage ConvertToImage() const;
118 #endif // wxUSE_IMAGE
119
120 // get the given part of bitmap
121 wxBitmap GetSubBitmap( const wxRect& rect ) const;
122
123 // NB: This should not be called from user code. It is for wx internal
124 // use only.
125 wxBitmap GetSubBitmapOfHDC( const wxRect& rect, WXHDC hdc ) const;
126
127 // copies the contents and mask of the given (colour) icon to the bitmap
128 bool CopyFromIcon(const wxIcon& icon,
129 wxBitmapTransparency transp = wxBitmapTransparency_Auto);
130
131 // copies the contents and mask of the given cursor to the bitmap
132 bool CopyFromCursor(const wxCursor& cursor,
133 wxBitmapTransparency transp = wxBitmapTransparency_Auto);
134
135 #if wxUSE_WXDIB
136 // copies from a device independent bitmap
137 bool CopyFromDIB(const wxDIB& dib);
138 #endif
139
140 virtual bool Create(int width, int height, int depth = wxBITMAP_SCREEN_DEPTH);
141 virtual bool Create(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH)
142 { return Create(sz.GetWidth(), sz.GetHeight(), depth); }
143
144 virtual bool Create(int width, int height, const wxDC& dc);
145 virtual bool Create(const void* data, wxBitmapType type, int width, int height, int depth = 1);
146 virtual bool LoadFile(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
147 virtual bool SaveFile(const wxString& name, wxBitmapType type, const wxPalette *cmap = NULL) const;
148
149 wxBitmapRefData *GetBitmapData() const
150 { return (wxBitmapRefData *)m_refData; }
151
152 // raw bitmap access support functions
153 void *GetRawData(wxPixelDataBase& data, int bpp);
154 void UngetRawData(wxPixelDataBase& data);
155
156 #if wxUSE_PALETTE
157 wxPalette* GetPalette() const;
158 void SetPalette(const wxPalette& palette);
159 #endif // wxUSE_PALETTE
160
161 wxMask *GetMask() const;
162 wxBitmap GetMaskBitmap() const;
163 void SetMask(wxMask *mask);
164
165 // these functions are internal and shouldn't be used, they risk to
166 // disappear in the future
167 bool HasAlpha() const;
168 void UseAlpha();
169
170 // implementation only from now on
171 // -------------------------------
172
173 public:
174 void SetHBITMAP(WXHBITMAP bmp) { SetHandle((WXHANDLE)bmp); }
175 WXHBITMAP GetHBITMAP() const { return (WXHBITMAP)GetHandle(); }
176
177 #ifdef __WXDEBUG__
178 void SetSelectedInto(wxDC *dc);
179 wxDC *GetSelectedInto() const;
180 #endif // __WXDEBUG__
181
182 protected:
183 virtual wxGDIImageRefData *CreateData() const;
184 virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
185
186 // creates an uninitialized bitmap, called from Create()s above
187 bool DoCreate(int w, int h, int depth, WXHDC hdc);
188
189 #if wxUSE_IMAGE
190 // creates the bitmap from wxImage, supposed to be called from ctor
191 bool CreateFromImage(const wxImage& image, int depth);
192
193 // creates a DDB from wxImage, supposed to be called from ctor
194 bool CreateFromImage(const wxImage& image, const wxDC& dc);
195
196 // common part of the 2 methods above (hdc may be 0)
197 bool CreateFromImage(const wxImage& image, int depth, WXHDC hdc);
198 #endif // wxUSE_IMAGE
199
200 private:
201 // common part of CopyFromIcon/CopyFromCursor for Win32
202 bool
203 CopyFromIconOrCursor(const wxGDIImage& icon,
204 wxBitmapTransparency transp = wxBitmapTransparency_Auto);
205
206
207 DECLARE_DYNAMIC_CLASS(wxBitmap)
208 };
209
210 // ----------------------------------------------------------------------------
211 // wxMask: a mono bitmap used for drawing bitmaps transparently.
212 // ----------------------------------------------------------------------------
213
214 class WXDLLIMPEXP_CORE wxMask : public wxObject
215 {
216 public:
217 wxMask();
218
219 // Copy constructor
220 wxMask(const wxMask &mask);
221
222 // Construct a mask from a bitmap and a colour indicating the transparent
223 // area
224 wxMask(const wxBitmap& bitmap, const wxColour& colour);
225
226 // Construct a mask from a bitmap and a palette index indicating the
227 // transparent area
228 wxMask(const wxBitmap& bitmap, int paletteIndex);
229
230 // Construct a mask from a mono bitmap (copies the bitmap).
231 wxMask(const wxBitmap& bitmap);
232
233 // construct a mask from the givne bitmap handle
234 wxMask(WXHBITMAP hbmp) { m_maskBitmap = hbmp; }
235
236 virtual ~wxMask();
237
238 bool Create(const wxBitmap& bitmap, const wxColour& colour);
239 bool Create(const wxBitmap& bitmap, int paletteIndex);
240 bool Create(const wxBitmap& bitmap);
241
242 // Implementation
243 WXHBITMAP GetMaskBitmap() const { return m_maskBitmap; }
244 void SetMaskBitmap(WXHBITMAP bmp) { m_maskBitmap = bmp; }
245
246 protected:
247 WXHBITMAP m_maskBitmap;
248
249 DECLARE_DYNAMIC_CLASS(wxMask)
250 };
251
252
253 // ----------------------------------------------------------------------------
254 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
255 // NOTE: for wxMSW we don't use the wxBitmapHandler class declared in bitmap.h!
256 // ----------------------------------------------------------------------------
257
258 class WXDLLIMPEXP_CORE wxBitmapHandler : public wxGDIImageHandler
259 {
260 public:
261 wxBitmapHandler() { }
262 wxBitmapHandler(const wxString& name, const wxString& ext, wxBitmapType type)
263 : wxGDIImageHandler(name, ext, type) { }
264
265 // implement wxGDIImageHandler's pure virtuals:
266
267 virtual bool Create(wxGDIImage *image,
268 const void* data,
269 wxBitmapType type,
270 int width, int height, int depth = 1);
271 virtual bool Load(wxGDIImage *image,
272 const wxString& name,
273 wxBitmapType type,
274 int desiredWidth, int desiredHeight);
275 virtual bool Save(const wxGDIImage *image,
276 const wxString& name,
277 wxBitmapType type) const;
278
279
280 // make wxBitmapHandler compatible with the wxBitmapHandler interface
281 // declared in bitmap.h, even if it's derived from wxGDIImageHandler:
282
283 virtual bool Create(wxBitmap *bitmap,
284 const void* data,
285 wxBitmapType type,
286 int width, int height, int depth = 1);
287 virtual bool LoadFile(wxBitmap *bitmap,
288 const wxString& name,
289 wxBitmapType type,
290 int desiredWidth, int desiredHeight);
291 virtual bool SaveFile(const wxBitmap *bitmap,
292 const wxString& name,
293 wxBitmapType type,
294 const wxPalette *palette = NULL) const;
295
296 private:
297 DECLARE_DYNAMIC_CLASS(wxBitmapHandler)
298 };
299
300 #endif
301 // _WX_BITMAP_H_