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