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 = %p\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 %p 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 %p 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
)
245 dprintf_resource(stddeb
,"CreateCursor: inst=%04x nXhotspot=%d nYhotspot=%d nWidth=%d nHeight=%d\n",
246 instance
, nXhotspot
, nYhotspot
, nWidth
, nHeight
);
247 dprintf_resource(stddeb
,"CreateCursor: inst=%04x lpANDbitPlane=%p lpXORbitPlane=%p\n",
248 instance
, lpANDbitPlane
, lpXORbitPlane
);
250 if (!(hdc
= GetDC(GetDesktopWindow()))) return 0;
251 hCursor
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(CURSORALLOC
) + 1024L);
252 if (hCursor
== (HCURSOR
)NULL
) {
253 ReleaseDC(GetDesktopWindow(), hdc
);
256 dprintf_cursor(stddeb
,"CreateCursor Alloc hCursor=%X\n", hCursor
);
257 lpcur
= (CURSORALLOC
*)GlobalLock(hCursor
);
258 memset(lpcur
, 0, sizeof(CURSORALLOC
));
259 lpcur
->descriptor
.curXHotspot
= nXhotspot
;
260 lpcur
->descriptor
.curYHotspot
= nYhotspot
;
261 lpcur
->pixshape
= XCreatePixmapFromBitmapData(
262 display
, DefaultRootWindow(display
),
263 lpXORbitPlane
, nWidth
, nHeight
,
264 WhitePixel(display
, DefaultScreen(display
)),
265 BlackPixel(display
, DefaultScreen(display
)), 1);
266 lpcur
->pixmask
= XCreatePixmapFromBitmapData(
267 display
, DefaultRootWindow(display
),
268 lpANDbitPlane
, nWidth
, nHeight
,
269 WhitePixel(display
, DefaultScreen(display
)),
270 BlackPixel(display
, DefaultScreen(display
)), 1);
271 memset(&bkcolor
, 0, sizeof(XColor
));
272 memset(&fgcolor
, 0, sizeof(XColor
));
273 bkcolor
.pixel
= WhitePixel(display
, DefaultScreen(display
));
274 fgcolor
.pixel
= BlackPixel(display
, DefaultScreen(display
));
275 lpcur
->xcursor
= XCreatePixmapCursor(display
,
276 lpcur
->pixshape
, lpcur
->pixmask
,
277 &fgcolor
, &bkcolor
, lpcur
->descriptor
.curXHotspot
,
278 lpcur
->descriptor
.curYHotspot
);
279 XFreePixmap(display
, lpcur
->pixshape
);
280 XFreePixmap(display
, lpcur
->pixmask
);
281 ReleaseDC(GetDesktopWindow(), hdc
);
282 GlobalUnlock(hCursor
);
288 /**********************************************************************
289 * DestroyCursor [USER.458]
291 BOOL
DestroyCursor(HCURSOR hCursor
)
294 if (hCursor
== (HCURSOR
)NULL
) return FALSE
;
295 lpcur
= (CURSORALLOC
*)GlobalLock(hCursor
);
296 if (lpcur
->hBitmap
!= (HBITMAP
)NULL
) DeleteObject(lpcur
->hBitmap
);
297 GlobalUnlock(hCursor
);
303 /**********************************************************************
306 * Internal helper function for SetCursor() and ShowCursor().
308 static void CURSOR_SetCursor( HCURSOR hCursor
)
312 if (!(lpcur
= (CURSORALLOC
*)GlobalLock(hCursor
))) return FALSE
;
313 if (rootWindow
!= DefaultRootWindow(display
))
315 XDefineCursor( display
, rootWindow
, lpcur
->xcursor
);
319 HWND hwnd
= GetWindow( GetDesktopWindow(), GW_CHILD
);
322 Window win
= WIN_GetXWindow( hwnd
);
323 if (win
) XDefineCursor( display
, win
, lpcur
->xcursor
);
324 hwnd
= GetWindow( hwnd
, GW_HWNDNEXT
);
327 GlobalUnlock( hCursor
);
330 /**********************************************************************
331 * SetCursor [USER.69]
333 HCURSOR
SetCursor(HCURSOR hCursor
)
337 dprintf_cursor(stddeb
,"SetCursor / hCursor=%04X !\n", hCursor
);
338 hOldCursor
= hActiveCursor
;
339 hActiveCursor
= hCursor
;
340 if ((hCursor
!= hOldCursor
) || (ShowCursCount
< 0))
342 CURSOR_SetCursor( hCursor
);
349 /**********************************************************************
350 * GetCursor [USER.247]
352 HCURSOR
GetCursor(void)
354 return hActiveCursor
;
358 /**********************************************************************
359 * SetCursorPos [USER.70]
361 void SetCursorPos(short x
, short y
)
363 dprintf_cursor(stddeb
,"SetCursorPos // x=%d y=%d\n", x
, y
);
364 XWarpPointer( display
, None
, rootWindow
, 0, 0, 0, 0, x
, y
);
368 /**********************************************************************
369 * GetCursorPos [USER.17]
371 void GetCursorPos(LPPOINT lpRetPoint
)
376 unsigned int mousebut
;
378 if (!lpRetPoint
) return;
379 if (!XQueryPointer( display
, rootWindow
, &root
, &child
,
380 &rootX
, &rootY
, &childX
, &childY
, &mousebut
))
381 lpRetPoint
->x
= lpRetPoint
->y
= 0;
384 lpRetPoint
->x
= rootX
+ desktopX
;
385 lpRetPoint
->y
= rootY
+ desktopY
;
387 dprintf_cursor(stddeb
,
388 "GetCursorPos // x=%d y=%d\n", lpRetPoint
->x
, lpRetPoint
->y
);
392 /**********************************************************************
393 * ShowCursor [USER.71]
395 int ShowCursor(BOOL bShow
)
397 dprintf_cursor(stddeb
, "ShowCursor(%d), count=%d\n", bShow
, ShowCursCount
);
401 if (++ShowCursCount
== 0) /* Time to show it */
402 CURSOR_SetCursor( hActiveCursor
);
406 if (--ShowCursCount
== -1) /* Time to hide it */
409 hEmptyCursor
= CreateCursor( 0, 1, 1, 1, 1,
410 "\xFF\xFF", "\xFF\xFF" );
411 CURSOR_SetCursor( hEmptyCursor
);
418 /**********************************************************************
419 * ClipCursor [USER.16]
421 void ClipCursor(LPRECT lpNewClipRect
)
423 CopyRect(&ClipCursorRect
, lpNewClipRect
);
427 /**********************************************************************
428 * GetClipCursor [USER.309]
430 void GetClipCursor(LPRECT lpRetClipRect
)
432 if (lpRetClipRect
!= NULL
)
433 CopyRect(lpRetClipRect
, &ClipCursorRect
);