Propogated a pointer-check from menu-attach to menu-detach. (Apparently,
[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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #define  G_LOG_DOMAIN  "freeglut-cursor"
33
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
36
37 #if TARGET_HOST_UNIX_X11
38     #include <X11/cursorfont.h>
39 #endif
40
41 /*
42  * TODO BEFORE THE STABLE RELEASE:
43  *  glutSetCursor()     -- Win32 mappings are incomplete
44  *                         X mappings are nearly right.
45  *
46  * It would be good to use custom mouse cursor shapes, and introduce
47  * an option to display them using glBitmap() and/or texture mapping,
48  * apart from the windowing system version.
49  */
50
51 /* -- INTERNAL FUNCTIONS --------------------------------------------------- */
52
53 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
54
55 /*
56  * Set the cursor image to be used for the current window
57  */
58 void FGAPIENTRY glutSetCursor( int cursorID )
59 {
60     freeglut_assert_ready;
61     freeglut_assert_window;
62
63 #if TARGET_HOST_UNIX_X11
64     /*
65      * Open issues:
66      * (a) GLUT_CURSOR_NONE doesn't do what it should.  We can probably
67      *     build an empty pixmap for it, though, quite painlessly.
68      * (b) Are we allocating resources, or causing X to do so?
69      *     If yes, we should arrange to deallocate!
70      * (c) No error checking.  Is that a problem?
71      * (d) FULL_CROSSHAIR demotes to plain CROSSHAIR.  Old GLUT allows
72      *     for this, but if there is a system that easily supports a full-
73      *     window (or full-screen) crosshair, we might consider it.
74      * (e) Out-of-range cursor-types are ignored.  Should we abort?
75      *     Print a warning message?
76      */
77     {
78         Cursor cursor;
79 #define MAP_CURSOR(a,b) case a: cursor = XCreateFontCursor( fgDisplay.Display, b ); break;
80         if( GLUT_CURSOR_FULL_CROSSHAIR == cursorID )
81             cursorID = GLUT_CURSOR_CROSSHAIR;
82         switch( cursorID )
83         {
84             MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, XC_right_ptr);
85             MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  XC_left_ptr);
86             MAP_CURSOR( GLUT_CURSOR_INFO,        XC_hand1);
87             MAP_CURSOR( GLUT_CURSOR_DESTROY,     XC_pirate);
88             MAP_CURSOR( GLUT_CURSOR_HELP,        XC_question_arrow);
89             MAP_CURSOR( GLUT_CURSOR_CYCLE,       XC_exchange);
90             MAP_CURSOR( GLUT_CURSOR_SPRAY,       XC_spraycan);
91             MAP_CURSOR( GLUT_CURSOR_WAIT,        XC_watch);
92             MAP_CURSOR( GLUT_CURSOR_TEXT,        XC_xterm);
93             MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   XC_crosshair);
94             MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     XC_sb_v_double_arrow);
95             MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT,  XC_sb_h_double_arrow);
96             MAP_CURSOR( GLUT_CURSOR_TOP_SIDE,    XC_top_side);
97             MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE, XC_bottom_side);
98             MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE,   XC_left_side);
99             MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE,  XC_right_side);
100             MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER,     XC_top_left_corner);
101             MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER,    XC_top_right_corner);
102             MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER, XC_bottom_right_corner);
103             MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER, XC_bottom_left_corner);
104             MAP_CURSOR( GLUT_CURSOR_NONE,        XC_bogosity);
105         case GLUT_CURSOR_INHERIT:
106             break;
107         default:
108             return;
109         }
110         
111         if( GLUT_CURSOR_INHERIT == cursorID )
112             XUndefineCursor( fgDisplay.Display, fgStructure.Window->Window.Handle );
113         else
114             XDefineCursor( fgDisplay.Display, fgStructure.Window->Window.Handle, cursor );
115     }
116
117 #elif TARGET_HOST_WIN32
118
119         /*
120          * This is a temporary solution only...
121          */
122         /* Set the cursor AND change it for this window class. */
123 #       define MAP_CURSOR(a,b) case a: SetCursor( LoadCursor( NULL, b ) ); \
124         SetClassLong(fgStructure.Window->Window.Handle,GCL_HCURSOR,(LONG)LoadCursor(NULL,b)); \
125         break;
126         /* Nuke the cursor AND change it for this window class. */
127 #       define ZAP_CURSOR(a,b) case a: SetCursor( NULL ); \
128         SetClassLong(fgStructure.Window->Window.Handle,GCL_HCURSOR,(LONG)NULL); \
129         break;
130
131         switch( cursorID )
132         {
133                 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW     );
134                 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  IDC_ARROW     );
135                 MAP_CURSOR( GLUT_CURSOR_INFO,        IDC_HELP      );
136                 MAP_CURSOR( GLUT_CURSOR_DESTROY,     IDC_CROSS     );
137                 MAP_CURSOR( GLUT_CURSOR_HELP,        IDC_HELP      );
138                 MAP_CURSOR( GLUT_CURSOR_CYCLE,       IDC_SIZEALL   );
139                 MAP_CURSOR( GLUT_CURSOR_SPRAY,       IDC_CROSS     );
140                 MAP_CURSOR( GLUT_CURSOR_WAIT,            IDC_WAIT      );
141                 MAP_CURSOR( GLUT_CURSOR_TEXT,        IDC_UPARROW   );
142                 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   IDC_CROSS     );
143                 /* MAP_CURSOR( GLUT_CURSOR_NONE,        IDC_NO             ); */
144                 ZAP_CURSOR( GLUT_CURSOR_NONE,        NULL          );
145
146                 default:
147                 MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     IDC_ARROW     );
148         }
149 #endif
150
151     fgStructure.Window->State.Cursor = cursorID;
152 }
153
154 /*
155  * Moves the mouse pointer to given window coordinates
156  */
157 void FGAPIENTRY glutWarpPointer( int x, int y )
158 {
159     freeglut_assert_ready;
160     freeglut_assert_window;
161
162 #if TARGET_HOST_UNIX_X11
163
164     XWarpPointer(
165         fgDisplay.Display,
166         None,
167         fgStructure.Window->Window.Handle,
168         0, 0, 0, 0,
169         x, y
170     );
171     XFlush( fgDisplay.Display );
172
173 #elif TARGET_HOST_WIN32
174
175     {
176         POINT coords = { x, y };
177         /*
178          * ClientToScreen() translates {coords} for us.
179          */
180         ClientToScreen( fgStructure.Window->Window.Handle, &coords );
181         SetCursorPos( coords.x, coords.y );
182     }
183
184 #endif
185
186 }
187
188 /*** END OF FILE ***/