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