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