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