a96f5d666d4f0c322e0f13eddbae8abf4fcf260d
[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_MS_WINDOWS
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_MS_WINDOWS
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_POSIX_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                 {
267                     menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
268                                                FREEGLUT_MENU_HEIGHT -
269                                                2 * FREEGLUT_MENU_BORDER );
270                     if( menuEntry->SubMenu->Y < 0 )
271                         menuEntry->SubMenu->Y = 0;
272                 }
273
274                 fgSetWindow( menuEntry->SubMenu->Window );
275                 glutPositionWindow( menuEntry->SubMenu->X,
276                                     menuEntry->SubMenu->Y );
277                 glutReshapeWindow( menuEntry->SubMenu->Width,
278                                    menuEntry->SubMenu->Height );
279                 glutPopWindow( );
280                 glutShowWindow( );
281                 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
282                 fgSetWindow( current_window );
283                 menuEntry->SubMenu->Window->State.MouseX =
284                     x + menu->X - menuEntry->SubMenu->X;
285                 menuEntry->SubMenu->Window->State.MouseY =
286                     y + menu->Y - menuEntry->SubMenu->Y;
287                 fghCheckMenuStatus( menuEntry->SubMenu );
288             }
289
290             /* Activate it because its parent entry is active */
291             menuEntry->SubMenu->IsActive = GL_TRUE;  /* XXX Do we need this? */
292         }
293
294         /* Report back that we have caught the menu cursor */
295         return GL_TRUE;
296     }
297
298     /* Looks like the menu cursor is somewhere else... */
299     if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
300         ( !menu->ActiveEntry->SubMenu ||
301           !menu->ActiveEntry->SubMenu->IsActive ) )
302     {
303         menu->Window->State.Redisplay = GL_TRUE;
304         menu->ActiveEntry->IsActive = GL_FALSE;
305         menu->ActiveEntry = NULL;
306     }
307
308     return GL_FALSE;
309 }
310
311 /*
312  * Displays a menu box and all of its submenus (if they are active)
313  */
314 static void fghDisplayMenuBox( SFG_Menu* menu )
315 {
316     SFG_MenuEntry *menuEntry;
317     int i;
318     int border = FREEGLUT_MENU_BORDER;
319
320     /*
321      * Have the menu box drawn first. The +- values are
322      * here just to make it more nice-looking...
323      */
324     /* a non-black dark version of the below. */
325     glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
326     glBegin( GL_QUAD_STRIP );
327         glVertex2i( menu->Width         , 0                    );
328         glVertex2i( menu->Width - border,                border);
329         glVertex2i( 0                   , 0                    );
330         glVertex2i(               border,                border);
331         glVertex2i( 0                   , menu->Height         );
332         glVertex2i(               border, menu->Height - border);
333     glEnd( );
334
335     /* a non-black dark version of the below. */
336     glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
337     glBegin( GL_QUAD_STRIP );
338         glVertex2i( 0                   , menu->Height         );
339         glVertex2i(               border, menu->Height - border);
340         glVertex2i( menu->Width         , menu->Height         );
341         glVertex2i( menu->Width - border, menu->Height - border);
342         glVertex2i( menu->Width         , 0                    );
343         glVertex2i( menu->Width - border,                border);
344     glEnd( );
345
346     glColor4fv( menu_pen_back );
347     glBegin( GL_QUADS );
348         glVertex2i(               border,                border);
349         glVertex2i( menu->Width - border,                border);
350         glVertex2i( menu->Width - border, menu->Height - border);
351         glVertex2i(               border, menu->Height - border);
352     glEnd( );
353
354     /* Check if any of the submenus is currently active... */
355     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
356          menuEntry;
357          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
358     {
359         /* Has the menu been marked as active, maybe? */
360         if( menuEntry->IsActive )
361         {
362             /*
363              * That's truly right, and we need to have it highlighted.
364              * There is an assumption that mouse cursor didn't move
365              * since the last check of menu activity state:
366              */
367             int menuID = menuEntry->Ordinal;
368
369             /* So have the highlight drawn... */
370             glColor4fv( menu_pen_hback );
371             glBegin( GL_QUADS );
372                 glVertex2i( border,
373                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
374                 glVertex2i( menu->Width - border,
375                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
376                 glVertex2i( menu->Width - border,
377                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
378                 glVertex2i( border,
379                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
380             glEnd( );
381         }
382     }
383
384     /* Print the menu entries now... */
385
386     glColor4fv( menu_pen_fore );
387
388     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
389          menuEntry;
390          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
391     {
392         /* If the menu entry is active, set the color to white */
393         if( menuEntry->IsActive )
394             glColor4fv( menu_pen_hfore );
395
396         /* Move the raster into position... */
397         /* Try to center the text - JCJ 31 July 2003*/
398         glRasterPos2i(
399             2 * border,
400             ( i + 1 )*FREEGLUT_MENU_HEIGHT -
401             ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
402         );
403
404         /* Have the label drawn, character after character: */
405         glutBitmapString( FREEGLUT_MENU_FONT,
406                           (unsigned char *)menuEntry->Text);
407
408         /* If it's a submenu, draw a right arrow */
409         if( menuEntry->SubMenu )
410         {
411             int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
412             int x_base = menu->Width - 2 - width;
413             int y_base = i*FREEGLUT_MENU_HEIGHT + border;
414             glBegin( GL_TRIANGLES );
415                 glVertex2i( x_base, y_base + 2*border);
416                 glVertex2i( menu->Width - 2, y_base +
417                             ( FREEGLUT_MENU_HEIGHT + border) / 2 );
418                 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
419             glEnd( );
420         }
421
422         /* If the menu entry is active, reset the color */
423         if( menuEntry->IsActive )
424             glColor4fv( menu_pen_fore );
425     }
426 }
427
428 /*
429  * Private static function to set the parent window of a submenu and all
430  * of its submenus
431  */
432 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
433 {
434     SFG_MenuEntry *menuEntry;
435
436     menu->ParentWindow = window;
437
438     for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
439          menuEntry;
440          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
441         if( menuEntry->SubMenu )
442             fghSetMenuParentWindow( window, menuEntry->SubMenu );
443 }
444
445 /*
446  * Function to check for menu entry selection on menu deactivation
447  */
448 static void fghExecuteMenuCallback( SFG_Menu* menu )
449 {
450     SFG_MenuEntry *menuEntry;
451
452     /* First of all check any of the active sub menus... */
453     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
454          menuEntry;
455          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
456     {
457         if( menuEntry->IsActive )
458         {
459             if( menuEntry->SubMenu )
460                 fghExecuteMenuCallback( menuEntry->SubMenu );
461             else
462                 if( menu->Callback )
463                 {
464                     SFG_Menu *save_menu = fgStructure.CurrentMenu;
465                     fgStructure.CurrentMenu = menu;
466                     menu->Callback( menuEntry->ID );
467                     fgStructure.CurrentMenu = save_menu;
468                 }
469
470             return;
471         }
472     }
473 }
474
475
476 /*
477  * Displays the currently active menu for the current window
478  */
479 void fgDisplayMenu( void )
480 {
481     SFG_Window* window = fgStructure.CurrentWindow;
482     SFG_Menu* menu = NULL;
483
484     FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
485                                    "fgDisplayMenu" );
486
487     /* Check if there is an active menu attached to this window... */
488     menu = window->ActiveMenu;
489     freeglut_return_if_fail( menu );
490
491     fgSetWindow( menu->Window );
492
493     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
494                   GL_POLYGON_BIT );
495
496     glDisable( GL_DEPTH_TEST );
497     glDisable( GL_TEXTURE_2D );
498     glDisable( GL_LIGHTING   );
499     glDisable( GL_CULL_FACE  );
500
501     glMatrixMode( GL_PROJECTION );
502     glPushMatrix( );
503     glLoadIdentity( );
504     glOrtho(
505          0, glutGet( GLUT_WINDOW_WIDTH  ),
506          glutGet( GLUT_WINDOW_HEIGHT ), 0,
507         -1, 1
508     );
509
510     glMatrixMode( GL_MODELVIEW );
511     glPushMatrix( );
512     glLoadIdentity( );
513
514     fghDisplayMenuBox( menu );
515
516     glPopAttrib( );
517
518     glMatrixMode( GL_PROJECTION );
519     glPopMatrix( );
520     glMatrixMode( GL_MODELVIEW );
521     glPopMatrix( );
522
523     glutSwapBuffers( );
524
525     fgSetWindow ( window );
526 }
527
528 /*
529  * Activates a menu pointed by the function argument
530  */
531 static void fghActivateMenu( SFG_Window* window, int button )
532 {
533     int max_x, max_y;
534
535     /* We'll be referencing this menu a lot, so remember its address: */
536     SFG_Menu* menu = window->Menu[ button ];
537     SFG_Window* current_window = fgStructure.CurrentWindow;
538
539     /* If the menu is already active in another window, deactivate it there */
540     if ( menu->ParentWindow )
541       menu->ParentWindow->ActiveMenu = NULL ;
542
543     /* Mark the menu as active, so that it gets displayed: */
544     window->ActiveMenu = menu;
545     menu->IsActive = GL_TRUE;
546     fghSetMenuParentWindow ( window, menu );
547     fgState.ActiveMenus++;
548
549     /* Set up the initial menu position now: */
550     fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
551     fgSetWindow( window );
552     menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
553     menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
554
555     if( menu->X + menu->Width > max_x )
556         menu->X -=menu->Width;
557
558     if( menu->Y + menu->Height > max_y )
559     {
560         menu->Y -=menu->Height;
561         if( menu->Y < 0 )
562             menu->Y = 0;
563     }
564
565     menu->Window->State.MouseX =
566         window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
567     menu->Window->State.MouseY =
568         window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
569
570     fgSetWindow( menu->Window );
571     glutPositionWindow( menu->X, menu->Y );
572     glutReshapeWindow( menu->Width, menu->Height );
573     glutPopWindow( );
574     glutShowWindow( );
575     menu->Window->ActiveMenu = menu;
576     fghCheckMenuStatus( menu );
577     fgSetWindow( current_window );
578 }
579
580 /*
581  * Update Highlight states of the menu
582  *
583  * Current mouse position is in menu->Window->State.MouseX/Y.
584  */
585 void fgUpdateMenuHighlight ( SFG_Menu *menu )
586 {
587     fghCheckMenuStatus( menu );
588 }
589
590 /*
591  * Check whether an active menu absorbs a mouse click
592  */
593 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
594                               int mouse_x, int mouse_y )
595 {
596     /*
597      * Near as I can tell, this is the menu behaviour:
598      *  - Down-click the menu button, menu not active:  activate
599      *    the menu with its upper left-hand corner at the mouse
600      *    location.
601      *  - Down-click any button outside the menu, menu active:
602      *    deactivate the menu
603      *  - Down-click any button inside the menu, menu active:
604      *    select the menu entry and deactivate the menu
605      *  - Up-click the menu button, menu not active:  nothing happens
606      *  - Up-click the menu button outside the menu, menu active:
607      *    nothing happens
608      *  - Up-click the menu button inside the menu, menu active:
609      *    select the menu entry and deactivate the menu
610      * Since menus can have submenus, we need to check this recursively.
611      */
612     if( window->ActiveMenu )
613     {
614         if( window == window->ActiveMenu->ParentWindow )
615         {
616             window->ActiveMenu->Window->State.MouseX =
617                                        mouse_x - window->ActiveMenu->X;
618             window->ActiveMenu->Window->State.MouseY =
619                                        mouse_y - window->ActiveMenu->Y;
620         }
621
622         /* In the menu, invoke the callback and deactivate the menu */
623         if( fghCheckMenuStatus( window->ActiveMenu ) )
624         {
625             /*
626              * Save the current window and menu and set the current
627              * window to the window whose menu this is
628              */
629             SFG_Window *save_window = fgStructure.CurrentWindow;
630             SFG_Menu *save_menu = fgStructure.CurrentMenu;
631             SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
632             fgSetWindow( parent_window );
633             fgStructure.CurrentMenu = window->ActiveMenu;
634
635             /* Execute the menu callback */
636             fghExecuteMenuCallback( window->ActiveMenu );
637             fgDeactivateMenu( parent_window );
638
639             /* Restore the current window and menu */
640             fgSetWindow( save_window );
641             fgStructure.CurrentMenu = save_menu;
642         }
643         else if( pressed )
644             /*
645              * Outside the menu, deactivate if it's a downclick
646              *
647              * XXX This isn't enough.  A downclick outside of
648              * XXX the interior of our freeglut windows should also
649              * XXX deactivate the menu.  This is more complicated.
650              */
651             fgDeactivateMenu( window->ActiveMenu->ParentWindow );
652
653         /*
654          * XXX Why does an active menu require a redisplay at
655          * XXX this point?  If this can come out cleanly, then
656          * XXX it probably should do so; if not, a comment should
657          * XXX explain it.
658          */
659         if( ! window->IsMenu )
660             window->State.Redisplay = GL_TRUE;
661
662         return GL_TRUE;
663     }
664
665     /* No active menu, let's check whether we need to activate one. */
666     if( ( 0 <= button ) &&
667         ( FREEGLUT_MAX_MENUS > button ) &&
668         ( window->Menu[ button ] ) &&
669         pressed )
670     {
671         /* XXX Posting a requisite Redisplay seems bogus. */
672         window->State.Redisplay = GL_TRUE;
673         fghActivateMenu( window, button );
674         return GL_TRUE;
675     }
676
677     return GL_FALSE;
678 }
679
680 /*
681  * Deactivates a menu pointed by the function argument.
682  */
683 void fgDeactivateMenu( SFG_Window *window )
684 {
685     SFG_Window *parent_window = NULL;
686
687     /* Check if there is an active menu attached to this window... */
688     SFG_Menu* menu = window->ActiveMenu;
689     SFG_MenuEntry *menuEntry;
690
691     /* Did we find an active window? */
692     freeglut_return_if_fail( menu );
693
694     parent_window = menu->ParentWindow;
695
696     /* Hide the present menu's window */
697     fgSetWindow( menu->Window );
698     glutHideWindow( );
699
700     /* Forget about having that menu active anymore, now: */
701     menu->Window->ActiveMenu = NULL;
702     menu->ParentWindow->ActiveMenu = NULL;
703     fghSetMenuParentWindow ( NULL, menu );
704     menu->IsActive = GL_FALSE;
705     menu->ActiveEntry = NULL;
706
707     fgState.ActiveMenus--;
708
709     /* Hide all submenu windows, and the root menu's window. */
710     for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
711           menuEntry;
712           menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
713     {
714         menuEntry->IsActive = GL_FALSE;
715
716         /* Is that an active submenu by any case? */
717         if( menuEntry->SubMenu )
718             fghDeactivateSubMenu( menuEntry );
719     }
720
721     fgSetWindow ( parent_window ) ;
722 }
723
724 /*
725  * Recalculates current menu's box size
726  */
727 void fghCalculateMenuBoxSize( void )
728 {
729     SFG_MenuEntry* menuEntry;
730     int width = 0, height = 0;
731
732     /* Make sure there is a current menu set */
733     freeglut_return_if_fail( fgStructure.CurrentMenu );
734
735     /* The menu's box size depends on the menu entries: */
736     for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
737          menuEntry;
738          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
739     {
740         /* Update the menu entry's width value */
741         menuEntry->Width = glutBitmapLength(
742             FREEGLUT_MENU_FONT,
743             (unsigned char *)menuEntry->Text
744         );
745
746         /*
747          * If the entry is a submenu, then it needs to be wider to
748          * accomodate the arrow. JCJ 31 July 2003
749          */
750         if (menuEntry->SubMenu )
751             menuEntry->Width += glutBitmapLength(
752                 FREEGLUT_MENU_FONT,
753                 (unsigned char *)"_"
754             );
755
756         /* Check if it's the biggest we've found */
757         if( menuEntry->Width > width )
758             width = menuEntry->Width;
759
760         height += FREEGLUT_MENU_HEIGHT;
761     }
762
763     /* Store the menu's box size now: */
764     fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
765     fgStructure.CurrentMenu->Width  = width  + 4 * FREEGLUT_MENU_BORDER;
766 }
767
768
769 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
770
771 /*
772  * Creates a new menu object, adding it to the freeglut structure
773  */
774 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
775 {
776     /* The menu object creation code resides in freeglut_structure.c */
777     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
778     return fgCreateMenu( callback )->ID;
779 }
780
781 /*
782  * Destroys a menu object, removing all references to it
783  */
784 void FGAPIENTRY glutDestroyMenu( int menuID )
785 {
786     SFG_Menu* menu;
787
788     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
789     menu = fgMenuByID( menuID );
790
791     freeglut_return_if_fail( menu );
792
793     /* The menu object destruction code resides in freeglut_structure.c */
794     fgDestroyMenu( menu );
795 }
796
797 /*
798  * Returns the ID number of the currently active menu
799  */
800 int FGAPIENTRY glutGetMenu( void )
801 {
802     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
803
804     if( fgStructure.CurrentMenu )
805         return fgStructure.CurrentMenu->ID;
806
807     return 0;
808 }
809
810 /*
811  * Sets the current menu given its menu ID
812  */
813 void FGAPIENTRY glutSetMenu( int menuID )
814 {
815     SFG_Menu* menu;
816
817     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
818     menu = fgMenuByID( menuID );
819
820     freeglut_return_if_fail( menu );
821
822     fgStructure.CurrentMenu = menu;
823 }
824
825 /*
826  * Adds a menu entry to the bottom of the current menu
827  */
828 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
829 {
830     SFG_MenuEntry* menuEntry;
831     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
832     menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
833     freeglut_return_if_fail( fgStructure.CurrentMenu );
834
835     menuEntry->Text = strdup( label );
836     menuEntry->ID   = value;
837
838     /* Have the new menu entry attached to the current menu */
839     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
840
841     fghCalculateMenuBoxSize( );
842 }
843
844 /*
845  * Add a sub menu to the bottom of the current menu
846  */
847 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
848 {
849     SFG_MenuEntry *menuEntry;
850     SFG_Menu *subMenu;
851
852     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
853     menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
854     subMenu = fgMenuByID( subMenuID );
855
856     freeglut_return_if_fail( fgStructure.CurrentMenu );
857     freeglut_return_if_fail( subMenu );
858
859     menuEntry->Text    = strdup( label );
860     menuEntry->SubMenu = subMenu;
861     menuEntry->ID      = -1;
862
863     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
864     fghCalculateMenuBoxSize( );
865 }
866
867 /*
868  * Changes the specified menu item in the current menu into a menu entry
869  */
870 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
871 {
872     SFG_MenuEntry* menuEntry = NULL;
873
874     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
875     freeglut_return_if_fail( fgStructure.CurrentMenu );
876
877     /* Get n-th menu entry in the current menu, starting from one: */
878     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
879
880     freeglut_return_if_fail( menuEntry );
881
882     /* We want it to become a normal menu entry, so: */
883     if( menuEntry->Text )
884         free( menuEntry->Text );
885
886     menuEntry->Text    = strdup( label );
887     menuEntry->ID      = value;
888     menuEntry->SubMenu = NULL;
889     fghCalculateMenuBoxSize( );
890 }
891
892 /*
893  * Changes the specified menu item in the current menu into a sub-menu trigger.
894  */
895 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
896                                      int subMenuID )
897 {
898     SFG_Menu*      subMenu;
899     SFG_MenuEntry* menuEntry;
900
901     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
902     subMenu = fgMenuByID( subMenuID );
903     menuEntry = NULL;
904
905     freeglut_return_if_fail( fgStructure.CurrentMenu );
906     freeglut_return_if_fail( subMenu );
907
908     /* Get n-th menu entry in the current menu, starting from one: */
909     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
910
911     freeglut_return_if_fail( menuEntry );
912
913     /* We want it to become a sub menu entry, so: */
914     if( menuEntry->Text )
915         free( menuEntry->Text );
916
917     menuEntry->Text    = strdup( label );
918     menuEntry->SubMenu = subMenu;
919     menuEntry->ID      = -1;
920     fghCalculateMenuBoxSize( );
921 }
922
923 /*
924  * Removes the specified menu item from the current menu
925  */
926 void FGAPIENTRY glutRemoveMenuItem( int item )
927 {
928     SFG_MenuEntry* menuEntry;
929
930     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
931     freeglut_return_if_fail( fgStructure.CurrentMenu );
932
933     /* Get n-th menu entry in the current menu, starting from one: */
934     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
935
936     freeglut_return_if_fail( menuEntry );
937
938     fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
939     if ( menuEntry->Text )
940       free( menuEntry->Text );
941
942     free( menuEntry );
943     fghCalculateMenuBoxSize( );
944 }
945
946 /*
947  * Attaches a menu to the current window
948  */
949 void FGAPIENTRY glutAttachMenu( int button )
950 {
951     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
952
953     freeglut_return_if_fail( fgStructure.CurrentWindow );
954     freeglut_return_if_fail( fgStructure.CurrentMenu );
955
956     freeglut_return_if_fail( button >= 0 );
957     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
958
959     fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
960 }
961
962 /*
963  * Detaches a menu from the current window
964  */
965 void FGAPIENTRY glutDetachMenu( int button )
966 {
967     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
968
969     freeglut_return_if_fail( fgStructure.CurrentWindow );
970     freeglut_return_if_fail( fgStructure.CurrentMenu );
971
972     freeglut_return_if_fail( button >= 0 );
973     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
974
975     fgStructure.CurrentWindow->Menu[ button ] = NULL;
976 }
977
978 /*
979  * A.Donev: Set and retrieve the menu's user data
980  */
981 void* FGAPIENTRY glutGetMenuData( void )
982 {
983     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
984     return fgStructure.CurrentMenu->UserData;
985 }
986
987 void FGAPIENTRY glutSetMenuData(void* data)
988 {
989     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
990     fgStructure.CurrentMenu->UserData=data;
991 }
992
993 /*** END OF FILE ***/