Debian Codegraph @

gdk/win32/gdkcursor-win32.c

raw
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 "gdkwin32.h"
25
26 #include "gdkdisplay-win32.h"
27
28 #ifdef __MINGW32__
29 #include <w32api.h>
30 #endif
31
32 #include "xcursors.h"
33
34 typedef struct _DefaultCursor {
35 char *name;
36 char *id;
37 } DefaultCursor;
38
39 static DefaultCursor default_cursors[] = {
40 { "appstarting", IDC_APPSTARTING },
41 { "arrow", IDC_ARROW },
42 { "cross", IDC_CROSS },
43 { "dnd-move", IDC_ARROW },
44 { "hand", IDC_HAND },
45 { "help", IDC_HELP },
46 { "ibeam", IDC_IBEAM },
47 /* an X cursor name, for compatibility with GTK: */
48 { "left_ptr_watch", IDC_APPSTARTING },
49 { "sizeall", IDC_SIZEALL },
50 { "sizenesw", IDC_SIZENESW },
51 { "sizens", IDC_SIZENS },
52 { "sizenwse", IDC_SIZENWSE },
53 { "sizewe", IDC_SIZEWE },
54 { "uparrow", IDC_UPARROW },
55 { "wait", IDC_WAIT },
56 /* css cursor names: */
57 { "default", IDC_ARROW },
58 { "pointer", IDC_HAND },
59 { "progress", IDC_APPSTARTING },
60 { "crosshair", IDC_CROSS },
61 { "text", IDC_IBEAM },
62 { "move", IDC_SIZEALL },
63 { "not-allowed", IDC_NO },
64 { "ew-resize", IDC_SIZEWE },
65 { "e-resize", IDC_SIZEWE },
66 { "w-resize", IDC_SIZEWE },
67 { "col-resize", IDC_SIZEWE },
68 { "ns-resize", IDC_SIZENS },
69 { "n-resize", IDC_SIZENS },
70 { "s-resize", IDC_SIZENS },
71 { "row-resize", IDC_SIZENS },
72 { "nesw-resize", IDC_SIZENESW },
73 { "ne-resize", IDC_SIZENESW },
74 { "sw-resize", IDC_SIZENESW },
75 { "nwse-resize", IDC_SIZENWSE },
76 { "nw-resize", IDC_SIZENWSE },
77 { "se-resize", IDC_SIZENWSE }
78 };
79
80 static HCURSOR
81 hcursor_from_x_cursor (gint i,
82 GdkCursorType cursor_type)
83 {
84 gint j, x, y, ofs;
85 HCURSOR rv;
86 gint w, h;
87 guchar *and_plane, *xor_plane;
88
89 w = GetSystemMetrics (SM_CXCURSOR);
90 h = GetSystemMetrics (SM_CYCURSOR);
91
92 and_plane = g_malloc ((w/8) * h);
93 memset (and_plane, 0xff, (w/8) * h);
94 xor_plane = g_malloc ((w/8) * h);
95 memset (xor_plane, 0, (w/8) * h);
96
97 if (cursor_type != GDK_BLANK_CURSOR)
98 {
99
100 #define SET_BIT(v,b) (v |= (1 << b))
101 #define RESET_BIT(v,b) (v &= ~(1 << b))
102
103 for (j = 0, y = 0; y < cursors[i].height && y < h ; y++)
104 {
105 ofs = (y * w) / 8;
106 j = y * cursors[i].width;
107
108 for (x = 0; x < cursors[i].width && x < w ; x++, j++)
109 {
110 gint pofs = ofs + x / 8;
111 guchar data = (cursors[i].data[j/4] & (0xc0 >> (2 * (j%4)))) >> (2 * (3 - (j%4)));
112 gint bit = 7 - (j % cursors[i].width) % 8;
113
114 if (data)
115 {
116 RESET_BIT (and_plane[pofs], bit);
117
118 if (data == 1)
119 SET_BIT (xor_plane[pofs], bit);
120 }
121 }
122 }
123
124 #undef SET_BIT
125 #undef RESET_BIT
126
127 rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
128 w, h, and_plane, xor_plane);
129 }
130 else
131 {
132 rv = CreateCursor (_gdk_app_hmodule, 0, 0,
133 w, h, and_plane, xor_plane);
134 }
135
136 if (rv == NULL)
137 WIN32_API_FAILED ("CreateCursor");
138
139 g_free (and_plane);
140 g_free (xor_plane);
141
142 return rv;
143 }
144
145 static HCURSOR
146 win32_cursor_create_hcursor (Win32Cursor *cursor)
147 {
148 HCURSOR result;
149
150 switch (cursor->load_type)
151 {
152 case GDK_WIN32_CURSOR_LOAD_FROM_FILE:
153 result = LoadImageW (NULL,
154 cursor->resource_name,
155 IMAGE_CURSOR,
156 cursor->width,
157 cursor->height,
158 cursor->load_flags);
159 break;
160 case GDK_WIN32_CURSOR_LOAD_FROM_RESOURCE_NULL:
161 result = LoadImageA (NULL,
162 (const gchar *) cursor->resource_name,
163 IMAGE_CURSOR,
164 cursor->width,
165 cursor->height,
166 cursor->load_flags);
167 break;
168 case GDK_WIN32_CURSOR_LOAD_FROM_RESOURCE_THIS:
169 result = LoadImageA (_gdk_app_hmodule,
170 (const gchar *) cursor->resource_name,
171 IMAGE_CURSOR,
172 cursor->width,
173 cursor->height,
174 cursor->load_flags);
175 break;
176 case GDK_WIN32_CURSOR_CREATE:
177 result = hcursor_from_x_cursor (cursor->xcursor_number,
178 cursor->cursor_type);
179 break;
180 default:
181 result = NULL;
182 }
183
184 return result;
185 }
186
187 static Win32Cursor *
188 win32_cursor_new (GdkWin32CursorLoadType load_type,
189 gpointer resource_name,
190 gint width,
191 gint height,
192 guint load_flags,
193 gint xcursor_number,
194 GdkCursorType cursor_type)
195 {
196 Win32Cursor *result;
197
198 result = g_new (Win32Cursor, 1);
199 result->load_type = load_type;
200 result->resource_name = resource_name;
201 result->width = width;
202 result->height = height;
203 result->load_flags = load_flags;
204 result->xcursor_number = xcursor_number;
205 result->cursor_type = cursor_type;
206
207 return result;
208 }
209
210
211 static void
212 win32_cursor_destroy (gpointer data)
213 {
214 Win32Cursor *cursor = data;
215
216 /* resource_name could be a resource ID (uint16_t stored as a pointer),
217 * which shouldn't be freed.
218 */
219 if (cursor->load_type == GDK_WIN32_CURSOR_LOAD_FROM_FILE)
220 g_free (cursor->resource_name);
221
222 g_free (cursor);
223 }
224
225 static void
226 win32_cursor_theme_load_from (Win32CursorTheme *theme,
227 gint size,
228 const gchar *dir)
229 {
230 GDir *gdir;
231 const gchar *filename;
232 HCURSOR hcursor;
233
234 gdir = g_dir_open (dir, 0, NULL);
235
236 if (gdir == NULL)
237 return;
238
239 while ((filename = g_dir_read_name (gdir)) != NULL)
240 {
241 gchar *fullname;
242 gunichar2 *filenamew;
243 gchar *cursor_name;
244 gchar *dot;
245 Win32Cursor *cursor;
246
247 fullname = g_strconcat (dir, "/", filename, NULL);
248 filenamew = g_utf8_to_utf16 (fullname, -1, NULL, NULL, NULL);
249 g_free (fullname);
250
251 if (filenamew == NULL)
252 continue;
253
254 hcursor = LoadImageW (NULL, filenamew, IMAGE_CURSOR, size, size,
255 LR_LOADFROMFILE | (size == 0 ? LR_DEFAULTSIZE : 0));
256
257 if (hcursor == NULL)
258 {
259 g_free (filenamew);
260 continue;
261 }
262
263 DestroyCursor (hcursor);
264 dot = strchr (filename, '.');
265
266 cursor_name = dot ? g_strndup (filename, dot - filename) : g_strdup (filename);
267
268 cursor = win32_cursor_new (GDK_WIN32_CURSOR_LOAD_FROM_FILE,
269 filenamew,
270 size,
271 size,
272 LR_LOADFROMFILE | (size == 0 ? LR_DEFAULTSIZE : 0),
273 0,
274 0);
275 g_hash_table_insert (theme->named_cursors, cursor_name, cursor);
276 }
277 }
278
279 static void
280 win32_cursor_theme_load_from_dirs (Win32CursorTheme *theme,
281 const gchar *name,
282 gint size)
283 {
284 gchar *theme_dir;
285 const gchar * const *dirs;
286 gint i;
287
288 dirs = g_get_system_data_dirs ();
289
290 /* <prefix>/share/icons */
291 for (i = 0; dirs[i]; i++)
292 {
293 theme_dir = g_strconcat (dirs[i], "/icons/", name, "/cursors", NULL);
294 win32_cursor_theme_load_from (theme, size, theme_dir);
295 g_free (theme_dir);
296 }
297
298 /* ~/.icons */
299 theme_dir = g_strconcat (g_get_home_dir (), "/icons/", name, "/cursors", NULL);
300 win32_cursor_theme_load_from (theme, size, theme_dir);
301 g_free (theme_dir);
302 }
303
304 static void
305 win32_cursor_theme_load_system (Win32CursorTheme *theme,
306 gint size)
307 {
308 gint i;
309 HCURSOR hcursor;
310 Win32Cursor *cursor;
311
312 for (i = 0; i < G_N_ELEMENTS (cursors); i++)
313 {
314
315 if (cursors[i].name == NULL)
316 break;
317
318 hcursor = NULL;
319
320 /* Prefer W32 cursors */
321 if (cursors[i].builtin)
322 hcursor = LoadImageA (NULL, cursors[i].builtin, IMAGE_CURSOR,
323 size, size,
324 LR_SHARED | (size == 0 ? LR_DEFAULTSIZE : 0));
325
326 /* Fall back to X cursors, but only if we've got no theme cursor */
327 if (hcursor == NULL && g_hash_table_lookup (theme->named_cursors, cursors[i].name) == NULL)
328 hcursor = hcursor_from_x_cursor (i, cursors[i].type);
329
330 if (hcursor == NULL)
331 continue;
332
333 DestroyCursor (hcursor);
334 cursor = win32_cursor_new (GDK_WIN32_CURSOR_LOAD_FROM_RESOURCE_NULL,
335 (gpointer) cursors[i].builtin,
336 size,
337 size,
338 LR_SHARED | (size == 0 ? LR_DEFAULTSIZE : 0),
339 0,
340 0);
341 g_hash_table_insert (theme->named_cursors,
342 g_strdup (cursors[i].name),
343 cursor);
344 }
345
346 for (i = 0; i < G_N_ELEMENTS (default_cursors); i++)
347 {
348 if (default_cursors[i].name == NULL)
349 break;
350
351 hcursor = LoadImageA (NULL, default_cursors[i].id, IMAGE_CURSOR, size, size,
352 LR_SHARED | (size == 0 ? LR_DEFAULTSIZE : 0));
353
354 if (hcursor == NULL)
355 continue;
356
357 DestroyCursor (hcursor);
358 cursor = win32_cursor_new (GDK_WIN32_CURSOR_LOAD_FROM_RESOURCE_NULL,
359 (gpointer) default_cursors[i].id,
360 size,
361 size,
362 LR_SHARED | (size == 0 ? LR_DEFAULTSIZE : 0),
363 0,
364 0);
365 g_hash_table_insert (theme->named_cursors,
366 g_strdup (default_cursors[i].name),
367 cursor);
368 }
369 }
370
371 Win32CursorTheme *
372 win32_cursor_theme_load (const gchar *name,
373 gint size)
374 {
375 Win32CursorTheme *result = g_new0 (Win32CursorTheme, 1);
376
377 result->named_cursors = g_hash_table_new_full (g_str_hash,
378 g_str_equal,
379 g_free,
380 win32_cursor_destroy);
381
382 if (strcmp (name, "system") == 0)
383 {
384 win32_cursor_theme_load_from_dirs (result, "Adwaita", size);
385 win32_cursor_theme_load_system (result, size);
386 }
387 else
388 {
389 win32_cursor_theme_load_from_dirs (result, name, size);
390 }
391
392 if (g_hash_table_size (result->named_cursors) > 0)
393 return result;
394
395 win32_cursor_theme_destroy (result);
396 return NULL;
397 }
398
399 void
400 win32_cursor_theme_destroy (Win32CursorTheme *theme)
401 {
402 g_hash_table_destroy (theme->named_cursors);
403 g_free (theme);
404 }
405
406 Win32Cursor *
407 win32_cursor_theme_get_cursor (Win32CursorTheme *theme,
408 const gchar *name)
409 {
410 return g_hash_table_lookup (theme->named_cursors, name);
411 }
412
413 static HCURSOR
414 hcursor_from_type (GdkCursorType cursor_type)
415 {
416 gint i = 0;
417
418 if (cursor_type != GDK_BLANK_CURSOR)
419 {
420 for (i = 0; i < G_N_ELEMENTS (cursors); i++)
421 if (cursors[i].type == cursor_type)
422 break;
423
424 if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name)
425 return NULL;
426
427 /* Use real Win32 cursor if possible */
428 if (cursors[i].builtin)
429 return LoadImageA (NULL, cursors[i].builtin, IMAGE_CURSOR, 0, 0,
430 LR_SHARED | LR_DEFAULTSIZE);
431 }
432
433 return hcursor_from_x_cursor (i, cursor_type);
434 }
435
436 struct _GdkWin32CursorClass
437 {
438 GdkCursorClass cursor_class;
439 };
440
441 G_DEFINE_TYPE (GdkWin32Cursor, gdk_win32_cursor, GDK_TYPE_CURSOR)
442
443 static void
444 _gdk_win32_cursor_finalize (GObject *object)
445 {
446 GdkWin32Cursor *private = GDK_WIN32_CURSOR (object);
447
448 if (GetCursor () == private->hcursor)
449 SetCursor (NULL);
450
451 if (!DestroyCursor (private->hcursor))
452 g_warning (G_STRLOC ": DestroyCursor (%p) failed: %lu", private->hcursor, GetLastError ());
453
454 g_free (private->name);
455
456 G_OBJECT_CLASS (gdk_win32_cursor_parent_class)->finalize (object);
457 }
458
459 static HCURSOR
460 hcursor_idc_from_name (const gchar *name)
461 {
462 int i;
463
464 for (i = 0; i < G_N_ELEMENTS (default_cursors); i++)
465 {
466 if (strcmp (default_cursors[i].name, name) != 0)
467 continue;
468
469 return LoadImageA (NULL, default_cursors[i].id, IMAGE_CURSOR, 0, 0,
470 LR_SHARED | LR_DEFAULTSIZE);
471 }
472
473 return NULL;
474 }
475
476 static HCURSOR
477 hcursor_x_from_name (const gchar *name)
478 {
479 gint i;
480
481 for (i = 0; i < G_N_ELEMENTS (cursors); i++)
482 if (cursors[i].name == NULL || strcmp (cursors[i].name, name) == 0)
483 return hcursor_from_x_cursor (i, cursors[i].type);
484
485 return NULL;
486 }
487
488 static HCURSOR
489 hcursor_from_theme (GdkDisplay *display,
490 const gchar *name)
491 {
492 Win32CursorTheme *theme;
493 Win32Cursor *theme_cursor;
494 GdkWin32Display *win32_display = GDK_WIN32_DISPLAY (display);
495
496 if (name == NULL)
497 return NULL;
498
499 theme = _gdk_win32_display_get_cursor_theme (win32_display);
500 theme_cursor = win32_cursor_theme_get_cursor (theme, name);
501
502 if (theme_cursor == NULL)
503 return NULL;
504
505 return win32_cursor_create_hcursor (theme_cursor);
506 }
507
508 static HCURSOR
509 hcursor_from_name (GdkDisplay *display,
510 const gchar *name)
511 {
512 HCURSOR hcursor;
513
514 if (strcmp (name, "none") == 0)
515 return hcursor_from_type (GDK_BLANK_CURSOR);
516
517 /* Try current theme first */
518 hcursor = hcursor_from_theme (display, name);
519
520 if (hcursor != NULL)
521 return hcursor;
522
523 hcursor = hcursor_idc_from_name (name);
524
525 if (hcursor != NULL)
526 return hcursor;
527
528 hcursor = hcursor_x_from_name (name);
529
530 return hcursor;
531 }
532
533 static GdkCursor*
534 cursor_new_from_hcursor (GdkDisplay *display,
535 HCURSOR hcursor,
536 const gchar *name,
537 GdkCursorType cursor_type)
538 {
539 GdkWin32Cursor *private;
540 GdkCursor *cursor;
541
542 private = g_object_new (GDK_TYPE_WIN32_CURSOR,
543 "cursor-type", cursor_type,
544 "display", display,
545 NULL);
546
547 private->name = g_strdup (name);
548
549 private->hcursor = hcursor;
550 cursor = (GdkCursor*) private;
551
552 return cursor;
553 }
554
555 static gboolean
556 _gdk_win32_cursor_update (GdkWin32Display *win32_display,
557 GdkWin32Cursor *cursor)
558 {
559 HCURSOR hcursor = NULL;
560 Win32CursorTheme *theme;
561 Win32Cursor *theme_cursor;
562
563 /* Do nothing if this is not a named cursor. */
564 if (cursor->name == NULL)
565 return FALSE;
566
567 theme = _gdk_win32_display_get_cursor_theme (win32_display);
568 theme_cursor = win32_cursor_theme_get_cursor (theme, cursor->name);
569
570 if (theme_cursor != NULL)
571 hcursor = win32_cursor_create_hcursor (theme_cursor);
572
573 if (hcursor == NULL)
574 {
575 g_warning (G_STRLOC ": Unable to load %s from the cursor theme", cursor->name);
576
577 hcursor = hcursor_idc_from_name (cursor->name);
578
579 if (hcursor == NULL)
580 hcursor = hcursor_x_from_name (cursor->name);
581
582 if (hcursor == NULL)
583 return FALSE;
584 }
585
586 if (GetCursor () == cursor->hcursor)
587 SetCursor (hcursor);
588
589 if (!DestroyCursor (cursor->hcursor))
590 g_warning (G_STRLOC ": DestroyCursor (%p) failed: %lu", cursor->hcursor, GetLastError ());
591
592 cursor->hcursor = hcursor;
593
594 return TRUE;
595 }
596
597 void
598 _gdk_win32_display_update_cursors (GdkWin32Display *display)
599 {
600 GHashTableIter iter;
601 const char *name;
602 GdkWin32Cursor *cursor;
603
604 g_hash_table_iter_init (&iter, display->cursor_cache);
605
606 while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer *) &cursor))
607 _gdk_win32_cursor_update (display, cursor);
608 }
609
610 void
611 _gdk_win32_display_init_cursors (GdkWin32Display *display)
612 {
613 display->cursor_cache = g_hash_table_new_full (g_str_hash,
614 g_str_equal,
615 NULL,
616 g_object_unref);
617 display->cursor_theme_name = g_strdup ("system");
618 }
619
620 void
621 _gdk_win32_display_finalize_cursors (GdkWin32Display *display)
622 {
623 g_free (display->cursor_theme_name);
624
625 if (display->cursor_theme)
626 win32_cursor_theme_destroy (display->cursor_theme);
627
628 g_hash_table_destroy (display->cursor_cache);
629 }
630
631
632 GdkCursor*
633 _gdk_win32_display_get_cursor_for_type (GdkDisplay *display,
634 GdkCursorType cursor_type)
635 {
636 GEnumClass *enum_class;
637 GEnumValue *enum_value;
638 gchar *cursor_name;
639 HCURSOR hcursor;
640 GdkCursor *result;
641 GdkWin32Display *win32_display = GDK_WIN32_DISPLAY (display);
642
643 enum_class = g_type_class_ref (GDK_TYPE_CURSOR_TYPE);
644 enum_value = g_enum_get_value (enum_class, cursor_type);
645 cursor_name = g_strdup (enum_value->value_nick);
646 g_strdelimit (cursor_name, "-", '_');
647 g_type_class_unref (enum_class);
648
649 result = g_hash_table_lookup (win32_display->cursor_cache, cursor_name);
650 if (result)
651 {
652 g_free (cursor_name);
653 return g_object_ref (result);
654 }
655
656 hcursor = hcursor_from_name (display, cursor_name);
657
658 if (hcursor == NULL)
659 hcursor = hcursor_from_type (cursor_type);
660
661 if (hcursor == NULL)
662 g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type);
663 else
664 GDK_NOTE (CURSOR, g_print ("gdk_cursor_new_for_display: %d: %p\n",
665 cursor_type, hcursor));
666
667 result = cursor_new_from_hcursor (display, hcursor, cursor_name, cursor_type);
668
669 if (result == NULL)
670 return result;
671
672 /* Blank cursor case */
673 if (cursor_type == GDK_BLANK_CURSOR ||
674 !cursor_name ||
675 g_str_equal (cursor_name, "none") ||
676 g_str_equal (cursor_name, "blank_cursor"))
677 {
678 g_free (cursor_name);
679 return result;
680 }
681
682 g_hash_table_insert (win32_display->cursor_cache,
683 cursor_name,
684 g_object_ref (result));
685
686 return result;
687 }
688
689 GdkCursor*
690 _gdk_win32_display_get_cursor_for_name (GdkDisplay *display,
691 const gchar *name)
692 {
693 HCURSOR hcursor = NULL;
694 GdkCursor *result;
695 GdkWin32Display *win32_display = GDK_WIN32_DISPLAY (display);
696
697 result = g_hash_table_lookup (win32_display->cursor_cache, name);
698 if (result)
699 return g_object_ref (result);
700
701 hcursor = hcursor_from_name (display, name);
702
703 /* allow to load named cursor resources linked into the executable */
704 if (!hcursor)
705 hcursor = LoadCursor (_gdk_app_hmodule, name);
706
707 if (hcursor == NULL)
708 return NULL;
709
710 result = cursor_new_from_hcursor (display, hcursor, name, GDK_X_CURSOR);
711
712 /* Blank cursor case */
713 if (!name ||
714 g_str_equal (name, "none") ||
715 g_str_equal (name, "blank_cursor"))
716 return result;
717
718 g_hash_table_insert (win32_display->cursor_cache,
719 g_strdup (name),
720 g_object_ref (result));
721
722 return result;
723 }
724
725 GdkPixbuf *
726 gdk_win32_icon_to_pixbuf_libgtk_only (HICON hicon,
727 gdouble *x_hot,
728 gdouble *y_hot)
729 {
730 GdkPixbuf *pixbuf = NULL;
731 ICONINFO ii;
732 struct
733 {
734 BITMAPINFOHEADER bi;
735 RGBQUAD colors[2];
736 } bmi;
737 HDC hdc;
738 guchar *pixels, *bits;
739 gint rowstride, x, y, w, h;
740
741 if (!GDI_CALL (GetIconInfo, (hicon, &ii)))
742 return NULL;
743
744 if (!(hdc = CreateCompatibleDC (NULL)))
745 {
746 WIN32_GDI_FAILED ("CreateCompatibleDC");
747 goto out0;
748 }
749
750 memset (&bmi, 0, sizeof (bmi));
751 bmi.bi.biSize = sizeof (bmi.bi);
752
753 if (ii.hbmColor != NULL)
754 {
755 /* Colour cursor */
756
757 gboolean no_alpha;
758
759 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, 1, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
760 goto out1;
761
762 w = bmi.bi.biWidth;
763 h = bmi.bi.biHeight;
764
765 bmi.bi.biBitCount = 32;
766 bmi.bi.biCompression = BI_RGB;
767 bmi.bi.biHeight = -h;
768
769 bits = g_malloc0 (4 * w * h);
770
771 /* color data */
772 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
773 goto out2;
774
775 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
776 pixels = gdk_pixbuf_get_pixels (pixbuf);
777 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
778 no_alpha = TRUE;
779 for (y = 0; y < h; y++)
780 {
781 for (x = 0; x < w; x++)
782 {
783 pixels[2] = bits[(x+y*w) * 4];
784 pixels[1] = bits[(x+y*w) * 4 + 1];
785 pixels[0] = bits[(x+y*w) * 4 + 2];
786 pixels[3] = bits[(x+y*w) * 4 + 3];
787 if (no_alpha && pixels[3] > 0)
788 no_alpha = FALSE;
789 pixels += 4;
790 }
791 pixels += (w * 4 - rowstride);
792 }
793
794 /* mask */
795 if (no_alpha &&
796 GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
797 {
798 pixels = gdk_pixbuf_get_pixels (pixbuf);
799 for (y = 0; y < h; y++)
800 {
801 for (x = 0; x < w; x++)
802 {
803 pixels[3] = 255 - bits[(x + y * w) * 4];
804 pixels += 4;
805 }
806 pixels += (w * 4 - rowstride);
807 }
808 }
809 }
810 else
811 {
812 /* B&W cursor */
813
814 int bpl;
815
816 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, 0, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
817 goto out1;
818
819 w = bmi.bi.biWidth;
820 h = ABS (bmi.bi.biHeight) / 2;
821
822 bits = g_malloc0 (4 * w * h);
823
824 /* masks */
825 if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h*2, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS)))
826 goto out2;
827
828 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
829 pixels = gdk_pixbuf_get_pixels (pixbuf);
830 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
831 bpl = ((w-1)/32 + 1)*4;
832 #if 0
833 for (y = 0; y < h*2; y++)
834 {
835 for (x = 0; x < w; x++)
836 {
837 const gint bit = 7 - (x % 8);
838 printf ("%c ", ((bits[bpl*y+x/8])&(1<<bit)) ? ' ' : 'X');
839 }
840 printf ("\n");
841 }
842 #endif
843
844 for (y = 0; y < h; y++)
845 {
846 const guchar *andp, *xorp;
847 if (bmi.bi.biHeight < 0)
848 {
849 andp = bits + bpl*y;
850 xorp = bits + bpl*(h+y);
851 }
852 else
853 {
854 andp = bits + bpl*(h-y-1);
855 xorp = bits + bpl*(h+h-y-1);
856 }
857 for (x = 0; x < w; x++)
858 {
859 const gint bit = 7 - (x % 8);
860 if ((*andp) & (1<<bit))
861 {
862 if ((*xorp) & (1<<bit))
863 pixels[2] = pixels[1] = pixels[0] = 0xFF;
864 else
865 pixels[2] = pixels[1] = pixels[0] = 0;
866 pixels[3] = 0xFF;
867 }
868 else
869 {
870 pixels[2] = pixels[1] = pixels[0] = 0;
871 pixels[3] = 0;
872 }
873 pixels += 4;
874 if (bit == 0)
875 {
876 andp++;
877 xorp++;
878 }
879 }
880 pixels += (w * 4 - rowstride);
881 }
882 }
883
884 if (x_hot)
885 *x_hot = ii.xHotspot;
886 if (y_hot)
887 *y_hot = ii.yHotspot;
888
889 /* release temporary resources */
890 out2:
891 g_free (bits);
892 out1:
893 DeleteDC (hdc);
894 out0:
895 DeleteObject (ii.hbmColor);
896 DeleteObject (ii.hbmMask);
897
898 return pixbuf;
899 }
900
901 static cairo_surface_t *
902 _gdk_win32_cursor_get_surface (GdkCursor *cursor,
903 gdouble *x_hot,
904 gdouble *y_hot)
905 {
906 GdkPixbuf *pixbuf;
907 cairo_surface_t *surface;
908
909 g_return_val_if_fail (cursor != NULL, NULL);
910
911 pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (((GdkWin32Cursor *) cursor)->hcursor, x_hot, y_hot);
912
913 if (pixbuf == NULL)
914 return NULL;
915
916 surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, 1, NULL);
917 g_object_unref (pixbuf);
918
919 return surface;
920 }
921
922 GdkCursor *
923 _gdk_win32_display_get_cursor_for_surface (GdkDisplay *display,
924 cairo_surface_t *surface,
925 gdouble x,
926 gdouble y)
927 {
928 HCURSOR hcursor;
929 GdkPixbuf *pixbuf;
930 gint width, height;
931
932 g_return_val_if_fail (surface != NULL, NULL);
933
934 width = cairo_image_surface_get_width (surface);
935 height = cairo_image_surface_get_height (surface);
936 pixbuf = gdk_pixbuf_get_from_surface (surface,
937 0,
938 0,
939 width,
940 height);
941
942 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
943 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
944 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
945
946 hcursor = _gdk_win32_pixbuf_to_hcursor (pixbuf, x, y);
947
948 g_object_unref (pixbuf);
949 if (!hcursor)
950 return NULL;
951 return cursor_new_from_hcursor (display, hcursor, NULL, GDK_CURSOR_IS_PIXMAP);
952 }
953
954 gboolean
955 _gdk_win32_display_supports_cursor_alpha (GdkDisplay *display)
956 {
957 return TRUE;
958 }
959
960 gboolean
961 _gdk_win32_display_supports_cursor_color (GdkDisplay *display)
962 {
963 return TRUE;
964 }
965
966 void
967 _gdk_win32_display_get_default_cursor_size (GdkDisplay *display,
968 guint *width,
969 guint *height)
970 {
971 /* TODO: Use per-monitor DPI functions (8.1 and newer) or
972 * calculate DPI ourselves and use that, assuming that 72 dpi
973 * corresponds to 32x32 cursors. Take into account that DPI
974 * can be artificially increased by the user to make stuff bigger.
975 */
976
977 if (width)
978 *width = GetSystemMetrics (SM_CXCURSOR);
979 if (height)
980 *height = GetSystemMetrics (SM_CYCURSOR);
981 }
982
983 void
984 _gdk_win32_display_get_maximal_cursor_size (GdkDisplay *display,
985 guint *width,
986 guint *height)
987 {
988 if (width)
989 *width = GetSystemMetrics (SM_CXCURSOR);
990 if (height)
991 *height = GetSystemMetrics (SM_CYCURSOR);
992 }
993
994
995 /* Convert a pixbuf to an HICON (or HCURSOR). Supports alpha under
996 * Windows XP, thresholds alpha otherwise. Also used from
997 * gdkwindow-win32.c for creating application icons.
998 */
999
1000 static HBITMAP
1001 create_alpha_bitmap (gint size,
1002 guchar **outdata)
1003 {
1004 BITMAPV5HEADER bi;
1005 HDC hdc;
1006 HBITMAP hBitmap;
1007
1008 ZeroMemory (&bi, sizeof (BITMAPV5HEADER));
1009 bi.bV5Size = sizeof (BITMAPV5HEADER);
1010 bi.bV5Height = bi.bV5Width = size;
1011 bi.bV5Planes = 1;
1012 bi.bV5BitCount = 32;
1013 bi.bV5Compression = BI_BITFIELDS;
1014 /* The following mask specification specifies a supported 32 BPP
1015 * alpha format for Windows XP (BGRA format).
1016 */
1017 bi.bV5RedMask = 0x00FF0000;
1018 bi.bV5GreenMask = 0x0000FF00;
1019 bi.bV5BlueMask = 0x000000FF;
1020 bi.bV5AlphaMask = 0xFF000000;
1021
1022 /* Create the DIB section with an alpha channel. */
1023 hdc = GetDC (NULL);
1024 if (!hdc)
1025 {
1026 WIN32_GDI_FAILED ("GetDC");
1027 return NULL;
1028 }
1029 hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
1030 (PVOID *) outdata, NULL, (DWORD)0);
1031 if (hBitmap == NULL)
1032 WIN32_GDI_FAILED ("CreateDIBSection");
1033 ReleaseDC (NULL, hdc);
1034
1035 return hBitmap;
1036 }
1037
1038 static HBITMAP
1039 create_color_bitmap (gint size,
1040 guchar **outdata,
1041 gint bits)
1042 {
1043 struct {
1044 BITMAPV4HEADER bmiHeader;
1045 RGBQUAD bmiColors[2];
1046 } bmi;
1047 HDC hdc;
1048 HBITMAP hBitmap;
1049
1050 ZeroMemory (&bmi, sizeof (bmi));
1051 bmi.bmiHeader.bV4Size = sizeof (BITMAPV4HEADER);
1052 bmi.bmiHeader.bV4Height = bmi.bmiHeader.bV4Width = size;
1053 bmi.bmiHeader.bV4Planes = 1;
1054 bmi.bmiHeader.bV4BitCount = bits;
1055 bmi.bmiHeader.bV4V4Compression = BI_RGB;
1056
1057 /* when bits is 1, these will be used.
1058 * bmiColors[0] already zeroed from ZeroMemory()
1059 */
1060 bmi.bmiColors[1].rgbBlue = 0xFF;
1061 bmi.bmiColors[1].rgbGreen = 0xFF;
1062 bmi.bmiColors[1].rgbRed = 0xFF;
1063
1064 hdc = GetDC (NULL);
1065 if (!hdc)
1066 {
1067 WIN32_GDI_FAILED ("GetDC");
1068 return NULL;
1069 }
1070 hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bmi, DIB_RGB_COLORS,
1071 (PVOID *) outdata, NULL, (DWORD)0);
1072 if (hBitmap == NULL)
1073 WIN32_GDI_FAILED ("CreateDIBSection");
1074 ReleaseDC (NULL, hdc);
1075
1076 return hBitmap;
1077 }
1078
1079 static gboolean
1080 pixbuf_to_hbitmaps_alpha_winxp (GdkPixbuf *pixbuf,
1081 HBITMAP *color,
1082 HBITMAP *mask)
1083 {
1084 /* Based on code from
1085 * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
1086 */
1087 HBITMAP hColorBitmap, hMaskBitmap;
1088 guchar *indata, *inrow;
1089 guchar *colordata, *colorrow, *maskdata, *maskbyte;
1090 gint width, height, size, i, i_offset, j, j_offset, rowstride;
1091 guint maskstride, mask_bit;
1092
1093 width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
1094 height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
1095
1096 /* The bitmaps are created square */
1097 size = MAX (width, height);
1098
1099 hColorBitmap = create_alpha_bitmap (size, &colordata);
1100 if (!hColorBitmap)
1101 return FALSE;
1102 hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
1103 if (!hMaskBitmap)
1104 {
1105 DeleteObject (hColorBitmap);
1106 return FALSE;
1107 }
1108
1109 /* MSDN says mask rows are aligned to "LONG" boundaries */
1110 maskstride = (((size + 31) & ~31) >> 3);
1111
1112 indata = gdk_pixbuf_get_pixels (pixbuf);
1113 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1114
1115 if (width > height)
1116 {
1117 i_offset = 0;
1118 j_offset = (width - height) / 2;
1119 }
1120 else
1121 {
1122 i_offset = (height - width) / 2;
1123 j_offset = 0;
1124 }
1125
1126 for (j = 0; j < height; j++)
1127 {
1128 colorrow = colordata + 4*(j+j_offset)*size + 4*i_offset;
1129 maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
1130 mask_bit = (0x80 >> (i_offset % 8));
1131 inrow = indata + (height-j-1)*rowstride;
1132 for (i = 0; i < width; i++)
1133 {
1134 colorrow[4*i+0] = inrow[4*i+2];
1135 colorrow[4*i+1] = inrow[4*i+1];
1136 colorrow[4*i+2] = inrow[4*i+0];
1137 colorrow[4*i+3] = inrow[4*i+3];
1138 if (inrow[4*i+3] == 0)
1139 maskbyte[0] |= mask_bit; /* turn ON bit */
1140 else
1141 maskbyte[0] &= ~mask_bit; /* turn OFF bit */
1142 mask_bit >>= 1;
1143 if (mask_bit == 0)
1144 {
1145 mask_bit = 0x80;
1146 maskbyte++;
1147 }
1148 }
1149 }
1150
1151 *color = hColorBitmap;
1152 *mask = hMaskBitmap;
1153
1154 return TRUE;
1155 }
1156
1157 static gboolean
1158 pixbuf_to_hbitmaps_normal (GdkPixbuf *pixbuf,
1159 HBITMAP *color,
1160 HBITMAP *mask)
1161 {
1162 /* Based on code from
1163 * http://www.dotnet247.com/247reference/msgs/13/66301.aspx
1164 */
1165 HBITMAP hColorBitmap, hMaskBitmap;
1166 guchar *indata, *inrow;
1167 guchar *colordata, *colorrow, *maskdata, *maskbyte;
1168 gint width, height, size, i, i_offset, j, j_offset, rowstride, nc, bmstride;
1169 gboolean has_alpha;
1170 guint maskstride, mask_bit;
1171
1172 width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
1173 height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
1174
1175 /* The bitmaps are created square */
1176 size = MAX (width, height);
1177
1178 hColorBitmap = create_color_bitmap (size, &colordata, 24);
1179 if (!hColorBitmap)
1180 return FALSE;
1181 hMaskBitmap = create_color_bitmap (size, &maskdata, 1);
1182 if (!hMaskBitmap)
1183 {
1184 DeleteObject (hColorBitmap);
1185 return FALSE;
1186 }
1187
1188 /* rows are always aligned on 4-byte boundarys */
1189 bmstride = size * 3;
1190 if (bmstride % 4 != 0)
1191 bmstride += 4 - (bmstride % 4);
1192
1193 /* MSDN says mask rows are aligned to "LONG" boundaries */
1194 maskstride = (((size + 31) & ~31) >> 3);
1195
1196 indata = gdk_pixbuf_get_pixels (pixbuf);
1197 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1198 nc = gdk_pixbuf_get_n_channels (pixbuf);
1199 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
1200
1201 if (width > height)
1202 {
1203 i_offset = 0;
1204 j_offset = (width - height) / 2;
1205 }
1206 else
1207 {
1208 i_offset = (height - width) / 2;
1209 j_offset = 0;
1210 }
1211
1212 for (j = 0; j < height; j++)
1213 {
1214 colorrow = colordata + (j+j_offset)*bmstride + 3*i_offset;
1215 maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
1216 mask_bit = (0x80 >> (i_offset % 8));
1217 inrow = indata + (height-j-1)*rowstride;
1218 for (i = 0; i < width; i++)
1219 {
1220 if (has_alpha && inrow[nc*i+3] < 128)
1221 {
1222 colorrow[3*i+0] = colorrow[3*i+1] = colorrow[3*i+2] = 0;
1223 maskbyte[0] |= mask_bit; /* turn ON bit */
1224 }
1225 else
1226 {
1227 colorrow[3*i+0] = inrow[nc*i+2];
1228 colorrow[3*i+1] = inrow[nc*i+1];
1229 colorrow[3*i+2] = inrow[nc*i+0];
1230 maskbyte[0] &= ~mask_bit; /* turn OFF bit */
1231 }
1232 mask_bit >>= 1;
1233 if (mask_bit == 0)
1234 {
1235 mask_bit = 0x80;
1236 maskbyte++;
1237 }
1238 }
1239 }
1240
1241 *color = hColorBitmap;
1242 *mask = hMaskBitmap;
1243
1244 return TRUE;
1245 }
1246
1247 static HICON
1248 pixbuf_to_hicon (GdkPixbuf *pixbuf,
1249 gboolean is_icon,
1250 gint x,
1251 gint y)
1252 {
1253 ICONINFO ii;
1254 HICON icon;
1255 gboolean success;
1256
1257 if (pixbuf == NULL)
1258 return NULL;
1259
1260 if (gdk_pixbuf_get_has_alpha (pixbuf))
1261 success = pixbuf_to_hbitmaps_alpha_winxp (pixbuf, &ii.hbmColor, &ii.hbmMask);
1262 else
1263 success = pixbuf_to_hbitmaps_normal (pixbuf, &ii.hbmColor, &ii.hbmMask);
1264
1265 if (!success)
1266 return NULL;
1267
1268 ii.fIcon = is_icon;
1269 ii.xHotspot = x;
1270 ii.yHotspot = y;
1271 icon = CreateIconIndirect (&ii);
1272 DeleteObject (ii.hbmColor);
1273 DeleteObject (ii.hbmMask);
1274 return icon;
1275 }
1276
1277 HICON
1278 _gdk_win32_pixbuf_to_hicon (GdkPixbuf *pixbuf)
1279 {
1280 return pixbuf_to_hicon (pixbuf, TRUE, 0, 0);
1281 }
1282
1283 HICON
1284 _gdk_win32_pixbuf_to_hcursor (GdkPixbuf *pixbuf,
1285 gint x_hotspot,
1286 gint y_hotspot)
1287 {
1288 return pixbuf_to_hicon (pixbuf, FALSE, x_hotspot, y_hotspot);
1289 }
1290
1291 HICON
1292 gdk_win32_pixbuf_to_hicon_libgtk_only (GdkPixbuf *pixbuf)
1293 {
1294 return _gdk_win32_pixbuf_to_hicon (pixbuf);
1295 }
1296
1297 static void
1298 gdk_win32_cursor_init (GdkWin32Cursor *cursor)
1299 {
1300 }
1301 static void
1302 gdk_win32_cursor_class_init(GdkWin32CursorClass *klass)
1303 {
1304 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1305 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (klass);
1306
1307 object_class->finalize = _gdk_win32_cursor_finalize;
1308
1309 cursor_class->get_surface = _gdk_win32_cursor_get_surface;
1310 }