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