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