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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #define GDK_PIXBUF_ENABLE_BACKEND /* Ugly? */
23 #include "gdkdisplay.h"
24 #include "gdkscreen.h"
25 #include "gdkcursor.h"
26 #include "gdkprivate-win32.h"
27 #include "gdkwin32cursor.h"
35 #if (defined(__MINGW32__) && (__W32API_MAJOR_VERSION < 3 || (__W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION < 8))) || (defined(_MSC_VER) && (WINVER < 0x0500))
44 LONG bV5XPelsPerMeter;
45 LONG bV5YPelsPerMeter;
47 DWORD bV5ClrImportant;
53 CIEXYZTRIPLE bV5Endpoints;
65 hcursor_from_type (GdkCursorType cursor_type)
70 guchar *and_plane, *xor_plane;
72 if (cursor_type != GDK_BLANK_CURSOR)
74 for (i = 0; i < G_N_ELEMENTS (cursors); i++)
75 if (cursors[i].type == cursor_type)
78 if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name)
81 /* Use real Win32 cursor if possible */
82 if (cursors[i].builtin)
83 return LoadCursor (NULL, cursors[i].builtin);
86 w = GetSystemMetrics (SM_CXCURSOR);
87 h = GetSystemMetrics (SM_CYCURSOR);
89 and_plane = g_malloc ((w/8) * h);
90 memset (and_plane, 0xff, (w/8) * h);
91 xor_plane = g_malloc ((w/8) * h);
92 memset (xor_plane, 0, (w/8) * h);
94 if (cursor_type != GDK_BLANK_CURSOR)
97 #define SET_BIT(v,b) (v |= (1 << b))
98 #define RESET_BIT(v,b) (v &= ~(1 << b))
100 for (j = 0, y = 0; y < cursors[i].height && y < h ; y++)
103 j = y * cursors[i].width;
105 for (x = 0; x < cursors[i].width && x < w ; x++, j++)
107 gint pofs = ofs + x / 8;
108 guchar data = (cursors[i].data[j/4] & (0xc0 >> (2 * (j%4)))) >> (2 * (3 - (j%4)));
109 gint bit = 7 - (j % cursors[i].width) % 8;
113 RESET_BIT (and_plane[pofs], bit);
115 SET_BIT (xor_plane[pofs], bit);
123 rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
124 w, h, and_plane, xor_plane);
128 rv = CreateCursor (_gdk_app_hmodule, 0, 0,
129 w, h, and_plane, xor_plane);
132 WIN32_API_FAILED ("CreateCursor");
139 struct _GdkWin32CursorClass
141 GdkCursorClass cursor_class;
144 G_DEFINE_TYPE (GdkWin32Cursor, gdk_win32_cursor, GDK_TYPE_CURSOR)
147 _gdk_win32_cursor_finalize (GObject *object)
149 GdkWin32Cursor *private = GDK_WIN32_CURSOR (object);
151 if (GetCursor () == private->hcursor)
154 if (!DestroyCursor (private->hcursor))
155 WIN32_API_FAILED ("DestroyCursor");
157 G_OBJECT_CLASS (gdk_win32_cursor_parent_class)->finalize (object);
161 cursor_new_from_hcursor (HCURSOR hcursor,
162 GdkCursorType cursor_type)
164 GdkWin32Cursor *private;
167 private = g_object_new (GDK_TYPE_WIN32_CURSOR,
168 "cursor-type", cursor_type,
169 "display", _gdk_display,
171 private->hcursor = hcursor;
172 cursor = (GdkCursor*) private;
178 _gdk_win32_display_get_cursor_for_type (GdkDisplay *display,
179 GdkCursorType cursor_type)
183 g_return_val_if_fail (display == _gdk_display, NULL);
185 hcursor = hcursor_from_type (cursor_type);
188 g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type);
190 GDK_NOTE (CURSOR, g_print ("gdk_cursor_new_for_display: %d: %p\n",
191 cursor_type, hcursor));
193 return cursor_new_from_hcursor (hcursor, cursor_type);
196 /* FIXME: The named cursors below are presumably not really useful, as
197 * the names are Win32-specific. No GTK+ application developed on Unix
198 * (and most cross-platform GTK+ apps are developed on Unix) is going
199 * to look for cursors under these Win32 names anyway.
201 * Would the following make any sense: The ms-windows theme engine
202 * calls some (to-be-defined private) API here in gdk/win32 to
203 * register the relevant cursors used by the currently active XP
204 * visual style under the names that libgtk uses to look for them
205 * ("color-picker", "dnd-ask", "dnd-copy", etc), and then when libgtk
206 * asks for those we return the ones registered by the ms-windows
207 * theme engine, if any.
213 } default_cursors[] = {
214 { "appstarting", IDC_APPSTARTING },
215 { "arrow", IDC_ARROW },
216 { "cross", IDC_CROSS },
218 { "hand", IDC_HAND },
220 { "help", IDC_HELP },
221 { "ibeam", IDC_IBEAM },
222 { "sizeall", IDC_SIZEALL },
223 { "sizenesw", IDC_SIZENESW },
224 { "sizens", IDC_SIZENS },
225 { "sizenwse", IDC_SIZENWSE },
226 { "sizewe", IDC_SIZEWE },
227 { "uparrow", IDC_UPARROW },
232 _gdk_win32_display_get_cursor_for_name (GdkDisplay *display,
235 HCURSOR hcursor = NULL;
238 g_return_val_if_fail (display == _gdk_display, NULL);
240 for (i = 0; i < G_N_ELEMENTS(default_cursors); i++)
242 if (0 == strcmp(default_cursors[i].name, name))
243 hcursor = LoadCursor (NULL, default_cursors[i].id);
245 /* allow to load named cursor resources linked into the executable */
247 hcursor = LoadCursor (_gdk_app_hmodule, name);
250 return cursor_new_from_hcursor (hcursor, GDK_X_CURSOR);
256 gdk_win32_icon_to_pixbuf_libgtk_only (HICON hicon)
258 GdkPixbuf *pixbuf = NULL;
266 guchar *pixels, *bits;
268 gint rowstride, x, y, w, h;
270 if (!GDI_CALL (GetIconInfo, (hicon, &ii)))
273 if (!(hdc = CreateCompatibleDC (NULL)))
275 WIN32_GDI_FAILED ("CreateCompatibleDC");
279 memset (&bmi, 0, sizeof (bmi));
280 bmi.bi.biSize = sizeof (bmi.bi);
282 if (ii.hbmColor != NULL)
288 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, 1, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
294 bmi.bi.biBitCount = 32;
295 bmi.bi.biCompression = BI_RGB;
296 bmi.bi.biHeight = -h;
298 bits = g_malloc0 (4 * w * h);
301 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
304 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
305 pixels = gdk_pixbuf_get_pixels (pixbuf);
306 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
308 for (y = 0; y < h; y++)
310 for (x = 0; x < w; x++)
312 pixels[2] = bits[(x+y*w) * 4];
313 pixels[1] = bits[(x+y*w) * 4 + 1];
314 pixels[0] = bits[(x+y*w) * 4 + 2];
315 pixels[3] = bits[(x+y*w) * 4 + 3];
316 if (no_alpha && pixels[3] > 0)
320 pixels += (w * 4 - rowstride);
325 GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
327 pixels = gdk_pixbuf_get_pixels (pixbuf);
328 for (y = 0; y < h; y++)
330 for (x = 0; x < w; x++)
332 pixels[3] = 255 - bits[(x + y * w) * 4];
335 pixels += (w * 4 - rowstride);
345 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, 0, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
349 h = ABS (bmi.bi.biHeight) / 2;
351 bits = g_malloc0 (4 * w * h);
354 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h*2, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
357 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
358 pixels = gdk_pixbuf_get_pixels (pixbuf);
359 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
360 bpl = ((w-1)/32 + 1)*4;
362 for (y = 0; y < h*2; y++)
364 for (x = 0; x < w; x++)
366 const gint bit = 7 - (x % 8);
367 printf ("%c ", ((bits[bpl*y+x/8])&(1<<bit)) ? ' ' : 'X');
373 for (y = 0; y < h; y++)
375 const guchar *andp, *xorp;
376 if (bmi.bi.biHeight < 0)
379 xorp = bits + bpl*(h+y);
383 andp = bits + bpl*(h-y-1);
384 xorp = bits + bpl*(h+h-y-1);
386 for (x = 0; x < w; x++)
388 const gint bit = 7 - (x % 8);
389 if ((*andp) & (1<<bit))
391 if ((*xorp) & (1<<bit))
392 pixels[2] = pixels[1] = pixels[0] = 0xFF;
394 pixels[2] = pixels[1] = pixels[0] = 0;
399 pixels[2] = pixels[1] = pixels[0] = 0;
409 pixels += (w * 4 - rowstride);
413 g_snprintf (buf, sizeof (buf), "%ld", ii.xHotspot);
414 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
416 g_snprintf (buf, sizeof (buf), "%ld", ii.yHotspot);
417 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
419 /* release temporary resources */
425 DeleteObject (ii.hbmColor);
426 DeleteObject (ii.hbmMask);
432 _gdk_win32_cursor_get_image (GdkCursor *cursor)
434 g_return_val_if_fail (cursor != NULL, NULL);
436 return gdk_win32_icon_to_pixbuf_libgtk_only (((GdkWin32Cursor *) cursor)->hcursor);
440 _gdk_win32_display_get_cursor_for_pixbuf (GdkDisplay *display,
447 g_return_val_if_fail (display == _gdk_display, NULL);
448 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
449 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
450 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
452 hcursor = _gdk_win32_pixbuf_to_hcursor (pixbuf, x, y);
455 return cursor_new_from_hcursor (hcursor, GDK_CURSOR_IS_PIXMAP);
459 _gdk_win32_display_supports_cursor_alpha (GdkDisplay *display)
461 g_return_val_if_fail (display == _gdk_display, FALSE);
463 return _gdk_win32_pixbuf_to_hicon_supports_alpha ();
467 _gdk_win32_display_supports_cursor_color (GdkDisplay *display)
469 g_return_val_if_fail (display == _gdk_display, FALSE);
475 _gdk_win32_display_get_default_cursor_size (GdkDisplay *display,
479 g_return_if_fail (display == _gdk_display);
482 *width = GetSystemMetrics (SM_CXCURSOR);
484 *height = GetSystemMetrics (SM_CYCURSOR);
488 _gdk_win32_display_get_maximal_cursor_size (GdkDisplay *display,
492 g_return_if_fail (display == _gdk_display);
495 *width = GetSystemMetrics (SM_CXCURSOR);
497 *height = GetSystemMetrics (SM_CYCURSOR);
501 /* Convert a pixbuf to an HICON (or HCURSOR). Supports alpha under
502 * Windows XP, thresholds alpha otherwise. Also used from
503 * gdkwindow-win32.c for creating application icons.
507 create_alpha_bitmap (gint size,
514 ZeroMemory (&bi, sizeof (BITMAPV5HEADER));
515 bi.bV5Size = sizeof (BITMAPV5HEADER);
516 bi.bV5Height = bi.bV5Width = size;
519 bi.bV5Compression = BI_BITFIELDS;
520 /* The following mask specification specifies a supported 32 BPP
521 * alpha format for Windows XP (BGRA format).
523 bi.bV5RedMask = 0x00FF0000;
524 bi.bV5GreenMask = 0x0000FF00;
525 bi.bV5BlueMask = 0x000000FF;
526 bi.bV5AlphaMask = 0xFF000000;
528 /* Create the DIB section with an alpha channel. */
532 WIN32_GDI_FAILED ("GetDC");
535 hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
536 (PVOID *) outdata, NULL, (DWORD)0);
538 WIN32_GDI_FAILED ("CreateDIBSection");
539 ReleaseDC (NULL, hdc);
545 create_color_bitmap (gint size,
550 BITMAPV4HEADER bmiHeader;
551 RGBQUAD bmiColors[2];
556 ZeroMemory (&bmi, sizeof (bmi));
557 bmi.bmiHeader.bV4Size = sizeof (BITMAPV4HEADER);
558 bmi.bmiHeader.bV4Height = bmi.bmiHeader.bV4Width = size;
559 bmi.bmiHeader.bV4Planes = 1;
560 bmi.bmiHeader.bV4BitCount = bits;
561 bmi.bmiHeader.bV4V4Compression = BI_RGB;
563 /* when bits is 1, these will be used.
564 * bmiColors[0] already zeroed from ZeroMemory()
566 bmi.bmiColors[1].rgbBlue = 0xFF;
567 bmi.bmiColors[1].rgbGreen = 0xFF;
568 bmi.bmiColors[1].rgbRed = 0xFF;
573 WIN32_GDI_FAILED ("GetDC");
576 hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bmi, DIB_RGB_COLORS,
577 (PVOID *) outdata, NULL, (DWORD)0);
579 WIN32_GDI_FAILED ("CreateDIBSection");
580 ReleaseDC (NULL, hdc);
586 pixbuf_to_hbitmaps_alpha_winxp (GdkPixbuf *pixbuf,
590 /* Based on code from
591 * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
593 HBITMAP hColorBitmap, hMaskBitmap;
594 guchar *indata, *inrow;
595 guchar *colordata, *colorrow, *maskdata, *maskbyte;
596 gint width, height, size, i, i_offset, j, j_offset, rowstride;
597 guint maskstride, mask_bit;
599 width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
600 height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
602 /* The bitmaps are created square */
603 size = MAX (width, height);
605 hColorBitmap = create_alpha_bitmap (size, &colordata);
608 hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
611 DeleteObject (hColorBitmap);
615 /* MSDN says mask rows are aligned to "LONG" boundaries */
616 maskstride = (((size + 31) & ~31) >> 3);
618 indata = gdk_pixbuf_get_pixels (pixbuf);
619 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
624 j_offset = (width - height) / 2;
628 i_offset = (height - width) / 2;
632 for (j = 0; j < height; j++)
634 colorrow = colordata + 4*(j+j_offset)*size + 4*i_offset;
635 maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
636 mask_bit = (0x80 >> (i_offset % 8));
637 inrow = indata + (height-j-1)*rowstride;
638 for (i = 0; i < width; i++)
640 colorrow[4*i+0] = inrow[4*i+2];
641 colorrow[4*i+1] = inrow[4*i+1];
642 colorrow[4*i+2] = inrow[4*i+0];
643 colorrow[4*i+3] = inrow[4*i+3];
644 if (inrow[4*i+3] == 0)
645 maskbyte[0] |= mask_bit; /* turn ON bit */
647 maskbyte[0] &= ~mask_bit; /* turn OFF bit */
657 *color = hColorBitmap;
664 pixbuf_to_hbitmaps_normal (GdkPixbuf *pixbuf,
668 /* Based on code from
669 * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
671 HBITMAP hColorBitmap, hMaskBitmap;
672 guchar *indata, *inrow;
673 guchar *colordata, *colorrow, *maskdata, *maskbyte;
674 gint width, height, size, i, i_offset, j, j_offset, rowstride, nc, bmstride;
676 guint maskstride, mask_bit;
678 width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
679 height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
681 /* The bitmaps are created square */
682 size = MAX (width, height);
684 hColorBitmap = create_color_bitmap (size, &colordata, 24);
687 hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
690 DeleteObject (hColorBitmap);
694 /* rows are always aligned on 4-byte boundarys */
696 if (bmstride % 4 != 0)
697 bmstride += 4 - (bmstride % 4);
699 /* MSDN says mask rows are aligned to "LONG" boundaries */
700 maskstride = (((size + 31) & ~31) >> 3);
702 indata = gdk_pixbuf_get_pixels (pixbuf);
703 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
704 nc = gdk_pixbuf_get_n_channels (pixbuf);
705 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
710 j_offset = (width - height) / 2;
714 i_offset = (height - width) / 2;
718 for (j = 0; j < height; j++)
720 colorrow = colordata + (j+j_offset)*bmstride + 3*i_offset;
721 maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
722 mask_bit = (0x80 >> (i_offset % 8));
723 inrow = indata + (height-j-1)*rowstride;
724 for (i = 0; i < width; i++)
726 if (has_alpha && inrow[nc*i+3] < 128)
728 colorrow[3*i+0] = colorrow[3*i+1] = colorrow[3*i+2] = 0;
729 maskbyte[0] |= mask_bit; /* turn ON bit */
733 colorrow[3*i+0] = inrow[nc*i+2];
734 colorrow[3*i+1] = inrow[nc*i+1];
735 colorrow[3*i+2] = inrow[nc*i+0];
736 maskbyte[0] &= ~mask_bit; /* turn OFF bit */
747 *color = hColorBitmap;
754 pixbuf_to_hicon (GdkPixbuf *pixbuf,
766 if (_gdk_win32_pixbuf_to_hicon_supports_alpha() && gdk_pixbuf_get_has_alpha (pixbuf))
767 success = pixbuf_to_hbitmaps_alpha_winxp (pixbuf, &ii.hbmColor, &ii.hbmMask);
769 success = pixbuf_to_hbitmaps_normal (pixbuf, &ii.hbmColor, &ii.hbmMask);
777 icon = CreateIconIndirect (&ii);
778 DeleteObject (ii.hbmColor);
779 DeleteObject (ii.hbmMask);
784 _gdk_win32_pixbuf_to_hicon (GdkPixbuf *pixbuf)
786 return pixbuf_to_hicon (pixbuf, TRUE, 0, 0);
790 _gdk_win32_pixbuf_to_hcursor (GdkPixbuf *pixbuf,
794 return pixbuf_to_hicon (pixbuf, FALSE, x_hotspot, y_hotspot);
798 _gdk_win32_pixbuf_to_hicon_supports_alpha (void)
800 static gboolean is_win_xp=FALSE, is_win_xp_checked=FALSE;
802 if (!is_win_xp_checked)
804 OSVERSIONINFO version;
806 is_win_xp_checked = TRUE;
808 memset (&version, 0, sizeof (version));
809 version.dwOSVersionInfoSize = sizeof (version);
810 is_win_xp = GetVersionEx (&version)
811 && version.dwPlatformId == VER_PLATFORM_WIN32_NT
812 && (version.dwMajorVersion > 5
813 || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
819 gdk_win32_pixbuf_to_hicon_libgtk_only (GdkPixbuf *pixbuf)
821 return _gdk_win32_pixbuf_to_hicon (pixbuf);
825 gdk_win32_cursor_init (GdkWin32Cursor *cursor)
829 gdk_win32_cursor_class_init(GdkWin32CursorClass *klass)
831 GObjectClass *object_class = G_OBJECT_CLASS (klass);
832 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (klass);
834 object_class->finalize = _gdk_win32_cursor_finalize;
836 cursor_class->get_image = _gdk_win32_cursor_get_image;