9784e9b62260e4ee7600f18abaa703a21cdb4e7d
[mesa.git] / src / glut / os2 / glut_cursor.cpp
1
2 /* Copyright (c) Mark J. Kilgard, 1995, 1998. */
3
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. */
7
8 #include "glutint.h"
9
10 #if !defined(_WIN32) && !defined(__OS2PM__)
11 #include <X11/Xatom.h> /* For XA_CURSOR */
12 #include <X11/cursorfont.h>
13 #endif
14
15 typedef struct _CursorTable {
16 #if defined(_WIN32)
17 char* glyph;
18 #else
19 int glyph;
20 #endif
21 Cursor cursor;
22 } CursorTable;
23 /* *INDENT-OFF* */
24
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 */
46 };
47 /* *INDENT-ON* */
48
49 #if !defined(_WIN32) && !defined(__OS2PM__)
50 static Cursor blankCursor = None;
51 static Cursor fullCrosshairCusor = None;
52
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. */
57 static Cursor
58 getFullCrosshairCursor(void)
59 {
60 Cursor cursor;
61 Atom crosshairAtom, actualType;
62 int rc, actualFormat;
63 unsigned long n, left;
64 unsigned long *value;
65
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) {
75 cursor = value[0];
76 XFree(value);
77 return cursor;
78 }
79 }
80 }
81 return XCreateFontCursor(__glutDisplay, XC_crosshair);
82 }
83
84 /* X11 forces you to create a blank cursor if you want
85 to disable the cursor. */
86 static Cursor
87 makeBlankCursor(void)
88 {
89 static char data[1] =
90 {0};
91 Cursor cursor;
92 Pixmap blank;
93 XColor dummy;
94
95 blank = XCreateBitmapFromData(__glutDisplay, __glutRoot,
96 data, 1, 1);
97 if (blank == None)
98 __glutFatalError("out of memory.");
99 cursor = XCreatePixmapCursor(__glutDisplay, blank, blank,
100 &dummy, &dummy, 0, 0);
101 XFreePixmap(__glutDisplay, blank);
102
103 return cursor;
104 }
105 #endif /* !_WIN32 && !__OS2PM__*/
106
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". */
117 void
118 __glutSetCursor(GLUTwindow *window)
119 {
120 int cursor = window->cursor;
121 Cursor xcursor = 0;
122
123 if (cursor >= 0 &&
124 cursor < sizeof(cursorTable) / sizeof(cursorTable[0])) {
125 if (cursorTable[cursor].cursor == None) {
126 cursorTable[cursor].cursor = XCreateFontCursor(__glutDisplay,
127 cursorTable[cursor].glyph);
128 }
129 xcursor = cursorTable[cursor].cursor;
130 } else {
131 /* Special cases. */
132 switch (cursor) {
133 case GLUT_CURSOR_INHERIT:
134 #if defined(_WIN32)
135 while (window->parent) {
136 window = window->parent;
137 if (window->cursor != GLUT_CURSOR_INHERIT) {
138 __glutSetCursor(window);
139 return;
140 }
141 }
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) {
147 xcursor =
148 cursorTable[0].cursor =
149 LoadCursor(NULL, cursorTable[0].glyph);
150 }
151
152 #elif defined(__OS2PM__)
153 //todo
154 xcursor = None;
155
156 #else
157 xcursor = None;
158 #endif
159 break;
160 case GLUT_CURSOR_NONE:
161 #if defined(_WIN32) || defined(__OS2PM__)
162 xcursor = NULL;
163 #else
164 if (blankCursor == None) {
165 blankCursor = makeBlankCursor();
166 }
167 xcursor = blankCursor;
168 #endif
169 break;
170 case GLUT_CURSOR_FULL_CROSSHAIR:
171 #if defined(_WIN32)
172 xcursor = (HICON) IDC_CROSS;
173 #elif defined(__OS2PM__)
174 //todo
175 #else
176 if (fullCrosshairCusor == None) {
177 fullCrosshairCusor = getFullCrosshairCursor();
178 }
179 xcursor = fullCrosshairCusor;
180 #endif
181 break;
182 }
183 }
184 XDefineCursor(__glutDisplay,
185 window->win, xcursor);
186 XFlush(__glutDisplay);
187 }
188
189 /* CENTRY */
190 void GLUTAPIENTRY
191 glutSetCursor(int cursor)
192 {
193 #ifdef _WIN32
194 POINT point;
195
196 __glutCurrentWindow->cursor = cursor;
197 /* Are we in the window right now? If so,
198 install the cursor. */
199 GetCursorPos(&point);
200 if (__glutCurrentWindow->win == WindowFromPoint(point)) {
201 __glutSetCursor(__glutCurrentWindow);
202 }
203 #elif defined(__OS2PM__)
204 //todo
205 #else
206 __glutCurrentWindow->cursor = cursor;
207 __glutSetCursor(__glutCurrentWindow);
208 #endif
209 }
210 /* ENDCENTRY */