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
68 // ----------------------------------------------------------------------------
69 // wxBitmap: a mono or colour bitmap
70 // ----------------------------------------------------------------------------
72 class WXDLLEXPORT wxBitmap
: public wxGDIImage
75 // default ctor creates an invalid bitmap, you must Create() it later
76 wxBitmap() { Init(); }
79 wxBitmap(const wxBitmap
& bitmap
) { Init(); Ref(bitmap
); }
81 // Initialize with raw data
82 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
84 // Initialize with XPM data
85 wxBitmap(const char **data
) { CreateFromXpm(data
); }
86 wxBitmap(char **data
) { CreateFromXpm((const char **)data
); }
88 // Load a file or resource
89 wxBitmap(const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_BMP_RESOURCE
);
91 // New constructor for generalised creation from data
92 wxBitmap(void *data
, long type
, int width
, int height
, int depth
= 1);
94 // If depth is omitted, will create a bitmap compatible with the display
95 wxBitmap(int width
, int height
, int depth
= -1);
98 // Convert from wxImage:
99 wxBitmap(const wxImage
& image
, int depth
= -1)
100 { (void)CreateFromImage(image
, depth
); }
101 #endif // wxUSE_IMAGE
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
); }
107 wxBitmap
& operator=(const wxBitmap
& bitmap
)
109 if ( m_refData
!= bitmap
.m_refData
)
114 wxBitmap
& operator=(const wxIcon
& icon
)
116 (void)CopyFromIcon(icon
);
121 wxBitmap
& operator=(const wxCursor
& cursor
)
123 (void)CopyFromCursor(cursor
);
131 wxImage
ConvertToImage() const;
132 #endif // wxUSE_IMAGE
134 // get the given part of bitmap
135 wxBitmap
GetSubBitmap( const wxRect
& rect
) const;
137 // copies the contents and mask of the given (colour) icon to the bitmap
138 bool CopyFromIcon(const wxIcon
& icon
);
140 // copies the contents and mask of the given cursor to the bitmap
141 bool CopyFromCursor(const wxCursor
& cursor
);
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
);
148 wxBitmapRefData
*GetBitmapData() const { return (wxBitmapRefData
*)m_refData
; }
150 int GetQuality() const { return (GetBitmapData() ? GetBitmapData()->m_quality
: 0); }
151 void SetQuality(int q
);
154 wxPalette
* GetPalette() const { return (GetBitmapData() ? (& GetBitmapData()->m_bitmapPalette
) : (wxPalette
*) NULL
); }
155 void SetPalette(const wxPalette
& palette
);
156 #endif // wxUSE_PALETTE
158 wxMask
*GetMask() const { return (GetBitmapData() ? GetBitmapData()->m_bitmapMask
: (wxMask
*) NULL
); }
159 void SetMask(wxMask
*mask
) ;
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
; }
164 #if WXWIN_COMPATIBILITY_2
165 void SetOk(bool isOk
);
166 #endif // WXWIN_COMPATIBILITY_2
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
177 void SetHBITMAP(WXHBITMAP bmp
) { SetHandle((WXHANDLE
)bmp
); }
178 WXHBITMAP
GetHBITMAP() const { return (WXHBITMAP
)GetHandle(); }
180 void SetSelectedInto(wxDC
*dc
) { if (GetBitmapData()) GetBitmapData()->m_selectedInto
= dc
; }
181 wxDC
*GetSelectedInto() const { return (GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
); }
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;
198 // common part of all ctors
201 virtual wxGDIImageRefData
*CreateData() const
202 { return new wxBitmapRefData
; }
204 // creates the bitmap from XPM data, supposed to be called from ctor
205 bool CreateFromXpm(const char **bits
);
208 // creates the bitmap from wxImage, supposed to be called from ctor
209 bool CreateFromImage(const wxImage
& image
, int depth
);
210 #endif // wxUSE_IMAGE
214 // common part of CopyFromIcon/CopyFromCursor for Win32
215 bool CopyFromIconOrCursor(const wxGDIImage
& icon
);
218 DECLARE_DYNAMIC_CLASS(wxBitmap
)
221 // ----------------------------------------------------------------------------
222 // wxMask: a mono bitmap used for drawing bitmaps transparently.
223 // ----------------------------------------------------------------------------
225 class WXDLLEXPORT wxMask
: public wxObject
230 // Construct a mask from a bitmap and a colour indicating the transparent
232 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
234 // Construct a mask from a bitmap and a palette index indicating the
236 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
238 // Construct a mask from a mono bitmap (copies the bitmap).
239 wxMask(const wxBitmap
& bitmap
);
241 // construct a mask from the givne bitmap handle
242 wxMask(WXHBITMAP hbmp
) { m_maskBitmap
= hbmp
; }
246 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
247 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
248 bool Create(const wxBitmap
& bitmap
);
251 WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
252 void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
255 WXHBITMAP m_maskBitmap
;
257 DECLARE_DYNAMIC_CLASS(wxMask
)
260 // ----------------------------------------------------------------------------
261 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
262 // ----------------------------------------------------------------------------
264 class WXDLLEXPORT wxBitmapHandler
: public wxGDIImageHandler
267 wxBitmapHandler() { m_type
= wxBITMAP_TYPE_INVALID
; }
268 wxBitmapHandler(const wxString
& name
, const wxString
& ext
, long type
)
269 : wxGDIImageHandler(name
, ext
, type
)
273 // keep wxBitmapHandler derived from wxGDIImageHandler compatible with the
274 // old class which worked only with bitmaps
275 virtual bool Create(wxBitmap
*bitmap
,
278 int width
, int height
, int depth
= 1);
279 virtual bool LoadFile(wxBitmap
*bitmap
,
280 const wxString
& name
,
282 int desiredWidth
, int desiredHeight
);
283 virtual bool SaveFile(wxBitmap
*bitmap
,
284 const wxString
& name
,
286 const wxPalette
*palette
= NULL
);
288 virtual bool Create(wxGDIImage
*image
,
291 int width
, int height
, int depth
= 1);
292 virtual bool Load(wxGDIImage
*image
,
293 const wxString
& name
,
295 int desiredWidth
, int desiredHeight
);
296 virtual bool Save(wxGDIImage
*image
,
297 const wxString
& name
,
301 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)