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"
32 * TODO BEFORE THE STABLE RELEASE:
33 * glutSetCursor() -- Win32 mappings are incomplete.
35 * It would be good to use custom mouse cursor shapes, and introduce
36 * an option to display them using glBitmap() and/or texture mapping,
37 * apart from the windowing system version.
40 /* -- PRIVATE FUNCTIONS --------------------------------------------------- */
42 #if TARGET_HOST_POSIX_X11 || TARGET_HOST_MAC_OSX || TARGET_HOST_SOLARIS
43 #include <X11/cursorfont.h>
46 * A factory method for an empty cursor
48 static Cursor getEmptyCursor( void )
50 static Cursor cursorNone = None;
51 if( cursorNone == None ) {
52 char cursorNoneBits[ 32 ];
54 Pixmap cursorNonePixmap;
55 memset( cursorNoneBits, 0, sizeof( cursorNoneBits ) );
56 memset( &dontCare, 0, sizeof( dontCare ) );
57 cursorNonePixmap = XCreateBitmapFromData ( fgDisplay.Display,
59 cursorNoneBits, 16, 16 );
60 if( cursorNonePixmap != None ) {
61 cursorNone = XCreatePixmapCursor( fgDisplay.Display,
62 cursorNonePixmap, cursorNonePixmap,
63 &dontCare, &dontCare, 0, 0 );
64 XFreePixmap( fgDisplay.Display, cursorNonePixmap );
70 typedef struct tag_cursorCacheEntry cursorCacheEntry;
71 struct tag_cursorCacheEntry {
72 unsigned int cursorShape; /* an XC_foo value */
73 Cursor cachedCursor; /* None if the corresponding cursor has
74 not been created yet */
78 * Note: The arrangement of the table below depends on the fact that
79 * the "normal" GLUT_CURSOR_* values start a 0 and are consecutive.
81 static cursorCacheEntry cursorCache[] = {
82 { XC_arrow, None }, /* GLUT_CURSOR_RIGHT_ARROW */
83 { XC_top_left_arrow, None }, /* GLUT_CURSOR_LEFT_ARROW */
84 { XC_hand1, None }, /* GLUT_CURSOR_INFO */
85 { XC_pirate, None }, /* GLUT_CURSOR_DESTROY */
86 { XC_question_arrow, None }, /* GLUT_CURSOR_HELP */
87 { XC_exchange, None }, /* GLUT_CURSOR_CYCLE */
88 { XC_spraycan, None }, /* GLUT_CURSOR_SPRAY */
89 { XC_watch, None }, /* GLUT_CURSOR_WAIT */
90 { XC_xterm, None }, /* GLUT_CURSOR_TEXT */
91 { XC_crosshair, None }, /* GLUT_CURSOR_CROSSHAIR */
92 { XC_sb_v_double_arrow, None }, /* GLUT_CURSOR_UP_DOWN */
93 { XC_sb_h_double_arrow, None }, /* GLUT_CURSOR_LEFT_RIGHT */
94 { XC_top_side, None }, /* GLUT_CURSOR_TOP_SIDE */
95 { XC_bottom_side, None }, /* GLUT_CURSOR_BOTTOM_SIDE */
96 { XC_left_side, None }, /* GLUT_CURSOR_LEFT_SIDE */
97 { XC_right_side, None }, /* GLUT_CURSOR_RIGHT_SIDE */
98 { XC_top_left_corner, None }, /* GLUT_CURSOR_TOP_LEFT_CORNER */
99 { XC_top_right_corner, None }, /* GLUT_CURSOR_TOP_RIGHT_CORNER */
100 { XC_bottom_right_corner, None }, /* GLUT_CURSOR_BOTTOM_RIGHT_CORNER */
101 { XC_bottom_left_corner, None } /* GLUT_CURSOR_BOTTOM_LEFT_CORNER */
104 static void fghSetCursor ( SFG_Window *window, int cursorID )
108 * XXX FULL_CROSSHAIR demotes to plain CROSSHAIR. Old GLUT allows
109 * for this, but if there is a system that easily supports a full-
110 * window (or full-screen) crosshair, we might consider it.
113 ( cursorID == GLUT_CURSOR_FULL_CROSSHAIR ) ? GLUT_CURSOR_CROSSHAIR : cursorID;
115 if( ( cursorIDToUse >= 0 ) &&
116 ( cursorIDToUse < sizeof( cursorCache ) / sizeof( cursorCache[0] ) ) ) {
117 cursorCacheEntry *entry = &cursorCache[ cursorIDToUse ];
118 if( entry->cachedCursor == None ) {
119 entry->cachedCursor =
120 XCreateFontCursor( fgDisplay.Display, entry->cursorShape );
122 cursor = entry->cachedCursor;
124 switch( cursorIDToUse )
126 case GLUT_CURSOR_NONE:
127 cursor = getEmptyCursor( );
130 case GLUT_CURSOR_INHERIT:
135 fgError( "Unknown cursor type: %d", cursorIDToUse );
140 if ( cursorIDToUse == GLUT_CURSOR_INHERIT ) {
141 XUndefineCursor( fgDisplay.Display, window->Window.Handle );
142 } else if ( cursor != None ) {
143 XDefineCursor( fgDisplay.Display, window->Window.Handle, cursor );
144 } else if ( cursorIDToUse != GLUT_CURSOR_NONE ) {
145 fgError( "Failed to create cursor" );
150 static void fghWarpPointer ( int x, int y )
155 fgStructure.CurrentWindow->Window.Handle,
159 /* Make the warp visible immediately. */
160 XFlush( fgDisplay.Display );
165 #if TARGET_HOST_MS_WINDOWS
166 static void fghSetCursor ( SFG_Window *window, int cursorID )
169 * Joe Krahn is re-writing the following code.
171 /* Set the cursor AND change it for this window class. */
172 #if !defined(__MINGW64__) && _MSC_VER <= 1200
173 # define MAP_CURSOR(a,b) \
175 SetCursor( LoadCursor( NULL, b ) ); \
176 SetClassLong( window->Window.Handle, \
178 ( LONG )LoadCursor( NULL, b ) ); \
180 /* Nuke the cursor AND change it for this window class. */
181 # define ZAP_CURSOR(a,b) \
184 SetClassLong( window->Window.Handle, \
185 GCL_HCURSOR, ( LONG )NULL ); \
188 # define MAP_CURSOR(a,b) \
190 SetCursor( LoadCursor( NULL, b ) ); \
191 SetClassLongPtr( window->Window.Handle, \
193 ( LONG )( LONG_PTR )LoadCursor( NULL, b ) ); \
195 /* Nuke the cursor AND change it for this window class. */
196 # define ZAP_CURSOR(a,b) \
199 SetClassLongPtr( window->Window.Handle, \
200 GCLP_HCURSOR, ( LONG )( LONG_PTR )NULL ); \
206 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
207 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
208 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
209 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
210 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
211 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
212 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
213 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
214 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_IBEAM );
215 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
216 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_SIZENS );
217 MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT, IDC_SIZEWE );
218 MAP_CURSOR( GLUT_CURSOR_TOP_SIDE, IDC_ARROW ); /* XXX ToDo */
219 MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE, IDC_ARROW ); /* XXX ToDo */
220 MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE, IDC_ARROW ); /* XXX ToDo */
221 MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE, IDC_ARROW ); /* XXX ToDo */
222 MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER, IDC_SIZENWSE );
223 MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER, IDC_SIZENESW );
224 MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER, IDC_SIZENWSE );
225 MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER, IDC_SIZENESW );
226 MAP_CURSOR( GLUT_CURSOR_INHERIT, IDC_ARROW ); /* XXX ToDo */
227 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
228 MAP_CURSOR( GLUT_CURSOR_FULL_CROSSHAIR, IDC_CROSS ); /* XXX ToDo */
231 fgError( "Unknown cursor type: %d", cursorID );
237 static void fghWarpPointer ( int x, int y )
243 /* ClientToScreen() translates {coords} for us. */
244 ClientToScreen( fgStructure.CurrentWindow->Window.Handle, &coords );
245 SetCursorPos( coords.x, coords.y );
250 /* -- INTERNAL FUNCTIONS ---------------------------------------------------- */
251 void fgSetCursor ( SFG_Window *window, int cursorID )
253 fghSetCursor ( window, cursorID );
257 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
260 * Set the cursor image to be used for the current window
262 void FGAPIENTRY glutSetCursor( int cursorID )
264 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetCursor" );
265 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetCursor" );
267 fghSetCursor ( fgStructure.CurrentWindow, cursorID );
268 fgStructure.CurrentWindow->State.Cursor = cursorID;
272 * Moves the mouse pointer to given window coordinates
274 void FGAPIENTRY glutWarpPointer( int x, int y )
276 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWarpPointer" );
277 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutWarpPointer" );
279 fghWarpPointer ( x, y );
282 /*** END OF FILE ***/