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