]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkpixmap-x11.c
Switch back to using XftDraw so that we take advantage of the
[~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         if (draw_impl->xft_draw)
137           XftDrawDestroy (draw_impl->xft_draw);
138       }
139 #endif /* HAVE_XFT */  
140
141       if (!impl->is_foreign)
142         XFreePixmap (GDK_DISPLAY_XDISPLAY (display), GDK_PIXMAP_XID (wrapper));
143     }
144       
145   _gdk_xid_table_remove (display, GDK_PIXMAP_XID (wrapper));
146   
147   G_OBJECT_CLASS (parent_class)->finalize (object);
148 }
149
150 static void
151 gdk_pixmap_impl_x11_get_size   (GdkDrawable *drawable,
152                                 gint        *width,
153                                 gint        *height)
154 {
155   if (width)
156     *width = GDK_PIXMAP_IMPL_X11 (drawable)->width;
157   if (height)
158     *height = GDK_PIXMAP_IMPL_X11 (drawable)->height;
159 }
160
161 GdkPixmap*
162 gdk_pixmap_new (GdkWindow *window,
163                 gint       width,
164                 gint       height,
165                 gint       depth)
166 {
167   GdkPixmap *pixmap;
168   GdkDrawableImplX11 *draw_impl;
169   GdkPixmapImplX11 *pix_impl;
170   GdkColormap *cmap;
171   gint window_depth;
172   
173   g_return_val_if_fail (window == NULL || GDK_IS_DRAWABLE (window), NULL);
174   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
175   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
176   
177   if (!window)
178     {
179       GDK_NOTE (MULTIHEAD, g_message ("need to specify the screen parent window "
180                                       "for gdk_pixmap_new() to be multihead safe"));
181       window = gdk_screen_get_root_window (gdk_screen_get_default ());
182     }
183
184   if (GDK_IS_WINDOW (window) && GDK_WINDOW_DESTROYED (window))
185     return NULL;
186
187   window_depth = gdk_drawable_get_depth (GDK_DRAWABLE (window));
188   if (depth == -1)
189     depth = window_depth;
190
191   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
192   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
193   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
194   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
195   
196   draw_impl->screen = GDK_WINDOW_SCREEN (window);
197   draw_impl->xid = XCreatePixmap (GDK_PIXMAP_XDISPLAY (pixmap),
198                                   GDK_WINDOW_XID (window),
199                                   width, height, depth);
200   
201   pix_impl->is_foreign = FALSE;
202   pix_impl->width = width;
203   pix_impl->height = height;
204   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
205
206   if (depth == window_depth)
207     {
208       cmap = gdk_drawable_get_colormap (window);
209       if (cmap)
210         gdk_drawable_set_colormap (pixmap, cmap);
211     }
212   
213   _gdk_xid_table_insert (GDK_WINDOW_DISPLAY (window), 
214                          &GDK_PIXMAP_XID (pixmap), pixmap);
215   return pixmap;
216 }
217
218 GdkPixmap *
219 gdk_bitmap_create_from_data (GdkWindow   *window,
220                              const gchar *data,
221                              gint         width,
222                              gint         height)
223 {
224   GdkPixmap *pixmap;
225   GdkDrawableImplX11 *draw_impl;
226   GdkPixmapImplX11 *pix_impl;
227   
228   g_return_val_if_fail (data != NULL, NULL);
229   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
230   g_return_val_if_fail (window == NULL || GDK_IS_DRAWABLE (window), NULL);
231
232   if (!window) 
233     {
234       GDK_NOTE (MULTIHEAD, g_message ("need to specify the screen parent window "
235                                      "for gdk_bitmap_create_from_data() to be multihead safe"));
236       window = gdk_screen_get_root_window (gdk_screen_get_default ());
237     }
238   
239   if (GDK_IS_WINDOW (window) && GDK_WINDOW_DESTROYED (window))
240     return NULL;
241
242   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
243   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
244   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
245   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
246
247   pix_impl->is_foreign = FALSE;
248   pix_impl->width = width;
249   pix_impl->height = height;
250   GDK_PIXMAP_OBJECT (pixmap)->depth = 1;
251
252   draw_impl->screen = GDK_WINDOW_SCREEN (window);
253   draw_impl->xid = XCreateBitmapFromData (GDK_WINDOW_XDISPLAY (window),
254                                           GDK_WINDOW_XID (window),
255                                           (char *)data, width, height);
256
257   _gdk_xid_table_insert (GDK_WINDOW_DISPLAY (window), 
258                          &GDK_PIXMAP_XID (pixmap), pixmap);
259   return pixmap;
260 }
261
262 GdkPixmap*
263 gdk_pixmap_create_from_data (GdkWindow   *window,
264                              const gchar *data,
265                              gint         width,
266                              gint         height,
267                              gint         depth,
268                              GdkColor    *fg,
269                              GdkColor    *bg)
270 {
271   GdkPixmap *pixmap;
272   GdkDrawableImplX11 *draw_impl;
273   GdkPixmapImplX11 *pix_impl;
274
275   g_return_val_if_fail (window == NULL || GDK_IS_DRAWABLE (window), NULL);
276   g_return_val_if_fail (data != NULL, NULL);
277   g_return_val_if_fail (fg != NULL, NULL);
278   g_return_val_if_fail (bg != NULL, NULL);
279   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
280   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
281
282   if (!window)
283     {
284       GDK_NOTE (MULTIHEAD, g_message ("need to specify the screen parent window"
285                                       "for gdk_pixmap_create_from_data() to be multihead safe"));
286       window = gdk_screen_get_root_window (gdk_screen_get_default ());
287     }
288
289   if (GDK_IS_WINDOW (window) && GDK_WINDOW_DESTROYED (window))
290     return NULL;
291
292   if (depth == -1)
293     depth = gdk_drawable_get_visual (window)->depth;
294
295   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
296   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
297   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
298   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
299   
300   pix_impl->is_foreign = FALSE;
301   pix_impl->width = width;
302   pix_impl->height = height;
303   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
304
305   draw_impl->screen = GDK_DRAWABLE_SCREEN (window);
306   draw_impl->xid = XCreatePixmapFromBitmapData (GDK_WINDOW_XDISPLAY (window),
307                                                 GDK_WINDOW_XID (window),
308                                                 (char *)data, width, height,
309                                                 fg->pixel, bg->pixel, depth);
310
311   _gdk_xid_table_insert (GDK_WINDOW_DISPLAY (window),
312                          &GDK_PIXMAP_XID (pixmap), pixmap);
313   return pixmap;
314 }
315
316 /**
317  * gdk_pixmap_foreign_new_for_display:
318  * @display: The #GdkDisplay where @anid is located.
319  * @anid: a native pixmap handle.
320  * 
321  * Wraps a native window in a #GdkPixmap.
322  * This may fail if the pixmap has been destroyed.
323  *
324  * For example in the X backend, a native pixmap handle is an Xlib
325  * <type>XID</type>.
326  *
327  * Return value: the newly-created #GdkPixmap wrapper for the 
328  *    native pixmap or %NULL if the pixmap has been destroyed.
329  **/
330 GdkPixmap *
331 gdk_pixmap_foreign_new_for_display (GdkDisplay      *display,
332                                     GdkNativeWindow  anid)
333 {
334   GdkPixmap *pixmap;
335   GdkDrawableImplX11 *draw_impl;
336   GdkPixmapImplX11 *pix_impl;
337   Pixmap xpixmap;
338   Window root_return;
339   int x_ret, y_ret;
340   unsigned int w_ret, h_ret, bw_ret, depth_ret;
341
342   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
343
344   /* check to make sure we were passed something at
345    * least a little sane */
346   g_return_val_if_fail ((anid != 0), NULL);
347   
348   /* set the pixmap to the passed in value */
349   xpixmap = anid;
350
351   /* get information about the Pixmap to fill in the structure for
352      the gdk window */
353   if (!XGetGeometry (GDK_DISPLAY_XDISPLAY (display),
354                      xpixmap, &root_return,
355                      &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
356     return NULL;
357
358   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
359   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
360   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
361   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
362
363   draw_impl->screen =  _gdk_x11_display_screen_for_xrootwin (display, root_return);
364   draw_impl->xid = xpixmap;
365
366   pix_impl->is_foreign = TRUE;
367   pix_impl->width = w_ret;
368   pix_impl->height = h_ret;
369   GDK_PIXMAP_OBJECT (pixmap)->depth = depth_ret;
370   
371   _gdk_xid_table_insert (display, &GDK_PIXMAP_XID (pixmap), pixmap);
372
373   return pixmap;
374 }
375
376 /**
377  * gdk_pixmap_foreign_new:
378  * @anid: a native pixmap handle.
379  * 
380  * Wraps a native window for the default display in a #GdkPixmap.
381  * This may fail if the pixmap has been destroyed.
382  *
383  * For example in the X backend, a native pixmap handle is an Xlib
384  * <type>XID</type>.
385  *
386  * Return value: the newly-created #GdkPixmap wrapper for the 
387  *    native pixmap or %NULL if the pixmap has been destroyed.
388  **/
389 GdkPixmap*
390 gdk_pixmap_foreign_new (GdkNativeWindow anid)
391 {
392    return gdk_pixmap_foreign_new_for_display (gdk_display_get_default (), anid);
393 }
394
395 /**
396  * gdk_pixmap_lookup:
397  * @anid: a native pixmap handle.
398  * 
399  * Looks up the #GdkPixmap that wraps the given native pixmap handle.
400  *
401  * For example in the X backend, a native pixmap handle is an Xlib
402  * <type>XID</type>.
403  *
404  * Return value: the #GdkWindow wrapper for the native window,
405  *    or %NULL if there is none.
406  **/
407 GdkPixmap*
408 gdk_pixmap_lookup (GdkNativeWindow anid)
409 {
410   return (GdkPixmap*) gdk_xid_table_lookup_for_display (gdk_display_get_default (), anid);
411 }
412
413 /**
414  * gdk_pixmap_lookup_for_display:
415  * @display : the #GdkDisplay associated with @anid
416  * @anid: a native pixmap handle.
417  * 
418  * Looks up the #GdkPixmap that wraps the given native pixmap handle.
419  *
420  * For example in the X backend, a native pixmap handle is an Xlib
421  * <type>XID</type>.
422  *
423  * Return value: the #GdkWindow wrapper for the native window,
424  *    or %NULL if there is none.
425  **/
426 GdkPixmap*
427 gdk_pixmap_lookup_for_display (GdkDisplay *display, GdkNativeWindow anid)
428 {
429   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
430   return (GdkPixmap*) gdk_xid_table_lookup_for_display (display, anid);
431 }