]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkpixmap-x11.c
Patch from Erwann Chenede, #73900 fixing a lot of warnings with Forte CC,
[~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
40 typedef struct
41 {
42   gchar *color_string;
43   GdkColor color;
44   gint transparent;
45 } _GdkPixmapColor;
46
47 typedef struct
48 {
49   guint ncolors;
50   GdkColormap *colormap;
51   gulong pixels[1];
52 } _GdkPixmapInfo;
53
54 static void gdk_pixmap_impl_x11_get_size   (GdkDrawable        *drawable,
55                                         gint               *width,
56                                         gint               *height);
57
58 static void gdk_pixmap_impl_x11_init       (GdkPixmapImplX11      *pixmap);
59 static void gdk_pixmap_impl_x11_class_init (GdkPixmapImplX11Class *klass);
60 static void gdk_pixmap_impl_x11_finalize   (GObject            *object);
61
62 static gpointer parent_class = NULL;
63
64 static GType
65 gdk_pixmap_impl_x11_get_type (void)
66 {
67   static GType object_type = 0;
68
69   if (!object_type)
70     {
71       static const GTypeInfo object_info =
72       {
73         sizeof (GdkPixmapImplX11Class),
74         (GBaseInitFunc) NULL,
75         (GBaseFinalizeFunc) NULL,
76         (GClassInitFunc) gdk_pixmap_impl_x11_class_init,
77         NULL,           /* class_finalize */
78         NULL,           /* class_data */
79         sizeof (GdkPixmapImplX11),
80         0,              /* n_preallocs */
81         (GInstanceInitFunc) gdk_pixmap_impl_x11_init,
82       };
83       
84       object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_X11,
85                                             "GdkPixmapImplX11",
86                                             &object_info, 0);
87     }
88   
89   return object_type;
90 }
91
92
93 GType
94 _gdk_pixmap_impl_get_type (void)
95 {
96   return gdk_pixmap_impl_x11_get_type ();
97 }
98
99 static void
100 gdk_pixmap_impl_x11_init (GdkPixmapImplX11 *impl)
101 {
102   impl->width = 1;
103   impl->height = 1;
104 }
105
106 static void
107 gdk_pixmap_impl_x11_class_init (GdkPixmapImplX11Class *klass)
108 {
109   GObjectClass *object_class = G_OBJECT_CLASS (klass);
110   GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
111   
112   parent_class = g_type_class_peek_parent (klass);
113
114   object_class->finalize = gdk_pixmap_impl_x11_finalize;
115
116   drawable_class->get_size = gdk_pixmap_impl_x11_get_size;
117 }
118
119 static void
120 gdk_pixmap_impl_x11_finalize (GObject *object)
121 {
122   GdkPixmapImplX11 *impl = GDK_PIXMAP_IMPL_X11 (object);
123   GdkPixmap *wrapper = GDK_PIXMAP (GDK_DRAWABLE_IMPL_X11 (impl)->wrapper);
124
125 #ifdef HAVE_XFT  
126   {
127     GdkDrawableImplX11 *draw_impl = GDK_DRAWABLE_IMPL_X11 (impl);
128
129     if (draw_impl->picture)
130       XRenderFreePicture (draw_impl->xdisplay, draw_impl->picture);
131   }
132 #endif /* HAVE_XFT */  
133
134   if (!impl->is_foreign)
135     XFreePixmap (GDK_PIXMAP_XDISPLAY (wrapper), GDK_PIXMAP_XID (wrapper));
136   
137   gdk_xid_table_remove (GDK_PIXMAP_XID (wrapper));
138   
139   G_OBJECT_CLASS (parent_class)->finalize (object);
140 }
141
142 static void
143 gdk_pixmap_impl_x11_get_size   (GdkDrawable *drawable,
144                                 gint        *width,
145                                 gint        *height)
146 {
147   if (width)
148     *width = GDK_PIXMAP_IMPL_X11 (drawable)->width;
149   if (height)
150     *height = GDK_PIXMAP_IMPL_X11 (drawable)->height;
151 }
152
153 GdkPixmap*
154 gdk_pixmap_new (GdkWindow *window,
155                 gint       width,
156                 gint       height,
157                 gint       depth)
158 {
159   GdkPixmap *pixmap;
160   GdkDrawableImplX11 *draw_impl;
161   GdkPixmapImplX11 *pix_impl;
162   GdkColormap *cmap;
163   gint window_depth;
164   
165   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
166   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
167   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
168   
169   if (!window)
170     window = _gdk_parent_root;
171
172   if (GDK_WINDOW_DESTROYED (window))
173     return NULL;
174
175   window_depth = gdk_drawable_get_depth (GDK_DRAWABLE (window));
176   if (depth == -1)
177     depth = window_depth;
178
179   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
180   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
181   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
182   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
183   
184   draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
185   draw_impl->xid = XCreatePixmap (GDK_PIXMAP_XDISPLAY (pixmap),
186                                   GDK_WINDOW_XID (window),
187                                   width, height, depth);
188   
189   pix_impl->is_foreign = FALSE;
190   pix_impl->width = width;
191   pix_impl->height = height;
192   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
193
194   if (depth == window_depth)
195     {
196       cmap = gdk_drawable_get_colormap (window);
197       if (cmap)
198         gdk_drawable_set_colormap (pixmap, cmap);
199     }
200   
201   gdk_xid_table_insert (&draw_impl->xid, pixmap);
202   
203   return pixmap;
204 }
205
206 GdkPixmap *
207 gdk_bitmap_create_from_data (GdkWindow   *window,
208                              const gchar *data,
209                              gint         width,
210                              gint         height)
211 {
212   GdkPixmap *pixmap;
213   GdkDrawableImplX11 *draw_impl;
214   GdkPixmapImplX11 *pix_impl;
215   
216   g_return_val_if_fail (data != NULL, NULL);
217   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
218   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
219
220   if (!window)
221     window = _gdk_parent_root;
222
223   if (GDK_WINDOW_DESTROYED (window))
224     return NULL;
225
226   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
227   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
228   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
229   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
230
231   pix_impl->is_foreign = FALSE;
232   pix_impl->width = width;
233   pix_impl->height = height;
234   GDK_PIXMAP_OBJECT (pixmap)->depth = 1;
235
236   draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
237   draw_impl->xid = XCreateBitmapFromData (GDK_WINDOW_XDISPLAY (window),
238                                           GDK_WINDOW_XID (window),
239                                           (char *)data, width, height);
240
241   gdk_xid_table_insert (&draw_impl->xid, pixmap);
242   
243   return pixmap;
244 }
245
246 GdkPixmap*
247 gdk_pixmap_create_from_data (GdkWindow   *window,
248                              const gchar *data,
249                              gint         width,
250                              gint         height,
251                              gint         depth,
252                              GdkColor    *fg,
253                              GdkColor    *bg)
254 {
255   GdkPixmap *pixmap;
256   GdkDrawableImplX11 *draw_impl;
257   GdkPixmapImplX11 *pix_impl;
258
259   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
260   g_return_val_if_fail (data != NULL, NULL);
261   g_return_val_if_fail (fg != NULL, NULL);
262   g_return_val_if_fail (bg != NULL, NULL);
263   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
264   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
265
266   if (!window)
267     window = _gdk_parent_root;
268
269   if (GDK_WINDOW_DESTROYED (window))
270     return NULL;
271
272   if (depth == -1)
273     depth = gdk_drawable_get_visual (window)->depth;
274
275   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
276   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
277   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
278   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
279   
280   pix_impl->is_foreign = FALSE;
281   pix_impl->width = width;
282   pix_impl->height = height;
283   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
284
285   draw_impl->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
286   draw_impl->xid = XCreatePixmapFromBitmapData (GDK_WINDOW_XDISPLAY (window),
287                                                 GDK_WINDOW_XID (window),
288                                                 (char *)data, width, height,
289                                                 fg->pixel, bg->pixel, depth);
290
291   gdk_xid_table_insert (&draw_impl->xid, pixmap);
292
293   return pixmap;
294 }
295
296 /**
297  * gdk_pixmap_foreign_new:
298  * @anid: a native pixmap handle.
299  * 
300  * Wraps a native window in a #GdkPixmap.
301  * This may fail if the pixmap has been destroyed.
302  *
303  * For example in the X backend, a native pixmap handle is an Xlib
304  * <type>XID</type>.
305  *
306  * Return value: the newly-created #GdkPixmap wrapper for the 
307  *    native pixmap or %NULL if the pixmap has been destroyed.
308  **/
309 GdkPixmap*
310 gdk_pixmap_foreign_new (GdkNativeWindow anid)
311 {
312   GdkPixmap *pixmap;
313   GdkDrawableImplX11 *draw_impl;
314   GdkPixmapImplX11 *pix_impl;
315   Pixmap xpixmap;
316   Window root_return;
317   int x_ret, y_ret;
318   unsigned int w_ret, h_ret, bw_ret, depth_ret;
319
320   /* check to make sure we were passed something at
321      least a little sane */
322   g_return_val_if_fail((anid != 0), NULL);
323   
324   /* set the pixmap to the passed in value */
325   xpixmap = anid;
326
327   /* get information about the Pixmap to fill in the structure for
328      the gdk window */
329   if (!XGetGeometry(GDK_DISPLAY(),
330                     xpixmap, &root_return,
331                     &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
332       return NULL;
333
334   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
335   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
336   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
337   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
338   
339
340   draw_impl->xdisplay = GDK_DISPLAY ();
341   draw_impl->xid = xpixmap;
342
343   pix_impl->is_foreign = TRUE;
344   pix_impl->width = w_ret;
345   pix_impl->height = h_ret;
346   GDK_PIXMAP_OBJECT (pixmap)->depth = depth_ret;
347   
348   gdk_xid_table_insert(&draw_impl->xid, pixmap);
349
350   return pixmap;
351 }
352
353 /**
354  * gdk_pixmap_lookup:
355  * @anid: a native pixmap handle.
356  * 
357  * Looks up the #GdkPixmap that wraps the given native pixmap handle.
358  *
359  * For example in the X backend, a native pixmap handle is an Xlib
360  * <type>XID</type>.
361  *
362  * Return value: the #GdkWindow wrapper for the native window,
363  *    or %NULL if there is none.
364  **/
365 GdkPixmap*
366 gdk_pixmap_lookup (GdkNativeWindow anid)
367 {
368   return (GdkPixmap*) gdk_xid_table_lookup (anid);
369 }