]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkpixmap-x11.c
Add a is_foreign flag to the structure. Do not call XFreePixmap on pixmaps
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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-1999.  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 <gdk/gdkpixmap.h>
36 #include "gdkpixmap-x11.h"
37 #include "gdkprivate-x11.h"
38
39 typedef struct
40 {
41   gchar *color_string;
42   GdkColor color;
43   gint transparent;
44 } _GdkPixmapColor;
45
46 typedef struct
47 {
48   guint ncolors;
49   GdkColormap *colormap;
50   gulong pixels[1];
51 } _GdkPixmapInfo;
52
53 static void gdk_pixmap_impl_x11_get_size   (GdkDrawable        *drawable,
54                                         gint               *width,
55                                         gint               *height);
56
57 static void gdk_pixmap_impl_x11_init       (GdkPixmapImplX11      *pixmap);
58 static void gdk_pixmap_impl_x11_class_init (GdkPixmapImplX11Class *klass);
59 static void gdk_pixmap_impl_x11_finalize   (GObject            *object);
60
61 static gpointer parent_class = NULL;
62
63 GType
64 gdk_pixmap_impl_x11_get_type (void)
65 {
66   static GType object_type = 0;
67
68   if (!object_type)
69     {
70       static const GTypeInfo object_info =
71       {
72         sizeof (GdkPixmapImplX11Class),
73         (GBaseInitFunc) NULL,
74         (GBaseFinalizeFunc) NULL,
75         (GClassInitFunc) gdk_pixmap_impl_x11_class_init,
76         NULL,           /* class_finalize */
77         NULL,           /* class_data */
78         sizeof (GdkPixmapImplX11),
79         0,              /* n_preallocs */
80         (GInstanceInitFunc) gdk_pixmap_impl_x11_init,
81       };
82       
83       object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_X11,
84                                             "GdkPixmapImplX11",
85                                             &object_info);
86     }
87   
88   return object_type;
89 }
90
91
92 GType
93 _gdk_pixmap_impl_get_type (void)
94 {
95   return gdk_pixmap_impl_x11_get_type ();
96 }
97
98 static void
99 gdk_pixmap_impl_x11_init (GdkPixmapImplX11 *impl)
100 {
101   impl->width = 1;
102   impl->height = 1;
103 }
104
105 static void
106 gdk_pixmap_impl_x11_class_init (GdkPixmapImplX11Class *klass)
107 {
108   GObjectClass *object_class = G_OBJECT_CLASS (klass);
109   GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
110   
111   parent_class = g_type_class_peek_parent (klass);
112
113   object_class->finalize = gdk_pixmap_impl_x11_finalize;
114
115   drawable_class->get_size = gdk_pixmap_impl_x11_get_size;
116 }
117
118 static void
119 gdk_pixmap_impl_x11_finalize (GObject *object)
120 {
121   GdkPixmapImplX11 *impl = GDK_PIXMAP_IMPL_X11 (object);
122   GdkPixmap *wrapper = GDK_PIXMAP (GDK_DRAWABLE_IMPL_X11 (impl)->wrapper);
123
124   if (!impl->is_foreign)
125     XFreePixmap (GDK_PIXMAP_XDISPLAY (wrapper), GDK_PIXMAP_XID (wrapper));
126   
127   gdk_xid_table_remove (GDK_PIXMAP_XID (wrapper));
128   
129   G_OBJECT_CLASS (parent_class)->finalize (object);
130 }
131
132 static void
133 gdk_pixmap_impl_x11_get_size   (GdkDrawable *drawable,
134                                 gint        *width,
135                                 gint        *height)
136 {
137   if (width)
138     *width = GDK_PIXMAP_IMPL_X11 (drawable)->width;
139   if (height)
140     *height = GDK_PIXMAP_IMPL_X11 (drawable)->height;
141 }
142
143 GdkPixmap*
144 gdk_pixmap_new (GdkWindow *window,
145                 gint       width,
146                 gint       height,
147                 gint       depth)
148 {
149   GdkPixmap *pixmap;
150   GdkDrawableImplX11 *draw_impl;
151   GdkPixmapImplX11 *pix_impl;
152   
153   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
154   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
155   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
156   
157   if (!window)
158     window = gdk_parent_root;
159
160   if (GDK_WINDOW_DESTROYED (window))
161     return NULL;
162
163   if (depth == -1)
164     depth = gdk_drawable_get_depth (GDK_DRAWABLE (window));
165
166   pixmap = GDK_PIXMAP (g_type_create_instance (gdk_pixmap_get_type ()));
167   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
168   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
169   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
170   
171   draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
172   draw_impl->xid = XCreatePixmap (GDK_PIXMAP_XDISPLAY (pixmap),
173                                   GDK_WINDOW_XID (window),
174                                   width, height, depth);
175   
176   pix_impl->is_foreign = FALSE;
177   pix_impl->width = width;
178   pix_impl->height = height;
179   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
180   
181   gdk_xid_table_insert (&GDK_PIXMAP_XID (pixmap), pixmap);
182
183   return pixmap;
184 }
185
186 GdkPixmap *
187 gdk_bitmap_create_from_data (GdkWindow   *window,
188                              const gchar *data,
189                              gint         width,
190                              gint         height)
191 {
192   GdkPixmap *pixmap;
193   GdkDrawableImplX11 *draw_impl;
194   GdkPixmapImplX11 *pix_impl;
195   
196   g_return_val_if_fail (data != NULL, NULL);
197   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
198   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
199
200   if (!window)
201     window = gdk_parent_root;
202
203   if (GDK_WINDOW_DESTROYED (window))
204     return NULL;
205
206   pixmap = GDK_PIXMAP (g_type_create_instance (gdk_pixmap_get_type ()));
207   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
208   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
209   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
210
211   pix_impl->is_foreign = FALSE;
212   pix_impl->width = width;
213   pix_impl->height = height;
214   GDK_PIXMAP_OBJECT (pixmap)->depth = 1;
215
216   draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
217   draw_impl->xid = XCreateBitmapFromData (GDK_WINDOW_XDISPLAY (window),
218                                           GDK_WINDOW_XID (window),
219                                           (char *)data, width, height);
220
221   gdk_xid_table_insert (&GDK_PIXMAP_XID (pixmap), pixmap);
222   
223   return pixmap;
224 }
225
226 GdkPixmap*
227 gdk_pixmap_create_from_data (GdkWindow   *window,
228                              const gchar *data,
229                              gint         width,
230                              gint         height,
231                              gint         depth,
232                              GdkColor    *fg,
233                              GdkColor    *bg)
234 {
235   GdkPixmap *pixmap;
236   GdkDrawableImplX11 *draw_impl;
237   GdkPixmapImplX11 *pix_impl;
238
239   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
240   g_return_val_if_fail (data != NULL, NULL);
241   g_return_val_if_fail (fg != NULL, NULL);
242   g_return_val_if_fail (bg != NULL, NULL);
243   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
244   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
245
246   if (!window)
247     window = gdk_parent_root;
248
249   if (GDK_WINDOW_DESTROYED (window))
250     return NULL;
251
252   if (depth == -1)
253     depth = gdk_drawable_get_visual (window)->depth;
254
255   pixmap = GDK_PIXMAP (g_type_create_instance (gdk_pixmap_get_type ()));
256   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
257   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
258   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
259   
260   pix_impl->is_foreign = FALSE;
261   pix_impl->width = width;
262   pix_impl->height = height;
263   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
264
265   draw_impl->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
266   draw_impl->xid = XCreatePixmapFromBitmapData (GDK_WINDOW_XDISPLAY (window),
267                                                 GDK_WINDOW_XID (window),
268                                                 (char *)data, width, height,
269                                                 fg->pixel, bg->pixel, depth);
270
271   gdk_xid_table_insert (&GDK_PIXMAP_XID (pixmap), pixmap);
272
273   return pixmap;
274 }
275
276 GdkPixmap*
277 gdk_pixmap_foreign_new (GdkNativeWindow anid)
278 {
279   GdkPixmap *pixmap;
280   GdkDrawableImplX11 *draw_impl;
281   GdkPixmapImplX11 *pix_impl;
282   Pixmap xpixmap;
283   Window root_return;
284   unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret;
285
286   /* check to make sure we were passed something at
287      least a little sane */
288   g_return_val_if_fail((anid != 0), NULL);
289   
290   /* set the pixmap to the passed in value */
291   xpixmap = anid;
292
293   /* get information about the Pixmap to fill in the structure for
294      the gdk window */
295   if (!XGetGeometry(GDK_DISPLAY(),
296                     xpixmap, &root_return,
297                     &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
298       return NULL;
299
300   pixmap = GDK_PIXMAP (g_type_create_instance (gdk_pixmap_get_type ()));
301   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
302   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
303   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
304   
305
306   draw_impl->xdisplay = GDK_DISPLAY ();
307   draw_impl->xid = xpixmap;
308
309   pix_impl->is_foreign = TRUE;
310   pix_impl->width = w_ret;
311   pix_impl->height = h_ret;
312   GDK_PIXMAP_OBJECT (pixmap)->depth = depth_ret;
313   
314   gdk_xid_table_insert(&GDK_PIXMAP_XID (pixmap), pixmap);
315
316   return pixmap;
317 }