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