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