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