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