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