Debian Codegraph @

src/win/wmcursor.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Windows mouse cursors.
12 *
13 * By Evert Glebbeek.
14 *
15 * Adapted for Allegro 4.9 by Peter Wang.
16 *
17 * GDI code adapted from src/win/gdi.c.
18 *
19 * See readme.txt for copyright information.
20 */
21
22
23 #include "allegro5/allegro.h"
24 #include "allegro5/internal/aintern.h"
25 #include "allegro5/internal/aintern_bitmap.h"
26 #include "allegro5/platform/aintwin.h"
27
28
29 static void local_stretch_blit_to_hdc(ALLEGRO_BITMAP *bitmap, HDC dc,
30 int src_x, int src_y, int src_w, int src_h,
31 int dest_x, int dest_y, int dest_w, int dest_h);
32 static void local_draw_to_hdc(HDC dc, ALLEGRO_BITMAP *bitmap, int x, int y);
33
34
35 HICON _al_win_create_icon(HWND wnd,
36 ALLEGRO_BITMAP *sprite, int xfocus, int yfocus, bool is_cursor, bool resize)
37 {
38 int x, y;
39 int sys_sm_cx, sys_sm_cy;
40 HDC h_dc;
41 HDC h_and_dc;
42 HDC h_xor_dc;
43 ICONINFO iconinfo;
44 HBITMAP and_mask;
45 HBITMAP xor_mask;
46 HBITMAP hOldAndMaskBitmap;
47 HBITMAP hOldXorMaskBitmap;
48 HICON icon;
49 bool was_locked;
50 ALLEGRO_BITMAP *tmp = sprite;
51
52 if (resize) {
53 if (is_cursor) {
54 /* Get allowed cursor size - Windows can't make cursors of arbitrary size */
55 sys_sm_cx = GetSystemMetrics(SM_CXCURSOR);
56 sys_sm_cy = GetSystemMetrics(SM_CYCURSOR);
57 }
58 else {
59 sys_sm_cx = GetSystemMetrics(SM_CXICON);
60 sys_sm_cy = GetSystemMetrics(SM_CYICON);
61 }
62
63 if ((tmp->w > sys_sm_cx) || (tmp->h > sys_sm_cy)) {
64 float ratio = tmp->w / (float)tmp->h;
65 int w, h;
66 ALLEGRO_STATE state;
67 if (ratio > 1) {
68 w = sys_sm_cx;
69 h = sys_sm_cy / ratio;
70 }
71 else {
72 w = sys_sm_cx * ratio;
73 h = sys_sm_cy;
74 }
75 al_store_state(&state, ALLEGRO_STATE_TARGET_BITMAP |
76 ALLEGRO_STATE_BLENDER);
77 tmp = _al_create_bitmap_params(al_get_current_display(), w, h,
78 al_get_bitmap_format(tmp), ALLEGRO_MEMORY_BITMAP, 0, 0);
79 al_set_target_bitmap(tmp);
80 al_clear_to_color(al_map_rgba_f(0, 0, 0, 0));
81 al_draw_scaled_bitmap(
82 sprite,
83 0, 0, sprite->w, sprite->h,
84 0, 0, w, h,
85 0
86 );
87 al_restore_state(&state);
88 }
89 }
90 else {
91 sys_sm_cx = al_get_bitmap_width(tmp);
92 sys_sm_cy = al_get_bitmap_height(tmp);
93 }
94
95 /* Create bitmap */
96 h_dc = GetDC(wnd);
97 h_xor_dc = CreateCompatibleDC(h_dc);
98 h_and_dc = CreateCompatibleDC(h_dc);
99
100 /* Prepare AND (monochrome) and XOR (colour) mask */
101 and_mask = CreateBitmap(sys_sm_cx, sys_sm_cy, 1, 1, NULL);
102 xor_mask = CreateCompatibleBitmap(h_dc, sys_sm_cx, sys_sm_cy);
103 hOldAndMaskBitmap = (HBITMAP) SelectObject(h_and_dc, and_mask);
104 hOldXorMaskBitmap = (HBITMAP) SelectObject(h_xor_dc, xor_mask);
105
106 /* Create transparent cursor */
107 for (y = 0; y < sys_sm_cy; y++) {
108 for (x = 0; x < sys_sm_cx; x++) {
109 SetPixel(h_and_dc, x, y, WINDOWS_RGB(255, 255, 255));
110 SetPixel(h_xor_dc, x, y, WINDOWS_RGB(0, 0, 0));
111 }
112 }
113
114 /* Lock sprite to speed up repeated get pixel calls. */
115 was_locked = al_is_bitmap_locked(tmp);
116 if (!was_locked) {
117 al_lock_bitmap(tmp, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY);
118 }
119
120 local_draw_to_hdc(h_xor_dc, tmp, 0, 0);
121
122 /* Make cursor background transparent */
123 for (y = 0; y < tmp->h; y++) {
124 for (x = 0; x < tmp->w; x++) {
125 ALLEGRO_COLOR c;
126 unsigned char r, g, b, a;
127
128 c = al_get_pixel(tmp, x, y);
129 al_unmap_rgba(c, &r, &g, &b, &a);
130 if (a != 0) {
131 /* Don't touch XOR value */
132 SetPixel(h_and_dc, x, y, 0);
133 }
134 else {
135 /* No need to touch AND value */
136 SetPixel(h_xor_dc, x, y, WINDOWS_RGB(0, 0, 0));
137 }
138 }
139 }
140
141 if (!was_locked) {
142 al_unlock_bitmap(tmp);
143 }
144
145 SelectObject(h_and_dc, hOldAndMaskBitmap);
146 SelectObject(h_xor_dc, hOldXorMaskBitmap);
147 DeleteDC(h_and_dc);
148 DeleteDC(h_xor_dc);
149 ReleaseDC(wnd, h_dc);
150
151 iconinfo.fIcon = is_cursor ? false : true;
152 iconinfo.xHotspot = xfocus;
153 iconinfo.yHotspot = yfocus;
154 iconinfo.hbmMask = and_mask;
155 iconinfo.hbmColor = xor_mask;
156
157 icon = CreateIconIndirect(&iconinfo);
158
159 DeleteObject(and_mask);
160 DeleteObject(xor_mask);
161
162 if (sprite != tmp) {
163 al_destroy_bitmap(tmp);
164 }
165
166 return icon;
167 }
168
169
170 ALLEGRO_MOUSE_CURSOR *_al_win_create_mouse_cursor(ALLEGRO_BITMAP *sprite,
171 int xfocus, int yfocus)
172 {
173 HWND wnd;
174 HCURSOR hcursor;
175 ALLEGRO_MOUSE_CURSOR_WIN *win_cursor;
176
177 /* A null HWND retrieves the DC for the entire screen. */
178 wnd = NULL;
179
180 hcursor = (HCURSOR)_al_win_create_icon(wnd, sprite, xfocus, yfocus, true, true);
181 if (!hcursor) {
182 return NULL;
183 }
184
185 win_cursor = al_malloc(sizeof *win_cursor);
186 if (!win_cursor) {
187 DestroyIcon(hcursor);
188 return NULL;
189 }
190
191 win_cursor->hcursor = hcursor;
192 return (ALLEGRO_MOUSE_CURSOR *)win_cursor;
193 }
194
195
196
197 void _al_win_destroy_mouse_cursor(ALLEGRO_MOUSE_CURSOR *cursor)
198 {
199 ALLEGRO_MOUSE_CURSOR_WIN *win_cursor = (ALLEGRO_MOUSE_CURSOR_WIN *) cursor;
200 ALLEGRO_SYSTEM *sys = al_get_system_driver();
201 unsigned i;
202
203 ASSERT(win_cursor);
204
205 /* XXX not at all thread safe */
206
207 for (i = 0; i < _al_vector_size(&sys->displays); i++) {
208 ALLEGRO_DISPLAY_WIN **slot = _al_vector_ref(&sys->displays, i);
209 ALLEGRO_DISPLAY_WIN *win_display = *slot;
210
211 if (win_cursor->hcursor == win_display->mouse_selected_hcursor) {
212 _al_win_set_system_mouse_cursor((ALLEGRO_DISPLAY *)win_display,
213 ALLEGRO_SYSTEM_MOUSE_CURSOR_ARROW);
214 }
215 }
216
217 DestroyIcon(win_cursor->hcursor);
218 al_free(win_cursor);
219 }
220
221
222
223 bool _al_win_set_mouse_cursor(ALLEGRO_DISPLAY *display,
224 ALLEGRO_MOUSE_CURSOR *cursor)
225 {
226 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *) display;
227 ALLEGRO_MOUSE_CURSOR_WIN *win_cursor = (ALLEGRO_MOUSE_CURSOR_WIN *) cursor;
228
229 ASSERT(win_cursor);
230 ASSERT(win_cursor->hcursor);
231
232 win_display->mouse_selected_hcursor = win_cursor->hcursor;
233
234 if (win_display->mouse_cursor_shown) {
235 POINT p;
236
237 SetCursor(win_cursor->hcursor);
238
239 /* Windows is too stupid to actually display the mouse pointer when we
240 * change it and waits until it is moved, so we have to generate a fake
241 * mouse move to actually show the cursor.
242 */
243 GetCursorPos(&p);
244 SetCursorPos(p.x, p.y);
245 }
246
247 return true;
248 }
249
250
251 bool _al_win_show_mouse_cursor(ALLEGRO_DISPLAY *display)
252 {
253 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *) display;
254 ALLEGRO_MOUSE_CURSOR_WIN tmp_cursor;
255 ALLEGRO_MOUSE_CURSOR_WIN *tmp_cursor_ptr = &tmp_cursor;
256
257 /* XXX do we need this? */
258 if (!win_display->mouse_selected_hcursor) {
259 _al_win_set_system_mouse_cursor(display, ALLEGRO_SYSTEM_MOUSE_CURSOR_ARROW);
260 }
261
262 tmp_cursor.hcursor = win_display->mouse_selected_hcursor;
263 win_display->mouse_cursor_shown = true;
264 win_display->hide_mouse_on_move = false;
265 _al_win_set_mouse_cursor(display, (ALLEGRO_MOUSE_CURSOR *)tmp_cursor_ptr);
266
267 return true;
268 }
269
270 bool _al_win_hide_mouse_cursor(ALLEGRO_DISPLAY *display)
271 {
272 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *) display;
273 win_display->mouse_cursor_shown = false;
274 PostMessage(win_display->window, WM_SETCURSOR, 0, 0);
275
276 return true;
277 }
278
279
280 static HCURSOR system_cursor_to_hcursor(ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id)
281 {
282 switch (cursor_id) {
283 case ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT:
284 case ALLEGRO_SYSTEM_MOUSE_CURSOR_ARROW:
285 return LoadCursor(NULL, IDC_ARROW);
286 case ALLEGRO_SYSTEM_MOUSE_CURSOR_BUSY:
287 return LoadCursor(NULL, IDC_WAIT);
288 case ALLEGRO_SYSTEM_MOUSE_CURSOR_QUESTION:
289 return LoadCursor(NULL, IDC_HELP);
290 case ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT:
291 return LoadCursor(NULL, IDC_IBEAM);
292 case ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE:
293 return LoadCursor(NULL, IDC_SIZEALL);
294 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N:
295 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_S:
296 return LoadCursor(NULL, IDC_SIZENS);
297 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E:
298 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_W:
299 return LoadCursor(NULL, IDC_SIZEWE);
300 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE:
301 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_SW:
302 return LoadCursor(NULL, IDC_SIZENESW);
303 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW:
304 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_SE:
305 return LoadCursor(NULL, IDC_SIZENWSE);
306 case ALLEGRO_SYSTEM_MOUSE_CURSOR_PROGRESS:
307 return LoadCursor(NULL, IDC_APPSTARTING);
308 case ALLEGRO_SYSTEM_MOUSE_CURSOR_PRECISION:
309 return LoadCursor(NULL, IDC_CROSS);
310 case ALLEGRO_SYSTEM_MOUSE_CURSOR_LINK:
311 return LoadCursor(NULL, IDC_HAND);
312 case ALLEGRO_SYSTEM_MOUSE_CURSOR_ALT_SELECT:
313 return LoadCursor(NULL, IDC_UPARROW);
314 case ALLEGRO_SYSTEM_MOUSE_CURSOR_UNAVAILABLE:
315 return LoadCursor(NULL, IDC_NO);
316 default:
317 return NULL;
318 }
319 }
320
321
322 bool _al_win_set_system_mouse_cursor(ALLEGRO_DISPLAY *display,
323 ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id)
324 {
325 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *) display;
326 HCURSOR wc;
327
328 wc = system_cursor_to_hcursor(cursor_id);
329 if (!wc) {
330 return false;
331 }
332
333 win_display->mouse_selected_hcursor = wc;
334
335 if (win_display->mouse_cursor_shown) {
336 /*
337 MySetCursor(wc);
338 PostMessage(wgl_display->window, WM_MOUSEMOVE, 0, 0);
339 */
340 ALLEGRO_MOUSE_CURSOR_WIN tmp_cursor;
341 ALLEGRO_MOUSE_CURSOR_WIN *tmp_cursor_ptr = &tmp_cursor;
342 tmp_cursor.hcursor = wc;
343 _al_win_set_mouse_cursor(display, (ALLEGRO_MOUSE_CURSOR *)tmp_cursor_ptr);
344 }
345 return true;
346 }
347
348
349
350 /* GDI stuff - this really belongs elsewhere */
351
352
353 /* get_bitmap_info:
354 * Returns a BITMAPINFO structure suited to an ALLEGRO_BITMAP.
355 * You have to free the memory allocated by this function.
356 *
357 * This version always returns a 32-bit BITMAPINFO.
358 */
359 static BITMAPINFO *get_bitmap_info(ALLEGRO_BITMAP *bitmap)
360 {
361 BITMAPINFO *bi;
362 int i;
363
364 bi = (BITMAPINFO *) al_malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256);
365
366 ZeroMemory(&bi->bmiHeader, sizeof(BITMAPINFOHEADER));
367
368 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
369 bi->bmiHeader.biBitCount = 32;
370 bi->bmiHeader.biPlanes = 1;
371 bi->bmiHeader.biWidth = al_get_bitmap_width(bitmap);
372 bi->bmiHeader.biHeight = -al_get_bitmap_height(bitmap);
373 bi->bmiHeader.biClrUsed = 256;
374 bi->bmiHeader.biCompression = BI_RGB;
375
376 for (i = 0; i < 256; i++) {
377 bi->bmiColors[i].rgbRed = 0;
378 bi->bmiColors[i].rgbGreen = 0;
379 bi->bmiColors[i].rgbBlue = 0;
380 bi->bmiColors[i].rgbReserved = 0;
381 }
382
383 return bi;
384 }
385
386
387
388 /* get_dib_from_bitmap_32:
389 * Creates a Windows device-independent bitmap (DIB) from an Allegro BITMAP.
390 * You have to free the memory allocated by this function.
391 *
392 * This version always creates a 32-bit DIB.
393 */
394 static BYTE *get_dib_from_bitmap_32(ALLEGRO_BITMAP *bitmap)
395 {
396 int w, h;
397 int x, y;
398 int pitch;
399 BYTE *pixels;
400 BYTE *dst;
401
402 w = al_get_bitmap_width(bitmap);
403 h = al_get_bitmap_height(bitmap);
404 pitch = w * 4;
405
406 pixels = (BYTE *) al_malloc(h * pitch);
407 if (!pixels)
408 return NULL;
409
410 for (y = 0; y < h; y++) {
411 dst = pixels + y * pitch;
412
413 for (x = 0; x < w; x++) {
414 ALLEGRO_COLOR col;
415 unsigned char r, g, b, a;
416
417 col = al_get_pixel(bitmap, x, y);
418 al_unmap_rgba(col, &r, &g, &b, &a);
419
420 /* BGR */
421 dst[0] = b;
422 dst[1] = g;
423 dst[2] = r;
424 dst[3] = a;
425
426 dst += 4;
427 }
428 }
429
430 return pixels;
431 }
432
433
434
435 /* draw_to_hdc:
436 * Draws an entire Allegro BITMAP to a Windows DC. Has a syntax similar to
437 * draw_sprite().
438 */
439 static void local_draw_to_hdc(HDC dc, ALLEGRO_BITMAP *bitmap, int x, int y)
440 {
441 int w = al_get_bitmap_width(bitmap);
442 int h = al_get_bitmap_height(bitmap);
443 local_stretch_blit_to_hdc(bitmap, dc, 0, 0, w, h, x, y, w, h);
444 }
445
446
447
448 /* stretch_blit_to_hdc:
449 * Blits an Allegro BITMAP to a Windows DC. Has a syntax similar to
450 * stretch_blit().
451 */
452 static void local_stretch_blit_to_hdc(ALLEGRO_BITMAP *bitmap, HDC dc,
453 int src_x, int src_y, int src_w, int src_h,
454 int dest_x, int dest_y, int dest_w, int dest_h)
455 {
456 const int bitmap_h = al_get_bitmap_height(bitmap);
457 const int bottom_up_src_y = bitmap_h - src_y - src_h;
458 BYTE *pixels;
459 BITMAPINFO *bi;
460
461 bi = get_bitmap_info(bitmap);
462 pixels = get_dib_from_bitmap_32(bitmap);
463
464 /* Windows treats all source bitmaps as bottom-up when using StretchDIBits
465 * unless the source (x,y) is (0,0). To work around this buggy behavior, we
466 * can use negative heights to reverse the direction of the blits.
467 *
468 * See <http://wiki.allegro.cc/StretchDIBits> for a detailed explanation.
469 */
470 if (bottom_up_src_y == 0 && src_x == 0 && src_h != bitmap_h) {
471 StretchDIBits(dc, dest_x, dest_h+dest_y-1, dest_w, -dest_h,
472 src_x, bitmap_h - src_y + 1, src_w, -src_h, pixels, bi,
473 DIB_RGB_COLORS, SRCCOPY);
474 }
475 else {
476 StretchDIBits(dc, dest_x, dest_y, dest_w, dest_h,
477 src_x, bottom_up_src_y, src_w, src_h, pixels, bi,
478 DIB_RGB_COLORS, SRCCOPY);
479 }
480
481 al_free(pixels);
482 al_free(bi);
483 }
484
485
486
487 /* vim: set sts=3 sw=3 et: */