]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkcursor-win32.c
Further hacking. Now actually compiles. Haven't tried yet whether
[~andy/gtk] / gdk / win32 / gdkcursor-win32.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 #include "config.h"
21
22 #include "gdkcursor.h"
23 #include "gdkwin32.h"
24
25 static const struct { const char *name; int type; } cursors[] = {
26   { "x_cursor", 0 },
27   { "arrow", 2 },
28   { "based_arrow_down", 4 },
29   { "based_arrow_up", 6 },
30   { "boat", 8 },
31   { "bogosity", 10 },
32   { "bottom_left_corner", 12 },
33   { "bottom_right_corner", 14 },
34   { "bottom_side", 16 },
35   { "bottom_tee", 18 },
36   { "box_spiral", 20 },
37   { "center_ptr", 22 },
38   { "circle", 24 },
39   { "clock", 26 },
40   { "coffee_mug", 28 },
41   { "cross", 30 },
42   { "cross_reverse", 32 },
43   { "crosshair", 34 },
44   { "diamond_cross", 36 },
45   { "dot", 38 },
46   { "dotbox", 40 },
47   { "double_arrow", 42 },
48   { "draft_large", 44 },
49   { "draft_small", 46 },
50   { "draped_box", 48 },
51   { "exchange", 50 },
52   { "fleur", 52 },
53   { "gobbler", 54 },
54   { "gumby", 56 },
55   { "hand1", 58 },
56   { "hand2", 60 },
57   { "heart", 62 },
58   { "icon", 64 },
59   { "iron_cross", 66 },
60   { "left_ptr", 68 },
61   { "left_side", 70 },
62   { "left_tee", 72 },
63   { "leftbutton", 74 },
64   { "ll_angle", 76 },
65   { "lr_angle", 78 },
66   { "man", 80 },
67   { "middlebutton", 82 },
68   { "mouse", 84 },
69   { "pencil", 86 },
70   { "pirate", 88 },
71   { "plus", 90 },
72   { "question_arrow", 92 },
73   { "right_ptr", 94 },
74   { "right_side", 96 },
75   { "right_tee", 98 },
76   { "rightbutton", 100 },
77   { "rtl_logo", 102 },
78   { "sailboat", 104 },
79   { "sb_down_arrow", 106 },
80   { "sb_h_double_arrow", 108 },
81   { "sb_left_arrow", 110 },
82   { "sb_right_arrow", 112 },
83   { "sb_up_arrow", 114 },
84   { "sb_v_double_arrow", 116 },
85   { "shuttle", 118 },
86   { "sizing", 120 },
87   { "spider", 122 },
88   { "spraycan", 124 },
89   { "star", 126 },
90   { "target", 128 },
91   { "tcross", 130 },
92   { "top_left_arrow", 132 },
93   { "top_left_corner", 134 },
94   { "top_right_corner", 136 },
95   { "top_side", 138 },
96   { "top_tee", 140 },
97   { "trek", 142 },
98   { "ul_angle", 144 },
99   { "umbrella", 146 },
100   { "ur_angle", 148 },
101   { "watch", 150 },
102   { "xterm", 152 },
103   { NULL, 0 }
104 };  
105
106 GdkCursor*
107 gdk_cursor_new (GdkCursorType cursor_type)
108 {
109   GdkCursorPrivate *private;
110   GdkCursor *cursor;
111   HCURSOR xcursor;
112   int i;
113
114   for (i = 0; cursors[i].name != NULL && cursors[i].type != cursor_type; i++)
115     ;
116   if (cursors[i].name != NULL)
117     {
118       xcursor = LoadCursor (gdk_DLLInstance, cursors[i].name);
119       if (xcursor == NULL)
120         g_warning ("gdk_cursor_new: LoadCursor failed");
121       GDK_NOTE (MISC, g_print ("gdk_cursor_new: %#x %d\n",
122                                xcursor, cursor_type));
123     }
124   else
125     {
126       g_warning ("gdk_cursor_new: no cursor %d found",
127                  cursor_type);
128       xcursor = NULL;
129     }
130
131   private = g_new (GdkCursorPrivate, 1);
132   private->xcursor = xcursor;
133   cursor = (GdkCursor*) private;
134   cursor->type = cursor_type;
135
136   return cursor;
137 }
138
139 GdkCursor*
140 gdk_cursor_new_from_pixmap (GdkPixmap *source,
141                             GdkPixmap *mask,
142                             GdkColor  *fg,
143                             GdkColor  *bg,
144                             gint       x,
145                             gint       y)
146 {
147   GdkCursorPrivate *private;
148   GdkCursor *cursor;
149   GdkDrawablePrivate *source_private, *mask_private;
150   GdkImage *source_image, *mask_image;
151   HCURSOR xcursor;
152   guchar *p, *q, *XORmask, *ANDmask;
153   gint width, height, cursor_width, cursor_height;
154   guchar residue;
155   gint ix, iy;
156   
157   g_return_val_if_fail (source != NULL, NULL);
158   g_return_val_if_fail (mask != NULL, NULL);
159
160   source_private = (GdkDrawablePrivate *) source;
161   mask_private   = (GdkDrawablePrivate *) mask;
162
163   g_return_val_if_fail (source_private->width == mask_private->width
164                         && source_private->height == mask_private->height,
165                         NULL);
166   width = source_private->width;
167   height = source_private->height;
168   cursor_width = GetSystemMetrics (SM_CXCURSOR);
169   cursor_height = GetSystemMetrics (SM_CYCURSOR);
170
171   g_return_val_if_fail (width <= cursor_width
172                         && height <= cursor_height, NULL);
173
174   residue = (1 << ((8-(width%8))%8)) - 1;
175
176   source_image = gdk_image_get (source, 0, 0, width, height);
177   mask_image = gdk_image_get (mask, 0, 0, width, height);
178
179   if (source_image->depth != 1 || mask_image->depth != 1)
180     {
181     gdk_image_unref (source_image);
182     gdk_image_unref (mask_image);
183     g_return_val_if_fail (source_image->depth == 1 && mask_image->depth == 1,
184                           NULL);
185     }
186
187   /* Such complex bit manipulation for this simple task, sigh.
188    * The X cursor and Windows cursor concepts are quite different.
189    * We assume here that we are always called with fg == black and
190    * bg == white.
191    */
192
193   /* First set masked-out source bits, as all source bits matter on Windoze.
194    * As we invert them below, they will be clear in the final XORmask.
195    */
196   for (iy = 0; iy < height; iy++)
197     {
198       p = (guchar *) source_image->mem + iy*source_image->bpl;
199       q = (guchar *) mask_image->mem + iy*mask_image->bpl;
200       
201       for (ix = 0; ix < ((width-1)/8+1); ix++)
202         *p++ |= ~(*q++);
203     }
204
205   /* XOR mask is initialized to zero */
206   XORmask = g_malloc0 (cursor_width/8 * cursor_height);
207
208   for (iy = 0; iy < height; iy++)
209     {
210       p = (guchar *) source_image->mem + iy*source_image->bpl;
211       q = XORmask + iy*cursor_width/8;
212
213       for (ix = 0; ix < ((width-1)/8+1); ix++)
214         *q++ = ~(*p++);
215       q[-1] &= ~residue;        /* Clear left-over bits */
216     }
217       
218   /* AND mask is initialized to ones */
219   ANDmask = g_malloc (cursor_width/8 * cursor_height);
220   memset (ANDmask, 0xFF, cursor_width/8 * cursor_height);
221
222   for (iy = 0; iy < height; iy++)
223     {
224       p = (guchar *) mask_image->mem + iy*mask_image->bpl;
225       q = ANDmask + iy*cursor_width/8;
226
227       for (ix = 0; ix < ((width-1)/8+1); ix++)
228         *q++ = ~(*p++);
229       q[-1] |= residue; /* Set left-over bits */
230     }
231       
232   xcursor = CreateCursor (gdk_ProgInstance, x, y, cursor_width, cursor_height,
233                           ANDmask, XORmask);
234
235   GDK_NOTE (MISC, g_print ("gdk_cursor_new_from_pixmap: "
236                            "%#x (%dx%d) %#x (%dx%d) = %#x (%dx%d)\n",
237                            GDK_DRAWABLE_XID (source),
238                            source_private->width, source_private->height,
239                            GDK_DRAWABLE_XID (mask),
240                            mask_private->width, mask_private->height,
241                            xcursor, cursor_width, cursor_height));
242
243   g_free (XORmask);
244   g_free (ANDmask);
245
246   gdk_image_unref (source_image);
247   gdk_image_unref (mask_image);
248
249   private = g_new (GdkCursorPrivate, 1);
250   private->xcursor = xcursor;
251   cursor = (GdkCursor*) private;
252   cursor->type = GDK_CURSOR_IS_PIXMAP;
253
254   return cursor;
255 }
256
257 void
258 gdk_cursor_destroy (GdkCursor *cursor)
259 {
260   GdkCursorPrivate *private;
261
262   g_return_if_fail (cursor != NULL);
263   private = (GdkCursorPrivate *) cursor;
264
265   GDK_NOTE (MISC, g_print ("gdk_cursor_destroy: %#x\n",
266                            (cursor->type == GDK_CURSOR_IS_PIXMAP) ? private->xcursor : 0));
267
268   if (cursor->type == GDK_CURSOR_IS_PIXMAP)
269     if (!DestroyIcon (private->xcursor))
270       g_warning ("gdk_cursor_destroy: DestroyIcon failed");
271
272   g_free (private);
273 }