4 * The mouse cursor related stuff.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Thu Dec 16 1999
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
31 #if TARGET_HOST_UNIX_X11
32 #include <X11/cursorfont.h>
36 * TODO BEFORE THE STABLE RELEASE:
37 * glutSetCursor() -- Win32 mappings are incomplete.
39 * It would be good to use custom mouse cursor shapes, and introduce
40 * an option to display them using glBitmap() and/or texture mapping,
41 * apart from the windowing system version.
44 /* -- INTERNAL FUNCTIONS --------------------------------------------------- */
46 #if TARGET_HOST_UNIX_X11
48 static int fghGetCursorError( Cursor cursor )
60 XGetErrorText( fgDisplay.Display, cursor, buf, sizeof buf );
61 fgWarning( "Error in setting cursor:\n %s.", buf );
75 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
78 * Set the cursor image to be used for the current window
80 void FGAPIENTRY glutSetCursor( int cursorID )
82 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetCursor" );
83 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetCursor" );
85 #if TARGET_HOST_UNIX_X11
88 * (a) Partial error checking. Is that a problem?
89 * Is fghGetCursorError() correct? Should we abort on errors?
90 * Should there be a freeglut-wide X error handler? Should
91 * we use the X error-handler mechanism?
92 * (b) FULL_CROSSHAIR demotes to plain CROSSHAIR. Old GLUT allows
93 * for this, but if there is a system that easily supports a full-
94 * window (or full-screen) crosshair, we might consider it.
95 * (c) Out-of-range cursor-types generate warnings. Should we abort?
99 Pixmap no_cursor = None ; /* Used for GLUT_CURSOR_NONE */
102 #define MAP_CURSOR(a,b) \
104 cursor = XCreateFontCursor( fgDisplay.Display, b ); \
107 if( GLUT_CURSOR_FULL_CROSSHAIR == cursorID )
108 cursorID = GLUT_CURSOR_CROSSHAIR;
112 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, XC_right_ptr);
113 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, XC_left_ptr);
114 MAP_CURSOR( GLUT_CURSOR_INFO, XC_hand1);
115 MAP_CURSOR( GLUT_CURSOR_DESTROY, XC_pirate);
116 MAP_CURSOR( GLUT_CURSOR_HELP, XC_question_arrow);
117 MAP_CURSOR( GLUT_CURSOR_CYCLE, XC_exchange);
118 MAP_CURSOR( GLUT_CURSOR_SPRAY, XC_spraycan);
119 MAP_CURSOR( GLUT_CURSOR_WAIT, XC_watch);
120 MAP_CURSOR( GLUT_CURSOR_TEXT, XC_xterm);
121 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, XC_crosshair);
122 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, XC_sb_v_double_arrow);
123 MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT, XC_sb_h_double_arrow);
124 MAP_CURSOR( GLUT_CURSOR_TOP_SIDE, XC_top_side);
125 MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE, XC_bottom_side);
126 MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE, XC_left_side);
127 MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE, XC_right_side);
128 MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER, XC_top_left_corner);
129 MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER, XC_top_right_corner);
130 MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER,
131 XC_bottom_right_corner);
132 MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER, XC_bottom_left_corner);
133 /* MAP_CURSOR( GLUT_CURSOR_NONE, XC_bogosity); */
135 case GLUT_CURSOR_NONE:
138 * Note that we *never* change {no_cursor_bits} from anything
139 * but all-zeros. It is our image and mask. We also apparently
140 * need to pick a color for foreground/background---but what
141 * one we pick doesn't matter for GLUT_CURSOR_NONE.
143 static char no_cursor_bits[ 32 ];
145 no_cursor = XCreatePixmapFromBitmapData( fgDisplay.Display,
146 fgDisplay.RootWindow,
150 XParseColor( fgDisplay.Display,
151 DefaultColormap( fgDisplay.Display,
152 DefaultScreen( fgDisplay.Display ) ),
155 cursor = XCreatePixmapCursor( fgDisplay.Display,
156 no_cursor, no_cursor,
162 case GLUT_CURSOR_INHERIT:
166 fgWarning( "Unknown cursor type: %d", cursorID );
170 error = fghGetCursorError( cursor );
172 if( GLUT_CURSOR_INHERIT == cursorID )
173 XUndefineCursor( fgDisplay.Display,
174 fgStructure.Window->Window.Handle );
177 XDefineCursor( fgDisplay.Display,
178 fgStructure.Window->Window.Handle, cursor );
179 XFreeCursor( fgDisplay.Display, cursor );
180 if( GLUT_CURSOR_NONE == cursorID )
181 XFreePixmap( fgDisplay.Display, no_cursor );
185 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
188 * This is a temporary solution only...
190 /* Set the cursor AND change it for this window class. */
191 # define MAP_CURSOR(a,b) \
193 SetCursor( LoadCursor( NULL, b ) ); \
194 SetClassLong( fgStructure.Window->Window.Handle, \
196 ( LONG )LoadCursor( NULL, b ) ); \
199 /* Nuke the cursor AND change it for this window class. */
200 # define ZAP_CURSOR(a,b) \
203 SetClassLong( fgStructure.Window->Window.Handle, \
204 GCL_HCURSOR, ( LONG )NULL ); \
209 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
210 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
211 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
212 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
213 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
214 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
215 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
216 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
217 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_UPARROW );
218 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
219 /* MAP_CURSOR( GLUT_CURSOR_NONE, IDC_NO ); */
220 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
223 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_ARROW );
227 fgStructure.Window->State.Cursor = cursorID;
231 * Moves the mouse pointer to given window coordinates
233 void FGAPIENTRY glutWarpPointer( int x, int y )
235 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWarpPointer" );
236 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutWarpPointer" );
238 #if TARGET_HOST_UNIX_X11
243 fgStructure.Window->Window.Handle,
247 XFlush( fgDisplay.Display ); /* XXX Is this really necessary? */
249 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
256 /* ClientToScreen() translates {coords} for us. */
257 ClientToScreen( fgStructure.Window->Window.Handle, &coords );
258 SetCursorPos( coords.x, coords.y );
264 /*** END OF FILE ***/