A first pass over freeglut_menu.c to bring it in line with the rest of
[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 "../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
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
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     /*
121      * First of all check any of the active sub menus...
122      */
123     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
124          menuEntry;
125          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
126     {
127         if( menuEntry->SubMenu && ( menuEntry->IsActive == TRUE ) )
128         {
129             /*
130              * OK, have the sub-menu checked, too. If it returns TRUE, it
131              * will mean that it caught the mouse cursor and we do not need
132              * to regenerate the activity list, and so our parents do...
133              */
134             GLboolean return_status = fghCheckMenuStatus( window,
135                                                           menuEntry->SubMenu );
136
137             /*
138              * Reactivate the submenu as the checkMenuStatus may have turned
139              * it off if the mouse is in its parent menu entry.
140              */
141             menuEntry->SubMenu->IsActive = TRUE;
142             if ( return_status == TRUE )
143                 return TRUE;
144         }
145     }
146
147     /*
148      * That much about our sub menus, let's get to checking the current menu:
149      */
150     x = window->State.MouseX;
151     y = window->State.MouseY;
152
153     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
154          menuEntry;
155          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
156         menuEntry->IsActive = FALSE;
157
158     menu->IsActive = FALSE;
159
160     /*
161      * Check if the mouse cursor is contained within the current menu box
162      */
163     if( ( x >= FREEGLUT_MENU_BORDER ) &&
164         ( x < menu->Width  - FREEGLUT_MENU_BORDER ) &&
165         ( y >= FREEGLUT_MENU_BORDER ) &&
166         ( y < menu->Height - FREEGLUT_MENU_BORDER ) &&
167         ( window == menu->Window ) )
168     {
169         int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
170
171         /*
172          * The mouse cursor is somewhere over our box, check it out.
173          */
174         menuEntry = fghFindMenuEntry( menu, menuID + 1 );
175         assert( menuEntry );
176
177         menuEntry->IsActive = TRUE;
178         menuEntry->Ordinal = menuID;
179
180         /*
181          * If this is not the same as the last active menu entry, deactivate
182          * the previous entry.  Specifically, if the previous active entry
183          * was a submenu then deactivate it.
184          */
185         if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
186         {
187             if( menu->ActiveEntry->SubMenu )
188                 fgDeactivateSubMenu( menu->ActiveEntry );
189         }
190
191         menu->ActiveEntry = menuEntry;
192         menu->IsActive = TRUE;
193
194         /*
195          * OKi, we have marked that entry as active, but it would be also
196          * nice to have its contents updated, in case it's a sub menu.
197          * Also, ignore the return value of the check function:
198          */
199         if( menuEntry->SubMenu )
200         {
201             if ( ! menuEntry->SubMenu->IsActive )
202             {
203                 SFG_Window *current_window = fgStructure.Window;
204
205                 /*
206                  * Set up the initial menu position now...
207                  */
208                 menuEntry->SubMenu->IsActive = TRUE;
209
210                 /*
211                  * Set up the initial submenu position now:
212                  */
213                 menuEntry->SubMenu->X = menu->X + menu->Width;
214                 menuEntry->SubMenu->Y = menu->Y +
215                     menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
216
217                 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width >
218                     glutGet( GLUT_SCREEN_WIDTH ) )
219                     menuEntry->SubMenu->X = menu->X -
220                         menuEntry->SubMenu->Width;
221
222                 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height >
223                     glutGet( GLUT_SCREEN_HEIGHT ) )
224                     menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
225                                                FREEGLUT_MENU_HEIGHT -
226                                                2 * FREEGLUT_MENU_BORDER );
227
228                 fgSetWindow( menuEntry->SubMenu->Window );
229                 glutPositionWindow( menuEntry->SubMenu->X,
230                                     menuEntry->SubMenu->Y );
231                 glutReshapeWindow( menuEntry->SubMenu->Width,
232                                    menuEntry->SubMenu->Height );
233                 glutPopWindow( );
234                 glutShowWindow( );
235                 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
236                 fgSetWindow( current_window );
237             }
238
239             fghCheckMenuStatus( window, menuEntry->SubMenu );
240
241             /*
242              * Activate it because its parent entry is active
243              */
244             menuEntry->SubMenu->IsActive = TRUE;
245         }
246
247         /*
248          * Report back that we have caught the menu cursor
249          */
250         return TRUE;
251     }
252
253     /*
254      * Looks like the menu cursor is somewhere else...
255      */
256     return FALSE;
257 }
258
259 /*
260  * Displays a menu box and all of its submenus (if they are active)
261  */
262 static void fghDisplayMenuBox( SFG_Menu* menu )
263 {
264     SFG_MenuEntry *menuEntry;
265     int i;
266     int border = FREEGLUT_MENU_BORDER;
267
268     /*
269      * Have the menu box drawn first. The +- values are
270      * here just to make it more nice-looking...
271      */
272     /* a non-black dark version of the below. */
273     glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
274     glBegin( GL_QUAD_STRIP );
275         glVertex2i( menu->Width       , 0                  );
276         glVertex2i( menu->Width-border,              border);
277         glVertex2i( 0                 , 0                  );
278         glVertex2i(             border,              border);
279         glVertex2i( 0                 , menu->Height       );
280         glVertex2i(             border, menu->Height-border);
281     glEnd( );
282
283     /* a non-black dark version of the below. */
284     glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
285     glBegin( GL_QUAD_STRIP );
286         glVertex2i( 0                 , menu->Height       );
287         glVertex2i(             border, menu->Height-border);
288         glVertex2i( menu->Width       , menu->Height       );
289         glVertex2i( menu->Width-border, menu->Height-border);
290         glVertex2i( menu->Width       , 0                  );
291         glVertex2i( menu->Width-border,              border);
292     glEnd( );
293
294     glColor4fv( menu_pen_back ) ;
295     glBegin( GL_QUADS );
296         glVertex2i(             border,              border);
297         glVertex2i( menu->Width-border,              border);
298         glVertex2i( menu->Width-border, menu->Height-border);
299         glVertex2i(             border, menu->Height-border);
300     glEnd( );
301
302     /*
303      * Check if any of the submenus is currently active...
304      */
305     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
306          menuEntry;
307          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
308     {
309         /*
310          * Has the menu been marked as active, maybe?
311          */
312         if( menuEntry->IsActive == TRUE )
313         {
314             /*
315              * That's truly right, and we need to have it highlighted.
316              * There is an assumption that mouse cursor didn't move
317              * since the last check of menu activity state:
318              */
319             int menuID = menuEntry->Ordinal;
320
321             /*
322              * So have the highlight drawn...
323              */
324             glColor4fv( menu_pen_hback );
325             glBegin( GL_QUADS );
326                 glVertex2i( FREEGLUT_MENU_BORDER,
327                             (menuID + 0)*FREEGLUT_MENU_HEIGHT +
328                             FREEGLUT_MENU_BORDER );
329                 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER,
330                             (menuID + 0)*FREEGLUT_MENU_HEIGHT +
331                             FREEGLUT_MENU_BORDER );
332                 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER,
333                             (menuID + 1)*FREEGLUT_MENU_HEIGHT +
334                             FREEGLUT_MENU_BORDER );
335                 glVertex2i( FREEGLUT_MENU_BORDER,
336                             (menuID + 1)*FREEGLUT_MENU_HEIGHT +
337                             FREEGLUT_MENU_BORDER );
338             glEnd( );
339         }
340     }
341
342     /*
343      * Print the menu entries now...
344      */
345
346     glColor4fv( menu_pen_fore );
347
348     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i=0;
349          menuEntry;
350          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
351     {
352         /*
353          * If the menu entry is active, set the color to white
354          */
355         if( menuEntry->IsActive )
356             glColor4fv( menu_pen_hfore );
357
358         /*
359          * Move the raster into position...
360          */
361         /* Try to center the text - JCJ 31 July 2003*/
362         glRasterPos2i(
363             2 * FREEGLUT_MENU_BORDER,
364             (i + 1)*FREEGLUT_MENU_HEIGHT-(int)( FREEGLUT_MENU_HEIGHT*0.3 -
365                                                 FREEGLUT_MENU_BORDER )
366         );
367
368         /*
369          * Have the label drawn, character after character:
370          */
371         glutBitmapString( FREEGLUT_MENU_FONT, menuEntry->Text);
372
373         /*
374          * If it's a submenu, draw a right arrow
375          */
376         if( menuEntry->SubMenu )
377         {
378             int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
379             int x_base = menu->Width - 2 - width;
380             int y_base = i*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER;
381             glBegin( GL_TRIANGLES );
382                 glVertex2i( x_base, y_base + 2*FREEGLUT_MENU_BORDER);
383                 glVertex2i( menu->Width - 2, y_base +
384                             ( FREEGLUT_MENU_HEIGHT +
385                               FREEGLUT_MENU_BORDER) / 2 );
386                 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT -
387                             FREEGLUT_MENU_BORDER);
388             glEnd( );
389         }
390
391         /*
392          * If the menu entry is active, reset the color
393          */
394         if( menuEntry->IsActive )
395             glColor4fv( menu_pen_fore );
396     }
397
398     /*
399      * Now we are ready to check if any of our children needs to be redrawn:
400      */
401     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
402          menuEntry;
403          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
404     {
405         /*
406          * Is that an active sub menu by any case?
407          */
408         if( menuEntry->SubMenu && ( menuEntry->IsActive == TRUE ) )
409         {
410             /*
411              * Yeah, indeed. Have it redrawn now:
412              */
413             fgSetWindow( menuEntry->SubMenu->Window );
414             fghDisplayMenuBox( menuEntry->SubMenu );
415             fgSetWindow( menu->Window );
416         }
417     }
418 }
419
420 /*
421  * Private static function to set the parent window of a submenu and all
422  * of its submenus
423  */
424 static void fghSetSubmenuParentWindow ( SFG_Window *window, SFG_Menu *menu )
425 {
426     SFG_MenuEntry *menuEntry;
427
428     menu->ParentWindow = window;
429
430     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
431          menuEntry;
432          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
433         if ( menuEntry->SubMenu )
434             fghSetSubmenuParentWindow ( window, menuEntry->SubMenu ) ;
435 }
436
437
438 /*
439  * Displays the currently active menu for the current window
440  */
441 void fgDisplayMenu( void )
442 {
443     SFG_Window* window = fgStructure.Window;
444     SFG_Menu* menu = NULL;
445
446     /*
447      * Make sure there is a current window available
448      */
449     freeglut_assert_window;
450
451     /*
452      * Check if there is an active menu attached to this window...
453      */
454     menu = window->ActiveMenu;
455
456     /*
457      * Did we find an active menu?
458      */
459     freeglut_return_if_fail( menu );
460
461     fgSetWindow ( menu->Window );
462
463     /*
464      * Prepare the OpenGL state to do the rendering first:
465      */
466     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
467                   GL_POLYGON_BIT );
468
469     glDisable( GL_DEPTH_TEST );
470     glDisable( GL_TEXTURE_2D );
471     glDisable( GL_LIGHTING   );
472     glDisable( GL_CULL_FACE  );
473
474     /*
475      * We'll use an orthogonal projection matrix to draw the menu:
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     /*
487      * Model-view matix gets reset to identity:
488      */
489     glMatrixMode( GL_MODELVIEW );
490     glPushMatrix();
491     glLoadIdentity();
492
493     /*
494      * First of all, have the exact menu status check:
495      */
496     fghCheckMenuStatus( window, menu );
497
498     /*
499      * The status has been updated and we're ready to have the menu drawn now:
500      */
501     fghDisplayMenuBox( menu );
502
503     /*
504      * Restore the old OpenGL settings now
505      */
506     glPopAttrib( );
507
508     glMatrixMode( GL_PROJECTION );
509     glPopMatrix( );
510     glMatrixMode( GL_MODELVIEW );
511     glPopMatrix( );
512
513     glutSwapBuffers( );
514
515     /*
516      * Restore the current window
517      */
518     fgSetWindow ( window );
519 }
520
521 /*
522  * Activates a menu pointed by the function argument
523  */
524 void fgActivateMenu( SFG_Window* window, int button )
525 {
526     /*
527      * We'll be referencing this menu a lot, so remember its address:
528      */
529     SFG_Menu* menu = window->Menu[ button ];
530
531     /*
532      * Mark the menu as active, so that it gets displayed:
533      */
534     window->ActiveMenu = menu;
535     menu->IsActive = TRUE;
536     fgState.ActiveMenus++;
537
538     /*
539      * Set up the initial menu position now:
540      */
541
542     menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
543     menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
544
545     if( menu->X + menu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
546         menu->X -=menu->Width;
547
548     if( menu->Y + menu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
549         menu->Y -=menu->Height;
550
551     fgSetWindow( menu->Window );
552     glutPositionWindow( menu->X, menu->Y );
553     glutReshapeWindow( menu->Width, menu->Height );
554     glutPopWindow( );
555     glutShowWindow( );
556     menu->Window->ActiveMenu = menu;
557 }
558
559 /*
560  * Check whether an active menu absorbs a mouse click
561  */
562 GLboolean fgCheckActiveMenu ( SFG_Window *window, SFG_Menu *menu )
563 {
564     /*
565      * Near as I can tell, this is the active menu behaviour:
566      *  - Down-click any button outside the menu, menu active:
567      *    deactivate the menu
568      *  - Down-click any button inside the menu, menu active:
569      *    select the menu entry and deactivate the menu
570      *  - Up-click the menu button outside the menu, menu active:
571      *    nothing happens
572      *  - Up-click the menu button inside the menu, menu active:
573      *    select the menu entry and deactivate the menu
574      * Since menus can have submenus, we need to check this recursively.
575      */
576   return fghCheckMenuStatus( window, menu );
577 }
578
579 /*
580  * Function to check for menu entry selection on menu deactivation
581  */
582 void fgExecuteMenuCallback( SFG_Menu* menu )
583 {
584     SFG_MenuEntry *menuEntry;
585
586     /*
587      * First of all check any of the active sub menus...
588      */
589     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
590          menuEntry;
591          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
592     {
593         /*
594          * Is this menu entry active?
595          */
596         if( menuEntry->IsActive == TRUE )
597         {
598             /*
599              * If there is not a sub menu, execute the menu callback and
600              * return...
601              */
602             if( !( menuEntry->SubMenu ) )
603             {
604                 /*
605                  * ...certainly given that there is one...
606                  */
607                 if( menu->Callback )
608                     menu->Callback( menuEntry->ID );
609
610                 return;
611             }
612
613             /*
614              * Otherwise recurse into the submenu.
615              */
616             fgExecuteMenuCallback( menuEntry->SubMenu );
617
618             /*
619              * There is little sense in dwelling the search on
620              */
621             return;
622         }
623     }
624 }
625
626 /*
627  * Deactivates a menu pointed by the function argument.
628  */
629 void fgDeactivateMenu( SFG_Window *window )
630 {
631     SFG_Window *current_window = fgStructure.Window;
632
633     /*
634      * Check if there is an active menu attached to this window...
635      */
636     SFG_Menu* menu = window->ActiveMenu;
637     SFG_MenuEntry *menuEntry;
638
639     /*
640      * Did we find an active window?
641      */
642     freeglut_return_if_fail( menu );
643
644     /*
645      * Hide the present menu's window
646      */
647     fgSetWindow( menu->Window );
648     glutHideWindow( );
649
650     /*
651      * Forget about having that menu active anymore, now:
652      */
653     menu->Window->ActiveMenu = NULL;
654     menu->ParentWindow->ActiveMenu = NULL;
655     menu->IsActive = FALSE;
656
657     fgState.ActiveMenus--;
658
659     /*
660      * Hide all submenu windows, and the root menu's window.
661      */
662     for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
663           menuEntry;
664           menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
665     {
666         /*
667          * Is that an active submenu by any case?
668          */
669         if( menuEntry->SubMenu )
670             fgDeactivateSubMenu( menuEntry );
671     }
672
673     fgSetWindow ( current_window ) ;
674 }
675
676 /*
677  * Deactivates a menu pointed by the function argument.
678  */
679 void fgDeactivateSubMenu( SFG_MenuEntry *menuEntry )
680 {
681     SFG_Window *current_window = fgStructure.Window;
682     SFG_MenuEntry *subMenuIter;
683     /*
684      * Hide the present menu's window
685      */
686     fgSetWindow( menuEntry->SubMenu->Window );
687     glutHideWindow( );
688
689     /*
690      * Forget about having that menu active anymore, now:
691      */
692     menuEntry->SubMenu->Window->ActiveMenu = NULL;
693     menuEntry->SubMenu->IsActive = FALSE;
694
695     /*
696      * Hide all submenu windows, and the root menu's window.
697      */
698     for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
699           subMenuIter;
700           subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
701     {
702         /*
703          * Is that an active submenu by any case?
704          */
705         if( subMenuIter->SubMenu )
706             fgDeactivateSubMenu( subMenuIter );
707     }
708
709     fgSetWindow( current_window );
710 }
711
712 /*
713  * Recalculates current menu's box size
714  */
715 void fghCalculateMenuBoxSize( void )
716 {
717     SFG_MenuEntry* menuEntry;
718     int width = 0, height = 0;
719
720     /*
721      * Make sure there is a current menu set
722      */
723     freeglut_assert_ready;
724     freeglut_return_if_fail( fgStructure.Menu );
725
726     /*
727      * The menu's box size depends on the menu entries:
728      */
729     for( menuEntry = (SFG_MenuEntry *)fgStructure.Menu->Entries.First;
730          menuEntry;
731          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
732     {
733         /*
734          * Update the menu entry's width value
735          */
736         menuEntry->Width = glutBitmapLength( FREEGLUT_MENU_FONT,
737                                              menuEntry->Text );
738
739         /*
740          * If the entry is a submenu, then it needs to be wider to
741          * accomodate the arrow. JCJ 31 July 2003
742          */
743         if (menuEntry->SubMenu )
744             menuEntry->Width += glutBitmapLength( FREEGLUT_MENU_FONT, "_" );
745
746         /*
747          * Check if it's the biggest we've found
748          */
749         if( menuEntry->Width > width )
750             width = menuEntry->Width;
751
752         height += FREEGLUT_MENU_HEIGHT;
753     }
754
755     /*
756      * Store the menu's box size now:
757      */
758     fgStructure.Menu->Height = height + 2 * FREEGLUT_MENU_BORDER;
759     fgStructure.Menu->Width  = width  + 4 * FREEGLUT_MENU_BORDER;
760 }
761
762
763 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
764
765 /*
766  * Creates a new menu object, adding it to the freeglut structure
767  */
768 int FGAPIENTRY glutCreateMenu( void (* callback)( int ) )
769 {
770     /*
771      * The menu object creation code resides in freeglut_structure.c
772      */
773     return fgCreateMenu( callback )->ID;
774 }
775
776 /*
777  * Destroys a menu object, removing all references to it
778  */
779 void FGAPIENTRY glutDestroyMenu( int menuID )
780 {
781     SFG_Menu* menu = fgMenuByID( menuID );
782
783     freeglut_assert_ready;
784     freeglut_return_if_fail( menu );
785
786     /*
787      * The menu object destruction code resides in freeglut_structure.c
788      */
789     fgDestroyMenu( menu );
790 }
791
792 /*
793  * Returns the ID number of the currently active menu
794  */
795 int FGAPIENTRY glutGetMenu( void )
796 {
797     freeglut_assert_ready;
798
799     if( fgStructure.Menu )
800         return fgStructure.Menu->ID;
801
802     return 0;
803 }
804
805 /*
806  * Sets the current menu given its menu ID
807  */
808 void FGAPIENTRY glutSetMenu( int menuID )
809 {
810     SFG_Menu* menu = fgMenuByID( menuID );
811
812     freeglut_assert_ready;
813     freeglut_return_if_fail( menu );
814
815     fgStructure.Menu = menu;
816 }
817
818 /*
819  * Adds a menu entry to the bottom of the current menu
820  */
821 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
822 {
823     SFG_MenuEntry* menuEntry =
824         (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
825
826     freeglut_assert_ready;
827     freeglut_return_if_fail( fgStructure.Menu );
828
829     menuEntry->Text = strdup( label );
830     menuEntry->ID   = value;
831
832     /*
833      * Have the new menu entry attached to the current menu
834      */
835     fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
836
837     fghCalculateMenuBoxSize( );
838 }
839
840 /*
841  * Add a sub menu to the bottom of the current menu
842  */
843 void FGAPIENTRY glutAddSubMenu( const char* label, int subMenuID )
844 {
845     SFG_MenuEntry* menuEntry =
846         (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
847     SFG_Menu*      subMenu = fgMenuByID( subMenuID );
848
849     freeglut_assert_ready;
850     freeglut_return_if_fail( fgStructure.Menu );
851     freeglut_return_if_fail( subMenu );
852
853     menuEntry->Text = strdup( label );
854     menuEntry->SubMenu = subMenu;
855     menuEntry->ID      = -1;
856
857     /*
858      * Make the submenu's parent window be the menu's parent window
859      */
860     fghSetSubmenuParentWindow( fgStructure.Menu->ParentWindow, subMenu );
861
862     fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
863     fghCalculateMenuBoxSize( );
864 }
865
866 /*
867  * Changes the specified menu item in the current menu into a menu entry
868  */
869 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
870 {
871     SFG_MenuEntry* menuEntry = NULL;
872
873     freeglut_assert_ready;
874     freeglut_return_if_fail( fgStructure.Menu );
875
876     /*
877      * Get n-th menu entry in the current menu, starting from one:
878      */
879     menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
880
881     freeglut_return_if_fail( menuEntry );
882
883     /*
884      * We want it to become a normal menu entry, so:
885      */
886     if( menuEntry->Text )
887         free( menuEntry->Text );
888
889     menuEntry->Text = strdup( label );
890     menuEntry->ID      = value;
891     menuEntry->SubMenu = NULL;
892     fghCalculateMenuBoxSize( );
893 }
894
895 /*
896  * Changes the specified menu item in the current menu into a sub-menu trigger.
897  */
898 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID )
899 {
900     SFG_Menu*      subMenu = fgMenuByID( subMenuID );
901     SFG_MenuEntry* menuEntry = NULL;
902
903     freeglut_assert_ready;
904     freeglut_return_if_fail( fgStructure.Menu );
905     freeglut_return_if_fail( subMenu );
906
907     /*
908      * Get n-th menu entry in the current menu, starting from one:
909      */
910     menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
911
912     freeglut_return_if_fail( menuEntry );
913
914     /*
915      * We want it to become a sub menu entry, so:
916      */
917     if( menuEntry->Text )
918         free( menuEntry->Text );
919
920     menuEntry->Text = strdup( label );
921     menuEntry->SubMenu = subMenu;
922     menuEntry->ID      = -1;
923     fghCalculateMenuBoxSize( );
924 }
925
926 /*
927  * Removes the specified menu item from the current menu
928  */
929 void FGAPIENTRY glutRemoveMenuItem( int item )
930 {
931     SFG_MenuEntry* menuEntry;
932
933     freeglut_assert_ready;
934     freeglut_return_if_fail( fgStructure.Menu );
935
936     /*
937      * Get n-th menu entry in the current menu, starting from one:
938      */
939     menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
940
941     freeglut_return_if_fail( menuEntry );
942
943     fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
944     if ( menuEntry->Text )
945       free( menuEntry->Text );
946
947     free( menuEntry );
948     fghCalculateMenuBoxSize( );
949 }
950
951 /*
952  * Attaches a menu to the current window
953  */
954 void FGAPIENTRY glutAttachMenu( int button )
955 {
956     freeglut_assert_ready;
957
958     freeglut_return_if_fail( fgStructure.Window );
959     freeglut_return_if_fail( fgStructure.Menu );
960
961     freeglut_return_if_fail( button >= 0 );
962     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
963
964     fgStructure.Window->Menu[ button ] = fgStructure.Menu;
965
966     /*
967      * Make the parent window of the menu (and all submenus) the current window
968      */
969     fghSetSubmenuParentWindow ( fgStructure.Window, fgStructure.Menu ) ;
970 }
971
972 /*
973  * Detaches a menu from the current window
974  */
975 void FGAPIENTRY glutDetachMenu( int button )
976 {
977     freeglut_assert_ready;
978
979     freeglut_return_if_fail( fgStructure.Window );
980     freeglut_return_if_fail( fgStructure.Menu );
981
982     freeglut_return_if_fail( button >= 0 );
983     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
984
985     fgStructure.Window->Menu[ button ] = NULL;
986 }
987
988 /*
989  * A.Donev: Set and retrieve the menu's user data
990  */
991 void* FGAPIENTRY glutGetMenuData( void )
992 {
993     return fgStructure.Menu->UserData;
994 }
995
996 void FGAPIENTRY glutSetMenuData(void* data)
997 {
998     fgStructure.Menu->UserData=data;
999 }
1000
1001 /*** END OF FILE ***/