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