1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/bitmap.h
3 // Purpose: wxBitmap class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "bitmap.h"
19 #include "wx/msw/gdiimage.h"
20 #include "wx/gdicmn.h"
21 #include "wx/palette.h"
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
;
34 // ----------------------------------------------------------------------------
37 // NB: this class is private, but declared here to make it possible inline
38 // wxBitmap functions accessing it
39 // ----------------------------------------------------------------------------
41 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
45 virtual ~wxBitmapRefData() { Free(); }
52 wxPalette m_bitmapPalette
;
53 #endif // wxUSE_PALETTE
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)
64 // optional mask for transparent drawing
67 #if wxUSE_DIB_FOR_BITMAP
68 WXHANDLE m_hFileMap
; // file mapping handle for large DIB's
72 DECLARE_NO_COPY_CLASS(wxBitmapRefData
)
75 // ----------------------------------------------------------------------------
76 // wxBitmap: a mono or colour bitmap
77 // ----------------------------------------------------------------------------
79 class WXDLLEXPORT wxBitmap
: public wxGDIImage
82 // default ctor creates an invalid bitmap, you must Create() it later
83 wxBitmap() { Init(); }
86 wxBitmap(const wxBitmap
& bitmap
) { Init(); Ref(bitmap
); }
88 // Initialize with raw data
89 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
91 // Initialize with XPM data
92 wxBitmap(const char **data
) { CreateFromXpm(data
); }
93 wxBitmap(char **data
) { CreateFromXpm((const char **)data
); }
95 // Load a file or resource
96 wxBitmap(const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_BMP_RESOURCE
);
98 // New constructor for generalised creation from data
99 wxBitmap(void *data
, long type
, int width
, int height
, int depth
= 1);
101 // If depth is omitted, will create a bitmap compatible with the display
102 wxBitmap(int width
, int height
, int depth
= -1);
105 // Convert from wxImage:
106 wxBitmap(const wxImage
& image
, int depth
= -1)
107 { (void)CreateFromImage(image
, depth
); }
108 #endif // wxUSE_IMAGE
110 // we must have this, otherwise icons are silently copied into bitmaps using
111 // the copy ctor but the resulting bitmap is invalid!
112 wxBitmap(const wxIcon
& icon
) { Init(); CopyFromIcon(icon
); }
114 wxBitmap
& operator=(const wxBitmap
& bitmap
)
116 if ( m_refData
!= bitmap
.m_refData
)
121 wxBitmap
& operator=(const wxIcon
& icon
)
123 (void)CopyFromIcon(icon
);
128 wxBitmap
& operator=(const wxCursor
& cursor
)
130 (void)CopyFromCursor(cursor
);
138 wxImage
ConvertToImage() const;
139 #endif // wxUSE_IMAGE
141 // get the given part of bitmap
142 wxBitmap
GetSubBitmap( const wxRect
& rect
) const;
144 // copies the contents and mask of the given (colour) icon to the bitmap
145 bool CopyFromIcon(const wxIcon
& icon
);
147 // copies the contents and mask of the given cursor to the bitmap
148 bool CopyFromCursor(const wxCursor
& cursor
);
150 virtual bool Create(int width
, int height
, int depth
= -1);
151 virtual bool Create(void *data
, long type
, int width
, int height
, int depth
= 1);
152 virtual bool LoadFile(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
153 virtual bool SaveFile(const wxString
& name
, int type
, const wxPalette
*cmap
= NULL
);
155 wxBitmapRefData
*GetBitmapData() const { return (wxBitmapRefData
*)m_refData
; }
157 int GetQuality() const { return (GetBitmapData() ? GetBitmapData()->m_quality
: 0); }
158 void SetQuality(int q
);
161 wxPalette
* GetPalette() const { return (GetBitmapData() ? (& GetBitmapData()->m_bitmapPalette
) : (wxPalette
*) NULL
); }
162 void SetPalette(const wxPalette
& palette
);
163 #endif // wxUSE_PALETTE
165 wxMask
*GetMask() const { return (GetBitmapData() ? GetBitmapData()->m_bitmapMask
: (wxMask
*) NULL
); }
166 void SetMask(wxMask
*mask
) ;
168 bool operator==(const wxBitmap
& bitmap
) const { return m_refData
== bitmap
.m_refData
; }
169 bool operator!=(const wxBitmap
& bitmap
) const { return m_refData
!= bitmap
.m_refData
; }
171 #if WXWIN_COMPATIBILITY_2
172 void SetOk(bool isOk
);
173 #endif // WXWIN_COMPATIBILITY_2
176 #if WXWIN_COMPATIBILITY
177 wxPalette
*GetColourMap() const { return GetPalette(); }
178 void SetColourMap(wxPalette
*cmap
) { SetPalette(*cmap
); };
179 #endif // WXWIN_COMPATIBILITY
180 #endif // wxUSE_PALETTE
184 void SetHBITMAP(WXHBITMAP bmp
) { SetHandle((WXHANDLE
)bmp
); }
185 WXHBITMAP
GetHBITMAP() const { return (WXHBITMAP
)GetHandle(); }
187 #if wxUSE_DIB_FOR_BITMAP
188 void SetHFileMap(WXHANDLE hFileMap
) { GetBitmapData()->m_hFileMap
= hFileMap
; }
189 WXHANDLE
GetHFileMap() const { return GetBitmapData()->m_hFileMap
; }
190 #endif // wxUSE_DIB_FOR_BITMAP
192 void SetSelectedInto(wxDC
*dc
) { if (GetBitmapData()) GetBitmapData()->m_selectedInto
= dc
; }
193 wxDC
*GetSelectedInto() const { return (GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
); }
195 // Creates a bitmap that matches the device context's depth, from an
196 // arbitray bitmap. At present, the original bitmap must have an associated
197 // palette. (TODO: use a default palette if no palette exists.) This
198 // function is necessary for you to Blit an arbitrary bitmap (which may
199 // have the wrong depth). wxDC::SelectObject will compare the depth of the
200 // bitmap with the DC's depth, and create a new bitmap if the depths
201 // differ. Eventually we should perhaps make this a public API function so
202 // that an app can efficiently produce bitmaps of the correct depth. The
203 // Windows solution is to use SetDibBits to blit an arbotrary DIB directly
204 // to a DC, but this is too Windows-specific, hence this solution of
205 // quietly converting the wxBitmap. Contributed by Frederic Villeneuve
206 // <frederic.villeneuve@natinst.com>
207 wxBitmap
GetBitmapForDC(wxDC
& dc
) const;
210 // common part of all ctors
213 virtual wxGDIImageRefData
*CreateData() const
214 { return new wxBitmapRefData
; }
216 // creates the bitmap from XPM data, supposed to be called from ctor
217 bool CreateFromXpm(const char **bits
);
220 // creates the bitmap from wxImage, supposed to be called from ctor
221 bool CreateFromImage(const wxImage
& image
, int depth
);
222 #endif // wxUSE_IMAGE
224 #if wxUSE_DIB_FOR_BITMAP
225 void *CreateDIB(int width
, int height
, int depth
);
226 void CopyDIBLine(void* src
, void* dest
, int count
) const;
231 // common part of CopyFromIcon/CopyFromCursor for Win32
232 bool CopyFromIconOrCursor(const wxGDIImage
& icon
);
235 DECLARE_DYNAMIC_CLASS(wxBitmap
)
238 // ----------------------------------------------------------------------------
239 // wxMask: a mono bitmap used for drawing bitmaps transparently.
240 // ----------------------------------------------------------------------------
242 class WXDLLEXPORT wxMask
: public wxObject
247 // Construct a mask from a bitmap and a colour indicating the transparent
249 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
251 // Construct a mask from a bitmap and a palette index indicating the
253 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
255 // Construct a mask from a mono bitmap (copies the bitmap).
256 wxMask(const wxBitmap
& bitmap
);
258 // construct a mask from the givne bitmap handle
259 wxMask(WXHBITMAP hbmp
) { m_maskBitmap
= hbmp
; }
263 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
264 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
265 bool Create(const wxBitmap
& bitmap
);
268 WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
269 void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
272 WXHBITMAP m_maskBitmap
;
274 DECLARE_DYNAMIC_CLASS(wxMask
)
277 // ----------------------------------------------------------------------------
278 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
279 // ----------------------------------------------------------------------------
281 class WXDLLEXPORT wxBitmapHandler
: public wxGDIImageHandler
284 wxBitmapHandler() { m_type
= wxBITMAP_TYPE_INVALID
; }
285 wxBitmapHandler(const wxString
& name
, const wxString
& ext
, long type
)
286 : wxGDIImageHandler(name
, ext
, type
)
290 // keep wxBitmapHandler derived from wxGDIImageHandler compatible with the
291 // old class which worked only with bitmaps
292 virtual bool Create(wxBitmap
*bitmap
,
295 int width
, int height
, int depth
= 1);
296 virtual bool LoadFile(wxBitmap
*bitmap
,
297 const wxString
& name
,
299 int desiredWidth
, int desiredHeight
);
300 virtual bool SaveFile(wxBitmap
*bitmap
,
301 const wxString
& name
,
303 const wxPalette
*palette
= NULL
);
305 virtual bool Create(wxGDIImage
*image
,
308 int width
, int height
, int depth
= 1);
309 virtual bool Load(wxGDIImage
*image
,
310 const wxString
& name
,
312 int desiredWidth
, int desiredHeight
);
313 virtual bool Save(wxGDIImage
*image
,
314 const wxString
& name
,
318 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)