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