1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBitmap class interface
4 // Author: Vaclav Slavik
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_BITMAP_H_BASE_
13 #define _WX_BITMAP_H_BASE_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/string.h"
20 #include "wx/gdicmn.h" // for wxBitmapType
21 #include "wx/colour.h"
24 class WXDLLIMPEXP_FWD_CORE wxBitmap
;
25 class WXDLLIMPEXP_FWD_CORE wxBitmapHandler
;
26 class WXDLLIMPEXP_FWD_CORE wxIcon
;
27 class WXDLLIMPEXP_FWD_CORE wxMask
;
28 class WXDLLIMPEXP_FWD_CORE wxPalette
;
29 class WXDLLIMPEXP_FWD_CORE wxDC
;
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
36 #include "wx/variant.h"
37 DECLARE_VARIANT_OBJECT_EXPORTED(wxBitmap
,WXDLLIMPEXP_CORE
)
40 // ----------------------------------------------------------------------------
41 // wxMask represents the transparent area of the bitmap
42 // ----------------------------------------------------------------------------
44 // TODO: all implementation of wxMask, except the generic one,
45 // do not derive from wxMaskBase,,, they should
46 class WXDLLIMPEXP_CORE wxMaskBase
: public wxObject
49 // create the mask from bitmap pixels of the given colour
50 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
53 // create the mask from bitmap pixels with the given palette index
54 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
55 #endif // wxUSE_PALETTE
57 // create the mask from the given mono bitmap
58 bool Create(const wxBitmap
& bitmap
);
61 // this function is called from Create() to free the existing mask data
62 virtual void FreeData() = 0;
64 // these functions must be overridden to implement the corresponding public
65 // Create() methods, they shouldn't call FreeData() as it's already called
66 // by the public wrappers
67 virtual bool InitFromColour(const wxBitmap
& bitmap
,
68 const wxColour
& colour
) = 0;
69 virtual bool InitFromMonoBitmap(const wxBitmap
& bitmap
) = 0;
72 #if defined(__WXDFB__) || \
73 defined(__WXMAC__) || \
74 defined(__WXGTK__) || \
75 defined(__WXCOCOA__) || \
76 defined(__WXMOTIF__) || \
78 #define wxUSE_BITMAP_BASE 1
80 #define wxUSE_BITMAP_BASE 0
83 // a more readable way to tell
84 #define wxBITMAP_SCREEN_DEPTH (-1)
87 // ----------------------------------------------------------------------------
88 // wxBitmapHelpers: container for various bitmap methods common to all ports.
89 // ----------------------------------------------------------------------------
91 // Unfortunately, currently wxBitmap does not inherit from wxBitmapBase on all
92 // platforms and this is not easy to fix. So we extract at least some common
93 // methods into this class from which both wxBitmapBase (and hence wxBitmap on
94 // all platforms where it does inherit from it) and wxBitmap in wxMSW and other
95 // exceptional ports (only wxPM and old wxCocoa) inherit.
96 class WXDLLIMPEXP_CORE wxBitmapHelpers
99 // Create a new wxBitmap from the PNG data in the given buffer.
100 static wxBitmap
NewFromPNGData(const void* data
, size_t size
);
104 // All ports except wxMSW and wxOS2 use wxBitmapHandler and wxBitmapBase as
105 // base class for wxBitmapHandler; wxMSW and wxOS2 use wxGDIImageHandler as
106 // base class since it allows some code reuse there.
107 #if wxUSE_BITMAP_BASE
109 // ----------------------------------------------------------------------------
110 // wxBitmapHandler: class which knows how to create/load/save bitmaps in
112 // ----------------------------------------------------------------------------
114 class WXDLLIMPEXP_CORE wxBitmapHandler
: public wxObject
117 wxBitmapHandler() { m_type
= wxBITMAP_TYPE_INVALID
; }
118 virtual ~wxBitmapHandler() { }
120 // NOTE: the following functions should be pure virtuals, but they aren't
121 // because otherwise almost all ports would have to implement
122 // them as "return false"...
124 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
), const void* WXUNUSED(data
),
125 wxBitmapType
WXUNUSED(type
), int WXUNUSED(width
), int WXUNUSED(height
),
126 int WXUNUSED(depth
) = 1)
129 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
), const wxString
& WXUNUSED(name
),
130 wxBitmapType
WXUNUSED(type
), int WXUNUSED(desiredWidth
),
131 int WXUNUSED(desiredHeight
))
134 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
), const wxString
& WXUNUSED(name
),
135 wxBitmapType
WXUNUSED(type
), const wxPalette
*WXUNUSED(palette
) = NULL
) const
138 void SetName(const wxString
& name
) { m_name
= name
; }
139 void SetExtension(const wxString
& ext
) { m_extension
= ext
; }
140 void SetType(wxBitmapType type
) { m_type
= type
; }
141 const wxString
& GetName() const { return m_name
; }
142 const wxString
& GetExtension() const { return m_extension
; }
143 wxBitmapType
GetType() const { return m_type
; }
147 wxString m_extension
;
150 DECLARE_ABSTRACT_CLASS(wxBitmapHandler
)
153 // ----------------------------------------------------------------------------
154 // wxBitmap: class which represents platform-dependent bitmap (unlike wxImage)
155 // ----------------------------------------------------------------------------
157 class WXDLLIMPEXP_CORE wxBitmapBase
: public wxGDIObject
,
158 public wxBitmapHelpers
162 Derived class must implement these:
165 wxBitmap(const wxBitmap& bmp);
166 wxBitmap(const char bits[], int width, int height, int depth = 1);
167 wxBitmap(int width, int height, int depth = wxBITMAP_SCREEN_DEPTH);
168 wxBitmap(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH);
169 wxBitmap(const char* const* bits);
170 wxBitmap(const wxString &filename, wxBitmapType type = wxBITMAP_TYPE_XPM);
171 wxBitmap(const wxImage& image, int depth = wxBITMAP_SCREEN_DEPTH);
173 static void InitStandardHandlers();
176 virtual bool Create(int width
, int height
, int depth
= wxBITMAP_SCREEN_DEPTH
) = 0;
177 virtual bool Create(const wxSize
& sz
, int depth
= wxBITMAP_SCREEN_DEPTH
) = 0;
178 // Create a bitmap compatible with the given DC, inheriting its magnification factor
180 virtual int GetHeight() const = 0;
181 virtual int GetWidth() const = 0;
182 virtual int GetDepth() const = 0;
184 wxSize
GetSize() const
185 { return wxSize(GetWidth(), GetHeight()); }
187 // support for scaled bitmaps
188 virtual double GetScaleFactor() const { return 1.0; }
189 virtual double GetScaledWidth() const { return GetWidth() / GetScaleFactor(); }
190 virtual double GetScaledHeight() const { return GetHeight() / GetScaleFactor(); }
191 virtual wxSize
GetScaledSize() const
192 { return wxSize(GetScaledWidth(), GetScaledHeight()); }
195 virtual wxImage
ConvertToImage() const = 0;
197 // Convert to disabled (dimmed) bitmap.
198 wxBitmap
ConvertToDisabled(unsigned char brightness
= 255) const;
199 #endif // wxUSE_IMAGE
201 virtual wxMask
*GetMask() const = 0;
202 virtual void SetMask(wxMask
*mask
) = 0;
204 virtual wxBitmap
GetSubBitmap(const wxRect
& rect
) const = 0;
206 virtual bool SaveFile(const wxString
&name
, wxBitmapType type
,
207 const wxPalette
*palette
= NULL
) const = 0;
208 virtual bool LoadFile(const wxString
&name
, wxBitmapType type
) = 0;
211 If raw bitmap access is supported (see wx/rawbmp.h), the following
212 methods should be implemented:
214 virtual bool GetRawData(wxRawBitmapData *data) = 0;
215 virtual void UngetRawData(wxRawBitmapData *data) = 0;
219 virtual wxPalette
*GetPalette() const = 0;
220 virtual void SetPalette(const wxPalette
& palette
) = 0;
221 #endif // wxUSE_PALETTE
223 // copies the contents and mask of the given (colour) icon to the bitmap
224 virtual bool CopyFromIcon(const wxIcon
& icon
) = 0;
227 virtual void SetHeight(int height
) = 0;
228 virtual void SetWidth(int width
) = 0;
229 virtual void SetDepth(int depth
) = 0;
232 static inline wxList
& GetHandlers() { return sm_handlers
; }
233 static void AddHandler(wxBitmapHandler
*handler
);
234 static void InsertHandler(wxBitmapHandler
*handler
);
235 static bool RemoveHandler(const wxString
& name
);
236 static wxBitmapHandler
*FindHandler(const wxString
& name
);
237 static wxBitmapHandler
*FindHandler(const wxString
& extension
, wxBitmapType bitmapType
);
238 static wxBitmapHandler
*FindHandler(wxBitmapType bitmapType
);
240 //static void InitStandardHandlers();
241 // (wxBitmap must implement this one)
243 static void CleanUpHandlers();
245 // this method is only used by the generic implementation of wxMask
246 // currently but could be useful elsewhere in the future: it can be
247 // overridden to quantize the colour to correspond to bitmap colour depth
248 // if necessary; default implementation simply returns the colour as is
249 virtual wxColour
QuantizeColour(const wxColour
& colour
) const
255 static wxList sm_handlers
;
257 DECLARE_ABSTRACT_CLASS(wxBitmapBase
)
260 #endif // wxUSE_BITMAP_BASE
263 // the wxBITMAP_DEFAULT_TYPE constant defines the default argument value
264 // for wxBitmap's ctor and wxBitmap::LoadFile() functions.
265 #if defined(__WXMSW__)
266 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
267 #include "wx/msw/bitmap.h"
268 #elif defined(__WXMOTIF__)
269 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_XPM
270 #include "wx/x11/bitmap.h"
271 #elif defined(__WXGTK20__)
273 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
275 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_XPM
277 #include "wx/gtk/bitmap.h"
278 #elif defined(__WXGTK__)
279 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_XPM
280 #include "wx/gtk1/bitmap.h"
281 #elif defined(__WXX11__)
282 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_XPM
283 #include "wx/x11/bitmap.h"
284 #elif defined(__WXDFB__)
285 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
286 #include "wx/dfb/bitmap.h"
287 #elif defined(__WXMAC__)
288 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_PICT_RESOURCE
289 #include "wx/osx/bitmap.h"
290 #elif defined(__WXCOCOA__)
291 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
292 #include "wx/cocoa/bitmap.h"
293 #elif defined(__WXPM__)
294 #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
295 #include "wx/os2/bitmap.h"
301 #if wxUSE_BITMAP_BASE
306 ConvertToDisabled(unsigned char brightness
) const
308 return ConvertToImage().ConvertToDisabled(brightness
);
310 #endif // wxUSE_IMAGE
312 // we must include generic mask.h after wxBitmap definition
313 #if defined(__WXDFB__)
314 #define wxUSE_GENERIC_MASK 1
316 #define wxUSE_GENERIC_MASK 0
319 #if wxUSE_GENERIC_MASK
320 #include "wx/generic/mask.h"
323 #endif // _WX_BITMAP_H_BASE_