1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBitmap class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/gdiobj.h"
16 #include "wx/gdicmn.h"
17 #include "wx/palette.h"
20 class WXDLLEXPORT wxDC
;
21 class WXDLLEXPORT wxControl
;
22 class WXDLLEXPORT wxBitmap
;
23 class WXDLLEXPORT wxBitmapHandler
;
24 class WXDLLEXPORT wxIcon
;
25 class WXDLLEXPORT wxCursor
;
26 class WXDLLEXPORT wxControl
;
28 // A mask is a mono bitmap used for drawing bitmaps
30 class WXDLLEXPORT wxMask
: public wxObject
32 DECLARE_DYNAMIC_CLASS(wxMask
)
37 // Construct a mask from a bitmap and a colour indicating
38 // the transparent area
39 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
41 // Construct a mask from a bitmap and a palette index indicating
42 // the transparent area
43 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
45 // Construct a mask from a mono bitmap (copies the bitmap).
46 wxMask(const wxBitmap
& bitmap
);
50 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
51 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
52 bool Create(const wxBitmap
& bitmap
);
55 inline WXHBITMAP
GetMaskBitmap() const { return m_maskBitmap
; }
56 inline void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
58 WXHBITMAP m_maskBitmap
;
61 class WXDLLEXPORT wxBitmapRefData
: public wxGDIRefData
63 friend class WXDLLEXPORT wxBitmap
;
64 friend class WXDLLEXPORT wxIcon
;
65 friend class WXDLLEXPORT wxCursor
;
76 wxPalette m_bitmapPalette
;
80 wxDC
* m_selectedInto
; // So bitmap knows whether it's been selected into
82 wxMask
* m_bitmapMask
; // Optional mask
85 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
87 class WXDLLEXPORT wxBitmapHandler
: public wxObject
89 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)
91 wxBitmapHandler() { m_name
= ""; m_extension
= ""; m_type
= 0; };
93 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
94 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
95 int desiredWidth
, int desiredHeight
);
96 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
98 inline void SetName(const wxString
& name
) { m_name
= name
; }
99 inline void SetExtension(const wxString
& ext
) { m_extension
= ext
; }
100 inline void SetType(long type
) { m_type
= type
; }
101 inline wxString
GetName() const { return m_name
; }
102 inline wxString
GetExtension() const { return m_extension
; }
103 inline long GetType() const { return m_type
; }
106 wxString m_extension
;
110 #define M_BITMAPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
112 class WXDLLEXPORT wxBitmap
: public wxGDIObject
114 DECLARE_DYNAMIC_CLASS(wxBitmap
)
116 friend class WXDLLEXPORT wxBitmapHandler
;
119 wxBitmap(); // Platform-specific
122 wxBitmap(const wxBitmap
& bitmap
);
124 // Initialize with raw data
125 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
127 // Initialize with XPM data
128 wxBitmap(char **data
, wxControl
*anItem
= NULL
);
130 // Load a file or resource
131 wxBitmap(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
133 // New constructor for generalised creation from data
134 wxBitmap(void *data
, long type
, int width
, int height
, int depth
= 1);
136 // If depth is omitted, will create a bitmap compatible with the display
137 wxBitmap(int width
, int height
, int depth
= -1);
140 virtual bool Create(int width
, int height
, int depth
= -1);
141 virtual bool Create(void *data
, long type
, int width
, int height
, int depth
= 1);
142 virtual bool LoadFile(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
143 virtual bool SaveFile(const wxString
& name
, int type
, const wxPalette
*cmap
= NULL
);
145 inline bool Ok() const { return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
); }
146 inline int GetWidth() const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_width
: 0); }
147 inline int GetHeight() const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_height
: 0); }
148 inline int GetDepth() const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_depth
: 0); }
149 inline int GetQuality() const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_quality
: 0); }
150 void SetWidth(int w
);
151 void SetHeight(int h
);
152 void SetDepth(int d
);
153 void SetQuality(int q
);
154 void SetOk(bool isOk
);
156 #if WXWIN_COMPATIBILITY
157 inline wxPalette
*GetColourMap(void) const { return GetPalette(); }
158 void SetColourMap(wxPalette
*cmap
) { SetPalette(*cmap
); };
160 inline wxPalette
* GetPalette() const { return (M_BITMAPDATA
? (& M_BITMAPDATA
->m_bitmapPalette
) : (wxPalette
*) NULL
); }
161 void SetPalette(const wxPalette
& palette
);
163 inline wxMask
*GetMask() const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_bitmapMask
: (wxMask
*) NULL
); }
164 void SetMask(wxMask
*mask
) ;
166 inline wxBitmap
& operator = (const wxBitmap
& bitmap
) { if (*this == bitmap
) return (*this); Ref(bitmap
); return *this; }
168 inline bool operator == (const wxBitmap
& bitmap
) { return m_refData
== bitmap
.m_refData
; }
169 inline bool operator != (const wxBitmap
& bitmap
) { return m_refData
!= bitmap
.m_refData
; }
172 static inline wxList
& GetHandlers() { return sm_handlers
; }
173 static void AddHandler(wxBitmapHandler
*handler
);
174 static void InsertHandler(wxBitmapHandler
*handler
);
175 static bool RemoveHandler(const wxString
& name
);
176 static wxBitmapHandler
*FindHandler(const wxString
& name
);
177 static wxBitmapHandler
*FindHandler(const wxString
& extension
, long bitmapType
);
178 static wxBitmapHandler
*FindHandler(long bitmapType
);
180 static void InitStandardHandlers();
181 static void CleanUpHandlers();
183 static wxList sm_handlers
;
187 void SetHBITMAP(WXHBITMAP bmp
);
188 inline WXHBITMAP
GetHBITMAP() const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_hBitmap
: 0); }
189 inline void SetSelectedInto(wxDC
*dc
) { if (M_BITMAPDATA
) M_BITMAPDATA
->m_selectedInto
= dc
; }
190 inline wxDC
*GetSelectedInto(void) const { return (M_BITMAPDATA
? M_BITMAPDATA
->m_selectedInto
: (wxDC
*) NULL
); }
191 bool FreeResource(bool force
= FALSE
);
193 // Creates a bitmap that matches the device context's depth, from
194 // an arbitray bitmap. At present, the original bitmap must have an
195 // associated palette. (TODO: use a default palette if no palette exists.)
196 // This function is necessary for you to Blit an arbitrary bitmap (which may have
197 // the wrong depth). wxDC::SelectObject will compare the depth of the bitmap
198 // with the DC's depth, and create a new bitmap if the depths differ.
199 // Eventually we should perhaps make this a public API function so that
200 // an app can efficiently produce bitmaps of the correct depth.
201 // The Windows solution is to use SetDibBits to blit an arbotrary DIB directly to a DC, but
202 // this is too Windows-specific, hence this solution of quietly converting the wxBitmap.
203 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
204 wxBitmap
GetBitmapForDC(wxDC
& dc
) const;