Update to reflect 2.2.0 release
[freeglut] / src / freeglut_main.c
1 /*
2  * freeglut_main.c
3  *
4  * The windows message processing 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: Fri Dec 3 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 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
34
35 #include <limits.h>
36 #if TARGET_HOST_UNIX_X11
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <unistd.h>
40 #include <errno.h>
41 #include <sys/stat.h>
42 #elif TARGET_HOST_WIN32
43 #endif
44
45 #ifndef MAX
46 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
47 #endif
48
49 #ifndef MIN
50 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
51 #endif
52
53
54 /*
55  * TODO BEFORE THE STABLE RELEASE:
56  *
57  * There are some issues concerning window redrawing under X11, and maybe
58  * some events are not handled. The Win32 version lacks some more features,
59  * but seems acceptable for not demanding purposes.
60  *
61  * Need to investigate why the X11 version breaks out with an error when
62  * closing a window (using the window manager, not glutDestroyWindow)...
63  */
64
65 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
66
67 /*
68  * Handle a window configuration change. When no reshape
69  * callback is hooked, the viewport size is updated to
70  * match the new window size.
71  */
72 static void fghReshapeWindowByHandle ( SFG_WindowHandleType handle,
73                                        int width, int height )
74 {
75     SFG_Window *current_window = fgStructure.Window;
76
77     SFG_Window* window = fgWindowByHandle( handle );
78     freeglut_return_if_fail( window != NULL );
79
80
81 #if TARGET_HOST_UNIX_X11
82
83     XResizeWindow( fgDisplay.Display, window->Window.Handle,
84                    width, height );
85     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
86
87 #elif TARGET_HOST_WIN32
88
89     {
90         RECT winRect;
91         int x, y;
92
93         GetWindowRect( window->Window.Handle, &winRect );
94         x = winRect.left;
95         y = winRect.top;
96
97         if ( window->Parent == NULL )
98         {
99             /*
100              * Adjust the size of the window to allow for the size of the
101              * frame, if we are not a menu
102              */
103             if ( ! window->IsMenu )
104             {
105                 width += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
106                 height += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
107                     GetSystemMetrics( SM_CYCAPTION );
108             }
109         }
110         else
111         {
112             GetWindowRect( window->Parent->Window.Handle,
113                            &winRect );
114             x -= winRect.left + GetSystemMetrics( SM_CXSIZEFRAME );
115             y -= winRect.top + GetSystemMetrics( SM_CYSIZEFRAME ) +
116                 GetSystemMetrics( SM_CYCAPTION );
117         }
118
119         MoveWindow(
120             window->Window.Handle,
121             x,
122             y,
123             width,
124             height,
125             TRUE
126         );
127     }
128
129     /*
130      * XXX Should update {window->State.OldWidth, window->State.OldHeight}
131      * XXX to keep in lockstep with UNIX_X11 code.
132      */
133     if( FETCH_WCB( *window, Reshape ) )
134         INVOKE_WCB( *window, Reshape, ( width, height ) );
135     else
136     {
137         fgSetWindow( window );
138         glViewport( 0, 0, width, height );
139     }
140
141 #endif
142
143     /*
144      * Force a window redraw.  In Windows at least this is only a partial
145      * solution:  if the window is increasing in size in either dimension,
146      * the already-drawn part does not get drawn again and things look funny.
147      * But without this we get this bad behaviour whenever we resize the
148      * window.
149      */
150     window->State.Redisplay = GL_TRUE;
151
152     if( window->IsMenu )
153         fgSetWindow( current_window );
154 }
155
156 /*
157  * Calls a window's redraw method. This is used when
158  * a redraw is forced by the incoming window messages.
159  */
160 static void fghRedrawWindowByHandle ( SFG_WindowHandleType handle )
161 {
162     SFG_Window* window = fgWindowByHandle( handle );
163     freeglut_return_if_fail( window );
164     freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
165
166     window->State.Redisplay = GL_FALSE;
167
168     freeglut_return_if_fail( window->State.Visible );
169
170     if( window->State.NeedToResize )
171     {
172         SFG_Window *current_window = fgStructure.Window;
173
174         fgSetWindow( window );
175
176         fghReshapeWindowByHandle( 
177             window->Window.Handle,
178             window->State.Width,
179             window->State.Height
180         );
181
182         window->State.NeedToResize = GL_FALSE;
183         fgSetWindow ( current_window );
184     }
185
186     INVOKE_WCB( *window, Display, ( ) );
187 }
188
189 /*
190  * A static helper function to execute display callback for a window
191  */
192 static void fghcbDisplayWindow( SFG_Window *window,
193                                 SFG_Enumerator *enumerator )
194 {
195     if( window->State.Redisplay &&
196         window->State.Visible )
197     {
198         /*
199          * XXX Resizing should *not* depend upon whether there
200          * XXX is a pending redisplay flag, as far as I can tell.
201          * XXX
202          * XXX Note, too, that the {NeedToResize} flag is a little
203          * XXX fuzzy in its meaning, since for WIN32, this also
204          * XXX means "we need to tell the application that the window has
205          * XXX changed size", while in X11, it only means "we need
206          * XXX to ask the window system to resize the window.
207          * XXX Splitting the flag's meaning might be desirable, but
208          * XXX that could complicate the code more.  (On X11, the
209          * XXX user callback is called as soon as the event is
210          * XXX discovered, but resizing the window is postponed
211          * XXX until after other events.)
212          */
213         if( window->State.NeedToResize )
214         {
215             SFG_Window *current_window = fgStructure.Window;
216
217             fgSetWindow( window );
218
219             fghReshapeWindowByHandle( 
220                 window->Window.Handle,
221                 window->State.Width,
222                 window->State.Height
223             );
224
225             window->State.NeedToResize = GL_FALSE;
226             fgSetWindow ( current_window );
227         }
228
229         window->State.Redisplay = GL_FALSE;
230
231 #if TARGET_HOST_UNIX_X11
232         {
233             SFG_Window *current_window = fgStructure.Window;
234
235             INVOKE_WCB( *window, Display, ( ) );
236             fgSetWindow( current_window );
237         }
238 #elif TARGET_HOST_WIN32
239         RedrawWindow(
240             window->Window.Handle, NULL, NULL, 
241             RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
242         );
243 #endif
244     }
245
246     fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
247 }
248
249 /*
250  * Make all windows perform a display call
251  */
252 static void fghDisplayAll( void )
253 {
254     SFG_Enumerator enumerator;
255
256     enumerator.found = GL_FALSE;
257     enumerator.data  =  NULL;
258
259     fgEnumWindows( fghcbDisplayWindow, &enumerator );
260 }
261
262 /*
263  * Window enumerator callback to check for the joystick polling code
264  */
265 static void fghcbCheckJoystickPolls( SFG_Window *window,
266                                      SFG_Enumerator *enumerator )
267 {
268     long int checkTime = fgElapsedTime( );
269
270     if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
271         checkTime )
272     {
273         fgJoystickPollWindow( window );
274         window->State.JoystickLastPoll = checkTime;
275     }
276
277     fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
278 }
279
280 /*
281  * Check all windows for joystick polling
282  */
283 static void fghCheckJoystickPolls( void )
284 {
285     SFG_Enumerator enumerator;
286
287     enumerator.found = GL_FALSE;
288     enumerator.data  =  NULL;
289
290     fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
291 }
292
293 /*
294  * Check the global timers
295  */
296 static void fghCheckTimers( void )
297 {
298     long checkTime = fgElapsedTime( );
299     SFG_Timer *timer;
300
301     while( timer = fgState.Timers.First )
302     {
303         if( timer->TriggerTime > checkTime )
304             break;
305
306         fgListRemove( &fgState.Timers, &timer->Node );
307         fgListAppend( &fgState.FreeTimers, &timer->Node );
308
309         timer->Callback( timer->ID );
310     }
311 }
312
313 /*
314  * Elapsed Time
315  */
316 long fgElapsedTime( void )
317 {
318     if ( fgState.Time.Set )
319     {
320 #if TARGET_HOST_UNIX_X11
321         struct timeval now;
322         long elapsed;
323
324         gettimeofday( &now, NULL );
325
326         elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
327         elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
328
329         return elapsed;
330 #elif TARGET_HOST_WIN32
331         return timeGetTime() - fgState.Time.Value;
332 #endif
333     }
334     else
335     {
336 #if TARGET_HOST_UNIX_X11
337         gettimeofday( &fgState.Time.Value, NULL );
338 #elif TARGET_HOST_WIN32
339         fgState.Time.Value = timeGetTime ();
340 #endif
341         fgState.Time.Set = GL_TRUE ;
342
343         return 0 ;
344     }
345 }
346
347 /*
348  * Error Messages.
349  */
350 void fgError( const char *fmt, ... )
351 {
352     va_list ap;
353
354     va_start( ap, fmt );
355
356     fprintf( stderr, "freeglut ");
357     if( fgState.ProgramName )
358         fprintf (stderr, "(%s): ", fgState.ProgramName);
359     vfprintf( stderr, fmt, ap );
360     fprintf( stderr, "\n" );
361
362     va_end( ap );
363
364     if ( fgState.Initialised )
365         fgDeinitialize ();
366
367     exit( 1 );
368 }
369
370 void fgWarning( const char *fmt, ... )
371 {
372     va_list ap;
373
374     va_start( ap, fmt );
375
376     fprintf( stderr, "freeglut ");
377     if( fgState.ProgramName )
378         fprintf( stderr, "(%s): ", fgState.ProgramName );
379     vfprintf( stderr, fmt, ap );
380     fprintf( stderr, "\n" );
381
382     va_end( ap );
383 }
384
385 /*
386  * Indicates whether Joystick events are being used by ANY window.
387  *
388  * The current mechanism is to walk all of the windows and ask if
389  * there is a joystick callback.  Certainly in some cases, maybe
390  * in all cases, the joystick is attached to the system and accessed
391  * from ONE point by GLUT/freeglut, so this is not the right way,
392  * in general, to do this.  However, the Joystick code is segregated
393  * in its own little world, so we can't access the information that
394  * we need in order to do that nicely.
395  *
396  * Some alternatives:
397  *  * Store Joystick data into freeglut global state.
398  *  * Provide NON-static functions or data from Joystick *.c file.
399  *
400  * Basically, the RIGHT way to do this requires knowing something
401  * about the Joystick.  Right now, the Joystick code is behind
402  * an opaque wall.
403  *
404  */
405 static void fgCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
406 {
407     if( FETCH_WCB( *w, Joystick ) )
408     {
409         e->found = GL_TRUE;
410         e->data = w;
411     }
412     fgEnumSubWindows( w, fgCheckJoystickCallback, e );
413 }
414 static int fgHaveJoystick( void )
415 {
416     SFG_Enumerator enumerator;
417     enumerator.found = GL_FALSE;
418     enumerator.data = NULL;
419     fgEnumWindows( fgCheckJoystickCallback, &enumerator );
420     return !!enumerator.data;
421 }
422 static void fgHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
423 {
424     if( w->State.Redisplay )
425     {
426         e->found = GL_TRUE;
427         e->data = w;
428     }
429     fgEnumSubWindows( w, fgHavePendingRedisplaysCallback, e );
430 }        
431 static int fgHavePendingRedisplays (void)
432 {
433     SFG_Enumerator enumerator;
434     enumerator.found = GL_FALSE;
435     enumerator.data = NULL;
436     fgEnumWindows( fgHavePendingRedisplaysCallback, &enumerator );
437     return !!enumerator.data;
438 }
439 /*
440  * Returns the number of GLUT ticks (milliseconds) till the next timer event.
441  */
442 static long fgNextTimer( void )
443 {
444     long ret = INT_MAX;
445     SFG_Timer *timer = fgState.Timers.First;
446
447     if( timer )
448         ret = timer->TriggerTime - fgElapsedTime();
449     if( ret < 0 )
450         ret = 0;
451
452     return ret;
453 }
454 /*
455  * Does the magic required to relinquish the CPU until something interesting
456  * happens.
457  */
458 static void fgSleepForEvents( void )
459 {
460 #if TARGET_HOST_UNIX_X11
461     fd_set fdset;
462     int err;
463     int socket;
464     struct timeval wait;
465     long msec;    
466     
467     if( fgState.IdleCallback || fgHavePendingRedisplays( ) )
468         return;
469     socket = ConnectionNumber( fgDisplay.Display );
470     FD_ZERO( &fdset );
471     FD_SET( socket, &fdset );
472     
473     msec = fgNextTimer( );
474     if( fgHaveJoystick( ) )
475         msec = MIN( msec, 10 );
476
477     wait.tv_sec = msec / 1000;
478     wait.tv_usec = (msec % 1000) * 1000;
479     err = select( socket+1, &fdset, NULL, NULL, &wait );
480
481     if( -1 == err )
482         fgWarning ( "freeglut select() error: %d\n", errno );
483     
484 #elif TARGET_HOST_WIN32
485 #endif
486 }
487
488 #if TARGET_HOST_UNIX_X11
489 /*
490  * Returns GLUT modifier mask for an XEvent.
491  */
492 int fgGetXModifiers( XEvent *event )
493 {
494     int ret = 0;
495
496     if( event->xkey.state & ( ShiftMask | LockMask ) )
497         ret |= GLUT_ACTIVE_SHIFT;
498     if( event->xkey.state & ControlMask )
499         ret |= GLUT_ACTIVE_CTRL;
500     if( event->xkey.state & Mod1Mask )
501         ret |= GLUT_ACTIVE_ALT;
502     
503     return ret;
504 }
505 #endif
506
507
508 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
509
510 /*
511  * Executes a single iteration in the freeglut processing loop.
512  */
513 void FGAPIENTRY glutMainLoopEvent( void )
514 {
515 #if TARGET_HOST_UNIX_X11
516     SFG_Window* window;
517     XEvent event;
518
519     /*
520      * This code was repeated constantly, so here it goes into a definition:
521      */
522 #define GETWINDOW(a)                             \
523     window = fgWindowByHandle( event.a.window ); \
524     if( window == NULL )                         \
525         break;
526
527 #define GETMOUSE(a)                              \
528     window->State.MouseX = event.a.x;            \
529     window->State.MouseY = event.a.y;
530
531     freeglut_assert_ready;
532
533     while( XPending( fgDisplay.Display ) )
534     {
535         XNextEvent( fgDisplay.Display, &event );
536
537         switch( event.type )
538         {
539         case ClientMessage:
540             /*
541              * Destroy the window when the WM_DELETE_WINDOW message arrives
542              */
543             if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
544             {
545                 GETWINDOW( xclient ); 
546
547                 fgDestroyWindow ( window );
548
549                 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
550                 {
551                     fgDeinitialize( );
552                     exit( 0 );
553                 }
554
555                 fgState.ExecState = GLUT_EXEC_STATE_STOP;
556                 return;
557             }
558             break;
559
560             /*
561              * CreateNotify causes a configure-event so that sub-windows are
562              * handled compatibly with GLUT.  Otherwise, your sub-windows
563              * (in freeglut only) will not get an initial reshape event,
564              * which can break things.
565              *
566              * GLUT presumably does this because it generally tries to treat
567              * sub-windows the same as windows.
568              *
569              * XXX Technically, GETWINDOW( xconfigure ) and
570              * XXX {event.xconfigure} may not be legit ways to get at
571              * XXX data for CreateNotify events.  In practice, the data
572              * XXX is in a union which is laid out much the same either
573              * XXX way.  But if you want to split hairs, this isn't legit,
574              * XXX and we should instead duplicate some code.
575              */
576         case CreateNotify:
577         case ConfigureNotify:
578             GETWINDOW( xconfigure );
579             {
580                 int width = event.xconfigure.width;
581                 int height = event.xconfigure.height;
582
583                 if( ( width != window->State.OldWidth ) ||
584                     ( height != window->State.OldHeight ) )
585                 {
586                     window->State.OldWidth = width;
587                     window->State.OldHeight = height;
588                     if( FETCH_WCB( *window, Reshape ) )
589                         INVOKE_WCB( *window, Reshape, ( width, height ) );
590                     else
591                     {
592                         fgSetWindow( window );
593                         glViewport( 0, 0, width, height );
594                     }
595                     glutPostRedisplay( );
596                 }
597             }
598             break;
599
600         case DestroyNotify:
601             /*
602              * This is sent to confirm the XDestroyWindow call.
603              * XXX WHY is this commented out?  Should we re-enable it?
604              */
605             /* fgAddToWindowDestroyList ( window ); */
606             break;
607
608         case Expose:
609             /*
610              * We are too dumb to process partial exposes...
611              *
612              * XXX Well, we could do it.  However, it seems to only
613              * XXX be potentially useful for single-buffered (since
614              * XXX double-buffered does not respect viewport when we
615              * XXX do a buffer-swap).
616              *
617              * XXX GETWINDOW( xexpose );
618              * XXX fgSetWindow( window );
619              * XXX glutPostRedisplay( );
620              */
621             if( event.xexpose.count == 0 )
622                 fghRedrawWindowByHandle( event.xexpose.window );
623             break;
624
625         case MapNotify:
626         case UnmapNotify:
627             /*
628              * If we never do anything with this, can we just not ask to
629              * get these messages?
630              */
631             break;
632
633         case MappingNotify:
634             /*
635              * Have the client's keyboard knowledge updated (xlib.ps,
636              * page 206, says that's a good thing to do)
637              */
638             XRefreshKeyboardMapping( (XMappingEvent *) &event );
639             break;
640
641         case VisibilityNotify:
642         {
643             GETWINDOW( xvisibility ); 
644             /*
645              * XXX INVOKE_WCB() does this check for us.
646              */
647             if( ! FETCH_WCB( *window, WindowStatus ) )
648                 break;
649             fgSetWindow( window );
650
651             /*
652              * Sending this event, the X server can notify us that the window
653              * has just acquired one of the three possible visibility states:
654              * VisibilityUnobscured, VisibilityPartiallyObscured or
655              * VisibilityFullyObscured
656              */
657             switch( event.xvisibility.state )
658             {
659             case VisibilityUnobscured:
660                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
661                 window->State.Visible = GL_TRUE;
662                 break;
663                 
664             case VisibilityPartiallyObscured:
665                 INVOKE_WCB( *window, WindowStatus,
666                             ( GLUT_PARTIALLY_RETAINED ) );
667                 window->State.Visible = GL_TRUE;
668                 break;
669                 
670             case VisibilityFullyObscured:
671                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
672                 window->State.Visible = GL_FALSE;
673                 break;
674
675             default:
676                 fgWarning( "Uknown X visibility state: %d",
677                            event.xvisibility.state );
678                 break;
679             }
680         }
681         break;
682
683         case EnterNotify:
684         case LeaveNotify:
685             GETWINDOW( xcrossing );
686             GETMOUSE( xcrossing );
687             INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
688                                           GLUT_ENTERED :
689                                           GLUT_LEFT ) );
690             break;
691
692         case MotionNotify:
693         {
694             GETWINDOW( xmotion );
695             GETMOUSE( xmotion );
696
697             if( window->ActiveMenu )
698             {
699                 if( window == window->ActiveMenu->ParentWindow )
700                 {
701                     window->ActiveMenu->Window->State.MouseX =
702                         event.xmotion.x_root - window->ActiveMenu->X;
703                     window->ActiveMenu->Window->State.MouseY =
704                         event.xmotion.y_root - window->ActiveMenu->Y;
705                 }
706                 window->ActiveMenu->Window->State.Redisplay = GL_TRUE ;
707                 fgSetWindow( window->ActiveMenu->ParentWindow );
708
709                 break;
710             }
711
712             /*
713              * XXX For more than 5 buttons, just check {event.xmotion.state},
714              * XXX rather than a host of bit-masks?  Or maybe we need to
715              * XXX track ButtonPress/ButtonRelease events in our own
716              * XXX bit-mask?
717              */
718 #define BUTTON_MASK \
719   ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
720             if ( event.xmotion.state & BUTTON_MASK )
721                 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
722                                                event.xmotion.y ) );
723             else
724                 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
725                                                 event.xmotion.y ) );
726         }
727         break;
728
729         case ButtonRelease:
730         case ButtonPress:
731         {
732             GLboolean pressed = GL_TRUE;
733             int button;
734
735             if( event.type == ButtonRelease )
736                 pressed = GL_FALSE ;
737
738             /*
739              * A mouse button has been pressed or released. Traditionally,
740              * break if the window was found within the freeglut structures.
741              */
742             GETWINDOW( xbutton );
743             GETMOUSE( xbutton );
744           
745             /*
746              * An X button (at least in XFree86) is numbered from 1.
747              * A GLUT button is numbered from 0.
748              * Old GLUT passed through buttons other than just the first
749              * three, though it only gave symbolic names and official
750              * support to the first three.
751              */
752             button = event.xbutton.button - 1;
753
754             /*
755              * XXX This comment is replicated in the WIN32 section and
756              * XXX maybe also in the menu code.  Can we move the info
757              * XXX to one central place and *reference* it from here?
758              *
759              * Do not execute the application's mouse callback if a menu
760              * is hooked to this button.  In that case an appropriate
761              * private call should be generated.
762              * Near as I can tell, this is the menu behaviour:
763              *  - Down-click the menu button, menu not active:  activate
764              *    the menu with its upper left-hand corner at the mouse
765              *    location.
766              *  - Down-click any button outside the menu, menu active:
767              *    deactivate the menu
768              *  - Down-click any button inside the menu, menu active:
769              *    select the menu entry and deactivate the menu
770              *  - Up-click the menu button, menu not active:  nothing happens
771              *  - Up-click the menu button outside the menu, menu active:
772              *    nothing happens
773              *  - Up-click the menu button inside the menu, menu active:
774              *    select the menu entry and deactivate the menu
775              */
776             /* Window has an active menu, it absorbs any mouse click */
777             if( window->ActiveMenu )
778             {
779                 if( window == window->ActiveMenu->ParentWindow )
780                 {
781                     window->ActiveMenu->Window->State.MouseX =
782                         event.xbutton.x_root - window->ActiveMenu->X;
783                     window->ActiveMenu->Window->State.MouseY =
784                         event.xbutton.y_root - window->ActiveMenu->Y;
785                 }
786                 
787                 /* In the menu, invoke the callback and deactivate the menu*/
788                 if( fgCheckActiveMenu( window->ActiveMenu->Window,
789                                        window->ActiveMenu ) )
790                 {
791                     /*
792                      * Save the current window and menu and set the current
793                      * window to the window whose menu this is
794                      */
795                     SFG_Window *save_window = fgStructure.Window;
796                     SFG_Menu *save_menu = fgStructure.Menu;
797                     SFG_Window *parent_window =
798                         window->ActiveMenu->ParentWindow;
799                     fgSetWindow( parent_window );
800                     fgStructure.Menu = window->ActiveMenu;
801
802                     /* Execute the menu callback */
803                     fgExecuteMenuCallback( window->ActiveMenu );
804                     fgDeactivateMenu( parent_window );
805
806                     /* Restore the current window and menu */
807                     fgSetWindow( save_window );
808                     fgStructure.Menu = save_menu;
809                 }
810                 else if( pressed )
811                     /*
812                      * Outside the menu, deactivate if it's a downclick
813                      *
814                      * XXX This isn't enough.  A downclick outside of
815                      * XXX the interior of our freeglut windows should also
816                      * XXX deactivate the menu.  This is more complicated.
817                      */
818                     fgDeactivateMenu( window->ActiveMenu->ParentWindow );
819
820                 /*
821                  * XXX Why does an active menu require a redisplay at
822                  * XXX this point?  If this can come out cleanly, then
823                  * XXX it probably should do so; if not, a comment should
824                  * XXX explain it.
825                  */
826                 window->State.Redisplay = GL_TRUE;
827                 break;
828             }
829
830             /*
831              * No active menu, let's check whether we need to activate one.
832              */
833             if( ( 0 <= button ) &&
834                 ( FREEGLUT_MAX_MENUS > button ) &&
835                 ( window->Menu[ button ] ) &&
836                 pressed )
837             {
838                 /*
839                  * XXX Posting a requisite Redisplay seems bogus.
840                  */
841                 window->State.Redisplay = GL_TRUE;
842                 fgSetWindow( window );
843                 fgActivateMenu( window, button );
844                 break;
845             }
846
847             /*
848              * Check if there is a mouse or mouse wheel callback hooked to the
849              * window
850              */
851             if( ! FETCH_WCB( *window, Mouse ) &&
852                 ! FETCH_WCB( *window, MouseWheel ) )
853                 break;
854
855             fgState.Modifiers = fgGetXModifiers( &event );
856
857             /*
858              * Finally execute the mouse or mouse wheel callback
859              *
860              * XXX Use a symbolic constant, *not* "4"!  ("3, sire!")
861              */
862             if( ( button < 3 ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
863                 INVOKE_WCB( *window, Mouse, ( button,
864                                               pressed ? GLUT_DOWN : GLUT_UP,
865                                               event.xbutton.x,
866                                               event.xbutton.y )
867                 );
868             else
869             {
870                 /*
871                  * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
872                  *  "  6 and 7 "    "   one; ...
873                  *
874                  * XXX This *should* be behind some variables/macros,
875                  * XXX since the order and numbering isn't certain
876                  * XXX See XFree86 configuration docs (even back in the
877                  * XXX 3.x days, and especially with 4.x).
878                  *
879                  * XXX Note that {button} has already been decremeted
880                  * XXX in mapping from X button numbering to GLUT.
881                  */
882                 int wheel_number = (button - 3) / 2;
883                 int direction = -1;
884                 if( button % 2 )
885                     direction = 1;
886                 
887                 if( pressed )
888                     INVOKE_WCB( *window, MouseWheel, ( wheel_number,
889                                                        direction,
890                                                        event.xbutton.x,
891                                                        event.xbutton.y )
892                     );
893             }
894
895             /*
896              * Trash the modifiers state
897              */
898             fgState.Modifiers = 0xffffffff;
899         }
900         break;
901
902         case KeyRelease:
903         case KeyPress:
904         {
905             FGCBKeyboard keyboard_cb;
906             FGCBSpecial special_cb;
907
908             GETWINDOW( xkey );
909             GETMOUSE( xkey );
910
911             if( event.type == KeyPress )
912             {
913                 keyboard_cb = FETCH_WCB( *window, Keyboard );
914                 special_cb  = FETCH_WCB( *window, Special  );
915             }
916             else
917             {
918                 keyboard_cb = FETCH_WCB( *window, KeyboardUp );
919                 special_cb  = FETCH_WCB( *window, SpecialUp  );
920             }
921
922             /*
923              * Is there a keyboard/special callback hooked for this window?
924              */
925             if( keyboard_cb || special_cb )
926             {
927                 XComposeStatus composeStatus;
928                 char asciiCode[ 32 ];
929                 KeySym keySym;
930                 int len;
931
932                 /*
933                  * Check for the ASCII/KeySym codes associated with the event:
934                  */
935                 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
936                                      &keySym, &composeStatus
937                 );
938
939                 /*
940                  * GLUT API tells us to have two separate callbacks...
941                  */
942                 if( len > 0 )
943                 {
944                     /*
945                      * ...one for the ASCII translateable keypresses...
946                      */
947                     if( keyboard_cb )
948                     {
949                         fgSetWindow( window );
950                         fgState.Modifiers = fgGetXModifiers( &event );
951                         keyboard_cb( asciiCode[ 0 ],
952                                      event.xkey.x, event.xkey.y
953                         );
954                         fgState.Modifiers = 0xffffffff;
955                     }
956                 }
957                 else
958                 {
959                     int special = -1;
960
961                     /*
962                      * ...and one for all the others, which need to be
963                      * translated to GLUT_KEY_Xs...
964                      */
965                     switch( keySym )
966                     {
967                     case XK_F1:     special = GLUT_KEY_F1;     break;
968                     case XK_F2:     special = GLUT_KEY_F2;     break;
969                     case XK_F3:     special = GLUT_KEY_F3;     break;
970                     case XK_F4:     special = GLUT_KEY_F4;     break;
971                     case XK_F5:     special = GLUT_KEY_F5;     break;
972                     case XK_F6:     special = GLUT_KEY_F6;     break;
973                     case XK_F7:     special = GLUT_KEY_F7;     break;
974                     case XK_F8:     special = GLUT_KEY_F8;     break;
975                     case XK_F9:     special = GLUT_KEY_F9;     break;
976                     case XK_F10:    special = GLUT_KEY_F10;    break;
977                     case XK_F11:    special = GLUT_KEY_F11;    break;
978                     case XK_F12:    special = GLUT_KEY_F12;    break;
979
980                     case XK_Left:   special = GLUT_KEY_LEFT;   break;
981                     case XK_Right:  special = GLUT_KEY_RIGHT;  break;
982                     case XK_Up:     special = GLUT_KEY_UP;     break;
983                     case XK_Down:   special = GLUT_KEY_DOWN;   break;
984
985                     case XK_KP_Prior:
986                     case XK_Prior:  special = GLUT_KEY_PAGE_UP; break;
987                     case XK_KP_Next:
988                     case XK_Next:   special = GLUT_KEY_PAGE_DOWN; break;
989                     case XK_KP_Home:
990                     case XK_Home:   special = GLUT_KEY_HOME;   break;
991                     case XK_KP_End:
992                     case XK_End:    special = GLUT_KEY_END;    break;
993                     case XK_KP_Insert:
994                     case XK_Insert: special = GLUT_KEY_INSERT; break;
995                     }
996
997                     /*
998                      * Execute the callback (if one has been specified),
999                      * given that the special code seems to be valid...
1000                      */
1001                     if( special_cb && (special != -1) )
1002                     {
1003                         fgSetWindow( window );
1004                         fgState.Modifiers = fgGetXModifiers( &event );
1005                         special_cb( special, event.xkey.x, event.xkey.y );
1006                         fgState.Modifiers = 0xffffffff;
1007                     }
1008                 }
1009             }
1010         }
1011         break;
1012
1013         case ReparentNotify:
1014             break; /* XXX Should disable this event */
1015
1016         default:
1017             fgWarning ("Unknown X event type: %d", event.type);
1018             break;
1019         }
1020     }
1021
1022 #elif TARGET_HOST_WIN32
1023
1024     MSG stMsg;
1025
1026     while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
1027     {
1028         if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
1029         {
1030             if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1031             {
1032                 fgDeinitialize( );
1033                 exit( 0 );
1034             }
1035             fgState.ExecState = GLUT_EXEC_STATE_STOP;
1036             return;
1037         }
1038
1039         TranslateMessage( &stMsg );
1040         DispatchMessage( &stMsg );
1041     }
1042 #endif
1043
1044     if( fgState.Timers.First )
1045         fghCheckTimers( );
1046     fghCheckJoystickPolls( );
1047     fghDisplayAll( );
1048
1049     fgCloseWindows( );
1050 }
1051
1052 /*
1053  * Enters the freeglut processing loop.
1054  * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1055  */
1056 void FGAPIENTRY glutMainLoop( void )
1057 {
1058 #if TARGET_HOST_WIN32
1059     SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1060 #endif
1061
1062     freeglut_assert_ready;
1063
1064 #if TARGET_HOST_WIN32
1065     /*
1066      * Processing before the main loop:  If there is a window which is open and
1067      * which has a visibility callback, call it.  I know this is an ugly hack,
1068      * but I'm not sure what else to do about it.  Ideally we should leave
1069      * something uninitialized in the create window code and initialize it in
1070      * the main loop, and have that initialization create a "WM_ACTIVATE"
1071      * message.  Then we would put the visibility callback code in the
1072      * "case WM_ACTIVATE" block below.         - John Fay -- 10/24/02
1073      */
1074     while( window )
1075     {
1076         if ( FETCH_WCB( *window, Visibility ) )
1077         {
1078             SFG_Window *current_window = fgStructure.Window ;
1079
1080             INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1081             fgSetWindow( current_window );
1082         }
1083         
1084         window = (SFG_Window *)window->Node.Next ;
1085     }
1086 #endif
1087
1088     fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1089     while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1090     {
1091         SFG_Window *window;
1092
1093         glutMainLoopEvent( );
1094         /*
1095          * Step through the list of windows, seeing if there are any
1096          * that are not menus
1097          */ 
1098         for( window = ( SFG_Window * )fgStructure.Windows.First;
1099              window;
1100              window = ( SFG_Window * )window->Node.Next )
1101             if ( ! ( window->IsMenu ) )
1102                 break;
1103         
1104         if( ! window )
1105             fgState.ExecState = GLUT_EXEC_STATE_STOP;
1106         else
1107         {
1108             if( fgState.IdleCallback )
1109                 fgState.IdleCallback( );
1110
1111             fgSleepForEvents( );
1112         }
1113     }
1114
1115     /*
1116      * When this loop terminates, destroy the display, state and structure
1117      * of a freeglut session, so that another glutInit() call can happen
1118      */
1119     fgDeinitialize( );
1120     if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1121         exit( 0 );
1122 }
1123
1124 /*
1125  * Leaves the freeglut processing loop.
1126  */
1127 void FGAPIENTRY glutLeaveMainLoop( void )
1128 {
1129     fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1130 }
1131
1132
1133 #if TARGET_HOST_WIN32
1134 /*
1135  * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1136  */
1137 int fgGetWin32Modifiers (void)
1138 {
1139     return
1140         ( ( ( GetKeyState( VK_LSHIFT   ) < 0 ) ||
1141             ( GetKeyState( VK_RSHIFT   ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1142         ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1143             ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL  : 0 ) |
1144         ( ( ( GetKeyState( VK_LMENU    ) < 0 ) ||
1145             ( GetKeyState( VK_RMENU    ) < 0 )) ? GLUT_ACTIVE_ALT   : 0 );
1146 }
1147
1148 /*
1149  * The window procedure for handling Win32 events
1150  */
1151 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1152                                LPARAM lParam )
1153 {
1154     SFG_Window* window = fgWindowByHandle( hWnd );
1155     PAINTSTRUCT ps;
1156     LONG lRet = 1;
1157
1158     if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1159       return DefWindowProc( hWnd, uMsg, wParam, lParam );
1160
1161     /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1162              uMsg, wParam, lParam ); */
1163     switch( uMsg )
1164     {
1165     case WM_CREATE:
1166         /*
1167          * The window structure is passed as the creation structure paramter...
1168          */
1169         window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1170         assert( window != NULL );
1171
1172         window->Window.Handle = hWnd;
1173         window->Window.Device = GetDC( hWnd );
1174         if( window->IsMenu )
1175         {
1176             unsigned int current_DisplayMode = fgState.DisplayMode;
1177             fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1178             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1179             fgState.DisplayMode = current_DisplayMode;
1180
1181             if( fgStructure.MenuContext )
1182                 wglMakeCurrent( window->Window.Device,
1183                                 fgStructure.MenuContext->Context
1184                 );
1185             else
1186             {
1187                 fgStructure.MenuContext =
1188                     (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1189                 fgStructure.MenuContext->Context =
1190                     wglCreateContext( window->Window.Device );
1191             }
1192
1193             /* window->Window.Context = wglGetCurrentContext ();   */
1194             window->Window.Context = wglCreateContext( window->Window.Device );
1195         }
1196         else
1197         {
1198             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1199
1200             if( ! fgState.UseCurrentContext )
1201                 window->Window.Context =
1202                     wglCreateContext( window->Window.Device );
1203             else
1204             {
1205                 window->Window.Context = wglGetCurrentContext( );
1206                 if( ! window->Window.Context )
1207                     window->Window.Context =
1208                         wglCreateContext( window->Window.Device );
1209             }
1210         }
1211
1212         window->State.NeedToResize = GL_TRUE;
1213         window->State.Width  = fgState.Size.X;
1214         window->State.Height = fgState.Size.Y;
1215
1216         ReleaseDC( window->Window.Handle, window->Window.Device );
1217         break;
1218
1219     case WM_SIZE:
1220         /*
1221          * We got resized... But check if the window has been already added...
1222          */
1223         window->State.NeedToResize = GL_TRUE;
1224         window->State.Width  = LOWORD(lParam);
1225         window->State.Height = HIWORD(lParam);
1226         break;
1227 #if 0
1228     case WM_SETFOCUS: 
1229         printf("WM_SETFOCUS: %p\n", window );
1230         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1231         break;
1232
1233     case WM_ACTIVATE: 
1234         if (LOWORD(wParam) != WA_INACTIVE)
1235         {
1236             /* glutSetCursor( fgStructure.Window->State.Cursor ); */
1237             printf("WM_ACTIVATE: glutSetCursor( %p, %d)\n", window,
1238                    window->State.Cursor );
1239             glutSetCursor( window->State.Cursor );
1240         }
1241
1242         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1243         break;
1244 #endif
1245
1246         /*
1247          * XXX Why not re-use some common code with the glutSetCursor()
1248          * XXX function (or perhaps invoke glutSetCursor())?
1249          * XXX That is, why are we duplicating code, here, from
1250          * XXX glutSetCursor()?  The WIN32 code should be able to just
1251          * XXX call glutSetCurdsor() instead of defining two macros
1252          * XXX and implementing a nested case in-line.
1253          */
1254     case WM_SETCURSOR: 
1255         /* Set the cursor AND change it for this window class. */
1256 #define MAP_CURSOR(a,b)                 \
1257     case a:                             \
1258     SetCursor( LoadCursor( NULL, b ) ); \
1259     break;
1260
1261         /* Nuke the cursor AND change it for this window class. */
1262 #define ZAP_CURSOR(a,b) \
1263     case a:             \
1264     SetCursor( NULL );  \
1265     break;
1266
1267         if( LOWORD( lParam ) == HTCLIENT )
1268             switch( window->State.Cursor )
1269             {
1270                 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW     );
1271                 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  IDC_ARROW     );
1272                 MAP_CURSOR( GLUT_CURSOR_INFO,        IDC_HELP      );
1273                 MAP_CURSOR( GLUT_CURSOR_DESTROY,     IDC_CROSS     );
1274                 MAP_CURSOR( GLUT_CURSOR_HELP,        IDC_HELP      );
1275                 MAP_CURSOR( GLUT_CURSOR_CYCLE,       IDC_SIZEALL   );
1276                 MAP_CURSOR( GLUT_CURSOR_SPRAY,       IDC_CROSS     );
1277                 MAP_CURSOR( GLUT_CURSOR_WAIT,        IDC_WAIT      );
1278                 MAP_CURSOR( GLUT_CURSOR_TEXT,        IDC_UPARROW   );
1279                 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   IDC_CROSS     );
1280                 /* MAP_CURSOR( GLUT_CURSOR_NONE,        IDC_NO         ); */
1281                 ZAP_CURSOR( GLUT_CURSOR_NONE,        NULL          );
1282
1283             default:
1284                 MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     IDC_ARROW     );
1285             }
1286         else
1287             lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1288         break;
1289
1290     case WM_SHOWWINDOW:
1291         window->State.Visible = GL_TRUE;
1292         window->State.Redisplay = GL_TRUE;
1293         break;
1294
1295     case WM_PAINT:
1296         /* Turn on the visibility in case it was turned off somehow */
1297         window->State.Visible = GL_TRUE;
1298         BeginPaint( hWnd, &ps );
1299         fghRedrawWindowByHandle( hWnd );
1300         EndPaint( hWnd, &ps );
1301         break;
1302
1303     case WM_CLOSE:
1304         fgDestroyWindow ( window );
1305         if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
1306             PostQuitMessage(0);
1307         break;
1308
1309     case WM_DESTROY:
1310         /*
1311          * The window already got destroyed, so don't bother with it.
1312          */
1313         return 0;
1314
1315     case WM_MOUSEMOVE:
1316     {
1317         window->State.MouseX = LOWORD( lParam );
1318         window->State.MouseY = HIWORD( lParam );
1319         
1320         if ( window->ActiveMenu )
1321         {
1322             window->State.Redisplay = GL_TRUE;
1323             fgSetWindow ( window->ActiveMenu->ParentWindow );
1324             break;
1325         }
1326
1327         fgState.Modifiers = fgGetWin32Modifiers( );
1328
1329         if( ( wParam & MK_LBUTTON ) ||
1330             ( wParam & MK_MBUTTON ) ||
1331             ( wParam & MK_RBUTTON ) )
1332             INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1333                                            window->State.MouseY ) );
1334         else
1335             INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1336                                             window->State.MouseY ) );
1337
1338         fgState.Modifiers = 0xffffffff;
1339     }
1340     break;
1341
1342     case WM_LBUTTONDOWN:
1343     case WM_MBUTTONDOWN:
1344     case WM_RBUTTONDOWN:
1345     case WM_LBUTTONUP:
1346     case WM_MBUTTONUP:
1347     case WM_RBUTTONUP:
1348     {
1349         GLboolean pressed = GL_TRUE;
1350         int button;
1351
1352         window->State.MouseX = LOWORD( lParam );
1353         window->State.MouseY = HIWORD( lParam );
1354
1355         switch( uMsg )
1356         {
1357         case WM_LBUTTONDOWN:
1358             pressed = GL_TRUE;
1359             button = GLUT_LEFT_BUTTON;
1360             break;
1361         case WM_MBUTTONDOWN:
1362             pressed = GL_TRUE;
1363             button = GLUT_MIDDLE_BUTTON;
1364             break;
1365         case WM_RBUTTONDOWN:
1366             pressed = GL_TRUE;
1367             button = GLUT_RIGHT_BUTTON;
1368             break;
1369         case WM_LBUTTONUP:
1370             pressed = GL_FALSE;
1371             button = GLUT_LEFT_BUTTON;
1372             break;
1373         case WM_MBUTTONUP:
1374             pressed = GL_FALSE;
1375             button = GLUT_MIDDLE_BUTTON;
1376             break;
1377         case WM_RBUTTONUP:
1378             pressed = GL_FALSE;
1379             button = GLUT_RIGHT_BUTTON;
1380             break;
1381         default:
1382             pressed = GL_FALSE;
1383             button = -1;
1384             break;
1385         }
1386
1387         if( GetSystemMetrics( SM_SWAPBUTTON ) )
1388             if( button == GLUT_LEFT_BUTTON )
1389                 button = GLUT_RIGHT_BUTTON;
1390             else if( button == GLUT_RIGHT_BUTTON )
1391                 button = GLUT_LEFT_BUTTON;
1392
1393         if( button == -1 )
1394             return DefWindowProc( hWnd, uMsg, lParam, wParam );
1395
1396         /*
1397          * XXX This comment is duplicated in two other spots.
1398          * XXX Can we centralize it?
1399          *
1400          * Do not execute the application's mouse callback if a
1401          * menu is hooked to this button.
1402          * In that case an appropriate private call should be generated.
1403          * Near as I can tell, this is the menu behaviour:
1404          *  - Down-click the menu button, menu not active:  activate
1405          *    the menu with its upper left-hand corner at the mouse location.
1406          *  - Down-click any button outside the menu, menu active:
1407          *    deactivate the menu
1408          *  - Down-click any button inside the menu, menu active:
1409          *    select the menu entry and deactivate the menu
1410          *  - Up-click the menu button, menu not active:  nothing happens
1411          *  - Up-click the menu button outside the menu, menu active:
1412          *    nothing happens
1413          *  - Up-click the menu button inside the menu, menu active:
1414          *    select the menu entry and deactivate the menu
1415          */
1416         /* Window has an active menu, it absorbs any mouse click */
1417         if( window->ActiveMenu )
1418         {
1419             /* Outside the menu, deactivate the menu if it's a downclick */
1420             if( ! fgCheckActiveMenu( window, window->ActiveMenu ) )
1421             {
1422                 if( pressed )
1423                     fgDeactivateMenu( window->ActiveMenu->ParentWindow );
1424             }
1425             else  /* In menu, invoke the callback and deactivate the menu*/
1426             {
1427                 /*
1428                  * Save the current window and menu and set the current
1429                  * window to the window whose menu this is
1430                  */
1431                 SFG_Window *save_window = fgStructure.Window;
1432                 SFG_Menu *save_menu = fgStructure.Menu;
1433                 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
1434                 fgSetWindow( parent_window );
1435                 fgStructure.Menu = window->ActiveMenu;
1436
1437                 /* Execute the menu callback */
1438                 fgExecuteMenuCallback( window->ActiveMenu );
1439                 fgDeactivateMenu( parent_window );
1440
1441                 /* Restore the current window and menu */
1442                 fgSetWindow( save_window );
1443                 fgStructure.Menu = save_menu;
1444             }
1445
1446             /*
1447              * Let's make the window redraw as a result of the mouse
1448              * click and menu activity.
1449              */
1450             if( ! window->IsMenu )
1451                 window->State.Redisplay = GL_TRUE;
1452
1453             break;
1454         }
1455
1456         if( window->Menu[ button ] && pressed )
1457         {
1458             window->State.Redisplay = GL_TRUE;
1459             fgSetWindow( window );
1460             fgActivateMenu( window, button );
1461
1462             break;
1463         }
1464
1465         if( ! FETCH_WCB( *window, Mouse ) )
1466             break;
1467
1468         fgSetWindow( window );
1469         fgState.Modifiers = fgGetWin32Modifiers( );
1470
1471         INVOKE_WCB(
1472             *window, Mouse,
1473             ( button,
1474               pressed ? GLUT_DOWN : GLUT_UP,
1475               window->State.MouseX,
1476               window->State.MouseY
1477             )
1478         );
1479
1480         fgState.Modifiers = 0xffffffff;
1481     }
1482     break;
1483
1484     case 0x020a:
1485         /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1486     {
1487         /*
1488          * XXX THIS IS SPECULATIVE -- John Fay, 10/2/03
1489          * XXX Should use WHEEL_DELTA instead of 120
1490          */
1491         int wheel_number = LOWORD( wParam );
1492         short ticks = ( short )HIWORD( wParam ) / 120;
1493         int direction = 1;
1494
1495         if( ticks < 0 )
1496         {
1497             direction = -1;
1498             ticks = -ticks;
1499         }
1500
1501         /*
1502          * The mouse cursor has moved. Remember the new mouse cursor's position
1503          */
1504         /*        window->State.MouseX = LOWORD( lParam ); */
1505         /* Need to adjust by window position, */
1506         /*        window->State.MouseY = HIWORD( lParam ); */
1507         /* change "lParam" to other parameter */
1508
1509         if( ! FETCH_WCB( *window, MouseWheel ) &&
1510             ! FETCH_WCB( *window, Mouse ) )
1511             break;
1512
1513         fgSetWindow( window );
1514         fgState.Modifiers = fgGetWin32Modifiers( );
1515
1516         while( ticks-- )
1517             if( FETCH_WCB( *window, MouseWheel ) )
1518                 INVOKE_WCB( *window, MouseWheel,
1519                             ( wheel_number,
1520                               direction,
1521                               window->State.MouseX,
1522                               window->State.MouseY
1523                             )
1524                 );
1525             else  /* No mouse wheel, call the mouse button callback twice */
1526             {
1527                 /*
1528                  * XXX The below assumes that you have no more than 3 mouse
1529                  * XXX buttons.  Sorry.
1530                  */
1531                 int button = wheel_number*2 + 4;
1532                 if( direction > 0 )
1533                     ++button;
1534                 INVOKE_WCB( *window, Mouse,
1535                             ( button, GLUT_DOWN,
1536                               window->State.MouseX, window->State.MouseY )
1537                 );
1538                 INVOKE_WCB( *window, Mouse,
1539                             ( button, GLUT_UP,
1540                               window->State.MouseX, window->State.MouseX )
1541                 );
1542             }
1543
1544         fgState.Modifiers = 0xffffffff;
1545     }
1546     break ;
1547
1548     case WM_SYSKEYDOWN:
1549     case WM_KEYDOWN:
1550     {
1551         int keypress = -1;
1552         POINT mouse_pos ;
1553
1554         if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1555             break;
1556
1557         /*
1558          * Remember the current modifiers state. This is done here in order 
1559          * to make sure the VK_DELETE keyboard callback is executed properly.
1560          */
1561         fgState.Modifiers = fgGetWin32Modifiers( );
1562
1563         GetCursorPos( &mouse_pos );
1564         ScreenToClient( window->Window.Handle, &mouse_pos );
1565
1566         window->State.MouseX = mouse_pos.x;
1567         window->State.MouseY = mouse_pos.y;
1568
1569         /*
1570          * Convert the Win32 keystroke codes to GLUTtish way
1571          */
1572 #       define KEY(a,b) case a: keypress = b; break;
1573
1574         switch( wParam )
1575         {
1576             KEY( VK_F1,     GLUT_KEY_F1        );
1577             KEY( VK_F2,     GLUT_KEY_F2        );
1578             KEY( VK_F3,     GLUT_KEY_F3        );
1579             KEY( VK_F4,     GLUT_KEY_F4        );
1580             KEY( VK_F5,     GLUT_KEY_F5        );
1581             KEY( VK_F6,     GLUT_KEY_F6        );
1582             KEY( VK_F7,     GLUT_KEY_F7        );
1583             KEY( VK_F8,     GLUT_KEY_F8        );
1584             KEY( VK_F9,     GLUT_KEY_F9        );
1585             KEY( VK_F10,    GLUT_KEY_F10       );
1586             KEY( VK_F11,    GLUT_KEY_F11       );
1587             KEY( VK_F12,    GLUT_KEY_F12       );
1588             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
1589             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
1590             KEY( VK_HOME,   GLUT_KEY_HOME      );
1591             KEY( VK_END,    GLUT_KEY_END       );
1592             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
1593             KEY( VK_UP,     GLUT_KEY_UP        );
1594             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
1595             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
1596             KEY( VK_INSERT, GLUT_KEY_INSERT    );
1597
1598         case VK_DELETE:
1599             /*
1600              * The delete key should be treated as an ASCII keypress:
1601              */
1602             INVOKE_WCB( *window, Keyboard,
1603                         ( 127, window->State.MouseX, window->State.MouseY )
1604             );
1605         }
1606
1607         if( keypress != -1 )
1608             INVOKE_WCB( *window, Special,
1609                         ( keypress,
1610                           window->State.MouseX, window->State.MouseY )
1611             );
1612
1613         fgState.Modifiers = 0xffffffff;
1614     }
1615     break;
1616
1617     case WM_SYSKEYUP:
1618     case WM_KEYUP:
1619     {
1620         int keypress = -1;
1621         POINT mouse_pos;
1622
1623         /*
1624          * Remember the current modifiers state. This is done here in order 
1625          * to make sure the VK_DELETE keyboard callback is executed properly.
1626          */
1627         fgState.Modifiers = fgGetWin32Modifiers( );
1628
1629         GetCursorPos( &mouse_pos );
1630         ScreenToClient( window->Window.Handle, &mouse_pos );
1631
1632         window->State.MouseX = mouse_pos.x;
1633         window->State.MouseY = mouse_pos.y;
1634
1635         /*
1636          * Convert the Win32 keystroke codes to GLUTtish way.
1637          * "KEY(a,b)" was defined under "WM_KEYDOWN"
1638          */
1639
1640         switch( wParam )
1641         {
1642             KEY( VK_F1,     GLUT_KEY_F1        );
1643             KEY( VK_F2,     GLUT_KEY_F2        );
1644             KEY( VK_F3,     GLUT_KEY_F3        );
1645             KEY( VK_F4,     GLUT_KEY_F4        );
1646             KEY( VK_F5,     GLUT_KEY_F5        );
1647             KEY( VK_F6,     GLUT_KEY_F6        );
1648             KEY( VK_F7,     GLUT_KEY_F7        );
1649             KEY( VK_F8,     GLUT_KEY_F8        );
1650             KEY( VK_F9,     GLUT_KEY_F9        );
1651             KEY( VK_F10,    GLUT_KEY_F10       );
1652             KEY( VK_F11,    GLUT_KEY_F11       );
1653             KEY( VK_F12,    GLUT_KEY_F12       );
1654             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
1655             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
1656             KEY( VK_HOME,   GLUT_KEY_HOME      );
1657             KEY( VK_END,    GLUT_KEY_END       );
1658             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
1659             KEY( VK_UP,     GLUT_KEY_UP        );
1660             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
1661             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
1662             KEY( VK_INSERT, GLUT_KEY_INSERT    );
1663
1664           case VK_DELETE:
1665               /*
1666                * The delete key should be treated as an ASCII keypress:
1667                */
1668               INVOKE_WCB( *window, KeyboardUp,
1669                           ( 127, window->State.MouseX, window->State.MouseY )
1670               );
1671               break;
1672
1673         default:
1674         {
1675             BYTE state[ 256 ];
1676             WORD code[ 2 ];
1677             
1678             GetKeyboardState( state );
1679             
1680             if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
1681                 wParam=code[ 0 ];
1682
1683             INVOKE_WCB( *window, KeyboardUp,
1684                         ( (char)wParam,
1685                           window->State.MouseX, window->State.MouseY )
1686             );
1687         }
1688         }
1689
1690         if( keypress != -1 )
1691             INVOKE_WCB( *window, SpecialUp,
1692                         ( keypress,
1693                           window->State.MouseX, window->State.MouseY )
1694             );
1695
1696         fgState.Modifiers = 0xffffffff;
1697     }
1698     break;
1699
1700     case WM_SYSCHAR:
1701     case WM_CHAR:
1702     {
1703         if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1704             break;
1705
1706         fgState.Modifiers = fgGetWin32Modifiers( );
1707         INVOKE_WCB( *window, Keyboard,
1708                     ( (char)wParam,
1709                       window->State.MouseX, window->State.MouseY )
1710         );
1711         fgState.Modifiers = 0xffffffff;
1712     }
1713     break;
1714
1715     case WM_CAPTURECHANGED:
1716         /* User has finished resizing the window, force a redraw */
1717         INVOKE_WCB( *window, Display, ( ) );
1718
1719         /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
1720         break;
1721
1722         /*
1723          * Other messages that I have seen and which are not handled already
1724          */
1725     case WM_SETTEXT:  /* 0x000c */
1726         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1727         /* Pass it on to "DefWindowProc" to set the window text */
1728         break;
1729
1730     case WM_GETTEXT:  /* 0x000d */
1731         /* Ideally we would copy the title of the window into "lParam" */
1732         /* strncpy ( (char *)lParam, "Window Title", wParam );
1733            lRet = ( wParam > 12 ) ? 12 : wParam;  */
1734         /* the number of characters copied */
1735         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1736         break;
1737
1738     case WM_GETTEXTLENGTH:  /* 0x000e */
1739         /* Ideally we would get the length of the title of the window */
1740         lRet = 12;
1741         /* the number of characters in "Window Title\0" (see above) */
1742         break;
1743
1744     case WM_ERASEBKGND:  /* 0x0014 */
1745         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1746         break;
1747
1748     case WM_SYNCPAINT:  /* 0x0088 */
1749         /* Another window has moved, need to update this one */
1750         window->State.Redisplay = GL_TRUE;
1751         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1752         /* Help screen says this message must be passed to "DefWindowProc" */
1753         break;
1754
1755     case WM_NCPAINT:  /* 0x0085 */
1756       /* Need to update the border of this window */
1757         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1758         /* Pass it on to "DefWindowProc" to repaint a standard border */
1759         break;
1760
1761     case WM_SYSCOMMAND :  /* 0x0112 */
1762         {
1763           /*
1764            * We have received a system command message.  Try to act on it.
1765            * The commands are passed in through the "lParam" parameter:
1766            * Clicking on a corner to resize the window gives a "F004" message
1767            * but this is not defined in my header file.
1768            */
1769             switch ( lParam )
1770             {
1771             case SC_SIZE       :
1772                 break ;
1773
1774             case SC_MOVE       :
1775                 break ;
1776
1777             case SC_MINIMIZE   :
1778                 /* User has clicked on the "-" to minimize the window */
1779                 /* Turn off the visibility */
1780                 window->State.Visible = GL_FALSE ;
1781
1782                 break ;
1783
1784             case SC_MAXIMIZE   :
1785                 break ;
1786
1787             case SC_NEXTWINDOW :
1788                 break ;
1789
1790             case SC_PREVWINDOW :
1791                 break ;
1792
1793             case SC_CLOSE      :
1794                 /* Followed very closely by a WM_CLOSE message */
1795                 break ;
1796
1797             case SC_VSCROLL    :
1798                 break ;
1799
1800             case SC_HSCROLL    :
1801                 break ;
1802
1803             case SC_MOUSEMENU  :
1804                 break ;
1805
1806             case SC_KEYMENU    :
1807                 break ;
1808
1809             case SC_ARRANGE    :
1810                 break ;
1811
1812             case SC_RESTORE    :
1813                 break ;
1814
1815             case SC_TASKLIST   :
1816                 break ;
1817
1818             case SC_SCREENSAVE :
1819                 break ;
1820
1821             case SC_HOTKEY     :
1822                 break ;
1823             }
1824         }
1825
1826         /* We need to pass the message on to the operating system as well */
1827         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1828         break;
1829
1830     default:
1831         /*
1832          * Handle unhandled messages
1833          */
1834         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1835         break;
1836     }
1837
1838     return lRet;
1839 }
1840 #endif
1841
1842 /*** END OF FILE ***/