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