4 * Pull-down menu creation and handling.
\r
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
\r
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
\r
8 * Creation date: Thu Dec 16 1999
\r
10 * Permission is hereby granted, free of charge, to any person obtaining a
\r
11 * copy of this software and associated documentation files (the "Software"),
\r
12 * to deal in the Software without restriction, including without limitation
\r
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
14 * and/or sell copies of the Software, and to permit persons to whom the
\r
15 * Software is furnished to do so, subject to the following conditions:
\r
17 * The above copyright notice and this permission notice shall be included
\r
18 * in all copies or substantial portions of the Software.
\r
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
\r
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
\r
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
28 #define FREEGLUT_BUILDING_LIB
\r
29 #include <GL/freeglut.h>
\r
30 #include "freeglut_internal.h"
\r
32 /* -- DEFINITIONS ---------------------------------------------------------- */
\r
35 * FREEGLUT_MENU_FONT can be any freeglut bitmapped font.
\r
36 * (Stroked fonts would not be out of the question, but we'd need to alter
\r
37 * code, since GLUT (hence freeglut) does not quite unify stroked and
\r
38 * bitmapped font handling.)
\r
39 * Old UNIX/X11 GLUT (BSD, UNIX, IRIX, LINUX, HPUX, ...) used a system
\r
40 * font best approximated by an 18-pixel HELVETICA, I think. MS-WINDOWS
\r
41 * GLUT used something closest to the 8x13 fixed-width font. (Old
\r
42 * GLUT apparently uses host-system menus rather than building its own.
\r
43 * freeglut is building its own menus from scratch.)
\r
45 * FREEGLUT_MENU_HEIGHT gives the height of ONE menu box. This should be
\r
46 * the distances between two adjacent menu entries. It should scale
\r
47 * automatically with the font choice, so you needn't alter it---unless you
\r
48 * use a stroked font.
\r
50 * FREEGLUT_MENU_BORDER says how many pixels to allow around the edge of a
\r
51 * menu. (It also seems to be the same as the number of pixels used as
\r
52 * a border around *items* to separate them from neighbors. John says
\r
53 * that that wasn't the original intent...if not, perhaps we need another
\r
54 * symbolic constant, FREEGLUT_MENU_ITEM_BORDER, or such.)
\r
56 #if TARGET_HOST_MS_WINDOWS
\r
57 #define FREEGLUT_MENU_FONT GLUT_BITMAP_8_BY_13
\r
59 #define FREEGLUT_MENU_FONT GLUT_BITMAP_HELVETICA_18
\r
62 #define FREEGLUT_MENU_HEIGHT (glutBitmapHeight(FREEGLUT_MENU_FONT) + \
\r
63 FREEGLUT_MENU_BORDER)
\r
64 #define FREEGLUT_MENU_BORDER 2
\r
68 * These variables are for rendering the freeglut menu items.
\r
70 * The choices are fore- and background, with and without h for Highlighting.
\r
71 * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
\r
72 * too. These variables should be stuffed into global state and initialized
\r
73 * via the glutInit*() system.
\r
75 #if TARGET_HOST_MS_WINDOWS
\r
76 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
\r
77 static float menu_pen_back [4] = {0.85f, 0.85f, 0.85f, 1.0f};
\r
78 static float menu_pen_hfore [4] = {1.0f, 1.0f, 1.0f, 1.0f};
\r
79 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
\r
81 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
\r
82 static float menu_pen_back [4] = {0.70f, 0.70f, 0.70f, 1.0f};
\r
83 static float menu_pen_hfore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
\r
84 static float menu_pen_hback [4] = {1.0f, 1.0f, 1.0f, 1.0f};
\r
88 extern GLvoid fgPlatformGetGameModeVMaxExtent( SFG_Window* window, int* x, int* y );
\r
90 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
\r
93 * Private function to find a menu entry by index
\r
95 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
\r
97 SFG_MenuEntry *entry;
\r
100 for( entry = (SFG_MenuEntry *)menu->Entries.First;
\r
102 entry = (SFG_MenuEntry *)entry->Node.Next )
\r
113 * Deactivates a menu pointed by the function argument.
\r
115 static void fghDeactivateSubMenu( SFG_MenuEntry *menuEntry )
\r
117 SFG_MenuEntry *subMenuIter;
\r
118 /* Hide the present menu's window */
\r
119 fgSetWindow( menuEntry->SubMenu->Window );
\r
122 /* Forget about having that menu active anymore, now: */
\r
123 menuEntry->SubMenu->Window->ActiveMenu = NULL;
\r
124 menuEntry->SubMenu->IsActive = GL_FALSE;
\r
125 menuEntry->SubMenu->ActiveEntry = NULL;
\r
127 /* Hide all submenu windows, and the root menu's window. */
\r
128 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
\r
130 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
\r
132 subMenuIter->IsActive = GL_FALSE;
\r
134 /* Is that an active submenu by any case? */
\r
135 if( subMenuIter->SubMenu )
\r
136 fghDeactivateSubMenu( subMenuIter );
\r
139 fgSetWindow ( menuEntry->SubMenu->ParentWindow ) ;
\r
143 * Private function to get the virtual maximum screen extent
\r
145 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
\r
147 if( fgStructure.GameModeWindow )
\r
148 fgPlatformGetGameModeVMaxExtent ( window, x, y );
\r
151 *x = fgDisplay.ScreenWidth;
\r
152 *y = fgDisplay.ScreenHeight;
\r
157 * Private function to check for the current menu/sub menu activity state
\r
159 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
\r
161 SFG_MenuEntry* menuEntry;
\r
164 /* First of all check any of the active sub menus... */
\r
165 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
\r
167 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
\r
169 if( menuEntry->SubMenu && menuEntry->IsActive )
\r
172 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
\r
173 * will mean that it caught the mouse cursor and we do not need
\r
174 * to regenerate the activity list, and so our parents do...
\r
176 GLboolean return_status;
\r
178 menuEntry->SubMenu->Window->State.MouseX =
\r
179 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
\r
180 menuEntry->SubMenu->Window->State.MouseY =
\r
181 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
\r
182 return_status = fghCheckMenuStatus( menuEntry->SubMenu );
\r
184 if ( return_status )
\r
189 /* That much about our sub menus, let's get to checking the current menu: */
\r
190 x = menu->Window->State.MouseX;
\r
191 y = menu->Window->State.MouseY;
\r
193 /* Check if the mouse cursor is contained within the current menu box */
\r
194 if( ( x >= FREEGLUT_MENU_BORDER ) &&
\r
195 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
\r
196 ( y >= FREEGLUT_MENU_BORDER ) &&
\r
197 ( y < menu->Height - FREEGLUT_MENU_BORDER ) )
\r
199 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
\r
201 /* The mouse cursor is somewhere over our box, check it out. */
\r
202 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
\r
203 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
\r
204 "fghCheckMenuStatus" );
\r
206 menuEntry->IsActive = GL_TRUE;
\r
207 menuEntry->Ordinal = menuID;
\r
210 * If this is not the same as the last active menu entry, deactivate
\r
211 * the previous entry. Specifically, if the previous active entry
\r
212 * was a submenu then deactivate it.
\r
214 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
\r
215 if( menu->ActiveEntry->SubMenu )
\r
216 fghDeactivateSubMenu( menu->ActiveEntry );
\r
218 if( menuEntry != menu->ActiveEntry )
\r
220 menu->Window->State.Redisplay = GL_TRUE;
\r
221 if( menu->ActiveEntry )
\r
222 menu->ActiveEntry->IsActive = GL_FALSE;
\r
225 menu->ActiveEntry = menuEntry;
\r
226 menu->IsActive = GL_TRUE; /* XXX Do we need this? */
\r
229 * OKi, we have marked that entry as active, but it would be also
\r
230 * nice to have its contents updated, in case it's a sub menu.
\r
231 * Also, ignore the return value of the check function:
\r
233 if( menuEntry->SubMenu )
\r
235 if ( ! menuEntry->SubMenu->IsActive )
\r
238 SFG_Window *current_window = fgStructure.CurrentWindow;
\r
240 /* Set up the initial menu position now... */
\r
241 menuEntry->SubMenu->IsActive = GL_TRUE;
\r
243 /* Set up the initial submenu position now: */
\r
244 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
\r
245 menuEntry->SubMenu->X = menu->X + menu->Width;
\r
246 menuEntry->SubMenu->Y = menu->Y +
\r
247 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
\r
249 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
\r
250 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
\r
252 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
\r
254 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
\r
255 FREEGLUT_MENU_HEIGHT -
\r
256 2 * FREEGLUT_MENU_BORDER );
\r
257 if( menuEntry->SubMenu->Y < 0 )
\r
258 menuEntry->SubMenu->Y = 0;
\r
261 fgSetWindow( menuEntry->SubMenu->Window );
\r
262 glutPositionWindow( menuEntry->SubMenu->X,
\r
263 menuEntry->SubMenu->Y );
\r
264 glutReshapeWindow( menuEntry->SubMenu->Width,
\r
265 menuEntry->SubMenu->Height );
\r
268 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
\r
269 fgSetWindow( current_window );
\r
270 menuEntry->SubMenu->Window->State.MouseX =
\r
271 x + menu->X - menuEntry->SubMenu->X;
\r
272 menuEntry->SubMenu->Window->State.MouseY =
\r
273 y + menu->Y - menuEntry->SubMenu->Y;
\r
274 fghCheckMenuStatus( menuEntry->SubMenu );
\r
277 /* Activate it because its parent entry is active */
\r
278 menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */
\r
281 /* Report back that we have caught the menu cursor */
\r
285 /* Looks like the menu cursor is somewhere else... */
\r
286 if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
\r
287 ( !menu->ActiveEntry->SubMenu ||
\r
288 !menu->ActiveEntry->SubMenu->IsActive ) )
\r
290 menu->Window->State.Redisplay = GL_TRUE;
\r
291 menu->ActiveEntry->IsActive = GL_FALSE;
\r
292 menu->ActiveEntry = NULL;
\r
299 * Displays a menu box and all of its submenus (if they are active)
\r
301 static void fghDisplayMenuBox( SFG_Menu* menu )
\r
303 SFG_MenuEntry *menuEntry;
\r
305 int border = FREEGLUT_MENU_BORDER;
\r
308 * Have the menu box drawn first. The +- values are
\r
309 * here just to make it more nice-looking...
\r
311 /* a non-black dark version of the below. */
\r
312 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
\r
313 glBegin( GL_QUAD_STRIP );
\r
314 glVertex2i( menu->Width , 0 );
\r
315 glVertex2i( menu->Width - border, border);
\r
316 glVertex2i( 0 , 0 );
\r
317 glVertex2i( border, border);
\r
318 glVertex2i( 0 , menu->Height );
\r
319 glVertex2i( border, menu->Height - border);
\r
322 /* a non-black dark version of the below. */
\r
323 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
\r
324 glBegin( GL_QUAD_STRIP );
\r
325 glVertex2i( 0 , menu->Height );
\r
326 glVertex2i( border, menu->Height - border);
\r
327 glVertex2i( menu->Width , menu->Height );
\r
328 glVertex2i( menu->Width - border, menu->Height - border);
\r
329 glVertex2i( menu->Width , 0 );
\r
330 glVertex2i( menu->Width - border, border);
\r
333 glColor4fv( menu_pen_back );
\r
334 glBegin( GL_QUADS );
\r
335 glVertex2i( border, border);
\r
336 glVertex2i( menu->Width - border, border);
\r
337 glVertex2i( menu->Width - border, menu->Height - border);
\r
338 glVertex2i( border, menu->Height - border);
\r
341 /* Check if any of the submenus is currently active... */
\r
342 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
\r
344 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
\r
346 /* Has the menu been marked as active, maybe? */
\r
347 if( menuEntry->IsActive )
\r
350 * That's truly right, and we need to have it highlighted.
\r
351 * There is an assumption that mouse cursor didn't move
\r
352 * since the last check of menu activity state:
\r
354 int menuID = menuEntry->Ordinal;
\r
356 /* So have the highlight drawn... */
\r
357 glColor4fv( menu_pen_hback );
\r
358 glBegin( GL_QUADS );
\r
359 glVertex2i( border,
\r
360 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
\r
361 glVertex2i( menu->Width - border,
\r
362 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
\r
363 glVertex2i( menu->Width - border,
\r
364 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
\r
365 glVertex2i( border,
\r
366 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
\r
371 /* Print the menu entries now... */
\r
373 glColor4fv( menu_pen_fore );
\r
375 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
\r
377 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
\r
379 /* If the menu entry is active, set the color to white */
\r
380 if( menuEntry->IsActive )
\r
381 glColor4fv( menu_pen_hfore );
\r
383 /* Move the raster into position... */
\r
384 /* Try to center the text - JCJ 31 July 2003*/
\r
387 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
\r
388 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
\r
391 /* Have the label drawn, character after character: */
\r
392 glutBitmapString( FREEGLUT_MENU_FONT,
\r
393 (unsigned char *)menuEntry->Text);
\r
395 /* If it's a submenu, draw a right arrow */
\r
396 if( menuEntry->SubMenu )
\r
398 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
\r
399 int x_base = menu->Width - 2 - width;
\r
400 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
\r
401 glBegin( GL_TRIANGLES );
\r
402 glVertex2i( x_base, y_base + 2*border);
\r
403 glVertex2i( menu->Width - 2, y_base +
\r
404 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
\r
405 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
\r
409 /* If the menu entry is active, reset the color */
\r
410 if( menuEntry->IsActive )
\r
411 glColor4fv( menu_pen_fore );
\r
416 * Private static function to set the parent window of a submenu and all
\r
419 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
\r
421 SFG_MenuEntry *menuEntry;
\r
423 menu->ParentWindow = window;
\r
425 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
\r
427 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
\r
428 if( menuEntry->SubMenu )
\r
429 fghSetMenuParentWindow( window, menuEntry->SubMenu );
\r
433 * Function to check for menu entry selection on menu deactivation
\r
435 static void fghExecuteMenuCallback( SFG_Menu* menu )
\r
437 SFG_MenuEntry *menuEntry;
\r
439 /* First of all check any of the active sub menus... */
\r
440 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
\r
442 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
\r
444 if( menuEntry->IsActive )
\r
446 if( menuEntry->SubMenu )
\r
447 fghExecuteMenuCallback( menuEntry->SubMenu );
\r
449 if( menu->Callback )
\r
451 SFG_Menu *save_menu = fgStructure.CurrentMenu;
\r
452 fgStructure.CurrentMenu = menu;
\r
453 menu->Callback( menuEntry->ID );
\r
454 fgStructure.CurrentMenu = save_menu;
\r
464 * Displays the currently active menu for the current window
\r
466 void fgDisplayMenu( void )
\r
468 SFG_Window* window = fgStructure.CurrentWindow;
\r
469 SFG_Menu* menu = NULL;
\r
471 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
\r
474 /* Check if there is an active menu attached to this window... */
\r
475 menu = window->ActiveMenu;
\r
476 freeglut_return_if_fail( menu );
\r
478 fgSetWindow( menu->Window );
\r
480 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
\r
483 glDisable( GL_DEPTH_TEST );
\r
484 glDisable( GL_TEXTURE_2D );
\r
485 glDisable( GL_LIGHTING );
\r
486 glDisable( GL_CULL_FACE );
\r
488 glMatrixMode( GL_PROJECTION );
\r
492 0, glutGet( GLUT_WINDOW_WIDTH ),
\r
493 glutGet( GLUT_WINDOW_HEIGHT ), 0,
\r
497 glMatrixMode( GL_MODELVIEW );
\r
501 fghDisplayMenuBox( menu );
\r
505 glMatrixMode( GL_PROJECTION );
\r
507 glMatrixMode( GL_MODELVIEW );
\r
510 glutSwapBuffers( );
\r
512 fgSetWindow ( window );
\r
516 * Activates a menu pointed by the function argument
\r
518 static void fghActivateMenu( SFG_Window* window, int button )
\r
522 /* We'll be referencing this menu a lot, so remember its address: */
\r
523 SFG_Menu* menu = window->Menu[ button ];
\r
524 SFG_Window* current_window = fgStructure.CurrentWindow;
\r
526 /* If the menu is already active in another window, deactivate it there */
\r
527 if ( menu->ParentWindow )
\r
528 menu->ParentWindow->ActiveMenu = NULL ;
\r
530 /* Mark the menu as active, so that it gets displayed: */
\r
531 window->ActiveMenu = menu;
\r
532 menu->IsActive = GL_TRUE;
\r
533 fghSetMenuParentWindow ( window, menu );
\r
534 fgState.ActiveMenus++;
\r
536 /* Set up the initial menu position now: */
\r
537 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
\r
538 fgSetWindow( window );
\r
539 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
\r
540 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
\r
542 if( menu->X + menu->Width > max_x )
\r
543 menu->X -=menu->Width;
\r
545 if( menu->Y + menu->Height > max_y )
\r
547 menu->Y -=menu->Height;
\r
552 menu->Window->State.MouseX =
\r
553 window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
\r
554 menu->Window->State.MouseY =
\r
555 window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
\r
557 fgSetWindow( menu->Window );
\r
558 glutPositionWindow( menu->X, menu->Y );
\r
559 glutReshapeWindow( menu->Width, menu->Height );
\r
562 menu->Window->ActiveMenu = menu;
\r
563 fghCheckMenuStatus( menu );
\r
564 fgSetWindow( current_window );
\r
568 * Update Highlight states of the menu
\r
570 * Current mouse position is in menu->Window->State.MouseX/Y.
\r
572 void fgUpdateMenuHighlight ( SFG_Menu *menu )
\r
574 fghCheckMenuStatus( menu );
\r
578 * Check whether an active menu absorbs a mouse click
\r
580 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
\r
581 int mouse_x, int mouse_y )
\r
584 * Near as I can tell, this is the menu behaviour:
\r
585 * - Down-click the menu button, menu not active: activate
\r
586 * the menu with its upper left-hand corner at the mouse
\r
588 * - Down-click any button outside the menu, menu active:
\r
589 * deactivate the menu
\r
590 * - Down-click any button inside the menu, menu active:
\r
591 * select the menu entry and deactivate the menu
\r
592 * - Up-click the menu button, menu not active: nothing happens
\r
593 * - Up-click the menu button outside the menu, menu active:
\r
595 * - Up-click the menu button inside the menu, menu active:
\r
596 * select the menu entry and deactivate the menu
\r
597 * Since menus can have submenus, we need to check this recursively.
\r
599 if( window->ActiveMenu )
\r
601 if( window == window->ActiveMenu->ParentWindow )
\r
603 window->ActiveMenu->Window->State.MouseX =
\r
604 mouse_x - window->ActiveMenu->X;
\r
605 window->ActiveMenu->Window->State.MouseY =
\r
606 mouse_y - window->ActiveMenu->Y;
\r
609 /* In the menu, invoke the callback and deactivate the menu */
\r
610 if( fghCheckMenuStatus( window->ActiveMenu ) )
\r
613 * Save the current window and menu and set the current
\r
614 * window to the window whose menu this is
\r
616 SFG_Window *save_window = fgStructure.CurrentWindow;
\r
617 SFG_Menu *save_menu = fgStructure.CurrentMenu;
\r
618 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
\r
619 fgSetWindow( parent_window );
\r
620 fgStructure.CurrentMenu = window->ActiveMenu;
\r
622 /* Execute the menu callback */
\r
623 fghExecuteMenuCallback( window->ActiveMenu );
\r
624 fgDeactivateMenu( parent_window );
\r
626 /* Restore the current window and menu */
\r
627 fgSetWindow( save_window );
\r
628 fgStructure.CurrentMenu = save_menu;
\r
632 * Outside the menu, deactivate if it's a downclick
\r
634 * XXX This isn't enough. A downclick outside of
\r
635 * XXX the interior of our freeglut windows should also
\r
636 * XXX deactivate the menu. This is more complicated.
\r
638 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
\r
641 * XXX Why does an active menu require a redisplay at
\r
642 * XXX this point? If this can come out cleanly, then
\r
643 * XXX it probably should do so; if not, a comment should
\r
646 if( ! window->IsMenu )
\r
647 window->State.Redisplay = GL_TRUE;
\r
652 /* No active menu, let's check whether we need to activate one. */
\r
653 if( ( 0 <= button ) &&
\r
654 ( FREEGLUT_MAX_MENUS > button ) &&
\r
655 ( window->Menu[ button ] ) &&
\r
658 /* XXX Posting a requisite Redisplay seems bogus. */
\r
659 window->State.Redisplay = GL_TRUE;
\r
660 fghActivateMenu( window, button );
\r
668 * Deactivates a menu pointed by the function argument.
\r
670 void fgDeactivateMenu( SFG_Window *window )
\r
672 SFG_Window *parent_window = NULL;
\r
674 /* Check if there is an active menu attached to this window... */
\r
675 SFG_Menu* menu = window->ActiveMenu;
\r
676 SFG_MenuEntry *menuEntry;
\r
678 /* Did we find an active window? */
\r
679 freeglut_return_if_fail( menu );
\r
681 parent_window = menu->ParentWindow;
\r
683 /* Hide the present menu's window */
\r
684 fgSetWindow( menu->Window );
\r
687 /* Forget about having that menu active anymore, now: */
\r
688 menu->Window->ActiveMenu = NULL;
\r
689 menu->ParentWindow->ActiveMenu = NULL;
\r
690 fghSetMenuParentWindow ( NULL, menu );
\r
691 menu->IsActive = GL_FALSE;
\r
692 menu->ActiveEntry = NULL;
\r
694 fgState.ActiveMenus--;
\r
696 /* Hide all submenu windows, and the root menu's window. */
\r
697 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
\r
699 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
\r
701 menuEntry->IsActive = GL_FALSE;
\r
703 /* Is that an active submenu by any case? */
\r
704 if( menuEntry->SubMenu )
\r
705 fghDeactivateSubMenu( menuEntry );
\r
708 fgSetWindow ( parent_window ) ;
\r
712 * Recalculates current menu's box size
\r
714 void fghCalculateMenuBoxSize( void )
\r
716 SFG_MenuEntry* menuEntry;
\r
717 int width = 0, height = 0;
\r
719 /* Make sure there is a current menu set */
\r
720 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
722 /* The menu's box size depends on the menu entries: */
\r
723 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
\r
725 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
\r
727 /* Update the menu entry's width value */
\r
728 menuEntry->Width = glutBitmapLength(
\r
729 FREEGLUT_MENU_FONT,
\r
730 (unsigned char *)menuEntry->Text
\r
734 * If the entry is a submenu, then it needs to be wider to
\r
735 * accomodate the arrow. JCJ 31 July 2003
\r
737 if (menuEntry->SubMenu )
\r
738 menuEntry->Width += glutBitmapLength(
\r
739 FREEGLUT_MENU_FONT,
\r
740 (unsigned char *)"_"
\r
743 /* Check if it's the biggest we've found */
\r
744 if( menuEntry->Width > width )
\r
745 width = menuEntry->Width;
\r
747 height += FREEGLUT_MENU_HEIGHT;
\r
750 /* Store the menu's box size now: */
\r
751 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
\r
752 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
\r
756 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
\r
759 * Creates a new menu object, adding it to the freeglut structure
\r
761 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
\r
763 /* The menu object creation code resides in freeglut_structure.c */
\r
764 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
\r
765 return fgCreateMenu( callback )->ID;
\r
769 * Destroys a menu object, removing all references to it
\r
771 void FGAPIENTRY glutDestroyMenu( int menuID )
\r
775 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
\r
776 menu = fgMenuByID( menuID );
\r
778 freeglut_return_if_fail( menu );
\r
780 /* The menu object destruction code resides in freeglut_structure.c */
\r
781 fgDestroyMenu( menu );
\r
785 * Returns the ID number of the currently active menu
\r
787 int FGAPIENTRY glutGetMenu( void )
\r
789 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
\r
791 if( fgStructure.CurrentMenu )
\r
792 return fgStructure.CurrentMenu->ID;
\r
798 * Sets the current menu given its menu ID
\r
800 void FGAPIENTRY glutSetMenu( int menuID )
\r
804 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
\r
805 menu = fgMenuByID( menuID );
\r
807 freeglut_return_if_fail( menu );
\r
809 fgStructure.CurrentMenu = menu;
\r
813 * Adds a menu entry to the bottom of the current menu
\r
815 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
\r
817 SFG_MenuEntry* menuEntry;
\r
818 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
\r
819 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
\r
820 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
822 menuEntry->Text = strdup( label );
\r
823 menuEntry->ID = value;
\r
825 /* Have the new menu entry attached to the current menu */
\r
826 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
\r
828 fghCalculateMenuBoxSize( );
\r
832 * Add a sub menu to the bottom of the current menu
\r
834 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
\r
836 SFG_MenuEntry *menuEntry;
\r
839 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
\r
840 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
\r
841 subMenu = fgMenuByID( subMenuID );
\r
843 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
844 freeglut_return_if_fail( subMenu );
\r
846 menuEntry->Text = strdup( label );
\r
847 menuEntry->SubMenu = subMenu;
\r
848 menuEntry->ID = -1;
\r
850 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
\r
851 fghCalculateMenuBoxSize( );
\r
855 * Changes the specified menu item in the current menu into a menu entry
\r
857 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
\r
859 SFG_MenuEntry* menuEntry = NULL;
\r
861 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
\r
862 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
864 /* Get n-th menu entry in the current menu, starting from one: */
\r
865 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
\r
867 freeglut_return_if_fail( menuEntry );
\r
869 /* We want it to become a normal menu entry, so: */
\r
870 if( menuEntry->Text )
\r
871 free( menuEntry->Text );
\r
873 menuEntry->Text = strdup( label );
\r
874 menuEntry->ID = value;
\r
875 menuEntry->SubMenu = NULL;
\r
876 fghCalculateMenuBoxSize( );
\r
880 * Changes the specified menu item in the current menu into a sub-menu trigger.
\r
882 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
\r
886 SFG_MenuEntry* menuEntry;
\r
888 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
\r
889 subMenu = fgMenuByID( subMenuID );
\r
892 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
893 freeglut_return_if_fail( subMenu );
\r
895 /* Get n-th menu entry in the current menu, starting from one: */
\r
896 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
\r
898 freeglut_return_if_fail( menuEntry );
\r
900 /* We want it to become a sub menu entry, so: */
\r
901 if( menuEntry->Text )
\r
902 free( menuEntry->Text );
\r
904 menuEntry->Text = strdup( label );
\r
905 menuEntry->SubMenu = subMenu;
\r
906 menuEntry->ID = -1;
\r
907 fghCalculateMenuBoxSize( );
\r
911 * Removes the specified menu item from the current menu
\r
913 void FGAPIENTRY glutRemoveMenuItem( int item )
\r
915 SFG_MenuEntry* menuEntry;
\r
917 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
\r
918 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
920 /* Get n-th menu entry in the current menu, starting from one: */
\r
921 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
\r
923 freeglut_return_if_fail( menuEntry );
\r
925 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
\r
926 if ( menuEntry->Text )
\r
927 free( menuEntry->Text );
\r
930 fghCalculateMenuBoxSize( );
\r
934 * Attaches a menu to the current window
\r
936 void FGAPIENTRY glutAttachMenu( int button )
\r
938 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
\r
940 freeglut_return_if_fail( fgStructure.CurrentWindow );
\r
941 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
943 freeglut_return_if_fail( button >= 0 );
\r
944 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
\r
946 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
\r
950 * Detaches a menu from the current window
\r
952 void FGAPIENTRY glutDetachMenu( int button )
\r
954 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
\r
956 freeglut_return_if_fail( fgStructure.CurrentWindow );
\r
957 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
959 freeglut_return_if_fail( button >= 0 );
\r
960 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
\r
962 fgStructure.CurrentWindow->Menu[ button ] = NULL;
\r
966 * A.Donev: Set and retrieve the menu's user data
\r
968 void* FGAPIENTRY glutGetMenuData( void )
\r
970 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
\r
971 return fgStructure.CurrentMenu->UserData;
\r
974 void FGAPIENTRY glutSetMenuData(void* data)
\r
976 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
\r
977 fgStructure.CurrentMenu->UserData=data;
\r
980 /*** END OF FILE ***/
\r