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