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