]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bitmap.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
6f65e337 | 5 | // RCS-ID: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
8bbe427f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "bitmap.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/bitmap.h" | |
52cbfcf0 | 15 | #include "wx/icon.h" |
fd0eed64 | 16 | #include "wx/filefn.h" |
83624f79 RR |
17 | #include "wx/image.h" |
18 | ||
20e05ffb RR |
19 | #include <gdk/gdk.h> |
20 | #include <gdk/gdkprivate.h> | |
21 | #include <gdk/gdkx.h> | |
c801d85f KB |
22 | |
23 | //----------------------------------------------------------------------------- | |
24 | // wxMask | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
28 | ||
8bbe427f | 29 | wxMask::wxMask() |
c801d85f | 30 | { |
fd0eed64 | 31 | m_bitmap = (GdkBitmap *) NULL; |
ff7b1510 | 32 | } |
c801d85f | 33 | |
91b8de8d | 34 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
c801d85f | 35 | { |
72a7edf0 | 36 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 37 | Create( bitmap, colour ); |
ff7b1510 | 38 | } |
c801d85f | 39 | |
91b8de8d | 40 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) |
c801d85f | 41 | { |
72a7edf0 | 42 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 43 | Create( bitmap, paletteIndex ); |
ff7b1510 | 44 | } |
c801d85f | 45 | |
91b8de8d | 46 | wxMask::wxMask( const wxBitmap& bitmap ) |
c801d85f | 47 | { |
72a7edf0 | 48 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 49 | Create( bitmap ); |
ff7b1510 | 50 | } |
c801d85f | 51 | |
8bbe427f | 52 | wxMask::~wxMask() |
c801d85f | 53 | { |
72a7edf0 RR |
54 | if (m_bitmap) |
55 | gdk_bitmap_unref( m_bitmap ); | |
ff7b1510 | 56 | } |
c801d85f | 57 | |
1fb4de31 RR |
58 | bool wxMask::Create( const wxBitmap& bitmap, |
59 | const wxColour& colour ) | |
91b8de8d RR |
60 | { |
61 | if (m_bitmap) | |
284b4c88 | 62 | { |
91b8de8d | 63 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 64 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 65 | } |
1fb4de31 RR |
66 | |
67 | wxImage image( bitmap ); | |
68 | if (!image.Ok()) return FALSE; | |
69 | ||
70 | GdkVisual *visual = gdk_visual_get_system(); | |
71 | ||
72 | GdkImage *mask_image = gdk_image_new( GDK_IMAGE_FASTEST, visual, image.GetWidth(), image.GetHeight() ); | |
73 | if (!mask_image) return FALSE; | |
74 | ||
75 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
76 | m_bitmap = gdk_pixmap_new( parent, image.GetWidth(), image.GetHeight(), 1 ); | |
77 | ||
78 | ||
79 | unsigned char *data = image.GetData(); | |
80 | int index = 0; | |
81 | ||
82 | unsigned char red = colour.Red(); | |
83 | unsigned char green = colour.Green(); | |
84 | unsigned char blue = colour.Blue(); | |
85 | ||
86 | int bpp = visual->depth; | |
87 | if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15; | |
88 | if (bpp == 15) | |
89 | { | |
90 | red = red & 0xf8; | |
91 | blue = blue & 0xf8; | |
92 | green = green & 0xf8; | |
93 | } | |
94 | if (bpp == 16) | |
95 | { | |
96 | red = red & 0xf8; | |
97 | blue = blue & 0xfc; | |
98 | green = green & 0xf8; | |
99 | } | |
100 | ||
101 | for (int j = 0; j < image.GetHeight(); j++) | |
102 | for (int i = 0; i < image.GetWidth(); i++) | |
103 | { | |
104 | if ((data[index] == red) && | |
105 | (data[index+1] == green) && | |
106 | (data[index+2] == blue)) | |
107 | { | |
108 | gdk_image_put_pixel( mask_image, i, j, 1 ); | |
109 | } | |
110 | else | |
111 | { | |
112 | gdk_image_put_pixel( mask_image, i, j, 1 ); | |
113 | } | |
114 | index += 3; | |
115 | } | |
116 | ||
117 | GdkGC *mask_gc = gdk_gc_new( m_bitmap ); | |
118 | ||
119 | gdk_draw_image( m_bitmap, mask_gc, mask_image, 0, 0, 0, 0, image.GetWidth(), image.GetHeight() ); | |
120 | ||
121 | gdk_gc_unref( mask_gc ); | |
122 | gdk_image_destroy( mask_image ); | |
123 | ||
91b8de8d RR |
124 | return FALSE; |
125 | } | |
126 | ||
284b4c88 VZ |
127 | bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap), |
128 | int WXUNUSED(paletteIndex) ) | |
91b8de8d RR |
129 | { |
130 | if (m_bitmap) | |
284b4c88 | 131 | { |
91b8de8d | 132 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 133 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 134 | } |
284b4c88 | 135 | |
223d09f6 | 136 | wxFAIL_MSG( wxT("not implemented") ); |
284b4c88 | 137 | |
91b8de8d RR |
138 | return FALSE; |
139 | } | |
140 | ||
141 | bool wxMask::Create( const wxBitmap& bitmap ) | |
142 | { | |
143 | if (m_bitmap) | |
284b4c88 | 144 | { |
91b8de8d | 145 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 146 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 147 | } |
284b4c88 | 148 | |
91b8de8d | 149 | if (!bitmap.Ok()) return FALSE; |
284b4c88 | 150 | |
223d09f6 | 151 | wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") ); |
284b4c88 | 152 | |
91b8de8d | 153 | m_bitmap = gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); |
284b4c88 | 154 | |
91b8de8d | 155 | if (!m_bitmap) return FALSE; |
284b4c88 | 156 | |
91b8de8d | 157 | GdkGC *gc = gdk_gc_new( m_bitmap ); |
284b4c88 | 158 | |
91b8de8d | 159 | gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() ); |
284b4c88 | 160 | |
91b8de8d | 161 | gdk_gc_unref( gc ); |
284b4c88 | 162 | |
91b8de8d RR |
163 | return TRUE; |
164 | } | |
165 | ||
166 | GdkBitmap *wxMask::GetBitmap() const | |
c801d85f | 167 | { |
fd0eed64 | 168 | return m_bitmap; |
ff7b1510 | 169 | } |
8bbe427f | 170 | |
c801d85f KB |
171 | //----------------------------------------------------------------------------- |
172 | // wxBitmap | |
173 | //----------------------------------------------------------------------------- | |
174 | ||
175 | class wxBitmapRefData: public wxObjectRefData | |
176 | { | |
fd0eed64 | 177 | public: |
91b8de8d RR |
178 | wxBitmapRefData(); |
179 | ~wxBitmapRefData(); | |
8bbe427f | 180 | |
fd0eed64 RR |
181 | GdkPixmap *m_pixmap; |
182 | GdkBitmap *m_bitmap; | |
183 | wxMask *m_mask; | |
184 | int m_width; | |
185 | int m_height; | |
186 | int m_bpp; | |
187 | wxPalette *m_palette; | |
c801d85f KB |
188 | }; |
189 | ||
8bbe427f | 190 | wxBitmapRefData::wxBitmapRefData() |
c801d85f | 191 | { |
fd0eed64 RR |
192 | m_pixmap = (GdkPixmap *) NULL; |
193 | m_bitmap = (GdkBitmap *) NULL; | |
194 | m_mask = (wxMask *) NULL; | |
195 | m_width = 0; | |
196 | m_height = 0; | |
197 | m_bpp = 0; | |
198 | m_palette = (wxPalette *) NULL; | |
ff7b1510 | 199 | } |
c801d85f | 200 | |
8bbe427f | 201 | wxBitmapRefData::~wxBitmapRefData() |
c801d85f | 202 | { |
fd0eed64 RR |
203 | if (m_pixmap) gdk_pixmap_unref( m_pixmap ); |
204 | if (m_bitmap) gdk_bitmap_unref( m_bitmap ); | |
205 | if (m_mask) delete m_mask; | |
206 | if (m_palette) delete m_palette; | |
ff7b1510 | 207 | } |
c801d85f KB |
208 | |
209 | //----------------------------------------------------------------------------- | |
210 | ||
211 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
212 | ||
213 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
214 | ||
8bbe427f | 215 | wxBitmap::wxBitmap() |
c801d85f | 216 | { |
fd0eed64 | 217 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
ff7b1510 | 218 | } |
8bbe427f | 219 | |
debe6624 | 220 | wxBitmap::wxBitmap( int width, int height, int depth ) |
c801d85f | 221 | { |
223d09f6 | 222 | wxCHECK_RET( (width > 0) && (height > 0), wxT("invalid bitmap size") ) |
284b4c88 | 223 | |
fd0eed64 | 224 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; |
eefa26be | 225 | if (depth == -1) depth = gdk_window_get_visual( parent )->depth; |
284b4c88 | 226 | |
eefa26be | 227 | wxCHECK_RET( (depth == gdk_window_get_visual( parent )->depth) || |
223d09f6 | 228 | (depth == 1), wxT("invalid bitmap depth") ) |
8bbe427f | 229 | |
eefa26be | 230 | m_refData = new wxBitmapRefData(); |
fd0eed64 | 231 | M_BMPDATA->m_mask = (wxMask *) NULL; |
fd0eed64 RR |
232 | M_BMPDATA->m_width = width; |
233 | M_BMPDATA->m_height = height; | |
eefa26be RR |
234 | if (depth == 1) |
235 | { | |
236 | M_BMPDATA->m_bitmap = gdk_pixmap_new( parent, width, height, 1 ); | |
237 | M_BMPDATA->m_bpp = 1; | |
238 | } | |
239 | else | |
240 | { | |
241 | M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth ); | |
242 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; | |
243 | } | |
8bbe427f | 244 | |
fd0eed64 | 245 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
ff7b1510 | 246 | } |
c801d85f | 247 | |
e838cc14 | 248 | bool wxBitmap::CreateFromXpm( const char **bits ) |
e52f60e6 | 249 | { |
e838cc14 | 250 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) |
8bbe427f | 251 | |
e52f60e6 RR |
252 | m_refData = new wxBitmapRefData(); |
253 | ||
254 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
255 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
8bbe427f | 256 | |
e52f60e6 | 257 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits ); |
8bbe427f | 258 | |
e838cc14 | 259 | wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") ); |
8bbe427f | 260 | |
fd0eed64 RR |
261 | if (mask) |
262 | { | |
263 | M_BMPDATA->m_mask = new wxMask(); | |
264 | M_BMPDATA->m_mask->m_bitmap = mask; | |
265 | } | |
8bbe427f | 266 | |
fd0eed64 | 267 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
8bbe427f | 268 | |
fd0eed64 | 269 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ? |
fd0eed64 | 270 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
e838cc14 VZ |
271 | |
272 | return TRUE; | |
ff7b1510 | 273 | } |
8bbe427f | 274 | |
c801d85f KB |
275 | wxBitmap::wxBitmap( const wxBitmap& bmp ) |
276 | { | |
fd0eed64 | 277 | Ref( bmp ); |
8bbe427f | 278 | |
fd0eed64 | 279 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
ff7b1510 | 280 | } |
6f65e337 | 281 | |
debe6624 | 282 | wxBitmap::wxBitmap( const wxString &filename, int type ) |
c801d85f | 283 | { |
fd0eed64 | 284 | LoadFile( filename, type ); |
8bbe427f | 285 | |
fd0eed64 | 286 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
ff7b1510 | 287 | } |
c801d85f | 288 | |
debe6624 | 289 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) |
6f65e337 | 290 | { |
fd0eed64 | 291 | m_refData = new wxBitmapRefData(); |
6f65e337 | 292 | |
fd0eed64 | 293 | M_BMPDATA->m_mask = (wxMask *) NULL; |
8bbe427f | 294 | M_BMPDATA->m_bitmap = |
fd0eed64 RR |
295 | gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height ); |
296 | M_BMPDATA->m_width = width; | |
297 | M_BMPDATA->m_height = height; | |
298 | M_BMPDATA->m_bpp = 1; | |
6f65e337 | 299 | |
223d09f6 | 300 | wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") ); |
8bbe427f | 301 | |
fd0eed64 | 302 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
6f65e337 | 303 | } |
8bbe427f VZ |
304 | |
305 | wxBitmap::~wxBitmap() | |
c801d85f | 306 | { |
fd0eed64 | 307 | if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this); |
ff7b1510 | 308 | } |
8bbe427f | 309 | |
c801d85f KB |
310 | wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp ) |
311 | { | |
8bbe427f VZ |
312 | if (*this == bmp) return (*this); |
313 | Ref( bmp ); | |
314 | return *this; | |
ff7b1510 | 315 | } |
8bbe427f | 316 | |
c801d85f KB |
317 | bool wxBitmap::operator == ( const wxBitmap& bmp ) |
318 | { | |
8bbe427f | 319 | return m_refData == bmp.m_refData; |
ff7b1510 | 320 | } |
8bbe427f | 321 | |
c801d85f KB |
322 | bool wxBitmap::operator != ( const wxBitmap& bmp ) |
323 | { | |
8bbe427f | 324 | return m_refData != bmp.m_refData; |
ff7b1510 | 325 | } |
8bbe427f | 326 | |
91b8de8d | 327 | bool wxBitmap::Ok() const |
c801d85f | 328 | { |
fd0eed64 | 329 | return (m_refData != NULL); |
ff7b1510 | 330 | } |
8bbe427f | 331 | |
91b8de8d | 332 | int wxBitmap::GetHeight() const |
c801d85f | 333 | { |
223d09f6 | 334 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
e55ad60e | 335 | |
fd0eed64 | 336 | return M_BMPDATA->m_height; |
ff7b1510 | 337 | } |
c801d85f | 338 | |
91b8de8d | 339 | int wxBitmap::GetWidth() const |
c801d85f | 340 | { |
223d09f6 | 341 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 342 | |
fd0eed64 | 343 | return M_BMPDATA->m_width; |
ff7b1510 | 344 | } |
c801d85f | 345 | |
91b8de8d | 346 | int wxBitmap::GetDepth() const |
c801d85f | 347 | { |
223d09f6 | 348 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 349 | |
fd0eed64 | 350 | return M_BMPDATA->m_bpp; |
ff7b1510 | 351 | } |
c801d85f | 352 | |
91b8de8d | 353 | wxMask *wxBitmap::GetMask() const |
c801d85f | 354 | { |
223d09f6 | 355 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 356 | |
fd0eed64 | 357 | return M_BMPDATA->m_mask; |
ff7b1510 | 358 | } |
c801d85f KB |
359 | |
360 | void wxBitmap::SetMask( wxMask *mask ) | |
361 | { | |
223d09f6 | 362 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); |
8bbe427f | 363 | |
fd0eed64 | 364 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
8bbe427f | 365 | |
fd0eed64 | 366 | M_BMPDATA->m_mask = mask; |
ff7b1510 | 367 | } |
c801d85f | 368 | |
fd0eed64 | 369 | bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) ) |
c801d85f | 370 | { |
223d09f6 | 371 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") ); |
8bbe427f | 372 | |
b75dd496 | 373 | // Try to save the bitmap via wxImage handlers: |
fd0eed64 | 374 | { |
4bc67cc5 | 375 | wxImage image( *this ); |
284b4c88 | 376 | if (image.Ok()) return image.SaveFile( name, type ); |
fd0eed64 | 377 | } |
8bbe427f | 378 | |
fd0eed64 | 379 | return FALSE; |
ff7b1510 | 380 | } |
c801d85f | 381 | |
fd0eed64 | 382 | bool wxBitmap::LoadFile( const wxString &name, int type ) |
c801d85f | 383 | { |
fd0eed64 | 384 | UnRef(); |
8bbe427f | 385 | |
fd0eed64 | 386 | if (!wxFileExists(name)) return FALSE; |
8bbe427f | 387 | |
fd0eed64 RR |
388 | if (type == wxBITMAP_TYPE_XPM) |
389 | { | |
390 | m_refData = new wxBitmapRefData(); | |
8bbe427f | 391 | |
fd0eed64 RR |
392 | GdkBitmap *mask = (GdkBitmap*) NULL; |
393 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
8bbe427f | 394 | |
62bd5cf0 | 395 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() ); |
8bbe427f | 396 | |
fd0eed64 RR |
397 | if (mask) |
398 | { | |
399 | M_BMPDATA->m_mask = new wxMask(); | |
400 | M_BMPDATA->m_mask->m_bitmap = mask; | |
401 | } | |
8bbe427f | 402 | |
fd0eed64 | 403 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
8bbe427f | 404 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; |
fd0eed64 | 405 | } |
b75dd496 | 406 | else // try if wxImage can load it |
fd0eed64 RR |
407 | { |
408 | wxImage image; | |
b75dd496 | 409 | if (!image.LoadFile( name, type )) return FALSE; |
4bc67cc5 | 410 | if (image.Ok()) *this = image.ConvertToBitmap(); |
b75dd496 | 411 | else return FALSE; |
fd0eed64 | 412 | } |
8bbe427f | 413 | |
fd0eed64 | 414 | return TRUE; |
ff7b1510 | 415 | } |
8bbe427f | 416 | |
91b8de8d | 417 | wxPalette *wxBitmap::GetPalette() const |
c801d85f | 418 | { |
fd0eed64 | 419 | if (!Ok()) return (wxPalette *) NULL; |
8bbe427f | 420 | |
fd0eed64 | 421 | return M_BMPDATA->m_palette; |
ff7b1510 | 422 | } |
c801d85f | 423 | |
4bc67cc5 RR |
424 | void wxBitmap::SetHeight( int height ) |
425 | { | |
426 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
427 | ||
428 | M_BMPDATA->m_height = height; | |
429 | } | |
430 | ||
431 | void wxBitmap::SetWidth( int width ) | |
432 | { | |
433 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
434 | ||
435 | M_BMPDATA->m_width = width; | |
436 | } | |
437 | ||
438 | void wxBitmap::SetDepth( int depth ) | |
439 | { | |
440 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
441 | ||
442 | M_BMPDATA->m_bpp = depth; | |
443 | } | |
444 | ||
445 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
446 | { | |
447 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
448 | ||
449 | M_BMPDATA->m_pixmap = pixmap; | |
450 | } | |
451 | ||
91b8de8d | 452 | GdkPixmap *wxBitmap::GetPixmap() const |
c801d85f | 453 | { |
223d09f6 | 454 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 455 | |
fd0eed64 | 456 | return M_BMPDATA->m_pixmap; |
ff7b1510 | 457 | } |
8bbe427f | 458 | |
91b8de8d | 459 | GdkBitmap *wxBitmap::GetBitmap() const |
6f65e337 | 460 | { |
223d09f6 | 461 | wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 462 | |
fd0eed64 | 463 | return M_BMPDATA->m_bitmap; |
ff7b1510 | 464 | } |