]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkpixmap-x11.c
06d19cd8a94dd042f2574a985c0ee5954c61a560
[~andy/gtk] / gdk / x11 / gdkpixmap-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 /* Needed for SEEK_END in SunOS */
32 #include <unistd.h>
33 #include <X11/Xlib.h>
34
35 #include "gdkx.h"
36
37 #include "gdkpixmap-x11.h"
38 #include "gdkprivate-x11.h"
39 #include "gdkscreen-x11.h"
40 #include "gdkdisplay-x11.h"
41
42 #include <gdk/gdkinternals.h>
43
44 typedef struct
45 {
46   gchar *color_string;
47   GdkColor color;
48   gint transparent;
49 } _GdkPixmapColor;
50
51 typedef struct
52 {
53   guint ncolors;
54   GdkColormap *colormap;
55   gulong pixels[1];
56 } _GdkPixmapInfo;
57
58 static void gdk_pixmap_impl_x11_get_size   (GdkDrawable        *drawable,
59                                         gint               *width,
60                                         gint               *height);
61
62 static void gdk_pixmap_impl_x11_init       (GdkPixmapImplX11      *pixmap);
63 static void gdk_pixmap_impl_x11_class_init (GdkPixmapImplX11Class *klass);
64 static void gdk_pixmap_impl_x11_finalize   (GObject            *object);
65
66 static gpointer parent_class = NULL;
67
68 static GType
69 gdk_pixmap_impl_x11_get_type (void)
70 {
71   static GType object_type = 0;
72
73   if (!object_type)
74     {
75       static const GTypeInfo object_info =
76       {
77         sizeof (GdkPixmapImplX11Class),
78         (GBaseInitFunc) NULL,
79         (GBaseFinalizeFunc) NULL,
80         (GClassInitFunc) gdk_pixmap_impl_x11_class_init,
81         NULL,           /* class_finalize */
82         NULL,           /* class_data */
83         sizeof (GdkPixmapImplX11),
84         0,              /* n_preallocs */
85         (GInstanceInitFunc) gdk_pixmap_impl_x11_init,
86       };
87       
88       object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_X11,
89                                             "GdkPixmapImplX11",
90                                             &object_info, 0);
91     }
92   
93   return object_type;
94 }
95
96
97 GType
98 _gdk_pixmap_impl_get_type (void)
99 {
100   return gdk_pixmap_impl_x11_get_type ();
101 }
102
103 static void
104 gdk_pixmap_impl_x11_init (GdkPixmapImplX11 *impl)
105 {
106   impl->width = 1;
107   impl->height = 1;
108 }
109
110 static void
111 gdk_pixmap_impl_x11_class_init (GdkPixmapImplX11Class *klass)
112 {
113   GObjectClass *object_class = G_OBJECT_CLASS (klass);
114   GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
115   
116   parent_class = g_type_class_peek_parent (klass);
117
118   object_class->finalize = gdk_pixmap_impl_x11_finalize;
119
120   drawable_class->get_size = gdk_pixmap_impl_x11_get_size;
121 }
122
123 static void
124 gdk_pixmap_impl_x11_finalize (GObject *object)
125 {
126   GdkPixmapImplX11 *impl = GDK_PIXMAP_IMPL_X11 (object);
127   GdkPixmap *wrapper = GDK_PIXMAP (GDK_DRAWABLE_IMPL_X11 (impl)->wrapper);
128   GdkDisplay *display = GDK_PIXMAP_DISPLAY (wrapper);
129
130   if (!display->closed)
131     {
132 #ifdef HAVE_XFT  
133       {
134         GdkDrawableImplX11 *draw_impl = GDK_DRAWABLE_IMPL_X11 (impl);
135         
136 #ifdef HAVE_XFT2
137         if (draw_impl->xft_draw)
138           XftDrawDestroy (draw_impl->xft_draw);
139 #else /* !HAVE_XFT2 */
140         if (draw_impl->picture)
141           XRenderFreePicture (GDK_DISPLAY_XDISPLAY (display), draw_impl->picture);
142 #endif /* HAVE_XFT2 */
143       }
144 #endif /* HAVE_XFT */
145
146       if (!impl->is_foreign)
147         XFreePixmap (GDK_DISPLAY_XDISPLAY (display), GDK_PIXMAP_XID (wrapper));
148     }
149       
150   _gdk_xid_table_remove (display, GDK_PIXMAP_XID (wrapper));
151   
152   G_OBJECT_CLASS (parent_class)->finalize (object);
153 }
154
155 static void
156 gdk_pixmap_impl_x11_get_size   (GdkDrawable *drawable,
157                                 gint        *width,
158                                 gint        *height)
159 {
160   if (width)
161     *width = GDK_PIXMAP_IMPL_X11 (drawable)->width;
162   if (height)
163     *height = GDK_PIXMAP_IMPL_X11 (drawable)->height;
164 }
165
166 GdkPixmap*
167 gdk_pixmap_new (GdkDrawable *drawable,
168                 gint         width,
169                 gint         height,
170                 gint         depth)
171 {
172   GdkPixmap *pixmap;
173   GdkDrawableImplX11 *draw_impl;
174   GdkPixmapImplX11 *pix_impl;
175   GdkColormap *cmap;
176   gint window_depth;
177   
178   g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
179   g_return_val_if_fail ((drawable != NULL) || (depth != -1), NULL);
180   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
181   
182   if (!drawable)
183     {
184       GDK_NOTE (MULTIHEAD, g_message ("need to specify the screen parent window "
185                                       "for gdk_pixmap_new() to be multihead safe"));
186       drawable = gdk_screen_get_root_window (gdk_screen_get_default ());
187     }
188
189   if (GDK_IS_WINDOW (drawable) && GDK_WINDOW_DESTROYED (drawable))
190     return NULL;
191
192   window_depth = gdk_drawable_get_depth (GDK_DRAWABLE (drawable));
193   if (depth == -1)
194     depth = window_depth;
195
196   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
197   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
198   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
199   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
200   
201   draw_impl->screen = GDK_WINDOW_SCREEN (drawable);
202   draw_impl->xid = XCreatePixmap (GDK_PIXMAP_XDISPLAY (pixmap),
203                                   GDK_WINDOW_XID (drawable),
204                                   width, height, depth);
205   
206   pix_impl->is_foreign = FALSE;
207   pix_impl->width = width;
208   pix_impl->height = height;
209   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
210
211   if (depth == window_depth)
212     {
213       cmap = gdk_drawable_get_colormap (drawable);
214       if (cmap)
215         gdk_drawable_set_colormap (pixmap, cmap);
216     }
217   
218   _gdk_xid_table_insert (GDK_WINDOW_DISPLAY (drawable), 
219                          &GDK_PIXMAP_XID (pixmap), pixmap);
220   return pixmap;
221 }
222
223 GdkPixmap *
224 gdk_bitmap_create_from_data (GdkDrawable *drawable,
225                              const gchar *data,
226                              gint         width,
227                              gint         height)
228 {
229   GdkPixmap *pixmap;
230   GdkDrawableImplX11 *draw_impl;
231   GdkPixmapImplX11 *pix_impl;
232   
233   g_return_val_if_fail (data != NULL, NULL);
234   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
235   g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
236
237   if (!drawable)
238     {
239       GDK_NOTE (MULTIHEAD, g_message ("need to specify the screen parent window "
240                                      "for gdk_bitmap_create_from_data() to be multihead safe"));
241       drawable = gdk_screen_get_root_window (gdk_screen_get_default ());
242     }
243   
244   if (GDK_IS_WINDOW (drawable) && GDK_WINDOW_DESTROYED (drawable))
245     return NULL;
246
247   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
248   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
249   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
250   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
251
252   pix_impl->is_foreign = FALSE;
253   pix_impl->width = width;
254   pix_impl->height = height;
255   GDK_PIXMAP_OBJECT (pixmap)->depth = 1;
256
257   draw_impl->screen = GDK_WINDOW_SCREEN (drawable);
258   draw_impl->xid = XCreateBitmapFromData (GDK_WINDOW_XDISPLAY (drawable),
259                                           GDK_WINDOW_XID (drawable),
260                                           (char *)data, width, height);
261
262   _gdk_xid_table_insert (GDK_WINDOW_DISPLAY (drawable), 
263                          &GDK_PIXMAP_XID (pixmap), pixmap);
264   return pixmap;
265 }
266
267 GdkPixmap*
268 gdk_pixmap_create_from_data (GdkDrawable *drawable,
269                              const gchar *data,
270                              gint         width,
271                              gint         height,
272                              gint         depth,
273                              GdkColor    *fg,
274                              GdkColor    *bg)
275 {
276   GdkPixmap *pixmap;
277   GdkDrawableImplX11 *draw_impl;
278   GdkPixmapImplX11 *pix_impl;
279
280   g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
281   g_return_val_if_fail (data != NULL, NULL);
282   g_return_val_if_fail (fg != NULL, NULL);
283   g_return_val_if_fail (bg != NULL, NULL);
284   g_return_val_if_fail ((drawable != NULL) || (depth != -1), NULL);
285   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
286
287   if (!drawable)
288     {
289       GDK_NOTE (MULTIHEAD, g_message ("need to specify the screen parent window"
290                                       "for gdk_pixmap_create_from_data() to be multihead safe"));
291       drawable = gdk_screen_get_root_window (gdk_screen_get_default ());
292     }
293
294   if (GDK_IS_WINDOW (drawable) && GDK_WINDOW_DESTROYED (drawable))
295     return NULL;
296
297   if (depth == -1)
298     depth = gdk_drawable_get_visual (drawable)->depth;
299
300   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
301   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
302   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
303   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
304   
305   pix_impl->is_foreign = FALSE;
306   pix_impl->width = width;
307   pix_impl->height = height;
308   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
309
310   draw_impl->screen = GDK_DRAWABLE_SCREEN (drawable);
311   draw_impl->xid = XCreatePixmapFromBitmapData (GDK_WINDOW_XDISPLAY (drawable),
312                                                 GDK_WINDOW_XID (drawable),
313                                                 (char *)data, width, height,
314                                                 fg->pixel, bg->pixel, depth);
315
316   _gdk_xid_table_insert (GDK_WINDOW_DISPLAY (drawable),
317                          &GDK_PIXMAP_XID (pixmap), pixmap);
318   return pixmap;
319 }
320
321 /**
322  * gdk_pixmap_foreign_new_for_display:
323  * @display: The #GdkDisplay where @anid is located.
324  * @anid: a native pixmap handle.
325  * 
326  * Wraps a native pixmap in a #GdkPixmap.
327  * This may fail if the pixmap has been destroyed.
328  *
329  * For example in the X backend, a native pixmap handle is an Xlib
330  * <type>XID</type>.
331  *
332  * Return value: the newly-created #GdkPixmap wrapper for the 
333  *    native pixmap or %NULL if the pixmap has been destroyed.
334  *
335  * Since: 2.2
336  **/
337 GdkPixmap *
338 gdk_pixmap_foreign_new_for_display (GdkDisplay      *display,
339                                     GdkNativeWindow  anid)
340 {
341   GdkPixmap *pixmap;
342   GdkDrawableImplX11 *draw_impl;
343   GdkPixmapImplX11 *pix_impl;
344   Pixmap xpixmap;
345   Window root_return;
346   int x_ret, y_ret;
347   unsigned int w_ret, h_ret, bw_ret, depth_ret;
348
349   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
350
351   /* check to make sure we were passed something at
352    * least a little sane */
353   g_return_val_if_fail ((anid != 0), NULL);
354   
355   /* set the pixmap to the passed in value */
356   xpixmap = anid;
357
358   /* get information about the Pixmap to fill in the structure for
359      the gdk window */
360   if (!XGetGeometry (GDK_DISPLAY_XDISPLAY (display),
361                      xpixmap, &root_return,
362                      &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
363     return NULL;
364
365   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
366   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
367   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
368   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
369
370   draw_impl->screen =  _gdk_x11_display_screen_for_xrootwin (display, root_return);
371   draw_impl->xid = xpixmap;
372
373   pix_impl->is_foreign = TRUE;
374   pix_impl->width = w_ret;
375   pix_impl->height = h_ret;
376   GDK_PIXMAP_OBJECT (pixmap)->depth = depth_ret;
377   
378   _gdk_xid_table_insert (display, &GDK_PIXMAP_XID (pixmap), pixmap);
379
380   return pixmap;
381 }
382
383 /**
384  * gdk_pixmap_foreign_new:
385  * @anid: a native pixmap handle.
386  * 
387  * Wraps a native window for the default display in a #GdkPixmap.
388  * This may fail if the pixmap has been destroyed.
389  *
390  * For example in the X backend, a native pixmap handle is an Xlib
391  * <type>XID</type>.
392  *
393  * Return value: the newly-created #GdkPixmap wrapper for the 
394  *    native pixmap or %NULL if the pixmap has been destroyed.
395  **/
396 GdkPixmap*
397 gdk_pixmap_foreign_new (GdkNativeWindow anid)
398 {
399    return gdk_pixmap_foreign_new_for_display (gdk_display_get_default (), anid);
400 }
401
402 /**
403  * gdk_pixmap_lookup:
404  * @anid: a native pixmap handle.
405  * 
406  * Looks up the #GdkPixmap that wraps the given native pixmap handle.
407  *
408  * For example in the X backend, a native pixmap handle is an Xlib
409  * <type>XID</type>.
410  *
411  * Return value: the #GdkWindow wrapper for the native window,
412  *    or %NULL if there is none.
413  **/
414 GdkPixmap*
415 gdk_pixmap_lookup (GdkNativeWindow anid)
416 {
417   return (GdkPixmap*) gdk_xid_table_lookup_for_display (gdk_display_get_default (), anid);
418 }
419
420 /**
421  * gdk_pixmap_lookup_for_display:
422  * @display: the #GdkDisplay associated with @anid
423  * @anid: a native pixmap handle.
424  * 
425  * Looks up the #GdkPixmap that wraps the given native pixmap handle.
426  *
427  * For example in the X backend, a native pixmap handle is an Xlib
428  * <type>XID</type>.
429  *
430  * Return value: the #GdkPixmap wrapper for the native pixmap,
431  *    or %NULL if there is none.
432  *
433  * Since: 2.2
434  **/
435 GdkPixmap*
436 gdk_pixmap_lookup_for_display (GdkDisplay *display, GdkNativeWindow anid)
437 {
438   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
439   return (GdkPixmap*) gdk_xid_table_lookup_for_display (display, anid);
440 }