Release 950522
[wine.git] / windows / cursor.c
blobf9e976e22fcdc073bd1598cdc45871581ef4ee26
1 /*
2 * WINE
3 static char Copyright[] = "Copyright Martin Ayotte, 1993";
4 */
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 "windows.h"
16 #include "win.h"
17 #include "gdi.h"
18 #include "neexe.h"
19 #include "wine.h"
20 #include "cursor.h"
21 #include "stddebug.h"
22 /* #define DEBUG_CURSOR */
23 /* #define DEBUG_RESOURCE */
24 #include "debug.h"
25 #include "arch.h"
27 static int ShowCursCount = 0;
28 static HCURSOR hActiveCursor;
29 static HCURSOR hEmptyCursor = 0;
30 RECT ClipCursorRect;
32 static struct { SEGPTR name; HCURSOR cursor; } system_cursor[] =
34 { IDC_ARROW, 0 },
35 { IDC_IBEAM, 0 },
36 { IDC_WAIT, 0 },
37 { IDC_CROSS, 0 },
38 { IDC_UPARROW, 0 },
39 { IDC_SIZE, 0 },
40 { IDC_ICON, 0 },
41 { IDC_SIZENWSE, 0 },
42 { IDC_SIZENESW, 0 },
43 { IDC_SIZEWE, 0 },
44 { IDC_SIZENS, 0 }
47 #define NB_SYS_CURSORS (sizeof(system_cursor)/sizeof(system_cursor[0]))
50 /**********************************************************************
51 * LoadCursor [USER.173]
53 HCURSOR LoadCursor(HANDLE instance, SEGPTR cursor_name)
55 HCURSOR hCursor;
56 HRSRC hRsrc;
57 HANDLE rsc_mem;
58 WORD *lp;
59 LONG *lpl,size;
60 CURSORDESCRIP *lpcurdesc;
61 CURSORALLOC *lpcur;
62 HDC hdc;
63 int i;
64 unsigned char *cp1,*cp2;
65 dprintf_resource(stddeb,"LoadCursor: instance = %04x, name = %08lx\n",
66 instance, cursor_name);
67 if (!instance)
69 for (i = 0; i < NB_SYS_CURSORS; i++)
70 if (system_cursor[i].name == cursor_name)
72 if (system_cursor[i].cursor) return system_cursor[i].cursor;
73 else break;
75 if (i == NB_SYS_CURSORS) return 0;
77 hCursor = GlobalAlloc(GMEM_MOVEABLE, sizeof(CURSORALLOC) + 1024L);
78 if (hCursor == (HCURSOR)NULL) return 0;
79 if (!instance) system_cursor[i].cursor = hCursor;
81 dprintf_cursor(stddeb,"LoadCursor Alloc hCursor=%X\n", hCursor);
82 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
83 memset(lpcur, 0, sizeof(CURSORALLOC));
84 if (instance == (HANDLE)NULL) {
85 switch((LONG)cursor_name) {
86 case IDC_ARROW:
87 lpcur->xcursor = XCreateFontCursor(display, XC_top_left_arrow);
88 GlobalUnlock(hCursor);
89 return hCursor;
90 case IDC_CROSS:
91 lpcur->xcursor = XCreateFontCursor(display, XC_crosshair);
92 GlobalUnlock(hCursor);
93 return hCursor;
94 case IDC_IBEAM:
95 lpcur->xcursor = XCreateFontCursor(display, XC_xterm);
96 GlobalUnlock(hCursor);
97 return hCursor;
98 case IDC_WAIT:
99 lpcur->xcursor = XCreateFontCursor(display, XC_watch);
100 GlobalUnlock(hCursor);
101 return hCursor;
102 case IDC_SIZENS:
103 lpcur->xcursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
104 GlobalUnlock(hCursor);
105 return hCursor;
106 case IDC_SIZEWE:
107 lpcur->xcursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
108 GlobalUnlock(hCursor);
109 return hCursor;
110 case IDC_SIZENWSE:
111 case IDC_SIZENESW:
112 lpcur->xcursor = XCreateFontCursor(display, XC_fleur);
113 GlobalUnlock(hCursor);
114 return hCursor;
115 default:
116 break;
120 #if 0
121 /* this code replaces all bitmap cursors with the default cursor */
122 lpcur->xcursor = XCreateFontCursor(display, XC_top_left_arrow);
123 GlobalUnlock(hCursor);
124 return hCursor;
125 #endif
127 if (!(hdc = GetDC(0))) return 0;
128 if (!(hRsrc = FindResource( instance, cursor_name, RT_GROUP_CURSOR )))
130 ReleaseDC(0, hdc);
131 return 0;
133 rsc_mem = LoadResource(instance, hRsrc );
134 if (rsc_mem == (HANDLE)NULL) {
135 fprintf(stderr,"LoadCursor / Cursor %08lx not Found !\n", cursor_name);
136 ReleaseDC(0, hdc);
137 return 0;
139 lp = (WORD *)LockResource(rsc_mem);
140 if (lp == NULL) {
141 FreeResource( rsc_mem );
142 ReleaseDC(0, hdc);
143 return 0;
145 lpcurdesc = (CURSORDESCRIP *)(lp + 3);
146 #if 0
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 #endif
163 lpcur->descriptor = *lpcurdesc;
164 FreeResource( rsc_mem );
165 if (!(hRsrc = FindResource( instance,
166 MAKEINTRESOURCE(lpcurdesc->curDIBOffset),
167 RT_CURSOR )))
169 ReleaseDC(0, hdc);
170 return 0;
172 rsc_mem = LoadResource(instance, hRsrc );
173 if (rsc_mem == (HANDLE)NULL) {
174 fprintf(stderr,
175 "LoadCursor / Cursor %08lx Bitmap not Found !\n", cursor_name);
176 ReleaseDC(0, hdc);
177 return 0;
179 lpl = (LONG *)LockResource(rsc_mem);
180 lpl++;
181 size = CONV_LONG (*lpl);
182 if (size == sizeof(BITMAPCOREHEADER)){
183 CONV_BITMAPCOREHEADER (lpl);
184 ((BITMAPINFOHEADER *)lpl)->biHeight /= 2;
185 lpcur->hBitmap = ConvertCoreBitmap( hdc, (BITMAPCOREHEADER *) lpl );
186 } else if (size == sizeof(BITMAPINFOHEADER)){
187 CONV_BITMAPINFO (lpl);
188 ((BITMAPINFOHEADER *)lpl)->biHeight /= 2;
189 lpcur->hBitmap = ConvertInfoBitmap( hdc, (BITMAPINFO *) lpl );
190 } else {
191 fprintf(stderr,"No bitmap for cursor?\n");
192 lpcur->hBitmap = 0;
194 lpl = (char *)lpl + size + 8;
195 /* This is rather strange! The data is stored *BACKWARDS* and */
196 /* mirrored! But why?? FIXME: the image must be flipped at the Y */
197 /* axis, either here or in CreateCusor(); */
198 size = lpcur->descriptor.Height/2 * ((lpcur->descriptor.Width+7)/8);
199 #if 0
200 dprintf_cursor(stddeb,"Before:\n");
201 for(i=0;i<2*size;i++) {
202 dprintf_cursor(stddeb,"%02x ",((unsigned char *)lpl)[i]);
203 if ((i & 7) == 7) dprintf_cursor(stddeb,"\n");
205 #endif
206 cp1 = (char *)lpl;
207 cp2 = cp1+2*size;
208 for(i = 0; i < size; i++) {
209 char tmp=*--cp2;
210 *cp2 = *cp1;
211 *cp1++ = tmp;
213 #if 0
214 dprintf_cursor(stddeb,"After:\n");
215 for(i=0;i<2*size;i++) {
216 dprintf_cursor(stddeb,"%02x ",((unsigned char *)lpl)[i]);
217 if ((i & 7) == 7) dprintf_cursor(stddeb,"\n");
219 #endif
220 hCursor = CreateCursor(instance, lpcur->descriptor.curXHotspot,
221 lpcur->descriptor.curYHotspot, lpcur->descriptor.Width,
222 lpcur->descriptor.Height/2,
223 (LPSTR)lpl, ((LPSTR)lpl)+size);
225 FreeResource( rsc_mem );
226 GlobalUnlock(hCursor);
227 ReleaseDC(0,hdc);
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 HCURSOR hCursor;
240 CURSORALLOC *lpcur;
241 HDC hdc;
242 int bpllen = (nWidth + 7)/8 * nHeight;
243 char *tmpbpl = malloc(bpllen);
244 int i;
246 XColor bkcolor,fgcolor;
247 Colormap cmap = XDefaultColormap(display,XDefaultScreen(display));
249 dprintf_resource(stddeb,"CreateCursor: inst=%04x nXhotspot=%d nYhotspot=%d nWidth=%d nHeight=%d\n",
250 instance, nXhotspot, nYhotspot, nWidth, nHeight);
251 dprintf_resource(stddeb,"CreateCursor: inst=%04x lpANDbitPlane=%p lpXORbitPlane=%p\n",
252 instance, lpANDbitPlane, lpXORbitPlane);
254 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
255 hCursor = GlobalAlloc(GMEM_MOVEABLE, sizeof(CURSORALLOC) + 1024L);
256 if (hCursor == (HCURSOR)NULL) {
257 ReleaseDC(GetDesktopWindow(), hdc);
258 return 0;
260 dprintf_cursor(stddeb,"CreateCursor Alloc hCursor=%X\n", hCursor);
261 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
262 memset(lpcur, 0, sizeof(CURSORALLOC));
263 lpcur->descriptor.curXHotspot = nXhotspot;
264 lpcur->descriptor.curYHotspot = nYhotspot;
265 for(i=0; i<bpllen; i++) tmpbpl[i] = ~lpANDbitPlane[i];
266 lpcur->pixmask = XCreatePixmapFromBitmapData(
267 display, DefaultRootWindow(display),
268 tmpbpl, nWidth, nHeight, 1, 0, 1);
269 for(i=0; i<bpllen; i++) tmpbpl[i] ^= lpXORbitPlane[i];
270 lpcur->pixshape = XCreatePixmapFromBitmapData(
271 display, DefaultRootWindow(display),
272 tmpbpl, nWidth, nHeight, 1, 0, 1);
273 XParseColor(display,cmap,"#000000",&fgcolor);
274 XParseColor(display,cmap,"#ffffff",&bkcolor);
275 lpcur->xcursor = XCreatePixmapCursor(display,
276 lpcur->pixshape, lpcur->pixmask,
277 &fgcolor, &bkcolor, lpcur->descriptor.curXHotspot,
278 lpcur->descriptor.curYHotspot);
279 free(tmpbpl);
280 XFreePixmap(display, lpcur->pixshape);
281 XFreePixmap(display, lpcur->pixmask);
282 ReleaseDC(GetDesktopWindow(), hdc);
283 GlobalUnlock(hCursor);
284 return hCursor;
289 /**********************************************************************
290 * DestroyCursor [USER.458]
292 BOOL DestroyCursor(HCURSOR hCursor)
294 CURSORALLOC *lpcur;
295 if (hCursor == (HCURSOR)NULL) return FALSE;
296 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
297 if (lpcur->hBitmap != (HBITMAP)NULL) DeleteObject(lpcur->hBitmap);
298 GlobalUnlock(hCursor);
299 GlobalFree(hCursor);
300 return TRUE;
304 /**********************************************************************
305 * CURSOR_SetCursor
307 * Internal helper function for SetCursor() and ShowCursor().
309 static void CURSOR_SetCursor( HCURSOR hCursor )
311 CURSORALLOC *lpcur;
313 if (!(lpcur = (CURSORALLOC *)GlobalLock(hCursor))) return;
314 if (rootWindow != DefaultRootWindow(display))
316 XDefineCursor( display, rootWindow, lpcur->xcursor );
318 else
320 HWND hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
321 while(hwnd)
323 Window win = WIN_GetXWindow( hwnd );
324 if (win) XDefineCursor( display, win, lpcur->xcursor );
325 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
328 GlobalUnlock( hCursor );
331 /**********************************************************************
332 * SetCursor [USER.69]
334 HCURSOR SetCursor(HCURSOR hCursor)
336 HCURSOR hOldCursor;
338 dprintf_cursor(stddeb,"SetCursor / hCursor=%04X !\n", hCursor);
339 hOldCursor = hActiveCursor;
340 hActiveCursor = hCursor;
341 if ((hCursor != hOldCursor) || (ShowCursCount < 0))
343 CURSOR_SetCursor( hCursor );
345 ShowCursCount = 0;
346 return hOldCursor;
350 /**********************************************************************
351 * GetCursor [USER.247]
353 HCURSOR GetCursor(void)
355 return hActiveCursor;
359 /**********************************************************************
360 * SetCursorPos [USER.70]
362 void SetCursorPos(short x, short y)
364 dprintf_cursor(stddeb,"SetCursorPos // x=%d y=%d\n", x, y);
365 XWarpPointer( display, None, rootWindow, 0, 0, 0, 0, x, y );
369 /**********************************************************************
370 * GetCursorPos [USER.17]
372 void GetCursorPos(LPPOINT lpRetPoint)
374 Window root, child;
375 int rootX, rootY;
376 int childX, childY;
377 unsigned int mousebut;
379 if (!lpRetPoint) return;
380 if (!XQueryPointer( display, rootWindow, &root, &child,
381 &rootX, &rootY, &childX, &childY, &mousebut ))
382 lpRetPoint->x = lpRetPoint->y = 0;
383 else
385 lpRetPoint->x = rootX + desktopX;
386 lpRetPoint->y = rootY + desktopY;
388 dprintf_cursor(stddeb,
389 "GetCursorPos // x=%d y=%d\n", lpRetPoint->x, lpRetPoint->y);
393 /**********************************************************************
394 * ShowCursor [USER.71]
396 int ShowCursor(BOOL bShow)
398 dprintf_cursor(stddeb, "ShowCursor(%d), count=%d\n", bShow, ShowCursCount);
400 if (bShow)
402 if (++ShowCursCount == 0) /* Time to show it */
403 CURSOR_SetCursor( hActiveCursor );
405 else /* Hide it */
407 if (--ShowCursCount == -1) /* Time to hide it */
409 if (!hEmptyCursor)
410 hEmptyCursor = CreateCursor( 0, 1, 1, 1, 1,
411 "\xFF\xFF", "\xFF\xFF" );
412 CURSOR_SetCursor( hEmptyCursor );
415 return 0;
419 /**********************************************************************
420 * ClipCursor [USER.16]
422 void ClipCursor(LPRECT lpNewClipRect)
424 if (!lpNewClipRect) SetRectEmpty( &ClipCursorRect );
425 else CopyRect( &ClipCursorRect, lpNewClipRect );
429 /**********************************************************************
430 * GetClipCursor [USER.309]
432 void GetClipCursor(LPRECT lpRetClipRect)
434 if (lpRetClipRect != NULL)
435 CopyRect(lpRetClipRect, &ClipCursorRect);