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
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.
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.
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/>.
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"
33 #if (defined(__MINGW32__) && (__W32API_MAJOR_VERSION < 3 || (__W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION < 8))) || (defined(_MSC_VER) && (WINVER < 0x0500))
42 LONG bV5XPelsPerMeter;
43 LONG bV5YPelsPerMeter;
45 DWORD bV5ClrImportant;
51 CIEXYZTRIPLE bV5Endpoints;
63 hcursor_from_type (GdkCursorType cursor_type)
68 guchar *and_plane, *xor_plane;
70 if (cursor_type != GDK_BLANK_CURSOR)
72 for (i = 0; i < G_N_ELEMENTS (cursors); i++)
73 if (cursors[i].type == cursor_type)
76 if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name)
79 /* Use real Win32 cursor if possible */
80 if (cursors[i].builtin)
81 return LoadCursor (NULL, cursors[i].builtin);
84 w = GetSystemMetrics (SM_CXCURSOR);
85 h = GetSystemMetrics (SM_CYCURSOR);
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);
92 if (cursor_type != GDK_BLANK_CURSOR)
95 #define SET_BIT(v,b) (v |= (1 << b))
96 #define RESET_BIT(v,b) (v &= ~(1 << b))
98 for (j = 0, y = 0; y < cursors[i].height && y < h ; y++)
101 j = y * cursors[i].width;
103 for (x = 0; x < cursors[i].width && x < w ; x++, j++)
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;
111 RESET_BIT (and_plane[pofs], bit);
113 SET_BIT (xor_plane[pofs], bit);
121 rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
122 w, h, and_plane, xor_plane);
126 rv = CreateCursor (_gdk_app_hmodule, 0, 0,
127 w, h, and_plane, xor_plane);
130 WIN32_API_FAILED ("CreateCursor");
137 struct _GdkWin32CursorClass
139 GdkCursorClass cursor_class;
142 G_DEFINE_TYPE (GdkWin32Cursor, gdk_win32_cursor, GDK_TYPE_CURSOR)
145 _gdk_win32_cursor_finalize (GObject *object)
147 GdkWin32Cursor *private = GDK_WIN32_CURSOR (object);
149 if (GetCursor () == private->hcursor)
152 if (!DestroyCursor (private->hcursor))
153 WIN32_API_FAILED ("DestroyCursor");
155 G_OBJECT_CLASS (gdk_win32_cursor_parent_class)->finalize (object);
159 cursor_new_from_hcursor (HCURSOR hcursor,
160 GdkCursorType cursor_type)
162 GdkWin32Cursor *private;
165 private = g_object_new (GDK_TYPE_WIN32_CURSOR,
166 "cursor-type", cursor_type,
167 "display", _gdk_display,
169 private->hcursor = hcursor;
170 cursor = (GdkCursor*) private;
176 _gdk_win32_display_get_cursor_for_type (GdkDisplay *display,
177 GdkCursorType cursor_type)
181 g_return_val_if_fail (display == _gdk_display, NULL);
183 hcursor = hcursor_from_type (cursor_type);
186 g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type);
188 GDK_NOTE (CURSOR, g_print ("gdk_cursor_new_for_display: %d: %p\n",
189 cursor_type, hcursor));
191 return cursor_new_from_hcursor (hcursor, cursor_type);
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.
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.
211 } default_cursors[] = {
212 { "appstarting", IDC_APPSTARTING },
213 { "arrow", IDC_ARROW },
214 { "cross", IDC_CROSS },
216 { "hand", IDC_HAND },
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 },
230 _gdk_win32_display_get_cursor_for_name (GdkDisplay *display,
233 HCURSOR hcursor = NULL;
236 g_return_val_if_fail (display == _gdk_display, NULL);
238 for (i = 0; i < G_N_ELEMENTS(default_cursors); i++)
240 if (0 == strcmp(default_cursors[i].name, name))
241 hcursor = LoadCursor (NULL, default_cursors[i].id);
243 /* allow to load named cursor resources linked into the executable */
245 hcursor = LoadCursor (_gdk_app_hmodule, name);
248 return cursor_new_from_hcursor (hcursor, GDK_X_CURSOR);
254 gdk_win32_icon_to_pixbuf_libgtk_only (HICON hicon)
256 GdkPixbuf *pixbuf = NULL;
264 guchar *pixels, *bits;
266 gint rowstride, x, y, w, h;
268 if (!GDI_CALL (GetIconInfo, (hicon, &ii)))
271 if (!(hdc = CreateCompatibleDC (NULL)))
273 WIN32_GDI_FAILED ("CreateCompatibleDC");
277 memset (&bmi, 0, sizeof (bmi));
278 bmi.bi.biSize = sizeof (bmi.bi);
280 if (ii.hbmColor != NULL)
286 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, 1, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
292 bmi.bi.biBitCount = 32;
293 bmi.bi.biCompression = BI_RGB;
294 bmi.bi.biHeight = -h;
296 bits = g_malloc0 (4 * w * h);
299 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
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);
306 for (y = 0; y < h; y++)
308 for (x = 0; x < w; x++)
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)
318 pixels += (w * 4 - rowstride);
323 GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
325 pixels = gdk_pixbuf_get_pixels (pixbuf);
326 for (y = 0; y < h; y++)
328 for (x = 0; x < w; x++)
330 pixels[3] = 255 - bits[(x + y * w) * 4];
333 pixels += (w * 4 - rowstride);
343 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, 0, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
347 h = ABS (bmi.bi.biHeight) / 2;
349 bits = g_malloc0 (4 * w * h);
352 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h*2, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
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;
360 for (y = 0; y < h*2; y++)
362 for (x = 0; x < w; x++)
364 const gint bit = 7 - (x % 8);
365 printf ("%c ", ((bits[bpl*y+x/8])&(1<<bit)) ? ' ' : 'X');
371 for (y = 0; y < h; y++)
373 const guchar *andp, *xorp;
374 if (bmi.bi.biHeight < 0)
377 xorp = bits + bpl*(h+y);
381 andp = bits + bpl*(h-y-1);
382 xorp = bits + bpl*(h+h-y-1);
384 for (x = 0; x < w; x++)
386 const gint bit = 7 - (x % 8);
387 if ((*andp) & (1<<bit))
389 if ((*xorp) & (1<<bit))
390 pixels[2] = pixels[1] = pixels[0] = 0xFF;
392 pixels[2] = pixels[1] = pixels[0] = 0;
397 pixels[2] = pixels[1] = pixels[0] = 0;
407 pixels += (w * 4 - rowstride);
411 g_snprintf (buf, sizeof (buf), "%ld", ii.xHotspot);
412 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
414 g_snprintf (buf, sizeof (buf), "%ld", ii.yHotspot);
415 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
417 /* release temporary resources */
423 DeleteObject (ii.hbmColor);
424 DeleteObject (ii.hbmMask);
430 _gdk_win32_cursor_get_image (GdkCursor *cursor)
432 g_return_val_if_fail (cursor != NULL, NULL);
434 return gdk_win32_icon_to_pixbuf_libgtk_only (((GdkWin32Cursor *) cursor)->hcursor);
438 _gdk_win32_display_get_cursor_for_pixbuf (GdkDisplay *display,
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);
450 hcursor = _gdk_win32_pixbuf_to_hcursor (pixbuf, x, y);
453 return cursor_new_from_hcursor (hcursor, GDK_CURSOR_IS_PIXMAP);
457 _gdk_win32_display_supports_cursor_alpha (GdkDisplay *display)
459 g_return_val_if_fail (display == _gdk_display, FALSE);
461 return _gdk_win32_pixbuf_to_hicon_supports_alpha ();
465 _gdk_win32_display_supports_cursor_color (GdkDisplay *display)
467 g_return_val_if_fail (display == _gdk_display, FALSE);
473 _gdk_win32_display_get_default_cursor_size (GdkDisplay *display,
477 g_return_if_fail (display == _gdk_display);
480 *width = GetSystemMetrics (SM_CXCURSOR);
482 *height = GetSystemMetrics (SM_CYCURSOR);
486 _gdk_win32_display_get_maximal_cursor_size (GdkDisplay *display,
490 g_return_if_fail (display == _gdk_display);
493 *width = GetSystemMetrics (SM_CXCURSOR);
495 *height = GetSystemMetrics (SM_CYCURSOR);
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.
505 create_alpha_bitmap (gint size,
512 ZeroMemory (&bi, sizeof (BITMAPV5HEADER));
513 bi.bV5Size = sizeof (BITMAPV5HEADER);
514 bi.bV5Height = bi.bV5Width = size;
517 bi.bV5Compression = BI_BITFIELDS;
518 /* The following mask specification specifies a supported 32 BPP
519 * alpha format for Windows XP (BGRA format).
521 bi.bV5RedMask = 0x00FF0000;
522 bi.bV5GreenMask = 0x0000FF00;
523 bi.bV5BlueMask = 0x000000FF;
524 bi.bV5AlphaMask = 0xFF000000;
526 /* Create the DIB section with an alpha channel. */
530 WIN32_GDI_FAILED ("GetDC");
533 hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
534 (PVOID *) outdata, NULL, (DWORD)0);
536 WIN32_GDI_FAILED ("CreateDIBSection");
537 ReleaseDC (NULL, hdc);
543 create_color_bitmap (gint size,
548 BITMAPV4HEADER bmiHeader;
549 RGBQUAD bmiColors[2];
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;
561 /* when bits is 1, these will be used.
562 * bmiColors[0] already zeroed from ZeroMemory()
564 bmi.bmiColors[1].rgbBlue = 0xFF;
565 bmi.bmiColors[1].rgbGreen = 0xFF;
566 bmi.bmiColors[1].rgbRed = 0xFF;
571 WIN32_GDI_FAILED ("GetDC");
574 hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bmi, DIB_RGB_COLORS,
575 (PVOID *) outdata, NULL, (DWORD)0);
577 WIN32_GDI_FAILED ("CreateDIBSection");
578 ReleaseDC (NULL, hdc);
584 pixbuf_to_hbitmaps_alpha_winxp (GdkPixbuf *pixbuf,
588 /* Based on code from
589 * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
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;
597 width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
598 height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
600 /* The bitmaps are created square */
601 size = MAX (width, height);
603 hColorBitmap = create_alpha_bitmap (size, &colordata);
606 hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
609 DeleteObject (hColorBitmap);
613 /* MSDN says mask rows are aligned to "LONG" boundaries */
614 maskstride = (((size + 31) & ~31) >> 3);
616 indata = gdk_pixbuf_get_pixels (pixbuf);
617 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
622 j_offset = (width - height) / 2;
626 i_offset = (height - width) / 2;
630 for (j = 0; j < height; j++)
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++)
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 */
645 maskbyte[0] &= ~mask_bit; /* turn OFF bit */
655 *color = hColorBitmap;
662 pixbuf_to_hbitmaps_normal (GdkPixbuf *pixbuf,
666 /* Based on code from
667 * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
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;
674 guint maskstride, mask_bit;
676 width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
677 height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
679 /* The bitmaps are created square */
680 size = MAX (width, height);
682 hColorBitmap = create_color_bitmap (size, &colordata, 24);
685 hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
688 DeleteObject (hColorBitmap);
692 /* rows are always aligned on 4-byte boundarys */
694 if (bmstride % 4 != 0)
695 bmstride += 4 - (bmstride % 4);
697 /* MSDN says mask rows are aligned to "LONG" boundaries */
698 maskstride = (((size + 31) & ~31) >> 3);
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);
708 j_offset = (width - height) / 2;
712 i_offset = (height - width) / 2;
716 for (j = 0; j < height; j++)
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++)
724 if (has_alpha && inrow[nc*i+3] < 128)
726 colorrow[3*i+0] = colorrow[3*i+1] = colorrow[3*i+2] = 0;
727 maskbyte[0] |= mask_bit; /* turn ON bit */
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 */
745 *color = hColorBitmap;
752 pixbuf_to_hicon (GdkPixbuf *pixbuf,
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);
767 success = pixbuf_to_hbitmaps_normal (pixbuf, &ii.hbmColor, &ii.hbmMask);
775 icon = CreateIconIndirect (&ii);
776 DeleteObject (ii.hbmColor);
777 DeleteObject (ii.hbmMask);
782 _gdk_win32_pixbuf_to_hicon (GdkPixbuf *pixbuf)
784 return pixbuf_to_hicon (pixbuf, TRUE, 0, 0);
788 _gdk_win32_pixbuf_to_hcursor (GdkPixbuf *pixbuf,
792 return pixbuf_to_hicon (pixbuf, FALSE, x_hotspot, y_hotspot);
796 _gdk_win32_pixbuf_to_hicon_supports_alpha (void)
798 static gboolean is_win_xp=FALSE, is_win_xp_checked=FALSE;
800 if (!is_win_xp_checked)
802 OSVERSIONINFO version;
804 is_win_xp_checked = TRUE;
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));
817 gdk_win32_pixbuf_to_hicon_libgtk_only (GdkPixbuf *pixbuf)
819 return _gdk_win32_pixbuf_to_hicon (pixbuf);
823 gdk_win32_cursor_init (GdkWin32Cursor *cursor)
827 gdk_win32_cursor_class_init(GdkWin32CursorClass *klass)
829 GObjectClass *object_class = G_OBJECT_CLASS (klass);
830 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (klass);
832 object_class->finalize = _gdk_win32_cursor_finalize;
834 cursor_class->get_image = _gdk_win32_cursor_get_image;