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