1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include "gdkprivate.h"
25 static const struct { const char *name; int type; } cursors[] = {
28 { "based_arrow_down", 4 },
29 { "based_arrow_up", 6 },
32 { "bottom_left_corner", 12 },
33 { "bottom_right_corner", 14 },
34 { "bottom_side", 16 },
42 { "cross_reverse", 32 },
44 { "diamond_cross", 36 },
47 { "double_arrow", 42 },
48 { "draft_large", 44 },
49 { "draft_small", 46 },
67 { "middlebutton", 82 },
72 { "question_arrow", 92 },
76 { "rightbutton", 100 },
79 { "sb_down_arrow", 106 },
80 { "sb_h_double_arrow", 108 },
81 { "sb_left_arrow", 110 },
82 { "sb_right_arrow", 112 },
83 { "sb_up_arrow", 114 },
84 { "sb_v_double_arrow", 116 },
92 { "top_left_arrow", 132 },
93 { "top_left_corner", 134 },
94 { "top_right_corner", 136 },
107 gdk_cursor_new (GdkCursorType cursor_type)
109 GdkCursorPrivate *private;
115 for (i = 0; cursors[i].name != NULL && cursors[i].type != cursor_type; i++)
117 if (cursors[i].name != NULL)
119 xcursor = LoadCursor (gdk_DLLInstance, cursors[i].name);
121 g_warning ("gdk_cursor_new: LoadCursor failed");
122 GDK_NOTE (MISC, g_print ("gdk_cursor_new: %#x %d\n",
123 xcursor, cursor_type));
127 g_warning ("gdk_cursor_new: no cursor %d found",
132 private = g_new (GdkCursorPrivate, 1);
133 private->xcursor = xcursor;
134 cursor = (GdkCursor*) private;
135 cursor->type = cursor_type;
141 gdk_cursor_new_from_pixmap (GdkPixmap *source,
148 GdkCursorPrivate *private;
150 GdkPixmapPrivate *source_private, *mask_private;
151 GdkImage *source_image, *mask_image;
153 guchar *p, *q, *XORmask, *ANDmask;
154 gint width, height, width32, height32;
158 g_return_val_if_fail (source != NULL, NULL);
159 g_return_val_if_fail (mask != NULL, NULL);
161 source_private = (GdkPixmapPrivate *) source;
162 mask_private = (GdkPixmapPrivate *) mask;
164 g_return_val_if_fail (source_private->width == mask_private->width
165 && source_private->height == mask_private->height,
167 width = source_private->width;
168 height = source_private->height;
169 width32 = ((width-1)/32+1)*32;
170 height32 = ((height-1)/32+1)*32;
172 residue = (1 << ((8-(width%8))%8)) - 1;
174 source_image = gdk_image_get (source, 0, 0, width, height);
175 mask_image = gdk_image_get (mask, 0, 0, width, height);
177 if (source_image->depth != 1 || mask_image->depth != 1)
179 gdk_image_destroy (source_image);
180 gdk_image_destroy (mask_image);
181 g_return_val_if_fail (source_image->depth == 1 && mask_image->depth == 1,
185 /* Such complex bit manipulation for this simple task, sigh.
186 * The X cursor and Windows cursor concepts are quite different.
187 * We assume here that we are always called with fg == black and
191 /* First set masked-out source bits, as all source bits matter on Windoze.
192 * As we invert them below, they will be clear in the final XORmask.
194 for (iy = 0; iy < height; iy++)
196 p = source_image->mem + iy*source_image->bpl;
197 q = mask_image->mem + iy*mask_image->bpl;
199 for (ix = 0; ix < ((width-1)/8+1); ix++)
203 /* XOR mask is initialized to zero */
204 XORmask = g_malloc0 (width32/8 * height32);
206 for (iy = 0; iy < height; iy++)
208 p = source_image->mem + iy*source_image->bpl;
209 q = XORmask + iy*width32/8;
211 for (ix = 0; ix < ((width-1)/8+1); ix++)
213 q[-1] &= ~residue; /* Clear left-over bits */
216 /* AND mask is initialized to ones */
217 ANDmask = g_malloc (width32/8 * height32);
218 memset (ANDmask, 0xFF, width32/8 * height32);
220 for (iy = 0; iy < height; iy++)
222 p = mask_image->mem + iy*mask_image->bpl;
223 q = ANDmask + iy*width32/8;
225 for (ix = 0; ix < ((width-1)/8+1); ix++)
227 q[-1] |= residue; /* Set left-over bits */
230 xcursor = CreateCursor (gdk_ProgInstance, x, y, width32, height32,
233 GDK_NOTE (MISC, g_print ("gdk_cursor_new_from_pixmap: "
234 "%#x (%dx%d) %#x (%dx%d) = %#x (%dx%d)\n",
235 source_private->xwindow,
236 source_private->width, source_private->height,
237 mask_private->xwindow,
238 mask_private->width, mask_private->height,
239 xcursor, width32, height32));
244 gdk_image_destroy (source_image);
245 gdk_image_destroy (mask_image);
247 private = g_new (GdkCursorPrivate, 1);
248 private->xcursor = xcursor;
249 cursor = (GdkCursor*) private;
250 cursor->type = GDK_CURSOR_IS_PIXMAP;
256 gdk_cursor_destroy (GdkCursor *cursor)
258 GdkCursorPrivate *private;
260 g_return_if_fail (cursor != NULL);
261 private = (GdkCursorPrivate *) cursor;
263 GDK_NOTE (MISC, g_print ("gdk_cursor_destroy: %#x\n",
264 (cursor->type == GDK_CURSOR_IS_PIXMAP) ? private->xcursor : 0));
266 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
267 if (!DestroyIcon (private->xcursor))
268 g_warning ("gdk_cursor_destroy: DestroyIcon failed");