added CopyFromDIB()
[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 #ifdef __GNUG__
16 #pragma interface "bitmap.h"
17 #endif
18
19 #include "wx/msw/gdiimage.h"
20 #include "wx/gdicmn.h"
21 #include "wx/palette.h"
22
23 class WXDLLEXPORT wxBitmap;
24 class WXDLLEXPORT wxBitmapHandler;
25 class WXDLLEXPORT wxBitmapRefData;
26 class WXDLLEXPORT wxControl;
27 class WXDLLEXPORT wxCursor;
28 class WXDLLEXPORT wxDC;
29 class WXDLLEXPORT wxDIB;
30 class WXDLLEXPORT wxIcon;
31 class WXDLLEXPORT wxImage;
32 class WXDLLEXPORT wxMask;
33 class WXDLLEXPORT wxPalette;
34 class WXDLLEXPORT wxRawBitmapData;
35
36 // ----------------------------------------------------------------------------
37 // wxBitmap: a mono or colour bitmap
38 // ----------------------------------------------------------------------------
39
40 class WXDLLEXPORT wxBitmap : public wxGDIImage
41 {
42 public:
43 // default ctor creates an invalid bitmap, you must Create() it later
44 wxBitmap() { Init(); }
45
46 // Copy constructors
47 wxBitmap(const wxBitmap& bitmap) { Init(); Ref(bitmap); }
48
49 // Initialize with raw data
50 wxBitmap(const char bits[], int width, int height, int depth = 1);
51
52 // Initialize with XPM data
53 wxBitmap(const char **data) { CreateFromXpm(data); }
54 wxBitmap(char **data) { CreateFromXpm((const char **)data); }
55
56 // Load a file or resource
57 wxBitmap(const wxString& name, wxBitmapType type = wxBITMAP_TYPE_BMP_RESOURCE);
58
59 // New constructor for generalised creation from data
60 wxBitmap(void *data, long type, int width, int height, int depth = 1);
61
62 // Create a new, uninitialized bitmap of the given size and depth (if it
63 // is omitted, will create a bitmap compatible with the display)
64 //
65 // NB: this ctor will create a DIB for 24 and 32bpp bitmaps, use ctor
66 // taking a DC argument if you want to force using DDB in this case
67 wxBitmap(int width, int height, int depth = -1);
68
69 // Create a bitmap compatible with the given DC
70 wxBitmap(int width, int height, const wxDC& dc);
71
72 #if wxUSE_IMAGE
73 // Convert from wxImage
74 wxBitmap(const wxImage& image, int depth = -1)
75 { (void)CreateFromImage(image, depth); }
76
77 // Create a DDB compatible with the given DC from wxImage
78 wxBitmap(const wxImage& image, const wxDC& dc)
79 { (void)CreateFromImage(image, dc); }
80 #endif // wxUSE_IMAGE
81
82 // we must have this, otherwise icons are silently copied into bitmaps using
83 // the copy ctor but the resulting bitmap is invalid!
84 wxBitmap(const wxIcon& icon) { Init(); CopyFromIcon(icon); }
85
86 wxBitmap& operator=(const wxBitmap& bitmap)
87 {
88 if ( m_refData != bitmap.m_refData )
89 Ref(bitmap);
90 return *this;
91 }
92
93 wxBitmap& operator=(const wxIcon& icon)
94 {
95 (void)CopyFromIcon(icon);
96
97 return *this;
98 }
99
100 wxBitmap& operator=(const wxCursor& cursor)
101 {
102 (void)CopyFromCursor(cursor);
103
104 return *this;
105 }
106
107 virtual ~wxBitmap();
108
109 #if wxUSE_IMAGE
110 wxImage ConvertToImage() const;
111 #endif // wxUSE_IMAGE
112
113 // get the given part of bitmap
114 wxBitmap GetSubBitmap( const wxRect& rect ) const;
115
116 // copies the contents and mask of the given (colour) icon to the bitmap
117 bool CopyFromIcon(const wxIcon& icon);
118
119 // copies the contents and mask of the given cursor to the bitmap
120 bool CopyFromCursor(const wxCursor& cursor);
121
122 // copies from a device independent bitmap
123 bool CopyFromDIB(const wxDIB& dib);
124
125 virtual bool Create(int width, int height, int depth = -1);
126 virtual bool Create(int width, int height, const wxDC& dc);
127 virtual bool Create(void *data, long type, int width, int height, int depth = 1);
128 virtual bool LoadFile(const wxString& name, long type = wxBITMAP_TYPE_BMP_RESOURCE);
129 virtual bool SaveFile(const wxString& name, int type, const wxPalette *cmap = NULL);
130
131 wxBitmapRefData *GetBitmapData() const
132 { return (wxBitmapRefData *)m_refData; }
133
134 // raw bitmap access support functions
135 bool GetRawData(wxRawBitmapData *data);
136 void UngetRawData(wxRawBitmapData *) { /* nothing to do here */ }
137
138 #if wxUSE_PALETTE
139 wxPalette* GetPalette() const;
140 void SetPalette(const wxPalette& palette);
141 #endif // wxUSE_PALETTE
142
143 wxMask *GetMask() const;
144 void SetMask(wxMask *mask);
145
146 bool operator==(const wxBitmap& bitmap) const { return m_refData == bitmap.m_refData; }
147 bool operator!=(const wxBitmap& bitmap) const { return m_refData != bitmap.m_refData; }
148
149 // this function is internal and shouldn't be used, it risks to disappear
150 bool HasAlpha() const;
151
152 #if WXWIN_COMPATIBILITY_2_4
153 // these functions do nothing and are only there for backwards
154 // compatibility
155 wxDEPRECATED( int GetQuality() const );
156 wxDEPRECATED( void SetQuality(int quality) );
157 #endif // WXWIN_COMPATIBILITY_2_4
158
159 #if WXWIN_COMPATIBILITY_2
160 void SetOk(bool isOk);
161 #endif // WXWIN_COMPATIBILITY_2
162
163 #if WXWIN_COMPATIBILITY
164 #if wxUSE_PALETTE
165 wxPalette *GetColourMap() const { return GetPalette(); }
166 void SetColourMap(wxPalette *cmap) { SetPalette(*cmap); };
167 #endif // wxUSE_PALETTE
168 #endif // WXWIN_COMPATIBILITY
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 // Creates a bitmap that matches the device context's depth, from an
183 // arbitray bitmap. At present, the original bitmap must have an associated
184 // palette. (TODO: use a default palette if no palette exists.) This
185 // function is necessary for you to Blit an arbitrary bitmap (which may
186 // have the wrong depth). wxDC::SelectObject will compare the depth of the
187 // bitmap with the DC's depth, and create a new bitmap if the depths
188 // differ. Eventually we should perhaps make this a public API function so
189 // that an app can efficiently produce bitmaps of the correct depth. The
190 // Windows solution is to use SetDibBits to blit an arbotrary DIB directly
191 // to a DC, but this is too Windows-specific, hence this solution of
192 // quietly converting the wxBitmap. Contributed by Frederic Villeneuve
193 // <frederic.villeneuve@natinst.com>
194 wxBitmap GetBitmapForDC(wxDC& dc) const;
195
196 protected:
197 // common part of all ctors
198 void Init();
199
200 virtual wxGDIImageRefData *CreateData() const;
201
202 // creates the bitmap from XPM data, supposed to be called from ctor
203 bool CreateFromXpm(const char **bits);
204
205 // creates an uninitialized bitmap, called from Create()s above
206 bool DoCreate(int w, int h, int depth, WXHDC hdc);
207
208 #if wxUSE_IMAGE
209 // creates the bitmap from wxImage, supposed to be called from ctor
210 bool CreateFromImage(const wxImage& image, int depth);
211
212 // creates a DDB from wxImage, supposed to be called from ctor
213 bool CreateFromImage(const wxImage& image, const wxDC& dc);
214
215 // common part of the 2 methods above (hdc may be 0)
216 bool CreateFromImage(const wxImage& image, int depth, WXHDC hdc);
217 #endif // wxUSE_IMAGE
218
219 private:
220 #ifdef __WIN32__
221 // common part of CopyFromIcon/CopyFromCursor for Win32
222 bool CopyFromIconOrCursor(const wxGDIImage& icon);
223 #endif // __WIN32__
224
225 DECLARE_DYNAMIC_CLASS(wxBitmap)
226 };
227
228 // ----------------------------------------------------------------------------
229 // wxMask: a mono bitmap used for drawing bitmaps transparently.
230 // ----------------------------------------------------------------------------
231
232 class WXDLLEXPORT wxMask : public wxObject
233 {
234 public:
235 wxMask();
236
237 // Construct a mask from a bitmap and a colour indicating the transparent
238 // area
239 wxMask(const wxBitmap& bitmap, const wxColour& colour);
240
241 // Construct a mask from a bitmap and a palette index indicating the
242 // transparent area
243 wxMask(const wxBitmap& bitmap, int paletteIndex);
244
245 // Construct a mask from a mono bitmap (copies the bitmap).
246 wxMask(const wxBitmap& bitmap);
247
248 // construct a mask from the givne bitmap handle
249 wxMask(WXHBITMAP hbmp) { m_maskBitmap = hbmp; }
250
251 virtual ~wxMask();
252
253 bool Create(const wxBitmap& bitmap, const wxColour& colour);
254 bool Create(const wxBitmap& bitmap, int paletteIndex);
255 bool Create(const wxBitmap& bitmap);
256
257 // Implementation
258 WXHBITMAP GetMaskBitmap() const { return m_maskBitmap; }
259 void SetMaskBitmap(WXHBITMAP bmp) { m_maskBitmap = bmp; }
260
261 protected:
262 WXHBITMAP m_maskBitmap;
263
264 DECLARE_DYNAMIC_CLASS(wxMask)
265 };
266
267 // ----------------------------------------------------------------------------
268 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
269 // ----------------------------------------------------------------------------
270
271 class WXDLLEXPORT wxBitmapHandler : public wxGDIImageHandler
272 {
273 public:
274 wxBitmapHandler() { m_type = wxBITMAP_TYPE_INVALID; }
275 wxBitmapHandler(const wxString& name, const wxString& ext, long type)
276 : wxGDIImageHandler(name, ext, type)
277 {
278 }
279
280 // keep wxBitmapHandler derived from wxGDIImageHandler compatible with the
281 // old class which worked only with bitmaps
282 virtual bool Create(wxBitmap *bitmap,
283 void *data,
284 long flags,
285 int width, int height, int depth = 1);
286 virtual bool LoadFile(wxBitmap *bitmap,
287 const wxString& name,
288 long flags,
289 int desiredWidth, int desiredHeight);
290 virtual bool SaveFile(wxBitmap *bitmap,
291 const wxString& name,
292 int type,
293 const wxPalette *palette = NULL);
294
295 virtual bool Create(wxGDIImage *image,
296 void *data,
297 long flags,
298 int width, int height, int depth = 1);
299 virtual bool Load(wxGDIImage *image,
300 const wxString& name,
301 long flags,
302 int desiredWidth, int desiredHeight);
303 virtual bool Save(wxGDIImage *image,
304 const wxString& name,
305 int type);
306
307 private:
308 DECLARE_DYNAMIC_CLASS(wxBitmapHandler)
309 };
310
311 #endif
312 // _WX_BITMAP_H_