2 /* Copyright (c) Mark J. Kilgard, 1995, 1998. */
4 /* This program is freely distributable without licensing fees
5 and is provided without guarantee or warrantee expressed or
6 implied. This program is -not- in the public domain. */
11 #include <X11/Xatom.h> /* For XA_CURSOR */
12 #include <X11/cursorfont.h>
15 typedef struct _CursorTable
{
25 static CursorTable cursorTable
[] = {
26 {XC_arrow
, None
}, /* GLUT_CURSOR_RIGHT_ARROW */
27 {XC_top_left_arrow
, None
}, /* GLUT_CURSOR_LEFT_ARROW */
28 {XC_hand1
, None
}, /* GLUT_CURSOR_INFO */
29 {XC_pirate
, None
}, /* GLUT_CURSOR_DESTROY */
30 {XC_question_arrow
, None
}, /* GLUT_CURSOR_HELP */
31 {XC_exchange
, None
}, /* GLUT_CURSOR_CYCLE */
32 {XC_spraycan
, None
}, /* GLUT_CURSOR_SPRAY */
33 {XC_watch
, None
}, /* GLUT_CURSOR_WAIT */
34 {XC_xterm
, None
}, /* GLUT_CURSOR_TEXT */
35 {XC_crosshair
, None
}, /* GLUT_CURSOR_CROSSHAIR */
36 {XC_sb_v_double_arrow
, None
}, /* GLUT_CURSOR_UP_DOWN */
37 {XC_sb_h_double_arrow
, None
}, /* GLUT_CURSOR_LEFT_RIGHT */
38 {XC_top_side
, None
}, /* GLUT_CURSOR_TOP_SIDE */
39 {XC_bottom_side
, None
}, /* GLUT_CURSOR_BOTTOM_SIDE */
40 {XC_left_side
, None
}, /* GLUT_CURSOR_LEFT_SIDE */
41 {XC_right_side
, None
}, /* GLUT_CURSOR_RIGHT_SIDE */
42 {XC_top_left_corner
, None
}, /* GLUT_CURSOR_TOP_LEFT_CORNER */
43 {XC_top_right_corner
, None
}, /* GLUT_CURSOR_TOP_RIGHT_CORNER */
44 {XC_bottom_right_corner
, None
}, /* GLUT_CURSOR_BOTTOM_RIGHT_CORNER */
45 {XC_bottom_left_corner
, None
}, /* GLUT_CURSOR_BOTTOM_LEFT_CORNER */
50 static Cursor blankCursor
= None
;
51 static Cursor fullCrosshairCusor
= None
;
53 /* SGI X server's support a special property called the
54 _SGI_CROSSHAIR_CURSOR that when installed as a window's
55 cursor, becomes a full screen crosshair cursor. SGI
56 has special cursor generation hardware for this case. */
58 getFullCrosshairCursor(void)
61 Atom crosshairAtom
, actualType
;
63 unsigned long n
, left
;
66 if (fullCrosshairCusor
== None
) {
67 crosshairAtom
= XInternAtom(__glutDisplay
,
68 "_SGI_CROSSHAIR_CURSOR", True
);
69 if (crosshairAtom
!= None
) {
70 value
= 0; /* Make compiler happy. */
71 rc
= XGetWindowProperty(__glutDisplay
, __glutRoot
,
72 crosshairAtom
, 0, 1, False
, XA_CURSOR
, &actualType
,
73 &actualFormat
, &n
, &left
, (unsigned char **) &value
);
74 if (rc
== Success
&& actualFormat
== 32 && n
>= 1) {
81 return XCreateFontCursor(__glutDisplay
, XC_crosshair
);
84 /* X11 forces you to create a blank cursor if you want
85 to disable the cursor. */
95 blank
= XCreateBitmapFromData(__glutDisplay
, __glutRoot
,
98 __glutFatalError("out of memory.");
99 cursor
= XCreatePixmapCursor(__glutDisplay
, blank
, blank
,
100 &dummy
, &dummy
, 0, 0);
101 XFreePixmap(__glutDisplay
, blank
);
107 /* Win32 and X11 use this same function to accomplish
108 fairly different tasks. X11 lets you just define the
109 cursor for a window and the window system takes care
110 of making sure that the window's cursor is installed
111 when the mouse is in the window. Win32 requires the
112 application to handle a WM_SETCURSOR message to install
113 the right cursor when windows are entered. Think of
114 the Win32 __glutSetCursor (called from __glutWindowProc)
115 as "install cursor". Think of the X11 __glutSetCursor
116 (called from glutSetCursor) as "define cursor". */
118 __glutSetCursor(GLUTwindow
*window
)
120 int cursor
= window
->cursor
;
124 cursor
< sizeof(cursorTable
) / sizeof(cursorTable
[0])) {
125 if (cursorTable
[cursor
].cursor
== None
) {
126 cursorTable
[cursor
].cursor
= XCreateFontCursor(__glutDisplay
,
127 cursorTable
[cursor
].glyph
);
129 xcursor
= cursorTable
[cursor
].cursor
;
133 case GLUT_CURSOR_INHERIT
:
135 while (window
->parent
) {
136 window
= window
->parent
;
137 if (window
->cursor
!= GLUT_CURSOR_INHERIT
) {
138 __glutSetCursor(window
);
142 /* XXX Default to an arrow cursor. Is this
143 right or should we be letting the default
144 window proc be installing some system cursor? */
145 xcursor
= cursorTable
[0].cursor
;
146 if (xcursor
== NULL
) {
148 cursorTable
[0].cursor
=
149 LoadCursor(NULL
, cursorTable
[0].glyph
);
155 case GLUT_CURSOR_NONE
:
159 if (blankCursor
== None
) {
160 blankCursor
= makeBlankCursor();
162 xcursor
= blankCursor
;
165 case GLUT_CURSOR_FULL_CROSSHAIR
:
167 xcursor
= (HICON
) IDC_CROSS
;
169 if (fullCrosshairCusor
== None
) {
170 fullCrosshairCusor
= getFullCrosshairCursor();
172 xcursor
= fullCrosshairCusor
;
177 XDefineCursor(__glutDisplay
,
178 window
->win
, xcursor
);
179 XFlush(__glutDisplay
);
184 glutSetCursor(int cursor
)
189 __glutCurrentWindow
->cursor
= cursor
;
190 /* Are we in the window right now? If so,
191 install the cursor. */
192 GetCursorPos(&point
);
193 if (__glutCurrentWindow
->win
== WindowFromPoint(point
)) {
194 __glutSetCursor(__glutCurrentWindow
);
197 __glutCurrentWindow
->cursor
= cursor
;
198 __glutSetCursor(__glutCurrentWindow
);