]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bitmap.cpp
bbde88901498891389b2f360631430c60bf34420
[wxWidgets.git] / src / gtk1 / bitmap.cpp
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 "wx/image.h"
18 #include "wx/dcmemory.h"
19
20 #include <gdk/gdk.h>
21 #include <gtk/gtk.h>
22
23 //-----------------------------------------------------------------------------
24 // data
25 //-----------------------------------------------------------------------------
26
27 extern GtkWidget *wxRootWindow;
28
29 //-----------------------------------------------------------------------------
30 // wxMask
31 //-----------------------------------------------------------------------------
32
33 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
34
35 wxMask::wxMask()
36 {
37 m_bitmap = (GdkBitmap *) NULL;
38 }
39
40 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
41 {
42 m_bitmap = (GdkBitmap *) NULL;
43 Create( bitmap, colour );
44 }
45
46 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
47 {
48 m_bitmap = (GdkBitmap *) NULL;
49 Create( bitmap, paletteIndex );
50 }
51
52 wxMask::wxMask( const wxBitmap& bitmap )
53 {
54 m_bitmap = (GdkBitmap *) NULL;
55 Create( bitmap );
56 }
57
58 wxMask::~wxMask()
59 {
60 if (m_bitmap)
61 gdk_bitmap_unref( m_bitmap );
62 }
63
64 bool wxMask::Create( const wxBitmap& bitmap,
65 const wxColour& colour )
66 {
67 if (m_bitmap)
68 {
69 gdk_bitmap_unref( m_bitmap );
70 m_bitmap = (GdkBitmap*) NULL;
71 }
72
73 wxImage image( bitmap );
74 if (!image.Ok()) return FALSE;
75
76 m_bitmap = gdk_pixmap_new( wxRootWindow->window, image.GetWidth(), image.GetHeight(), 1 );
77 GdkGC *gc = gdk_gc_new( m_bitmap );
78
79 GdkColor color;
80 color.red = 65000;
81 color.green = 65000;
82 color.blue = 65000;
83 color.pixel = 1;
84 gdk_gc_set_foreground( gc, &color );
85 gdk_gc_set_fill( gc, GDK_SOLID );
86 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
87
88 unsigned char *data = image.GetData();
89 int index = 0;
90
91 unsigned char red = colour.Red();
92 unsigned char green = colour.Green();
93 unsigned char blue = colour.Blue();
94
95 GdkVisual *visual = gdk_visual_get_system();
96 int bpp = visual->depth;
97 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
98 if (bpp == 15)
99 {
100 red = red & 0xf8;
101 blue = blue & 0xf8;
102 green = green & 0xf8;
103 }
104 if (bpp == 16)
105 {
106 red = red & 0xf8;
107 blue = blue & 0xfc;
108 green = green & 0xf8;
109 }
110
111 color.red = 0;
112 color.green = 0;
113 color.blue = 0;
114 color.pixel = 0;
115 gdk_gc_set_foreground( gc, &color );
116
117 for (int j = 0; j < image.GetHeight(); j++)
118 {
119 int start_x = -1;
120 int i;
121 for (i = 0; i < image.GetWidth(); i++)
122 {
123 if ((data[index] == red) &&
124 (data[index+1] == green) &&
125 (data[index+2] == blue))
126 {
127 if (start_x == -1)
128 start_x = i;
129 }
130 else
131 {
132 if (start_x != -1)
133 {
134 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
135 start_x = -1;
136 }
137 }
138 index += 3;
139 }
140 if (start_x != -1)
141 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
142 }
143
144 gdk_gc_unref( gc );
145
146 return TRUE;
147 }
148
149 bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap),
150 int WXUNUSED(paletteIndex) )
151 {
152 if (m_bitmap)
153 {
154 gdk_bitmap_unref( m_bitmap );
155 m_bitmap = (GdkBitmap*) NULL;
156 }
157
158 wxFAIL_MSG( wxT("not implemented") );
159
160 return FALSE;
161 }
162
163 bool wxMask::Create( const wxBitmap& bitmap )
164 {
165 if (m_bitmap)
166 {
167 gdk_bitmap_unref( m_bitmap );
168 m_bitmap = (GdkBitmap*) NULL;
169 }
170
171 if (!bitmap.Ok()) return FALSE;
172
173 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
174
175 m_bitmap = gdk_pixmap_new( wxRootWindow->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
176
177 if (!m_bitmap) return FALSE;
178
179 GdkGC *gc = gdk_gc_new( m_bitmap );
180
181 gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
182
183 gdk_gc_unref( gc );
184
185 return TRUE;
186 }
187
188 GdkBitmap *wxMask::GetBitmap() const
189 {
190 return m_bitmap;
191 }
192
193 //-----------------------------------------------------------------------------
194 // wxBitmap
195 //-----------------------------------------------------------------------------
196
197 class wxBitmapRefData: public wxObjectRefData
198 {
199 public:
200 wxBitmapRefData();
201 ~wxBitmapRefData();
202
203 GdkPixmap *m_pixmap;
204 GdkBitmap *m_bitmap;
205 wxMask *m_mask;
206 int m_width;
207 int m_height;
208 int m_bpp;
209 wxPalette *m_palette;
210 };
211
212 wxBitmapRefData::wxBitmapRefData()
213 {
214 m_pixmap = (GdkPixmap *) NULL;
215 m_bitmap = (GdkBitmap *) NULL;
216 m_mask = (wxMask *) NULL;
217 m_width = 0;
218 m_height = 0;
219 m_bpp = 0;
220 m_palette = (wxPalette *) NULL;
221 }
222
223 wxBitmapRefData::~wxBitmapRefData()
224 {
225 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
226 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
227 if (m_mask) delete m_mask;
228 if (m_palette) delete m_palette;
229 }
230
231 //-----------------------------------------------------------------------------
232
233 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
234
235 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
236
237 wxBitmap::wxBitmap()
238 {
239 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
240 }
241
242 wxBitmap::wxBitmap( int width, int height, int depth )
243 {
244 wxCHECK_RET( (width > 0) && (height > 0), wxT("invalid bitmap size") )
245
246 if (depth == -1) depth = gdk_window_get_visual( wxRootWindow->window )->depth;
247
248 wxCHECK_RET( (depth == gdk_window_get_visual( wxRootWindow->window )->depth) ||
249 (depth == 1), wxT("invalid bitmap depth") )
250
251 m_refData = new wxBitmapRefData();
252 M_BMPDATA->m_mask = (wxMask *) NULL;
253 M_BMPDATA->m_width = width;
254 M_BMPDATA->m_height = height;
255 if (depth == 1)
256 {
257 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxRootWindow->window, width, height, 1 );
258 M_BMPDATA->m_bpp = 1;
259 }
260 else
261 {
262 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxRootWindow->window, width, height, depth );
263 M_BMPDATA->m_bpp = gdk_window_get_visual( wxRootWindow->window )->depth;
264 }
265
266 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
267 }
268
269 bool wxBitmap::CreateFromXpm( const char **bits )
270 {
271 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
272
273 m_refData = new wxBitmapRefData();
274
275 GdkBitmap *mask = (GdkBitmap*) NULL;
276
277 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxRootWindow->window, &mask, NULL, (gchar **) bits );
278
279 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
280
281 if (mask)
282 {
283 M_BMPDATA->m_mask = new wxMask();
284 M_BMPDATA->m_mask->m_bitmap = mask;
285 }
286
287 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
288
289 M_BMPDATA->m_bpp = gdk_window_get_visual( wxRootWindow->window )->depth; // ?
290 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
291
292 return TRUE;
293 }
294
295 wxBitmap::wxBitmap( const wxBitmap& bmp )
296 {
297 Ref( bmp );
298
299 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
300 }
301
302 wxBitmap::wxBitmap( const wxString &filename, int type )
303 {
304 LoadFile( filename, type );
305
306 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
307 }
308
309 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
310 {
311 m_refData = new wxBitmapRefData();
312
313 M_BMPDATA->m_mask = (wxMask *) NULL;
314 M_BMPDATA->m_bitmap =
315 gdk_bitmap_create_from_data( wxRootWindow->window, (gchar *) bits, width, height );
316 M_BMPDATA->m_width = width;
317 M_BMPDATA->m_height = height;
318 M_BMPDATA->m_bpp = 1;
319
320 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
321
322 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
323 }
324
325 wxBitmap::~wxBitmap()
326 {
327 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
328 }
329
330 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
331 {
332 if (*this == bmp) return (*this);
333 Ref( bmp );
334 return *this;
335 }
336
337 bool wxBitmap::operator == ( const wxBitmap& bmp )
338 {
339 return m_refData == bmp.m_refData;
340 }
341
342 bool wxBitmap::operator != ( const wxBitmap& bmp )
343 {
344 return m_refData != bmp.m_refData;
345 }
346
347 bool wxBitmap::Ok() const
348 {
349 return (m_refData != NULL);
350 }
351
352 int wxBitmap::GetHeight() const
353 {
354 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
355
356 return M_BMPDATA->m_height;
357 }
358
359 int wxBitmap::GetWidth() const
360 {
361 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
362
363 return M_BMPDATA->m_width;
364 }
365
366 int wxBitmap::GetDepth() const
367 {
368 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
369
370 return M_BMPDATA->m_bpp;
371 }
372
373 wxMask *wxBitmap::GetMask() const
374 {
375 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
376
377 return M_BMPDATA->m_mask;
378 }
379
380 void wxBitmap::SetMask( wxMask *mask )
381 {
382 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
383
384 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
385
386 M_BMPDATA->m_mask = mask;
387 }
388
389 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
390 {
391 wxCHECK_MSG( Ok() &&
392 (rect.x >= 0) && (rect.y >= 0) &&
393 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
394 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
395
396 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
397 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
398
399 if (ret.GetPixmap())
400 {
401 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
402 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
403 gdk_gc_destroy( gc );
404 }
405 else
406 {
407 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
408 gdk_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
409 gdk_gc_destroy( gc );
410 }
411
412 if (GetMask())
413 {
414 wxMask *mask = new wxMask;
415 mask->m_bitmap = gdk_pixmap_new( wxRootWindow->window, rect.width, rect.height, 1 );
416
417 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
418 gdk_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
419 gdk_gc_destroy( gc );
420
421 ret.SetMask( mask );
422 }
423
424 return ret;
425 }
426
427 bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
428 {
429 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
430
431 // Try to save the bitmap via wxImage handlers:
432 {
433 wxImage image( *this );
434 if (image.Ok()) return image.SaveFile( name, type );
435 }
436
437 return FALSE;
438 }
439
440 bool wxBitmap::LoadFile( const wxString &name, int type )
441 {
442 UnRef();
443
444 if (!wxFileExists(name)) return FALSE;
445
446 if (type == wxBITMAP_TYPE_XPM)
447 {
448 m_refData = new wxBitmapRefData();
449
450 GdkBitmap *mask = (GdkBitmap*) NULL;
451
452 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( wxRootWindow->window, &mask, NULL, name.fn_str() );
453
454 if (mask)
455 {
456 M_BMPDATA->m_mask = new wxMask();
457 M_BMPDATA->m_mask->m_bitmap = mask;
458 }
459
460 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
461 M_BMPDATA->m_bpp = gdk_window_get_visual( wxRootWindow->window )->depth;
462 }
463 else // try if wxImage can load it
464 {
465 wxImage image;
466 if (!image.LoadFile( name, type )) return FALSE;
467 if (image.Ok()) *this = image.ConvertToBitmap();
468 else return FALSE;
469 }
470
471 return TRUE;
472 }
473
474 wxPalette *wxBitmap::GetPalette() const
475 {
476 if (!Ok()) return (wxPalette *) NULL;
477
478 return M_BMPDATA->m_palette;
479 }
480
481 void wxBitmap::SetHeight( int height )
482 {
483 if (!m_refData) m_refData = new wxBitmapRefData();
484
485 M_BMPDATA->m_height = height;
486 }
487
488 void wxBitmap::SetWidth( int width )
489 {
490 if (!m_refData) m_refData = new wxBitmapRefData();
491
492 M_BMPDATA->m_width = width;
493 }
494
495 void wxBitmap::SetDepth( int depth )
496 {
497 if (!m_refData) m_refData = new wxBitmapRefData();
498
499 M_BMPDATA->m_bpp = depth;
500 }
501
502 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
503 {
504 if (!m_refData) m_refData = new wxBitmapRefData();
505
506 M_BMPDATA->m_pixmap = pixmap;
507 }
508
509 void wxBitmap::SetBitmap( GdkPixmap *bitmap )
510 {
511 if (!m_refData) m_refData = new wxBitmapRefData();
512
513 M_BMPDATA->m_bitmap = bitmap;
514 }
515
516 GdkPixmap *wxBitmap::GetPixmap() const
517 {
518 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
519
520 return M_BMPDATA->m_pixmap;
521 }
522
523 GdkBitmap *wxBitmap::GetBitmap() const
524 {
525 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
526
527 return M_BMPDATA->m_bitmap;
528 }