Changing "GameMode" to "GameModeWindow ... and testing whether I can get to SVN directly
[freeglut] / src / freeglut_menu.c
1 /*
2  * freeglut_menu.c
3  *
4  * Pull-down menu creation and handling.
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 "freeglut_internal.h"
30
31 /* -- DEFINITIONS ---------------------------------------------------------- */
32
33 /*
34  * FREEGLUT_MENU_FONT can be any freeglut bitmapped font.
35  * (Stroked fonts would not be out of the question, but we'd need to alter
36  *  code, since GLUT (hence freeglut) does not quite unify stroked and
37  *  bitmapped font handling.)
38  * Old UNIX/X11 GLUT (BSD, UNIX, IRIX, LINUX, HPUX, ...) used a system
39  * font best approximated by an 18-pixel HELVETICA, I think.  MS-WINDOWS
40  * GLUT used something closest to the 8x13 fixed-width font.  (Old
41  * GLUT apparently uses host-system menus rather than building its own.
42  * freeglut is building its own menus from scratch.)
43  *
44  * FREEGLUT_MENU_HEIGHT gives the height of ONE menu box.  This should be
45  * the distances between two adjacent menu entries.  It should scale
46  * automatically with the font choice, so you needn't alter it---unless you
47  * use a stroked font.
48  *
49  * FREEGLUT_MENU_BORDER says how many pixels to allow around the edge of a
50  * menu.  (It also seems to be the same as the number of pixels used as
51  * a border around *items* to separate them from neighbors.  John says
52  * that that wasn't the original intent...if not, perhaps we need another
53  * symbolic constant, FREEGLUT_MENU_ITEM_BORDER, or such.)
54  */
55 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
56 #define  FREEGLUT_MENU_FONT    GLUT_BITMAP_8_BY_13
57 #else
58 #define  FREEGLUT_MENU_FONT    GLUT_BITMAP_HELVETICA_18
59 #endif
60
61 #define  FREEGLUT_MENU_HEIGHT  (glutBitmapHeight(FREEGLUT_MENU_FONT) + \
62                                 FREEGLUT_MENU_BORDER)
63 #define  FREEGLUT_MENU_BORDER   2
64
65
66 /*
67  * These variables are for rendering the freeglut menu items.
68  *
69  * The choices are fore- and background, with and without h for Highlighting.
70  * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
71  * too.  These variables should be stuffed into global state and initialized
72  * via the glutInit*() system.
73  */
74 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
75 static float menu_pen_fore  [4] = {0.0f,  0.0f,  0.0f,  1.0f};
76 static float menu_pen_back  [4] = {0.85f, 0.85f, 0.85f, 1.0f};
77 static float menu_pen_hfore [4] = {1.0f,  1.0f,  1.0f,  1.0f};
78 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
79 #else
80 static float menu_pen_fore  [4] = {0.0f,  0.0f,  0.0f,  1.0f};
81 static float menu_pen_back  [4] = {0.70f, 0.70f, 0.70f, 1.0f};
82 static float menu_pen_hfore [4] = {0.0f,  0.0f,  0.0f,  1.0f};
83 static float menu_pen_hback [4] = {1.0f,  1.0f,  1.0f,  1.0f};
84 #endif
85
86 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
87
88 /*
89  * Private function to find a menu entry by index
90  */
91 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
92 {
93     SFG_MenuEntry *entry;
94     int i = 1;
95
96     for( entry = (SFG_MenuEntry *)menu->Entries.First;
97          entry;
98          entry = (SFG_MenuEntry *)entry->Node.Next )
99     {
100         if( i == index )
101             break;
102         ++i;
103     }
104
105     return entry;
106 }
107
108 /*
109  * Deactivates a menu pointed by the function argument.
110  */
111 static void fghDeactivateSubMenu( SFG_MenuEntry *menuEntry )
112 {
113     SFG_MenuEntry *subMenuIter;
114     /* Hide the present menu's window */
115     fgSetWindow( menuEntry->SubMenu->Window );
116     glutHideWindow( );
117
118     /* Forget about having that menu active anymore, now: */
119     menuEntry->SubMenu->Window->ActiveMenu = NULL;
120     menuEntry->SubMenu->IsActive = GL_FALSE;
121     menuEntry->SubMenu->ActiveEntry = NULL;
122
123     /* Hide all submenu windows, and the root menu's window. */
124     for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
125           subMenuIter;
126           subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
127     {
128         subMenuIter->IsActive = GL_FALSE;
129
130         /* Is that an active submenu by any case? */
131         if( subMenuIter->SubMenu )
132             fghDeactivateSubMenu( subMenuIter );
133     }
134
135     fgSetWindow ( menuEntry->SubMenu->ParentWindow ) ;
136 }
137
138 /*
139  * Private function to get the virtual maximum screen extent
140  */
141 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
142 {
143     if( fgStructure.GameModeWindow )
144     {
145 #if TARGET_HOST_UNIX_X11
146         int wx, wy;
147         Window w;
148
149         XTranslateCoordinates(
150             fgDisplay.Display,
151             window->Window.Handle,
152             fgDisplay.RootWindow,
153             0, 0, &wx, &wy, &w);
154
155         *x = fgState.GameModeSize.X + wx;
156         *y = fgState.GameModeSize.Y + wy;
157 #else
158         *x = glutGet ( GLUT_SCREEN_WIDTH );
159         *y = glutGet ( GLUT_SCREEN_HEIGHT );
160 #endif
161     }
162     else
163     {
164         *x = fgDisplay.ScreenWidth;
165         *y = fgDisplay.ScreenHeight;
166     }
167 }
168
169 /*
170  * Private function to check for the current menu/sub menu activity state
171  */
172 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
173 {
174     SFG_MenuEntry* menuEntry;
175     int x, y;
176
177     /* First of all check any of the active sub menus... */
178     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
179          menuEntry;
180          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
181     {
182         if( menuEntry->SubMenu && menuEntry->IsActive )
183         {
184             /*
185              * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
186              * will mean that it caught the mouse cursor and we do not need
187              * to regenerate the activity list, and so our parents do...
188              */
189             GLboolean return_status;
190
191             menuEntry->SubMenu->Window->State.MouseX =
192                 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
193             menuEntry->SubMenu->Window->State.MouseY =
194                 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
195             return_status = fghCheckMenuStatus( menuEntry->SubMenu );
196
197             if ( return_status )
198                 return GL_TRUE;
199         }
200     }
201
202     /* That much about our sub menus, let's get to checking the current menu: */
203     x = menu->Window->State.MouseX;
204     y = menu->Window->State.MouseY;
205
206     /* Check if the mouse cursor is contained within the current menu box */
207     if( ( x >= FREEGLUT_MENU_BORDER ) &&
208         ( x < menu->Width  - FREEGLUT_MENU_BORDER ) &&
209         ( y >= FREEGLUT_MENU_BORDER ) &&
210         ( y < menu->Height - FREEGLUT_MENU_BORDER )  )
211     {
212         int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
213
214         /* The mouse cursor is somewhere over our box, check it out. */
215         menuEntry = fghFindMenuEntry( menu, menuID + 1 );
216         FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
217                                       "fghCheckMenuStatus" );
218
219         menuEntry->IsActive = GL_TRUE;
220         menuEntry->Ordinal = menuID;
221
222         /*
223          * If this is not the same as the last active menu entry, deactivate
224          * the previous entry.  Specifically, if the previous active entry
225          * was a submenu then deactivate it.
226          */
227         if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
228             if( menu->ActiveEntry->SubMenu )
229                 fghDeactivateSubMenu( menu->ActiveEntry );
230
231         if( menuEntry != menu->ActiveEntry )
232         {
233             menu->Window->State.Redisplay = GL_TRUE;
234             if( menu->ActiveEntry )
235                 menu->ActiveEntry->IsActive = GL_FALSE;
236         }
237
238         menu->ActiveEntry = menuEntry;
239         menu->IsActive = GL_TRUE;  /* XXX Do we need this? */
240
241         /*
242          * OKi, we have marked that entry as active, but it would be also
243          * nice to have its contents updated, in case it's a sub menu.
244          * Also, ignore the return value of the check function:
245          */
246         if( menuEntry->SubMenu )
247         {
248             if ( ! menuEntry->SubMenu->IsActive )
249             {
250                 int max_x, max_y;
251                 SFG_Window *current_window = fgStructure.CurrentWindow;
252
253                 /* Set up the initial menu position now... */
254                 menuEntry->SubMenu->IsActive = GL_TRUE;
255
256                 /* Set up the initial submenu position now: */
257                 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
258                 menuEntry->SubMenu->X = menu->X + menu->Width;
259                 menuEntry->SubMenu->Y = menu->Y +
260                     menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
261
262                 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
263                     menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
264
265                 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
266                     menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
267                                                FREEGLUT_MENU_HEIGHT -
268                                                2 * FREEGLUT_MENU_BORDER );
269
270                 fgSetWindow( menuEntry->SubMenu->Window );
271                 glutPositionWindow( menuEntry->SubMenu->X,
272                                     menuEntry->SubMenu->Y );
273                 glutReshapeWindow( menuEntry->SubMenu->Width,
274                                    menuEntry->SubMenu->Height );
275                 glutPopWindow( );
276                 glutShowWindow( );
277                 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
278                 fgSetWindow( current_window );
279                 menuEntry->SubMenu->Window->State.MouseX =
280                     x + menu->X - menuEntry->SubMenu->X;
281                 menuEntry->SubMenu->Window->State.MouseY =
282                     y + menu->Y - menuEntry->SubMenu->Y;
283                 fghCheckMenuStatus( menuEntry->SubMenu );
284             }
285
286             /* Activate it because its parent entry is active */
287             menuEntry->SubMenu->IsActive = GL_TRUE;  /* XXX Do we need this? */
288         }
289
290         /* Report back that we have caught the menu cursor */
291         return GL_TRUE;
292     }
293
294     /* Looks like the menu cursor is somewhere else... */
295     if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
296         ( !menu->ActiveEntry->SubMenu ||
297           !menu->ActiveEntry->SubMenu->IsActive ) )
298     {
299         menu->Window->State.Redisplay = GL_TRUE;
300         menu->ActiveEntry->IsActive = GL_FALSE;
301         menu->ActiveEntry = NULL;
302     }
303
304     return GL_FALSE;
305 }
306
307 /*
308  * Displays a menu box and all of its submenus (if they are active)
309  */
310 static void fghDisplayMenuBox( SFG_Menu* menu )
311 {
312     SFG_MenuEntry *menuEntry;
313     int i;
314     int border = FREEGLUT_MENU_BORDER;
315
316     /*
317      * Have the menu box drawn first. The +- values are
318      * here just to make it more nice-looking...
319      */
320     /* a non-black dark version of the below. */
321     glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
322     glBegin( GL_QUAD_STRIP );
323         glVertex2i( menu->Width         , 0                    );
324         glVertex2i( menu->Width - border,                border);
325         glVertex2i( 0                   , 0                    );
326         glVertex2i(               border,                border);
327         glVertex2i( 0                   , menu->Height         );
328         glVertex2i(               border, menu->Height - border);
329     glEnd( );
330
331     /* a non-black dark version of the below. */
332     glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
333     glBegin( GL_QUAD_STRIP );
334         glVertex2i( 0                   , menu->Height         );
335         glVertex2i(               border, menu->Height - border);
336         glVertex2i( menu->Width         , menu->Height         );
337         glVertex2i( menu->Width - border, menu->Height - border);
338         glVertex2i( menu->Width         , 0                    );
339         glVertex2i( menu->Width - border,                border);
340     glEnd( );
341
342     glColor4fv( menu_pen_back );
343     glBegin( GL_QUADS );
344         glVertex2i(               border,                border);
345         glVertex2i( menu->Width - border,                border);
346         glVertex2i( menu->Width - border, menu->Height - border);
347         glVertex2i(               border, menu->Height - border);
348     glEnd( );
349
350     /* Check if any of the submenus is currently active... */
351     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
352          menuEntry;
353          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
354     {
355         /* Has the menu been marked as active, maybe? */
356         if( menuEntry->IsActive )
357         {
358             /*
359              * That's truly right, and we need to have it highlighted.
360              * There is an assumption that mouse cursor didn't move
361              * since the last check of menu activity state:
362              */
363             int menuID = menuEntry->Ordinal;
364
365             /* So have the highlight drawn... */
366             glColor4fv( menu_pen_hback );
367             glBegin( GL_QUADS );
368                 glVertex2i( border,
369                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
370                 glVertex2i( menu->Width - border,
371                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
372                 glVertex2i( menu->Width - border,
373                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
374                 glVertex2i( border,
375                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
376             glEnd( );
377         }
378     }
379
380     /* Print the menu entries now... */
381
382     glColor4fv( menu_pen_fore );
383
384     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
385          menuEntry;
386          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
387     {
388         /* If the menu entry is active, set the color to white */
389         if( menuEntry->IsActive )
390             glColor4fv( menu_pen_hfore );
391
392         /* Move the raster into position... */
393         /* Try to center the text - JCJ 31 July 2003*/
394         glRasterPos2i(
395             2 * border,
396             ( i + 1 )*FREEGLUT_MENU_HEIGHT -
397             ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
398         );
399
400         /* Have the label drawn, character after character: */
401         glutBitmapString( FREEGLUT_MENU_FONT,
402                           (unsigned char *)menuEntry->Text);
403
404         /* If it's a submenu, draw a right arrow */
405         if( menuEntry->SubMenu )
406         {
407             int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
408             int x_base = menu->Width - 2 - width;
409             int y_base = i*FREEGLUT_MENU_HEIGHT + border;
410             glBegin( GL_TRIANGLES );
411                 glVertex2i( x_base, y_base + 2*border);
412                 glVertex2i( menu->Width - 2, y_base +
413                             ( FREEGLUT_MENU_HEIGHT + border) / 2 );
414                 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
415             glEnd( );
416         }
417
418         /* If the menu entry is active, reset the color */
419         if( menuEntry->IsActive )
420             glColor4fv( menu_pen_fore );
421     }
422 }
423
424 /*
425  * Private static function to set the parent window of a submenu and all
426  * of its submenus
427  */
428 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
429 {
430     SFG_MenuEntry *menuEntry;
431
432     menu->ParentWindow = window;
433
434     for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
435          menuEntry;
436          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
437         if( menuEntry->SubMenu )
438             fghSetMenuParentWindow( window, menuEntry->SubMenu );
439 }
440
441 /*
442  * Function to check for menu entry selection on menu deactivation
443  */
444 static void fghExecuteMenuCallback( SFG_Menu* menu )
445 {
446     SFG_MenuEntry *menuEntry;
447
448     /* First of all check any of the active sub menus... */
449     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
450          menuEntry;
451          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
452     {
453         if( menuEntry->IsActive )
454         {
455             if( menuEntry->SubMenu )
456                 fghExecuteMenuCallback( menuEntry->SubMenu );
457             else
458                 if( menu->Callback )
459                 {
460                     SFG_Menu *save_menu = fgStructure.CurrentMenu;
461                     fgStructure.CurrentMenu = menu;
462                     menu->Callback( menuEntry->ID );
463                     fgStructure.CurrentMenu = save_menu;
464                 }
465
466             return;
467         }
468     }
469 }
470
471
472 /*
473  * Displays the currently active menu for the current window
474  */
475 void fgDisplayMenu( void )
476 {
477     SFG_Window* window = fgStructure.CurrentWindow;
478     SFG_Menu* menu = NULL;
479
480     FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
481                                    "fgDisplayMenu" );
482
483     /* Check if there is an active menu attached to this window... */
484     menu = window->ActiveMenu;
485     freeglut_return_if_fail( menu );
486
487     fgSetWindow( menu->Window );
488
489     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
490                   GL_POLYGON_BIT );
491
492     glDisable( GL_DEPTH_TEST );
493     glDisable( GL_TEXTURE_2D );
494     glDisable( GL_LIGHTING   );
495     glDisable( GL_CULL_FACE  );
496
497     glMatrixMode( GL_PROJECTION );
498     glPushMatrix( );
499     glLoadIdentity( );
500     glOrtho(
501          0, glutGet( GLUT_WINDOW_WIDTH  ),
502          glutGet( GLUT_WINDOW_HEIGHT ), 0,
503         -1, 1
504     );
505
506     glMatrixMode( GL_MODELVIEW );
507     glPushMatrix( );
508     glLoadIdentity( );
509
510     fghDisplayMenuBox( menu );
511
512     glPopAttrib( );
513
514     glMatrixMode( GL_PROJECTION );
515     glPopMatrix( );
516     glMatrixMode( GL_MODELVIEW );
517     glPopMatrix( );
518
519     glutSwapBuffers( );
520
521     fgSetWindow ( window );
522 }
523
524 /*
525  * Activates a menu pointed by the function argument
526  */
527 static void fghActivateMenu( SFG_Window* window, int button )
528 {
529     int max_x, max_y;
530
531     /* We'll be referencing this menu a lot, so remember its address: */
532     SFG_Menu* menu = window->Menu[ button ];
533     SFG_Window* current_window = fgStructure.CurrentWindow;
534
535     /* If the menu is already active in another window, deactivate it there */
536     if ( menu->ParentWindow )
537       menu->ParentWindow->ActiveMenu = NULL ;
538
539     /* Mark the menu as active, so that it gets displayed: */
540     window->ActiveMenu = menu;
541     menu->IsActive = GL_TRUE;
542     fghSetMenuParentWindow ( window, menu );
543     fgState.ActiveMenus++;
544
545     /* Set up the initial menu position now: */
546     fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
547     fgSetWindow( window );
548     menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
549     menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
550
551     if( menu->X + menu->Width > max_x )
552         menu->X -=menu->Width;
553
554     if( menu->Y + menu->Height > max_y )
555         menu->Y -=menu->Height;
556
557     menu->Window->State.MouseX =
558         window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
559     menu->Window->State.MouseY =
560         window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
561
562     fgSetWindow( menu->Window );
563     glutPositionWindow( menu->X, menu->Y );
564     glutReshapeWindow( menu->Width, menu->Height );
565     glutPopWindow( );
566     glutShowWindow( );
567     menu->Window->ActiveMenu = menu;
568     fghCheckMenuStatus( menu );
569     fgSetWindow( current_window );
570 }
571
572 /*
573  * Update Highlight states of the menu
574  *
575  * Current mouse position is in menu->Window->State.MouseX/Y.
576  */
577 void fgUpdateMenuHighlight ( SFG_Menu *menu )
578 {
579     fghCheckMenuStatus( menu );
580 }
581
582 /*
583  * Check whether an active menu absorbs a mouse click
584  */
585 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
586                               int mouse_x, int mouse_y )
587 {
588     /*
589      * Near as I can tell, this is the menu behaviour:
590      *  - Down-click the menu button, menu not active:  activate
591      *    the menu with its upper left-hand corner at the mouse
592      *    location.
593      *  - Down-click any button outside the menu, menu active:
594      *    deactivate the menu
595      *  - Down-click any button inside the menu, menu active:
596      *    select the menu entry and deactivate the menu
597      *  - Up-click the menu button, menu not active:  nothing happens
598      *  - Up-click the menu button outside the menu, menu active:
599      *    nothing happens
600      *  - Up-click the menu button inside the menu, menu active:
601      *    select the menu entry and deactivate the menu
602      * Since menus can have submenus, we need to check this recursively.
603      */
604     if( window->ActiveMenu )
605     {
606         if( window == window->ActiveMenu->ParentWindow )
607         {
608             window->ActiveMenu->Window->State.MouseX =
609                                        mouse_x - window->ActiveMenu->X;
610             window->ActiveMenu->Window->State.MouseY =
611                                        mouse_y - window->ActiveMenu->Y;
612         }
613
614         /* In the menu, invoke the callback and deactivate the menu */
615         if( fghCheckMenuStatus( window->ActiveMenu ) )
616         {
617             /*
618              * Save the current window and menu and set the current
619              * window to the window whose menu this is
620              */
621             SFG_Window *save_window = fgStructure.CurrentWindow;
622             SFG_Menu *save_menu = fgStructure.CurrentMenu;
623             SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
624             fgSetWindow( parent_window );
625             fgStructure.CurrentMenu = window->ActiveMenu;
626
627             /* Execute the menu callback */
628             fghExecuteMenuCallback( window->ActiveMenu );
629             fgDeactivateMenu( parent_window );
630
631             /* Restore the current window and menu */
632             fgSetWindow( save_window );
633             fgStructure.CurrentMenu = save_menu;
634         }
635         else if( pressed )
636             /*
637              * Outside the menu, deactivate if it's a downclick
638              *
639              * XXX This isn't enough.  A downclick outside of
640              * XXX the interior of our freeglut windows should also
641              * XXX deactivate the menu.  This is more complicated.
642              */
643             fgDeactivateMenu( window->ActiveMenu->ParentWindow );
644
645         /*
646          * XXX Why does an active menu require a redisplay at
647          * XXX this point?  If this can come out cleanly, then
648          * XXX it probably should do so; if not, a comment should
649          * XXX explain it.
650          */
651         if( ! window->IsMenu )
652             window->State.Redisplay = GL_TRUE;
653
654         return GL_TRUE;
655     }
656
657     /* No active menu, let's check whether we need to activate one. */
658     if( ( 0 <= button ) &&
659         ( FREEGLUT_MAX_MENUS > button ) &&
660         ( window->Menu[ button ] ) &&
661         pressed )
662     {
663         /* XXX Posting a requisite Redisplay seems bogus. */
664         window->State.Redisplay = GL_TRUE;
665         fghActivateMenu( window, button );
666         return GL_TRUE;
667     }
668
669     return GL_FALSE;
670 }
671
672 /*
673  * Deactivates a menu pointed by the function argument.
674  */
675 void fgDeactivateMenu( SFG_Window *window )
676 {
677     SFG_Window *parent_window = NULL;
678
679     /* Check if there is an active menu attached to this window... */
680     SFG_Menu* menu = window->ActiveMenu;
681     SFG_MenuEntry *menuEntry;
682
683     /* Did we find an active window? */
684     freeglut_return_if_fail( menu );
685
686     parent_window = menu->ParentWindow;
687
688     /* Hide the present menu's window */
689     fgSetWindow( menu->Window );
690     glutHideWindow( );
691
692     /* Forget about having that menu active anymore, now: */
693     menu->Window->ActiveMenu = NULL;
694     menu->ParentWindow->ActiveMenu = NULL;
695     fghSetMenuParentWindow ( NULL, menu );
696     menu->IsActive = GL_FALSE;
697     menu->ActiveEntry = NULL;
698
699     fgState.ActiveMenus--;
700
701     /* Hide all submenu windows, and the root menu's window. */
702     for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
703           menuEntry;
704           menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
705     {
706         menuEntry->IsActive = GL_FALSE;
707
708         /* Is that an active submenu by any case? */
709         if( menuEntry->SubMenu )
710             fghDeactivateSubMenu( menuEntry );
711     }
712
713     fgSetWindow ( parent_window ) ;
714 }
715
716 /*
717  * Recalculates current menu's box size
718  */
719 void fghCalculateMenuBoxSize( void )
720 {
721     SFG_MenuEntry* menuEntry;
722     int width = 0, height = 0;
723
724     /* Make sure there is a current menu set */
725     freeglut_return_if_fail( fgStructure.CurrentMenu );
726
727     /* The menu's box size depends on the menu entries: */
728     for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
729          menuEntry;
730          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
731     {
732         /* Update the menu entry's width value */
733         menuEntry->Width = glutBitmapLength(
734             FREEGLUT_MENU_FONT,
735             (unsigned char *)menuEntry->Text
736         );
737
738         /*
739          * If the entry is a submenu, then it needs to be wider to
740          * accomodate the arrow. JCJ 31 July 2003
741          */
742         if (menuEntry->SubMenu )
743             menuEntry->Width += glutBitmapLength(
744                 FREEGLUT_MENU_FONT,
745                 (unsigned char *)"_"
746             );
747
748         /* Check if it's the biggest we've found */
749         if( menuEntry->Width > width )
750             width = menuEntry->Width;
751
752         height += FREEGLUT_MENU_HEIGHT;
753     }
754
755     /* Store the menu's box size now: */
756     fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
757     fgStructure.CurrentMenu->Width  = width  + 4 * FREEGLUT_MENU_BORDER;
758 }
759
760
761 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
762
763 /*
764  * Creates a new menu object, adding it to the freeglut structure
765  */
766 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
767 {
768     /* The menu object creation code resides in freeglut_structure.c */
769     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
770     return fgCreateMenu( callback )->ID;
771 }
772
773 /*
774  * Destroys a menu object, removing all references to it
775  */
776 void FGAPIENTRY glutDestroyMenu( int menuID )
777 {
778     SFG_Menu* menu;
779
780     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
781     menu = fgMenuByID( menuID );
782
783     freeglut_return_if_fail( menu );
784
785     /* The menu object destruction code resides in freeglut_structure.c */
786     fgDestroyMenu( menu );
787 }
788
789 /*
790  * Returns the ID number of the currently active menu
791  */
792 int FGAPIENTRY glutGetMenu( void )
793 {
794     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
795
796     if( fgStructure.CurrentMenu )
797         return fgStructure.CurrentMenu->ID;
798
799     return 0;
800 }
801
802 /*
803  * Sets the current menu given its menu ID
804  */
805 void FGAPIENTRY glutSetMenu( int menuID )
806 {
807     SFG_Menu* menu;
808
809     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
810     menu = fgMenuByID( menuID );
811
812     freeglut_return_if_fail( menu );
813
814     fgStructure.CurrentMenu = menu;
815 }
816
817 /*
818  * Adds a menu entry to the bottom of the current menu
819  */
820 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
821 {
822     SFG_MenuEntry* menuEntry;
823     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
824     menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
825     freeglut_return_if_fail( fgStructure.CurrentMenu );
826
827     menuEntry->Text = strdup( label );
828     menuEntry->ID   = value;
829
830     /* Have the new menu entry attached to the current menu */
831     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
832
833     fghCalculateMenuBoxSize( );
834 }
835
836 /*
837  * Add a sub menu to the bottom of the current menu
838  */
839 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
840 {
841     SFG_MenuEntry *menuEntry;
842     SFG_Menu *subMenu;
843
844     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
845     menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
846     subMenu = fgMenuByID( subMenuID );
847
848     freeglut_return_if_fail( fgStructure.CurrentMenu );
849     freeglut_return_if_fail( subMenu );
850
851     menuEntry->Text    = strdup( label );
852     menuEntry->SubMenu = subMenu;
853     menuEntry->ID      = -1;
854
855     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
856     fghCalculateMenuBoxSize( );
857 }
858
859 /*
860  * Changes the specified menu item in the current menu into a menu entry
861  */
862 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
863 {
864     SFG_MenuEntry* menuEntry = NULL;
865
866     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
867     freeglut_return_if_fail( fgStructure.CurrentMenu );
868
869     /* Get n-th menu entry in the current menu, starting from one: */
870     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
871
872     freeglut_return_if_fail( menuEntry );
873
874     /* We want it to become a normal menu entry, so: */
875     if( menuEntry->Text )
876         free( menuEntry->Text );
877
878     menuEntry->Text    = strdup( label );
879     menuEntry->ID      = value;
880     menuEntry->SubMenu = NULL;
881     fghCalculateMenuBoxSize( );
882 }
883
884 /*
885  * Changes the specified menu item in the current menu into a sub-menu trigger.
886  */
887 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
888                                      int subMenuID )
889 {
890     SFG_Menu*      subMenu;
891     SFG_MenuEntry* menuEntry;
892
893     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
894     subMenu = fgMenuByID( subMenuID );
895     menuEntry = NULL;
896
897     freeglut_return_if_fail( fgStructure.CurrentMenu );
898     freeglut_return_if_fail( subMenu );
899
900     /* Get n-th menu entry in the current menu, starting from one: */
901     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
902
903     freeglut_return_if_fail( menuEntry );
904
905     /* We want it to become a sub menu entry, so: */
906     if( menuEntry->Text )
907         free( menuEntry->Text );
908
909     menuEntry->Text    = strdup( label );
910     menuEntry->SubMenu = subMenu;
911     menuEntry->ID      = -1;
912     fghCalculateMenuBoxSize( );
913 }
914
915 /*
916  * Removes the specified menu item from the current menu
917  */
918 void FGAPIENTRY glutRemoveMenuItem( int item )
919 {
920     SFG_MenuEntry* menuEntry;
921
922     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
923     freeglut_return_if_fail( fgStructure.CurrentMenu );
924
925     /* Get n-th menu entry in the current menu, starting from one: */
926     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
927
928     freeglut_return_if_fail( menuEntry );
929
930     fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
931     if ( menuEntry->Text )
932       free( menuEntry->Text );
933
934     free( menuEntry );
935     fghCalculateMenuBoxSize( );
936 }
937
938 /*
939  * Attaches a menu to the current window
940  */
941 void FGAPIENTRY glutAttachMenu( int button )
942 {
943     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
944
945     freeglut_return_if_fail( fgStructure.CurrentWindow );
946     freeglut_return_if_fail( fgStructure.CurrentMenu );
947
948     freeglut_return_if_fail( button >= 0 );
949     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
950
951     fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
952 }
953
954 /*
955  * Detaches a menu from the current window
956  */
957 void FGAPIENTRY glutDetachMenu( int button )
958 {
959     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
960
961     freeglut_return_if_fail( fgStructure.CurrentWindow );
962     freeglut_return_if_fail( fgStructure.CurrentMenu );
963
964     freeglut_return_if_fail( button >= 0 );
965     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
966
967     fgStructure.CurrentWindow->Menu[ button ] = NULL;
968 }
969
970 /*
971  * A.Donev: Set and retrieve the menu's user data
972  */
973 void* FGAPIENTRY glutGetMenuData( void )
974 {
975     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
976     return fgStructure.CurrentMenu->UserData;
977 }
978
979 void FGAPIENTRY glutSetMenuData(void* data)
980 {
981     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
982     fgStructure.CurrentMenu->UserData=data;
983 }
984
985 /*** END OF FILE ***/