]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkcursor-win32.c
313b0d8a4f836cdb712748c6579f01e57171506f
[~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 #define GDK_PIXBUF_ENABLE_BACKEND /* Ugly? */
23 #include "gdkdisplay.h"
24 #include "gdkscreen.h"
25 #include "gdkcursor.h"
26 #include "gdkprivate-win32.h"
27 #include "gdkwin32cursor.h"
28
29 #ifdef __MINGW32__
30 #include <w32api.h>
31 #endif
32
33 #include "xcursors.h"
34
35 #if (defined(__MINGW32__) && (__W32API_MAJOR_VERSION < 3 || (__W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION < 8))) || (defined(_MSC_VER) && (WINVER < 0x0500))
36 typedef struct { 
37   DWORD        bV5Size; 
38   LONG         bV5Width; 
39   LONG         bV5Height; 
40   WORD         bV5Planes; 
41   WORD         bV5BitCount; 
42   DWORD        bV5Compression; 
43   DWORD        bV5SizeImage; 
44   LONG         bV5XPelsPerMeter; 
45   LONG         bV5YPelsPerMeter; 
46   DWORD        bV5ClrUsed; 
47   DWORD        bV5ClrImportant; 
48   DWORD        bV5RedMask; 
49   DWORD        bV5GreenMask; 
50   DWORD        bV5BlueMask; 
51   DWORD        bV5AlphaMask; 
52   DWORD        bV5CSType; 
53   CIEXYZTRIPLE bV5Endpoints; 
54   DWORD        bV5GammaRed; 
55   DWORD        bV5GammaGreen; 
56   DWORD        bV5GammaBlue; 
57   DWORD        bV5Intent; 
58   DWORD        bV5ProfileData; 
59   DWORD        bV5ProfileSize; 
60   DWORD        bV5Reserved; 
61 } BITMAPV5HEADER;
62 #endif
63
64 static HCURSOR
65 hcursor_from_type (GdkCursorType cursor_type)
66 {
67   gint i, j, x, y, ofs;
68   HCURSOR rv;
69   gint w, h;
70   guchar *and_plane, *xor_plane;
71
72   if (cursor_type != GDK_BLANK_CURSOR)
73     {
74       for (i = 0; i < G_N_ELEMENTS (cursors); i++)
75         if (cursors[i].type == cursor_type)
76           break;
77
78       if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name)
79         return NULL;
80
81       /* Use real Win32 cursor if possible */
82       if (cursors[i].builtin)
83         return LoadCursor (NULL, cursors[i].builtin);
84     }
85   
86   w = GetSystemMetrics (SM_CXCURSOR);
87   h = GetSystemMetrics (SM_CYCURSOR);
88
89   and_plane = g_malloc ((w/8) * h);
90   memset (and_plane, 0xff, (w/8) * h);
91   xor_plane = g_malloc ((w/8) * h);
92   memset (xor_plane, 0, (w/8) * h);
93
94   if (cursor_type != GDK_BLANK_CURSOR)
95     {
96
97 #define SET_BIT(v,b)  (v |= (1 << b))
98 #define RESET_BIT(v,b)  (v &= ~(1 << b))
99
100       for (j = 0, y = 0; y < cursors[i].height && y < h ; y++)
101         {
102           ofs = (y * w) / 8;
103           j = y * cursors[i].width;
104           
105           for (x = 0; x < cursors[i].width && x < w ; x++, j++)
106             {
107               gint pofs = ofs + x / 8;
108               guchar data = (cursors[i].data[j/4] & (0xc0 >> (2 * (j%4)))) >> (2 * (3 - (j%4)));
109               gint bit = 7 - (j % cursors[i].width) % 8;
110               
111               if (data)
112                 {
113                   RESET_BIT (and_plane[pofs], bit);
114                   if (data == 1)
115                     SET_BIT (xor_plane[pofs], bit);
116                 }
117             }
118         }
119
120 #undef SET_BIT
121 #undef RESET_BIT
122
123       rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
124                          w, h, and_plane, xor_plane);
125     }
126   else
127     {
128       rv = CreateCursor (_gdk_app_hmodule, 0, 0,
129                          w, h, and_plane, xor_plane);
130     }
131   if (rv == NULL)
132     WIN32_API_FAILED ("CreateCursor");
133   g_free (and_plane);
134   g_free (xor_plane);
135   
136   return rv;
137 }
138
139 struct _GdkWin32CursorClass
140 {
141   GdkCursorClass cursor_class;
142 };
143
144 G_DEFINE_TYPE (GdkWin32Cursor, gdk_win32_cursor, GDK_TYPE_CURSOR)
145
146 static void
147 _gdk_win32_cursor_finalize (GObject *object)
148 {
149   GdkWin32Cursor *private = GDK_WIN32_CURSOR (object);
150
151   if (GetCursor () == private->hcursor)
152     SetCursor (NULL);
153
154   if (!DestroyCursor (private->hcursor))
155     WIN32_API_FAILED ("DestroyCursor");
156
157   G_OBJECT_CLASS (gdk_win32_cursor_parent_class)->finalize (object);
158 }
159
160 static GdkCursor*
161 cursor_new_from_hcursor (HCURSOR       hcursor,
162                          GdkCursorType cursor_type)
163 {
164   GdkWin32Cursor *private;
165   GdkCursor *cursor;
166
167   private = g_object_new (GDK_TYPE_WIN32_CURSOR,
168                           "cursor-type", cursor_type,
169                           "display", _gdk_display,
170                           NULL);
171   private->hcursor = hcursor;
172   cursor = (GdkCursor*) private;
173
174   return cursor;
175 }
176
177 GdkCursor*
178 _gdk_win32_display_get_cursor_for_type (GdkDisplay   *display,
179                                         GdkCursorType cursor_type)
180 {
181   HCURSOR hcursor;
182
183   g_return_val_if_fail (display == _gdk_display, NULL);
184
185   hcursor = hcursor_from_type (cursor_type);
186
187   if (hcursor == NULL)
188     g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type);
189   else
190     GDK_NOTE (CURSOR, g_print ("gdk_cursor_new_for_display: %d: %p\n",
191                                cursor_type, hcursor));
192
193   return cursor_new_from_hcursor (hcursor, cursor_type);
194 }
195
196 /* FIXME: The named cursors below are presumably not really useful, as
197  * the names are Win32-specific. No GTK+ application developed on Unix
198  * (and most cross-platform GTK+ apps are developed on Unix) is going
199  * to look for cursors under these Win32 names anyway.
200  *
201  * Would the following make any sense: The ms-windows theme engine
202  * calls some (to-be-defined private) API here in gdk/win32 to
203  * register the relevant cursors used by the currently active XP
204  * visual style under the names that libgtk uses to look for them
205  * ("color-picker", "dnd-ask", "dnd-copy", etc), and then when libgtk
206  * asks for those we return the ones registered by the ms-windows
207  * theme engine, if any.
208  */
209
210 static struct {
211   char *name;
212   char *id;
213 } default_cursors[] = {
214   { "appstarting", IDC_APPSTARTING },
215   { "arrow", IDC_ARROW },
216   { "cross", IDC_CROSS },
217 #ifdef IDC_HAND
218   { "hand",  IDC_HAND },
219 #endif
220   { "help",  IDC_HELP },
221   { "ibeam", IDC_IBEAM },
222   { "sizeall", IDC_SIZEALL },
223   { "sizenesw", IDC_SIZENESW },
224   { "sizens", IDC_SIZENS },
225   { "sizenwse", IDC_SIZENWSE },
226   { "sizewe", IDC_SIZEWE },
227   { "uparrow", IDC_UPARROW },
228   { "wait", IDC_WAIT }
229 };
230
231 GdkCursor*  
232 _gdk_win32_display_get_cursor_for_name (GdkDisplay  *display,
233                                         const gchar *name)
234 {
235   HCURSOR hcursor = NULL;
236   int i;
237
238   g_return_val_if_fail (display == _gdk_display, NULL);
239
240   for (i = 0; i < G_N_ELEMENTS(default_cursors); i++)
241     {
242       if (0 == strcmp(default_cursors[i].name, name))
243         hcursor = LoadCursor (NULL, default_cursors[i].id);
244     }
245   /* allow to load named cursor resources linked into the executable */
246   if (!hcursor)
247     hcursor = LoadCursor (_gdk_app_hmodule, name);
248
249   if (hcursor)
250     return cursor_new_from_hcursor (hcursor, GDK_X_CURSOR);
251
252   return NULL;
253 }
254
255 GdkPixbuf *
256 gdk_win32_icon_to_pixbuf_libgtk_only (HICON hicon)
257 {
258   GdkPixbuf *pixbuf = NULL;
259   ICONINFO ii;
260   struct
261   {
262     BITMAPINFOHEADER bi;
263     RGBQUAD colors[2];
264   } bmi;
265   HDC hdc;
266   guchar *pixels, *bits;
267   gchar buf[32];
268   gint rowstride, x, y, w, h;
269
270   if (!GDI_CALL (GetIconInfo, (hicon, &ii)))
271     return NULL;
272
273   if (!(hdc = CreateCompatibleDC (NULL)))
274     {
275       WIN32_GDI_FAILED ("CreateCompatibleDC");
276       goto out0;
277     }
278
279   memset (&bmi, 0, sizeof (bmi));
280   bmi.bi.biSize = sizeof (bmi.bi);
281
282   if (ii.hbmColor != NULL)
283     {
284       /* Colour cursor */
285
286       gboolean no_alpha;
287       
288       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, 1, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
289         goto out1;
290
291       w = bmi.bi.biWidth;
292       h = bmi.bi.biHeight;
293
294       bmi.bi.biBitCount = 32;
295       bmi.bi.biCompression = BI_RGB;
296       bmi.bi.biHeight = -h;
297
298       bits = g_malloc0 (4 * w * h);
299       
300       /* color data */
301       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
302         goto out2;
303
304       pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
305       pixels = gdk_pixbuf_get_pixels (pixbuf);
306       rowstride = gdk_pixbuf_get_rowstride (pixbuf);
307       no_alpha = TRUE;
308       for (y = 0; y < h; y++)
309         {
310           for (x = 0; x < w; x++)
311             {
312               pixels[2] = bits[(x+y*w) * 4];
313               pixels[1] = bits[(x+y*w) * 4 + 1];
314               pixels[0] = bits[(x+y*w) * 4 + 2];
315               pixels[3] = bits[(x+y*w) * 4 + 3];
316               if (no_alpha && pixels[3] > 0)
317                 no_alpha = FALSE;
318               pixels += 4;
319             }
320           pixels += (w * 4 - rowstride);
321         }
322
323       /* mask */
324       if (no_alpha &&
325           GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
326         {
327           pixels = gdk_pixbuf_get_pixels (pixbuf);
328           for (y = 0; y < h; y++)
329             {
330               for (x = 0; x < w; x++)
331                 {
332                   pixels[3] = 255 - bits[(x + y * w) * 4];
333                   pixels += 4;
334                 }
335               pixels += (w * 4 - rowstride);
336             }
337         }
338     }
339   else
340     {
341       /* B&W cursor */
342
343       int bpl;
344
345       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, 0, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
346         goto out1;
347
348       w = bmi.bi.biWidth;
349       h = ABS (bmi.bi.biHeight) / 2;
350       
351       bits = g_malloc0 (4 * w * h);
352       
353       /* masks */
354       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h*2, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
355         goto out2;
356
357       pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
358       pixels = gdk_pixbuf_get_pixels (pixbuf);
359       rowstride = gdk_pixbuf_get_rowstride (pixbuf);
360       bpl = ((w-1)/32 + 1)*4;
361 #if 0
362       for (y = 0; y < h*2; y++)
363         {
364           for (x = 0; x < w; x++)
365             {
366               const gint bit = 7 - (x % 8);
367               printf ("%c ", ((bits[bpl*y+x/8])&(1<<bit)) ? ' ' : 'X');
368             }
369           printf ("\n");
370         }
371 #endif
372
373       for (y = 0; y < h; y++)
374         {
375           const guchar *andp, *xorp;
376           if (bmi.bi.biHeight < 0)
377             {
378               andp = bits + bpl*y;
379               xorp = bits + bpl*(h+y);
380             }
381           else
382             {
383               andp = bits + bpl*(h-y-1);
384               xorp = bits + bpl*(h+h-y-1);
385             }
386           for (x = 0; x < w; x++)
387             {
388               const gint bit = 7 - (x % 8);
389               if ((*andp) & (1<<bit))
390                 {
391                   if ((*xorp) & (1<<bit))
392                     pixels[2] = pixels[1] = pixels[0] = 0xFF;
393                   else
394                     pixels[2] = pixels[1] = pixels[0] = 0;
395                   pixels[3] = 0xFF;
396                 }
397               else
398                 {
399                   pixels[2] = pixels[1] = pixels[0] = 0;
400                   pixels[3] = 0;
401                 }
402               pixels += 4;
403               if (bit == 0)
404                 {
405                   andp++;
406                   xorp++;
407                 }
408             }
409           pixels += (w * 4 - rowstride);
410         }
411     }
412
413   g_snprintf (buf, sizeof (buf), "%ld", ii.xHotspot);
414   gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
415
416   g_snprintf (buf, sizeof (buf), "%ld", ii.yHotspot);
417   gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
418
419   /* release temporary resources */
420  out2:
421   g_free (bits);
422  out1:
423   DeleteDC (hdc);
424  out0:
425   DeleteObject (ii.hbmColor);
426   DeleteObject (ii.hbmMask);
427
428   return pixbuf;
429 }
430
431 static GdkPixbuf *
432 _gdk_win32_cursor_get_image (GdkCursor *cursor)
433 {
434   g_return_val_if_fail (cursor != NULL, NULL);
435
436   return gdk_win32_icon_to_pixbuf_libgtk_only (((GdkWin32Cursor *) cursor)->hcursor);
437 }
438
439 GdkCursor *
440 _gdk_win32_display_get_cursor_for_pixbuf (GdkDisplay *display, 
441                                           GdkPixbuf  *pixbuf,
442                                           gint        x,
443                                           gint        y)
444 {
445   HCURSOR hcursor;
446
447   g_return_val_if_fail (display == _gdk_display, NULL);
448   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
449   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
450   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
451
452   hcursor = _gdk_win32_pixbuf_to_hcursor (pixbuf, x, y);
453   if (!hcursor)
454     return NULL;
455   return cursor_new_from_hcursor (hcursor, GDK_CURSOR_IS_PIXMAP);
456 }
457
458 gboolean
459 _gdk_win32_display_supports_cursor_alpha (GdkDisplay    *display)
460 {
461   g_return_val_if_fail (display == _gdk_display, FALSE);
462
463   return _gdk_win32_pixbuf_to_hicon_supports_alpha ();
464 }
465
466 gboolean 
467 _gdk_win32_display_supports_cursor_color (GdkDisplay    *display)
468 {
469   g_return_val_if_fail (display == _gdk_display, FALSE);
470
471   return TRUE;
472 }
473
474 void
475 _gdk_win32_display_get_default_cursor_size (GdkDisplay *display,
476                                             guint       *width,
477                                             guint       *height)
478 {
479   g_return_if_fail (display == _gdk_display);
480
481   if (width)
482     *width = GetSystemMetrics (SM_CXCURSOR);
483   if (height)
484     *height = GetSystemMetrics (SM_CYCURSOR);
485 }
486
487 void     
488 _gdk_win32_display_get_maximal_cursor_size (GdkDisplay *display,
489                                             guint       *width,
490                                             guint       *height)
491 {
492   g_return_if_fail (display == _gdk_display);
493   
494   if (width)
495     *width = GetSystemMetrics (SM_CXCURSOR);
496   if (height)
497     *height = GetSystemMetrics (SM_CYCURSOR);
498 }
499
500
501 /* Convert a pixbuf to an HICON (or HCURSOR).  Supports alpha under
502  * Windows XP, thresholds alpha otherwise.  Also used from
503  * gdkwindow-win32.c for creating application icons.
504  */
505
506 static HBITMAP
507 create_alpha_bitmap (gint     size,
508                      guchar **outdata)
509 {
510   BITMAPV5HEADER bi;
511   HDC hdc;
512   HBITMAP hBitmap;
513
514   ZeroMemory (&bi, sizeof (BITMAPV5HEADER));
515   bi.bV5Size = sizeof (BITMAPV5HEADER);
516   bi.bV5Height = bi.bV5Width = size;
517   bi.bV5Planes = 1;
518   bi.bV5BitCount = 32;
519   bi.bV5Compression = BI_BITFIELDS;
520   /* The following mask specification specifies a supported 32 BPP
521    * alpha format for Windows XP (BGRA format).
522    */
523   bi.bV5RedMask   = 0x00FF0000;
524   bi.bV5GreenMask = 0x0000FF00;
525   bi.bV5BlueMask  = 0x000000FF;
526   bi.bV5AlphaMask = 0xFF000000;
527
528   /* Create the DIB section with an alpha channel. */
529   hdc = GetDC (NULL);
530   if (!hdc)
531     {
532       WIN32_GDI_FAILED ("GetDC");
533       return NULL;
534     }
535   hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
536                               (PVOID *) outdata, NULL, (DWORD)0);
537   if (hBitmap == NULL)
538     WIN32_GDI_FAILED ("CreateDIBSection");
539   ReleaseDC (NULL, hdc);
540
541   return hBitmap;
542 }
543
544 static HBITMAP
545 create_color_bitmap (gint     size,
546                      guchar **outdata,
547                      gint     bits)
548 {
549   struct {
550     BITMAPV4HEADER bmiHeader;
551     RGBQUAD bmiColors[2];
552   } bmi;
553   HDC hdc;
554   HBITMAP hBitmap;
555
556   ZeroMemory (&bmi, sizeof (bmi));
557   bmi.bmiHeader.bV4Size = sizeof (BITMAPV4HEADER);
558   bmi.bmiHeader.bV4Height = bmi.bmiHeader.bV4Width = size;
559   bmi.bmiHeader.bV4Planes = 1;
560   bmi.bmiHeader.bV4BitCount = bits;
561   bmi.bmiHeader.bV4V4Compression = BI_RGB;
562
563   /* when bits is 1, these will be used.
564    * bmiColors[0] already zeroed from ZeroMemory()
565    */
566   bmi.bmiColors[1].rgbBlue = 0xFF;
567   bmi.bmiColors[1].rgbGreen = 0xFF;
568   bmi.bmiColors[1].rgbRed = 0xFF;
569
570   hdc = GetDC (NULL);
571   if (!hdc)
572     {
573       WIN32_GDI_FAILED ("GetDC");
574       return NULL;
575     }
576   hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bmi, DIB_RGB_COLORS,
577                               (PVOID *) outdata, NULL, (DWORD)0);
578   if (hBitmap == NULL)
579     WIN32_GDI_FAILED ("CreateDIBSection");
580   ReleaseDC (NULL, hdc);
581
582   return hBitmap;
583 }
584
585 static gboolean
586 pixbuf_to_hbitmaps_alpha_winxp (GdkPixbuf *pixbuf,
587                                 HBITMAP   *color,
588                                 HBITMAP   *mask)
589 {
590   /* Based on code from
591    * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
592    */
593   HBITMAP hColorBitmap, hMaskBitmap;
594   guchar *indata, *inrow;
595   guchar *colordata, *colorrow, *maskdata, *maskbyte;
596   gint width, height, size, i, i_offset, j, j_offset, rowstride;
597   guint maskstride, mask_bit;
598
599   width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
600   height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
601
602   /* The bitmaps are created square */
603   size = MAX (width, height);
604
605   hColorBitmap = create_alpha_bitmap (size, &colordata);
606   if (!hColorBitmap)
607     return FALSE;
608   hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
609   if (!hMaskBitmap)
610     {
611       DeleteObject (hColorBitmap);
612       return FALSE;
613     }
614
615   /* MSDN says mask rows are aligned to "LONG" boundaries */
616   maskstride = (((size + 31) & ~31) >> 3);
617
618   indata = gdk_pixbuf_get_pixels (pixbuf);
619   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
620
621   if (width > height)
622     {
623       i_offset = 0;
624       j_offset = (width - height) / 2;
625     }
626   else
627     {
628       i_offset = (height - width) / 2;
629       j_offset = 0;
630     }
631
632   for (j = 0; j < height; j++)
633     {
634       colorrow = colordata + 4*(j+j_offset)*size + 4*i_offset;
635       maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
636       mask_bit = (0x80 >> (i_offset % 8));
637       inrow = indata + (height-j-1)*rowstride;
638       for (i = 0; i < width; i++)
639         {
640           colorrow[4*i+0] = inrow[4*i+2];
641           colorrow[4*i+1] = inrow[4*i+1];
642           colorrow[4*i+2] = inrow[4*i+0];
643           colorrow[4*i+3] = inrow[4*i+3];
644           if (inrow[4*i+3] == 0)
645             maskbyte[0] |= mask_bit;    /* turn ON bit */
646           else
647             maskbyte[0] &= ~mask_bit;   /* turn OFF bit */
648           mask_bit >>= 1;
649           if (mask_bit == 0)
650             {
651               mask_bit = 0x80;
652               maskbyte++;
653             }
654         }
655     }
656
657   *color = hColorBitmap;
658   *mask = hMaskBitmap;
659
660   return TRUE;
661 }
662
663 static gboolean
664 pixbuf_to_hbitmaps_normal (GdkPixbuf *pixbuf,
665                            HBITMAP   *color,
666                            HBITMAP   *mask)
667 {
668   /* Based on code from
669    * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
670    */
671   HBITMAP hColorBitmap, hMaskBitmap;
672   guchar *indata, *inrow;
673   guchar *colordata, *colorrow, *maskdata, *maskbyte;
674   gint width, height, size, i, i_offset, j, j_offset, rowstride, nc, bmstride;
675   gboolean has_alpha;
676   guint maskstride, mask_bit;
677
678   width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
679   height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
680
681   /* The bitmaps are created square */
682   size = MAX (width, height);
683
684   hColorBitmap = create_color_bitmap (size, &colordata, 24);
685   if (!hColorBitmap)
686     return FALSE;
687   hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
688   if (!hMaskBitmap)
689     {
690       DeleteObject (hColorBitmap);
691       return FALSE;
692     }
693
694   /* rows are always aligned on 4-byte boundarys */
695   bmstride = size * 3;
696   if (bmstride % 4 != 0)
697     bmstride += 4 - (bmstride % 4);
698
699   /* MSDN says mask rows are aligned to "LONG" boundaries */
700   maskstride = (((size + 31) & ~31) >> 3);
701
702   indata = gdk_pixbuf_get_pixels (pixbuf);
703   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
704   nc = gdk_pixbuf_get_n_channels (pixbuf);
705   has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
706
707   if (width > height)
708     {
709       i_offset = 0;
710       j_offset = (width - height) / 2;
711     }
712   else
713     {
714       i_offset = (height - width) / 2;
715       j_offset = 0;
716     }
717
718   for (j = 0; j < height; j++)
719     {
720       colorrow = colordata + (j+j_offset)*bmstride + 3*i_offset;
721       maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
722       mask_bit = (0x80 >> (i_offset % 8));
723       inrow = indata + (height-j-1)*rowstride;
724       for (i = 0; i < width; i++)
725         {
726           if (has_alpha && inrow[nc*i+3] < 128)
727             {
728               colorrow[3*i+0] = colorrow[3*i+1] = colorrow[3*i+2] = 0;
729               maskbyte[0] |= mask_bit;  /* turn ON bit */
730             }
731           else
732             {
733               colorrow[3*i+0] = inrow[nc*i+2];
734               colorrow[3*i+1] = inrow[nc*i+1];
735               colorrow[3*i+2] = inrow[nc*i+0];
736               maskbyte[0] &= ~mask_bit; /* turn OFF bit */
737             }
738           mask_bit >>= 1;
739           if (mask_bit == 0)
740             {
741               mask_bit = 0x80;
742               maskbyte++;
743             }
744         }
745     }
746
747   *color = hColorBitmap;
748   *mask = hMaskBitmap;
749
750   return TRUE;
751 }
752
753 static HICON
754 pixbuf_to_hicon (GdkPixbuf *pixbuf,
755                  gboolean   is_icon,
756                  gint       x,
757                  gint       y)
758 {
759   ICONINFO ii;
760   HICON icon;
761   gboolean success;
762
763   if (pixbuf == NULL)
764     return NULL;
765
766   if (_gdk_win32_pixbuf_to_hicon_supports_alpha() && gdk_pixbuf_get_has_alpha (pixbuf))
767     success = pixbuf_to_hbitmaps_alpha_winxp (pixbuf, &ii.hbmColor, &ii.hbmMask);
768   else
769     success = pixbuf_to_hbitmaps_normal (pixbuf, &ii.hbmColor, &ii.hbmMask);
770
771   if (!success)
772     return NULL;
773
774   ii.fIcon = is_icon;
775   ii.xHotspot = x;
776   ii.yHotspot = y;
777   icon = CreateIconIndirect (&ii);
778   DeleteObject (ii.hbmColor);
779   DeleteObject (ii.hbmMask);
780   return icon;
781 }
782
783 HICON
784 _gdk_win32_pixbuf_to_hicon (GdkPixbuf *pixbuf)
785 {
786   return pixbuf_to_hicon (pixbuf, TRUE, 0, 0);
787 }
788
789 HICON
790 _gdk_win32_pixbuf_to_hcursor (GdkPixbuf *pixbuf,
791                               gint       x_hotspot,
792                               gint       y_hotspot)
793 {
794   return pixbuf_to_hicon (pixbuf, FALSE, x_hotspot, y_hotspot);
795 }
796
797 gboolean
798 _gdk_win32_pixbuf_to_hicon_supports_alpha (void)
799 {
800   static gboolean is_win_xp=FALSE, is_win_xp_checked=FALSE;
801
802   if (!is_win_xp_checked)
803     {
804       OSVERSIONINFO version;
805
806       is_win_xp_checked = TRUE;
807
808       memset (&version, 0, sizeof (version));
809       version.dwOSVersionInfoSize = sizeof (version);
810       is_win_xp = GetVersionEx (&version)
811         && version.dwPlatformId == VER_PLATFORM_WIN32_NT
812         && (version.dwMajorVersion > 5
813             || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
814     }
815   return is_win_xp;
816 }
817
818 HICON
819 gdk_win32_pixbuf_to_hicon_libgtk_only (GdkPixbuf *pixbuf)
820 {
821   return _gdk_win32_pixbuf_to_hicon (pixbuf);
822 }
823
824 static void
825 gdk_win32_cursor_init (GdkWin32Cursor *cursor)
826 {
827 }
828 static void
829 gdk_win32_cursor_class_init(GdkWin32CursorClass *klass)
830 {
831   GObjectClass *object_class = G_OBJECT_CLASS (klass);
832   GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (klass);
833
834   object_class->finalize = _gdk_win32_cursor_finalize;
835   
836   cursor_class->get_image = _gdk_win32_cursor_get_image;
837 }