]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
added version info hidden dialog
[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 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
f9ee644e 95 GdkVisual *visual = gdk_visual_get_system();
1fb4de31
RR
96 int bpp = visual->depth;
97 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
98 if (bpp == 15)
99 {
100 red = red & 0xf8;
101 blue = blue & 0xf8;
102 green = green & 0xf8;
103 }
104 if (bpp == 16)
105 {
106 red = red & 0xf8;
107 blue = blue & 0xfc;
108 green = green & 0xf8;
109 }
13111b2a 110
f9ee644e
RR
111 color.red = 0;
112 color.green = 0;
113 color.blue = 0;
114 color.pixel = 0;
115 gdk_gc_set_foreground( gc, &color );
13111b2a 116
1fb4de31 117 for (int j = 0; j < image.GetHeight(); j++)
f9ee644e 118 {
f2593d0d
RR
119 int start_x = -1;
120 int i;
121 for (i = 0; i < image.GetWidth(); i++)
1fb4de31 122 {
13111b2a
VZ
123 if ((data[index] == red) &&
124 (data[index+1] == green) &&
125 (data[index+2] == blue))
126 {
127 if (start_x == -1)
128 start_x = i;
129 }
130 else
131 {
132 if (start_x != -1)
133 {
134 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
135 start_x = -1;
136 }
f9ee644e
RR
137 }
138 index += 3;
139 }
140 if (start_x != -1)
141 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
142 }
1fb4de31 143
f9ee644e 144 gdk_gc_unref( gc );
1fb4de31 145
f9ee644e 146 return TRUE;
91b8de8d
RR
147}
148
284b4c88
VZ
149bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap),
150 int WXUNUSED(paletteIndex) )
91b8de8d
RR
151{
152 if (m_bitmap)
284b4c88 153 {
91b8de8d 154 gdk_bitmap_unref( m_bitmap );
284b4c88 155 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 156 }
284b4c88 157
223d09f6 158 wxFAIL_MSG( wxT("not implemented") );
284b4c88 159
91b8de8d
RR
160 return FALSE;
161}
162
163bool wxMask::Create( const wxBitmap& bitmap )
164{
165 if (m_bitmap)
284b4c88 166 {
91b8de8d 167 gdk_bitmap_unref( m_bitmap );
284b4c88 168 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 169 }
284b4c88 170
91b8de8d 171 if (!bitmap.Ok()) return FALSE;
284b4c88 172
223d09f6 173 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
284b4c88 174
d76fe38b 175 m_bitmap = gdk_pixmap_new( wxRootWindow->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
284b4c88 176
91b8de8d 177 if (!m_bitmap) return FALSE;
284b4c88 178
91b8de8d 179 GdkGC *gc = gdk_gc_new( m_bitmap );
284b4c88 180
91b8de8d 181 gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
284b4c88 182
91b8de8d 183 gdk_gc_unref( gc );
284b4c88 184
91b8de8d
RR
185 return TRUE;
186}
187
188GdkBitmap *wxMask::GetBitmap() const
c801d85f 189{
fd0eed64 190 return m_bitmap;
ff7b1510 191}
8bbe427f 192
c801d85f
KB
193//-----------------------------------------------------------------------------
194// wxBitmap
195//-----------------------------------------------------------------------------
196
197class wxBitmapRefData: public wxObjectRefData
198{
fd0eed64 199public:
f2593d0d
RR
200 wxBitmapRefData();
201 ~wxBitmapRefData();
202
203 GdkPixmap *m_pixmap;
204 GdkBitmap *m_bitmap;
205 wxMask *m_mask;
206 int m_width;
207 int m_height;
208 int m_bpp;
209 wxPalette *m_palette;
c801d85f
KB
210};
211
8bbe427f 212wxBitmapRefData::wxBitmapRefData()
c801d85f 213{
fd0eed64
RR
214 m_pixmap = (GdkPixmap *) NULL;
215 m_bitmap = (GdkBitmap *) NULL;
216 m_mask = (wxMask *) NULL;
217 m_width = 0;
218 m_height = 0;
219 m_bpp = 0;
220 m_palette = (wxPalette *) NULL;
ff7b1510 221}
c801d85f 222
8bbe427f 223wxBitmapRefData::~wxBitmapRefData()
c801d85f 224{
fd0eed64
RR
225 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
226 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
227 if (m_mask) delete m_mask;
228 if (m_palette) delete m_palette;
ff7b1510 229}
c801d85f
KB
230
231//-----------------------------------------------------------------------------
232
233#define M_BMPDATA ((wxBitmapRefData *)m_refData)
234
235IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
236
8bbe427f 237wxBitmap::wxBitmap()
c801d85f 238{
fd0eed64 239 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 240}
8bbe427f 241
debe6624 242wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 243{
223d09f6 244 wxCHECK_RET( (width > 0) && (height > 0), wxT("invalid bitmap size") )
284b4c88 245
d76fe38b 246 if (depth == -1) depth = gdk_window_get_visual( wxRootWindow->window )->depth;
284b4c88 247
d76fe38b 248 wxCHECK_RET( (depth == gdk_window_get_visual( wxRootWindow->window )->depth) ||
223d09f6 249 (depth == 1), wxT("invalid bitmap depth") )
8bbe427f 250
eefa26be 251 m_refData = new wxBitmapRefData();
fd0eed64 252 M_BMPDATA->m_mask = (wxMask *) NULL;
fd0eed64
RR
253 M_BMPDATA->m_width = width;
254 M_BMPDATA->m_height = height;
eefa26be
RR
255 if (depth == 1)
256 {
d76fe38b 257 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxRootWindow->window, width, height, 1 );
eefa26be
RR
258 M_BMPDATA->m_bpp = 1;
259 }
260 else
261 {
d76fe38b
RR
262 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxRootWindow->window, width, height, depth );
263 M_BMPDATA->m_bpp = gdk_window_get_visual( wxRootWindow->window )->depth;
eefa26be 264 }
8bbe427f 265
fd0eed64 266 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 267}
c801d85f 268
e838cc14 269bool wxBitmap::CreateFromXpm( const char **bits )
e52f60e6 270{
e838cc14 271 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
8bbe427f 272
e52f60e6
RR
273 m_refData = new wxBitmapRefData();
274
275 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 276
d76fe38b 277 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxRootWindow->window, &mask, NULL, (gchar **) bits );
8bbe427f 278
e838cc14 279 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
8bbe427f 280
fd0eed64
RR
281 if (mask)
282 {
283 M_BMPDATA->m_mask = new wxMask();
284 M_BMPDATA->m_mask->m_bitmap = mask;
285 }
8bbe427f 286
fd0eed64 287 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 288
d76fe38b 289 M_BMPDATA->m_bpp = gdk_window_get_visual( wxRootWindow->window )->depth; // ?
fd0eed64 290 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
e838cc14
VZ
291
292 return TRUE;
ff7b1510 293}
8bbe427f 294
c801d85f
KB
295wxBitmap::wxBitmap( const wxBitmap& bmp )
296{
fd0eed64 297 Ref( bmp );
8bbe427f 298
fd0eed64 299 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 300}
6f65e337 301
debe6624 302wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f 303{
fd0eed64 304 LoadFile( filename, type );
8bbe427f 305
fd0eed64 306 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 307}
c801d85f 308
debe6624 309wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 310{
fd0eed64 311 m_refData = new wxBitmapRefData();
6f65e337 312
fd0eed64 313 M_BMPDATA->m_mask = (wxMask *) NULL;
8bbe427f 314 M_BMPDATA->m_bitmap =
d76fe38b 315 gdk_bitmap_create_from_data( wxRootWindow->window, (gchar *) bits, width, height );
fd0eed64
RR
316 M_BMPDATA->m_width = width;
317 M_BMPDATA->m_height = height;
318 M_BMPDATA->m_bpp = 1;
6f65e337 319
223d09f6 320 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
8bbe427f 321
fd0eed64 322 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
6f65e337 323}
8bbe427f
VZ
324
325wxBitmap::~wxBitmap()
c801d85f 326{
fd0eed64 327 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
ff7b1510 328}
8bbe427f 329
c801d85f
KB
330wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
331{
8bbe427f
VZ
332 if (*this == bmp) return (*this);
333 Ref( bmp );
334 return *this;
ff7b1510 335}
8bbe427f 336
c801d85f
KB
337bool wxBitmap::operator == ( const wxBitmap& bmp )
338{
8bbe427f 339 return m_refData == bmp.m_refData;
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
91b8de8d 347bool wxBitmap::Ok() const
c801d85f 348{
fd0eed64 349 return (m_refData != NULL);
ff7b1510 350}
8bbe427f 351
91b8de8d 352int wxBitmap::GetHeight() const
c801d85f 353{
223d09f6 354 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 355
fd0eed64 356 return M_BMPDATA->m_height;
ff7b1510 357}
c801d85f 358
91b8de8d 359int wxBitmap::GetWidth() const
c801d85f 360{
223d09f6 361 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 362
fd0eed64 363 return M_BMPDATA->m_width;
ff7b1510 364}
c801d85f 365
91b8de8d 366int wxBitmap::GetDepth() const
c801d85f 367{
223d09f6 368 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 369
fd0eed64 370 return M_BMPDATA->m_bpp;
ff7b1510 371}
c801d85f 372
91b8de8d 373wxMask *wxBitmap::GetMask() const
c801d85f 374{
223d09f6 375 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 376
fd0eed64 377 return M_BMPDATA->m_mask;
ff7b1510 378}
c801d85f
KB
379
380void wxBitmap::SetMask( wxMask *mask )
381{
223d09f6 382 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 383
fd0eed64 384 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 385
fd0eed64 386 M_BMPDATA->m_mask = mask;
ff7b1510 387}
c801d85f 388
17bec151
RR
389wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
390{
391 wxCHECK_MSG( Ok() &&
13111b2a
VZ
392 (rect.x >= 0) && (rect.y >= 0) &&
393 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
17bec151 394 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
13111b2a 395
17bec151
RR
396 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
397 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
13111b2a 398
17bec151
RR
399 if (ret.GetPixmap())
400 {
401 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
13111b2a
VZ
402 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
403 gdk_gc_destroy( gc );
17bec151
RR
404 }
405 else
406 {
407 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
13111b2a
VZ
408 gdk_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
409 gdk_gc_destroy( gc );
17bec151 410 }
13111b2a 411
17bec151
RR
412 if (GetMask())
413 {
414 wxMask *mask = new wxMask;
d76fe38b 415 mask->m_bitmap = gdk_pixmap_new( wxRootWindow->window, rect.width, rect.height, 1 );
13111b2a 416
17bec151 417 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
13111b2a
VZ
418 gdk_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
419 gdk_gc_destroy( gc );
420
421 ret.SetMask( mask );
17bec151 422 }
13111b2a 423
17bec151
RR
424 return ret;
425}
426
fd0eed64 427bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 428{
223d09f6 429 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
8bbe427f 430
b75dd496 431 // Try to save the bitmap via wxImage handlers:
fd0eed64 432 {
4bc67cc5 433 wxImage image( *this );
284b4c88 434 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 435 }
8bbe427f 436
fd0eed64 437 return FALSE;
ff7b1510 438}
c801d85f 439
fd0eed64 440bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 441{
fd0eed64 442 UnRef();
8bbe427f 443
fd0eed64 444 if (!wxFileExists(name)) return FALSE;
8bbe427f 445
fd0eed64
RR
446 if (type == wxBITMAP_TYPE_XPM)
447 {
448 m_refData = new wxBitmapRefData();
8bbe427f 449
fd0eed64 450 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 451
d76fe38b 452 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( wxRootWindow->window, &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) );
d76fe38b 461 M_BMPDATA->m_bpp = gdk_window_get_visual( wxRootWindow->window )->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
82ea63e6
RR
509void wxBitmap::SetBitmap( GdkPixmap *bitmap )
510{
511 if (!m_refData) m_refData = new wxBitmapRefData();
512
513 M_BMPDATA->m_bitmap = bitmap;
514}
515
91b8de8d 516GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 517{
223d09f6 518 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 519
fd0eed64 520 return M_BMPDATA->m_pixmap;
ff7b1510 521}
8bbe427f 522
91b8de8d 523GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 524{
223d09f6 525 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 526
fd0eed64 527 return M_BMPDATA->m_bitmap;
ff7b1510 528}