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