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