added glutPositionFunc callback, now just need to implement so it does
[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     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(&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         /* XXX Posting a requisite Redisplay seems bogus. */
671         window->State.Redisplay = GL_TRUE;
672         fghActivateMenu( window, button );
673         return GL_TRUE;
674     }
675
676     return GL_FALSE;
677 }
678
679 /*
680  * Deactivates a menu pointed by the function argument.
681  */
682 void fgDeactivateMenu( SFG_Window *window )
683 {
684     SFG_Window *parent_window = NULL;
685     SFG_Menu* menu;
686     SFG_MenuEntry *menuEntry;
687
688     /* Did we find an active window? */
689     freeglut_return_if_fail( window );
690     /* Check if there is an active menu attached to this window... */
691     menu = window->ActiveMenu;
692     freeglut_return_if_fail( menu );
693
694     parent_window = menu->ParentWindow;
695
696     /* Hide the present menu's window */
697     fgSetWindow( menu->Window );
698     glutHideWindow( );
699
700     /* Forget about having that menu active anymore, now: */
701     menu->Window->ActiveMenu = NULL;
702     menu->ParentWindow->ActiveMenu = NULL;
703     fghSetMenuParentWindow ( NULL, menu );
704     menu->IsActive = GL_FALSE;
705     menu->ActiveEntry = NULL;
706
707     fgState.ActiveMenus--;
708
709     /* Hide all submenu windows, and the root menu's window. */
710     for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
711           menuEntry;
712           menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
713     {
714         menuEntry->IsActive = GL_FALSE;
715
716         /* Is that an active submenu by any chance? */
717         if( menuEntry->SubMenu )
718             fghDeactivateSubMenu( menuEntry );
719     }
720
721     fgSetWindow ( parent_window ) ;
722
723     /* Menu status callback */
724     if (fgState.MenuStateCallback || fgState.MenuStatusCallback)
725     {
726         fgStructure.CurrentMenu = menu;
727         fgStructure.CurrentWindow = parent_window;
728         if (fgState.MenuStateCallback)
729             fgState.MenuStateCallback(GLUT_MENU_NOT_IN_USE);
730         if (fgState.MenuStatusCallback)
731         {
732             /* Get cursor position on screen and convert to relative to parent_window's client area */
733             SFG_XYUse mouse_pos;
734             fghPlatformGetCursorPos(&mouse_pos);
735             
736             mouse_pos.X -= glutGet( GLUT_WINDOW_X );
737             mouse_pos.Y -= glutGet( GLUT_WINDOW_Y );
738
739             fgState.MenuStatusCallback(GLUT_MENU_NOT_IN_USE, mouse_pos.X, mouse_pos.Y);
740         }
741     }
742 }
743
744 /*
745  * Recalculates current menu's box size
746  */
747 void fghCalculateMenuBoxSize( void )
748 {
749     SFG_MenuEntry* menuEntry;
750     int width = 0, height = 0;
751
752     /* Make sure there is a current menu set */
753     freeglut_return_if_fail( fgStructure.CurrentMenu );
754
755     /* The menu's box size depends on the menu entries: */
756     for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
757          menuEntry;
758          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
759     {
760         /* Update the menu entry's width value */
761         menuEntry->Width = glutBitmapLength(
762             FREEGLUT_MENU_FONT,
763             (unsigned char *)menuEntry->Text
764         );
765
766         /*
767          * If the entry is a submenu, then it needs to be wider to
768          * accomodate the arrow. JCJ 31 July 2003
769          */
770         if (menuEntry->SubMenu )
771             menuEntry->Width += glutBitmapLength(
772                 FREEGLUT_MENU_FONT,
773                 (unsigned char *)"_"
774             );
775
776         /* Check if it's the biggest we've found */
777         if( menuEntry->Width > width )
778             width = menuEntry->Width;
779
780         height += FREEGLUT_MENU_HEIGHT;
781     }
782
783     /* Store the menu's box size now: */
784     fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
785     fgStructure.CurrentMenu->Width  = width  + 4 * FREEGLUT_MENU_BORDER;
786 }
787
788
789 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
790
791 /*
792  * Creates a new menu object, adding it to the freeglut structure
793  */
794 int FGAPIENTRY glutCreateMenu( FGCBMenu callback )
795 {
796     /* The menu object creation code resides in freeglut_structure.c */
797     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
798     if (fgGetActiveMenu())
799         fgError("Menu manipulation not allowed while menus in use.");
800
801     return fgCreateMenu( callback )->ID;
802 }
803
804 /*
805  * Destroys a menu object, removing all references to it
806  */
807 void FGAPIENTRY glutDestroyMenu( int menuID )
808 {
809     SFG_Menu* menu;
810
811     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
812     menu = fgMenuByID( menuID );
813
814     freeglut_return_if_fail( menu );
815     if (fgGetActiveMenu())
816         fgError("Menu manipulation not allowed while menus in use.");
817
818     /* The menu object destruction code resides in freeglut_structure.c */
819     fgDestroyMenu( menu );
820 }
821
822 /*
823  * Returns the ID number of the currently active menu
824  */
825 int FGAPIENTRY glutGetMenu( void )
826 {
827     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
828
829     if( fgStructure.CurrentMenu )
830         return fgStructure.CurrentMenu->ID;
831
832     return 0;
833 }
834
835 /*
836  * Sets the current menu given its menu ID
837  */
838 void FGAPIENTRY glutSetMenu( int menuID )
839 {
840     SFG_Menu* menu;
841
842     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
843     menu = fgMenuByID( menuID );
844
845     freeglut_return_if_fail( menu );
846
847     fgStructure.CurrentMenu = menu;
848 }
849
850 /*
851  * Adds a menu entry to the bottom of the current menu
852  */
853 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
854 {
855     SFG_MenuEntry* menuEntry;
856     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
857     menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
858
859     freeglut_return_if_fail( fgStructure.CurrentMenu );
860     if (fgGetActiveMenu())
861         fgError("Menu manipulation not allowed while menus in use.");
862
863     menuEntry->Text = strdup( label );
864     menuEntry->ID   = value;
865
866     /* Have the new menu entry attached to the current menu */
867     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
868
869     fghCalculateMenuBoxSize( );
870 }
871
872 /*
873  * Add a sub menu to the bottom of the current menu
874  */
875 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
876 {
877     SFG_MenuEntry *menuEntry;
878     SFG_Menu *subMenu;
879
880     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
881     menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
882     subMenu = fgMenuByID( subMenuID );
883
884     freeglut_return_if_fail( fgStructure.CurrentMenu );
885     if (fgGetActiveMenu())
886         fgError("Menu manipulation not allowed while menus in use.");
887
888     freeglut_return_if_fail( subMenu );
889
890     menuEntry->Text    = strdup( label );
891     menuEntry->SubMenu = subMenu;
892     menuEntry->ID      = -1;
893
894     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
895     fghCalculateMenuBoxSize( );
896 }
897
898 /*
899  * Changes the specified menu item in the current menu into a menu entry
900  */
901 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
902 {
903     SFG_MenuEntry* menuEntry = NULL;
904
905     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
906
907     freeglut_return_if_fail( fgStructure.CurrentMenu );
908     if (fgGetActiveMenu())
909         fgError("Menu manipulation not allowed while menus in use.");
910
911     /* Get n-th menu entry in the current menu, starting from one: */
912     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
913
914     freeglut_return_if_fail( menuEntry );
915
916     /* We want it to become a normal menu entry, so: */
917     if( menuEntry->Text )
918         free( menuEntry->Text );
919
920     menuEntry->Text    = strdup( label );
921     menuEntry->ID      = value;
922     menuEntry->SubMenu = NULL;
923     fghCalculateMenuBoxSize( );
924 }
925
926 /*
927  * Changes the specified menu item in the current menu into a sub-menu trigger.
928  */
929 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
930                                      int subMenuID )
931 {
932     SFG_Menu*      subMenu;
933     SFG_MenuEntry* menuEntry;
934
935     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
936
937     freeglut_return_if_fail( fgStructure.CurrentMenu );
938     if (fgGetActiveMenu())
939         fgError("Menu manipulation not allowed while menus in use.");
940
941     /* Get handle to sub menu */
942     subMenu = fgMenuByID( subMenuID );
943     menuEntry = NULL;
944     freeglut_return_if_fail( subMenu );
945
946     /* Get n-th menu entry in the current menu, starting from one: */
947     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
948
949     freeglut_return_if_fail( menuEntry );
950
951     /* We want it to become a sub menu entry, so: */
952     if( menuEntry->Text )
953         free( menuEntry->Text );
954
955     menuEntry->Text    = strdup( label );
956     menuEntry->SubMenu = subMenu;
957     menuEntry->ID      = -1;
958     fghCalculateMenuBoxSize( );
959 }
960
961 /*
962  * Removes the specified menu item from the current menu
963  */
964 void FGAPIENTRY glutRemoveMenuItem( int item )
965 {
966     SFG_MenuEntry* menuEntry;
967
968     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
969
970     freeglut_return_if_fail( fgStructure.CurrentMenu );
971     if (fgGetActiveMenu())
972         fgError("Menu manipulation not allowed while menus in use.");
973
974     /* Get n-th menu entry in the current menu, starting from one: */
975     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
976
977     freeglut_return_if_fail( menuEntry );
978
979     fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
980     if ( menuEntry->Text )
981       free( menuEntry->Text );
982
983     free( menuEntry );
984     fghCalculateMenuBoxSize( );
985 }
986
987 /*
988  * Attaches a menu to the current window
989  */
990 void FGAPIENTRY glutAttachMenu( int button )
991 {
992     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
993
994     freeglut_return_if_fail( fgStructure.CurrentWindow );
995
996     freeglut_return_if_fail( fgStructure.CurrentMenu );
997     if (fgGetActiveMenu())
998         fgError("Menu manipulation not allowed while menus in use.");
999
1000     freeglut_return_if_fail( button >= 0 );
1001     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
1002
1003     fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
1004 }
1005
1006 /*
1007  * Detaches a menu from the current window
1008  */
1009 void FGAPIENTRY glutDetachMenu( int button )
1010 {
1011     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
1012
1013     freeglut_return_if_fail( fgStructure.CurrentWindow );
1014
1015     freeglut_return_if_fail( fgStructure.CurrentMenu );
1016     if (fgGetActiveMenu())
1017         fgError("Menu manipulation not allowed while menus in use.");
1018
1019     freeglut_return_if_fail( button >= 0 );
1020     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
1021
1022     fgStructure.CurrentWindow->Menu[ button ] = NULL;
1023 }
1024
1025 /*
1026  * A.Donev: Set and retrieve the menu's user data
1027  */
1028 void* FGAPIENTRY glutGetMenuData( void )
1029 {
1030     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
1031     return fgStructure.CurrentMenu->UserData;
1032 }
1033
1034 void FGAPIENTRY glutSetMenuData(void* data)
1035 {
1036     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
1037     fgStructure.CurrentMenu->UserData=data;
1038 }
1039
1040 /*** END OF FILE ***/