Release 0.5
[wine/gsoc_dplay.git] / loader / cursor.c
blob039c1c35bf9d50d50244a4373ed0d03ca9988f7b
1 /*
2 * WINE
3 */
4 static char Copyright[] = "Copyright Martin Ayotte, 1993";
6 /*
7 #define DEBUG_CURSOR
8 */
10 #include <X11/Intrinsic.h>
11 #include <X11/cursorfont.h>
12 #include <X11/Xlib.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include "prototypes.h"
20 #include "windows.h"
21 #include "win.h"
22 #include "gdi.h"
23 #include "wine.h"
24 #include "cursor.h"
26 static int ShowCursCount = 0;
27 static HCURSOR hActiveCursor;
28 static HCURSOR hEmptyCursor = 0;
29 RECT ClipCursorRect;
30 extern HINSTANCE hSysRes;
31 extern Window winHasCursor;
33 /**********************************************************************
34 * LoadCursor [USER.173]
36 HCURSOR LoadCursor(HANDLE instance, LPSTR cursor_name)
38 XColor bkcolor;
39 XColor fgcolor;
40 HCURSOR hCursor;
41 HANDLE rsc_mem;
42 WORD *lp;
43 CURSORDESCRIP *lpcurdesc;
44 CURSORALLOC *lpcur;
45 BITMAP BitMap;
46 HBITMAP hBitMap;
47 HDC hMemDC;
48 HDC hdc;
49 int i, j, image_size;
50 #ifdef DEBUG_RESOURCE
51 printf("LoadCursor: instance = %04x, name = %08x\n",
52 instance, cursor_name);
53 #endif
54 hCursor = GlobalAlloc(GMEM_MOVEABLE, sizeof(CURSORALLOC) + 1024L);
55 if (hCursor == (HCURSOR)NULL) return 0;
56 #ifdef DEBUG_CURSOR
57 printf("LoadCursor Alloc hCursor=%X\n", hCursor);
58 #endif
59 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
60 memset(lpcur, 0, sizeof(CURSORALLOC));
61 if (instance == (HANDLE)NULL) {
62 instance = hSysRes;
63 switch((LONG)cursor_name) {
64 case IDC_ARROW:
65 lpcur->xcursor = XCreateFontCursor(XT_display, XC_top_left_arrow);
66 GlobalUnlock(hCursor);
67 return hCursor;
68 case IDC_CROSS:
69 lpcur->xcursor = XCreateFontCursor(XT_display, XC_crosshair);
70 GlobalUnlock(hCursor);
71 return hCursor;
72 case IDC_IBEAM:
73 lpcur->xcursor = XCreateFontCursor(XT_display, XC_xterm);
74 GlobalUnlock(hCursor);
75 return hCursor;
76 case IDC_WAIT:
77 lpcur->xcursor = XCreateFontCursor(XT_display, XC_watch);
78 GlobalUnlock(hCursor);
79 return hCursor;
80 default:
81 break;
84 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
85 rsc_mem = RSC_LoadResource(instance, cursor_name, NE_RSCTYPE_GROUP_CURSOR,
86 &image_size);
87 if (rsc_mem == (HANDLE)NULL) {
88 printf("LoadCursor / Cursor %08X not Found !\n", cursor_name);
89 ReleaseDC(GetDesktopWindow(), hdc);
90 return 0;
92 lp = (WORD *)GlobalLock(rsc_mem);
93 if (lp == NULL) {
94 GlobalFree(rsc_mem);
95 ReleaseDC(GetDesktopWindow(), hdc);
96 return 0;
98 lpcurdesc = (CURSORDESCRIP *)(lp + 3);
99 #ifdef DEBUG_CURSOR
100 printf("LoadCursor / image_size=%d\n", image_size);
101 printf("LoadCursor / curReserved=%X\n", *lp);
102 printf("LoadCursor / curResourceType=%X\n", *(lp + 1));
103 printf("LoadCursor / curResourceCount=%X\n", *(lp + 2));
104 printf("LoadCursor / cursor Width=%d\n", (int)lpcurdesc->Width);
105 printf("LoadCursor / cursor Height=%d\n", (int)lpcurdesc->Height);
106 printf("LoadCursor / cursor curXHotspot=%d\n", (int)lpcurdesc->curXHotspot);
107 printf("LoadCursor / cursor curYHotspot=%d\n", (int)lpcurdesc->curYHotspot);
108 printf("LoadCursor / cursor curDIBSize=%lX\n", (DWORD)lpcurdesc->curDIBSize);
109 printf("LoadCursor / cursor curDIBOffset=%lX\n", (DWORD)lpcurdesc->curDIBOffset);
110 #endif
111 lpcur->descriptor = *lpcurdesc;
112 GlobalUnlock(rsc_mem);
113 GlobalFree(rsc_mem);
114 rsc_mem = RSC_LoadResource(instance,
115 MAKEINTRESOURCE(lpcurdesc->curDIBOffset),
116 NE_RSCTYPE_CURSOR, &image_size);
117 if (rsc_mem == (HANDLE)NULL) {
118 printf("LoadCursor / Cursor %08X Bitmap not Found !\n", cursor_name);
119 ReleaseDC(GetDesktopWindow(), hdc);
120 return 0;
122 lp = (WORD *)GlobalLock(rsc_mem);
123 if (lp == NULL) {
124 GlobalFree(rsc_mem);
125 ReleaseDC(GetDesktopWindow(), hdc);
126 return 0;
128 lp += 2;
129 for (j = 0; j < 16; j++)
130 printf("%04X ", *(lp + j));
132 if (*lp == sizeof(BITMAPINFOHEADER))
133 lpcur->hBitmap = ConvertInfoBitmap(hdc, (BITMAPINFO *)lp);
134 else
136 lpcur->hBitmap = 0;
137 lp += sizeof(BITMAP);
138 for (i = 0; i < 81; i++) {
139 char temp = *((char *)lp + 162 + i);
140 *((char *)lp + 162 + i) = *((char *)lp + 324 - i);
141 *((char *)lp + 324 - i) = temp;
143 lpcur->pixshape = XCreatePixmapFromBitmapData(
144 XT_display, DefaultRootWindow(XT_display),
145 ((char *)lp + 211), 32, 32,
147 lpcurdesc->Width / 2, lpcurdesc->Height / 4,
149 WhitePixel(XT_display, DefaultScreen(XT_display)),
150 BlackPixel(XT_display, DefaultScreen(XT_display)), 1);
151 lpcur->pixmask = XCreatePixmapFromBitmapData(
152 XT_display, DefaultRootWindow(XT_display),
153 ((char *)lp + 211), 32, 32,
154 WhitePixel(XT_display, DefaultScreen(XT_display)),
155 BlackPixel(XT_display, DefaultScreen(XT_display)), 1);
156 memset(&bkcolor, 0, sizeof(XColor));
157 memset(&fgcolor, 0, sizeof(XColor));
158 bkcolor.pixel = WhitePixel(XT_display, DefaultScreen(XT_display));
159 fgcolor.pixel = BlackPixel(XT_display, DefaultScreen(XT_display));
160 printf("LoadCursor / before XCreatePixmapCursor !\n");
161 lpcur->xcursor = XCreatePixmapCursor(XT_display,
162 lpcur->pixshape, lpcur->pixmask,
163 &fgcolor, &bkcolor, lpcur->descriptor.curXHotspot,
164 lpcur->descriptor.curYHotspot);
165 GlobalUnlock(rsc_mem);
166 GlobalFree(rsc_mem);
168 hCursor = CreateCursor(instance, lpcur->descriptor.curXHotspot,
169 lpcur->descriptor.curYHotspot, 32, 32,
170 (LPSTR)lp + 211, , (LPSTR)lp + 211);
172 XFreePixmap(XT_display, lpcur->pixshape);
173 XFreePixmap(XT_display, lpcur->pixmask);
174 ReleaseDC(GetDesktopWindow(), hdc);
175 GlobalUnlock(hCursor);
176 return hCursor;
181 /**********************************************************************
182 * CreateCursor [USER.406]
184 HCURSOR CreateCursor(HANDLE instance, short nXhotspot, short nYhotspot,
185 short nWidth, short nHeight, LPSTR lpANDbitPlane, LPSTR lpXORbitPlane)
187 XColor bkcolor;
188 XColor fgcolor;
189 HCURSOR hCursor;
190 CURSORALLOC *lpcur;
191 BITMAP BitMap;
192 HBITMAP hBitMap;
193 HDC hMemDC;
194 HDC hdc;
195 int i, j;
196 #ifdef DEBUG_RESOURCE
197 printf("CreateCursor: inst=%04x nXhotspot=%d nYhotspot=%d nWidth=%d nHeight=%d\n",
198 nXhotspot, nYhotspot, nWidth, nHeight);
199 printf("CreateCursor: inst=%04x lpANDbitPlane=%08X lpXORbitPlane=%08X\n",
200 LPSTR lpANDbitPlane, LPSTR lpXORbitPlane);
201 #endif
202 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
203 hCursor = GlobalAlloc(GMEM_MOVEABLE, sizeof(CURSORALLOC) + 1024L);
204 if (hCursor == (HCURSOR)NULL) {
205 ReleaseDC(GetDesktopWindow(), hdc);
206 return 0;
208 printf("CreateCursor Alloc hCursor=%X\n", hCursor);
209 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
210 memset(lpcur, 0, sizeof(CURSORALLOC));
211 lpcur->descriptor.curXHotspot = nXhotspot;
212 lpcur->descriptor.curYHotspot = nYhotspot;
213 lpcur->pixshape = XCreatePixmapFromBitmapData(
214 XT_display, DefaultRootWindow(XT_display),
215 lpXORbitPlane, nWidth, nHeight,
216 WhitePixel(XT_display, DefaultScreen(XT_display)),
217 BlackPixel(XT_display, DefaultScreen(XT_display)), 1);
218 lpcur->pixmask = XCreatePixmapFromBitmapData(
219 XT_display, DefaultRootWindow(XT_display),
220 lpANDbitPlane, nWidth, nHeight,
221 WhitePixel(XT_display, DefaultScreen(XT_display)),
222 BlackPixel(XT_display, DefaultScreen(XT_display)), 1);
223 memset(&bkcolor, 0, sizeof(XColor));
224 memset(&fgcolor, 0, sizeof(XColor));
225 bkcolor.pixel = WhitePixel(XT_display, DefaultScreen(XT_display));
226 fgcolor.pixel = BlackPixel(XT_display, DefaultScreen(XT_display));
227 lpcur->xcursor = XCreatePixmapCursor(XT_display,
228 lpcur->pixshape, lpcur->pixmask,
229 &fgcolor, &bkcolor, lpcur->descriptor.curXHotspot,
230 lpcur->descriptor.curYHotspot);
231 XFreePixmap(XT_display, lpcur->pixshape);
232 XFreePixmap(XT_display, lpcur->pixmask);
233 ReleaseDC(GetDesktopWindow(), hdc);
234 GlobalUnlock(hCursor);
235 return hCursor;
240 /**********************************************************************
241 * DestroyCursor [USER.458]
243 BOOL DestroyCursor(HCURSOR hCursor)
245 CURSORALLOC *lpcur;
246 if (hCursor == (HCURSOR)NULL) return FALSE;
247 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
248 if (lpcur->hBitmap != (HBITMAP)NULL) DeleteObject(lpcur->hBitmap);
249 GlobalUnlock(hCursor);
250 GlobalFree(hCursor);
251 return TRUE;
255 /**********************************************************************
256 * SetCursor [USER.69]
258 HCURSOR SetCursor(HCURSOR hCursor)
260 HDC hDC;
261 HDC hMemDC;
262 BITMAP bm;
263 CURSORALLOC *lpcur;
264 HCURSOR hOldCursor;
265 Window root, child;
266 int rootX, rootY;
267 int childX, childY;
268 unsigned int mousebut;
269 #ifdef DEBUG_CURSOR
270 printf("SetCursor / hCursor=%04X !\n", hCursor);
271 #endif
272 if (hCursor == (HCURSOR)NULL) return FALSE;
273 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
274 hOldCursor = hActiveCursor;
275 #ifdef DEBUG_CURSOR
276 printf("SetCursor / lpcur->xcursor=%08X !\n", &lpcur->xcursor);
277 XQueryPointer(XT_display, DefaultRootWindow(XT_display),
278 &root, &child, &rootX, &rootY, &childX, &childY, &mousebut);
279 printf("SetCursor / winHasCursor=%08X !\n", winHasCursor);
280 printf("SetCursor / child=%08X !\n", child);
281 #endif
282 if (hActiveCursor != hCursor) ShowCursCount = 0;
283 if ((ShowCursCount >= 0) & (winHasCursor != 0)) {
284 /* XUndefineCursor(XT_display, winHasCursor); */
285 XDefineCursor(XT_display, winHasCursor, lpcur->xcursor);
287 GlobalUnlock(hCursor);
288 hActiveCursor = hCursor;
289 return hOldCursor;
293 /**********************************************************************
294 * SetCursorPos [USER.70]
296 void SetCursorPos(short x, short y)
298 Window root, child;
299 int rootX, rootY;
300 int childX, childY;
301 unsigned int mousebut;
302 #ifdef DEBUG_CURSOR
303 printf("SetCursorPos // x=%d y=%d\n", x, y);
304 #endif
305 XQueryPointer(XT_display, DefaultRootWindow(XT_display),
306 &root, &child, &rootX, &rootY, &childX, &childY, &mousebut);
307 XWarpPointer(XT_display, child, root, 0, 0,
308 DisplayWidth(XT_display, DefaultScreen(XT_display)),
309 DisplayHeight(XT_display, DefaultScreen(XT_display)),
310 (int)x, (int)y);
314 /**********************************************************************
315 * GetCursorPos [USER.17]
317 void GetCursorPos(LPPOINT lpRetPoint)
319 Window root, child;
320 int rootX, rootY;
321 int childX, childY;
322 unsigned int mousebut;
323 if (lpRetPoint != NULL) {
324 XQueryPointer(XT_display, DefaultRootWindow(XT_display),
325 &root, &child, &rootX, &rootY, &childX, &childY, &mousebut);
326 #ifdef DEBUG_CURSOR
327 printf("GetCursorPos // x=%d y=%d\n", rootX, rootY);
328 #endif
329 lpRetPoint->x = rootX;
330 lpRetPoint->y = rootY;
335 /**********************************************************************
336 * ShowCursor [USER.71]
338 int ShowCursor(BOOL bShow)
340 HCURSOR hCursor;
341 #ifdef DEBUG_CURSOR
342 printf("ShowCursor bShow=%d ShowCount=%d !\n", bShow, ShowCursCount);
343 #endif
344 if (bShow)
345 ShowCursCount++;
346 else
347 ShowCursCount--;
348 if (ShowCursCount >= 0) {
349 /* if (hCursor == (HCURSOR)NULL) */
350 hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
351 SetCursor(hCursor);
353 else {
354 /* XUndefineCursor(XT_display, winHasCursor); */
355 if (hEmptyCursor == (HCURSOR)NULL)
356 hEmptyCursor = CreateCursor((HINSTANCE)NULL, 1, 1, 1, 1,
357 "\xFF\xFF", "\xFF\xFF");
358 hCursor = SetCursor(hEmptyCursor);
359 hActiveCursor = hCursor;
361 return 0;
365 /**********************************************************************
366 * ClipCursor [USER.16]
368 void ClipCursor(LPRECT lpNewClipRect)
370 CopyRect(&ClipCursorRect, lpNewClipRect);
374 /**********************************************************************
375 * GetClipCursor [USER.309]
377 void GetClipCursor(LPRECT lpRetClipRect)
379 if (lpRetClipRect != NULL)
380 CopyRect(lpRetClipRect, &ClipCursorRect);