can now set the font used for a menu
[freeglut] / src / fg_state.c
1 /*
2  * freeglut_state.c
3  *
4  * Freeglut state query methods.
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 "fg_internal.h"
30
31 /*
32  * TODO BEFORE THE STABLE RELEASE:
33  *
34  *  glutGet()               -- X11 tests passed, but check if all enums
35  *                             handled (what about Win32?)
36  *  glutDeviceGet()         -- X11 tests passed, but check if all enums
37  *                             handled (what about Win32?)
38  *  glutGetModifiers()      -- OK, but could also remove the limitation
39  *  glutLayerGet()          -- what about GLUT_NORMAL_DAMAGED?
40  *
41  * The fail-on-call policy will help adding the most needed things imho.
42  */
43
44 extern int fgPlatformGlutGet ( GLenum eWhat );
45 extern int fgPlatformGlutDeviceGet ( GLenum eWhat );
46 extern int *fgPlatformGlutGetModeValues(GLenum eWhat, int *size);
47 extern SFG_Font* fghFontByID( void* font );
48
49
50 /* -- LOCAL DEFINITIONS ---------------------------------------------------- */
51
52 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
53
54
55 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
56
57 /*
58  * General settings assignment method
59  */
60 void FGAPIENTRY glutSetOption( GLenum eWhat, int value )
61 {
62     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetOption" );
63
64     /*
65      * XXX In chronological code add order.  (WHY in that order?)
66      */
67     switch( eWhat )
68     {
69     case GLUT_INIT_WINDOW_X:
70         fgState.Position.X = (GLint)value;
71         break;
72
73     case GLUT_INIT_WINDOW_Y:
74         fgState.Position.Y = (GLint)value;
75         break;
76
77     case GLUT_INIT_WINDOW_WIDTH:
78         fgState.Size.X = (GLint)value;
79         break;
80
81     case GLUT_INIT_WINDOW_HEIGHT:
82         fgState.Size.Y = (GLint)value;
83         break;
84
85     case GLUT_INIT_DISPLAY_MODE:
86         fgState.DisplayMode = (unsigned int)value;
87         break;
88
89     case GLUT_ACTION_ON_WINDOW_CLOSE:
90         fgState.ActionOnWindowClose = value;
91         break;
92
93     case GLUT_RENDERING_CONTEXT:
94         fgState.UseCurrentContext =
95             ( value == GLUT_USE_CURRENT_CONTEXT ) ? GL_TRUE : GL_FALSE;
96         break;
97
98     case GLUT_DIRECT_RENDERING:
99         fgState.DirectContext = value;
100         break;
101
102     case GLUT_WINDOW_CURSOR:
103         if( fgStructure.CurrentWindow != NULL )
104             fgStructure.CurrentWindow->State.Cursor = value;
105         break;
106
107     case GLUT_AUX:
108       fgState.AuxiliaryBufferNumber = value;
109       break;
110
111     case GLUT_MULTISAMPLE:
112       fgState.SampleNumber = value;
113       break;
114
115     case GLUT_SKIP_STALE_MOTION_EVENTS:
116       fgState.SkipStaleMotion = value;
117       break;
118
119     case GLUT_GEOMETRY_VISUALIZE_NORMALS:
120       if( fgStructure.CurrentWindow != NULL )
121         fgStructure.CurrentWindow->State.VisualizeNormals = value;
122       break;
123
124     case GLUT_MENU_FONT:
125         {
126             void* fontID = (void*)value;
127             SFG_Font* font;
128             font = fghFontByID( fontID );
129             if (!font)
130                 fgWarning("glutSetOption(GLUT_MENU_FONT,...): bitmap font 0x%08x not found. Make sure you're not passing a stroke font. Ignoring...\n",fontID);
131             else
132                 fgState.MenuFont = fontID;
133         }
134       break;
135
136     default:
137         fgWarning( "glutSetOption(): missing enum handle %d", eWhat );
138         break;
139     }
140 }
141
142 /*
143  * General settings query method
144  */
145 int FGAPIENTRY glutGet( GLenum eWhat )
146 {
147     switch (eWhat)
148     {
149     case GLUT_INIT_STATE:
150         return fgState.Initialised;
151
152     /* Although internally the time store is 64bits wide, the return value
153      * here still wraps every 49.7 days. Integer overflows cancel however
154      * when subtracting an initial start time, unless the total time exceeds
155      * 32-bit, so you can still work with this.
156      * XXX: a glutGet64 to return the time might be an idea...
157      */
158     case GLUT_ELAPSED_TIME:
159         return (int) fgElapsedTime();
160     }
161
162     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGet" );
163
164     /* XXX In chronological code add order.  (WHY in that order?) */
165     switch( eWhat )
166     {
167     /* Following values are stored in fgState and fgDisplay global structures */
168     case GLUT_SCREEN_WIDTH:         return fgDisplay.ScreenWidth   ;
169     case GLUT_SCREEN_HEIGHT:        return fgDisplay.ScreenHeight  ;
170     case GLUT_SCREEN_WIDTH_MM:      return fgDisplay.ScreenWidthMM ;
171     case GLUT_SCREEN_HEIGHT_MM:     return fgDisplay.ScreenHeightMM;
172     case GLUT_INIT_WINDOW_X:        return fgState.Position.Use ?
173                                            fgState.Position.X : -1 ;
174     case GLUT_INIT_WINDOW_Y:        return fgState.Position.Use ?
175                                            fgState.Position.Y : -1 ;
176     case GLUT_INIT_WINDOW_WIDTH:    return fgState.Size.Use ?
177                                            fgState.Size.X : -1     ;
178     case GLUT_INIT_WINDOW_HEIGHT:   return fgState.Size.Use ?
179                                            fgState.Size.Y : -1     ;
180     case GLUT_INIT_DISPLAY_MODE:    return fgState.DisplayMode     ;
181     case GLUT_INIT_MAJOR_VERSION:   return fgState.MajorVersion    ;
182     case GLUT_INIT_MINOR_VERSION:   return fgState.MinorVersion    ;
183     case GLUT_INIT_FLAGS:           return fgState.ContextFlags    ;
184     case GLUT_INIT_PROFILE:         return fgState.ContextProfile  ;
185
186     /* The window structure queries */
187     case GLUT_WINDOW_PARENT:
188         if( fgStructure.CurrentWindow         == NULL ) return 0;
189         if( fgStructure.CurrentWindow->Parent == NULL ) return 0;
190         return fgStructure.CurrentWindow->Parent->ID;
191
192     case GLUT_WINDOW_NUM_CHILDREN:
193         if( fgStructure.CurrentWindow == NULL )
194             return 0;
195         return fgListLength( &fgStructure.CurrentWindow->Children );
196
197     case GLUT_WINDOW_CURSOR:
198         if( fgStructure.CurrentWindow == NULL )
199             return 0;
200         return fgStructure.CurrentWindow->State.Cursor;
201
202     case GLUT_MENU_NUM_ITEMS:
203         if( fgStructure.CurrentMenu == NULL )
204             return 0;
205         return fgListLength( &fgStructure.CurrentMenu->Entries );
206
207     case GLUT_ACTION_ON_WINDOW_CLOSE:
208         return fgState.ActionOnWindowClose;
209
210     case GLUT_VERSION :
211         return VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH;
212
213     case GLUT_RENDERING_CONTEXT:
214         return fgState.UseCurrentContext ? GLUT_USE_CURRENT_CONTEXT
215                                          : GLUT_CREATE_NEW_CONTEXT;
216
217     case GLUT_DIRECT_RENDERING:
218         return fgState.DirectContext;
219
220     case GLUT_FULL_SCREEN:
221         return fgStructure.CurrentWindow->State.IsFullscreen;
222
223     case GLUT_AUX:
224       return fgState.AuxiliaryBufferNumber;
225
226     case GLUT_MULTISAMPLE:
227       return fgState.SampleNumber;
228
229     case GLUT_SKIP_STALE_MOTION_EVENTS:
230       return fgState.SkipStaleMotion;
231
232     case GLUT_GEOMETRY_VISUALIZE_NORMALS:
233       if( fgStructure.CurrentWindow == NULL )
234         return GL_FALSE;
235       return fgStructure.CurrentWindow->State.VisualizeNormals;
236
237     default:
238         return fgPlatformGlutGet ( eWhat );
239         break;
240     }
241     return -1;
242 }
243
244 /*
245  * Returns various device information.
246  */
247 int FGAPIENTRY glutDeviceGet( GLenum eWhat )
248 {
249     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDeviceGet" );
250
251     /* XXX WARNING: we are mostly lying in this function. */
252     switch( eWhat )
253     {
254     case GLUT_HAS_JOYSTICK:
255         return fgJoystickDetect ();
256
257     case GLUT_OWNS_JOYSTICK:
258         return fgState.JoysticksInitialised;
259
260     case GLUT_JOYSTICK_POLL_RATE:
261         return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.JoystickPollRate : 0;
262
263     /* XXX The following two are only for Joystick 0 but this is an improvement */
264     case GLUT_JOYSTICK_BUTTONS:
265         return glutJoystickGetNumButtons ( 0 );
266
267     case GLUT_JOYSTICK_AXES:
268         return glutJoystickGetNumAxes ( 0 );
269
270     case GLUT_HAS_DIAL_AND_BUTTON_BOX:
271         return fgInputDeviceDetect ();
272
273     case GLUT_NUM_DIALS:
274         if ( fgState.InputDevsInitialised ) return 8;
275         return 0;
276  
277     case GLUT_NUM_BUTTON_BOX_BUTTONS:
278         return 0;
279
280     case GLUT_HAS_SPACEBALL:
281         return fgHasSpaceball();
282
283     case GLUT_HAS_TABLET:
284         return 0;
285
286     case GLUT_NUM_SPACEBALL_BUTTONS:
287         return fgSpaceballNumButtons();
288
289     case GLUT_NUM_TABLET_BUTTONS:
290         return 0;
291
292     case GLUT_DEVICE_IGNORE_KEY_REPEAT:
293         return fgStructure.CurrentWindow ? fgStructure.CurrentWindow->State.IgnoreKeyRepeat : 0;
294
295     case GLUT_DEVICE_KEY_REPEAT:
296         return fgState.KeyRepeat;
297
298     default:
299                 return fgPlatformGlutDeviceGet ( eWhat );
300     }
301
302     /* And now -- the failure. */
303     return -1;
304 }
305
306 /*
307  * This should return the current state of ALT, SHIFT and CTRL keys.
308  */
309 int FGAPIENTRY glutGetModifiers( void )
310 {
311     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetModifiers" );
312     if( fgState.Modifiers == INVALID_MODIFIERS )
313     {
314         fgWarning( "glutGetModifiers() called outside an input callback" );
315         return 0;
316     }
317
318     return fgState.Modifiers;
319 }
320
321 /*
322  * Return the state of the GLUT API overlay subsystem. A misery ;-)
323  */
324 int FGAPIENTRY glutLayerGet( GLenum eWhat )
325 {
326     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLayerGet" );
327
328     /*
329      * This is easy as layers are not implemented and
330      * overlay support is not planned. E.g. on Windows,
331      * overlay requests in PFDs are ignored
332      * (see iLayerType at http://msdn.microsoft.com/en-us/library/dd368826(v=vs.85).aspx)
333      */
334     switch( eWhat )
335     {
336
337     case GLUT_OVERLAY_POSSIBLE:
338         return 0 ;
339
340     case GLUT_LAYER_IN_USE:
341         return GLUT_NORMAL;
342
343     case GLUT_HAS_OVERLAY:
344         return 0;
345
346     case GLUT_TRANSPARENT_INDEX:
347         /*
348         * Return just anything, which is always defined as zero
349         *
350         * XXX HUH?
351         */
352         return 0;
353
354     case GLUT_NORMAL_DAMAGED:
355         /* XXX Actually I do not know. Maybe. */
356         return 0;
357
358     case GLUT_OVERLAY_DAMAGED:
359         return -1;
360
361     default:
362         fgWarning( "glutLayerGet(): missing enum handle %d", eWhat );
363         break;
364     }
365
366     /* And fail. That's good. Programs do love failing. */
367     return -1;
368 }
369
370 int * FGAPIENTRY glutGetModeValues(GLenum eWhat, int *size)
371 {
372   int *array;
373
374   FREEGLUT_EXIT_IF_NOT_INITIALISED("glutGetModeValues");
375
376   *size = 0;
377   array = fgPlatformGlutGetModeValues ( eWhat, size );
378
379   return array;
380 }
381
382 /*** END OF FILE ***/