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 // ----------------------------------------------------------------------------
73 // wxBitmap: a mono or colour bitmap
74 // ----------------------------------------------------------------------------
76 class WXDLLEXPORT wxBitmap
: public wxGDIImage
79 // default ctor creates an invalid bitmap, you must Create() it later
80 wxBitmap() { Init(); }
83 wxBitmap(const wxBitmap
& bitmap
) { Init(); Ref(bitmap
); }
85 // Initialize with raw data
86 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
88 // Initialize with XPM data
89 wxBitmap(const char **data
) { CreateFromXpm(data
); }
90 wxBitmap(char **data
) { CreateFromXpm((const char **)data
); }
92 // Load a file or resource
93 wxBitmap(const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_BMP_RESOURCE
);
95 // New constructor for generalised creation from data
96 wxBitmap(void *data
, long type
, int width
, int height
, int depth
= 1);
98 // If depth is omitted, will create a bitmap compatible with the display
99 wxBitmap(int width
, int height
, int depth
= -1);
102 // Convert from wxImage:
103 wxBitmap(const wxImage
& image
, int depth
= -1)
104 { (void)CreateFromImage(image
, depth
); }
105 #endif // wxUSE_IMAGE
107 // we must have this, otherwise icons are silently copied into bitmaps using
108 // the copy ctor but the resulting bitmap is invalid!
109 wxBitmap(const wxIcon
& icon
) { Init(); CopyFromIcon(icon
); }
111 wxBitmap
& operator=(const wxBitmap
& bitmap
)
113 if ( m_refData
!= bitmap
.m_refData
)
118 wxBitmap
& operator=(const wxIcon
& icon
)
120 (void)CopyFromIcon(icon
);
125 wxBitmap
& operator=(const wxCursor
& cursor
)
127 (void)CopyFromCursor(cursor
);
135 wxImage
ConvertToImage() const;
136 #endif // wxUSE_IMAGE
138 // get the given part of bitmap
139 wxBitmap
GetSubBitmap( const wxRect
& rect
) const;
141 // copies the contents and mask of the given (colour) icon to the bitmap
142 bool CopyFromIcon(const wxIcon
& icon
);
144 // copies the contents and mask of the given cursor to the bitmap
145 bool CopyFromCursor(const wxCursor
& cursor
);
147 virtual bool Create(int width
, int height
, int depth
= -1);
148 virtual bool Create(void *data
, long type
, int width
, int height
, int depth
= 1);
149 virtual bool LoadFile(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
150 virtual bool SaveFile(const wxString
& name
, int type
, const wxPalette
*cmap
= NULL
);
152 wxBitmapRefData
*GetBitmapData() const { return (wxBitmapRefData
*)m_refData
; }
154 int GetQuality() const { return (GetBitmapData() ? GetBitmapData()->m_quality
: 0); }
155 void SetQuality(int q
);
158 wxPalette
* GetPalette() const { return (GetBitmapData() ? (& GetBitmapData()->m_bitmapPalette
) : (wxPalette
*) NULL
); }
159 void SetPalette(const wxPalette
& palette
);
160 #endif // wxUSE_PALETTE
162 wxMask
*GetMask() const { return (GetBitmapData() ? GetBitmapData()->m_bitmapMask
: (wxMask
*) NULL
); }
163 void SetMask(wxMask
*mask
) ;
165 bool operator==(const wxBitmap
& bitmap
) const { return m_refData
== bitmap
.m_refData
; }
166 bool operator!=(const wxBitmap
& bitmap
) const { return m_refData
!= bitmap
.m_refData
; }
168 #if WXWIN_COMPATIBILITY_2
169 void SetOk(bool isOk
);
170 #endif // WXWIN_COMPATIBILITY_2
173 #if WXWIN_COMPATIBILITY
174 wxPalette
*GetColourMap() const { return GetPalette(); }
175 void SetColourMap(wxPalette
*cmap
) { SetPalette(*cmap
); };
176 #endif // WXWIN_COMPATIBILITY
177 #endif // wxUSE_PALETTE
181 void SetHBITMAP(WXHBITMAP bmp
) { SetHandle((WXHANDLE
)bmp
); }
182 WXHBITMAP
GetHBITMAP() const { return (WXHBITMAP
)GetHandle(); }
184 #if wxUSE_DIB_FOR_BITMAP
185 void SetHFileMap(WXHANDLE hFileMap
) { GetBitmapData()->m_hFileMap
= hFileMap
; }
186 WXHANDLE
GetHFileMap() const { return GetBitmapData()->m_hFileMap
; }
187 #endif // wxUSE_DIB_FOR_BITMAP
189 void SetSelectedInto(wxDC
*dc
) { if (GetBitmapData()) GetBitmapData()->m_selectedInto
= dc
; }
190 wxDC
*GetSelectedInto() const { return (GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
); }
192 // Creates a bitmap that matches the device context's depth, from an
193 // arbitray bitmap. At present, the original bitmap must have an associated
194 // palette. (TODO: use a default palette if no palette exists.) This
195 // function is necessary for you to Blit an arbitrary bitmap (which may
196 // have the wrong depth). wxDC::SelectObject will compare the depth of the
197 // bitmap with the DC's depth, and create a new bitmap if the depths
198 // differ. Eventually we should perhaps make this a public API function so
199 // that an app can efficiently produce bitmaps of the correct depth. The
200 // Windows solution is to use SetDibBits to blit an arbotrary DIB directly
201 // to a DC, but this is too Windows-specific, hence this solution of
202 // quietly converting the wxBitmap. Contributed by Frederic Villeneuve
203 // <frederic.villeneuve@natinst.com>
204 wxBitmap
GetBitmapForDC(wxDC
& dc
) const;
207 // common part of all ctors
210 virtual wxGDIImageRefData
*CreateData() const
211 { return new wxBitmapRefData
; }
213 // creates the bitmap from XPM data, supposed to be called from ctor
214 bool CreateFromXpm(const char **bits
);
217 // creates the bitmap from wxImage, supposed to be called from ctor
218 bool CreateFromImage(const wxImage
& image
, int depth
);
219 #endif // wxUSE_IMAGE
221 #if wxUSE_DIB_FOR_BITMAP
222 void *CreateDIB(int width
, int height
, int depth
);
223 void CopyDIBLine(void* src
, void* dest
, int count
) const;
228 // common part of CopyFromIcon/CopyFromCursor for Win32
229 bool CopyFromIconOrCursor(const wxGDIImage
& icon
);
232 DECLARE_DYNAMIC_CLASS(wxBitmap
)
235 // ----------------------------------------------------------------------------
236 // wxMask: a mono bitmap used for drawing bitmaps transparently.
237 // ----------------------------------------------------------------------------
239 class WXDLLEXPORT wxMask
: public wxObject
244 // Construct a mask from a bitmap and a colour indicating the transparent
246 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
248 // Construct a mask from a bitmap and a palette index indicating the
250 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
252 // Construct a mask from a mono bitmap (copies the bitmap).
253 wxMask(const wxBitmap
& bitmap
);
255 // construct a mask from the givne bitmap handle
256 wxMask(WXHBITMAP hbmp
) { m_maskBitmap
= hbmp
; }
260 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
261 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
262 bool Create(const wxBitmap
& bitmap
);
265 WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
266 void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
269 WXHBITMAP m_maskBitmap
;
271 DECLARE_DYNAMIC_CLASS(wxMask
)
274 // ----------------------------------------------------------------------------
275 // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
276 // ----------------------------------------------------------------------------
278 class WXDLLEXPORT wxBitmapHandler
: public wxGDIImageHandler
281 wxBitmapHandler() { m_type
= wxBITMAP_TYPE_INVALID
; }
282 wxBitmapHandler(const wxString
& name
, const wxString
& ext
, long type
)
283 : wxGDIImageHandler(name
, ext
, type
)
287 // keep wxBitmapHandler derived from wxGDIImageHandler compatible with the
288 // old class which worked only with bitmaps
289 virtual bool Create(wxBitmap
*bitmap
,
292 int width
, int height
, int depth
= 1);
293 virtual bool LoadFile(wxBitmap
*bitmap
,
294 const wxString
& name
,
296 int desiredWidth
, int desiredHeight
);
297 virtual bool SaveFile(wxBitmap
*bitmap
,
298 const wxString
& name
,
300 const wxPalette
*palette
= NULL
);
302 virtual bool Create(wxGDIImage
*image
,
305 int width
, int height
, int depth
= 1);
306 virtual bool Load(wxGDIImage
*image
,
307 const wxString
& name
,
309 int desiredWidth
, int desiredHeight
);
310 virtual bool Save(wxGDIImage
*image
,
311 const wxString
& name
,
315 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)