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 DECLARE_NO_COPY_CLASS(wxBitmapRefData
)
69 #if wxUSE_DIB_FOR_BITMAP
70 WXHANDLE m_hFileMap
; // file mapping handle for large DIB's
74 // ----------------------------------------------------------------------------
75 // wxBitmap: a mono or colour bitmap
76 // ----------------------------------------------------------------------------
78 class WXDLLEXPORT wxBitmap
: public wxGDIImage
81 // default ctor creates an invalid bitmap, you must Create() it later
82 wxBitmap() { Init(); }
85 wxBitmap(const wxBitmap
& bitmap
) { Init(); Ref(bitmap
); }
87 // Initialize with raw data
88 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
90 // Initialize with XPM data
91 wxBitmap(const char **data
) { CreateFromXpm(data
); }
92 wxBitmap(char **data
) { CreateFromXpm((const char **)data
); }
94 // Load a file or resource
95 wxBitmap(const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_BMP_RESOURCE
);
97 // New constructor for generalised creation from data
98 wxBitmap(void *data
, long type
, int width
, int height
, int depth
= 1);
100 // If depth is omitted, will create a bitmap compatible with the display
101 wxBitmap(int width
, int height
, int depth
= -1);
104 // Convert from wxImage:
105 wxBitmap(const wxImage
& image
, int depth
= -1)
106 { (void)CreateFromImage(image
, depth
); }
107 #endif // wxUSE_IMAGE
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
); }
113 wxBitmap
& operator=(const wxBitmap
& bitmap
)
115 if ( m_refData
!= bitmap
.m_refData
)
120 wxBitmap
& operator=(const wxIcon
& icon
)
122 (void)CopyFromIcon(icon
);
127 wxBitmap
& operator=(const wxCursor
& cursor
)
129 (void)CopyFromCursor(cursor
);
137 wxImage
ConvertToImage() const;
138 #endif // wxUSE_IMAGE
140 // get the given part of bitmap
141 wxBitmap
GetSubBitmap( const wxRect
& rect
) const;
143 // copies the contents and mask of the given (colour) icon to the bitmap
144 bool CopyFromIcon(const wxIcon
& icon
);
146 // copies the contents and mask of the given cursor to the bitmap
147 bool CopyFromCursor(const wxCursor
& cursor
);
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
);
154 wxBitmapRefData
*GetBitmapData() const { return (wxBitmapRefData
*)m_refData
; }
156 int GetQuality() const { return (GetBitmapData() ?
GetBitmapData()->m_quality
: 0); }
157 void SetQuality(int q
);
160 wxPalette
* GetPalette() const { return (GetBitmapData() ?
(& GetBitmapData()->m_bitmapPalette
) : (wxPalette
*) NULL
); }
161 void SetPalette(const wxPalette
& palette
);
162 #endif // wxUSE_PALETTE
164 wxMask
*GetMask() const { return (GetBitmapData() ?
GetBitmapData()->m_bitmapMask
: (wxMask
*) NULL
); }
165 void SetMask(wxMask
*mask
) ;
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
; }
170 #if WXWIN_COMPATIBILITY_2
171 void SetOk(bool isOk
);
172 #endif // WXWIN_COMPATIBILITY_2
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
183 void SetHBITMAP(WXHBITMAP bmp
) { SetHandle((WXHANDLE
)bmp
); }
184 WXHBITMAP
GetHBITMAP() const { return (WXHBITMAP
)GetHandle(); }
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
191 void SetSelectedInto(wxDC
*dc
) { if (GetBitmapData()) GetBitmapData()->m_selectedInto
= dc
; }
192 wxDC
*GetSelectedInto() const { return (GetBitmapData() ?
GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
); }
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;
209 // common part of all ctors
212 virtual wxGDIImageRefData
*CreateData() const
213 { return new wxBitmapRefData
; }
215 // creates the bitmap from XPM data, supposed to be called from ctor
216 bool CreateFromXpm(const char **bits
);
219 // creates the bitmap from wxImage, supposed to be called from ctor
220 bool CreateFromImage(const wxImage
& image
, int depth
);
221 #endif // wxUSE_IMAGE
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;
230 // common part of CopyFromIcon/CopyFromCursor for Win32
231 bool CopyFromIconOrCursor(const wxGDIImage
& icon
);
234 DECLARE_DYNAMIC_CLASS(wxBitmap
)
237 // ----------------------------------------------------------------------------
238 // wxMask: a mono bitmap used for drawing bitmaps transparently.
239 // ----------------------------------------------------------------------------
241 class WXDLLEXPORT wxMask
: public wxObject
246 // Construct a mask from a bitmap and a colour indicating the transparent
248 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
250 // Construct a mask from a bitmap and a palette index indicating the
252 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
254 // Construct a mask from a mono bitmap (copies the bitmap).
255 wxMask(const wxBitmap
& bitmap
);
257 // construct a mask from the givne bitmap handle
258 wxMask(WXHBITMAP hbmp
) { m_maskBitmap
= hbmp
; }
262 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
263 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
264 bool Create(const wxBitmap
& bitmap
);
267 WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
268 void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
271 WXHBITMAP m_maskBitmap
;
273 DECLARE_DYNAMIC_CLASS(wxMask
)
276 // ----------------------------------------------------------------------------
277 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
278 // ----------------------------------------------------------------------------
280 class WXDLLEXPORT wxBitmapHandler
: public wxGDIImageHandler
283 wxBitmapHandler() { m_type
= wxBITMAP_TYPE_INVALID
; }
284 wxBitmapHandler(const wxString
& name
, const wxString
& ext
, long type
)
285 : wxGDIImageHandler(name
, ext
, type
)
289 // keep wxBitmapHandler derived from wxGDIImageHandler compatible with the
290 // old class which worked only with bitmaps
291 virtual bool Create(wxBitmap
*bitmap
,
294 int width
, int height
, int depth
= 1);
295 virtual bool LoadFile(wxBitmap
*bitmap
,
296 const wxString
& name
,
298 int desiredWidth
, int desiredHeight
);
299 virtual bool SaveFile(wxBitmap
*bitmap
,
300 const wxString
& name
,
302 const wxPalette
*palette
= NULL
);
304 virtual bool Create(wxGDIImage
*image
,
307 int width
, int height
, int depth
= 1);
308 virtual bool Load(wxGDIImage
*image
,
309 const wxString
& name
,
311 int desiredWidth
, int desiredHeight
);
312 virtual bool Save(wxGDIImage
*image
,
313 const wxString
& name
,
317 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)