]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkpixmap-x11.c
Throughout: assorted docs
[~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   if (!impl->is_foreign)
126     XFreePixmap (GDK_PIXMAP_XDISPLAY (wrapper), GDK_PIXMAP_XID (wrapper));
127   
128   gdk_xid_table_remove (GDK_PIXMAP_XID (wrapper));
129   
130   G_OBJECT_CLASS (parent_class)->finalize (object);
131 }
132
133 static void
134 gdk_pixmap_impl_x11_get_size   (GdkDrawable *drawable,
135                                 gint        *width,
136                                 gint        *height)
137 {
138   if (width)
139     *width = GDK_PIXMAP_IMPL_X11 (drawable)->width;
140   if (height)
141     *height = GDK_PIXMAP_IMPL_X11 (drawable)->height;
142 }
143
144 GdkPixmap*
145 gdk_pixmap_new (GdkWindow *window,
146                 gint       width,
147                 gint       height,
148                 gint       depth)
149 {
150   GdkPixmap *pixmap;
151   GdkDrawableImplX11 *draw_impl;
152   GdkPixmapImplX11 *pix_impl;
153   GdkColormap *cmap;
154   gint window_depth;
155   
156   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
157   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
158   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
159   
160   if (!window)
161     window = _gdk_parent_root;
162
163   if (GDK_WINDOW_DESTROYED (window))
164     return NULL;
165
166   window_depth = gdk_drawable_get_depth (GDK_DRAWABLE (window));
167   if (depth == -1)
168     depth = window_depth;
169
170   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
171   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
172   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
173   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
174   
175   draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
176   draw_impl->xid = XCreatePixmap (GDK_PIXMAP_XDISPLAY (pixmap),
177                                   GDK_WINDOW_XID (window),
178                                   width, height, depth);
179   
180   pix_impl->is_foreign = FALSE;
181   pix_impl->width = width;
182   pix_impl->height = height;
183   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
184
185   if (depth == window_depth)
186     {
187       cmap = gdk_drawable_get_colormap (window);
188       if (cmap)
189         gdk_drawable_set_colormap (pixmap, cmap);
190     }
191   
192   gdk_xid_table_insert (&draw_impl->xid, pixmap);
193   
194   return pixmap;
195 }
196
197 GdkPixmap *
198 gdk_bitmap_create_from_data (GdkWindow   *window,
199                              const gchar *data,
200                              gint         width,
201                              gint         height)
202 {
203   GdkPixmap *pixmap;
204   GdkDrawableImplX11 *draw_impl;
205   GdkPixmapImplX11 *pix_impl;
206   
207   g_return_val_if_fail (data != NULL, NULL);
208   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
209   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
210
211   if (!window)
212     window = _gdk_parent_root;
213
214   if (GDK_WINDOW_DESTROYED (window))
215     return NULL;
216
217   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
218   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
219   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
220   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
221
222   pix_impl->is_foreign = FALSE;
223   pix_impl->width = width;
224   pix_impl->height = height;
225   GDK_PIXMAP_OBJECT (pixmap)->depth = 1;
226
227   draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
228   draw_impl->xid = XCreateBitmapFromData (GDK_WINDOW_XDISPLAY (window),
229                                           GDK_WINDOW_XID (window),
230                                           (char *)data, width, height);
231
232   gdk_xid_table_insert (&draw_impl->xid, pixmap);
233   
234   return pixmap;
235 }
236
237 GdkPixmap*
238 gdk_pixmap_create_from_data (GdkWindow   *window,
239                              const gchar *data,
240                              gint         width,
241                              gint         height,
242                              gint         depth,
243                              GdkColor    *fg,
244                              GdkColor    *bg)
245 {
246   GdkPixmap *pixmap;
247   GdkDrawableImplX11 *draw_impl;
248   GdkPixmapImplX11 *pix_impl;
249
250   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
251   g_return_val_if_fail (data != NULL, NULL);
252   g_return_val_if_fail (fg != NULL, NULL);
253   g_return_val_if_fail (bg != NULL, NULL);
254   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
255   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
256
257   if (!window)
258     window = _gdk_parent_root;
259
260   if (GDK_WINDOW_DESTROYED (window))
261     return NULL;
262
263   if (depth == -1)
264     depth = gdk_drawable_get_visual (window)->depth;
265
266   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
267   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
268   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
269   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
270   
271   pix_impl->is_foreign = FALSE;
272   pix_impl->width = width;
273   pix_impl->height = height;
274   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
275
276   draw_impl->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
277   draw_impl->xid = XCreatePixmapFromBitmapData (GDK_WINDOW_XDISPLAY (window),
278                                                 GDK_WINDOW_XID (window),
279                                                 (char *)data, width, height,
280                                                 fg->pixel, bg->pixel, depth);
281
282   gdk_xid_table_insert (&draw_impl->xid, pixmap);
283
284   return pixmap;
285 }
286
287 /**
288  * gdk_pixmap_foreign_new:
289  * @anid: an X window ID for a pixmap
290  * 
291  * Creates a #GdkPixmap for the Xlib pixmap ID @anid.
292  * If the Xlib pixmap is destroyed while creating
293  * the #GdkPixmap, this function returns %NULL.
294  * 
295  * Return value: a new #GdkPixmap
296  **/
297 GdkPixmap*
298 gdk_pixmap_foreign_new (GdkNativeWindow anid)
299 {
300   GdkPixmap *pixmap;
301   GdkDrawableImplX11 *draw_impl;
302   GdkPixmapImplX11 *pix_impl;
303   Pixmap xpixmap;
304   Window root_return;
305   unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret;
306
307   /* check to make sure we were passed something at
308      least a little sane */
309   g_return_val_if_fail((anid != 0), NULL);
310   
311   /* set the pixmap to the passed in value */
312   xpixmap = anid;
313
314   /* get information about the Pixmap to fill in the structure for
315      the gdk window */
316   if (!XGetGeometry(GDK_DISPLAY(),
317                     xpixmap, &root_return,
318                     &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
319       return NULL;
320
321   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
322   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
323   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
324   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
325   
326
327   draw_impl->xdisplay = GDK_DISPLAY ();
328   draw_impl->xid = xpixmap;
329
330   pix_impl->is_foreign = TRUE;
331   pix_impl->width = w_ret;
332   pix_impl->height = h_ret;
333   GDK_PIXMAP_OBJECT (pixmap)->depth = depth_ret;
334   
335   gdk_xid_table_insert(&draw_impl->xid, pixmap);
336
337   return pixmap;
338 }