]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
moved wxDash typedef to gdicmn.h
[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
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{
c826213d
RR
246 Create( width, height, depth );
247
248 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
249}
250
251bool wxBitmap::Create( int width, int height, int depth )
252{
253 UnRef();
254
255 wxCHECK_MSG( (width > 0) && (height > 0), FALSE, wxT("invalid bitmap size") )
284b4c88 256
103aab26
RR
257 GdkVisual *visual = gdk_window_get_visual( wxRootWindow->window );
258 wxASSERT( visual );
284b4c88 259
103aab26
RR
260 if (depth == -1) depth = visual->depth;
261
c826213d
RR
262 wxCHECK_MSG( (depth == visual->depth) ||
263 (depth == 1), FALSE, wxT("invalid bitmap depth") )
8bbe427f 264
eefa26be 265 m_refData = new wxBitmapRefData();
fd0eed64 266 M_BMPDATA->m_mask = (wxMask *) NULL;
fd0eed64
RR
267 M_BMPDATA->m_width = width;
268 M_BMPDATA->m_height = height;
eefa26be
RR
269 if (depth == 1)
270 {
d76fe38b 271 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxRootWindow->window, width, height, 1 );
eefa26be
RR
272 M_BMPDATA->m_bpp = 1;
273 }
274 else
275 {
d76fe38b 276 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxRootWindow->window, width, height, depth );
103aab26 277 M_BMPDATA->m_bpp = visual->depth;
eefa26be 278 }
8bbe427f 279
c826213d 280 return Ok();
ff7b1510 281}
e838cc14 282bool wxBitmap::CreateFromXpm( const char **bits )
e52f60e6 283{
e838cc14 284 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
8bbe427f 285
103aab26
RR
286 GdkVisual *visual = gdk_window_get_visual( wxRootWindow->window );
287 wxASSERT( visual );
288
e52f60e6
RR
289 m_refData = new wxBitmapRefData();
290
291 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 292
d76fe38b 293 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxRootWindow->window, &mask, NULL, (gchar **) bits );
8bbe427f 294
e838cc14 295 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
8bbe427f 296
fd0eed64
RR
297 if (mask)
298 {
299 M_BMPDATA->m_mask = new wxMask();
300 M_BMPDATA->m_mask->m_bitmap = mask;
301 }
8bbe427f 302
fd0eed64 303 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 304
103aab26
RR
305 M_BMPDATA->m_bpp = visual->depth; // ?
306
fd0eed64 307 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
e838cc14
VZ
308
309 return TRUE;
ff7b1510 310}
8bbe427f 311
c801d85f
KB
312wxBitmap::wxBitmap( const wxBitmap& bmp )
313{
fd0eed64 314 Ref( bmp );
8bbe427f 315
fd0eed64 316 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 317}
6f65e337 318
debe6624 319wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f 320{
fd0eed64 321 LoadFile( filename, type );
8bbe427f 322
fd0eed64 323 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 324}
c801d85f 325
debe6624 326wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 327{
fd0eed64 328 m_refData = new wxBitmapRefData();
6f65e337 329
fd0eed64 330 M_BMPDATA->m_mask = (wxMask *) NULL;
8bbe427f 331 M_BMPDATA->m_bitmap =
d76fe38b 332 gdk_bitmap_create_from_data( wxRootWindow->window, (gchar *) bits, width, height );
fd0eed64
RR
333 M_BMPDATA->m_width = width;
334 M_BMPDATA->m_height = height;
335 M_BMPDATA->m_bpp = 1;
6f65e337 336
223d09f6 337 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
8bbe427f 338
fd0eed64 339 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
6f65e337 340}
8bbe427f
VZ
341
342wxBitmap::~wxBitmap()
c801d85f 343{
fd0eed64 344 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
ff7b1510 345}
8bbe427f 346
c801d85f
KB
347wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
348{
8bbe427f
VZ
349 if (*this == bmp) return (*this);
350 Ref( bmp );
351 return *this;
ff7b1510 352}
8bbe427f 353
c801d85f
KB
354bool wxBitmap::operator == ( const wxBitmap& bmp )
355{
8bbe427f 356 return m_refData == bmp.m_refData;
ff7b1510 357}
8bbe427f 358
c801d85f
KB
359bool wxBitmap::operator != ( const wxBitmap& bmp )
360{
8bbe427f 361 return m_refData != bmp.m_refData;
ff7b1510 362}
8bbe427f 363
91b8de8d 364bool wxBitmap::Ok() const
c801d85f 365{
fd0eed64 366 return (m_refData != NULL);
ff7b1510 367}
8bbe427f 368
91b8de8d 369int wxBitmap::GetHeight() const
c801d85f 370{
223d09f6 371 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 372
fd0eed64 373 return M_BMPDATA->m_height;
ff7b1510 374}
c801d85f 375
91b8de8d 376int wxBitmap::GetWidth() const
c801d85f 377{
223d09f6 378 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 379
fd0eed64 380 return M_BMPDATA->m_width;
ff7b1510 381}
c801d85f 382
91b8de8d 383int wxBitmap::GetDepth() const
c801d85f 384{
223d09f6 385 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 386
fd0eed64 387 return M_BMPDATA->m_bpp;
ff7b1510 388}
c801d85f 389
91b8de8d 390wxMask *wxBitmap::GetMask() const
c801d85f 391{
223d09f6 392 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 393
fd0eed64 394 return M_BMPDATA->m_mask;
ff7b1510 395}
c801d85f
KB
396
397void wxBitmap::SetMask( wxMask *mask )
398{
223d09f6 399 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 400
fd0eed64 401 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 402
fd0eed64 403 M_BMPDATA->m_mask = mask;
ff7b1510 404}
c801d85f 405
17bec151
RR
406wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
407{
408 wxCHECK_MSG( Ok() &&
13111b2a
VZ
409 (rect.x >= 0) && (rect.y >= 0) &&
410 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
17bec151 411 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
13111b2a 412
17bec151
RR
413 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
414 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
13111b2a 415
17bec151
RR
416 if (ret.GetPixmap())
417 {
418 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
13111b2a
VZ
419 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
420 gdk_gc_destroy( gc );
17bec151
RR
421 }
422 else
423 {
424 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
13111b2a
VZ
425 gdk_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
426 gdk_gc_destroy( gc );
17bec151 427 }
13111b2a 428
17bec151
RR
429 if (GetMask())
430 {
431 wxMask *mask = new wxMask;
d76fe38b 432 mask->m_bitmap = gdk_pixmap_new( wxRootWindow->window, rect.width, rect.height, 1 );
13111b2a 433
17bec151 434 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
13111b2a
VZ
435 gdk_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
436 gdk_gc_destroy( gc );
437
438 ret.SetMask( mask );
17bec151 439 }
13111b2a 440
17bec151
RR
441 return ret;
442}
443
fd0eed64 444bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 445{
223d09f6 446 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
8bbe427f 447
b75dd496 448 // Try to save the bitmap via wxImage handlers:
fd0eed64 449 {
4bc67cc5 450 wxImage image( *this );
284b4c88 451 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 452 }
8bbe427f 453
fd0eed64 454 return FALSE;
ff7b1510 455}
c801d85f 456
fd0eed64 457bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 458{
fd0eed64 459 UnRef();
8bbe427f 460
fd0eed64 461 if (!wxFileExists(name)) return FALSE;
8bbe427f 462
103aab26
RR
463 GdkVisual *visual = gdk_window_get_visual( wxRootWindow->window );
464 wxASSERT( visual );
465
fd0eed64
RR
466 if (type == wxBITMAP_TYPE_XPM)
467 {
468 m_refData = new wxBitmapRefData();
8bbe427f 469
fd0eed64 470 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 471
d76fe38b 472 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( wxRootWindow->window, &mask, NULL, name.fn_str() );
8bbe427f 473
fd0eed64
RR
474 if (mask)
475 {
476 M_BMPDATA->m_mask = new wxMask();
477 M_BMPDATA->m_mask->m_bitmap = mask;
478 }
8bbe427f 479
fd0eed64 480 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
103aab26
RR
481
482 M_BMPDATA->m_bpp = visual->depth;
fd0eed64 483 }
b75dd496 484 else // try if wxImage can load it
fd0eed64
RR
485 {
486 wxImage image;
b75dd496 487 if (!image.LoadFile( name, type )) return FALSE;
4bc67cc5 488 if (image.Ok()) *this = image.ConvertToBitmap();
b75dd496 489 else return FALSE;
fd0eed64 490 }
8bbe427f 491
fd0eed64 492 return TRUE;
ff7b1510 493}
8bbe427f 494
91b8de8d 495wxPalette *wxBitmap::GetPalette() const
c801d85f 496{
fd0eed64 497 if (!Ok()) return (wxPalette *) NULL;
8bbe427f 498
fd0eed64 499 return M_BMPDATA->m_palette;
ff7b1510 500}
c801d85f 501
4bc67cc5
RR
502void wxBitmap::SetHeight( int height )
503{
504 if (!m_refData) m_refData = new wxBitmapRefData();
505
506 M_BMPDATA->m_height = height;
507}
508
509void wxBitmap::SetWidth( int width )
510{
511 if (!m_refData) m_refData = new wxBitmapRefData();
512
513 M_BMPDATA->m_width = width;
514}
515
516void wxBitmap::SetDepth( int depth )
517{
518 if (!m_refData) m_refData = new wxBitmapRefData();
519
520 M_BMPDATA->m_bpp = depth;
521}
522
523void wxBitmap::SetPixmap( GdkPixmap *pixmap )
524{
525 if (!m_refData) m_refData = new wxBitmapRefData();
526
527 M_BMPDATA->m_pixmap = pixmap;
528}
529
82ea63e6
RR
530void wxBitmap::SetBitmap( GdkPixmap *bitmap )
531{
532 if (!m_refData) m_refData = new wxBitmapRefData();
533
534 M_BMPDATA->m_bitmap = bitmap;
535}
536
91b8de8d 537GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 538{
223d09f6 539 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 540
fd0eed64 541 return M_BMPDATA->m_pixmap;
ff7b1510 542}
8bbe427f 543
91b8de8d 544GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 545{
223d09f6 546 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 547
fd0eed64 548 return M_BMPDATA->m_bitmap;
ff7b1510 549}