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