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