]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkcursor-win32.c
Add gdk_frame_clock_begin/end_updating()
[~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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20 #define GDK_PIXBUF_ENABLE_BACKEND /* Ugly? */
21 #include "gdkdisplay.h"
22 #include "gdkscreen.h"
23 #include "gdkcursor.h"
24 #include "gdkprivate-win32.h"
25 #include "gdkwin32cursor.h"
26
27 #ifdef __MINGW32__
28 #include <w32api.h>
29 #endif
30
31 #include "xcursors.h"
32
33 #if (defined(__MINGW32__) && (__W32API_MAJOR_VERSION < 3 || (__W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION < 8))) || (defined(_MSC_VER) && (WINVER < 0x0500))
34 typedef struct { 
35   DWORD        bV5Size; 
36   LONG         bV5Width; 
37   LONG         bV5Height; 
38   WORD         bV5Planes; 
39   WORD         bV5BitCount; 
40   DWORD        bV5Compression; 
41   DWORD        bV5SizeImage; 
42   LONG         bV5XPelsPerMeter; 
43   LONG         bV5YPelsPerMeter; 
44   DWORD        bV5ClrUsed; 
45   DWORD        bV5ClrImportant; 
46   DWORD        bV5RedMask; 
47   DWORD        bV5GreenMask; 
48   DWORD        bV5BlueMask; 
49   DWORD        bV5AlphaMask; 
50   DWORD        bV5CSType; 
51   CIEXYZTRIPLE bV5Endpoints; 
52   DWORD        bV5GammaRed; 
53   DWORD        bV5GammaGreen; 
54   DWORD        bV5GammaBlue; 
55   DWORD        bV5Intent; 
56   DWORD        bV5ProfileData; 
57   DWORD        bV5ProfileSize; 
58   DWORD        bV5Reserved; 
59 } BITMAPV5HEADER;
60 #endif
61
62 static HCURSOR
63 hcursor_from_type (GdkCursorType cursor_type)
64 {
65   gint i, j, x, y, ofs;
66   HCURSOR rv;
67   gint w, h;
68   guchar *and_plane, *xor_plane;
69
70   if (cursor_type != GDK_BLANK_CURSOR)
71     {
72       for (i = 0; i < G_N_ELEMENTS (cursors); i++)
73         if (cursors[i].type == cursor_type)
74           break;
75
76       if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name)
77         return NULL;
78
79       /* Use real Win32 cursor if possible */
80       if (cursors[i].builtin)
81         return LoadCursor (NULL, cursors[i].builtin);
82     }
83   
84   w = GetSystemMetrics (SM_CXCURSOR);
85   h = GetSystemMetrics (SM_CYCURSOR);
86
87   and_plane = g_malloc ((w/8) * h);
88   memset (and_plane, 0xff, (w/8) * h);
89   xor_plane = g_malloc ((w/8) * h);
90   memset (xor_plane, 0, (w/8) * h);
91
92   if (cursor_type != GDK_BLANK_CURSOR)
93     {
94
95 #define SET_BIT(v,b)  (v |= (1 << b))
96 #define RESET_BIT(v,b)  (v &= ~(1 << b))
97
98       for (j = 0, y = 0; y < cursors[i].height && y < h ; y++)
99         {
100           ofs = (y * w) / 8;
101           j = y * cursors[i].width;
102           
103           for (x = 0; x < cursors[i].width && x < w ; x++, j++)
104             {
105               gint pofs = ofs + x / 8;
106               guchar data = (cursors[i].data[j/4] & (0xc0 >> (2 * (j%4)))) >> (2 * (3 - (j%4)));
107               gint bit = 7 - (j % cursors[i].width) % 8;
108               
109               if (data)
110                 {
111                   RESET_BIT (and_plane[pofs], bit);
112                   if (data == 1)
113                     SET_BIT (xor_plane[pofs], bit);
114                 }
115             }
116         }
117
118 #undef SET_BIT
119 #undef RESET_BIT
120
121       rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
122                          w, h, and_plane, xor_plane);
123     }
124   else
125     {
126       rv = CreateCursor (_gdk_app_hmodule, 0, 0,
127                          w, h, and_plane, xor_plane);
128     }
129   if (rv == NULL)
130     WIN32_API_FAILED ("CreateCursor");
131   g_free (and_plane);
132   g_free (xor_plane);
133   
134   return rv;
135 }
136
137 struct _GdkWin32CursorClass
138 {
139   GdkCursorClass cursor_class;
140 };
141
142 G_DEFINE_TYPE (GdkWin32Cursor, gdk_win32_cursor, GDK_TYPE_CURSOR)
143
144 static void
145 _gdk_win32_cursor_finalize (GObject *object)
146 {
147   GdkWin32Cursor *private = GDK_WIN32_CURSOR (object);
148
149   if (GetCursor () == private->hcursor)
150     SetCursor (NULL);
151
152   if (!DestroyCursor (private->hcursor))
153     WIN32_API_FAILED ("DestroyCursor");
154
155   G_OBJECT_CLASS (gdk_win32_cursor_parent_class)->finalize (object);
156 }
157
158 static GdkCursor*
159 cursor_new_from_hcursor (HCURSOR       hcursor,
160                          GdkCursorType cursor_type)
161 {
162   GdkWin32Cursor *private;
163   GdkCursor *cursor;
164
165   private = g_object_new (GDK_TYPE_WIN32_CURSOR,
166                           "cursor-type", cursor_type,
167                           "display", _gdk_display,
168                           NULL);
169   private->hcursor = hcursor;
170   cursor = (GdkCursor*) private;
171
172   return cursor;
173 }
174
175 GdkCursor*
176 _gdk_win32_display_get_cursor_for_type (GdkDisplay   *display,
177                                         GdkCursorType cursor_type)
178 {
179   HCURSOR hcursor;
180
181   g_return_val_if_fail (display == _gdk_display, NULL);
182
183   hcursor = hcursor_from_type (cursor_type);
184
185   if (hcursor == NULL)
186     g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type);
187   else
188     GDK_NOTE (CURSOR, g_print ("gdk_cursor_new_for_display: %d: %p\n",
189                                cursor_type, hcursor));
190
191   return cursor_new_from_hcursor (hcursor, cursor_type);
192 }
193
194 /* FIXME: The named cursors below are presumably not really useful, as
195  * the names are Win32-specific. No GTK+ application developed on Unix
196  * (and most cross-platform GTK+ apps are developed on Unix) is going
197  * to look for cursors under these Win32 names anyway.
198  *
199  * Would the following make any sense: The ms-windows theme engine
200  * calls some (to-be-defined private) API here in gdk/win32 to
201  * register the relevant cursors used by the currently active XP
202  * visual style under the names that libgtk uses to look for them
203  * ("color-picker", "dnd-ask", "dnd-copy", etc), and then when libgtk
204  * asks for those we return the ones registered by the ms-windows
205  * theme engine, if any.
206  */
207
208 static struct {
209   char *name;
210   char *id;
211 } default_cursors[] = {
212   { "appstarting", IDC_APPSTARTING },
213   { "arrow", IDC_ARROW },
214   { "cross", IDC_CROSS },
215 #ifdef IDC_HAND
216   { "hand",  IDC_HAND },
217 #endif
218   { "help",  IDC_HELP },
219   { "ibeam", IDC_IBEAM },
220   { "sizeall", IDC_SIZEALL },
221   { "sizenesw", IDC_SIZENESW },
222   { "sizens", IDC_SIZENS },
223   { "sizenwse", IDC_SIZENWSE },
224   { "sizewe", IDC_SIZEWE },
225   { "uparrow", IDC_UPARROW },
226   { "wait", IDC_WAIT }
227 };
228
229 GdkCursor*  
230 _gdk_win32_display_get_cursor_for_name (GdkDisplay  *display,
231                                         const gchar *name)
232 {
233   HCURSOR hcursor = NULL;
234   int i;
235
236   g_return_val_if_fail (display == _gdk_display, NULL);
237
238   for (i = 0; i < G_N_ELEMENTS(default_cursors); i++)
239     {
240       if (0 == strcmp(default_cursors[i].name, name))
241         hcursor = LoadCursor (NULL, default_cursors[i].id);
242     }
243   /* allow to load named cursor resources linked into the executable */
244   if (!hcursor)
245     hcursor = LoadCursor (_gdk_app_hmodule, name);
246
247   if (hcursor)
248     return cursor_new_from_hcursor (hcursor, GDK_X_CURSOR);
249
250   return NULL;
251 }
252
253 GdkPixbuf *
254 gdk_win32_icon_to_pixbuf_libgtk_only (HICON hicon)
255 {
256   GdkPixbuf *pixbuf = NULL;
257   ICONINFO ii;
258   struct
259   {
260     BITMAPINFOHEADER bi;
261     RGBQUAD colors[2];
262   } bmi;
263   HDC hdc;
264   guchar *pixels, *bits;
265   gchar buf[32];
266   gint rowstride, x, y, w, h;
267
268   if (!GDI_CALL (GetIconInfo, (hicon, &ii)))
269     return NULL;
270
271   if (!(hdc = CreateCompatibleDC (NULL)))
272     {
273       WIN32_GDI_FAILED ("CreateCompatibleDC");
274       goto out0;
275     }
276
277   memset (&bmi, 0, sizeof (bmi));
278   bmi.bi.biSize = sizeof (bmi.bi);
279
280   if (ii.hbmColor != NULL)
281     {
282       /* Colour cursor */
283
284       gboolean no_alpha;
285       
286       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, 1, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
287         goto out1;
288
289       w = bmi.bi.biWidth;
290       h = bmi.bi.biHeight;
291
292       bmi.bi.biBitCount = 32;
293       bmi.bi.biCompression = BI_RGB;
294       bmi.bi.biHeight = -h;
295
296       bits = g_malloc0 (4 * w * h);
297       
298       /* color data */
299       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
300         goto out2;
301
302       pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
303       pixels = gdk_pixbuf_get_pixels (pixbuf);
304       rowstride = gdk_pixbuf_get_rowstride (pixbuf);
305       no_alpha = TRUE;
306       for (y = 0; y < h; y++)
307         {
308           for (x = 0; x < w; x++)
309             {
310               pixels[2] = bits[(x+y*w) * 4];
311               pixels[1] = bits[(x+y*w) * 4 + 1];
312               pixels[0] = bits[(x+y*w) * 4 + 2];
313               pixels[3] = bits[(x+y*w) * 4 + 3];
314               if (no_alpha && pixels[3] > 0)
315                 no_alpha = FALSE;
316               pixels += 4;
317             }
318           pixels += (w * 4 - rowstride);
319         }
320
321       /* mask */
322       if (no_alpha &&
323           GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
324         {
325           pixels = gdk_pixbuf_get_pixels (pixbuf);
326           for (y = 0; y < h; y++)
327             {
328               for (x = 0; x < w; x++)
329                 {
330                   pixels[3] = 255 - bits[(x + y * w) * 4];
331                   pixels += 4;
332                 }
333               pixels += (w * 4 - rowstride);
334             }
335         }
336     }
337   else
338     {
339       /* B&W cursor */
340
341       int bpl;
342
343       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, 0, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
344         goto out1;
345
346       w = bmi.bi.biWidth;
347       h = ABS (bmi.bi.biHeight) / 2;
348       
349       bits = g_malloc0 (4 * w * h);
350       
351       /* masks */
352       if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h*2, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
353         goto out2;
354
355       pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
356       pixels = gdk_pixbuf_get_pixels (pixbuf);
357       rowstride = gdk_pixbuf_get_rowstride (pixbuf);
358       bpl = ((w-1)/32 + 1)*4;
359 #if 0
360       for (y = 0; y < h*2; y++)
361         {
362           for (x = 0; x < w; x++)
363             {
364               const gint bit = 7 - (x % 8);
365               printf ("%c ", ((bits[bpl*y+x/8])&(1<<bit)) ? ' ' : 'X');
366             }
367           printf ("\n");
368         }
369 #endif
370
371       for (y = 0; y < h; y++)
372         {
373           const guchar *andp, *xorp;
374           if (bmi.bi.biHeight < 0)
375             {
376               andp = bits + bpl*y;
377               xorp = bits + bpl*(h+y);
378             }
379           else
380             {
381               andp = bits + bpl*(h-y-1);
382               xorp = bits + bpl*(h+h-y-1);
383             }
384           for (x = 0; x < w; x++)
385             {
386               const gint bit = 7 - (x % 8);
387               if ((*andp) & (1<<bit))
388                 {
389                   if ((*xorp) & (1<<bit))
390                     pixels[2] = pixels[1] = pixels[0] = 0xFF;
391                   else
392                     pixels[2] = pixels[1] = pixels[0] = 0;
393                   pixels[3] = 0xFF;
394                 }
395               else
396                 {
397                   pixels[2] = pixels[1] = pixels[0] = 0;
398                   pixels[3] = 0;
399                 }
400               pixels += 4;
401               if (bit == 0)
402                 {
403                   andp++;
404                   xorp++;
405                 }
406             }
407           pixels += (w * 4 - rowstride);
408         }
409     }
410
411   g_snprintf (buf, sizeof (buf), "%ld", ii.xHotspot);
412   gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
413
414   g_snprintf (buf, sizeof (buf), "%ld", ii.yHotspot);
415   gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
416
417   /* release temporary resources */
418  out2:
419   g_free (bits);
420  out1:
421   DeleteDC (hdc);
422  out0:
423   DeleteObject (ii.hbmColor);
424   DeleteObject (ii.hbmMask);
425
426   return pixbuf;
427 }
428
429 static GdkPixbuf *
430 _gdk_win32_cursor_get_image (GdkCursor *cursor)
431 {
432   g_return_val_if_fail (cursor != NULL, NULL);
433
434   return gdk_win32_icon_to_pixbuf_libgtk_only (((GdkWin32Cursor *) cursor)->hcursor);
435 }
436
437 GdkCursor *
438 _gdk_win32_display_get_cursor_for_pixbuf (GdkDisplay *display, 
439                                           GdkPixbuf  *pixbuf,
440                                           gint        x,
441                                           gint        y)
442 {
443   HCURSOR hcursor;
444
445   g_return_val_if_fail (display == _gdk_display, NULL);
446   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
447   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
448   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
449
450   hcursor = _gdk_win32_pixbuf_to_hcursor (pixbuf, x, y);
451   if (!hcursor)
452     return NULL;
453   return cursor_new_from_hcursor (hcursor, GDK_CURSOR_IS_PIXMAP);
454 }
455
456 gboolean
457 _gdk_win32_display_supports_cursor_alpha (GdkDisplay    *display)
458 {
459   g_return_val_if_fail (display == _gdk_display, FALSE);
460
461   return _gdk_win32_pixbuf_to_hicon_supports_alpha ();
462 }
463
464 gboolean 
465 _gdk_win32_display_supports_cursor_color (GdkDisplay    *display)
466 {
467   g_return_val_if_fail (display == _gdk_display, FALSE);
468
469   return TRUE;
470 }
471
472 void
473 _gdk_win32_display_get_default_cursor_size (GdkDisplay *display,
474                                             guint       *width,
475                                             guint       *height)
476 {
477   g_return_if_fail (display == _gdk_display);
478
479   if (width)
480     *width = GetSystemMetrics (SM_CXCURSOR);
481   if (height)
482     *height = GetSystemMetrics (SM_CYCURSOR);
483 }
484
485 void     
486 _gdk_win32_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 }
821
822 static void
823 gdk_win32_cursor_init (GdkWin32Cursor *cursor)
824 {
825 }
826 static void
827 gdk_win32_cursor_class_init(GdkWin32CursorClass *klass)
828 {
829   GObjectClass *object_class = G_OBJECT_CLASS (klass);
830   GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (klass);
831
832   object_class->finalize = _gdk_win32_cursor_finalize;
833   
834   cursor_class->get_image = _gdk_win32_cursor_get_image;
835 }