]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bitmap.cpp
IsExposed() corrections in calendar.
[wxWidgets.git] / src / gtk / bitmap.cpp
CommitLineData
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 17#include "wx/image.h"
f9ee644e 18#include "wx/dcmemory.h"
83624f79 19
20e05ffb
RR
20#include <gdk/gdk.h>
21#include <gdk/gdkprivate.h>
22#include <gdk/gdkx.h>
c801d85f
KB
23
24//-----------------------------------------------------------------------------
25// wxMask
26//-----------------------------------------------------------------------------
27
28IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
29
8bbe427f 30wxMask::wxMask()
c801d85f 31{
fd0eed64 32 m_bitmap = (GdkBitmap *) NULL;
ff7b1510 33}
c801d85f 34
91b8de8d 35wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 36{
72a7edf0 37 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 38 Create( bitmap, colour );
ff7b1510 39}
c801d85f 40
91b8de8d 41wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
c801d85f 42{
72a7edf0 43 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 44 Create( bitmap, paletteIndex );
ff7b1510 45}
c801d85f 46
91b8de8d 47wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 48{
72a7edf0 49 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 50 Create( bitmap );
ff7b1510 51}
c801d85f 52
8bbe427f 53wxMask::~wxMask()
c801d85f 54{
72a7edf0
RR
55 if (m_bitmap)
56 gdk_bitmap_unref( m_bitmap );
ff7b1510 57}
c801d85f 58
1fb4de31
RR
59bool wxMask::Create( const wxBitmap& bitmap,
60 const wxColour& colour )
91b8de8d
RR
61{
62 if (m_bitmap)
284b4c88 63 {
91b8de8d 64 gdk_bitmap_unref( m_bitmap );
284b4c88 65 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 66 }
1fb4de31
RR
67
68 wxImage image( bitmap );
69 if (!image.Ok()) return FALSE;
70
1fb4de31
RR
71 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
72 m_bitmap = gdk_pixmap_new( parent, image.GetWidth(), image.GetHeight(), 1 );
f9ee644e 73 GdkGC *gc = gdk_gc_new( m_bitmap );
1fb4de31 74
f9ee644e
RR
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() );
1fb4de31
RR
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
f9ee644e 91 GdkVisual *visual = gdk_visual_get_system();
1fb4de31
RR
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
f9ee644e
RR
107 color.red = 0;
108 color.green = 0;
109 color.blue = 0;
110 color.pixel = 0;
111 gdk_gc_set_foreground( gc, &color );
112
1fb4de31 113 for (int j = 0; j < image.GetHeight(); j++)
f9ee644e 114 {
f2593d0d
RR
115 int start_x = -1;
116 int i;
117 for (i = 0; i < image.GetWidth(); i++)
1fb4de31 118 {
f2593d0d
RR
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 }
f9ee644e
RR
133 }
134 index += 3;
135 }
136 if (start_x != -1)
137 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
138 }
1fb4de31 139
f9ee644e 140 gdk_gc_unref( gc );
1fb4de31 141
f9ee644e 142 return TRUE;
91b8de8d
RR
143}
144
284b4c88
VZ
145bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap),
146 int WXUNUSED(paletteIndex) )
91b8de8d
RR
147{
148 if (m_bitmap)
284b4c88 149 {
91b8de8d 150 gdk_bitmap_unref( m_bitmap );
284b4c88 151 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 152 }
284b4c88 153
223d09f6 154 wxFAIL_MSG( wxT("not implemented") );
284b4c88 155
91b8de8d
RR
156 return FALSE;
157}
158
159bool wxMask::Create( const wxBitmap& bitmap )
160{
161 if (m_bitmap)
284b4c88 162 {
91b8de8d 163 gdk_bitmap_unref( m_bitmap );
284b4c88 164 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 165 }
284b4c88 166
91b8de8d 167 if (!bitmap.Ok()) return FALSE;
284b4c88 168
223d09f6 169 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
284b4c88 170
91b8de8d 171 m_bitmap = gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
284b4c88 172
91b8de8d 173 if (!m_bitmap) return FALSE;
284b4c88 174
91b8de8d 175 GdkGC *gc = gdk_gc_new( m_bitmap );
284b4c88 176
91b8de8d 177 gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
284b4c88 178
91b8de8d 179 gdk_gc_unref( gc );
284b4c88 180
91b8de8d
RR
181 return TRUE;
182}
183
184GdkBitmap *wxMask::GetBitmap() const
c801d85f 185{
fd0eed64 186 return m_bitmap;
ff7b1510 187}
8bbe427f 188
c801d85f
KB
189//-----------------------------------------------------------------------------
190// wxBitmap
191//-----------------------------------------------------------------------------
192
193class wxBitmapRefData: public wxObjectRefData
194{
fd0eed64 195public:
f2593d0d
RR
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;
c801d85f
KB
206};
207
8bbe427f 208wxBitmapRefData::wxBitmapRefData()
c801d85f 209{
fd0eed64
RR
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;
ff7b1510 217}
c801d85f 218
8bbe427f 219wxBitmapRefData::~wxBitmapRefData()
c801d85f 220{
fd0eed64
RR
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;
ff7b1510 225}
c801d85f
KB
226
227//-----------------------------------------------------------------------------
228
229#define M_BMPDATA ((wxBitmapRefData *)m_refData)
230
231IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
232
8bbe427f 233wxBitmap::wxBitmap()
c801d85f 234{
fd0eed64 235 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 236}
8bbe427f 237
debe6624 238wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 239{
223d09f6 240 wxCHECK_RET( (width > 0) && (height > 0), wxT("invalid bitmap size") )
284b4c88 241
fd0eed64 242 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
eefa26be 243 if (depth == -1) depth = gdk_window_get_visual( parent )->depth;
284b4c88 244
eefa26be 245 wxCHECK_RET( (depth == gdk_window_get_visual( parent )->depth) ||
223d09f6 246 (depth == 1), wxT("invalid bitmap depth") )
8bbe427f 247
eefa26be 248 m_refData = new wxBitmapRefData();
fd0eed64 249 M_BMPDATA->m_mask = (wxMask *) NULL;
fd0eed64
RR
250 M_BMPDATA->m_width = width;
251 M_BMPDATA->m_height = height;
eefa26be
RR
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 }
8bbe427f 262
fd0eed64 263 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 264}
c801d85f 265
e838cc14 266bool wxBitmap::CreateFromXpm( const char **bits )
e52f60e6 267{
e838cc14 268 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
8bbe427f 269
e52f60e6
RR
270 m_refData = new wxBitmapRefData();
271
272 GdkBitmap *mask = (GdkBitmap*) NULL;
273 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 274
e52f60e6 275 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
8bbe427f 276
e838cc14 277 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
8bbe427f 278
fd0eed64
RR
279 if (mask)
280 {
281 M_BMPDATA->m_mask = new wxMask();
282 M_BMPDATA->m_mask->m_bitmap = mask;
283 }
8bbe427f 284
fd0eed64 285 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 286
fd0eed64 287 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
fd0eed64 288 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
e838cc14
VZ
289
290 return TRUE;
ff7b1510 291}
8bbe427f 292
c801d85f
KB
293wxBitmap::wxBitmap( const wxBitmap& bmp )
294{
fd0eed64 295 Ref( bmp );
8bbe427f 296
fd0eed64 297 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 298}
6f65e337 299
debe6624 300wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f 301{
fd0eed64 302 LoadFile( filename, type );
8bbe427f 303
fd0eed64 304 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 305}
c801d85f 306
debe6624 307wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 308{
fd0eed64 309 m_refData = new wxBitmapRefData();
6f65e337 310
fd0eed64 311 M_BMPDATA->m_mask = (wxMask *) NULL;
8bbe427f 312 M_BMPDATA->m_bitmap =
fd0eed64
RR
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;
6f65e337 317
223d09f6 318 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
8bbe427f 319
fd0eed64 320 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
6f65e337 321}
8bbe427f
VZ
322
323wxBitmap::~wxBitmap()
c801d85f 324{
fd0eed64 325 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
ff7b1510 326}
8bbe427f 327
c801d85f
KB
328wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
329{
8bbe427f
VZ
330 if (*this == bmp) return (*this);
331 Ref( bmp );
332 return *this;
ff7b1510 333}
8bbe427f 334
c801d85f
KB
335bool wxBitmap::operator == ( const wxBitmap& bmp )
336{
8bbe427f 337 return m_refData == bmp.m_refData;
ff7b1510 338}
8bbe427f 339
c801d85f
KB
340bool wxBitmap::operator != ( const wxBitmap& bmp )
341{
8bbe427f 342 return m_refData != bmp.m_refData;
ff7b1510 343}
8bbe427f 344
91b8de8d 345bool wxBitmap::Ok() const
c801d85f 346{
fd0eed64 347 return (m_refData != NULL);
ff7b1510 348}
8bbe427f 349
91b8de8d 350int wxBitmap::GetHeight() const
c801d85f 351{
223d09f6 352 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 353
fd0eed64 354 return M_BMPDATA->m_height;
ff7b1510 355}
c801d85f 356
91b8de8d 357int wxBitmap::GetWidth() const
c801d85f 358{
223d09f6 359 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 360
fd0eed64 361 return M_BMPDATA->m_width;
ff7b1510 362}
c801d85f 363
91b8de8d 364int wxBitmap::GetDepth() const
c801d85f 365{
223d09f6 366 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 367
fd0eed64 368 return M_BMPDATA->m_bpp;
ff7b1510 369}
c801d85f 370
91b8de8d 371wxMask *wxBitmap::GetMask() const
c801d85f 372{
223d09f6 373 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 374
fd0eed64 375 return M_BMPDATA->m_mask;
ff7b1510 376}
c801d85f
KB
377
378void wxBitmap::SetMask( wxMask *mask )
379{
223d09f6 380 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 381
fd0eed64 382 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 383
fd0eed64 384 M_BMPDATA->m_mask = mask;
ff7b1510 385}
c801d85f 386
17bec151
RR
387wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
388{
389 wxCHECK_MSG( Ok() &&
390 (rect.x >= 0) && (rect.y >= 0) &&
f2593d0d 391 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
17bec151
RR
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() );
f2593d0d
RR
400 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
401 gdk_gc_destroy( gc );
17bec151
RR
402 }
403 else
404 {
405 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
f2593d0d
RR
406 gdk_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
407 gdk_gc_destroy( gc );
17bec151
RR
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 );
f2593d0d
RR
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 );
17bec151 419
f2593d0d 420 ret.SetMask( mask );
17bec151
RR
421 }
422
423 return ret;
424}
425
fd0eed64 426bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 427{
223d09f6 428 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
8bbe427f 429
b75dd496 430 // Try to save the bitmap via wxImage handlers:
fd0eed64 431 {
4bc67cc5 432 wxImage image( *this );
284b4c88 433 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 434 }
8bbe427f 435
fd0eed64 436 return FALSE;
ff7b1510 437}
c801d85f 438
fd0eed64 439bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 440{
fd0eed64 441 UnRef();
8bbe427f 442
fd0eed64 443 if (!wxFileExists(name)) return FALSE;
8bbe427f 444
fd0eed64
RR
445 if (type == wxBITMAP_TYPE_XPM)
446 {
447 m_refData = new wxBitmapRefData();
8bbe427f 448
fd0eed64
RR
449 GdkBitmap *mask = (GdkBitmap*) NULL;
450 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 451
62bd5cf0 452 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() );
8bbe427f 453
fd0eed64
RR
454 if (mask)
455 {
456 M_BMPDATA->m_mask = new wxMask();
457 M_BMPDATA->m_mask->m_bitmap = mask;
458 }
8bbe427f 459
fd0eed64 460 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 461 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
fd0eed64 462 }
b75dd496 463 else // try if wxImage can load it
fd0eed64
RR
464 {
465 wxImage image;
b75dd496 466 if (!image.LoadFile( name, type )) return FALSE;
4bc67cc5 467 if (image.Ok()) *this = image.ConvertToBitmap();
b75dd496 468 else return FALSE;
fd0eed64 469 }
8bbe427f 470
fd0eed64 471 return TRUE;
ff7b1510 472}
8bbe427f 473
91b8de8d 474wxPalette *wxBitmap::GetPalette() const
c801d85f 475{
fd0eed64 476 if (!Ok()) return (wxPalette *) NULL;
8bbe427f 477
fd0eed64 478 return M_BMPDATA->m_palette;
ff7b1510 479}
c801d85f 480
4bc67cc5
RR
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
91b8de8d 509GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 510{
223d09f6 511 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 512
fd0eed64 513 return M_BMPDATA->m_pixmap;
ff7b1510 514}
8bbe427f 515
91b8de8d 516GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 517{
223d09f6 518 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 519
fd0eed64 520 return M_BMPDATA->m_bitmap;
ff7b1510 521}