]>
Commit | Line | Data |
---|---|---|
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 RR |
17 | #include "wx/image.h" |
18 | ||
20e05ffb RR |
19 | #include <gdk/gdk.h> |
20 | #include <gdk/gdkprivate.h> | |
21 | #include <gdk/gdkx.h> | |
c801d85f KB |
22 | |
23 | //----------------------------------------------------------------------------- | |
24 | // wxMask | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
28 | ||
8bbe427f | 29 | wxMask::wxMask() |
c801d85f | 30 | { |
fd0eed64 | 31 | m_bitmap = (GdkBitmap *) NULL; |
ff7b1510 | 32 | } |
c801d85f | 33 | |
91b8de8d | 34 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
c801d85f | 35 | { |
72a7edf0 | 36 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 37 | Create( bitmap, colour ); |
ff7b1510 | 38 | } |
c801d85f | 39 | |
91b8de8d | 40 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) |
c801d85f | 41 | { |
72a7edf0 | 42 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 43 | Create( bitmap, paletteIndex ); |
ff7b1510 | 44 | } |
c801d85f | 45 | |
91b8de8d | 46 | wxMask::wxMask( const wxBitmap& bitmap ) |
c801d85f | 47 | { |
72a7edf0 | 48 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 49 | Create( bitmap ); |
ff7b1510 | 50 | } |
c801d85f | 51 | |
8bbe427f | 52 | wxMask::~wxMask() |
c801d85f | 53 | { |
72a7edf0 RR |
54 | if (m_bitmap) |
55 | gdk_bitmap_unref( m_bitmap ); | |
ff7b1510 | 56 | } |
c801d85f | 57 | |
284b4c88 VZ |
58 | bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap), |
59 | const wxColour& WXUNUSED(colour) ) | |
91b8de8d RR |
60 | { |
61 | if (m_bitmap) | |
284b4c88 | 62 | { |
91b8de8d | 63 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 64 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 65 | } |
284b4c88 | 66 | |
223d09f6 | 67 | wxFAIL_MSG( wxT("TODO") ); |
284b4c88 | 68 | |
91b8de8d RR |
69 | return FALSE; |
70 | } | |
71 | ||
284b4c88 VZ |
72 | bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap), |
73 | int WXUNUSED(paletteIndex) ) | |
91b8de8d RR |
74 | { |
75 | if (m_bitmap) | |
284b4c88 | 76 | { |
91b8de8d | 77 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 78 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 79 | } |
284b4c88 | 80 | |
223d09f6 | 81 | wxFAIL_MSG( wxT("not implemented") ); |
284b4c88 | 82 | |
91b8de8d RR |
83 | return FALSE; |
84 | } | |
85 | ||
86 | bool wxMask::Create( const wxBitmap& bitmap ) | |
87 | { | |
88 | if (m_bitmap) | |
284b4c88 | 89 | { |
91b8de8d | 90 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 91 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 92 | } |
284b4c88 | 93 | |
91b8de8d | 94 | if (!bitmap.Ok()) return FALSE; |
284b4c88 | 95 | |
223d09f6 | 96 | wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") ); |
284b4c88 | 97 | |
91b8de8d | 98 | m_bitmap = gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); |
284b4c88 | 99 | |
91b8de8d | 100 | if (!m_bitmap) return FALSE; |
284b4c88 | 101 | |
91b8de8d | 102 | GdkGC *gc = gdk_gc_new( m_bitmap ); |
284b4c88 | 103 | |
91b8de8d | 104 | gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() ); |
284b4c88 | 105 | |
91b8de8d | 106 | gdk_gc_unref( gc ); |
284b4c88 | 107 | |
91b8de8d RR |
108 | return TRUE; |
109 | } | |
110 | ||
111 | GdkBitmap *wxMask::GetBitmap() const | |
c801d85f | 112 | { |
fd0eed64 | 113 | return m_bitmap; |
ff7b1510 | 114 | } |
8bbe427f | 115 | |
c801d85f KB |
116 | //----------------------------------------------------------------------------- |
117 | // wxBitmap | |
118 | //----------------------------------------------------------------------------- | |
119 | ||
120 | class wxBitmapRefData: public wxObjectRefData | |
121 | { | |
fd0eed64 | 122 | public: |
91b8de8d RR |
123 | wxBitmapRefData(); |
124 | ~wxBitmapRefData(); | |
8bbe427f | 125 | |
fd0eed64 RR |
126 | GdkPixmap *m_pixmap; |
127 | GdkBitmap *m_bitmap; | |
128 | wxMask *m_mask; | |
129 | int m_width; | |
130 | int m_height; | |
131 | int m_bpp; | |
132 | wxPalette *m_palette; | |
c801d85f KB |
133 | }; |
134 | ||
8bbe427f | 135 | wxBitmapRefData::wxBitmapRefData() |
c801d85f | 136 | { |
fd0eed64 RR |
137 | m_pixmap = (GdkPixmap *) NULL; |
138 | m_bitmap = (GdkBitmap *) NULL; | |
139 | m_mask = (wxMask *) NULL; | |
140 | m_width = 0; | |
141 | m_height = 0; | |
142 | m_bpp = 0; | |
143 | m_palette = (wxPalette *) NULL; | |
ff7b1510 | 144 | } |
c801d85f | 145 | |
8bbe427f | 146 | wxBitmapRefData::~wxBitmapRefData() |
c801d85f | 147 | { |
fd0eed64 RR |
148 | if (m_pixmap) gdk_pixmap_unref( m_pixmap ); |
149 | if (m_bitmap) gdk_bitmap_unref( m_bitmap ); | |
150 | if (m_mask) delete m_mask; | |
151 | if (m_palette) delete m_palette; | |
ff7b1510 | 152 | } |
c801d85f KB |
153 | |
154 | //----------------------------------------------------------------------------- | |
155 | ||
156 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
157 | ||
158 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
159 | ||
8bbe427f | 160 | wxBitmap::wxBitmap() |
c801d85f | 161 | { |
fd0eed64 | 162 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
ff7b1510 | 163 | } |
8bbe427f | 164 | |
debe6624 | 165 | wxBitmap::wxBitmap( int width, int height, int depth ) |
c801d85f | 166 | { |
223d09f6 | 167 | wxCHECK_RET( (width > 0) && (height > 0), wxT("invalid bitmap size") ) |
284b4c88 | 168 | |
fd0eed64 | 169 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; |
eefa26be | 170 | if (depth == -1) depth = gdk_window_get_visual( parent )->depth; |
284b4c88 | 171 | |
eefa26be | 172 | wxCHECK_RET( (depth == gdk_window_get_visual( parent )->depth) || |
223d09f6 | 173 | (depth == 1), wxT("invalid bitmap depth") ) |
8bbe427f | 174 | |
eefa26be | 175 | m_refData = new wxBitmapRefData(); |
fd0eed64 | 176 | M_BMPDATA->m_mask = (wxMask *) NULL; |
fd0eed64 RR |
177 | M_BMPDATA->m_width = width; |
178 | M_BMPDATA->m_height = height; | |
eefa26be RR |
179 | if (depth == 1) |
180 | { | |
181 | M_BMPDATA->m_bitmap = gdk_pixmap_new( parent, width, height, 1 ); | |
182 | M_BMPDATA->m_bpp = 1; | |
183 | } | |
184 | else | |
185 | { | |
186 | M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth ); | |
187 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; | |
188 | } | |
8bbe427f | 189 | |
fd0eed64 | 190 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
ff7b1510 | 191 | } |
c801d85f | 192 | |
e52f60e6 RR |
193 | wxBitmap::wxBitmap( const char **bits ) |
194 | { | |
223d09f6 | 195 | wxCHECK_RET( bits != NULL, wxT("invalid bitmap data") ) |
8bbe427f | 196 | |
e52f60e6 RR |
197 | m_refData = new wxBitmapRefData(); |
198 | ||
199 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
200 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
8bbe427f | 201 | |
e52f60e6 | 202 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits ); |
8bbe427f | 203 | |
e52f60e6 RR |
204 | if (mask) |
205 | { | |
206 | M_BMPDATA->m_mask = new wxMask(); | |
207 | M_BMPDATA->m_mask->m_bitmap = mask; | |
208 | } | |
8bbe427f | 209 | |
e52f60e6 | 210 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
8bbe427f | 211 | |
e52f60e6 RR |
212 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ? |
213 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
214 | } | |
8bbe427f | 215 | |
c801d85f KB |
216 | wxBitmap::wxBitmap( char **bits ) |
217 | { | |
223d09f6 | 218 | wxCHECK_RET( bits != NULL, wxT("invalid bitmap data") ) |
8bbe427f | 219 | |
fd0eed64 | 220 | m_refData = new wxBitmapRefData(); |
c801d85f | 221 | |
fd0eed64 RR |
222 | GdkBitmap *mask = (GdkBitmap*) NULL; |
223 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
8bbe427f | 224 | |
fd0eed64 | 225 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits ); |
8bbe427f | 226 | |
223d09f6 | 227 | wxCHECK_RET( M_BMPDATA->m_pixmap, wxT("couldn't create pixmap") ); |
8bbe427f | 228 | |
fd0eed64 RR |
229 | if (mask) |
230 | { | |
231 | M_BMPDATA->m_mask = new wxMask(); | |
232 | M_BMPDATA->m_mask->m_bitmap = mask; | |
233 | } | |
8bbe427f | 234 | |
fd0eed64 | 235 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
8bbe427f | 236 | |
fd0eed64 | 237 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ? |
fd0eed64 | 238 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
ff7b1510 | 239 | } |
8bbe427f | 240 | |
c801d85f KB |
241 | wxBitmap::wxBitmap( const wxBitmap& bmp ) |
242 | { | |
fd0eed64 | 243 | Ref( bmp ); |
8bbe427f | 244 | |
fd0eed64 | 245 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
ff7b1510 | 246 | } |
6f65e337 | 247 | |
debe6624 | 248 | wxBitmap::wxBitmap( const wxString &filename, int type ) |
c801d85f | 249 | { |
fd0eed64 | 250 | LoadFile( filename, type ); |
8bbe427f | 251 | |
fd0eed64 | 252 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
ff7b1510 | 253 | } |
c801d85f | 254 | |
debe6624 | 255 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) |
6f65e337 | 256 | { |
fd0eed64 | 257 | m_refData = new wxBitmapRefData(); |
6f65e337 | 258 | |
fd0eed64 | 259 | M_BMPDATA->m_mask = (wxMask *) NULL; |
8bbe427f | 260 | M_BMPDATA->m_bitmap = |
fd0eed64 RR |
261 | gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height ); |
262 | M_BMPDATA->m_width = width; | |
263 | M_BMPDATA->m_height = height; | |
264 | M_BMPDATA->m_bpp = 1; | |
6f65e337 | 265 | |
223d09f6 | 266 | wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") ); |
8bbe427f | 267 | |
fd0eed64 | 268 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
6f65e337 | 269 | } |
8bbe427f VZ |
270 | |
271 | wxBitmap::~wxBitmap() | |
c801d85f | 272 | { |
fd0eed64 | 273 | if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this); |
ff7b1510 | 274 | } |
8bbe427f | 275 | |
c801d85f KB |
276 | wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp ) |
277 | { | |
8bbe427f VZ |
278 | if (*this == bmp) return (*this); |
279 | Ref( bmp ); | |
280 | return *this; | |
ff7b1510 | 281 | } |
8bbe427f | 282 | |
c801d85f KB |
283 | bool wxBitmap::operator == ( const wxBitmap& bmp ) |
284 | { | |
8bbe427f | 285 | return m_refData == bmp.m_refData; |
ff7b1510 | 286 | } |
8bbe427f | 287 | |
c801d85f KB |
288 | bool wxBitmap::operator != ( const wxBitmap& bmp ) |
289 | { | |
8bbe427f | 290 | return m_refData != bmp.m_refData; |
ff7b1510 | 291 | } |
8bbe427f | 292 | |
91b8de8d | 293 | bool wxBitmap::Ok() const |
c801d85f | 294 | { |
fd0eed64 | 295 | return (m_refData != NULL); |
ff7b1510 | 296 | } |
8bbe427f | 297 | |
91b8de8d | 298 | int wxBitmap::GetHeight() const |
c801d85f | 299 | { |
223d09f6 | 300 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
e55ad60e | 301 | |
fd0eed64 | 302 | return M_BMPDATA->m_height; |
ff7b1510 | 303 | } |
c801d85f | 304 | |
91b8de8d | 305 | int wxBitmap::GetWidth() const |
c801d85f | 306 | { |
223d09f6 | 307 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 308 | |
fd0eed64 | 309 | return M_BMPDATA->m_width; |
ff7b1510 | 310 | } |
c801d85f | 311 | |
91b8de8d | 312 | int wxBitmap::GetDepth() const |
c801d85f | 313 | { |
223d09f6 | 314 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 315 | |
fd0eed64 | 316 | return M_BMPDATA->m_bpp; |
ff7b1510 | 317 | } |
c801d85f | 318 | |
91b8de8d | 319 | wxMask *wxBitmap::GetMask() const |
c801d85f | 320 | { |
223d09f6 | 321 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 322 | |
fd0eed64 | 323 | return M_BMPDATA->m_mask; |
ff7b1510 | 324 | } |
c801d85f KB |
325 | |
326 | void wxBitmap::SetMask( wxMask *mask ) | |
327 | { | |
223d09f6 | 328 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); |
8bbe427f | 329 | |
fd0eed64 | 330 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
8bbe427f | 331 | |
fd0eed64 | 332 | M_BMPDATA->m_mask = mask; |
ff7b1510 | 333 | } |
c801d85f | 334 | |
fd0eed64 | 335 | bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) ) |
c801d85f | 336 | { |
223d09f6 | 337 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") ); |
8bbe427f | 338 | |
b75dd496 | 339 | // Try to save the bitmap via wxImage handlers: |
fd0eed64 | 340 | { |
4bc67cc5 | 341 | wxImage image( *this ); |
284b4c88 | 342 | if (image.Ok()) return image.SaveFile( name, type ); |
fd0eed64 | 343 | } |
8bbe427f | 344 | |
fd0eed64 | 345 | return FALSE; |
ff7b1510 | 346 | } |
c801d85f | 347 | |
fd0eed64 | 348 | bool wxBitmap::LoadFile( const wxString &name, int type ) |
c801d85f | 349 | { |
fd0eed64 | 350 | UnRef(); |
8bbe427f | 351 | |
fd0eed64 | 352 | if (!wxFileExists(name)) return FALSE; |
8bbe427f | 353 | |
fd0eed64 RR |
354 | if (type == wxBITMAP_TYPE_XPM) |
355 | { | |
356 | m_refData = new wxBitmapRefData(); | |
8bbe427f | 357 | |
fd0eed64 RR |
358 | GdkBitmap *mask = (GdkBitmap*) NULL; |
359 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
8bbe427f | 360 | |
62bd5cf0 | 361 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() ); |
8bbe427f | 362 | |
fd0eed64 RR |
363 | if (mask) |
364 | { | |
365 | M_BMPDATA->m_mask = new wxMask(); | |
366 | M_BMPDATA->m_mask->m_bitmap = mask; | |
367 | } | |
8bbe427f | 368 | |
fd0eed64 | 369 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
8bbe427f | 370 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; |
fd0eed64 | 371 | } |
b75dd496 | 372 | else // try if wxImage can load it |
fd0eed64 RR |
373 | { |
374 | wxImage image; | |
b75dd496 | 375 | if (!image.LoadFile( name, type )) return FALSE; |
4bc67cc5 | 376 | if (image.Ok()) *this = image.ConvertToBitmap(); |
b75dd496 | 377 | else return FALSE; |
fd0eed64 | 378 | } |
8bbe427f | 379 | |
fd0eed64 | 380 | return TRUE; |
ff7b1510 | 381 | } |
8bbe427f | 382 | |
91b8de8d | 383 | wxPalette *wxBitmap::GetPalette() const |
c801d85f | 384 | { |
fd0eed64 | 385 | if (!Ok()) return (wxPalette *) NULL; |
8bbe427f | 386 | |
fd0eed64 | 387 | return M_BMPDATA->m_palette; |
ff7b1510 | 388 | } |
c801d85f | 389 | |
4bc67cc5 RR |
390 | void wxBitmap::SetHeight( int height ) |
391 | { | |
392 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
393 | ||
394 | M_BMPDATA->m_height = height; | |
395 | } | |
396 | ||
397 | void wxBitmap::SetWidth( int width ) | |
398 | { | |
399 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
400 | ||
401 | M_BMPDATA->m_width = width; | |
402 | } | |
403 | ||
404 | void wxBitmap::SetDepth( int depth ) | |
405 | { | |
406 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
407 | ||
408 | M_BMPDATA->m_bpp = depth; | |
409 | } | |
410 | ||
411 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
412 | { | |
413 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
414 | ||
415 | M_BMPDATA->m_pixmap = pixmap; | |
416 | } | |
417 | ||
91b8de8d | 418 | GdkPixmap *wxBitmap::GetPixmap() const |
c801d85f | 419 | { |
223d09f6 | 420 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 421 | |
fd0eed64 | 422 | return M_BMPDATA->m_pixmap; |
ff7b1510 | 423 | } |
8bbe427f | 424 | |
91b8de8d | 425 | GdkBitmap *wxBitmap::GetBitmap() const |
6f65e337 | 426 | { |
223d09f6 | 427 | wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 428 | |
fd0eed64 | 429 | return M_BMPDATA->m_bitmap; |
ff7b1510 | 430 | } |