]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: bitmap.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "bitmap.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/bitmap.h" | |
15 | #include "wx/icon.h" | |
16 | #include "wx/filefn.h" | |
17 | #include "gdk/gdkprivate.h" | |
18 | #include "gdk/gdkx.h" | |
19 | #include "wx/image.h" | |
20 | ||
21 | //----------------------------------------------------------------------------- | |
22 | // wxMask | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
26 | ||
27 | wxMask::wxMask() | |
28 | { | |
29 | m_bitmap = (GdkBitmap *) NULL; | |
30 | } | |
31 | ||
32 | wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const wxColour& WXUNUSED(colour) ) | |
33 | { | |
34 | } | |
35 | ||
36 | wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), int WXUNUSED(paletteIndex) ) | |
37 | { | |
38 | } | |
39 | ||
40 | wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap) ) | |
41 | { | |
42 | } | |
43 | ||
44 | wxMask::~wxMask() | |
45 | { | |
46 | if (m_bitmap) gdk_bitmap_unref( m_bitmap ); | |
47 | } | |
48 | ||
49 | GdkBitmap *wxMask::GetBitmap(void) const | |
50 | { | |
51 | return m_bitmap; | |
52 | } | |
53 | ||
54 | //----------------------------------------------------------------------------- | |
55 | // wxBitmap | |
56 | //----------------------------------------------------------------------------- | |
57 | ||
58 | class wxBitmapRefData: public wxObjectRefData | |
59 | { | |
60 | public: | |
61 | wxBitmapRefData(void); | |
62 | ~wxBitmapRefData(void); | |
63 | ||
64 | GdkPixmap *m_pixmap; | |
65 | GdkBitmap *m_bitmap; | |
66 | wxMask *m_mask; | |
67 | int m_width; | |
68 | int m_height; | |
69 | int m_bpp; | |
70 | wxPalette *m_palette; | |
71 | }; | |
72 | ||
73 | wxBitmapRefData::wxBitmapRefData() | |
74 | { | |
75 | m_pixmap = (GdkPixmap *) NULL; | |
76 | m_bitmap = (GdkBitmap *) NULL; | |
77 | m_mask = (wxMask *) NULL; | |
78 | m_width = 0; | |
79 | m_height = 0; | |
80 | m_bpp = 0; | |
81 | m_palette = (wxPalette *) NULL; | |
82 | } | |
83 | ||
84 | wxBitmapRefData::~wxBitmapRefData() | |
85 | { | |
86 | if (m_pixmap) gdk_pixmap_unref( m_pixmap ); | |
87 | if (m_bitmap) gdk_bitmap_unref( m_bitmap ); | |
88 | if (m_mask) delete m_mask; | |
89 | if (m_palette) delete m_palette; | |
90 | } | |
91 | ||
92 | //----------------------------------------------------------------------------- | |
93 | ||
94 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
95 | ||
96 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
97 | ||
98 | wxBitmap::wxBitmap() | |
99 | { | |
100 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
101 | } | |
102 | ||
103 | wxBitmap::wxBitmap( int width, int height, int depth ) | |
104 | { | |
105 | wxCHECK_RET( (width > 0) && (height > 0), "invalid bitmap size" ) | |
106 | wxCHECK_RET( (depth > 0) || (depth == -1), "invalid bitmap depth" ) | |
107 | ||
108 | m_refData = new wxBitmapRefData(); | |
109 | ||
110 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
111 | ||
112 | M_BMPDATA->m_mask = (wxMask *) NULL; | |
113 | M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth ); | |
114 | M_BMPDATA->m_width = width; | |
115 | M_BMPDATA->m_height = height; | |
116 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; | |
117 | ||
118 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
119 | } | |
120 | ||
121 | wxBitmap::wxBitmap( const char **bits ) | |
122 | { | |
123 | wxCHECK_RET( bits != NULL, "invalid bitmap data" ) | |
124 | ||
125 | m_refData = new wxBitmapRefData(); | |
126 | ||
127 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
128 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
129 | ||
130 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits ); | |
131 | ||
132 | if (mask) | |
133 | { | |
134 | M_BMPDATA->m_mask = new wxMask(); | |
135 | M_BMPDATA->m_mask->m_bitmap = mask; | |
136 | } | |
137 | ||
138 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); | |
139 | ||
140 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ? | |
141 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
142 | } | |
143 | ||
144 | wxBitmap::wxBitmap( char **bits ) | |
145 | { | |
146 | wxCHECK_RET( bits != NULL, "invalid bitmap data" ) | |
147 | ||
148 | m_refData = new wxBitmapRefData(); | |
149 | ||
150 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
151 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
152 | ||
153 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits ); | |
154 | ||
155 | wxCHECK_RET( M_BMPDATA->m_pixmap, "couldn't create pixmap" ); | |
156 | ||
157 | if (mask) | |
158 | { | |
159 | M_BMPDATA->m_mask = new wxMask(); | |
160 | M_BMPDATA->m_mask->m_bitmap = mask; | |
161 | } | |
162 | ||
163 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); | |
164 | ||
165 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ? | |
166 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
167 | } | |
168 | ||
169 | wxBitmap::wxBitmap( const wxBitmap& bmp ) | |
170 | { | |
171 | Ref( bmp ); | |
172 | ||
173 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
174 | } | |
175 | ||
176 | wxBitmap::wxBitmap( const wxString &filename, int type ) | |
177 | { | |
178 | LoadFile( filename, type ); | |
179 | ||
180 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
181 | } | |
182 | ||
183 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) | |
184 | { | |
185 | m_refData = new wxBitmapRefData(); | |
186 | ||
187 | M_BMPDATA->m_mask = (wxMask *) NULL; | |
188 | M_BMPDATA->m_bitmap = | |
189 | gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height ); | |
190 | M_BMPDATA->m_width = width; | |
191 | M_BMPDATA->m_height = height; | |
192 | M_BMPDATA->m_bpp = 1; | |
193 | ||
194 | wxCHECK_RET( M_BMPDATA->m_bitmap, "couldn't create bitmap" ); | |
195 | ||
196 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
197 | } | |
198 | ||
199 | wxBitmap::~wxBitmap() | |
200 | { | |
201 | if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this); | |
202 | } | |
203 | ||
204 | wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp ) | |
205 | { | |
206 | if (*this == bmp) return (*this); | |
207 | Ref( bmp ); | |
208 | return *this; | |
209 | } | |
210 | ||
211 | bool wxBitmap::operator == ( const wxBitmap& bmp ) | |
212 | { | |
213 | return m_refData == bmp.m_refData; | |
214 | } | |
215 | ||
216 | bool wxBitmap::operator != ( const wxBitmap& bmp ) | |
217 | { | |
218 | return m_refData != bmp.m_refData; | |
219 | } | |
220 | ||
221 | bool wxBitmap::Ok(void) const | |
222 | { | |
223 | return (m_refData != NULL); | |
224 | } | |
225 | ||
226 | int wxBitmap::GetHeight(void) const | |
227 | { | |
228 | wxCHECK_MSG( Ok(), -1, "invalid bitmap" ); | |
229 | ||
230 | return M_BMPDATA->m_height; | |
231 | } | |
232 | ||
233 | int wxBitmap::GetWidth(void) const | |
234 | { | |
235 | wxCHECK_MSG( Ok(), -1, "invalid bitmap" ); | |
236 | ||
237 | return M_BMPDATA->m_width; | |
238 | } | |
239 | ||
240 | int wxBitmap::GetDepth(void) const | |
241 | { | |
242 | wxCHECK_MSG( Ok(), -1, "invalid bitmap" ); | |
243 | ||
244 | return M_BMPDATA->m_bpp; | |
245 | } | |
246 | ||
247 | wxMask *wxBitmap::GetMask(void) const | |
248 | { | |
249 | wxCHECK_MSG( Ok(), (wxMask *) NULL, "invalid bitmap" ); | |
250 | ||
251 | return M_BMPDATA->m_mask; | |
252 | } | |
253 | ||
254 | void wxBitmap::SetMask( wxMask *mask ) | |
255 | { | |
256 | wxCHECK_RET( Ok(), "invalid bitmap" ); | |
257 | ||
258 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; | |
259 | ||
260 | M_BMPDATA->m_mask = mask; | |
261 | } | |
262 | ||
263 | bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) ) | |
264 | { | |
265 | wxCHECK_MSG( Ok(), FALSE, "invalid bitmap" ); | |
266 | ||
267 | if (type == wxBITMAP_TYPE_PNG) | |
268 | { | |
269 | wxImage image( *this ); | |
270 | if (image.Ok()) return image.SaveFile( name, type ); | |
271 | } | |
272 | ||
273 | return FALSE; | |
274 | } | |
275 | ||
276 | bool wxBitmap::LoadFile( const wxString &name, int type ) | |
277 | { | |
278 | UnRef(); | |
279 | ||
280 | if (!wxFileExists(name)) return FALSE; | |
281 | ||
282 | if (type == wxBITMAP_TYPE_XPM) | |
283 | { | |
284 | m_refData = new wxBitmapRefData(); | |
285 | ||
286 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
287 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
288 | ||
289 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name ); | |
290 | ||
291 | if (mask) | |
292 | { | |
293 | M_BMPDATA->m_mask = new wxMask(); | |
294 | M_BMPDATA->m_mask->m_bitmap = mask; | |
295 | } | |
296 | ||
297 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); | |
298 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; | |
299 | } | |
300 | else if (type == wxBITMAP_TYPE_PNG) | |
301 | { | |
302 | wxImage image; | |
303 | image.LoadFile( name, type ); | |
304 | if (image.Ok()) *this = image.ConvertToBitmap(); | |
305 | } | |
306 | else if (type == wxBITMAP_TYPE_BMP) | |
307 | { | |
308 | wxImage image; | |
309 | image.LoadFile( name, type ); | |
310 | if (image.Ok()) *this = image.ConvertToBitmap(); | |
311 | } | |
312 | else | |
313 | return FALSE; | |
314 | ||
315 | return TRUE; | |
316 | } | |
317 | ||
318 | wxPalette *wxBitmap::GetPalette(void) const | |
319 | { | |
320 | if (!Ok()) return (wxPalette *) NULL; | |
321 | ||
322 | return M_BMPDATA->m_palette; | |
323 | } | |
324 | ||
325 | void wxBitmap::SetHeight( int height ) | |
326 | { | |
327 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
328 | ||
329 | M_BMPDATA->m_height = height; | |
330 | } | |
331 | ||
332 | void wxBitmap::SetWidth( int width ) | |
333 | { | |
334 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
335 | ||
336 | M_BMPDATA->m_width = width; | |
337 | } | |
338 | ||
339 | void wxBitmap::SetDepth( int depth ) | |
340 | { | |
341 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
342 | ||
343 | M_BMPDATA->m_bpp = depth; | |
344 | } | |
345 | ||
346 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
347 | { | |
348 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
349 | ||
350 | M_BMPDATA->m_pixmap = pixmap; | |
351 | } | |
352 | ||
353 | GdkPixmap *wxBitmap::GetPixmap(void) const | |
354 | { | |
355 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, "invalid bitmap" ); | |
356 | ||
357 | return M_BMPDATA->m_pixmap; | |
358 | } | |
359 | ||
360 | GdkBitmap *wxBitmap::GetBitmap(void) const | |
361 | { | |
362 | wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, "invalid bitmap" ); | |
363 | ||
364 | return M_BMPDATA->m_bitmap; | |
365 | } | |
366 | ||
367 |