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 #include "gdkdisplay.h"
23 #include "gdkscreen.h"
24 #include "gdkcursor.h"
25 #include "gdkprivate-win32.h"
30 _gdk_win32_data_to_wcursor (GdkCursorType cursor_type)
35 guchar *and_plane, *xor_plane;
37 for (i = 0; i < G_N_ELEMENTS (cursors); i++)
38 if (cursors[i].type == cursor_type)
41 if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name)
44 w = GetSystemMetrics (SM_CXCURSOR);
45 h = GetSystemMetrics (SM_CYCURSOR);
47 and_plane = g_malloc ((w/8) * h);
48 memset (and_plane, 0xff, (w/8) * h);
49 xor_plane = g_malloc ((w/8) * h);
50 memset (xor_plane, 0, (w/8) * h);
52 #define SET_BIT(v,b) (v |= (1 << b))
53 #define RESET_BIT(v,b) (v &= ~(1 << b))
55 for (j = 0, y = 0; y < cursors[i].height && y < h ; y++)
58 j = y * cursors[i].width;
60 for (x = 0; x < cursors[i].width && x < w ; x++, j++)
62 gint pofs = ofs + x / 8;
63 guchar data = (cursors[i].data[j/4] & (0xc0 >> (2 * (j%4)))) >> (2 * (3 - (j%4)));
64 gint bit = 7 - (j % cursors[i].width) % 8;
68 RESET_BIT (and_plane[pofs], bit);
70 SET_BIT (xor_plane[pofs], bit);
78 rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
79 w, h, and_plane, xor_plane);
81 WIN32_API_FAILED ("CreateCursor");
89 gdk_cursor_new_for_display (GdkDisplay *display,
90 GdkCursorType cursor_type)
92 GdkCursorPrivate *private;
96 g_return_val_if_fail (display == gdk_display_get_default (), NULL);
98 hcursor = _gdk_win32_data_to_wcursor (cursor_type);
101 g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type);
103 GDK_NOTE (MISC, g_print ("gdk_cursor_new_for_display: %d: %p\n",
104 cursor_type, hcursor));
106 private = g_new (GdkCursorPrivate, 1);
107 private->hcursor = hcursor;
108 cursor = (GdkCursor*) private;
109 cursor->type = cursor_type;
110 cursor->ref_count = 1;
116 color_is_white (const GdkColor *color)
118 return (color->red == 0xFFFF
119 && color->green == 0xFFFF
120 && color->blue == 0xFFFF);
124 gdk_cursor_new_from_pixmap (GdkPixmap *source,
131 GdkCursorPrivate *private;
133 GdkPixmapImplWin32 *source_impl, *mask_impl;
134 guchar *source_bits, *mask_bits;
135 gint source_bpl, mask_bpl;
137 guchar *p, *q, *xor_mask, *and_mask;
138 gint width, height, cursor_width, cursor_height;
141 const gboolean bg_is_white = color_is_white (bg);
143 g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
144 g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
145 g_return_val_if_fail (fg != NULL, NULL);
146 g_return_val_if_fail (bg != NULL, NULL);
148 source_impl = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (source)->impl);
149 mask_impl = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (mask)->impl);
151 g_return_val_if_fail (source_impl->width == mask_impl->width
152 && source_impl->height == mask_impl->height,
154 width = source_impl->width;
155 height = source_impl->height;
156 cursor_width = GetSystemMetrics (SM_CXCURSOR);
157 cursor_height = GetSystemMetrics (SM_CYCURSOR);
159 g_return_val_if_fail (width <= cursor_width && height <= cursor_height,
162 residue = (1 << ((8-(width%8))%8)) - 1;
164 source_bits = source_impl->bits;
165 mask_bits = mask_impl->bits;
167 g_return_val_if_fail (GDK_PIXMAP_OBJECT (source)->depth == 1
168 && GDK_PIXMAP_OBJECT (mask)->depth == 1,
171 source_bpl = ((width - 1)/32 + 1)*4;
172 mask_bpl = ((mask_impl->width - 1)/32 + 1)*4;
174 #ifdef G_ENABLE_DEBUG
175 if (_gdk_debug_flags & GDK_DEBUG_CURSOR)
177 g_print ("gdk_cursor_new_from_pixmap: source=%p:\n",
178 source_impl->parent_instance.handle);
179 for (iy = 0; iy < height; iy++)
184 p = source_bits + iy*source_bpl;
185 for (ix = 0; ix < width; ix++)
189 g_print ("%c", ".X"[((*p)>>(7-(ix%8)))&1]);
195 g_print ("...mask=%p:\n", mask_impl->parent_instance.handle);
196 for (iy = 0; iy < height; iy++)
201 p = mask_bits + iy*source_bpl;
202 for (ix = 0; ix < width; ix++)
206 g_print ("%c", ".X"[((*p)>>(7-(ix%8)))&1]);
215 /* Such complex bit manipulation for this simple task, sigh.
216 * The X cursor and Windows cursor concepts are quite different.
217 * We assume here that we are always called with fg == black and
218 * bg == white, *or* the other way around. Random colours won't work.
219 * (Well, you will get a cursor, but not in those colours.)
222 /* Note: The comments below refer to the case fg==black and
223 * bg==white, as that was what was implemented first. The fg==white
224 * (the "if (fg->pixel)" branches) case was added later.
227 /* First set masked-out source bits, as all source bits matter on Windoze.
228 * As we invert them below, they will be clear in the final xor_mask.
230 for (iy = 0; iy < height; iy++)
232 p = source_bits + iy*source_bpl;
233 q = mask_bits + iy*mask_bpl;
235 for (ix = 0; ix < ((width-1)/8+1); ix++)
242 /* XOR mask is initialized to zero */
243 xor_mask = g_malloc0 (cursor_width/8 * cursor_height);
245 for (iy = 0; iy < height; iy++)
247 p = source_bits + iy*source_bpl;
248 q = xor_mask + iy*cursor_width/8;
250 for (ix = 0; ix < ((width-1)/8+1); ix++)
256 q[-1] &= ~residue; /* Clear left-over bits */
259 /* AND mask is initialized to ones */
260 and_mask = g_malloc (cursor_width/8 * cursor_height);
261 memset (and_mask, 0xFF, cursor_width/8 * cursor_height);
263 for (iy = 0; iy < height; iy++)
265 p = mask_bits + iy*mask_bpl;
266 q = and_mask + iy*cursor_width/8;
268 for (ix = 0; ix < ((width-1)/8+1); ix++)
271 q[-1] |= residue; /* Set left-over bits */
274 hcursor = CreateCursor (_gdk_app_hmodule, x, y, cursor_width, cursor_height,
277 GDK_NOTE (MISC, g_print ("gdk_cursor_new_from_pixmap: "
278 "%p (%dx%d) %p (%dx%d) = %p (%dx%d)\n",
279 GDK_PIXMAP_HBITMAP (source),
280 source_impl->width, source_impl->height,
281 GDK_PIXMAP_HBITMAP (mask),
282 mask_impl->width, mask_impl->height,
283 hcursor, cursor_width, cursor_height));
288 private = g_new (GdkCursorPrivate, 1);
289 private->hcursor = hcursor;
290 cursor = (GdkCursor*) private;
291 cursor->type = GDK_CURSOR_IS_PIXMAP;
292 cursor->ref_count = 1;
298 _gdk_cursor_destroy (GdkCursor *cursor)
300 GdkCursorPrivate *private;
302 g_return_if_fail (cursor != NULL);
303 private = (GdkCursorPrivate *) cursor;
305 GDK_NOTE (MISC, g_print ("_gdk_cursor_destroy: %p\n",
306 (cursor->type == GDK_CURSOR_IS_PIXMAP) ? private->hcursor : 0));
308 if (GetCursor () == private->hcursor)
311 if (!DestroyCursor (private->hcursor))
312 WIN32_API_FAILED ("DestroyCursor");
318 gdk_cursor_get_display (GdkCursor *cursor)
320 return gdk_display_get_default ();
324 gdk_cursor_new_from_pixbuf (GdkDisplay *display,
329 /* FIXME: Use alpha if supported */
332 GdkPixmap *pixmap, *mask;
333 guint width, height, n_channels, rowstride, i, j;
334 guint8 *data, *mask_data, *pixels;
335 GdkColor fg = { 0, 0, 0, 0 };
336 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
339 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
340 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
342 width = gdk_pixbuf_get_width (pixbuf);
343 height = gdk_pixbuf_get_height (pixbuf);
345 g_return_val_if_fail (0 <= x && x < width, NULL);
346 g_return_val_if_fail (0 <= y && y < height, NULL);
348 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
349 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
350 pixels = gdk_pixbuf_get_pixels (pixbuf);
352 data = g_new0 (guint8, (width + 7) / 8 * height);
353 mask_data = g_new0 (guint8, (width + 7) / 8 * height);
355 for (j = 0; j < height; j++)
357 guint8 *src = pixels + j * rowstride;
358 guint8 *d = data + (width + 7) / 8 * j;
359 guint8 *md = mask_data + (width + 7) / 8 * j;
361 for (i = 0; i < width; i++)
366 if (n_channels == 3 || src[3] >= 0x80)
378 screen = gdk_display_get_default_screen (display);
379 pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
380 data, width, height);
382 mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
383 mask_data, width, height);
385 cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
387 g_object_unref (pixmap);
388 g_object_unref (mask);
397 gdk_display_supports_cursor_alpha (GdkDisplay *display)
399 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
406 gdk_display_supports_cursor_color (GdkDisplay *display)
408 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
415 gdk_display_get_default_cursor_size (GdkDisplay *display)
417 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
419 return MIN (GetSystemMetrics (SM_CXCURSOR), GetSystemMetrics (SM_CYCURSOR));
423 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
427 g_return_if_fail (GDK_IS_DISPLAY (display));
430 *width = GetSystemMetrics (SM_CXCURSOR);
432 *height = GetSystemMetrics (SM_CYCURSOR);