1 /////////////////////////////////////////////////////////////////////////////
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/gdiobj.h"
20 #include "wx/gdicmn.h"
21 #include "wx/palette.h"
24 class WXDLLEXPORT wxDC
;
25 class WXDLLEXPORT wxControl
;
26 class WXDLLEXPORT wxBitmap
;
27 class WXDLLEXPORT wxBitmapHandler
;
28 class WXDLLEXPORT wxIcon
;
29 class WXDLLEXPORT wxCursor
;
31 // A mask is a mono bitmap used for drawing bitmaps
33 class WXDLLEXPORT wxMask
: public wxObject
35 DECLARE_DYNAMIC_CLASS(wxMask
)
40 // Construct a mask from a bitmap and a colour indicating
41 // the transparent area
42 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
44 // Construct a mask from a bitmap and a palette index indicating
45 // the transparent area
46 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
48 // Construct a mask from a mono bitmap (copies the bitmap).
49 wxMask(const wxBitmap
& bitmap
);
53 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
54 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
55 bool Create(const wxBitmap
& bitmap
);
58 inline WXHBITMAP
GetMaskBitmap(void) const { return m_maskBitmap
; }
59 inline void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
61 WXHBITMAP m_maskBitmap
;
64 class WXDLLEXPORT wxBitmapRefData
: public wxGDIRefData
66 friend class WXDLLEXPORT wxBitmap
;
67 friend class WXDLLEXPORT wxIcon
;
68 friend class WXDLLEXPORT wxCursor
;
70 wxBitmapRefData(void);
71 ~wxBitmapRefData(void);
79 wxPalette m_bitmapPalette
;
84 wxDC
* m_selectedInto
; // So bitmap knows whether it's been selected into
85 // a device context (for error checking)
86 wxMask
* m_bitmapMask
; // Option mask
90 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
92 class WXDLLEXPORT wxBitmapHandler
: public wxObject
94 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)
96 wxBitmapHandler(void) { m_name
= ""; m_extension
= ""; m_type
= 0; };
98 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
99 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
100 int desiredWidth
, int desiredHeight
);
101 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
103 inline void SetName(const wxString
& name
) { m_name
= name
; }
104 inline void SetExtension(const wxString
& ext
) { m_extension
= ext
; }
105 inline void SetType(long type
) { m_type
= type
; }
106 inline wxString
GetName(void) const { return m_name
; }
107 inline wxString
GetExtension(void) const { return m_extension
; }
108 inline long GetType(void) const { return m_type
; }
111 wxString m_extension
;
114 #define M_BITMAPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
116 class WXDLLEXPORT wxBitmap
: public wxGDIObject
118 DECLARE_DYNAMIC_CLASS(wxBitmap
)
120 friend class WXDLLEXPORT wxBitmapHandler
;
123 wxBitmap(void); // Platform-specific
126 inline wxBitmap(const wxBitmap
& bitmap
)
127 { Ref(bitmap
); if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this); }
128 inline wxBitmap(const wxBitmap
* bitmap
) { if (bitmap
) Ref(*bitmap
); if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this); }
130 // Initialize with raw data
131 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
135 // Initialize with XPM data
136 wxBitmap(char **data
, wxItem
*anItem
= NULL
);
139 // Load a file or resource
140 wxBitmap(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
142 // New constructor for generalised creation from data
143 wxBitmap(void *data
, long type
, int width
, int height
, int depth
= 1);
145 // If depth is omitted, will create a bitmap compatible with the display
146 wxBitmap(int width
, int height
, int depth
= -1);
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 inline bool Ok(void) const { return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
); }
155 inline int GetWidth(void) const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_width
: 0); }
156 inline int GetHeight(void) const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_height
: 0); }
157 inline int GetDepth(void) const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_depth
: 0); }
158 inline int GetQuality(void) const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_quality
: 0); }
159 void SetWidth(int w
);
160 void SetHeight(int h
);
161 void SetDepth(int d
);
162 void SetQuality(int q
);
163 void SetOk(bool isOk
);
164 #if WXWIN_COMPATIBILITY
165 inline wxPalette
*GetColourMap(void) const { return GetPalette(); }
166 void SetColourMap(wxPalette
*cmap
) { SetPalette(*cmap
); };
168 inline wxPalette
* GetPalette(void) const { return (M_BITMAPDATA
? (& M_BITMAPDATA
->m_bitmapPalette
) : NULL
); }
169 void SetPalette(const wxPalette
& palette
);
171 inline wxMask
*GetMask(void) const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_bitmapMask
: NULL
); }
172 void SetMask(wxMask
*mask
) ;
174 inline wxBitmap
& operator = (const wxBitmap
& bitmap
) { if (*this == bitmap
) return (*this); Ref(bitmap
); return *this; }
175 inline bool operator == (const wxBitmap
& bitmap
) { return m_refData
== bitmap
.m_refData
; }
176 inline bool operator != (const wxBitmap
& bitmap
) { return m_refData
!= bitmap
.m_refData
; }
179 static inline wxList
& GetHandlers(void) { return sm_handlers
; }
180 static void AddHandler(wxBitmapHandler
*handler
);
181 static void InsertHandler(wxBitmapHandler
*handler
);
182 static bool RemoveHandler(const wxString
& name
);
183 static wxBitmapHandler
*FindHandler(const wxString
& name
);
184 static wxBitmapHandler
*FindHandler(const wxString
& extension
, long bitmapType
);
185 static wxBitmapHandler
*FindHandler(long bitmapType
);
187 static void InitStandardHandlers(void);
188 static void CleanUpHandlers(void);
190 static wxList sm_handlers
;
194 void SetHBITMAP(WXHBITMAP bmp
);
195 inline WXHBITMAP
GetHBITMAP(void) const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_hBitmap
: 0); }
196 inline void SetSelectedInto(wxDC
*dc
) { if (M_BITMAPDATA
) M_BITMAPDATA
->m_selectedInto
= dc
; }
197 inline wxDC
*GetSelectedInto(void) const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_selectedInto
: NULL
); }
198 bool FreeResource(bool force
= FALSE
);
200 // Creates a bitmap that matches the device context's depth, from
201 // an arbitray bitmap. At present, the original bitmap must have an
202 // associated palette. (TODO: use a default palette if no palette exists.)
203 // This function is necessary for you to Blit an arbitrary bitmap (which may have
204 // the wrong depth). wxDC::SelectObject will compare the depth of the bitmap
205 // with the DC's depth, and create a new bitmap if the depths differ.
206 // Eventually we should perhaps make this a public API function so that
207 // an app can efficiently produce bitmaps of the correct depth.
208 // The Windows solution is to use SetDibBits to blit an arbotrary DIB directly to a DC, but
209 // this is too Windows-specific, hence this solution of quietly converting the wxBitmap.
210 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
211 wxBitmap
GetBitmapForDC(wxDC
& dc
) const;