| 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, write to the |
| 17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 |
* Boston, MA 02111-1307, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 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 |
|
| 28 |
#ifdef __MINGW32__ |
| 29 |
#include <w32api.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "xcursors.h" |
| 33 |
|
| 34 |
#if (defined(__MINGW32__) && (__W32API_MAJOR_VERSION < 3 || (__W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION < 8))) || (defined(_MSC_VER) && (WINVER < 0x0500)) |
| 35 |
typedef struct { |
| 36 |
DWORD bV5Size; |
| 37 |
LONG bV5Width; |
| 38 |
LONG bV5Height; |
| 39 |
WORD bV5Planes; |
| 40 |
WORD bV5BitCount; |
| 41 |
DWORD bV5Compression; |
| 42 |
DWORD bV5SizeImage; |
| 43 |
LONG bV5XPelsPerMeter; |
| 44 |
LONG bV5YPelsPerMeter; |
| 45 |
DWORD bV5ClrUsed; |
| 46 |
DWORD bV5ClrImportant; |
| 47 |
DWORD bV5RedMask; |
| 48 |
DWORD bV5GreenMask; |
| 49 |
DWORD bV5BlueMask; |
| 50 |
DWORD bV5AlphaMask; |
| 51 |
DWORD bV5CSType; |
| 52 |
CIEXYZTRIPLE bV5Endpoints; |
| 53 |
DWORD bV5GammaRed; |
| 54 |
DWORD bV5GammaGreen; |
| 55 |
DWORD bV5GammaBlue; |
| 56 |
DWORD bV5Intent; |
| 57 |
DWORD bV5ProfileData; |
| 58 |
DWORD bV5ProfileSize; |
| 59 |
DWORD bV5Reserved; |
| 60 |
} BITMAPV5HEADER; |
| 61 |
#endif |
| 62 |
|
| 63 |
static HCURSOR |
| 64 |
hcursor_from_type (GdkCursorType cursor_type) |
| 65 |
{ |
| 66 |
gint i, j, x, y, ofs; |
| 67 |
HCURSOR rv; |
| 68 |
gint w, h; |
| 69 |
guchar *and_plane, *xor_plane; |
| 70 |
|
| 71 |
if (cursor_type != GDK_BLANK_CURSOR) |
| 72 |
{ |
| 73 |
for (i = 0; i < G_N_ELEMENTS (cursors); i++) |
| 74 |
if (cursors[i].type == cursor_type) |
| 75 |
break; |
| 76 |
|
| 77 |
if (i >= G_N_ELEMENTS (cursors) || !cursors[i].name) |
| 78 |
return NULL; |
| 79 |
|
| 80 |
/* Use real Win32 cursor if possible */ |
| 81 |
if (cursors[i].builtin) |
| 82 |
return LoadCursor (NULL, cursors[i].builtin); |
| 83 |
} |
| 84 |
|
| 85 |
w = GetSystemMetrics (SM_CXCURSOR); |
| 86 |
h = GetSystemMetrics (SM_CYCURSOR); |
| 87 |
|
| 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); |
| 92 |
|
| 93 |
if (cursor_type != GDK_BLANK_CURSOR) |
| 94 |
{ |
| 95 |
|
| 96 |
#define SET_BIT(v,b) (v |= (1 << b)) |
| 97 |
#define RESET_BIT(v,b) (v &= ~(1 << b)) |
| 98 |
|
| 99 |
for (j = 0, y = 0; y < cursors[i].height && y < h ; y++) |
| 100 |
{ |
| 101 |
ofs = (y * w) / 8; |
| 102 |
j = y * cursors[i].width; |
| 103 |
|
| 104 |
for (x = 0; x < cursors[i].width && x < w ; x++, j++) |
| 105 |
{ |
| 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; |
| 109 |
|
| 110 |
if (data) |
| 111 |
{ |
| 112 |
RESET_BIT (and_plane[pofs], bit); |
| 113 |
if (data == 1) |
| 114 |
SET_BIT (xor_plane[pofs], bit); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
#undef SET_BIT |
| 120 |
#undef RESET_BIT |
| 121 |
|
| 122 |
rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty, |
| 123 |
w, h, and_plane, xor_plane); |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
rv = CreateCursor (_gdk_app_hmodule, 0, 0, |
| 128 |
w, h, and_plane, xor_plane); |
| 129 |
} |
| 130 |
if (rv == NULL) |
| 131 |
WIN32_API_FAILED ("CreateCursor"); |
| 132 |
g_free (and_plane); |
| 133 |
g_free (xor_plane); |
| 134 |
|
| 135 |
return rv; |
| 136 |
} |
| 137 |
|
| 138 |
static GdkCursor* |
| 139 |
cursor_new_from_hcursor (HCURSOR hcursor, |
| 140 |
GdkCursorType cursor_type) |
| 141 |
{ |
| 142 |
GdkCursorPrivate *private; |
| 143 |
GdkCursor *cursor; |
| 144 |
|
| 145 |
private = g_new (GdkCursorPrivate, 1); |
| 146 |
private->hcursor = hcursor; |
| 147 |
cursor = (GdkCursor*) private; |
| 148 |
cursor->type = cursor_type; |
| 149 |
cursor->ref_count = 1; |
| 150 |
|
| 151 |
return cursor; |
| 152 |
} |
| 153 |
|
| 154 |
GdkCursor* |
| 155 |
gdk_cursor_new_for_display (GdkDisplay *display, |
| 156 |
GdkCursorType cursor_type) |
| 157 |
{ |
| 158 |
HCURSOR hcursor; |
| 159 |
|
| 160 |
g_return_val_if_fail (display == _gdk_display, NULL); |
| 161 |
|
| 162 |
hcursor = hcursor_from_type (cursor_type); |
| 163 |
|
| 164 |
if (hcursor == NULL) |
| 165 |
g_warning ("gdk_cursor_new_for_display: no cursor %d found", cursor_type); |
| 166 |
else |
| 167 |
GDK_NOTE (CURSOR, g_print ("gdk_cursor_new_for_display: %d: %p\n", |
| 168 |
cursor_type, hcursor)); |
| 169 |
|
| 170 |
return cursor_new_from_hcursor (hcursor, cursor_type); |
| 171 |
} |
| 172 |
|
| 173 |
static gboolean |
| 174 |
color_is_white (const GdkColor *color) |
| 175 |
{ |
| 176 |
return (color->red == 0xFFFF |
| 177 |
&& color->green == 0xFFFF |
| 178 |
&& color->blue == 0xFFFF); |
| 179 |
} |
| 180 |
|
| 181 |
GdkCursor* |
| 182 |
gdk_cursor_new_from_pixmap (GdkPixmap *source, |
| 183 |
GdkPixmap *mask, |
| 184 |
const GdkColor *fg, |
| 185 |
const GdkColor *bg, |
| 186 |
gint x, |
| 187 |
gint y) |
| 188 |
{ |
| 189 |
GdkPixmapImplWin32 *source_impl, *mask_impl; |
| 190 |
guchar *source_bits, *mask_bits; |
| 191 |
gint source_bpl, mask_bpl; |
| 192 |
HCURSOR hcursor; |
| 193 |
guchar *p, *q, *xor_mask, *and_mask; |
| 194 |
gint width, height, cursor_width, cursor_height; |
| 195 |
guchar residue; |
| 196 |
gint ix, iy; |
| 197 |
const gboolean bg_is_white = color_is_white (bg); |
| 198 |
|
| 199 |
g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL); |
| 200 |
g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL); |
| 201 |
g_return_val_if_fail (fg != NULL, NULL); |
| 202 |
g_return_val_if_fail (bg != NULL, NULL); |
| 203 |
|
| 204 |
/* Flush outstanding GDI ops before accessing pixmap->bits */ |
| 205 |
GdiFlush (); |
| 206 |
|
| 207 |
source_impl = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (source)->impl); |
| 208 |
mask_impl = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (mask)->impl); |
| 209 |
|
| 210 |
g_return_val_if_fail (source_impl->width == mask_impl->width |
| 211 |
&& source_impl->height == mask_impl->height, |
| 212 |
NULL); |
| 213 |
width = source_impl->width; |
| 214 |
height = source_impl->height; |
| 215 |
cursor_width = GetSystemMetrics (SM_CXCURSOR); |
| 216 |
cursor_height = GetSystemMetrics (SM_CYCURSOR); |
| 217 |
|
| 218 |
g_return_val_if_fail (width <= cursor_width && height <= cursor_height, |
| 219 |
NULL); |
| 220 |
|
| 221 |
residue = (1 << ((8-(width%8))%8)) - 1; |
| 222 |
|
| 223 |
source_bits = source_impl->bits; |
| 224 |
mask_bits = mask_impl->bits; |
| 225 |
|
| 226 |
g_return_val_if_fail (GDK_PIXMAP_OBJECT (source)->depth == 1 |
| 227 |
&& GDK_PIXMAP_OBJECT (mask)->depth == 1, |
| 228 |
NULL); |
| 229 |
|
| 230 |
source_bpl = ((width - 1)/32 + 1)*4; |
| 231 |
mask_bpl = ((mask_impl->width - 1)/32 + 1)*4; |
| 232 |
|
| 233 |
GDK_NOTE (CURSOR, { |
| 234 |
g_print ("gdk_cursor_new_from_pixmap: source=%p:\n", |
| 235 |
source_impl->parent_instance.handle); |
| 236 |
for (iy = 0; iy < height; iy++) |
| 237 |
{ |
| 238 |
if (iy == 16) |
| 239 |
break; |
| 240 |
|
| 241 |
p = source_bits + iy*source_bpl; |
| 242 |
for (ix = 0; ix < width; ix++) |
| 243 |
{ |
| 244 |
if (ix == 79) |
| 245 |
break; |
| 246 |
g_print ("%c", ".X"[((*p)>>(7-(ix%8)))&1]); |
| 247 |
if ((ix%8) == 7) |
| 248 |
p++; |
| 249 |
} |
| 250 |
g_print ("\n"); |
| 251 |
} |
| 252 |
g_print ("...mask=%p:\n", mask_impl->parent_instance.handle); |
| 253 |
for (iy = 0; iy < height; iy++) |
| 254 |
{ |
| 255 |
if (iy == 16) |
| 256 |
break; |
| 257 |
|
| 258 |
p = mask_bits + iy*source_bpl; |
| 259 |
for (ix = 0; ix < width; ix++) |
| 260 |
{ |
| 261 |
if (ix == 79) |
| 262 |
break; |
| 263 |
g_print ("%c", ".X"[((*p)>>(7-(ix%8)))&1]); |
| 264 |
if ((ix%8) == 7) |
| 265 |
p++; |
| 266 |
} |
| 267 |
g_print ("\n"); |
| 268 |
} |
| 269 |
}); |
| 270 |
|
| 271 |
/* Such complex bit manipulation for this simple task, sigh. |
| 272 |
* The X cursor and Windows cursor concepts are quite different. |
| 273 |
* We assume here that we are always called with fg == black and |
| 274 |
* bg == white, *or* the other way around. Random colours won't work. |
| 275 |
* (Well, you will get a cursor, but not in those colours.) |
| 276 |
*/ |
| 277 |
|
| 278 |
/* Note: The comments below refer to the case fg==black and |
| 279 |
* bg==white, as that was what was implemented first. The fg==white |
| 280 |
* (the "if (fg->pixel)" branches) case was added later. |
| 281 |
*/ |
| 282 |
|
| 283 |
/* First set masked-out source bits, as all source bits matter on Windoze. |
| 284 |
* As we invert them below, they will be clear in the final xor_mask. |
| 285 |
*/ |
| 286 |
for (iy = 0; iy < height; iy++) |
| 287 |
{ |
| 288 |
p = source_bits + iy*source_bpl; |
| 289 |
q = mask_bits + iy*mask_bpl; |
| 290 |
|
| 291 |
for (ix = 0; ix < ((width-1)/8+1); ix++) |
| 292 |
if (bg_is_white) |
| 293 |
*p++ |= ~(*q++); |
| 294 |
else |
| 295 |
*p++ &= *q++; |
| 296 |
} |
| 297 |
|
| 298 |
/* XOR mask is initialized to zero */ |
| 299 |
xor_mask = g_malloc0 (cursor_width/8 * cursor_height); |
| 300 |
|
| 301 |
for (iy = 0; iy < height; iy++) |
| 302 |
{ |
| 303 |
p = source_bits + iy*source_bpl; |
| 304 |
q = xor_mask + iy*cursor_width/8; |
| 305 |
|
| 306 |
for (ix = 0; ix < ((width-1)/8+1); ix++) |
| 307 |
if (bg_is_white) |
| 308 |
*q++ = ~(*p++); |
| 309 |
else |
| 310 |
*q++ = *p++; |
| 311 |
|
| 312 |
q[-1] &= ~residue; /* Clear left-over bits */ |
| 313 |
} |
| 314 |
|
| 315 |
/* AND mask is initialized to ones */ |
| 316 |
and_mask = g_malloc (cursor_width/8 * cursor_height); |
| 317 |
memset (and_mask, 0xFF, cursor_width/8 * cursor_height); |
| 318 |
|
| 319 |
for (iy = 0; iy < height; iy++) |
| 320 |
{ |
| 321 |
p = mask_bits + iy*mask_bpl; |
| 322 |
q = and_mask + iy*cursor_width/8; |
| 323 |
|
| 324 |
for (ix = 0; ix < ((width-1)/8+1); ix++) |
| 325 |
*q++ = ~(*p++); |
| 326 |
|
| 327 |
q[-1] |= residue; /* Set left-over bits */ |
| 328 |
} |
| 329 |
|
| 330 |
hcursor = CreateCursor (_gdk_app_hmodule, x, y, cursor_width, cursor_height, |
| 331 |
and_mask, xor_mask); |
| 332 |
|
| 333 |
GDK_NOTE (CURSOR, g_print ("gdk_cursor_new_from_pixmap: " |
| 334 |
"%p (%dx%d) %p (%dx%d) = %p (%dx%d)\n", |
| 335 |
GDK_PIXMAP_HBITMAP (source), |
| 336 |
source_impl->width, source_impl->height, |
| 337 |
GDK_PIXMAP_HBITMAP (mask), |
| 338 |
mask_impl->width, mask_impl->height, |
| 339 |
hcursor, cursor_width, cursor_height)); |
| 340 |
|
| 341 |
g_free (xor_mask); |
| 342 |
g_free (and_mask); |
| 343 |
|
| 344 |
return cursor_new_from_hcursor (hcursor, GDK_CURSOR_IS_PIXMAP); |
| 345 |
} |
| 346 |
|
| 347 |
/* FIXME: The named cursors below are presumably not really useful, as |
| 348 |
* the names are Win32-specific. No GTK+ application developed on Unix |
| 349 |
* (and most cross-platform GTK+ apps are developed on Unix) is going |
| 350 |
* to look for cursors under these Win32 names anyway. |
| 351 |
* |
| 352 |
* Would the following make any sense: The ms-windows theme engine |
| 353 |
* calls some (to-be-defined private) API here in gdk/win32 to |
| 354 |
* register the relevant cursors used by the currently active XP |
| 355 |
* visual style under the names that libgtk uses to look for them |
| 356 |
* ("color-picker", "dnd-ask", "dnd-copy", etc), and then when libgtk |
| 357 |
* asks for those we return the ones registered by the ms-windows |
| 358 |
* theme engine, if any. |
| 359 |
*/ |
| 360 |
|
| 361 |
static struct { |
| 362 |
char *name; |
| 363 |
char *id; |
| 364 |
} default_cursors[] = { |
| 365 |
{ "appstarting", IDC_APPSTARTING }, |
| 366 |
{ "arrow", IDC_ARROW }, |
| 367 |
{ "cross", IDC_CROSS }, |
| 368 |
#ifdef IDC_HAND |
| 369 |
{ "hand", IDC_HAND }, |
| 370 |
#endif |
| 371 |
{ "help", IDC_HELP }, |
| 372 |
{ "ibeam", IDC_IBEAM }, |
| 373 |
{ "sizeall", IDC_SIZEALL }, |
| 374 |
{ "sizenesw", IDC_SIZENESW }, |
| 375 |
{ "sizens", IDC_SIZENS }, |
| 376 |
{ "sizenwse", IDC_SIZENWSE }, |
| 377 |
{ "sizewe", IDC_SIZEWE }, |
| 378 |
{ "uparrow", IDC_UPARROW }, |
| 379 |
{ "wait", IDC_WAIT } |
| 380 |
}; |
| 381 |
|
| 382 |
GdkCursor* |
| 383 |
gdk_cursor_new_from_name (GdkDisplay *display, |
| 384 |
const gchar *name) |
| 385 |
{ |
| 386 |
HCURSOR hcursor = NULL; |
| 387 |
int i; |
| 388 |
|
| 389 |
g_return_val_if_fail (display == _gdk_display, NULL); |
| 390 |
|
| 391 |
for (i = 0; i < G_N_ELEMENTS(default_cursors); i++) |
| 392 |
{ |
| 393 |
if (0 == strcmp(default_cursors[i].name, name)) |
| 394 |
hcursor = LoadCursor (NULL, default_cursors[i].id); |
| 395 |
} |
| 396 |
/* allow to load named cursor resources linked into the executable */ |
| 397 |
if (!hcursor) |
| 398 |
hcursor = LoadCursor (_gdk_app_hmodule, name); |
| 399 |
|
| 400 |
if (hcursor) |
| 401 |
return cursor_new_from_hcursor (hcursor, GDK_X_CURSOR); |
| 402 |
|
| 403 |
return NULL; |
| 404 |
} |
| 405 |
|
| 406 |
void |
| 407 |
_gdk_cursor_destroy (GdkCursor *cursor) |
| 408 |
{ |
| 409 |
GdkCursorPrivate *private; |
| 410 |
|
| 411 |
g_return_if_fail (cursor != NULL); |
| 412 |
private = (GdkCursorPrivate *) cursor; |
| 413 |
|
| 414 |
GDK_NOTE (CURSOR, g_print ("_gdk_cursor_destroy: %p\n", |
| 415 |
(cursor->type == GDK_CURSOR_IS_PIXMAP) ? private->hcursor : 0)); |
| 416 |
|
| 417 |
if (GetCursor () == private->hcursor) |
| 418 |
SetCursor (NULL); |
| 419 |
|
| 420 |
if (!DestroyCursor (private->hcursor)) |
| 421 |
WIN32_API_FAILED ("DestroyCursor"); |
| 422 |
|
| 423 |
g_free (private); |
| 424 |
} |
| 425 |
|
| 426 |
GdkDisplay * |
| 427 |
gdk_cursor_get_display (GdkCursor *cursor) |
| 428 |
{ |
| 429 |
return gdk_display_get_default (); |
| 430 |
} |
| 431 |
|
| 432 |
GdkPixbuf * |
| 433 |
gdk_win32_icon_to_pixbuf_libgtk_only (HICON hicon) |
| 434 |
{ |
| 435 |
GdkPixbuf *pixbuf = NULL; |
| 436 |
ICONINFO ii; |
| 437 |
struct |
| 438 |
{ |
| 439 |
BITMAPINFOHEADER bi; |
| 440 |
RGBQUAD colors[2]; |
| 441 |
} bmi; |
| 442 |
HDC hdc; |
| 443 |
guchar *pixels, *bits; |
| 444 |
gchar buf[32]; |
| 445 |
gint rowstride, x, y, w, h; |
| 446 |
|
| 447 |
if (!GDI_CALL (GetIconInfo, (hicon, &ii))) |
| 448 |
return NULL; |
| 449 |
|
| 450 |
if (!(hdc = CreateCompatibleDC (NULL))) |
| 451 |
{ |
| 452 |
WIN32_GDI_FAILED ("CreateCompatibleDC"); |
| 453 |
goto out0; |
| 454 |
} |
| 455 |
|
| 456 |
memset (&bmi, 0, sizeof (bmi)); |
| 457 |
bmi.bi.biSize = sizeof (bmi.bi); |
| 458 |
|
| 459 |
if (ii.hbmColor != NULL) |
| 460 |
{ |
| 461 |
/* Colour cursor */ |
| 462 |
|
| 463 |
gboolean no_alpha; |
| 464 |
|
| 465 |
if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, 1, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS))) |
| 466 |
goto out1; |
| 467 |
|
| 468 |
w = bmi.bi.biWidth; |
| 469 |
h = bmi.bi.biHeight; |
| 470 |
|
| 471 |
bmi.bi.biBitCount = 32; |
| 472 |
bmi.bi.biCompression = BI_RGB; |
| 473 |
bmi.bi.biHeight = -h; |
| 474 |
|
| 475 |
bits = g_malloc0 (4 * w * h); |
| 476 |
|
| 477 |
/* color data */ |
| 478 |
if (!GDI_CALL (GetDIBits, (hdc, ii.hbmColor, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS))) |
| 479 |
goto out2; |
| 480 |
|
| 481 |
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h); |
| 482 |
pixels = gdk_pixbuf_get_pixels (pixbuf); |
| 483 |
rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
| 484 |
no_alpha = TRUE; |
| 485 |
for (y = 0; y < h; y++) |
| 486 |
{ |
| 487 |
for (x = 0; x < w; x++) |
| 488 |
{ |
| 489 |
pixels[2] = bits[(x+y*w) * 4]; |
| 490 |
pixels[1] = bits[(x+y*w) * 4 + 1]; |
| 491 |
pixels[0] = bits[(x+y*w) * 4 + 2]; |
| 492 |
pixels[3] = bits[(x+y*w) * 4 + 3]; |
| 493 |
if (no_alpha && pixels[3] > 0) |
| 494 |
no_alpha = FALSE; |
| 495 |
pixels += 4; |
| 496 |
} |
| 497 |
pixels += (w * 4 - rowstride); |
| 498 |
} |
| 499 |
|
| 500 |
/* mask */ |
| 501 |
if (no_alpha && |
| 502 |
GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS))) |
| 503 |
{ |
| 504 |
pixels = gdk_pixbuf_get_pixels (pixbuf); |
| 505 |
for (y = 0; y < h; y++) |
| 506 |
{ |
| 507 |
for (x = 0; x < w; x++) |
| 508 |
{ |
| 509 |
pixels[3] = 255 - bits[(x + y * w) * 4]; |
| 510 |
pixels += 4; |
| 511 |
} |
| 512 |
pixels += (w * 4 - rowstride); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
else |
| 517 |
{ |
| 518 |
/* B&W cursor */ |
| 519 |
|
| 520 |
int bpl; |
| 521 |
|
| 522 |
if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, 0, NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS))) |
| 523 |
goto out1; |
| 524 |
|
| 525 |
w = bmi.bi.biWidth; |
| 526 |
h = ABS (bmi.bi.biHeight) / 2; |
| 527 |
|
| 528 |
bits = g_malloc0 (4 * w * h); |
| 529 |
|
| 530 |
/* masks */ |
| 531 |
if (!GDI_CALL (GetDIBits, (hdc, ii.hbmMask, 0, h*2, bits, (BITMAPINFO *)&bmi, DIB_RGB_COLORS))) |
| 532 |
goto out2; |
| 533 |
|
| 534 |
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h); |
| 535 |
pixels = gdk_pixbuf_get_pixels (pixbuf); |
| 536 |
rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
| 537 |
bpl = ((w-1)/32 + 1)*4; |
| 538 |
#if 0 |
| 539 |
for (y = 0; y < h*2; y++) |
| 540 |
{ |
| 541 |
for (x = 0; x < w; x++) |
| 542 |
{ |
| 543 |
const gint bit = 7 - (x % 8); |
| 544 |
printf ("%c ", ((bits[bpl*y+x/8])&(1<<bit)) ? ' ' : 'X'); |
| 545 |
} |
| 546 |
printf ("\n"); |
| 547 |
} |
| 548 |
#endif |
| 549 |
|
| 550 |
for (y = 0; y < h; y++) |
| 551 |
{ |
| 552 |
const guchar *andp, *xorp; |
| 553 |
if (bmi.bi.biHeight < 0) |
| 554 |
{ |
| 555 |
andp = bits + bpl*y; |
| 556 |
xorp = bits + bpl*(h+y); |
| 557 |
} |
| 558 |
else |
| 559 |
{ |
| 560 |
andp = bits + bpl*(h-y-1); |
| 561 |
xorp = bits + bpl*(h+h-y-1); |
| 562 |
} |
| 563 |
for (x = 0; x < w; x++) |
| 564 |
{ |
| 565 |
const gint bit = 7 - (x % 8); |
| 566 |
if ((*andp) & (1<<bit)) |
| 567 |
{ |
| 568 |
if ((*xorp) & (1<<bit)) |
| 569 |
pixels[2] = pixels[1] = pixels[0] = 0xFF; |
| 570 |
else |
| 571 |
pixels[2] = pixels[1] = pixels[0] = 0; |
| 572 |
pixels[3] = 0xFF; |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
pixels[2] = pixels[1] = pixels[0] = 0; |
| 577 |
pixels[3] = 0; |
| 578 |
} |
| 579 |
pixels += 4; |
| 580 |
if (bit == 0) |
| 581 |
{ |
| 582 |
andp++; |
| 583 |
xorp++; |
| 584 |
} |
| 585 |
} |
| 586 |
pixels += (w * 4 - rowstride); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
g_snprintf (buf, sizeof (buf), "%ld", ii.xHotspot); |
| 591 |
gdk_pixbuf_set_option (pixbuf, "x_hot", buf); |
| 592 |
|
| 593 |
g_snprintf (buf, sizeof (buf), "%ld", ii.yHotspot); |
| 594 |
gdk_pixbuf_set_option (pixbuf, "y_hot", buf); |
| 595 |
|
| 596 |
/* release temporary resources */ |
| 597 |
out2: |
| 598 |
g_free (bits); |
| 599 |
out1: |
| 600 |
DeleteDC (hdc); |
| 601 |
out0: |
| 602 |
DeleteObject (ii.hbmColor); |
| 603 |
DeleteObject (ii.hbmMask); |
| 604 |
|
| 605 |
return pixbuf; |
| 606 |
} |
| 607 |
|
| 608 |
GdkPixbuf* |
| 609 |
gdk_cursor_get_image (GdkCursor *cursor) |
| 610 |
{ |
| 611 |
g_return_val_if_fail (cursor != NULL, NULL); |
| 612 |
|
| 613 |
return gdk_win32_icon_to_pixbuf_libgtk_only (((GdkCursorPrivate *) cursor)->hcursor); |
| 614 |
} |
| 615 |
|
| 616 |
GdkCursor * |
| 617 |
gdk_cursor_new_from_pixbuf (GdkDisplay *display, |
| 618 |
GdkPixbuf *pixbuf, |
| 619 |
gint x, |
| 620 |
gint y) |
| 621 |
{ |
| 622 |
HCURSOR hcursor; |
| 623 |
|
| 624 |
g_return_val_if_fail (display == _gdk_display, NULL); |
| 625 |
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL); |
| 626 |
g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL); |
| 627 |
g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL); |
| 628 |
|
| 629 |
hcursor = _gdk_win32_pixbuf_to_hcursor (pixbuf, x, y); |
| 630 |
if (!hcursor) |
| 631 |
return NULL; |
| 632 |
return cursor_new_from_hcursor (hcursor, GDK_CURSOR_IS_PIXMAP); |
| 633 |
} |
| 634 |
|
| 635 |
gboolean |
| 636 |
gdk_display_supports_cursor_alpha (GdkDisplay *display) |
| 637 |
{ |
| 638 |
g_return_val_if_fail (display == _gdk_display, FALSE); |
| 639 |
|
| 640 |
return _gdk_win32_pixbuf_to_hicon_supports_alpha (); |
| 641 |
} |
| 642 |
|
| 643 |
gboolean |
| 644 |
gdk_display_supports_cursor_color (GdkDisplay *display) |
| 645 |
{ |
| 646 |
g_return_val_if_fail (display == _gdk_display, FALSE); |
| 647 |
|
| 648 |
return TRUE; |
| 649 |
} |
| 650 |
|
| 651 |
guint |
| 652 |
gdk_display_get_default_cursor_size (GdkDisplay *display) |
| 653 |
{ |
| 654 |
g_return_val_if_fail (display == _gdk_display, 0); |
| 655 |
|
| 656 |
return MIN (GetSystemMetrics (SM_CXCURSOR), GetSystemMetrics (SM_CYCURSOR)); |
| 657 |
} |
| 658 |
|
| 659 |
void |
| 660 |
gdk_display_get_maximal_cursor_size (GdkDisplay *display, |
| 661 |
guint *width, |
| 662 |
guint *height) |
| 663 |
{ |
| 664 |
g_return_if_fail (display == _gdk_display); |
| 665 |
|
| 666 |
if (width) |
| 667 |
*width = GetSystemMetrics (SM_CXCURSOR); |
| 668 |
if (height) |
| 669 |
*height = GetSystemMetrics (SM_CYCURSOR); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
/* Convert a pixbuf to an HICON (or HCURSOR). Supports alpha under |
| 674 |
* Windows XP, thresholds alpha otherwise. Also used from |
| 675 |
* gdkwindow-win32.c for creating application icons. |
| 676 |
*/ |
| 677 |
|
| 678 |
static HBITMAP |
| 679 |
create_alpha_bitmap (gint size, |
| 680 |
guchar **outdata) |
| 681 |
{ |
| 682 |
BITMAPV5HEADER bi; |
| 683 |
HDC hdc; |
| 684 |
HBITMAP hBitmap; |
| 685 |
|
| 686 |
ZeroMemory (&bi, sizeof (BITMAPV5HEADER)); |
| 687 |
bi.bV5Size = sizeof (BITMAPV5HEADER); |
| 688 |
bi.bV5Height = bi.bV5Width = size; |
| 689 |
bi.bV5Planes = 1; |
| 690 |
bi.bV5BitCount = 32; |
| 691 |
bi.bV5Compression = BI_BITFIELDS; |
| 692 |
/* The following mask specification specifies a supported 32 BPP |
| 693 |
* alpha format for Windows XP (BGRA format). |
| 694 |
*/ |
| 695 |
bi.bV5RedMask = 0x00FF0000; |
| 696 |
bi.bV5GreenMask = 0x0000FF00; |
| 697 |
bi.bV5BlueMask = 0x000000FF; |
| 698 |
bi.bV5AlphaMask = 0xFF000000; |
| 699 |
|
| 700 |
/* Create the DIB section with an alpha channel. */ |
| 701 |
hdc = GetDC (NULL); |
| 702 |
if (!hdc) |
| 703 |
{ |
| 704 |
WIN32_GDI_FAILED ("GetDC"); |
| 705 |
return NULL; |
| 706 |
} |
| 707 |
hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS, |
| 708 |
(PVOID *) outdata, NULL, (DWORD)0); |
| 709 |
if (hBitmap == NULL) |
| 710 |
WIN32_GDI_FAILED ("CreateDIBSection"); |
| 711 |
ReleaseDC (NULL, hdc); |
| 712 |
|
| 713 |
return hBitmap; |
| 714 |
} |
| 715 |
|
| 716 |
static HBITMAP |
| 717 |
create_color_bitmap (gint size, |
| 718 |
guchar **outdata, |
| 719 |
gint bits) |
| 720 |
{ |
| 721 |
struct { |
| 722 |
BITMAPV4HEADER bmiHeader; |
| 723 |
RGBQUAD bmiColors[2]; |
| 724 |
} bmi; |
| 725 |
HDC hdc; |
| 726 |
HBITMAP hBitmap; |
| 727 |
|
| 728 |
ZeroMemory (&bmi, sizeof (bmi)); |
| 729 |
bmi.bmiHeader.bV4Size = sizeof (BITMAPV4HEADER); |
| 730 |
bmi.bmiHeader.bV4Height = bmi.bmiHeader.bV4Width = size; |
| 731 |
bmi.bmiHeader.bV4Planes = 1; |
| 732 |
bmi.bmiHeader.bV4BitCount = bits; |
| 733 |
bmi.bmiHeader.bV4V4Compression = BI_RGB; |
| 734 |
|
| 735 |
/* when bits is 1, these will be used. |
| 736 |
* bmiColors[0] already zeroed from ZeroMemory() |
| 737 |
*/ |
| 738 |
bmi.bmiColors[1].rgbBlue = 0xFF; |
| 739 |
bmi.bmiColors[1].rgbGreen = 0xFF; |
| 740 |
bmi.bmiColors[1].rgbRed = 0xFF; |
| 741 |
|
| 742 |
hdc = GetDC (NULL); |
| 743 |
if (!hdc) |
| 744 |
{ |
| 745 |
WIN32_GDI_FAILED ("GetDC"); |
| 746 |
return NULL; |
| 747 |
} |
| 748 |
hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bmi, DIB_RGB_COLORS, |
| 749 |
(PVOID *) outdata, NULL, (DWORD)0); |
| 750 |
if (hBitmap == NULL) |
| 751 |
WIN32_GDI_FAILED ("CreateDIBSection"); |
| 752 |
ReleaseDC (NULL, hdc); |
| 753 |
|
| 754 |
return hBitmap; |
| 755 |
} |
| 756 |
|
| 757 |
static gboolean |
| 758 |
pixbuf_to_hbitmaps_alpha_winxp (GdkPixbuf *pixbuf, |
| 759 |
HBITMAP *color, |
| 760 |
HBITMAP *mask) |
| 761 |
{ |
| 762 |
/* Based on code from |
| 763 |
* http://www.dotnet247.com/247reference/msgs/13/66301.aspx |
| 764 |
*/ |
| 765 |
HBITMAP hColorBitmap, hMaskBitmap; |
| 766 |
guchar *indata, *inrow; |
| 767 |
guchar *colordata, *colorrow, *maskdata, *maskbyte; |
| 768 |
gint width, height, size, i, i_offset, j, j_offset, rowstride; |
| 769 |
guint maskstride, mask_bit; |
| 770 |
|
| 771 |
width = gdk_pixbuf_get_width (pixbuf); /* width of icon */ |
| 772 |
height = gdk_pixbuf_get_height (pixbuf); /* height of icon */ |
| 773 |
|
| 774 |
/* The bitmaps are created square */ |
| 775 |
size = MAX (width, height); |
| 776 |
|
| 777 |
hColorBitmap = create_alpha_bitmap (size, &colordata); |
| 778 |
if (!hColorBitmap) |
| 779 |
return FALSE; |
| 780 |
hMaskBitmap = create_color_bitmap (size, &maskdata, 1); |
| 781 |
if (!hMaskBitmap) |
| 782 |
{ |
| 783 |
DeleteObject (hColorBitmap); |
| 784 |
return FALSE; |
| 785 |
} |
| 786 |
|
| 787 |
/* MSDN says mask rows are aligned to "LONG" boundaries */ |
| 788 |
maskstride = (((size + 31) & ~31) >> 3); |
| 789 |
|
| 790 |
indata = gdk_pixbuf_get_pixels (pixbuf); |
| 791 |
rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
| 792 |
|
| 793 |
if (width > height) |
| 794 |
{ |
| 795 |
i_offset = 0; |
| 796 |
j_offset = (width - height) / 2; |
| 797 |
} |
| 798 |
else |
| 799 |
{ |
| 800 |
i_offset = (height - width) / 2; |
| 801 |
j_offset = 0; |
| 802 |
} |
| 803 |
|
| 804 |
for (j = 0; j < height; j++) |
| 805 |
{ |
| 806 |
colorrow = colordata + 4*(j+j_offset)*size + 4*i_offset; |
| 807 |
maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8; |
| 808 |
mask_bit = (0x80 >> (i_offset % 8)); |
| 809 |
inrow = indata + (height-j-1)*rowstride; |
| 810 |
for (i = 0; i < width; i++) |
| 811 |
{ |
| 812 |
colorrow[4*i+0] = inrow[4*i+2]; |
| 813 |
colorrow[4*i+1] = inrow[4*i+1]; |
| 814 |
colorrow[4*i+2] = inrow[4*i+0]; |
| 815 |
colorrow[4*i+3] = inrow[4*i+3]; |
| 816 |
if (inrow[4*i+3] == 0) |
| 817 |
maskbyte[0] |= mask_bit; /* turn ON bit */ |
| 818 |
else |
| 819 |
maskbyte[0] &= ~mask_bit; /* turn OFF bit */ |
| 820 |
mask_bit >>= 1; |
| 821 |
if (mask_bit == 0) |
| 822 |
{ |
| 823 |
mask_bit = 0x80; |
| 824 |
maskbyte++; |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
*color = hColorBitmap; |
| 830 |
*mask = hMaskBitmap; |
| 831 |
|
| 832 |
return TRUE; |
| 833 |
} |
| 834 |
|
| 835 |
static gboolean |
| 836 |
pixbuf_to_hbitmaps_normal (GdkPixbuf *pixbuf, |
| 837 |
HBITMAP *color, |
| 838 |
HBITMAP *mask) |
| 839 |
{ |
| 840 |
/* Based on code from |
| 841 |
* http://www.dotnet247.com/247reference/msgs/13/66301.aspx |
| 842 |
*/ |
| 843 |
HBITMAP hColorBitmap, hMaskBitmap; |
| 844 |
guchar *indata, *inrow; |
| 845 |
guchar *colordata, *colorrow, *maskdata, *maskbyte; |
| 846 |
gint width, height, size, i, i_offset, j, j_offset, rowstride, nc, bmstride; |
| 847 |
gboolean has_alpha; |
| 848 |
guint maskstride, mask_bit; |
| 849 |
|
| 850 |
width = gdk_pixbuf_get_width (pixbuf); /* width of icon */ |
| 851 |
height = gdk_pixbuf_get_height (pixbuf); /* height of icon */ |
| 852 |
|
| 853 |
/* The bitmaps are created square */ |
| 854 |
size = MAX (width, height); |
| 855 |
|
| 856 |
hColorBitmap = create_color_bitmap (size, &colordata, 24); |
| 857 |
if (!hColorBitmap) |
| 858 |
return FALSE; |
| 859 |
hMaskBitmap = create_color_bitmap (size, &maskdata, 1); |
| 860 |
if (!hMaskBitmap) |
| 861 |
{ |
| 862 |
DeleteObject (hColorBitmap); |
| 863 |
return FALSE; |
| 864 |
} |
| 865 |
|
| 866 |
/* rows are always aligned on 4-byte boundarys */ |
| 867 |
bmstride = size * 3; |
| 868 |
if (bmstride % 4 != 0) |
| 869 |
bmstride += 4 - (bmstride % 4); |
| 870 |
|
| 871 |
/* MSDN says mask rows are aligned to "LONG" boundaries */ |
| 872 |
maskstride = (((size + 31) & ~31) >> 3); |
| 873 |
|
| 874 |
indata = gdk_pixbuf_get_pixels (pixbuf); |
| 875 |
rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
| 876 |
nc = gdk_pixbuf_get_n_channels (pixbuf); |
| 877 |
has_alpha = gdk_pixbuf_get_has_alpha (pixbuf); |
| 878 |
|
| 879 |
if (width > height) |
| 880 |
{ |
| 881 |
i_offset = 0; |
| 882 |
j_offset = (width - height) / 2; |
| 883 |
} |
| 884 |
else |
| 885 |
{ |
| 886 |
i_offset = (height - width) / 2; |
| 887 |
j_offset = 0; |
| 888 |
} |
| 889 |
|
| 890 |
for (j = 0; j < height; j++) |
| 891 |
{ |
| 892 |
colorrow = colordata + (j+j_offset)*bmstride + 3*i_offset; |
| 893 |
maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8; |
| 894 |
mask_bit = (0x80 >> (i_offset % 8)); |
| 895 |
inrow = indata + (height-j-1)*rowstride; |
| 896 |
for (i = 0; i < width; i++) |
| 897 |
{ |
| 898 |
if (has_alpha && inrow[nc*i+3] < 128) |
| 899 |
{ |
| 900 |
colorrow[3*i+0] = colorrow[3*i+1] = colorrow[3*i+2] = 0; |
| 901 |
maskbyte[0] |= mask_bit; /* turn ON bit */ |
| 902 |
} |
| 903 |
else |
| 904 |
{ |
| 905 |
colorrow[3*i+0] = inrow[nc*i+2]; |
| 906 |
colorrow[3*i+1] = inrow[nc*i+1]; |
| 907 |
colorrow[3*i+2] = inrow[nc*i+0]; |
| 908 |
maskbyte[0] &= ~mask_bit; /* turn OFF bit */ |
| 909 |
} |
| 910 |
mask_bit >>= 1; |
| 911 |
if (mask_bit == 0) |
| 912 |
{ |
| 913 |
mask_bit = 0x80; |
| 914 |
maskbyte++; |
| 915 |
} |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
*color = hColorBitmap; |
| 920 |
*mask = hMaskBitmap; |
| 921 |
|
| 922 |
return TRUE; |
| 923 |
} |
| 924 |
|
| 925 |
static HICON |
| 926 |
pixbuf_to_hicon (GdkPixbuf *pixbuf, |
| 927 |
gboolean is_icon, |
| 928 |
gint x, |
| 929 |
gint y) |
| 930 |
{ |
| 931 |
ICONINFO ii; |
| 932 |
HICON icon; |
| 933 |
gboolean success; |
| 934 |
|
| 935 |
if (pixbuf == NULL) |
| 936 |
return NULL; |
| 937 |
|
| 938 |
if (_gdk_win32_pixbuf_to_hicon_supports_alpha() && gdk_pixbuf_get_has_alpha (pixbuf)) |
| 939 |
success = pixbuf_to_hbitmaps_alpha_winxp (pixbuf, &ii.hbmColor, &ii.hbmMask); |
| 940 |
else |
| 941 |
success = pixbuf_to_hbitmaps_normal (pixbuf, &ii.hbmColor, &ii.hbmMask); |
| 942 |
|
| 943 |
if (!success) |
| 944 |
return NULL; |
| 945 |
|
| 946 |
ii.fIcon = is_icon; |
| 947 |
ii.xHotspot = x; |
| 948 |
ii.yHotspot = y; |
| 949 |
icon = CreateIconIndirect (&ii); |
| 950 |
DeleteObject (ii.hbmColor); |
| 951 |
DeleteObject (ii.hbmMask); |
| 952 |
return icon; |
| 953 |
} |
| 954 |
|
| 955 |
HICON |
| 956 |
_gdk_win32_pixbuf_to_hicon (GdkPixbuf *pixbuf) |
| 957 |
{ |
| 958 |
return pixbuf_to_hicon (pixbuf, TRUE, 0, 0); |
| 959 |
} |
| 960 |
|
| 961 |
HICON |
| 962 |
_gdk_win32_pixbuf_to_hcursor (GdkPixbuf *pixbuf, |
| 963 |
gint x_hotspot, |
| 964 |
gint y_hotspot) |
| 965 |
{ |
| 966 |
return pixbuf_to_hicon (pixbuf, FALSE, x_hotspot, y_hotspot); |
| 967 |
} |
| 968 |
|
| 969 |
gboolean |
| 970 |
_gdk_win32_pixbuf_to_hicon_supports_alpha (void) |
| 971 |
{ |
| 972 |
static gboolean is_win_xp=FALSE, is_win_xp_checked=FALSE; |
| 973 |
|
| 974 |
if (!is_win_xp_checked) |
| 975 |
{ |
| 976 |
OSVERSIONINFO version; |
| 977 |
|
| 978 |
is_win_xp_checked = TRUE; |
| 979 |
|
| 980 |
memset (&version, 0, sizeof (version)); |
| 981 |
version.dwOSVersionInfoSize = sizeof (version); |
| 982 |
is_win_xp = GetVersionEx (&version) |
| 983 |
&& version.dwPlatformId == VER_PLATFORM_WIN32_NT |
| 984 |
&& (version.dwMajorVersion > 5 |
| 985 |
|| (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1)); |
| 986 |
} |
| 987 |
return is_win_xp; |
| 988 |
} |
| 989 |
|
| 990 |
HICON |
| 991 |
gdk_win32_pixbuf_to_hicon_libgtk_only (GdkPixbuf *pixbuf) |
| 992 |
{ |
| 993 |
return _gdk_win32_pixbuf_to_hicon (pixbuf); |
| 994 |
} |