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"
34 #if (defined(__MINGW32__) && (__W32API_MAJOR_VERSION < 3 || (__W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION < 8))) || (defined(_MSC_VER) && (WINVER < 0x0500))
43 LONG bV5XPelsPerMeter;
44 LONG bV5YPelsPerMeter;
46 DWORD bV5ClrImportant;
52 CIEXYZTRIPLE bV5Endpoints;
64 hcursor_from_type (GdkCursorType cursor_type)
69 guchar *and_plane, *xor_plane;
71 if (cursor_type != GDK_BLANK_CURSOR)
73 for (i = 0; i < G_N_ELEMENTS (cursors); i++)
74 if (cursors[i].type == cursor_type)
77 if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name)
80 /* Use real Win32 cursor if possible */
81 if (cursors[i].builtin)
82 return LoadCursor (NULL, cursors[i].builtin);
85 w = GetSystemMetrics (SM_CXCURSOR);
86 h = GetSystemMetrics (SM_CYCURSOR);
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);
93 if (cursor_type != GDK_BLANK_CURSOR)
96 #define SET_BIT(v,b) (v |= (1 << b))
97 #define RESET_BIT(v,b) (v &= ~(1 << b))
99 for (j = 0, y = 0; y < cursors[i].height && y < h ; y++)
102 j = y * cursors[i].width;
104 for (x = 0; x < cursors[i].width && x < w ; x++, j++)
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;
112 RESET_BIT (and_plane[pofs], bit);
114 SET_BIT (xor_plane[pofs], bit);
122 rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
123 w, h, and_plane, xor_plane);
127 rv = CreateCursor (_gdk_app_hmodule, 0, 0,
128 w, h, and_plane, xor_plane);
131 WIN32_API_FAILED ("CreateCursor");
139 cursor_new_from_hcursor (HCURSOR hcursor,
140 GdkCursorType cursor_type)
142 GdkCursorPrivate *private;
145 private = g_new (GdkCursorPrivate, 1);
146 private->hcursor = hcursor;
147 cursor = (GdkCursor*) private;
148 cursor->type = cursor_type;
149 cursor->ref_count = 1;
155 gdk_cursor_new_for_display (GdkDisplay *display,
156 GdkCursorType cursor_type)
160 g_return_val_if_fail (display == _gdk_display, NULL);
162 hcursor = hcursor_from_type (cursor_type);
165 g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type);
167 GDK_NOTE (CURSOR, g_print ("gdk_cursor_new_for_display: %d: %p\n",
168 cursor_type, hcursor));
170 return cursor_new_from_hcursor (hcursor, cursor_type);
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.
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.
190 } default_cursors[] = {
191 { "appstarting", IDC_APPSTARTING },
192 { "arrow", IDC_ARROW },
193 { "cross", IDC_CROSS },
195 { "hand", IDC_HAND },
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 },
209 gdk_cursor_new_from_name (GdkDisplay *display,
212 HCURSOR hcursor = NULL;
215 g_return_val_if_fail (display == _gdk_display, NULL);
217 for (i = 0; i < G_N_ELEMENTS(default_cursors); i++)
219 if (0 == strcmp(default_cursors[i].name, name))
220 hcursor = LoadCursor (NULL, default_cursors[i].id);
222 /* allow to load named cursor resources linked into the executable */
224 hcursor = LoadCursor (_gdk_app_hmodule, name);
227 return cursor_new_from_hcursor (hcursor, GDK_X_CURSOR);
233 _gdk_cursor_destroy (GdkCursor *cursor)
235 GdkCursorPrivate *private;
237 g_return_if_fail (cursor != NULL);
238 private = (GdkCursorPrivate *) cursor;
240 GDK_NOTE (CURSOR, g_print ("_gdk_cursor_destroy: %p\n",
241 (cursor->type == GDK_CURSOR_IS_PIXMAP) ? private->hcursor : 0));
243 if (GetCursor () == private->hcursor)
246 if (!DestroyCursor (private->hcursor))
247 WIN32_API_FAILED ("DestroyCursor");
253 gdk_cursor_get_display (GdkCursor *cursor)
255 return gdk_display_get_default ();
259 gdk_win32_icon_to_pixbuf_libgtk_only (HICON hicon)
261 GdkPixbuf *pixbuf = NULL;
269 guchar *pixels, *bits;
271 gint rowstride, x, y, w, h;
273 if (!GDI_CALL (GetIconInfo, (hicon, &ii)))
276 if (!(hdc = CreateCompatibleDC (NULL)))
278 WIN32_GDI_FAILED ("CreateCompatibleDC");
282 memset (&bmi, 0, sizeof (bmi));
283 bmi.bi.biSize = sizeof (bmi.bi);
285 if (ii.hbmColor != NULL)
291 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, 1, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
297 bmi.bi.biBitCount = 32;
298 bmi.bi.biCompression = BI_RGB;
299 bmi.bi.biHeight = -h;
301 bits = g_malloc0 (4 * w * h);
304 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
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);
311 for (y = 0; y < h; y++)
313 for (x = 0; x < w; x++)
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)
323 pixels += (w * 4 - rowstride);
328 GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
330 pixels = gdk_pixbuf_get_pixels (pixbuf);
331 for (y = 0; y < h; y++)
333 for (x = 0; x < w; x++)
335 pixels[3] = 255 - bits[(x + y * w) * 4];
338 pixels += (w * 4 - rowstride);
348 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, 0, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
352 h = ABS (bmi.bi.biHeight) / 2;
354 bits = g_malloc0 (4 * w * h);
357 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h*2, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
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;
365 for (y = 0; y < h*2; y++)
367 for (x = 0; x < w; x++)
369 const gint bit = 7 - (x % 8);
370 printf ("%c ", ((bits[bpl*y+x/8])&(1<<bit)) ? ' ' : 'X');
376 for (y = 0; y < h; y++)
378 const guchar *andp, *xorp;
379 if (bmi.bi.biHeight < 0)
382 xorp = bits + bpl*(h+y);
386 andp = bits + bpl*(h-y-1);
387 xorp = bits + bpl*(h+h-y-1);
389 for (x = 0; x < w; x++)
391 const gint bit = 7 - (x % 8);
392 if ((*andp) & (1<<bit))
394 if ((*xorp) & (1<<bit))
395 pixels[2] = pixels[1] = pixels[0] = 0xFF;
397 pixels[2] = pixels[1] = pixels[0] = 0;
402 pixels[2] = pixels[1] = pixels[0] = 0;
412 pixels += (w * 4 - rowstride);
416 g_snprintf (buf, sizeof (buf), "%ld", ii.xHotspot);
417 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
419 g_snprintf (buf, sizeof (buf), "%ld", ii.yHotspot);
420 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
422 /* release temporary resources */
428 DeleteObject (ii.hbmColor);
429 DeleteObject (ii.hbmMask);
435 gdk_cursor_get_image (GdkCursor *cursor)
437 g_return_val_if_fail (cursor != NULL, NULL);
439 return gdk_win32_icon_to_pixbuf_libgtk_only (((GdkCursorPrivate *) cursor)->hcursor);
443 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
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);
455 hcursor = _gdk_win32_pixbuf_to_hcursor (pixbuf, x, y);
458 return cursor_new_from_hcursor (hcursor, GDK_CURSOR_IS_PIXMAP);
462 gdk_display_supports_cursor_alpha (GdkDisplay *display)
464 g_return_val_if_fail (display == _gdk_display, FALSE);
466 return _gdk_win32_pixbuf_to_hicon_supports_alpha ();
470 gdk_display_supports_cursor_color (GdkDisplay *display)
472 g_return_val_if_fail (display == _gdk_display, FALSE);
478 gdk_display_get_default_cursor_size (GdkDisplay *display)
480 g_return_val_if_fail (display == _gdk_display, 0);
482 return MIN (GetSystemMetrics (SM_CXCURSOR), GetSystemMetrics (SM_CYCURSOR));
486 gdk_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);