]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkpixmap-x11.c
use _gdk_region_get_xrectangles()
[~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 <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, 0);
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   GdkColormap *cmap;
153   
154   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
155   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
156   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
157   
158   if (!window)
159     window = gdk_parent_root;
160
161   if (GDK_WINDOW_DESTROYED (window))
162     return NULL;
163
164   if (depth == -1)
165     depth = gdk_drawable_get_depth (GDK_DRAWABLE (window));
166
167   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
168   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
169   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
170   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
171   
172   draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
173   draw_impl->xid = XCreatePixmap (GDK_PIXMAP_XDISPLAY (pixmap),
174                                   GDK_WINDOW_XID (window),
175                                   width, height, depth);
176   
177   pix_impl->is_foreign = FALSE;
178   pix_impl->width = width;
179   pix_impl->height = height;
180   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
181
182   if (window)
183     {
184       cmap = gdk_drawable_get_colormap (window);
185       if (cmap)
186         gdk_drawable_set_colormap (pixmap, cmap);
187     }
188   
189   gdk_xid_table_insert (&GDK_PIXMAP_XID (pixmap), pixmap);
190   
191   return pixmap;
192 }
193
194 GdkPixmap *
195 gdk_bitmap_create_from_data (GdkWindow   *window,
196                              const gchar *data,
197                              gint         width,
198                              gint         height)
199 {
200   GdkPixmap *pixmap;
201   GdkDrawableImplX11 *draw_impl;
202   GdkPixmapImplX11 *pix_impl;
203   
204   g_return_val_if_fail (data != NULL, NULL);
205   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
206   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
207
208   if (!window)
209     window = gdk_parent_root;
210
211   if (GDK_WINDOW_DESTROYED (window))
212     return NULL;
213
214   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
215   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
216   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
217   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
218
219   pix_impl->is_foreign = FALSE;
220   pix_impl->width = width;
221   pix_impl->height = height;
222   GDK_PIXMAP_OBJECT (pixmap)->depth = 1;
223
224   draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (window);
225   draw_impl->xid = XCreateBitmapFromData (GDK_WINDOW_XDISPLAY (window),
226                                           GDK_WINDOW_XID (window),
227                                           (char *)data, width, height);
228
229   gdk_xid_table_insert (&GDK_PIXMAP_XID (pixmap), pixmap);
230   
231   return pixmap;
232 }
233
234 GdkPixmap*
235 gdk_pixmap_create_from_data (GdkWindow   *window,
236                              const gchar *data,
237                              gint         width,
238                              gint         height,
239                              gint         depth,
240                              GdkColor    *fg,
241                              GdkColor    *bg)
242 {
243   GdkPixmap *pixmap;
244   GdkDrawableImplX11 *draw_impl;
245   GdkPixmapImplX11 *pix_impl;
246
247   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
248   g_return_val_if_fail (data != NULL, NULL);
249   g_return_val_if_fail (fg != NULL, NULL);
250   g_return_val_if_fail (bg != NULL, NULL);
251   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
252   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
253
254   if (!window)
255     window = gdk_parent_root;
256
257   if (GDK_WINDOW_DESTROYED (window))
258     return NULL;
259
260   if (depth == -1)
261     depth = gdk_drawable_get_visual (window)->depth;
262
263   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
264   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
265   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
266   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
267   
268   pix_impl->is_foreign = FALSE;
269   pix_impl->width = width;
270   pix_impl->height = height;
271   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
272
273   draw_impl->xdisplay = GDK_DRAWABLE_XDISPLAY (window);
274   draw_impl->xid = XCreatePixmapFromBitmapData (GDK_WINDOW_XDISPLAY (window),
275                                                 GDK_WINDOW_XID (window),
276                                                 (char *)data, width, height,
277                                                 fg->pixel, bg->pixel, depth);
278
279   gdk_xid_table_insert (&GDK_PIXMAP_XID (pixmap), pixmap);
280
281   return pixmap;
282 }
283
284 GdkPixmap*
285 gdk_pixmap_foreign_new (GdkNativeWindow anid)
286 {
287   GdkPixmap *pixmap;
288   GdkDrawableImplX11 *draw_impl;
289   GdkPixmapImplX11 *pix_impl;
290   Pixmap xpixmap;
291   Window root_return;
292   unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret;
293
294   /* check to make sure we were passed something at
295      least a little sane */
296   g_return_val_if_fail((anid != 0), NULL);
297   
298   /* set the pixmap to the passed in value */
299   xpixmap = anid;
300
301   /* get information about the Pixmap to fill in the structure for
302      the gdk window */
303   if (!XGetGeometry(GDK_DISPLAY(),
304                     xpixmap, &root_return,
305                     &x_ret, &y_ret, &w_ret, &h_ret, &bw_ret, &depth_ret))
306       return NULL;
307
308   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
309   draw_impl = GDK_DRAWABLE_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
310   pix_impl = GDK_PIXMAP_IMPL_X11 (GDK_PIXMAP_OBJECT (pixmap)->impl);
311   draw_impl->wrapper = GDK_DRAWABLE (pixmap);
312   
313
314   draw_impl->xdisplay = GDK_DISPLAY ();
315   draw_impl->xid = xpixmap;
316
317   pix_impl->is_foreign = TRUE;
318   pix_impl->width = w_ret;
319   pix_impl->height = h_ret;
320   GDK_PIXMAP_OBJECT (pixmap)->depth = depth_ret;
321   
322   gdk_xid_table_insert(&GDK_PIXMAP_XID (pixmap), pixmap);
323
324   return pixmap;
325 }