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 /* -- PRIVATE FUNCTIONS --------------------------------------------------- */
46 #if TARGET_HOST_UNIX_X11
48 * A factory method for an empty cursor
50 static Cursor getEmptyCursor( void )
52 static Cursor cursorNone = None;
53 if( cursorNone == None ) {
54 char cursorNoneBits[ 32 ];
56 Pixmap cursorNonePixmap;
57 memset( cursorNoneBits, 0, sizeof( cursorNoneBits ) );
58 memset( &dontCare, 0, sizeof( dontCare ) );
59 cursorNonePixmap = XCreateBitmapFromData ( fgDisplay.Display,
61 cursorNoneBits, 16, 16 );
62 if( cursorNonePixmap != None ) {
63 cursorNone = XCreatePixmapCursor( fgDisplay.Display,
64 cursorNonePixmap, cursorNonePixmap,
65 &dontCare, &dontCare, 0, 0 );
66 XFreePixmap( fgDisplay.Display, cursorNonePixmap );
72 typedef struct tag_cursorCacheEntry cursorCacheEntry;
73 struct tag_cursorCacheEntry {
74 unsigned int cursorShape; /* an XC_foo value */
75 Cursor cachedCursor; /* None if the corresponding cursor has
76 not been created yet */
80 * Note: The arrangement of the table below depends on the fact that
81 * the "normal" GLUT_CURSOR_* values start a 0 and are consecutive.
83 static cursorCacheEntry cursorCache[] = {
84 { XC_arrow, None }, /* GLUT_CURSOR_RIGHT_ARROW */
85 { XC_top_left_arrow, None }, /* GLUT_CURSOR_LEFT_ARROW */
86 { XC_hand1, None }, /* GLUT_CURSOR_INFO */
87 { XC_pirate, None }, /* GLUT_CURSOR_DESTROY */
88 { XC_question_arrow, None }, /* GLUT_CURSOR_HELP */
89 { XC_exchange, None }, /* GLUT_CURSOR_CYCLE */
90 { XC_spraycan, None }, /* GLUT_CURSOR_SPRAY */
91 { XC_watch, None }, /* GLUT_CURSOR_WAIT */
92 { XC_xterm, None }, /* GLUT_CURSOR_TEXT */
93 { XC_crosshair, None }, /* GLUT_CURSOR_CROSSHAIR */
94 { XC_sb_v_double_arrow, None }, /* GLUT_CURSOR_UP_DOWN */
95 { XC_sb_h_double_arrow, None }, /* GLUT_CURSOR_LEFT_RIGHT */
96 { XC_top_side, None }, /* GLUT_CURSOR_TOP_SIDE */
97 { XC_bottom_side, None }, /* GLUT_CURSOR_BOTTOM_SIDE */
98 { XC_left_side, None }, /* GLUT_CURSOR_LEFT_SIDE */
99 { XC_right_side, None }, /* GLUT_CURSOR_RIGHT_SIDE */
100 { XC_top_left_corner, None }, /* GLUT_CURSOR_TOP_LEFT_CORNER */
101 { XC_top_right_corner, None }, /* GLUT_CURSOR_TOP_RIGHT_CORNER */
102 { XC_bottom_right_corner, None }, /* GLUT_CURSOR_BOTTOM_RIGHT_CORNER */
103 { XC_bottom_left_corner, None } /* GLUT_CURSOR_BOTTOM_LEFT_CORNER */
107 /* -- INTERNAL FUNCTIONS ---------------------------------------------------- */
110 * Set the cursor image to be used for the current window
112 void fgSetCursor ( SFG_Window *window, int cursorID )
114 #if TARGET_HOST_UNIX_X11
118 * XXX FULL_CROSSHAIR demotes to plain CROSSHAIR. Old GLUT allows
119 * for this, but if there is a system that easily supports a full-
120 * window (or full-screen) crosshair, we might consider it.
123 ( cursorID == GLUT_CURSOR_FULL_CROSSHAIR ) ? GLUT_CURSOR_CROSSHAIR : cursorID;
125 if( ( cursorIDToUse >= 0 ) &&
126 ( cursorIDToUse < sizeof( cursorCache ) / sizeof( cursorCache[0] ) ) ) {
127 cursorCacheEntry *entry = &cursorCache[ cursorIDToUse ];
128 if( entry->cachedCursor == None ) {
129 entry->cachedCursor =
130 XCreateFontCursor( fgDisplay.Display, entry->cursorShape );
132 cursor = entry->cachedCursor;
134 switch( cursorIDToUse )
136 case GLUT_CURSOR_NONE:
137 cursor = getEmptyCursor( );
140 case GLUT_CURSOR_INHERIT:
145 fgError( "Unknown cursor type: %d", cursorIDToUse );
150 if ( ( cursorIDToUse != GLUT_CURSOR_NONE ) && ( cursor == None ) ) {
151 fgError( "Failed to create cursor" );
153 XDefineCursor( fgDisplay.Display,
154 window->Window.Handle, cursor );
157 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
160 * This is a temporary solution only...
162 /* Set the cursor AND change it for this window class. */
163 # define MAP_CURSOR(a,b) \
165 SetCursor( LoadCursor( NULL, b ) ); \
166 SetClassLong( window->Window.Handle, \
168 ( LONG )LoadCursor( NULL, b ) ); \
171 /* Nuke the cursor AND change it for this window class. */
172 # define ZAP_CURSOR(a,b) \
175 SetClassLong( window->Window.Handle, \
176 GCL_HCURSOR, ( LONG )NULL ); \
181 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
182 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
183 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
184 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
185 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
186 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
187 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
188 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
189 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_IBEAM );
190 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
191 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_SIZENS );
192 MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT, IDC_SIZEWE );
193 MAP_CURSOR( GLUT_CURSOR_TOP_SIDE, IDC_ARROW ); /* XXX ToDo */
194 MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE, IDC_ARROW ); /* XXX ToDo */
195 MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE, IDC_ARROW ); /* XXX ToDo */
196 MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE, IDC_ARROW ); /* XXX ToDo */
197 MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER, IDC_SIZENWSE );
198 MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER, IDC_SIZENESW );
199 MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER, IDC_SIZENWSE );
200 MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER, IDC_SIZENESW );
201 MAP_CURSOR( GLUT_CURSOR_INHERIT, IDC_ARROW ); /* XXX ToDo */
202 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
203 MAP_CURSOR( GLUT_CURSOR_FULL_CROSSHAIR, IDC_CROSS ); /* XXX ToDo */
206 fgError( "Unknown cursor type: %d", cursorID );
211 window->State.Cursor = cursorID;
214 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
217 * Set the cursor image to be used for the current window
219 void FGAPIENTRY glutSetCursor( int cursorID )
221 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetCursor" );
222 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetCursor" );
224 fgSetCursor ( fgStructure.CurrentWindow, cursorID );
228 * Moves the mouse pointer to given window coordinates
230 void FGAPIENTRY glutWarpPointer( int x, int y )
232 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWarpPointer" );
233 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutWarpPointer" );
235 #if TARGET_HOST_UNIX_X11
240 fgStructure.CurrentWindow->Window.Handle,
244 /* Make the warp visible immediately. */
245 XFlush( fgDisplay.Display );
247 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
254 /* ClientToScreen() translates {coords} for us. */
255 ClientToScreen( fgStructure.CurrentWindow->Window.Handle, &coords );
256 SetCursorPos( coords.x, coords.y );
262 /*** END OF FILE ***/