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
;
30 class WXDLLEXPORT wxControl
;
32 // A mask is a mono bitmap used for drawing bitmaps
34 class WXDLLEXPORT wxMask
: public wxObject
36 DECLARE_DYNAMIC_CLASS(wxMask
)
41 // Construct a mask from a bitmap and a colour indicating
42 // the transparent area
43 wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
);
45 // Construct a mask from a bitmap and a palette index indicating
46 // the transparent area
47 wxMask(const wxBitmap
& bitmap
, int paletteIndex
);
49 // Construct a mask from a mono bitmap (copies the bitmap).
50 wxMask(const wxBitmap
& bitmap
);
54 bool Create(const wxBitmap
& bitmap
, const wxColour
& colour
);
55 bool Create(const wxBitmap
& bitmap
, int paletteIndex
);
56 bool Create(const wxBitmap
& bitmap
);
59 inline WXHBITMAP
GetMaskBitmap(void) const { return m_maskBitmap
; }
60 inline void SetMaskBitmap(WXHBITMAP bmp
) { m_maskBitmap
= bmp
; }
62 WXHBITMAP m_maskBitmap
;
65 class WXDLLEXPORT wxBitmapRefData
: public wxGDIRefData
67 friend class WXDLLEXPORT wxBitmap
;
68 friend class WXDLLEXPORT wxIcon
;
69 friend class WXDLLEXPORT wxCursor
;
80 wxPalette m_bitmapPalette
;
85 wxDC
* m_selectedInto
; // So bitmap knows whether it's been selected into
86 // a device context (for error checking)
87 wxMask
* m_bitmapMask
; // Option mask
91 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
93 class WXDLLEXPORT wxBitmapHandler
: public wxObject
95 DECLARE_DYNAMIC_CLASS(wxBitmapHandler
)
97 wxBitmapHandler(void) { m_name
= ""; m_extension
= ""; m_type
= 0; };
99 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
100 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
101 int desiredWidth
, int desiredHeight
);
102 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
104 inline void SetName(const wxString
& name
) { m_name
= name
; }
105 inline void SetExtension(const wxString
& ext
) { m_extension
= ext
; }
106 inline void SetType(long type
) { m_type
= type
; }
107 inline wxString
GetName(void) const { return m_name
; }
108 inline wxString
GetExtension(void) const { return m_extension
; }
109 inline long GetType(void) const { return m_type
; }
112 wxString m_extension
;
115 #define M_BITMAPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
117 class WXDLLEXPORT wxBitmap
: public wxGDIObject
119 DECLARE_DYNAMIC_CLASS(wxBitmap
)
121 friend class WXDLLEXPORT wxBitmapHandler
;
124 wxBitmap(); // Platform-specific
127 wxBitmap(const wxBitmap
& bitmap
);
129 // Initialize with raw data
130 wxBitmap(const char bits
[], int width
, int height
, int depth
= 1);
132 // Initialize with XPM data
133 wxBitmap(char **data
, wxControl
*anItem
= NULL
);
135 // Load a file or resource
136 wxBitmap(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
138 // New constructor for generalised creation from data
139 wxBitmap(void *data
, long type
, int width
, int height
, int depth
= 1);
141 // If depth is omitted, will create a bitmap compatible with the display
142 wxBitmap(int width
, int height
, int depth
= -1);
144 // we must have this, otherwise icons are silently copied into bitmaps using
145 // the copy ctor but the resulting bitmap is invalid!
146 wxBitmap(const wxIcon
& icon
);
150 virtual bool Create(int width
, int height
, int depth
= -1);
151 virtual bool Create(void *data
, long type
, int width
, int height
, int depth
= 1);
152 virtual bool LoadFile(const wxString
& name
, long type
= wxBITMAP_TYPE_BMP_RESOURCE
);
153 virtual bool SaveFile(const wxString
& name
, int type
, const wxPalette
*cmap
= NULL
);
155 inline bool Ok(void) const { return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
); }
156 inline int GetWidth(void) const { return (M_BITMAPDATA ? M_BITMAPDATA
->m_width
: 0); }
157 inline int GetHeight(void) const { return (M_BITMAPDATA ? M_BITMAPDATA
->m_height
: 0); }
158 inline int GetDepth(void) const { return (M_BITMAPDATA ? M_BITMAPDATA
->m_depth
: 0); }
159 inline int GetQuality(void) const { return (M_BITMAPDATA ? M_BITMAPDATA
->m_quality
: 0); }
160 void SetWidth(int w
);
161 void SetHeight(int h
);
162 void SetDepth(int d
);
163 void SetQuality(int q
);
164 void SetOk(bool isOk
);
165 #if WXWIN_COMPATIBILITY
166 inline wxPalette
*GetColourMap(void) const { return GetPalette(); }
167 void SetColourMap(wxPalette
*cmap
) { SetPalette(*cmap
); };
169 inline wxPalette
* GetPalette(void) const { return (M_BITMAPDATA ?
(& M_BITMAPDATA
->m_bitmapPalette
) : (wxPalette
*) NULL
); }
170 void SetPalette(const wxPalette
& palette
);
172 inline wxMask
*GetMask(void) const { return (M_BITMAPDATA ? M_BITMAPDATA
->m_bitmapMask
: (wxMask
*) NULL
); }
173 void SetMask(wxMask
*mask
) ;
175 inline wxBitmap
& operator = (const wxBitmap
& bitmap
) { if (*this == bitmap
) return (*this); Ref(bitmap
); return *this; }
177 inline bool operator == (const wxBitmap
& bitmap
) { return m_refData
== bitmap
.m_refData
; }
178 inline bool operator != (const wxBitmap
& bitmap
) { return m_refData
!= bitmap
.m_refData
; }
181 static inline wxList
& GetHandlers(void) { return sm_handlers
; }
182 static void AddHandler(wxBitmapHandler
*handler
);
183 static void InsertHandler(wxBitmapHandler
*handler
);
184 static bool RemoveHandler(const wxString
& name
);
185 static wxBitmapHandler
*FindHandler(const wxString
& name
);
186 static wxBitmapHandler
*FindHandler(const wxString
& extension
, long bitmapType
);
187 static wxBitmapHandler
*FindHandler(long bitmapType
);
189 static void InitStandardHandlers();
190 static void CleanUpHandlers();
192 static wxList sm_handlers
;
196 void SetHBITMAP(WXHBITMAP bmp
);
197 inline WXHBITMAP
GetHBITMAP(void) const { return (M_BITMAPDATA ? M_BITMAPDATA
->m_hBitmap
: 0); }
198 inline void SetSelectedInto(wxDC
*dc
) { if (M_BITMAPDATA
) M_BITMAPDATA
->m_selectedInto
= dc
; }
199 inline wxDC
*GetSelectedInto(void) const { return (M_BITMAPDATA ? M_BITMAPDATA
->m_selectedInto
: (wxDC
*) NULL
); }
200 bool FreeResource(bool force
= FALSE
);
202 // Creates a bitmap that matches the device context's depth, from
203 // an arbitray bitmap. At present, the original bitmap must have an
204 // associated palette. (TODO: use a default palette if no palette exists.)
205 // This function is necessary for you to Blit an arbitrary bitmap (which may have
206 // the wrong depth). wxDC::SelectObject will compare the depth of the bitmap
207 // with the DC's depth, and create a new bitmap if the depths differ.
208 // Eventually we should perhaps make this a public API function so that
209 // an app can efficiently produce bitmaps of the correct depth.
210 // The Windows solution is to use SetDibBits to blit an arbotrary DIB directly to a DC, but
211 // this is too Windows-specific, hence this solution of quietly converting the wxBitmap.
212 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
213 wxBitmap
GetBitmapForDC(wxDC
& dc
) const;