Release 941017
[wine/gsoc-2012-control.git] / windows / cursor.c
blob1c866d16dfe47a935ad25d5d425f750f2c86b9c7
1 /*
2 * WINE
3 */
4 static char Copyright[] = "Copyright Martin Ayotte, 1993";
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <X11/cursorfont.h>
14 #include <X11/Xlib.h>
15 #include "prototypes.h"
16 #include "windows.h"
17 #include "win.h"
18 #include "gdi.h"
19 #include "wine.h"
20 #include "cursor.h"
21 #include "stddebug.h"
22 /* #define DEBUG_CURSOR /* */
23 /* #undef DEBUG_CURSOR /* */
24 /* #define DEBUG_RESOURCE /* */
25 /* #undef DEBUG_RESOURCE /* */
26 #include "debug.h"
28 static int ShowCursCount = 0;
29 static HCURSOR hActiveCursor;
30 static HCURSOR hEmptyCursor = 0;
31 RECT ClipCursorRect;
32 extern HINSTANCE hSysRes;
33 extern int desktopX, desktopY; /* misc/main.c */
35 static struct { LPSTR name; HCURSOR cursor; } system_cursor[] =
37 { IDC_ARROW, 0 },
38 { IDC_IBEAM, 0 },
39 { IDC_WAIT, 0 },
40 { IDC_CROSS, 0 },
41 { IDC_UPARROW, 0 },
42 { IDC_SIZE, 0 },
43 { IDC_ICON, 0 },
44 { IDC_SIZENWSE, 0 },
45 { IDC_SIZENESW, 0 },
46 { IDC_SIZEWE, 0 },
47 { IDC_SIZENS, 0 }
50 #define NB_SYS_CURSORS (sizeof(system_cursor)/sizeof(system_cursor[0]))
53 /**********************************************************************
54 * LoadCursor [USER.173]
56 HCURSOR LoadCursor(HANDLE instance, LPSTR cursor_name)
58 XColor bkcolor;
59 XColor fgcolor;
60 HCURSOR hCursor;
61 HANDLE rsc_mem;
62 WORD *lp;
63 CURSORDESCRIP *lpcurdesc;
64 CURSORALLOC *lpcur;
65 HDC hdc;
66 int i, j, image_size;
68 dprintf_resource(stddeb,"LoadCursor: instance = %04x, name = %08x\n",
69 instance, cursor_name);
70 if (!instance)
72 for (i = 0; i < NB_SYS_CURSORS; i++)
73 if (system_cursor[i].name == cursor_name)
75 hCursor = system_cursor[i].cursor;
76 break;
78 if (i == NB_SYS_CURSORS) return 0;
79 if (hCursor) return hCursor;
81 hCursor = GlobalAlloc(GMEM_MOVEABLE, sizeof(CURSORALLOC) + 1024L);
82 if (hCursor == (HCURSOR)NULL) return 0;
83 if (!instance) system_cursor[i].cursor = hCursor;
85 dprintf_cursor(stddeb,"LoadCursor Alloc hCursor=%X\n", hCursor);
86 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
87 memset(lpcur, 0, sizeof(CURSORALLOC));
88 if (instance == (HANDLE)NULL) {
89 instance = hSysRes;
90 switch((LONG)cursor_name) {
91 case IDC_ARROW:
92 lpcur->xcursor = XCreateFontCursor(display, XC_top_left_arrow);
93 GlobalUnlock(hCursor);
94 return hCursor;
95 case IDC_CROSS:
96 lpcur->xcursor = XCreateFontCursor(display, XC_crosshair);
97 GlobalUnlock(hCursor);
98 return hCursor;
99 case IDC_IBEAM:
100 lpcur->xcursor = XCreateFontCursor(display, XC_xterm);
101 GlobalUnlock(hCursor);
102 return hCursor;
103 case IDC_WAIT:
104 lpcur->xcursor = XCreateFontCursor(display, XC_watch);
105 GlobalUnlock(hCursor);
106 return hCursor;
107 case IDC_SIZENS:
108 lpcur->xcursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
109 GlobalUnlock(hCursor);
110 return hCursor;
111 case IDC_SIZEWE:
112 lpcur->xcursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
113 GlobalUnlock(hCursor);
114 return hCursor;
115 case IDC_SIZENWSE:
116 case IDC_SIZENESW:
117 lpcur->xcursor = XCreateFontCursor(display, XC_fleur);
118 GlobalUnlock(hCursor);
119 return hCursor;
120 default:
121 break;
125 #if 1
126 lpcur->xcursor = XCreateFontCursor(display, XC_top_left_arrow);
127 GlobalUnlock(hCursor);
128 return hCursor;
129 #endif
131 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
132 rsc_mem = RSC_LoadResource(instance, cursor_name, NE_RSCTYPE_GROUP_CURSOR,
133 &image_size);
134 if (rsc_mem == (HANDLE)NULL) {
135 fprintf(stderr,"LoadCursor / Cursor %08X not Found !\n", cursor_name);
136 ReleaseDC(GetDesktopWindow(), hdc);
137 return 0;
139 lp = (WORD *)GlobalLock(rsc_mem);
140 if (lp == NULL) {
141 GlobalFree(rsc_mem);
142 ReleaseDC(GetDesktopWindow(), hdc);
143 return 0;
145 lpcurdesc = (CURSORDESCRIP *)(lp + 3);
146 dprintf_cursor(stddeb,"LoadCursor / image_size=%d\n", image_size);
147 dprintf_cursor(stddeb,"LoadCursor / curReserved=%X\n", *lp);
148 dprintf_cursor(stddeb,"LoadCursor / curResourceType=%X\n", *(lp + 1));
149 dprintf_cursor(stddeb,"LoadCursor / curResourceCount=%X\n", *(lp + 2));
150 dprintf_cursor(stddeb,"LoadCursor / cursor Width=%d\n",
151 (int)lpcurdesc->Width);
152 dprintf_cursor(stddeb,"LoadCursor / cursor Height=%d\n",
153 (int)lpcurdesc->Height);
154 dprintf_cursor(stddeb,"LoadCursor / cursor curXHotspot=%d\n",
155 (int)lpcurdesc->curXHotspot);
156 dprintf_cursor(stddeb,"LoadCursor / cursor curYHotspot=%d\n",
157 (int)lpcurdesc->curYHotspot);
158 dprintf_cursor(stddeb,"LoadCursor / cursor curDIBSize=%lX\n",
159 (DWORD)lpcurdesc->curDIBSize);
160 dprintf_cursor(stddeb,"LoadCursor / cursor curDIBOffset=%lX\n",
161 (DWORD)lpcurdesc->curDIBOffset);
162 lpcur->descriptor = *lpcurdesc;
163 GlobalUnlock(rsc_mem);
164 GlobalFree(rsc_mem);
165 rsc_mem = RSC_LoadResource(instance,
166 MAKEINTRESOURCE(lpcurdesc->curDIBOffset),
167 NE_RSCTYPE_CURSOR, &image_size);
168 if (rsc_mem == (HANDLE)NULL) {
169 fprintf(stderr,
170 "LoadCursor / Cursor %08X Bitmap not Found !\n", cursor_name);
171 ReleaseDC(GetDesktopWindow(), hdc);
172 return 0;
174 lp = (WORD *)GlobalLock(rsc_mem);
175 if (lp == NULL) {
176 GlobalFree(rsc_mem);
177 ReleaseDC(GetDesktopWindow(), hdc);
178 return 0;
180 lp++;
181 for (j = 0; j < 16; j++)
182 dprintf_cursor(stddeb,"%04X ", *(lp + j));
184 if (*lp == sizeof(BITMAPINFOHEADER))
185 lpcur->hBitmap = ConvertInfoBitmap(hdc, (BITMAPINFO *)lp);
186 else
188 lpcur->hBitmap = 0;
189 /* lp += sizeof(BITMAP); */
190 for (i = 0; i < 81; i++) {
191 char temp = *((char *)lp + 162 + i);
192 *((char *)lp + 162 + i) = *((char *)lp + 324 - i);
193 *((char *)lp + 324 - i) = temp;
195 lpcur->pixshape = XCreatePixmapFromBitmapData(
196 display, DefaultRootWindow(display),
197 ((char *)lp + 211), 32, 32,
199 lpcurdesc->Width / 2, lpcurdesc->Height / 4,
201 WhitePixel(display, DefaultScreen(display)),
202 BlackPixel(display, DefaultScreen(display)), 1);
203 lpcur->pixmask = XCreatePixmapFromBitmapData(
204 display, DefaultRootWindow(display),
205 ((char *)lp + 211), 32, 32,
206 WhitePixel(display, DefaultScreen(display)),
207 BlackPixel(display, DefaultScreen(display)), 1);
208 memset(&bkcolor, 0, sizeof(XColor));
209 memset(&fgcolor, 0, sizeof(XColor));
210 bkcolor.pixel = WhitePixel(display, DefaultScreen(display));
211 fgcolor.pixel = BlackPixel(display, DefaultScreen(display));
212 dprintf_cursor(stddeb,"LoadCursor / before XCreatePixmapCursor !\n");
213 lpcur->xcursor = XCreatePixmapCursor(display,
214 lpcur->pixshape, lpcur->pixmask,
215 &fgcolor, &bkcolor, lpcur->descriptor.curXHotspot,
216 lpcur->descriptor.curYHotspot);
217 GlobalUnlock(rsc_mem);
218 GlobalFree(rsc_mem);
220 hCursor = CreateCursor(instance, lpcur->descriptor.curXHotspot,
221 lpcur->descriptor.curYHotspot, 32, 32,
222 (LPSTR)lp + 211, , (LPSTR)lp + 211);
224 XFreePixmap(display, lpcur->pixshape);
225 XFreePixmap(display, lpcur->pixmask);
226 ReleaseDC(GetDesktopWindow(), hdc);
227 GlobalUnlock(hCursor);
228 return hCursor;
233 /**********************************************************************
234 * CreateCursor [USER.406]
236 HCURSOR CreateCursor(HANDLE instance, short nXhotspot, short nYhotspot,
237 short nWidth, short nHeight, LPSTR lpANDbitPlane, LPSTR lpXORbitPlane)
239 XColor bkcolor;
240 XColor fgcolor;
241 HCURSOR hCursor;
242 CURSORALLOC *lpcur;
243 BITMAP BitMap;
244 HBITMAP hBitMap;
245 HDC hMemDC;
246 HDC hdc;
248 dprintf_resource(stddeb,"CreateCursor: inst=%04x nXhotspot=%d nYhotspot=%d nWidth=%d nHeight=%d\n",
249 instance, nXhotspot, nYhotspot, nWidth, nHeight);
250 dprintf_resource(stddeb,"CreateCursor: inst=%04x lpANDbitPlane=%08X lpXORbitPlane=%08X\n",
251 instance, lpANDbitPlane, lpXORbitPlane);
253 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
254 hCursor = GlobalAlloc(GMEM_MOVEABLE, sizeof(CURSORALLOC) + 1024L);
255 if (hCursor == (HCURSOR)NULL) {
256 ReleaseDC(GetDesktopWindow(), hdc);
257 return 0;
259 dprintf_cursor(stddeb,"CreateCursor Alloc hCursor=%X\n", hCursor);
260 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
261 memset(lpcur, 0, sizeof(CURSORALLOC));
262 lpcur->descriptor.curXHotspot = nXhotspot;
263 lpcur->descriptor.curYHotspot = nYhotspot;
264 lpcur->pixshape = XCreatePixmapFromBitmapData(
265 display, DefaultRootWindow(display),
266 lpXORbitPlane, nWidth, nHeight,
267 WhitePixel(display, DefaultScreen(display)),
268 BlackPixel(display, DefaultScreen(display)), 1);
269 lpcur->pixmask = XCreatePixmapFromBitmapData(
270 display, DefaultRootWindow(display),
271 lpANDbitPlane, nWidth, nHeight,
272 WhitePixel(display, DefaultScreen(display)),
273 BlackPixel(display, DefaultScreen(display)), 1);
274 memset(&bkcolor, 0, sizeof(XColor));
275 memset(&fgcolor, 0, sizeof(XColor));
276 bkcolor.pixel = WhitePixel(display, DefaultScreen(display));
277 fgcolor.pixel = BlackPixel(display, DefaultScreen(display));
278 lpcur->xcursor = XCreatePixmapCursor(display,
279 lpcur->pixshape, lpcur->pixmask,
280 &fgcolor, &bkcolor, lpcur->descriptor.curXHotspot,
281 lpcur->descriptor.curYHotspot);
282 XFreePixmap(display, lpcur->pixshape);
283 XFreePixmap(display, lpcur->pixmask);
284 ReleaseDC(GetDesktopWindow(), hdc);
285 GlobalUnlock(hCursor);
286 return hCursor;
291 /**********************************************************************
292 * DestroyCursor [USER.458]
294 BOOL DestroyCursor(HCURSOR hCursor)
296 CURSORALLOC *lpcur;
297 if (hCursor == (HCURSOR)NULL) return FALSE;
298 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
299 if (lpcur->hBitmap != (HBITMAP)NULL) DeleteObject(lpcur->hBitmap);
300 GlobalUnlock(hCursor);
301 GlobalFree(hCursor);
302 return TRUE;
306 /**********************************************************************
307 * CURSOR_SetCursor
309 * Internal helper function for SetCursor() and ShowCursor().
311 static BOOL CURSOR_SetCursor( HCURSOR hCursor )
313 CURSORALLOC *lpcur;
315 if (!(lpcur = (CURSORALLOC *)GlobalLock(hCursor))) return FALSE;
316 if (rootWindow != DefaultRootWindow(display))
318 XDefineCursor( display, rootWindow, lpcur->xcursor );
320 else
322 HWND hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
323 while(hwnd)
325 Window win = WIN_GetXWindow( hwnd );
326 if (win) XDefineCursor( display, win, lpcur->xcursor );
327 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
330 GlobalUnlock( hCursor );
333 /**********************************************************************
334 * SetCursor [USER.69]
336 HCURSOR SetCursor(HCURSOR hCursor)
338 HCURSOR hOldCursor;
340 dprintf_cursor(stddeb,"SetCursor / hCursor=%04X !\n", hCursor);
341 hOldCursor = hActiveCursor;
342 hActiveCursor = hCursor;
343 if ((hCursor != hOldCursor) || (ShowCursCount < 0))
345 CURSOR_SetCursor( hCursor );
347 ShowCursCount = 0;
348 return hOldCursor;
352 /**********************************************************************
353 * GetCursor [USER.247]
355 HCURSOR GetCursor(void)
357 return hActiveCursor;
361 /**********************************************************************
362 * SetCursorPos [USER.70]
364 void SetCursorPos(short x, short y)
366 dprintf_cursor(stddeb,"SetCursorPos // x=%d y=%d\n", x, y);
367 XWarpPointer( display, None, rootWindow, 0, 0, 0, 0, x, y );
371 /**********************************************************************
372 * GetCursorPos [USER.17]
374 void GetCursorPos(LPPOINT lpRetPoint)
376 Window root, child;
377 int rootX, rootY;
378 int childX, childY;
379 unsigned int mousebut;
381 if (!lpRetPoint) return;
382 if (!XQueryPointer( display, rootWindow, &root, &child,
383 &rootX, &rootY, &childX, &childY, &mousebut ))
384 lpRetPoint->x = lpRetPoint->y = 0;
385 else
387 lpRetPoint->x = rootX + desktopX;
388 lpRetPoint->y = rootY + desktopY;
390 dprintf_cursor(stddeb,
391 "GetCursorPos // x=%d y=%d\n", lpRetPoint->x, lpRetPoint->y);
395 /**********************************************************************
396 * ShowCursor [USER.71]
398 int ShowCursor(BOOL bShow)
400 HCURSOR hCursor;
402 dprintf_cursor(stddeb, "ShowCursor(%d), count=%d\n", bShow, ShowCursCount);
404 if (bShow)
406 if (++ShowCursCount == 0) /* Time to show it */
407 CURSOR_SetCursor( hActiveCursor );
409 else /* Hide it */
411 if (--ShowCursCount == -1) /* Time to hide it */
413 if (!hEmptyCursor)
414 hEmptyCursor = CreateCursor( 0, 1, 1, 1, 1,
415 "\xFF\xFF", "\xFF\xFF" );
416 CURSOR_SetCursor( hEmptyCursor );
419 return 0;
423 /**********************************************************************
424 * ClipCursor [USER.16]
426 void ClipCursor(LPRECT lpNewClipRect)
428 CopyRect(&ClipCursorRect, lpNewClipRect);
432 /**********************************************************************
433 * GetClipCursor [USER.309]
435 void GetClipCursor(LPRECT lpRetClipRect)
437 if (lpRetClipRect != NULL)
438 CopyRect(lpRetClipRect, &ClipCursorRect);