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