4 static char Copyright
[] = "Copyright Martin Ayotte, 1993";
13 #include <X11/cursorfont.h>
15 #include "prototypes.h"
22 /* #define DEBUG_CURSOR /* */
23 /* #undef DEBUG_CURSOR /* */
24 /* #define DEBUG_RESOURCE /* */
25 /* #undef DEBUG_RESOURCE /* */
28 static int ShowCursCount
= 0;
29 static HCURSOR hActiveCursor
;
30 static HCURSOR hEmptyCursor
= 0;
32 extern HINSTANCE hSysRes
;
33 extern int desktopX
, desktopY
; /* misc/main.c */
35 static struct { LPSTR name
; HCURSOR cursor
; } system_cursor
[] =
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
)
63 CURSORDESCRIP
*lpcurdesc
;
68 dprintf_resource(stddeb
,"LoadCursor: instance = %04x, name = %08x\n",
69 instance
, cursor_name
);
72 for (i
= 0; i
< NB_SYS_CURSORS
; i
++)
73 if (system_cursor
[i
].name
== cursor_name
)
75 hCursor
= system_cursor
[i
].cursor
;
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
) {
90 switch((LONG
)cursor_name
) {
92 lpcur
->xcursor
= XCreateFontCursor(display
, XC_top_left_arrow
);
93 GlobalUnlock(hCursor
);
96 lpcur
->xcursor
= XCreateFontCursor(display
, XC_crosshair
);
97 GlobalUnlock(hCursor
);
100 lpcur
->xcursor
= XCreateFontCursor(display
, XC_xterm
);
101 GlobalUnlock(hCursor
);
104 lpcur
->xcursor
= XCreateFontCursor(display
, XC_watch
);
105 GlobalUnlock(hCursor
);
108 lpcur
->xcursor
= XCreateFontCursor(display
, XC_sb_v_double_arrow
);
109 GlobalUnlock(hCursor
);
112 lpcur
->xcursor
= XCreateFontCursor(display
, XC_sb_h_double_arrow
);
113 GlobalUnlock(hCursor
);
117 lpcur
->xcursor
= XCreateFontCursor(display
, XC_fleur
);
118 GlobalUnlock(hCursor
);
126 lpcur
->xcursor
= XCreateFontCursor(display
, XC_top_left_arrow
);
127 GlobalUnlock(hCursor
);
131 if (!(hdc
= GetDC(GetDesktopWindow()))) return 0;
132 rsc_mem
= RSC_LoadResource(instance
, cursor_name
, NE_RSCTYPE_GROUP_CURSOR
,
134 if (rsc_mem
== (HANDLE
)NULL
) {
135 fprintf(stderr
,"LoadCursor / Cursor %08X not Found !\n", cursor_name
);
136 ReleaseDC(GetDesktopWindow(), hdc
);
139 lp
= (WORD
*)GlobalLock(rsc_mem
);
142 ReleaseDC(GetDesktopWindow(), hdc
);
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
);
165 rsc_mem
= RSC_LoadResource(instance
,
166 MAKEINTRESOURCE(lpcurdesc
->curDIBOffset
),
167 NE_RSCTYPE_CURSOR
, &image_size
);
168 if (rsc_mem
== (HANDLE
)NULL
) {
170 "LoadCursor / Cursor %08X Bitmap not Found !\n", cursor_name
);
171 ReleaseDC(GetDesktopWindow(), hdc
);
174 lp
= (WORD
*)GlobalLock(rsc_mem
);
177 ReleaseDC(GetDesktopWindow(), hdc
);
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);
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
);
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
);
233 /**********************************************************************
234 * CreateCursor [USER.406]
236 HCURSOR
CreateCursor(HANDLE instance
, short nXhotspot
, short nYhotspot
,
237 short nWidth
, short nHeight
, LPSTR lpANDbitPlane
, LPSTR lpXORbitPlane
)
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
);
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
);
291 /**********************************************************************
292 * DestroyCursor [USER.458]
294 BOOL
DestroyCursor(HCURSOR hCursor
)
297 if (hCursor
== (HCURSOR
)NULL
) return FALSE
;
298 lpcur
= (CURSORALLOC
*)GlobalLock(hCursor
);
299 if (lpcur
->hBitmap
!= (HBITMAP
)NULL
) DeleteObject(lpcur
->hBitmap
);
300 GlobalUnlock(hCursor
);
306 /**********************************************************************
309 * Internal helper function for SetCursor() and ShowCursor().
311 static BOOL
CURSOR_SetCursor( HCURSOR hCursor
)
315 if (!(lpcur
= (CURSORALLOC
*)GlobalLock(hCursor
))) return FALSE
;
316 if (rootWindow
!= DefaultRootWindow(display
))
318 XDefineCursor( display
, rootWindow
, lpcur
->xcursor
);
322 HWND hwnd
= GetWindow( GetDesktopWindow(), GW_CHILD
);
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
)
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
);
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
)
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;
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
)
402 dprintf_cursor(stddeb
, "ShowCursor(%d), count=%d\n", bShow
, ShowCursCount
);
406 if (++ShowCursCount
== 0) /* Time to show it */
407 CURSOR_SetCursor( hActiveCursor
);
411 if (--ShowCursCount
== -1) /* Time to hide it */
414 hEmptyCursor
= CreateCursor( 0, 1, 1, 1, 1,
415 "\xFF\xFF", "\xFF\xFF" );
416 CURSOR_SetCursor( hEmptyCursor
);
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
);