853c1b451509b84d14f5b3d3defa6b6720b1ca7f
[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(const SFG_Window *window, GLboolean client, 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     SFG_XYUse mouse_pos;
513
514     /* We'll be referencing this menu a lot, so remember its address: */
515     SFG_Menu* menu = window->Menu[ button ];
516     SFG_Window* current_window = fgStructure.CurrentWindow;
517
518     /* If the menu is already active in another window, deactivate it (and any submenus) there */
519     if ( menu->ParentWindow )
520       fgDeactivateMenu(menu->ParentWindow);
521
522     /* Mark the menu as active, so that it gets displayed: */
523     window->ActiveMenu = menu;
524     menu->IsActive = GL_TRUE;
525     fghSetMenuParentWindow ( window, menu );
526     fgState.ActiveMenus++;
527
528     /* Set up the initial menu position now: */
529     fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
530     fgSetWindow( window );
531     /* get mouse position on screen (window->State.MouseX and window->State.MouseY
532      * are relative to client area origin), and not easy to correct given that
533      * glutGet( GLUT_WINDOW_X ) and glutGet( GLUT_WINDOW_Y ) return relative to parent
534      * origin when looking at a child window
535      * for parent windows: window->State.MouseX + glutGet( GLUT_WINDOW_X ) == mouse_pos.X
536      */
537     fghPlatformGetCursorPos(NULL, GL_FALSE, &mouse_pos);
538     menu->X = mouse_pos.X;
539     menu->Y = mouse_pos.Y;
540
541     /* Make sure the whole menu is on the screen */
542     if( menu->X + menu->Width > max_x )
543         menu->X -=menu->Width;
544
545     if( menu->Y + menu->Height > max_y )
546     {
547         menu->Y -=menu->Height;
548         if( menu->Y < 0 )
549             menu->Y = 0;
550     }
551
552     /* Set position of mouse relative to top-left menu in menu's window state (could as well set 0 at creation time...) */
553     menu->Window->State.MouseX = mouse_pos.X - menu->X;
554     menu->Window->State.MouseY = mouse_pos.Y - menu->Y;
555
556     /* Menu status callback */
557     if (fgState.MenuStateCallback || fgState.MenuStatusCallback)
558     {
559         fgStructure.CurrentMenu = menu;
560         fgStructure.CurrentWindow = window;
561         if (fgState.MenuStateCallback)
562             fgState.MenuStateCallback(GLUT_MENU_IN_USE);
563         if (fgState.MenuStatusCallback)
564             /* window->State.MouseX and window->State.MouseY are relative to client area origin, as needed */
565             fgState.MenuStatusCallback(GLUT_MENU_IN_USE, window->State.MouseX, window->State.MouseY);
566     }
567
568     fgSetWindow( menu->Window );
569     glutPositionWindow( menu->X, menu->Y );
570     glutReshapeWindow( menu->Width, menu->Height );
571     glutPopWindow( );
572     glutShowWindow( );
573     menu->Window->ActiveMenu = menu;
574     fghCheckMenuStatus( menu );
575     fgSetWindow( current_window );
576 }
577
578 /*
579  * Update Highlight states of the menu
580  *
581  * Current mouse position is in menu->Window->State.MouseX/Y.
582  */
583 void fgUpdateMenuHighlight ( SFG_Menu *menu )
584 {
585     fghCheckMenuStatus( menu );
586 }
587
588 /*
589  * Check whether an active menu absorbs a mouse click
590  */
591 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
592                               int mouse_x, int mouse_y )
593 {
594     /*
595      * Near as I can tell, this is the menu behaviour:
596      *  - Down-click the menu button, menu not active:  activate
597      *    the menu with its upper left-hand corner at the mouse
598      *    location.
599      *  - Down-click any button outside the menu, menu active:
600      *    deactivate the menu
601      *  - Down-click any button inside the menu, menu active:
602      *    select the menu entry and deactivate the menu
603      *  - Up-click the menu button, menu not active:  nothing happens
604      *  - Up-click the menu button outside the menu, menu active:
605      *    nothing happens
606      *  - Up-click the menu button inside the menu, menu active:
607      *    select the menu entry and deactivate the menu
608      * Since menus can have submenus, we need to check this recursively.
609      */
610     if( window->ActiveMenu )
611     {
612         if( window == window->ActiveMenu->ParentWindow )
613         {
614             window->ActiveMenu->Window->State.MouseX =
615                                        mouse_x - window->ActiveMenu->X;
616             window->ActiveMenu->Window->State.MouseY =
617                                        mouse_y - window->ActiveMenu->Y;
618         }
619
620         /* In the menu, invoke the callback and deactivate the menu */
621         if( fghCheckMenuStatus( window->ActiveMenu ) )
622         {
623             /*
624              * Save the current window and menu and set the current
625              * window to the window whose menu this is
626              */
627             SFG_Window *save_window = fgStructure.CurrentWindow;
628             SFG_Menu *save_menu = fgStructure.CurrentMenu;
629             SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
630             fgSetWindow( parent_window );
631             fgStructure.CurrentMenu = window->ActiveMenu;
632
633             /* Execute the menu callback */
634             fghExecuteMenuCallback( window->ActiveMenu );
635             fgDeactivateMenu( parent_window );
636
637             /* Restore the current window and menu */
638             fgSetWindow( save_window );
639             fgStructure.CurrentMenu = save_menu;
640         }
641         else if( pressed )
642             /*
643              * Outside the menu, deactivate if it's a downclick
644              *
645              * A downclick outside of the interior of our freeglut windows
646              * is dealt with in the WM_KILLFOCUS handler of fgPlatformWindowProc
647              */
648         {
649             fgDeactivateMenu( window->ActiveMenu->ParentWindow );
650         }
651
652         /*
653          * XXX Why does an active menu require a redisplay at
654          * XXX this point?  If this can come out cleanly, then
655          * XXX it probably should do so; if not, a comment should
656          * XXX explain it.
657          */
658         if( ! window->IsMenu )
659             window->State.Redisplay = GL_TRUE;
660
661         return GL_TRUE;
662     }
663
664     /* No active menu, let's check whether we need to activate one. */
665     if( ( 0 <= button ) &&
666         ( FREEGLUT_MAX_MENUS > button ) &&
667         ( window->Menu[ button ] ) &&
668         pressed )
669     {
670         /* If mouseclick was outside the parent window, ignore. This can
671          * happen when another mouse button is already depressed and the
672          * window thus has mouse capture
673          */
674         if (window->State.MouseX>0 && window->State.MouseY>0 &&
675             window->State.MouseX<window->State.Width && window->State.MouseX<window->State.Height)
676         {
677             /* XXX Posting a requisite Redisplay seems bogus. */
678             window->State.Redisplay = GL_TRUE;
679             fghActivateMenu( window, button );
680             return GL_TRUE;
681         }
682     }
683
684     return GL_FALSE;
685 }
686
687 /*
688  * Deactivates a menu pointed by the function argument.
689  */
690 static SFG_Menu* menuDeactivating = NULL;
691 void fgDeactivateMenu( SFG_Window *window )
692 {
693     SFG_Window *parent_window = NULL;
694     SFG_Menu* menu;
695     SFG_MenuEntry *menuEntry;
696
697     /* Did we find an active window? */
698     freeglut_return_if_fail( window );
699     /* Check if there is an active menu attached to this window... */
700     menu = window->ActiveMenu;
701     freeglut_return_if_fail( menu );
702     /* Check if we are already deactivating this menu, abort in that case (glutHideWindow below can cause this function to be called again on the same menu..) */
703     if (menu==menuDeactivating)
704         return;
705     menuDeactivating = menu;
706
707     parent_window = menu->ParentWindow;
708
709     /* Hide the present menu's window */
710     fgSetWindow( menu->Window );
711     glutHideWindow( );
712
713     /* Forget about having that menu active anymore, now: */
714     menu->Window->ActiveMenu = NULL;
715     menu->ParentWindow->ActiveMenu = NULL;
716     fghSetMenuParentWindow ( NULL, menu );
717     menu->IsActive = GL_FALSE;
718     menu->ActiveEntry = NULL;
719
720     fgState.ActiveMenus--;
721
722     /* Hide all submenu windows, and the root menu's window. */
723     for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
724           menuEntry;
725           menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
726     {
727         menuEntry->IsActive = GL_FALSE;
728
729         /* Is that an active submenu by any chance? */
730         if( menuEntry->SubMenu )
731             fghDeactivateSubMenu( menuEntry );
732     }
733     /* Done deactivating menu */
734     menuDeactivating = NULL;
735
736     fgSetWindow ( parent_window ) ;
737
738     /* Menu status callback */
739     if (fgState.MenuStateCallback || fgState.MenuStatusCallback)
740     {
741         fgStructure.CurrentMenu = menu;
742         fgStructure.CurrentWindow = parent_window;
743         if (fgState.MenuStateCallback)
744             fgState.MenuStateCallback(GLUT_MENU_NOT_IN_USE);
745         if (fgState.MenuStatusCallback)
746         {
747             /* Get cursor position relative to parent_window's client area */
748             SFG_XYUse mouse_pos;
749             fghPlatformGetCursorPos(parent_window, GL_TRUE, &mouse_pos);
750
751             fgState.MenuStatusCallback(GLUT_MENU_NOT_IN_USE, mouse_pos.X, mouse_pos.Y);
752         }
753     }
754 }
755
756 /*
757  * Recalculates current menu's box size
758  */
759 void fghCalculateMenuBoxSize( void )
760 {
761     SFG_MenuEntry* menuEntry;
762     int width = 0, height = 0;
763
764     /* Make sure there is a current menu set */
765     freeglut_return_if_fail( fgStructure.CurrentMenu );
766
767     /* The menu's box size depends on the menu entries: */
768     for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
769          menuEntry;
770          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
771     {
772         /* Update the menu entry's width value */
773         menuEntry->Width = glutBitmapLength(
774             FREEGLUT_MENU_FONT,
775             (unsigned char *)menuEntry->Text
776         );
777
778         /*
779          * If the entry is a submenu, then it needs to be wider to
780          * accomodate the arrow. JCJ 31 July 2003
781          */
782         if (menuEntry->SubMenu )
783             menuEntry->Width += glutBitmapLength(
784                 FREEGLUT_MENU_FONT,
785                 (unsigned char *)"_"
786             );
787
788         /* Check if it's the biggest we've found */
789         if( menuEntry->Width > width )
790             width = menuEntry->Width;
791
792         height += FREEGLUT_MENU_HEIGHT;
793     }
794
795     /* Store the menu's box size now: */
796     fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
797     fgStructure.CurrentMenu->Width  = width  + 4 * FREEGLUT_MENU_BORDER;
798 }
799
800
801 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
802
803 /*
804  * Creates a new menu object, adding it to the freeglut structure
805  */
806 int FGAPIENTRY glutCreateMenu( FGCBMenu callback )
807 {
808     /* The menu object creation code resides in freeglut_structure.c */
809     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
810     if (fgGetActiveMenu())
811         fgError("Menu manipulation not allowed while menus in use.");
812
813     return fgCreateMenu( callback )->ID;
814 }
815
816 /*
817  * Destroys a menu object, removing all references to it
818  */
819 void FGAPIENTRY glutDestroyMenu( int menuID )
820 {
821     SFG_Menu* menu;
822
823     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
824     menu = fgMenuByID( menuID );
825
826     freeglut_return_if_fail( menu );
827     if (fgGetActiveMenu())
828         fgError("Menu manipulation not allowed while menus in use.");
829
830     /* The menu object destruction code resides in freeglut_structure.c */
831     fgDestroyMenu( menu );
832 }
833
834 /*
835  * Returns the ID number of the currently active menu
836  */
837 int FGAPIENTRY glutGetMenu( void )
838 {
839     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
840
841     if( fgStructure.CurrentMenu )
842         return fgStructure.CurrentMenu->ID;
843
844     return 0;
845 }
846
847 /*
848  * Sets the current menu given its menu ID
849  */
850 void FGAPIENTRY glutSetMenu( int menuID )
851 {
852     SFG_Menu* menu;
853
854     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
855     menu = fgMenuByID( menuID );
856
857     freeglut_return_if_fail( menu );
858
859     fgStructure.CurrentMenu = menu;
860 }
861
862 /*
863  * Adds a menu entry to the bottom of the current menu
864  */
865 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
866 {
867     SFG_MenuEntry* menuEntry;
868     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
869     menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
870
871     freeglut_return_if_fail( fgStructure.CurrentMenu );
872     if (fgGetActiveMenu())
873         fgError("Menu manipulation not allowed while menus in use.");
874
875     menuEntry->Text = strdup( label );
876     menuEntry->ID   = value;
877
878     /* Have the new menu entry attached to the current menu */
879     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
880
881     fghCalculateMenuBoxSize( );
882 }
883
884 /*
885  * Add a sub menu to the bottom of the current menu
886  */
887 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
888 {
889     SFG_MenuEntry *menuEntry;
890     SFG_Menu *subMenu;
891
892     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
893     menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
894     subMenu = fgMenuByID( subMenuID );
895
896     freeglut_return_if_fail( fgStructure.CurrentMenu );
897     if (fgGetActiveMenu())
898         fgError("Menu manipulation not allowed while menus in use.");
899
900     freeglut_return_if_fail( subMenu );
901
902     menuEntry->Text    = strdup( label );
903     menuEntry->SubMenu = subMenu;
904     menuEntry->ID      = -1;
905
906     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
907     fghCalculateMenuBoxSize( );
908 }
909
910 /*
911  * Changes the specified menu item in the current menu into a menu entry
912  */
913 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
914 {
915     SFG_MenuEntry* menuEntry = NULL;
916
917     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
918
919     freeglut_return_if_fail( fgStructure.CurrentMenu );
920     if (fgGetActiveMenu())
921         fgError("Menu manipulation not allowed while menus in use.");
922
923     /* Get n-th menu entry in the current menu, starting from one: */
924     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
925
926     freeglut_return_if_fail( menuEntry );
927
928     /* We want it to become a normal menu entry, so: */
929     if( menuEntry->Text )
930         free( menuEntry->Text );
931
932     menuEntry->Text    = strdup( label );
933     menuEntry->ID      = value;
934     menuEntry->SubMenu = NULL;
935     fghCalculateMenuBoxSize( );
936 }
937
938 /*
939  * Changes the specified menu item in the current menu into a sub-menu trigger.
940  */
941 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
942                                      int subMenuID )
943 {
944     SFG_Menu*      subMenu;
945     SFG_MenuEntry* menuEntry;
946
947     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
948
949     freeglut_return_if_fail( fgStructure.CurrentMenu );
950     if (fgGetActiveMenu())
951         fgError("Menu manipulation not allowed while menus in use.");
952
953     /* Get handle to sub menu */
954     subMenu = fgMenuByID( subMenuID );
955     menuEntry = NULL;
956     freeglut_return_if_fail( subMenu );
957
958     /* Get n-th menu entry in the current menu, starting from one: */
959     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
960
961     freeglut_return_if_fail( menuEntry );
962
963     /* We want it to become a sub menu entry, so: */
964     if( menuEntry->Text )
965         free( menuEntry->Text );
966
967     menuEntry->Text    = strdup( label );
968     menuEntry->SubMenu = subMenu;
969     menuEntry->ID      = -1;
970     fghCalculateMenuBoxSize( );
971 }
972
973 /*
974  * Removes the specified menu item from the current menu
975  */
976 void FGAPIENTRY glutRemoveMenuItem( int item )
977 {
978     SFG_MenuEntry* menuEntry;
979
980     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
981
982     freeglut_return_if_fail( fgStructure.CurrentMenu );
983     if (fgGetActiveMenu())
984         fgError("Menu manipulation not allowed while menus in use.");
985
986     /* Get n-th menu entry in the current menu, starting from one: */
987     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
988
989     freeglut_return_if_fail( menuEntry );
990
991     fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
992     if ( menuEntry->Text )
993       free( menuEntry->Text );
994
995     free( menuEntry );
996     fghCalculateMenuBoxSize( );
997 }
998
999 /*
1000  * Attaches a menu to the current window
1001  */
1002 void FGAPIENTRY glutAttachMenu( int button )
1003 {
1004     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
1005
1006     freeglut_return_if_fail( fgStructure.CurrentWindow );
1007
1008     freeglut_return_if_fail( fgStructure.CurrentMenu );
1009     if (fgGetActiveMenu())
1010         fgError("Menu manipulation not allowed while menus in use.");
1011
1012     freeglut_return_if_fail( button >= 0 );
1013     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
1014
1015     fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
1016 }
1017
1018 /*
1019  * Detaches a menu from the current window
1020  */
1021 void FGAPIENTRY glutDetachMenu( int button )
1022 {
1023     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
1024
1025     freeglut_return_if_fail( fgStructure.CurrentWindow );
1026
1027     freeglut_return_if_fail( fgStructure.CurrentMenu );
1028     if (fgGetActiveMenu())
1029         fgError("Menu manipulation not allowed while menus in use.");
1030
1031     freeglut_return_if_fail( button >= 0 );
1032     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
1033
1034     fgStructure.CurrentWindow->Menu[ button ] = NULL;
1035 }
1036
1037 /*
1038  * A.Donev: Set and retrieve the menu's user data
1039  */
1040 void* FGAPIENTRY glutGetMenuData( void )
1041 {
1042     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
1043     return fgStructure.CurrentMenu->UserData;
1044 }
1045
1046 void FGAPIENTRY glutSetMenuData(void* data)
1047 {
1048     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
1049     fgStructure.CurrentMenu->UserData=data;
1050 }
1051
1052 /*** END OF FILE ***/