]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
GTK
[wxWidgets.git] / src / gtk / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // RCS-ID: $Id$
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "bitmap.h"
13 #endif
14
15 #include "wx/bitmap.h"
16 #include "gdk/gdkprivate.h"
17
18 #ifdef USE_GDK_IMLIB
19 #include "gdk_imlib.h"
20 #endif
21
22 //-----------------------------------------------------------------------------
23 // wxMask
24 //-----------------------------------------------------------------------------
25
26 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
27
28 wxMask::wxMask(void)
29 {
30 m_bitmap = NULL;
31 };
32
33 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const wxColour& WXUNUSED(colour) )
34 {
35 };
36
37 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const int WXUNUSED(paletteIndex) )
38 {
39 };
40
41 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap) )
42 {
43 };
44
45 wxMask::~wxMask(void)
46 {
47 #ifdef USE_GDK_IMLIB
48 // do not delete the mask, gdk_imlib does it for you
49 #else
50 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
51 #endif
52 };
53
54 GdkBitmap *wxMask::GetBitmap(void) const
55 {
56 return m_bitmap;
57 };
58
59 //-----------------------------------------------------------------------------
60 // wxBitmap
61 //-----------------------------------------------------------------------------
62
63 // CMB 20/5/98: added m_bitmap for GdkBitmaps
64 class wxBitmapRefData: public wxObjectRefData
65 {
66 public:
67
68 wxBitmapRefData(void);
69 ~wxBitmapRefData(void);
70
71 GdkPixmap *m_pixmap;
72 GdkBitmap *m_bitmap;
73 wxMask *m_mask;
74 int m_width;
75 int m_height;
76 int m_bpp;
77 wxPalette *m_palette;
78 };
79
80 wxBitmapRefData::wxBitmapRefData(void)
81 {
82 m_pixmap = NULL;
83 m_bitmap = NULL;
84 m_mask = NULL;
85 m_width = 0;
86 m_height = 0;
87 m_bpp = 0;
88 m_palette = NULL;
89 };
90
91 wxBitmapRefData::~wxBitmapRefData(void)
92 {
93 #ifdef USE_GDK_IMLIB
94 if (m_pixmap) gdk_imlib_free_pixmap( m_pixmap );
95 #else
96 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
97 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
98 #endif
99 if (m_mask) delete m_mask;
100 if (m_palette) delete m_palette;
101 };
102
103 //-----------------------------------------------------------------------------
104
105 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
106
107 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
108
109 wxBitmap::wxBitmap(void)
110 {
111 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
112 };
113
114 wxBitmap::wxBitmap( const int width, const int height, const int depth )
115 {
116 m_refData = new wxBitmapRefData();
117 M_BMPDATA->m_mask = NULL;
118 M_BMPDATA->m_pixmap =
119 gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, width, height, depth );
120 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
121 M_BMPDATA->m_bpp = depth;
122
123 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
124 };
125
126 wxBitmap::wxBitmap( char **bits )
127 {
128 m_refData = new wxBitmapRefData();
129
130 GdkBitmap *mask = NULL;
131
132 #ifndef USE_GDK_IMLIB
133 M_BMPDATA->m_pixmap =
134 gdk_pixmap_create_from_xpm_d( (GdkWindow*) &gdk_root_parent, &mask, NULL, (gchar **) bits );
135 #else
136 M_BMPDATA->m_pixmap = NULL;
137 int res = gdk_imlib_data_to_pixmap( bits, &M_BMPDATA->m_pixmap, &mask );
138 #endif
139
140 if (mask)
141 {
142 M_BMPDATA->m_mask = new wxMask();
143 M_BMPDATA->m_mask->m_bitmap = mask;
144 };
145
146 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
147 M_BMPDATA->m_bpp = 24; // ?
148
149 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
150 };
151
152 wxBitmap::wxBitmap( const wxBitmap& bmp )
153 {
154 Ref( bmp );
155
156 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
157 };
158
159 wxBitmap::wxBitmap( const wxBitmap* bmp )
160 {
161 if (bmp) Ref( *bmp );
162
163 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
164 };
165
166 wxBitmap::wxBitmap( const wxString &filename, const int type )
167 {
168 LoadFile( filename, type );
169 };
170
171 // CMB 15/5/98: add constructor for xbm bitmaps
172 wxBitmap::wxBitmap( const char bits[], const int width, const int height, const int WXUNUSED(depth))
173 {
174 m_refData = new wxBitmapRefData();
175
176 M_BMPDATA->m_mask = NULL;
177 M_BMPDATA->m_bitmap =
178 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
179 gdk_window_get_size( M_BMPDATA->m_bitmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
180 M_BMPDATA->m_bpp = 1;
181
182 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
183 }
184
185 wxBitmap::~wxBitmap(void)
186 {
187 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
188 };
189
190 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
191 {
192 if (*this == bmp) return (*this);
193 Ref( bmp );
194 return *this;
195 };
196
197 bool wxBitmap::operator == ( const wxBitmap& bmp )
198 {
199 return m_refData == bmp.m_refData;
200 };
201
202 bool wxBitmap::operator != ( const wxBitmap& bmp )
203 {
204 return m_refData != bmp.m_refData;
205 };
206
207 bool wxBitmap::Ok(void) const
208 {
209 return m_refData != NULL;
210 };
211
212 int wxBitmap::GetHeight(void) const
213 {
214 if (!Ok()) return 0;
215 return M_BMPDATA->m_height;
216 };
217
218 int wxBitmap::GetWidth(void) const
219 {
220 if (!Ok()) return 0;
221 return M_BMPDATA->m_width;
222 };
223
224 int wxBitmap::GetDepth(void) const
225 {
226 if (!Ok()) return 0;
227 return M_BMPDATA->m_bpp;
228 };
229
230 void wxBitmap::SetHeight( const int height )
231 {
232 if (!Ok()) return;
233 M_BMPDATA->m_height = height;
234 };
235
236 void wxBitmap::SetWidth( const int width )
237 {
238 if (!Ok()) return;
239 M_BMPDATA->m_width = width;
240 };
241
242 void wxBitmap::SetDepth( const int depth )
243 {
244 if (!Ok()) return;
245 M_BMPDATA->m_bpp = depth;
246 };
247
248 wxMask *wxBitmap::GetMask(void) const
249 {
250 if (!Ok()) return NULL;
251 return M_BMPDATA->m_mask;
252 };
253
254 void wxBitmap::SetMask( wxMask *mask )
255 {
256 if (!Ok()) return;
257 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
258 M_BMPDATA->m_mask = mask;
259 };
260
261 bool wxBitmap::SaveFile( const wxString &WXUNUSED(name), const int WXUNUSED(type),
262 wxPalette *WXUNUSED(palette) )
263 {
264 return FALSE;
265 };
266
267 bool wxBitmap::LoadFile( const wxString &name, const int WXUNUSED(type) )
268 {
269 #ifdef USE_GDK_IMLIB
270
271 UnRef();
272 m_refData = new wxBitmapRefData();
273 M_BMPDATA->m_mask = NULL;
274
275 GdkBitmap *mask = NULL;
276
277 int res = gdk_imlib_load_file_to_pixmap( WXSTRINGCAST name, &M_BMPDATA->m_pixmap, &mask );
278
279 if (res != 1)
280 {
281 UnRef();
282 return FALSE;
283 };
284
285 if (mask)
286 {
287 M_BMPDATA->m_mask = new wxMask();
288 M_BMPDATA->m_mask->m_bitmap = mask;
289 }
290
291 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
292 M_BMPDATA->m_bpp = 24; // ?
293
294 return TRUE;
295 #endif
296
297 return FALSE;
298 };
299
300 wxPalette *wxBitmap::GetPalette(void) const
301 {
302 if (!Ok()) return NULL;
303 return M_BMPDATA->m_palette;
304 };
305
306 GdkPixmap *wxBitmap::GetPixmap(void) const
307 {
308 if (!Ok()) return NULL;
309 return M_BMPDATA->m_pixmap;
310 };
311
312 GdkBitmap *wxBitmap::GetBitmap(void) const
313 {
314 if (!Ok()) return NULL;
315 return M_BMPDATA->m_bitmap;
316 };
317