now checking for NULL window in fgDeactivateMenu (thanks for Ioannis to
[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 (and any submenus) there */
516     if ( menu->ParentWindow )
517       fgDeactivateMenu(menu->ParentWindow);
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              * A downclick outside of the interior of our freeglut windows
624              * is dealt with in the WM_KILLFOCUS handler of fgPlatformWindowProc
625              */
626         {
627             if (window->ActiveMenu->ParentWindow)
628                 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
629             else
630             {
631                 /* 
632                  * Its a rare occasion that a window has an ActiveMenu but
633                  * that menus does not have a parent window. It happens
634                  * however in the corner case bug when one opens a menu in
635                  * a main window, then opens a different menu in this main
636                  * window's child (you now have two menus open
637                  * simultaneously, thats the bug) and then click somewhere
638                  * else that causes both menus to close. One of them is
639                  * then not properly cleaned up. This finishes the cleaning
640                  * and minimizes the impact on the user, he only needs one
641                  * extra mouse click.
642                  */
643                 fghSetMenuParentWindow ( NULL, window->ActiveMenu );
644                 window->ActiveMenu->IsActive = GL_FALSE;
645                 window->ActiveMenu->ActiveEntry = NULL;
646                 window->ActiveMenu = NULL;
647             }
648         }
649
650         /*
651          * XXX Why does an active menu require a redisplay at
652          * XXX this point?  If this can come out cleanly, then
653          * XXX it probably should do so; if not, a comment should
654          * XXX explain it.
655          */
656         if( ! window->IsMenu )
657             window->State.Redisplay = GL_TRUE;
658
659         return GL_TRUE;
660     }
661
662     /* No active menu, let's check whether we need to activate one. */
663     if( ( 0 <= button ) &&
664         ( FREEGLUT_MAX_MENUS > button ) &&
665         ( window->Menu[ button ] ) &&
666         pressed )
667     {
668         /* XXX Posting a requisite Redisplay seems bogus. */
669         window->State.Redisplay = GL_TRUE;
670         fghActivateMenu( window, button );
671         return GL_TRUE;
672     }
673
674     return GL_FALSE;
675 }
676
677 /*
678  * Deactivates a menu pointed by the function argument.
679  */
680 void fgDeactivateMenu( SFG_Window *window )
681 {
682     SFG_Window *parent_window = NULL;
683     SFG_Menu* menu;
684     SFG_MenuEntry *menuEntry;
685
686     /* Check if there is an active menu attached to this window... */
687     freeglut_return_if_fail( window );
688     menu = window->ActiveMenu;
689     /* Did we find an active window? */
690     freeglut_return_if_fail( menu );
691
692     parent_window = menu->ParentWindow;
693
694     /* Hide the present menu's window */
695     fgSetWindow( menu->Window );
696     glutHideWindow( );
697
698     /* Forget about having that menu active anymore, now: */
699     menu->Window->ActiveMenu = NULL;
700     menu->ParentWindow->ActiveMenu = NULL;
701     fghSetMenuParentWindow ( NULL, menu );
702     menu->IsActive = GL_FALSE;
703     menu->ActiveEntry = NULL;
704
705     fgState.ActiveMenus--;
706
707     /* Hide all submenu windows, and the root menu's window. */
708     for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
709           menuEntry;
710           menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
711     {
712         menuEntry->IsActive = GL_FALSE;
713
714         /* Is that an active submenu by any case? */
715         if( menuEntry->SubMenu )
716             fghDeactivateSubMenu( menuEntry );
717     }
718
719     fgSetWindow ( parent_window ) ;
720 }
721
722 /*
723  * Recalculates current menu's box size
724  */
725 void fghCalculateMenuBoxSize( void )
726 {
727     SFG_MenuEntry* menuEntry;
728     int width = 0, height = 0;
729
730     /* Make sure there is a current menu set */
731     freeglut_return_if_fail( fgStructure.CurrentMenu );
732
733     /* The menu's box size depends on the menu entries: */
734     for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
735          menuEntry;
736          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
737     {
738         /* Update the menu entry's width value */
739         menuEntry->Width = glutBitmapLength(
740             FREEGLUT_MENU_FONT,
741             (unsigned char *)menuEntry->Text
742         );
743
744         /*
745          * If the entry is a submenu, then it needs to be wider to
746          * accomodate the arrow. JCJ 31 July 2003
747          */
748         if (menuEntry->SubMenu )
749             menuEntry->Width += glutBitmapLength(
750                 FREEGLUT_MENU_FONT,
751                 (unsigned char *)"_"
752             );
753
754         /* Check if it's the biggest we've found */
755         if( menuEntry->Width > width )
756             width = menuEntry->Width;
757
758         height += FREEGLUT_MENU_HEIGHT;
759     }
760
761     /* Store the menu's box size now: */
762     fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
763     fgStructure.CurrentMenu->Width  = width  + 4 * FREEGLUT_MENU_BORDER;
764 }
765
766
767 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
768
769 /*
770  * Creates a new menu object, adding it to the freeglut structure
771  */
772 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
773 {
774     /* The menu object creation code resides in freeglut_structure.c */
775     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
776     return fgCreateMenu( callback )->ID;
777 }
778
779 /*
780  * Destroys a menu object, removing all references to it
781  */
782 void FGAPIENTRY glutDestroyMenu( int menuID )
783 {
784     SFG_Menu* menu;
785
786     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
787     menu = fgMenuByID( menuID );
788
789     freeglut_return_if_fail( menu );
790
791     /* The menu object destruction code resides in freeglut_structure.c */
792     fgDestroyMenu( menu );
793 }
794
795 /*
796  * Returns the ID number of the currently active menu
797  */
798 int FGAPIENTRY glutGetMenu( void )
799 {
800     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
801
802     if( fgStructure.CurrentMenu )
803         return fgStructure.CurrentMenu->ID;
804
805     return 0;
806 }
807
808 /*
809  * Sets the current menu given its menu ID
810  */
811 void FGAPIENTRY glutSetMenu( int menuID )
812 {
813     SFG_Menu* menu;
814
815     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
816     menu = fgMenuByID( menuID );
817
818     freeglut_return_if_fail( menu );
819
820     fgStructure.CurrentMenu = menu;
821 }
822
823 /*
824  * Adds a menu entry to the bottom of the current menu
825  */
826 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
827 {
828     SFG_MenuEntry* menuEntry;
829     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
830     menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
831     freeglut_return_if_fail( fgStructure.CurrentMenu );
832
833     menuEntry->Text = strdup( label );
834     menuEntry->ID   = value;
835
836     /* Have the new menu entry attached to the current menu */
837     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
838
839     fghCalculateMenuBoxSize( );
840 }
841
842 /*
843  * Add a sub menu to the bottom of the current menu
844  */
845 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
846 {
847     SFG_MenuEntry *menuEntry;
848     SFG_Menu *subMenu;
849
850     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
851     menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
852     subMenu = fgMenuByID( subMenuID );
853
854     freeglut_return_if_fail( fgStructure.CurrentMenu );
855     freeglut_return_if_fail( subMenu );
856
857     menuEntry->Text    = strdup( label );
858     menuEntry->SubMenu = subMenu;
859     menuEntry->ID      = -1;
860
861     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
862     fghCalculateMenuBoxSize( );
863 }
864
865 /*
866  * Changes the specified menu item in the current menu into a menu entry
867  */
868 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
869 {
870     SFG_MenuEntry* menuEntry = NULL;
871
872     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
873     freeglut_return_if_fail( fgStructure.CurrentMenu );
874
875     /* Get n-th menu entry in the current menu, starting from one: */
876     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
877
878     freeglut_return_if_fail( menuEntry );
879
880     /* We want it to become a normal menu entry, so: */
881     if( menuEntry->Text )
882         free( menuEntry->Text );
883
884     menuEntry->Text    = strdup( label );
885     menuEntry->ID      = value;
886     menuEntry->SubMenu = NULL;
887     fghCalculateMenuBoxSize( );
888 }
889
890 /*
891  * Changes the specified menu item in the current menu into a sub-menu trigger.
892  */
893 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
894                                      int subMenuID )
895 {
896     SFG_Menu*      subMenu;
897     SFG_MenuEntry* menuEntry;
898
899     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
900     subMenu = fgMenuByID( subMenuID );
901     menuEntry = NULL;
902
903     freeglut_return_if_fail( fgStructure.CurrentMenu );
904     freeglut_return_if_fail( subMenu );
905
906     /* Get n-th menu entry in the current menu, starting from one: */
907     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
908
909     freeglut_return_if_fail( menuEntry );
910
911     /* We want it to become a sub menu entry, so: */
912     if( menuEntry->Text )
913         free( menuEntry->Text );
914
915     menuEntry->Text    = strdup( label );
916     menuEntry->SubMenu = subMenu;
917     menuEntry->ID      = -1;
918     fghCalculateMenuBoxSize( );
919 }
920
921 /*
922  * Removes the specified menu item from the current menu
923  */
924 void FGAPIENTRY glutRemoveMenuItem( int item )
925 {
926     SFG_MenuEntry* menuEntry;
927
928     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
929     freeglut_return_if_fail( fgStructure.CurrentMenu );
930
931     /* Get n-th menu entry in the current menu, starting from one: */
932     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
933
934     freeglut_return_if_fail( menuEntry );
935
936     fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
937     if ( menuEntry->Text )
938       free( menuEntry->Text );
939
940     free( menuEntry );
941     fghCalculateMenuBoxSize( );
942 }
943
944 /*
945  * Attaches a menu to the current window
946  */
947 void FGAPIENTRY glutAttachMenu( int button )
948 {
949     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
950
951     freeglut_return_if_fail( fgStructure.CurrentWindow );
952     freeglut_return_if_fail( fgStructure.CurrentMenu );
953
954     freeglut_return_if_fail( button >= 0 );
955     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
956
957     fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
958 }
959
960 /*
961  * Detaches a menu from the current window
962  */
963 void FGAPIENTRY glutDetachMenu( int button )
964 {
965     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
966
967     freeglut_return_if_fail( fgStructure.CurrentWindow );
968     freeglut_return_if_fail( fgStructure.CurrentMenu );
969
970     freeglut_return_if_fail( button >= 0 );
971     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
972
973     fgStructure.CurrentWindow->Menu[ button ] = NULL;
974 }
975
976 /*
977  * A.Donev: Set and retrieve the menu's user data
978  */
979 void* FGAPIENTRY glutGetMenuData( void )
980 {
981     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
982     return fgStructure.CurrentMenu->UserData;
983 }
984
985 void FGAPIENTRY glutSetMenuData(void* data)
986 {
987     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
988     fgStructure.CurrentMenu->UserData=data;
989 }
990
991 /*** END OF FILE ***/