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
;
32 // ----------------------------------------------------------------------------
35 // NB: this class is private, but declared here to make it possible inline
36 // wxBitmap functions accessing it
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
43 virtual ~wxBitmapRefData() { Free(); }
49 wxPalette m_bitmapPalette
;
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)
60 // optional mask for transparent drawing
64 // ----------------------------------------------------------------------------
65 // wxBitmap: a mono or colour bitmap
66 // ----------------------------------------------------------------------------
68 class WXDLLEXPORT wxBitmap
: public wxGDIImage
71 // default ctor creates an invalid bitmap, you must Create() it later
72 wxBitmap() { Init(); }
75 wxBitmap(const wxBitmap
& bitmap
) { Init(); Ref(bitmap
); }
77 // Initialize with raw data
78 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
80 // Initialize with XPM data
81 wxBitmap(char **data
, wxControl
*anItem
= NULL
);
83 // Load a file or resource
84 wxBitmap(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
86 // New constructor for generalised creation from data
87 wxBitmap(void *data
, long type
, int width
, int height
, int depth
= 1);
89 // If depth is omitted, will create a bitmap compatible with the display
90 wxBitmap(int width
, int height
, int depth
= -1);
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
); }
96 wxBitmap
& operator=(const wxBitmap
& bitmap
)
98 if ( m_refData
!= bitmap
.m_refData
)
103 wxBitmap
& operator=(const wxIcon
& icon
)
105 (void)CopyFromIcon(icon
);
110 wxBitmap
& operator=(const wxCursor
& cursor
)
112 (void)CopyFromCursor(cursor
);
119 // copies the contents and mask of the given (colour) icon to the bitmap
120 bool CopyFromIcon(const wxIcon
& icon
);
122 // copies the contents and mask of the given cursor to the bitmap
123 bool CopyFromCursor(const wxCursor
& cursor
);
125 virtual bool Create(int width
, int height
, int depth
= -1);
126 virtual bool Create(void *data
, long type
, int width
, int height
, int depth
= 1);
127 virtual bool LoadFile(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
128 virtual bool SaveFile(const wxString
& name
, int type
, const wxPalette
*cmap
= NULL
);
130 wxBitmapRefData
*GetBitmapData() const { return (wxBitmapRefData
*)m_refData
; }
132 int GetQuality() const { return (GetBitmapData() ? GetBitmapData()->m_quality
: 0); }
133 void SetQuality(int q
);
135 wxPalette
* GetPalette() const { return (GetBitmapData() ? (& GetBitmapData()->m_bitmapPalette
) : (wxPalette
*) NULL
); }
136 void SetPalette(const wxPalette
& palette
);
138 wxMask
*GetMask() const { return (GetBitmapData() ? GetBitmapData()->m_bitmapMask
: (wxMask
*) NULL
); }
139 void SetMask(wxMask
*mask
) ;
141 bool operator==(const wxBitmap
& bitmap
) { return m_refData
== bitmap
.m_refData
; }
142 bool operator!=(const wxBitmap
& bitmap
) { return m_refData
!= bitmap
.m_refData
; }
144 #if WXWIN_COMPATIBILITY_2
145 void SetOk(bool isOk
);
146 #endif // WXWIN_COMPATIBILITY_2
148 #if WXWIN_COMPATIBILITY
149 wxPalette
*GetColourMap() const { return GetPalette(); }
150 void SetColourMap(wxPalette
*cmap
) { SetPalette(*cmap
); };
151 #endif // WXWIN_COMPATIBILITY
155 void SetHBITMAP(WXHBITMAP bmp
) { SetHandle((WXHANDLE
)bmp
); }
156 WXHBITMAP
GetHBITMAP() const { return (WXHBITMAP
)GetHandle(); }
158 void SetSelectedInto(wxDC
*dc
) { if (GetBitmapData()) GetBitmapData()->m_selectedInto
= dc
; }
159 wxDC
*GetSelectedInto() const { return (GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
); }
161 // Creates a bitmap that matches the device context's depth, from an
162 // arbitray bitmap. At present, the original bitmap must have an associated
163 // palette. (TODO: use a default palette if no palette exists.) This
164 // function is necessary for you to Blit an arbitrary bitmap (which may
165 // have the wrong depth). wxDC::SelectObject will compare the depth of the
166 // bitmap with the DC's depth, and create a new bitmap if the depths
167 // differ. Eventually we should perhaps make this a public API function so
168 // that an app can efficiently produce bitmaps of the correct depth. The
169 // Windows solution is to use SetDibBits to blit an arbotrary DIB directly
170 // to a DC, but this is too Windows-specific, hence this solution of
171 // quietly converting the wxBitmap. Contributed by Frederic Villeneuve
172 // <frederic.villeneuve@natinst.com>
173 wxBitmap
GetBitmapForDC(wxDC
& dc
) const;
176 // common part of all ctors
179 virtual wxGDIImageRefData
*CreateData() const
180 { return new wxBitmapRefData
; }
184 // common part of CopyFromIcon/CopyFromCursor for Win32
185 bool CopyFromIconOrCursor(const wxGDIImage
& icon
);
188 DECLARE_DYNAMIC_CLASS(wxBitmap
)
191 // ----------------------------------------------------------------------------
192 // wxMask: a mono bitmap used for drawing bitmaps transparently.
193 // ----------------------------------------------------------------------------
195 class WXDLLEXPORT wxMask
: public wxObject
200 // Construct a mask from a bitmap and a colour indicating the transparent
202 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
204 // Construct a mask from a bitmap and a palette index indicating the
206 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
208 // Construct a mask from a mono bitmap (copies the bitmap).
209 wxMask(const wxBitmap
& bitmap
);
211 // construct a mask from the givne bitmap handle
212 wxMask(WXHBITMAP hbmp
) { m_maskBitmap
= hbmp
; }
216 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
217 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
218 bool Create(const wxBitmap
& bitmap
);
221 WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
222 void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
225 WXHBITMAP m_maskBitmap
;
227 DECLARE_DYNAMIC_CLASS(wxMask
)
230 // ----------------------------------------------------------------------------
231 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
232 // ----------------------------------------------------------------------------
234 class WXDLLEXPORT wxBitmapHandler
: public wxGDIImageHandler
237 wxBitmapHandler() { m_type
= wxBITMAP_TYPE_INVALID
; }
238 wxBitmapHandler(const wxString
& name
, const wxString
& ext
, long type
)
239 : wxGDIImageHandler(name
, ext
, type
)
243 // keep wxBitmapHandler derived from wxGDIImageHandler compatible with the
244 // old class which worked only with bitmaps
245 virtual bool Create(wxBitmap
*bitmap
,
248 int width
, int height
, int depth
= 1);
249 virtual bool LoadFile(wxBitmap
*bitmap
,
250 const wxString
& name
,
252 int desiredWidth
, int desiredHeight
);
253 virtual bool SaveFile(wxBitmap
*bitmap
,
254 const wxString
& name
,
256 const wxPalette
*palette
= NULL
);
258 virtual bool Create(wxGDIImage
*image
,
261 int width
, int height
, int depth
= 1);
262 virtual bool Load(wxGDIImage
*image
,
263 const wxString
& name
,
265 int desiredWidth
, int desiredHeight
);
266 virtual bool Save(wxGDIImage
*image
,
267 const wxString
& name
,
271 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)