]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkcursor-win32.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~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  * Copyright (C) 1998-2002 Tor Lillqvist
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22 #include "gdkdisplay.h"
23 #include "gdkscreen.h"
24 #include "gdkcursor.h"
25 #include "gdkprivate-win32.h"
26
27 #include "xcursors.h"
28
29 static HCURSOR
30 _gdk_win32_data_to_wcursor (GdkCursorType cursor_type)
31 {
32   gint i, j, x, y, ofs;
33   HCURSOR rv = NULL;
34   gint w, h;
35   guchar *and_plane, *xor_plane;
36
37   for (i = 0; i < G_N_ELEMENTS (cursors); i++)
38     if (cursors[i].type == cursor_type)
39       break;
40
41   if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name)
42     return NULL;
43
44   w = GetSystemMetrics (SM_CXCURSOR);
45   h = GetSystemMetrics (SM_CYCURSOR);
46
47   and_plane = g_malloc ((w/8) * h);
48   memset (and_plane, 0xff, (w/8) * h);
49   xor_plane = g_malloc ((w/8) * h);
50   memset (xor_plane, 0, (w/8) * h);
51
52 #define SET_BIT(v,b)  (v |= (1 << b))
53 #define RESET_BIT(v,b)  (v &= ~(1 << b))
54
55   for (j = 0, y = 0; y < cursors[i].height && y < h ; y++)
56     {
57       ofs = (y * w) / 8;
58       j = y * cursors[i].width;
59
60       for (x = 0; x < cursors[i].width && x < w ; x++, j++)
61       {
62         gint pofs = ofs + x / 8;
63         guchar data = (cursors[i].data[j/4] & (0xc0 >> (2 * (j%4)))) >> (2 * (3 - (j%4)));
64         gint bit = 7 - (j % cursors[i].width) % 8;
65
66         if (data)
67           {
68             RESET_BIT (and_plane[pofs], bit);
69             if (data == 1)
70               SET_BIT (xor_plane[pofs], bit);
71           }
72       }
73     }
74
75 #undef SET_BIT
76 #undef RESET_BIT
77
78   rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
79                      w, h, and_plane, xor_plane);
80   if (rv == NULL)
81     WIN32_API_FAILED ("CreateCursor");
82   g_free (and_plane);
83   g_free (xor_plane);
84   
85   return rv;
86 }
87
88 GdkCursor*
89 gdk_cursor_new_for_display (GdkDisplay   *display,
90                             GdkCursorType cursor_type)
91 {
92   GdkCursorPrivate *private;
93   GdkCursor *cursor;
94   HCURSOR hcursor;
95
96   g_return_val_if_fail (display == gdk_display_get_default (), NULL);
97
98   hcursor = _gdk_win32_data_to_wcursor (cursor_type);
99
100   if (hcursor == NULL)
101     g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type);
102   else
103     GDK_NOTE (MISC, g_print ("gdk_cursor_new_for_display: %d: %p\n",
104                              cursor_type, hcursor));
105
106   private = g_new (GdkCursorPrivate, 1);
107   private->hcursor = hcursor;
108   cursor = (GdkCursor*) private;
109   cursor->type = cursor_type;
110   cursor->ref_count = 1;
111
112   return cursor;
113 }
114
115 static gboolean
116 color_is_white (const GdkColor *color)
117 {
118   return (color->red == 0xFFFF
119           && color->green == 0xFFFF
120           && color->blue == 0xFFFF);
121 }
122
123 GdkCursor*
124 gdk_cursor_new_from_pixmap (GdkPixmap      *source,
125                             GdkPixmap      *mask,
126                             const GdkColor *fg,
127                             const GdkColor *bg,
128                             gint            x,
129                             gint            y)
130 {
131   GdkCursorPrivate *private;
132   GdkCursor *cursor;
133   GdkPixmapImplWin32 *source_impl, *mask_impl;
134   guchar *source_bits, *mask_bits;
135   gint source_bpl, mask_bpl;
136   HCURSOR hcursor;
137   guchar *p, *q, *xor_mask, *and_mask;
138   gint width, height, cursor_width, cursor_height;
139   guchar residue;
140   gint ix, iy;
141   const gboolean bg_is_white = color_is_white (bg);
142   
143   g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
144   g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
145   g_return_val_if_fail (fg != NULL, NULL);
146   g_return_val_if_fail (bg != NULL, NULL);
147
148   source_impl = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (source)->impl);
149   mask_impl = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (mask)->impl);
150
151   g_return_val_if_fail (source_impl->width == mask_impl->width
152                         && source_impl->height == mask_impl->height,
153                         NULL);
154   width = source_impl->width;
155   height = source_impl->height;
156   cursor_width = GetSystemMetrics (SM_CXCURSOR);
157   cursor_height = GetSystemMetrics (SM_CYCURSOR);
158
159   g_return_val_if_fail (width <= cursor_width && height <= cursor_height,
160                         NULL);
161
162   residue = (1 << ((8-(width%8))%8)) - 1;
163
164   source_bits = source_impl->bits;
165   mask_bits = mask_impl->bits;
166
167   g_return_val_if_fail (GDK_PIXMAP_OBJECT (source)->depth == 1
168                         && GDK_PIXMAP_OBJECT (mask)->depth == 1,
169                         NULL);
170
171   source_bpl = ((width - 1)/32 + 1)*4;
172   mask_bpl = ((mask_impl->width - 1)/32 + 1)*4;
173
174 #ifdef G_ENABLE_DEBUG
175   if (_gdk_debug_flags & GDK_DEBUG_CURSOR)
176     {
177       g_print ("gdk_cursor_new_from_pixmap: source=%p:\n",
178                source_impl->parent_instance.handle);
179       for (iy = 0; iy < height; iy++)
180         {
181           if (iy == 16)
182             break;
183
184           p = source_bits + iy*source_bpl;
185           for (ix = 0; ix < width; ix++)
186             {
187               if (ix == 79)
188                 break;
189               g_print ("%c", ".X"[((*p)>>(7-(ix%8)))&1]);
190               if ((ix%8) == 7)
191                 p++;
192             }
193           g_print ("\n");
194         }
195       g_print ("...mask=%p:\n", mask_impl->parent_instance.handle);
196       for (iy = 0; iy < height; iy++)
197         {
198           if (iy == 16)
199             break;
200
201           p = mask_bits + iy*source_bpl;
202           for (ix = 0; ix < width; ix++)
203             {
204               if (ix == 79)
205                 break;
206               g_print ("%c", ".X"[((*p)>>(7-(ix%8)))&1]);
207               if ((ix%8) == 7)
208                 p++;
209             }
210           g_print ("\n");
211         }
212     }
213 #endif
214
215   /* Such complex bit manipulation for this simple task, sigh.
216    * The X cursor and Windows cursor concepts are quite different.
217    * We assume here that we are always called with fg == black and
218    * bg == white, *or* the other way around. Random colours won't work.
219    * (Well, you will get a cursor, but not in those colours.)
220    */
221
222   /* Note: The comments below refer to the case fg==black and
223    * bg==white, as that was what was implemented first. The fg==white
224    * (the "if (fg->pixel)" branches) case was added later.
225    */
226
227   /* First set masked-out source bits, as all source bits matter on Windoze.
228    * As we invert them below, they will be clear in the final xor_mask.
229    */
230   for (iy = 0; iy < height; iy++)
231     {
232       p = source_bits + iy*source_bpl;
233       q = mask_bits + iy*mask_bpl;
234       
235       for (ix = 0; ix < ((width-1)/8+1); ix++)
236         if (bg_is_white)
237           *p++ |= ~(*q++);
238         else
239           *p++ &= *q++;
240     }
241
242   /* XOR mask is initialized to zero */
243   xor_mask = g_malloc0 (cursor_width/8 * cursor_height);
244
245   for (iy = 0; iy < height; iy++)
246     {
247       p = source_bits + iy*source_bpl;
248       q = xor_mask + iy*cursor_width/8;
249
250       for (ix = 0; ix < ((width-1)/8+1); ix++)
251         if (bg_is_white)
252           *q++ = ~(*p++);
253         else
254           *q++ = *p++;
255
256       q[-1] &= ~residue;        /* Clear left-over bits */
257     }
258       
259   /* AND mask is initialized to ones */
260   and_mask = g_malloc (cursor_width/8 * cursor_height);
261   memset (and_mask, 0xFF, cursor_width/8 * cursor_height);
262
263   for (iy = 0; iy < height; iy++)
264     {
265       p = mask_bits + iy*mask_bpl;
266       q = and_mask + iy*cursor_width/8;
267
268       for (ix = 0; ix < ((width-1)/8+1); ix++)
269         *q++ = ~(*p++);
270
271       q[-1] |= residue; /* Set left-over bits */
272     }
273       
274   hcursor = CreateCursor (_gdk_app_hmodule, x, y, cursor_width, cursor_height,
275                           and_mask, xor_mask);
276
277   GDK_NOTE (MISC, g_print ("gdk_cursor_new_from_pixmap: "
278                            "%p (%dx%d) %p (%dx%d) = %p (%dx%d)\n",
279                            GDK_PIXMAP_HBITMAP (source),
280                            source_impl->width, source_impl->height,
281                            GDK_PIXMAP_HBITMAP (mask),
282                            mask_impl->width, mask_impl->height,
283                            hcursor, cursor_width, cursor_height));
284
285   g_free (xor_mask);
286   g_free (and_mask);
287
288   private = g_new (GdkCursorPrivate, 1);
289   private->hcursor = hcursor;
290   cursor = (GdkCursor*) private;
291   cursor->type = GDK_CURSOR_IS_PIXMAP;
292   cursor->ref_count = 1;
293   
294   return cursor;
295 }
296
297 void
298 _gdk_cursor_destroy (GdkCursor *cursor)
299 {
300   GdkCursorPrivate *private;
301
302   g_return_if_fail (cursor != NULL);
303   private = (GdkCursorPrivate *) cursor;
304
305   GDK_NOTE (MISC, g_print ("_gdk_cursor_destroy: %p\n",
306                            (cursor->type == GDK_CURSOR_IS_PIXMAP) ? private->hcursor : 0));
307
308   if (GetCursor () == private->hcursor)
309     SetCursor (NULL);
310
311   if (!DestroyCursor (private->hcursor))
312     WIN32_API_FAILED ("DestroyCursor");
313
314   g_free (private);
315 }
316
317 GdkDisplay *
318 gdk_cursor_get_display (GdkCursor *cursor)
319 {
320   return gdk_display_get_default ();
321 }
322
323 GdkCursor *
324 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
325                             GdkPixbuf  *pixbuf,
326                             gint        x,
327                             gint        y)
328 {
329   /* FIXME: Use alpha if supported */
330
331   GdkCursor *cursor;
332   GdkPixmap *pixmap, *mask;
333   guint width, height, n_channels, rowstride, i, j;
334   guint8 *data, *mask_data, *pixels;
335   GdkColor fg = { 0, 0, 0, 0 };
336   GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
337   GdkScreen *screen;
338
339   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
340   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
341
342   width = gdk_pixbuf_get_width (pixbuf);
343   height = gdk_pixbuf_get_height (pixbuf);
344
345   g_return_val_if_fail (0 <= x && x < width, NULL);
346   g_return_val_if_fail (0 <= y && y < height, NULL);
347
348   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
349   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
350   pixels = gdk_pixbuf_get_pixels (pixbuf);
351
352   data = g_new0 (guint8, (width + 7) / 8 * height);
353   mask_data = g_new0 (guint8, (width + 7) / 8 * height);
354
355   for (j = 0; j < height; j++)
356     {
357       guint8 *src = pixels + j * rowstride;
358       guint8 *d = data + (width + 7) / 8 * j;
359       guint8 *md = mask_data + (width + 7) / 8 * j;
360         
361       for (i = 0; i < width; i++)
362         {
363           if (src[1] < 0x80)
364             *d |= 1 << (i % 8);
365           
366           if (n_channels == 3 || src[3] >= 0x80)
367             *md |= 1 << (i % 8);
368           
369           src += n_channels;
370           if (i % 8 == 7)
371             {
372               d++; 
373               md++;
374             }
375         }
376     }
377       
378   screen = gdk_display_get_default_screen (display);
379   pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen), 
380                                         data, width, height);
381  
382   mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
383                                       mask_data, width, height);
384    
385   cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
386    
387   g_object_unref (pixmap);
388   g_object_unref (mask);
389
390   g_free (data);
391   g_free (mask_data);
392   
393   return cursor;
394 }
395
396 gboolean 
397 gdk_display_supports_cursor_alpha (GdkDisplay    *display)
398 {
399   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
400
401   /* FIXME */
402   return FALSE;
403 }
404
405 gboolean 
406 gdk_display_supports_cursor_color (GdkDisplay    *display)
407 {
408   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
409
410   /* FIXME */
411   return FALSE;
412 }
413
414 guint     
415 gdk_display_get_default_cursor_size (GdkDisplay    *display)
416 {
417   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
418   
419   return MIN (GetSystemMetrics (SM_CXCURSOR), GetSystemMetrics (SM_CYCURSOR));
420 }
421
422 void     
423 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
424                                      guint       *width,
425                                      guint       *height)
426 {
427   g_return_if_fail (GDK_IS_DISPLAY (display));
428   
429   if (width)
430     *width = GetSystemMetrics (SM_CXCURSOR);
431   if (height)
432     *height = GetSystemMetrics (SM_CYCURSOR);
433 }