Removed glib dependancy
[freeglut] / freeglut-1.3 / 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 "../include/GL/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  *
44  *  fgDisplayCursor()   -- this waits for better times
45  *  glutSetCursor()     -- both X and Win32 mappings are incomplete
46  *  glutWarpPointer()   -- check the Win32 version
47  *
48  * It would be good to use custom mouse cursor shapes, and introduce
49  * an option to display them using glBitmap() and/or texture mapping,
50  * apart from the windowing system version.
51  */
52
53 /* -- INTERNAL FUNCTIONS --------------------------------------------------- */
54
55 /*
56  * Display the mouse cursor using OpenGL calls
57  */
58 void fgDisplayCursor( void )
59 {
60     /*
61      * Do nothing for the moment
62      */
63 }
64
65
66 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
67
68 /*
69  * Set the cursor image to be used for the current window
70  */
71 void FGAPIENTRY glutSetCursor( int cursorID )
72 {
73     /*
74      * Make sure freeglut is ready and there is a current window set
75      */
76     freeglut_assert_ready; freeglut_assert_window;
77
78 #if TARGET_HOST_UNIX_X11
79         {
80                 Cursor cursor;
81
82             /*
83                  * For now we'll limit ourselves to the X cursor fonts...
84                  */
85 #               define MAP_CURSOR(a,b) case a: cursor = XCreateFontCursor( fgDisplay.Display, b ); break;
86
87                 switch( cursorID )
88                 {
89                         MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, XC_left_ptr        );
90                         MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  XC_right_ptr       );
91                         MAP_CURSOR( GLUT_CURSOR_INFO,        XC_question_arrow  );
92                         MAP_CURSOR( GLUT_CURSOR_DESTROY,     XC_target          );
93                         MAP_CURSOR( GLUT_CURSOR_HELP,        XC_question_arrow  );
94                         MAP_CURSOR( GLUT_CURSOR_CYCLE,       XC_circle          );
95                         MAP_CURSOR( GLUT_CURSOR_SPRAY,       XC_spraycan        );
96                         MAP_CURSOR( GLUT_CURSOR_WAIT,        XC_watch           );
97                         MAP_CURSOR( GLUT_CURSOR_TEXT,        XC_draft_large     );
98                         MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   XC_crosshair       );
99                         MAP_CURSOR( GLUT_CURSOR_NONE,        XC_trek            );
100
101                         default:
102                         MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     XC_arrow           );
103                 }
104
105             /*
106              * Define a window's cursor now
107              */
108             XDefineCursor( fgDisplay.Display, fgStructure.Window->Window.Handle, cursor );
109         }
110
111 #elif TARGET_HOST_WIN32
112         /*
113          * This is a temporary solution only...
114          */
115 #       define MAP_CURSOR(a,b) case a: SetCursor( LoadCursor( NULL, b ) ); break;
116
117         switch( cursorID )
118         {
119                 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW     );
120                 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  IDC_ARROW     );
121                 MAP_CURSOR( GLUT_CURSOR_INFO,        IDC_HELP      );
122                 MAP_CURSOR( GLUT_CURSOR_DESTROY,     IDC_CROSS     );
123                 MAP_CURSOR( GLUT_CURSOR_HELP,        IDC_HELP      );
124                 MAP_CURSOR( GLUT_CURSOR_CYCLE,       IDC_SIZEALL   );
125                 MAP_CURSOR( GLUT_CURSOR_SPRAY,       IDC_CROSS     );
126                 MAP_CURSOR( GLUT_CURSOR_WAIT,            IDC_WAIT      );
127                 MAP_CURSOR( GLUT_CURSOR_TEXT,        IDC_UPARROW   );
128                 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   IDC_CROSS     );
129                 MAP_CURSOR( GLUT_CURSOR_NONE,        IDC_NO                );
130
131                 default:
132                 MAP_CURSOR( GLUT_CURSOR_UP_DOWN,         IDC_UPARROW   );
133         }
134
135 #endif
136
137     /*
138      * Remember the currently selected cursor
139      */
140     fgStructure.Window->State.Cursor = cursorID;
141 }
142
143 /*
144  * Moves the mouse pointer to given window coordinates
145  */
146 void FGAPIENTRY glutWarpPointer( int x, int y )
147 {
148     freeglut_assert_ready; freeglut_assert_window;
149
150 #if TARGET_HOST_UNIX_X11
151     /*
152      * Move the mouse pointer to given window coordinates
153      */
154     XWarpPointer(
155         fgDisplay.Display,
156         None,
157         fgStructure.Window->Window.Handle,
158         0, 0, 0, 0,
159         x, y
160     );
161
162     XFlush( fgDisplay.Display );
163
164 #elif TARGET_HOST_WIN32
165     {
166         POINT coords = { x, y };
167
168         /*
169          * First of all, we need to find the new screen-relative coordinates of the mouse cursor
170          */
171         ClientToScreen( fgStructure.Window->Window.Handle, &coords );
172
173         /*
174          * Now set the new mouse cursor position...
175          */
176         SetCursorPos( coords.x, coords.y );
177     }
178
179 #endif
180 }
181
182 /*** END OF FILE ***/