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