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