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