Trying again to change \"fgStructure.Window\" to \"fgStructure.CurrentWindow\" and...
[freeglut] / src / freeglut_cursor.c
1 /*
2  * freeglut_cursor.c
3  *
4  * The mouse cursor related stuff.
5  *
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
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
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.
26  */
27
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
30
31 #if TARGET_HOST_UNIX_X11
32   #include <X11/cursorfont.h>
33 #endif
34
35 /*
36  * TODO BEFORE THE STABLE RELEASE:
37  *  glutSetCursor()     -- Win32 mappings are incomplete.
38  *
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.
42  */
43
44 /* -- INTERNAL FUNCTIONS --------------------------------------------------- */
45
46 #if TARGET_HOST_UNIX_X11
47 /*
48  * A factory method for an empty cursor
49  */
50 static Cursor getEmptyCursor( void )
51 {
52     static Cursor cursorNone = None;
53     if( cursorNone == None ) {
54         char cursorNoneBits[ 32 ];
55         XColor dontCare;
56         Pixmap cursorNonePixmap;
57         memset( cursorNoneBits, 0, sizeof( cursorNoneBits ) );
58         memset( &dontCare, 0, sizeof( dontCare ) );
59         cursorNonePixmap = XCreateBitmapFromData ( fgDisplay.Display,
60                                                    fgDisplay.RootWindow,
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 );
67         }
68     }
69     return cursorNone;
70 }
71
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 */
77 };
78
79 /*
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.
82  */ 
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 */
104 };
105 #endif
106
107 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
108
109 /*
110  * Set the cursor image to be used for the current window
111  */
112 void FGAPIENTRY glutSetCursor( int cursorID )
113 {
114     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetCursor" );
115     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetCursor" );
116
117 #if TARGET_HOST_UNIX_X11
118     {
119         Cursor cursor;
120         /*
121          * XXX FULL_CROSSHAIR demotes to plain CROSSHAIR. Old GLUT allows
122          * for this, but if there is a system that easily supports a full-
123          * window (or full-screen) crosshair, we might consider it.
124          */
125         int cursorIDToUse =
126             ( cursorID == GLUT_CURSOR_FULL_CROSSHAIR ) ? GLUT_CURSOR_CROSSHAIR : cursorID;
127
128         if( ( cursorIDToUse >= 0 ) &&
129             ( cursorIDToUse < sizeof( cursorCache ) / sizeof( cursorCache[0] ) ) ) {
130             cursorCacheEntry *entry = &cursorCache[ cursorIDToUse ];
131             if( entry->cachedCursor == None ) {
132                 entry->cachedCursor =
133                     XCreateFontCursor( fgDisplay.Display, entry->cursorShape );
134             }
135             cursor = entry->cachedCursor;
136         } else {
137             switch( cursorIDToUse )
138             {
139             case GLUT_CURSOR_NONE:
140                 cursor = getEmptyCursor( );
141                 break;
142
143             case GLUT_CURSOR_INHERIT:
144                 cursor = None;
145                 break;
146
147             default:
148                 fgError( "Unknown cursor type: %d", cursorIDToUse );
149                 return;
150             }
151         }
152
153         if ( ( cursorIDToUse != GLUT_CURSOR_NONE ) && ( cursor == None ) ) {
154             fgError( "Failed to create cursor" );
155         }
156         XDefineCursor( fgDisplay.Display,
157                        fgStructure.CurrentWindow->Window.Handle, cursor );
158     }
159
160 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
161
162     /*
163      * This is a temporary solution only...
164      */
165     /* Set the cursor AND change it for this window class. */
166 #       define MAP_CURSOR(a,b)                                          \
167         case a:                                                         \
168             SetCursor( LoadCursor( NULL, b ) );                         \
169             SetClassLong( fgStructure.CurrentWindow->Window.Handle,     \
170                           GCL_HCURSOR,                                  \
171                           ( LONG )LoadCursor( NULL, b ) );              \
172         break;
173
174     /* Nuke the cursor AND change it for this window class. */
175 #       define ZAP_CURSOR(a,b)                                          \
176         case a:                                                         \
177             SetCursor( NULL );                                          \
178             SetClassLong( fgStructure.CurrentWindow->Window.Handle,     \
179                           GCL_HCURSOR, ( LONG )NULL );                  \
180         break;
181
182     switch( cursorID )
183     {
184         MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW,         IDC_ARROW     );
185         MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,          IDC_ARROW     );
186         MAP_CURSOR( GLUT_CURSOR_INFO,                IDC_HELP      );
187         MAP_CURSOR( GLUT_CURSOR_DESTROY,             IDC_CROSS     );
188         MAP_CURSOR( GLUT_CURSOR_HELP,                IDC_HELP      );
189         MAP_CURSOR( GLUT_CURSOR_CYCLE,               IDC_SIZEALL   );
190         MAP_CURSOR( GLUT_CURSOR_SPRAY,               IDC_CROSS     );
191         MAP_CURSOR( GLUT_CURSOR_WAIT,                IDC_WAIT      );
192         MAP_CURSOR( GLUT_CURSOR_TEXT,                IDC_IBEAM     );
193         MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,           IDC_CROSS     );
194         MAP_CURSOR( GLUT_CURSOR_UP_DOWN,             IDC_SIZENS    );
195         MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT,          IDC_SIZEWE    );
196         MAP_CURSOR( GLUT_CURSOR_TOP_SIDE,            IDC_ARROW     ); /* XXX ToDo */
197         MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE,         IDC_ARROW     ); /* XXX ToDo */
198         MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE,           IDC_ARROW     ); /* XXX ToDo */
199         MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE,          IDC_ARROW     ); /* XXX ToDo */
200         MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER,     IDC_SIZENWSE  );
201         MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER,    IDC_SIZENESW  );
202         MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER, IDC_SIZENWSE  );
203         MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER,  IDC_SIZENESW  );
204         MAP_CURSOR( GLUT_CURSOR_INHERIT,             IDC_ARROW     ); /* XXX ToDo */
205         ZAP_CURSOR( GLUT_CURSOR_NONE,                NULL          );
206         MAP_CURSOR( GLUT_CURSOR_FULL_CROSSHAIR,      IDC_CROSS     ); /* XXX ToDo */
207
208     default:
209         fgError( "Unknown cursor type: %d", cursorID );
210         break;
211     }
212 #endif
213
214     fgStructure.CurrentWindow->State.Cursor = cursorID;
215 }
216
217 /*
218  * Moves the mouse pointer to given window coordinates
219  */
220 void FGAPIENTRY glutWarpPointer( int x, int y )
221 {
222     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWarpPointer" );
223     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutWarpPointer" );
224
225 #if TARGET_HOST_UNIX_X11
226
227     XWarpPointer(
228         fgDisplay.Display,
229         None,
230         fgStructure.CurrentWindow->Window.Handle,
231         0, 0, 0, 0,
232         x, y
233     );
234     /* Make the warp visible immediately. */
235     XFlush( fgDisplay.Display );
236
237 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
238
239     {
240         POINT coords;
241         coords.x = x;
242         coords.y = y;
243
244         /* ClientToScreen() translates {coords} for us. */
245         ClientToScreen( fgStructure.CurrentWindow->Window.Handle, &coords );
246         SetCursorPos( coords.x, coords.y );
247     }
248
249 #endif
250 }
251
252 /*** END OF FILE ***/