]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkcursor-win32.c
28151f93c32698bdae958601673d8141b20cf31f
[~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_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 _gdk_win32_data_to_wcursor (GdkCursorType cursor_type)
65 {
66   gint i, j, x, y, ofs;
67   HCURSOR rv = NULL;
68   gint w, h;
69   guchar *and_plane, *xor_plane;
70
71   for (i = 0; i < G_N_ELEMENTS (cursors); i++)
72     if (cursors[i].type == cursor_type)
73       break;
74
75   if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name)
76     return NULL;
77
78   w = GetSystemMetrics (SM_CXCURSOR);
79   h = GetSystemMetrics (SM_CYCURSOR);
80
81   and_plane = g_malloc ((w/8) * h);
82   memset (and_plane, 0xff, (w/8) * h);
83   xor_plane = g_malloc ((w/8) * h);
84   memset (xor_plane, 0, (w/8) * h);
85
86 #define SET_BIT(v,b)  (v |= (1 << b))
87 #define RESET_BIT(v,b)  (v &= ~(1 << b))
88
89   for (j = 0, y = 0; y < cursors[i].height && y < h ; y++)
90     {
91       ofs = (y * w) / 8;
92       j = y * cursors[i].width;
93
94       for (x = 0; x < cursors[i].width && x < w ; x++, j++)
95       {
96         gint pofs = ofs + x / 8;
97         guchar data = (cursors[i].data[j/4] & (0xc0 >> (2 * (j%4)))) >> (2 * (3 - (j%4)));
98         gint bit = 7 - (j % cursors[i].width) % 8;
99
100         if (data)
101           {
102             RESET_BIT (and_plane[pofs], bit);
103             if (data == 1)
104               SET_BIT (xor_plane[pofs], bit);
105           }
106       }
107     }
108
109 #undef SET_BIT
110 #undef RESET_BIT
111
112   rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
113                      w, h, and_plane, xor_plane);
114   if (rv == NULL)
115     WIN32_API_FAILED ("CreateCursor");
116   g_free (and_plane);
117   g_free (xor_plane);
118   
119   return rv;
120 }
121
122 static GdkCursor*
123 _gdk_win32_cursor_new_from_hcursor (HCURSOR hcursor, GdkCursorType cursor_type)
124 {
125   GdkCursorPrivate *private;
126   GdkCursor *cursor;
127
128   private = g_new (GdkCursorPrivate, 1);
129   private->hcursor = hcursor;
130   cursor = (GdkCursor*) private;
131   cursor->type = cursor_type;
132   cursor->ref_count = 1;
133
134   return cursor;
135 }
136
137 GdkCursor*
138 gdk_cursor_new_for_display (GdkDisplay   *display,
139                             GdkCursorType cursor_type)
140 {
141   HCURSOR hcursor;
142
143   g_return_val_if_fail (display == _gdk_display, NULL);
144
145   hcursor = _gdk_win32_data_to_wcursor (cursor_type);
146
147   if (hcursor == NULL)
148     g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type);
149   else
150     GDK_NOTE (MISC, g_print ("gdk_cursor_new_for_display: %d: %p\n",
151                              cursor_type, hcursor));
152
153   return _gdk_win32_cursor_new_from_hcursor (hcursor, cursor_type);
154 }
155
156 static gboolean
157 color_is_white (const GdkColor *color)
158 {
159   return (color->red == 0xFFFF
160           && color->green == 0xFFFF
161           && color->blue == 0xFFFF);
162 }
163
164 GdkCursor*
165 gdk_cursor_new_from_pixmap (GdkPixmap      *source,
166                             GdkPixmap      *mask,
167                             const GdkColor *fg,
168                             const GdkColor *bg,
169                             gint            x,
170                             gint            y)
171 {
172   GdkPixmapImplWin32 *source_impl, *mask_impl;
173   guchar *source_bits, *mask_bits;
174   gint source_bpl, mask_bpl;
175   HCURSOR hcursor;
176   guchar *p, *q, *xor_mask, *and_mask;
177   gint width, height, cursor_width, cursor_height;
178   guchar residue;
179   gint ix, iy;
180   const gboolean bg_is_white = color_is_white (bg);
181   
182   g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
183   g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
184   g_return_val_if_fail (fg != NULL, NULL);
185   g_return_val_if_fail (bg != NULL, NULL);
186
187   source_impl = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (source)->impl);
188   mask_impl = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (mask)->impl);
189
190   g_return_val_if_fail (source_impl->width == mask_impl->width
191                         && source_impl->height == mask_impl->height,
192                         NULL);
193   width = source_impl->width;
194   height = source_impl->height;
195   cursor_width = GetSystemMetrics (SM_CXCURSOR);
196   cursor_height = GetSystemMetrics (SM_CYCURSOR);
197
198   g_return_val_if_fail (width <= cursor_width && height <= cursor_height,
199                         NULL);
200
201   residue = (1 << ((8-(width%8))%8)) - 1;
202
203   source_bits = source_impl->bits;
204   mask_bits = mask_impl->bits;
205
206   g_return_val_if_fail (GDK_PIXMAP_OBJECT (source)->depth == 1
207                         && GDK_PIXMAP_OBJECT (mask)->depth == 1,
208                         NULL);
209
210   source_bpl = ((width - 1)/32 + 1)*4;
211   mask_bpl = ((mask_impl->width - 1)/32 + 1)*4;
212
213 #ifdef G_ENABLE_DEBUG
214   if (_gdk_debug_flags & GDK_DEBUG_CURSOR)
215     {
216       g_print ("gdk_cursor_new_from_pixmap: source=%p:\n",
217                source_impl->parent_instance.handle);
218       for (iy = 0; iy < height; iy++)
219         {
220           if (iy == 16)
221             break;
222
223           p = source_bits + iy*source_bpl;
224           for (ix = 0; ix < width; ix++)
225             {
226               if (ix == 79)
227                 break;
228               g_print ("%c", ".X"[((*p)>>(7-(ix%8)))&1]);
229               if ((ix%8) == 7)
230                 p++;
231             }
232           g_print ("\n");
233         }
234       g_print ("...mask=%p:\n", mask_impl->parent_instance.handle);
235       for (iy = 0; iy < height; iy++)
236         {
237           if (iy == 16)
238             break;
239
240           p = mask_bits + iy*source_bpl;
241           for (ix = 0; ix < width; ix++)
242             {
243               if (ix == 79)
244                 break;
245               g_print ("%c", ".X"[((*p)>>(7-(ix%8)))&1]);
246               if ((ix%8) == 7)
247                 p++;
248             }
249           g_print ("\n");
250         }
251     }
252 #endif
253
254   /* Such complex bit manipulation for this simple task, sigh.
255    * The X cursor and Windows cursor concepts are quite different.
256    * We assume here that we are always called with fg == black and
257    * bg == white, *or* the other way around. Random colours won't work.
258    * (Well, you will get a cursor, but not in those colours.)
259    */
260
261   /* Note: The comments below refer to the case fg==black and
262    * bg==white, as that was what was implemented first. The fg==white
263    * (the "if (fg->pixel)" branches) case was added later.
264    */
265
266   /* First set masked-out source bits, as all source bits matter on Windoze.
267    * As we invert them below, they will be clear in the final xor_mask.
268    */
269   for (iy = 0; iy < height; iy++)
270     {
271       p = source_bits + iy*source_bpl;
272       q = mask_bits + iy*mask_bpl;
273       
274       for (ix = 0; ix < ((width-1)/8+1); ix++)
275         if (bg_is_white)
276           *p++ |= ~(*q++);
277         else
278           *p++ &= *q++;
279     }
280
281   /* XOR mask is initialized to zero */
282   xor_mask = g_malloc0 (cursor_width/8 * cursor_height);
283
284   for (iy = 0; iy < height; iy++)
285     {
286       p = source_bits + iy*source_bpl;
287       q = xor_mask + iy*cursor_width/8;
288
289       for (ix = 0; ix < ((width-1)/8+1); ix++)
290         if (bg_is_white)
291           *q++ = ~(*p++);
292         else
293           *q++ = *p++;
294
295       q[-1] &= ~residue;        /* Clear left-over bits */
296     }
297       
298   /* AND mask is initialized to ones */
299   and_mask = g_malloc (cursor_width/8 * cursor_height);
300   memset (and_mask, 0xFF, cursor_width/8 * cursor_height);
301
302   for (iy = 0; iy < height; iy++)
303     {
304       p = mask_bits + iy*mask_bpl;
305       q = and_mask + iy*cursor_width/8;
306
307       for (ix = 0; ix < ((width-1)/8+1); ix++)
308         *q++ = ~(*p++);
309
310       q[-1] |= residue; /* Set left-over bits */
311     }
312       
313   hcursor = CreateCursor (_gdk_app_hmodule, x, y, cursor_width, cursor_height,
314                           and_mask, xor_mask);
315
316   GDK_NOTE (MISC, g_print ("gdk_cursor_new_from_pixmap: "
317                            "%p (%dx%d) %p (%dx%d) = %p (%dx%d)\n",
318                            GDK_PIXMAP_HBITMAP (source),
319                            source_impl->width, source_impl->height,
320                            GDK_PIXMAP_HBITMAP (mask),
321                            mask_impl->width, mask_impl->height,
322                            hcursor, cursor_width, cursor_height));
323
324   g_free (xor_mask);
325   g_free (and_mask);
326
327   return _gdk_win32_cursor_new_from_hcursor (hcursor, GDK_CURSOR_IS_PIXMAP);
328 }
329
330 /* The named cursors below are presumably not really useful, as the
331  * names are Win32-specific. No GTK+ application developed on Unix
332  * (and most cross-platform GTK+ apps are developed on Unix) is going
333  * to look for cursors under these Win32 names anyway.
334  *
335  * Would the following make any sense: The ms-windows theme engine
336  * calls some (to-be-defined private) API here in gdk/win32 to
337  * register the relevant cursors used by the currently active XP
338  * visual style under the names that libgtk uses to look for them
339  * ("color-picker", "dnd-ask", "dnd-copy", etc), and then when libgtk
340  * asks for those we return the ones registered by the ms-windows
341  * theme engine, if any.
342  */
343
344 static struct {
345   char *name;
346   char *id;
347 } _default_cursors[] = {
348   { "appstarting", IDC_APPSTARTING },
349   { "arrow", IDC_ARROW },
350   { "cross", IDC_CROSS },
351 #ifdef IDC_HAND
352   { "hand",  IDC_HAND },
353 #endif
354   { "help",  IDC_HELP },
355   { "ibeam", IDC_IBEAM },
356   { "sizeall", IDC_SIZEALL },
357   { "sizenesw", IDC_SIZENESW },
358   { "sizens", IDC_SIZENS },
359   { "sizenwse", IDC_SIZENWSE },
360   { "sizewe", IDC_SIZEWE },
361   { "uparrow", IDC_UPARROW },
362   { "wait", IDC_WAIT }
363 };
364
365 GdkCursor*  
366 gdk_cursor_new_from_name (GdkDisplay  *display,
367                           const gchar *name)
368 {
369   HCURSOR hcursor = NULL;
370   int i;
371
372   g_return_val_if_fail (display == _gdk_display, NULL);
373
374   for (i = 0; i < G_N_ELEMENTS(_default_cursors); i++)
375     {
376       if (0 == strcmp(_default_cursors[i].name, name))
377         hcursor = LoadCursor (NULL, _default_cursors[i].id);
378     }
379   /* allow to load named cursor resources linked into the executable */
380   if (!hcursor)
381     hcursor = LoadCursor (_gdk_app_hmodule, name);
382
383   if (hcursor)
384     return _gdk_win32_cursor_new_from_hcursor (hcursor, GDK_X_CURSOR);
385
386   return NULL;
387 }
388
389 void
390 _gdk_cursor_destroy (GdkCursor *cursor)
391 {
392   GdkCursorPrivate *private;
393
394   g_return_if_fail (cursor != NULL);
395   private = (GdkCursorPrivate *) cursor;
396
397   GDK_NOTE (MISC, g_print ("_gdk_cursor_destroy: %p\n",
398                            (cursor->type == GDK_CURSOR_IS_PIXMAP) ? private->hcursor : 0));
399
400   if (GetCursor () == private->hcursor)
401     SetCursor (NULL);
402
403   if (!DestroyCursor (private->hcursor))
404     WIN32_API_FAILED ("DestroyCursor");
405
406   g_free (private);
407 }
408
409 GdkDisplay *
410 gdk_cursor_get_display (GdkCursor *cursor)
411 {
412   return gdk_display_get_default ();
413 }
414
415 GdkPixbuf *
416 gdk_win32_icon_to_pixbuf_libgtk_only (HICON hicon)
417 {
418   GdkPixbuf *pixbuf = NULL;
419   ICONINFO ii;
420   struct
421   {
422     BITMAPINFOHEADER bi;
423     RGBQUAD colors[2];
424   } bmi;
425   HDC hdc;
426   gchar *pixels, *bits, buf[32];
427   gint rowstride, x, y, w, h;
428
429   if (!GDI_CALL (GetIconInfo, (hicon, &ii)))
430     return NULL;
431
432   if (!(hdc = CreateCompatibleDC (NULL)))
433     {
434       WIN32_GDI_FAILED ("CreateCompatibleDC");
435       goto out0;
436     }
437
438   memset (&bmi, 0, sizeof (bmi));
439   bmi.bi.biSize = sizeof (bmi.bi);
440
441   if (ii.hbmColor != NULL)
442     {
443       /* Colour cursor */
444
445       gboolean no_alpha;
446       
447       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, 1, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
448         goto out1;
449
450       w = bmi.bi.biWidth;
451       h = bmi.bi.biHeight;
452
453       bmi.bi.biBitCount = 32;
454       bmi.bi.biCompression = BI_RGB;
455       bmi.bi.biHeight = -h;
456
457       bits = g_malloc0 (4 * w * h);
458       
459       /* color data */
460       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
461         goto out2;
462
463       pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
464       pixels = gdk_pixbuf_get_pixels (pixbuf);
465       rowstride = gdk_pixbuf_get_rowstride (pixbuf);
466       no_alpha = TRUE;
467       for (y = 0; y < h; y++)
468         {
469           for (x = 0; x < w; x++)
470             {
471               pixels[2] = bits[(x+y*w) * 4];
472               pixels[1] = bits[(x+y*w) * 4 + 1];
473               pixels[0] = bits[(x+y*w) * 4 + 2];
474               pixels[3] = bits[(x+y*w) * 4 + 3];
475               if (no_alpha && pixels[3] > 0)
476                 no_alpha = FALSE;
477               pixels += 4;
478             }
479           pixels += (w * 4 - rowstride);
480         }
481
482       /* mask */
483       if (no_alpha &&
484           GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
485         {
486           pixels = gdk_pixbuf_get_pixels (pixbuf);
487           for (y = 0; y < h; y++)
488             {
489               for (x = 0; x < w; x++)
490                 {
491                   pixels[3] = 255 - bits[(x + y * w) * 4];
492                   pixels += 4;
493                 }
494               pixels += (w * 4 - rowstride);
495             }
496         }
497     }
498   else
499     {
500       /* B&W cursor */
501
502       int bpl;
503
504       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, 0, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
505         goto out1;
506
507       w = bmi.bi.biWidth;
508       h = ABS (bmi.bi.biHeight) / 2;
509       
510       bits = g_malloc0 (4 * w * h);
511       
512       /* masks */
513       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h*2, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
514         goto out2;
515
516       pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
517       pixels = gdk_pixbuf_get_pixels (pixbuf);
518       rowstride = gdk_pixbuf_get_rowstride (pixbuf);
519       bpl = ((w-1)/32 + 1)*4;
520 #if 0
521       for (y = 0; y < h*2; y++)
522         {
523           for (x = 0; x < w; x++)
524             {
525               const gint bit = 7 - (x % 8);
526               printf ("%c ", ((bits[bpl*y+x/8])&(1<<bit)) ? ' ' : 'X');
527             }
528           printf ("\n");
529         }
530 #endif
531
532       for (y = 0; y < h; y++)
533         {
534           const guchar *andp, *xorp;
535           if (bmi.bi.biHeight < 0)
536             {
537               andp = bits + bpl*y;
538               xorp = bits + bpl*(h+y);
539             }
540           else
541             {
542               andp = bits + bpl*(h-y-1);
543               xorp = bits + bpl*(h+h-y-1);
544             }
545           for (x = 0; x < w; x++)
546             {
547               const gint bit = 7 - (x % 8);
548               if ((*andp) & (1<<bit))
549                 {
550                   if ((*xorp) & (1<<bit))
551                     pixels[2] = pixels[1] = pixels[0] = 0xFF;
552                   else
553                     pixels[2] = pixels[1] = pixels[0] = 0;
554                   pixels[3] = 0xFF;
555                 }
556               else
557                 {
558                   pixels[2] = pixels[1] = pixels[0] = 0;
559                   pixels[3] = 0;
560                 }
561               pixels += 4;
562               if (bit == 0)
563                 {
564                   andp++;
565                   xorp++;
566                 }
567             }
568           pixels += (w * 4 - rowstride);
569         }
570     }
571
572   g_snprintf (buf, sizeof (buf), "%ld", ii.xHotspot);
573   gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
574
575   g_snprintf (buf, sizeof (buf), "%ld", ii.yHotspot);
576   gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
577
578   /* release temporary resources */
579  out2:
580   g_free (bits);
581  out1:
582   DeleteDC (hdc);
583  out0:
584   DeleteObject (ii.hbmColor);
585   DeleteObject (ii.hbmMask);
586
587   return pixbuf;
588 }
589
590 GdkPixbuf*  
591 gdk_cursor_get_image (GdkCursor *cursor)
592 {
593   g_return_val_if_fail (cursor != NULL, NULL);
594
595   return gdk_win32_icon_to_pixbuf_libgtk_only (((GdkCursorPrivate *) cursor)->hcursor);
596 }
597
598 GdkCursor *
599 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
600                             GdkPixbuf  *pixbuf,
601                             gint        x,
602                             gint        y)
603 {
604   HCURSOR hcursor;
605
606   g_return_val_if_fail (display == _gdk_display, NULL);
607   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
608   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
609   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
610
611   hcursor = _gdk_win32_pixbuf_to_hcursor (pixbuf, x, y);
612   if (!hcursor)
613     return NULL;
614   return _gdk_win32_cursor_new_from_hcursor (hcursor, GDK_CURSOR_IS_PIXMAP);
615 }
616
617 gboolean 
618 gdk_display_supports_cursor_alpha (GdkDisplay    *display)
619 {
620   g_return_val_if_fail (display == _gdk_display, FALSE);
621
622   return _gdk_win32_pixbuf_to_hicon_supports_alpha ();
623 }
624
625 gboolean 
626 gdk_display_supports_cursor_color (GdkDisplay    *display)
627 {
628   g_return_val_if_fail (display == _gdk_display, FALSE);
629
630   return TRUE;
631 }
632
633 guint     
634 gdk_display_get_default_cursor_size (GdkDisplay    *display)
635 {
636   g_return_val_if_fail (display == _gdk_display, 0);
637   
638   return MIN (GetSystemMetrics (SM_CXCURSOR), GetSystemMetrics (SM_CYCURSOR));
639 }
640
641 void     
642 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
643                                      guint       *width,
644                                      guint       *height)
645 {
646   g_return_if_fail (display == _gdk_display);
647   
648   if (width)
649     *width = GetSystemMetrics (SM_CXCURSOR);
650   if (height)
651     *height = GetSystemMetrics (SM_CYCURSOR);
652 }
653
654
655 /* Convert a pixbuf to an HICON (or HCURSOR).  Supports alpha under
656  * Windows XP, thresholds alpha otherwise.  Also used from
657  * gdkwindow-win32.c for creating application icons.
658  */
659
660 static HBITMAP
661 create_alpha_bitmap (gint     size,
662                      guchar **outdata)
663 {
664   BITMAPV5HEADER bi;
665   HDC hdc;
666   HBITMAP hBitmap;
667
668   ZeroMemory (&bi, sizeof (BITMAPV5HEADER));
669   bi.bV5Size = sizeof (BITMAPV5HEADER);
670   bi.bV5Height = bi.bV5Width = size;
671   bi.bV5Planes = 1;
672   bi.bV5BitCount = 32;
673   bi.bV5Compression = BI_BITFIELDS;
674   /* The following mask specification specifies a supported 32 BPP
675    * alpha format for Windows XP (BGRA format).
676    */
677   bi.bV5RedMask   = 0x00FF0000;
678   bi.bV5GreenMask = 0x0000FF00;
679   bi.bV5BlueMask  = 0x000000FF;
680   bi.bV5AlphaMask = 0xFF000000;
681
682   /* Create the DIB section with an alpha channel. */
683   hdc = GetDC (NULL);
684   if (!hdc)
685     {
686       WIN32_GDI_FAILED ("GetDC");
687       return NULL;
688     }
689   hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
690                               (PVOID *) outdata, NULL, (DWORD)0);
691   if (hBitmap == NULL)
692     WIN32_GDI_FAILED ("CreateDIBSection");
693   ReleaseDC (NULL, hdc);
694
695   return hBitmap;
696 }
697
698 static HBITMAP
699 create_color_bitmap (gint     size,
700                      guchar **outdata,
701                      gint     bits)
702 {
703   struct {
704     BITMAPV4HEADER bmiHeader;
705     RGBQUAD bmiColors[2];
706   } bmi;
707   HDC hdc;
708   HBITMAP hBitmap;
709
710   ZeroMemory (&bmi, sizeof (bmi));
711   bmi.bmiHeader.bV4Size = sizeof (BITMAPV4HEADER);
712   bmi.bmiHeader.bV4Height = bmi.bmiHeader.bV4Width = size;
713   bmi.bmiHeader.bV4Planes = 1;
714   bmi.bmiHeader.bV4BitCount = bits;
715   bmi.bmiHeader.bV4V4Compression = BI_RGB;
716
717   /* when bits is 1, these will be used.
718    * bmiColors[0] already zeroed from ZeroMemory()
719    */
720   bmi.bmiColors[1].rgbBlue = 0xFF;
721   bmi.bmiColors[1].rgbGreen = 0xFF;
722   bmi.bmiColors[1].rgbRed = 0xFF;
723
724   hdc = GetDC (NULL);
725   if (!hdc)
726     {
727       WIN32_GDI_FAILED ("GetDC");
728       return NULL;
729     }
730   hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bmi, DIB_RGB_COLORS,
731                               (PVOID *) outdata, NULL, (DWORD)0);
732   if (hBitmap == NULL)
733     WIN32_GDI_FAILED ("CreateDIBSection");
734   ReleaseDC (NULL, hdc);
735
736   return hBitmap;
737 }
738
739 static gboolean
740 pixbuf_to_hbitmaps_alpha_winxp (GdkPixbuf *pixbuf,
741                                 HBITMAP   *color,
742                                 HBITMAP   *mask)
743 {
744   /* Based on code from
745    * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
746    */
747   HBITMAP hColorBitmap, hMaskBitmap;
748   guchar *indata, *inrow;
749   guchar *colordata, *colorrow, *maskdata, *maskbyte;
750   gint width, height, size, i, i_offset, j, j_offset, rowstride;
751   guint maskstride, mask_bit;
752
753   width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
754   height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
755
756   /* The bitmaps are created square */
757   size = MAX (width, height);
758
759   hColorBitmap = create_alpha_bitmap (size, &colordata);
760   if (!hColorBitmap)
761     return FALSE;
762   hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
763   if (!hMaskBitmap)
764     {
765       DeleteObject (hColorBitmap);
766       return FALSE;
767     }
768
769   /* MSDN says mask rows are aligned to "LONG" boundaries */
770   maskstride = (((size + 31) & ~31) >> 3);
771
772   indata = gdk_pixbuf_get_pixels (pixbuf);
773   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
774
775   if (width > height)
776     {
777       i_offset = 0;
778       j_offset = (width - height) / 2;
779     }
780   else
781     {
782       i_offset = (height - width) / 2;
783       j_offset = 0;
784     }
785
786   for (j = 0; j < height; j++)
787     {
788       colorrow = colordata + 4*(j+j_offset)*size + 4*i_offset;
789       maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
790       mask_bit = (0x80 >> (i_offset % 8));
791       inrow = indata + (height-j-1)*rowstride;
792       for (i = 0; i < width; i++)
793         {
794           colorrow[4*i+0] = inrow[4*i+2];
795           colorrow[4*i+1] = inrow[4*i+1];
796           colorrow[4*i+2] = inrow[4*i+0];
797           colorrow[4*i+3] = inrow[4*i+3];
798           if (inrow[4*i+3] == 0)
799             maskbyte[0] |= mask_bit;    /* turn ON bit */
800           else
801             maskbyte[0] &= ~mask_bit;   /* turn OFF bit */
802           mask_bit >>= 1;
803           if (mask_bit == 0)
804             {
805               mask_bit = 0x80;
806               maskbyte++;
807             }
808         }
809     }
810
811   *color = hColorBitmap;
812   *mask = hMaskBitmap;
813
814   return TRUE;
815 }
816
817 static gboolean
818 pixbuf_to_hbitmaps_normal (GdkPixbuf *pixbuf,
819                            HBITMAP   *color,
820                            HBITMAP   *mask)
821 {
822   /* Based on code from
823    * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
824    */
825   HBITMAP hColorBitmap, hMaskBitmap;
826   guchar *indata, *inrow;
827   guchar *colordata, *colorrow, *maskdata, *maskbyte;
828   gint width, height, size, i, i_offset, j, j_offset, rowstride, nc, bmstride;
829   gboolean has_alpha;
830   guint maskstride, mask_bit;
831
832   width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
833   height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
834
835   /* The bitmaps are created square */
836   size = MAX (width, height);
837
838   hColorBitmap = create_color_bitmap (size, &colordata, 24);
839   if (!hColorBitmap)
840     return FALSE;
841   hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
842   if (!hMaskBitmap)
843     {
844       DeleteObject (hColorBitmap);
845       return FALSE;
846     }
847
848   /* rows are always aligned on 4-byte boundarys */
849   bmstride = size * 3;
850   if (bmstride % 4 != 0)
851     bmstride += 4 - (bmstride % 4);
852
853   /* MSDN says mask rows are aligned to "LONG" boundaries */
854   maskstride = (((size + 31) & ~31) >> 3);
855
856   indata = gdk_pixbuf_get_pixels (pixbuf);
857   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
858   nc = gdk_pixbuf_get_n_channels (pixbuf);
859   has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
860
861   if (width > height)
862     {
863       i_offset = 0;
864       j_offset = (width - height) / 2;
865     }
866   else
867     {
868       i_offset = (height - width) / 2;
869       j_offset = 0;
870     }
871
872   for (j = 0; j < height; j++)
873     {
874       colorrow = colordata + (j+j_offset)*bmstride + 3*i_offset;
875       maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
876       mask_bit = (0x80 >> (i_offset % 8));
877       inrow = indata + (height-j-1)*rowstride;
878       for (i = 0; i < width; i++)
879         {
880           if (has_alpha && inrow[nc*i+3] < 128)
881             {
882               colorrow[3*i+0] = colorrow[3*i+1] = colorrow[3*i+2] = 0;
883               maskbyte[0] |= mask_bit;  /* turn ON bit */
884             }
885           else
886             {
887               colorrow[3*i+0] = inrow[nc*i+2];
888               colorrow[3*i+1] = inrow[nc*i+1];
889               colorrow[3*i+2] = inrow[nc*i+0];
890               maskbyte[0] &= ~mask_bit; /* turn OFF bit */
891             }
892           mask_bit >>= 1;
893           if (mask_bit == 0)
894             {
895               mask_bit = 0x80;
896               maskbyte++;
897             }
898         }
899     }
900
901   *color = hColorBitmap;
902   *mask = hMaskBitmap;
903
904   return TRUE;
905 }
906
907 static HICON
908 pixbuf_to_hicon (GdkPixbuf *pixbuf,
909                  gboolean   is_icon,
910                  gint       x,
911                  gint       y)
912 {
913   ICONINFO ii;
914   HICON icon;
915   gboolean success;
916
917   if (pixbuf == NULL)
918     return NULL;
919
920   if (_gdk_win32_pixbuf_to_hicon_supports_alpha() && gdk_pixbuf_get_has_alpha (pixbuf))
921     success = pixbuf_to_hbitmaps_alpha_winxp (pixbuf, &ii.hbmColor, &ii.hbmMask);
922   else
923     success = pixbuf_to_hbitmaps_normal (pixbuf, &ii.hbmColor, &ii.hbmMask);
924
925   if (!success)
926     return NULL;
927
928   ii.fIcon = is_icon;
929   ii.xHotspot = x;
930   ii.yHotspot = y;
931   icon = CreateIconIndirect (&ii);
932   DeleteObject (ii.hbmColor);
933   DeleteObject (ii.hbmMask);
934   return icon;
935 }
936
937 HICON
938 _gdk_win32_pixbuf_to_hicon (GdkPixbuf *pixbuf)
939 {
940   return pixbuf_to_hicon (pixbuf, TRUE, 0, 0);
941 }
942
943 HICON
944 _gdk_win32_pixbuf_to_hcursor (GdkPixbuf *pixbuf,
945                               gint       x_hotspot,
946                               gint       y_hotspot)
947 {
948   return pixbuf_to_hicon (pixbuf, FALSE, x_hotspot, y_hotspot);
949 }
950
951 gboolean
952 _gdk_win32_pixbuf_to_hicon_supports_alpha (void)
953 {
954   static gboolean is_win_xp=FALSE, is_win_xp_checked=FALSE;
955
956   if (!is_win_xp_checked)
957     {
958       OSVERSIONINFO version;
959
960       is_win_xp_checked = TRUE;
961
962       memset (&version, 0, sizeof (version));
963       version.dwOSVersionInfoSize = sizeof (version);
964       is_win_xp = GetVersionEx (&version)
965         && version.dwPlatformId == VER_PLATFORM_WIN32_NT
966         && (version.dwMajorVersion > 5
967             || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
968     }
969   return is_win_xp;
970 }
971
972 HICON
973 gdk_win32_pixbuf_to_hicon_libgtk_only (GdkPixbuf *pixbuf)
974 {
975   return _gdk_win32_pixbuf_to_hicon (pixbuf);
976 }