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 #if TARGET_HOST_POSIX_X11
\r
146 static GLvoid fgPlatformGetGameModeVMaxExtent( SFG_Window* window, int* x, int* y )
\r
151 XTranslateCoordinates(
\r
153 window->Window.Handle,
\r
154 fgDisplay.RootWindow,
\r
155 0, 0, &wx, &wy, &w);
\r
157 *x = fgState.GameModeSize.X + wx;
\r
158 *y = fgState.GameModeSize.Y + wy;
\r
163 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
\r
165 if( fgStructure.GameModeWindow )
\r
166 fgPlatformGetGameModeVMaxExtent ( window, x, y );
\r
169 *x = fgDisplay.ScreenWidth;
\r
170 *y = fgDisplay.ScreenHeight;
\r
175 * Private function to check for the current menu/sub menu activity state
\r
177 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
\r
179 SFG_MenuEntry* menuEntry;
\r
182 /* First of all check any of the active sub menus... */
\r
183 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
\r
185 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
\r
187 if( menuEntry->SubMenu && menuEntry->IsActive )
\r
190 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
\r
191 * will mean that it caught the mouse cursor and we do not need
\r
192 * to regenerate the activity list, and so our parents do...
\r
194 GLboolean return_status;
\r
196 menuEntry->SubMenu->Window->State.MouseX =
\r
197 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
\r
198 menuEntry->SubMenu->Window->State.MouseY =
\r
199 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
\r
200 return_status = fghCheckMenuStatus( menuEntry->SubMenu );
\r
202 if ( return_status )
\r
207 /* That much about our sub menus, let's get to checking the current menu: */
\r
208 x = menu->Window->State.MouseX;
\r
209 y = menu->Window->State.MouseY;
\r
211 /* Check if the mouse cursor is contained within the current menu box */
\r
212 if( ( x >= FREEGLUT_MENU_BORDER ) &&
\r
213 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
\r
214 ( y >= FREEGLUT_MENU_BORDER ) &&
\r
215 ( y < menu->Height - FREEGLUT_MENU_BORDER ) )
\r
217 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
\r
219 /* The mouse cursor is somewhere over our box, check it out. */
\r
220 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
\r
221 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
\r
222 "fghCheckMenuStatus" );
\r
224 menuEntry->IsActive = GL_TRUE;
\r
225 menuEntry->Ordinal = menuID;
\r
228 * If this is not the same as the last active menu entry, deactivate
\r
229 * the previous entry. Specifically, if the previous active entry
\r
230 * was a submenu then deactivate it.
\r
232 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
\r
233 if( menu->ActiveEntry->SubMenu )
\r
234 fghDeactivateSubMenu( menu->ActiveEntry );
\r
236 if( menuEntry != menu->ActiveEntry )
\r
238 menu->Window->State.Redisplay = GL_TRUE;
\r
239 if( menu->ActiveEntry )
\r
240 menu->ActiveEntry->IsActive = GL_FALSE;
\r
243 menu->ActiveEntry = menuEntry;
\r
244 menu->IsActive = GL_TRUE; /* XXX Do we need this? */
\r
247 * OKi, we have marked that entry as active, but it would be also
\r
248 * nice to have its contents updated, in case it's a sub menu.
\r
249 * Also, ignore the return value of the check function:
\r
251 if( menuEntry->SubMenu )
\r
253 if ( ! menuEntry->SubMenu->IsActive )
\r
256 SFG_Window *current_window = fgStructure.CurrentWindow;
\r
258 /* Set up the initial menu position now... */
\r
259 menuEntry->SubMenu->IsActive = GL_TRUE;
\r
261 /* Set up the initial submenu position now: */
\r
262 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
\r
263 menuEntry->SubMenu->X = menu->X + menu->Width;
\r
264 menuEntry->SubMenu->Y = menu->Y +
\r
265 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
\r
267 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
\r
268 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
\r
270 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
\r
272 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
\r
273 FREEGLUT_MENU_HEIGHT -
\r
274 2 * FREEGLUT_MENU_BORDER );
\r
275 if( menuEntry->SubMenu->Y < 0 )
\r
276 menuEntry->SubMenu->Y = 0;
\r
279 fgSetWindow( menuEntry->SubMenu->Window );
\r
280 glutPositionWindow( menuEntry->SubMenu->X,
\r
281 menuEntry->SubMenu->Y );
\r
282 glutReshapeWindow( menuEntry->SubMenu->Width,
\r
283 menuEntry->SubMenu->Height );
\r
286 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
\r
287 fgSetWindow( current_window );
\r
288 menuEntry->SubMenu->Window->State.MouseX =
\r
289 x + menu->X - menuEntry->SubMenu->X;
\r
290 menuEntry->SubMenu->Window->State.MouseY =
\r
291 y + menu->Y - menuEntry->SubMenu->Y;
\r
292 fghCheckMenuStatus( menuEntry->SubMenu );
\r
295 /* Activate it because its parent entry is active */
\r
296 menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */
\r
299 /* Report back that we have caught the menu cursor */
\r
303 /* Looks like the menu cursor is somewhere else... */
\r
304 if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
\r
305 ( !menu->ActiveEntry->SubMenu ||
\r
306 !menu->ActiveEntry->SubMenu->IsActive ) )
\r
308 menu->Window->State.Redisplay = GL_TRUE;
\r
309 menu->ActiveEntry->IsActive = GL_FALSE;
\r
310 menu->ActiveEntry = NULL;
\r
317 * Displays a menu box and all of its submenus (if they are active)
\r
319 static void fghDisplayMenuBox( SFG_Menu* menu )
\r
321 SFG_MenuEntry *menuEntry;
\r
323 int border = FREEGLUT_MENU_BORDER;
\r
326 * Have the menu box drawn first. The +- values are
\r
327 * here just to make it more nice-looking...
\r
329 /* a non-black dark version of the below. */
\r
330 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
\r
331 glBegin( GL_QUAD_STRIP );
\r
332 glVertex2i( menu->Width , 0 );
\r
333 glVertex2i( menu->Width - border, border);
\r
334 glVertex2i( 0 , 0 );
\r
335 glVertex2i( border, border);
\r
336 glVertex2i( 0 , menu->Height );
\r
337 glVertex2i( border, menu->Height - border);
\r
340 /* a non-black dark version of the below. */
\r
341 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
\r
342 glBegin( GL_QUAD_STRIP );
\r
343 glVertex2i( 0 , menu->Height );
\r
344 glVertex2i( border, menu->Height - border);
\r
345 glVertex2i( menu->Width , menu->Height );
\r
346 glVertex2i( menu->Width - border, menu->Height - border);
\r
347 glVertex2i( menu->Width , 0 );
\r
348 glVertex2i( menu->Width - border, border);
\r
351 glColor4fv( menu_pen_back );
\r
352 glBegin( GL_QUADS );
\r
353 glVertex2i( border, border);
\r
354 glVertex2i( menu->Width - border, border);
\r
355 glVertex2i( menu->Width - border, menu->Height - border);
\r
356 glVertex2i( border, menu->Height - border);
\r
359 /* Check if any of the submenus is currently active... */
\r
360 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
\r
362 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
\r
364 /* Has the menu been marked as active, maybe? */
\r
365 if( menuEntry->IsActive )
\r
368 * That's truly right, and we need to have it highlighted.
\r
369 * There is an assumption that mouse cursor didn't move
\r
370 * since the last check of menu activity state:
\r
372 int menuID = menuEntry->Ordinal;
\r
374 /* So have the highlight drawn... */
\r
375 glColor4fv( menu_pen_hback );
\r
376 glBegin( GL_QUADS );
\r
377 glVertex2i( border,
\r
378 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
\r
379 glVertex2i( menu->Width - border,
\r
380 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
\r
381 glVertex2i( menu->Width - border,
\r
382 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
\r
383 glVertex2i( border,
\r
384 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
\r
389 /* Print the menu entries now... */
\r
391 glColor4fv( menu_pen_fore );
\r
393 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
\r
395 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
\r
397 /* If the menu entry is active, set the color to white */
\r
398 if( menuEntry->IsActive )
\r
399 glColor4fv( menu_pen_hfore );
\r
401 /* Move the raster into position... */
\r
402 /* Try to center the text - JCJ 31 July 2003*/
\r
405 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
\r
406 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
\r
409 /* Have the label drawn, character after character: */
\r
410 glutBitmapString( FREEGLUT_MENU_FONT,
\r
411 (unsigned char *)menuEntry->Text);
\r
413 /* If it's a submenu, draw a right arrow */
\r
414 if( menuEntry->SubMenu )
\r
416 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
\r
417 int x_base = menu->Width - 2 - width;
\r
418 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
\r
419 glBegin( GL_TRIANGLES );
\r
420 glVertex2i( x_base, y_base + 2*border);
\r
421 glVertex2i( menu->Width - 2, y_base +
\r
422 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
\r
423 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
\r
427 /* If the menu entry is active, reset the color */
\r
428 if( menuEntry->IsActive )
\r
429 glColor4fv( menu_pen_fore );
\r
434 * Private static function to set the parent window of a submenu and all
\r
437 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
\r
439 SFG_MenuEntry *menuEntry;
\r
441 menu->ParentWindow = window;
\r
443 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
\r
445 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
\r
446 if( menuEntry->SubMenu )
\r
447 fghSetMenuParentWindow( window, menuEntry->SubMenu );
\r
451 * Function to check for menu entry selection on menu deactivation
\r
453 static void fghExecuteMenuCallback( SFG_Menu* menu )
\r
455 SFG_MenuEntry *menuEntry;
\r
457 /* First of all check any of the active sub menus... */
\r
458 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
\r
460 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
\r
462 if( menuEntry->IsActive )
\r
464 if( menuEntry->SubMenu )
\r
465 fghExecuteMenuCallback( menuEntry->SubMenu );
\r
467 if( menu->Callback )
\r
469 SFG_Menu *save_menu = fgStructure.CurrentMenu;
\r
470 fgStructure.CurrentMenu = menu;
\r
471 menu->Callback( menuEntry->ID );
\r
472 fgStructure.CurrentMenu = save_menu;
\r
482 * Displays the currently active menu for the current window
\r
484 void fgDisplayMenu( void )
\r
486 SFG_Window* window = fgStructure.CurrentWindow;
\r
487 SFG_Menu* menu = NULL;
\r
489 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
\r
492 /* Check if there is an active menu attached to this window... */
\r
493 menu = window->ActiveMenu;
\r
494 freeglut_return_if_fail( menu );
\r
496 fgSetWindow( menu->Window );
\r
498 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
\r
501 glDisable( GL_DEPTH_TEST );
\r
502 glDisable( GL_TEXTURE_2D );
\r
503 glDisable( GL_LIGHTING );
\r
504 glDisable( GL_CULL_FACE );
\r
506 glMatrixMode( GL_PROJECTION );
\r
510 0, glutGet( GLUT_WINDOW_WIDTH ),
\r
511 glutGet( GLUT_WINDOW_HEIGHT ), 0,
\r
515 glMatrixMode( GL_MODELVIEW );
\r
519 fghDisplayMenuBox( menu );
\r
523 glMatrixMode( GL_PROJECTION );
\r
525 glMatrixMode( GL_MODELVIEW );
\r
528 glutSwapBuffers( );
\r
530 fgSetWindow ( window );
\r
534 * Activates a menu pointed by the function argument
\r
536 static void fghActivateMenu( SFG_Window* window, int button )
\r
540 /* We'll be referencing this menu a lot, so remember its address: */
\r
541 SFG_Menu* menu = window->Menu[ button ];
\r
542 SFG_Window* current_window = fgStructure.CurrentWindow;
\r
544 /* If the menu is already active in another window, deactivate it there */
\r
545 if ( menu->ParentWindow )
\r
546 menu->ParentWindow->ActiveMenu = NULL ;
\r
548 /* Mark the menu as active, so that it gets displayed: */
\r
549 window->ActiveMenu = menu;
\r
550 menu->IsActive = GL_TRUE;
\r
551 fghSetMenuParentWindow ( window, menu );
\r
552 fgState.ActiveMenus++;
\r
554 /* Set up the initial menu position now: */
\r
555 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
\r
556 fgSetWindow( window );
\r
557 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
\r
558 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
\r
560 if( menu->X + menu->Width > max_x )
\r
561 menu->X -=menu->Width;
\r
563 if( menu->Y + menu->Height > max_y )
\r
565 menu->Y -=menu->Height;
\r
570 menu->Window->State.MouseX =
\r
571 window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
\r
572 menu->Window->State.MouseY =
\r
573 window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
\r
575 fgSetWindow( menu->Window );
\r
576 glutPositionWindow( menu->X, menu->Y );
\r
577 glutReshapeWindow( menu->Width, menu->Height );
\r
580 menu->Window->ActiveMenu = menu;
\r
581 fghCheckMenuStatus( menu );
\r
582 fgSetWindow( current_window );
\r
586 * Update Highlight states of the menu
\r
588 * Current mouse position is in menu->Window->State.MouseX/Y.
\r
590 void fgUpdateMenuHighlight ( SFG_Menu *menu )
\r
592 fghCheckMenuStatus( menu );
\r
596 * Check whether an active menu absorbs a mouse click
\r
598 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
\r
599 int mouse_x, int mouse_y )
\r
602 * Near as I can tell, this is the menu behaviour:
\r
603 * - Down-click the menu button, menu not active: activate
\r
604 * the menu with its upper left-hand corner at the mouse
\r
606 * - Down-click any button outside the menu, menu active:
\r
607 * deactivate the menu
\r
608 * - Down-click any button inside the menu, menu active:
\r
609 * select the menu entry and deactivate the menu
\r
610 * - Up-click the menu button, menu not active: nothing happens
\r
611 * - Up-click the menu button outside the menu, menu active:
\r
613 * - Up-click the menu button inside the menu, menu active:
\r
614 * select the menu entry and deactivate the menu
\r
615 * Since menus can have submenus, we need to check this recursively.
\r
617 if( window->ActiveMenu )
\r
619 if( window == window->ActiveMenu->ParentWindow )
\r
621 window->ActiveMenu->Window->State.MouseX =
\r
622 mouse_x - window->ActiveMenu->X;
\r
623 window->ActiveMenu->Window->State.MouseY =
\r
624 mouse_y - window->ActiveMenu->Y;
\r
627 /* In the menu, invoke the callback and deactivate the menu */
\r
628 if( fghCheckMenuStatus( window->ActiveMenu ) )
\r
631 * Save the current window and menu and set the current
\r
632 * window to the window whose menu this is
\r
634 SFG_Window *save_window = fgStructure.CurrentWindow;
\r
635 SFG_Menu *save_menu = fgStructure.CurrentMenu;
\r
636 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
\r
637 fgSetWindow( parent_window );
\r
638 fgStructure.CurrentMenu = window->ActiveMenu;
\r
640 /* Execute the menu callback */
\r
641 fghExecuteMenuCallback( window->ActiveMenu );
\r
642 fgDeactivateMenu( parent_window );
\r
644 /* Restore the current window and menu */
\r
645 fgSetWindow( save_window );
\r
646 fgStructure.CurrentMenu = save_menu;
\r
650 * Outside the menu, deactivate if it's a downclick
\r
652 * XXX This isn't enough. A downclick outside of
\r
653 * XXX the interior of our freeglut windows should also
\r
654 * XXX deactivate the menu. This is more complicated.
\r
656 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
\r
659 * XXX Why does an active menu require a redisplay at
\r
660 * XXX this point? If this can come out cleanly, then
\r
661 * XXX it probably should do so; if not, a comment should
\r
664 if( ! window->IsMenu )
\r
665 window->State.Redisplay = GL_TRUE;
\r
670 /* No active menu, let's check whether we need to activate one. */
\r
671 if( ( 0 <= button ) &&
\r
672 ( FREEGLUT_MAX_MENUS > button ) &&
\r
673 ( window->Menu[ button ] ) &&
\r
676 /* XXX Posting a requisite Redisplay seems bogus. */
\r
677 window->State.Redisplay = GL_TRUE;
\r
678 fghActivateMenu( window, button );
\r
686 * Deactivates a menu pointed by the function argument.
\r
688 void fgDeactivateMenu( SFG_Window *window )
\r
690 SFG_Window *parent_window = NULL;
\r
692 /* Check if there is an active menu attached to this window... */
\r
693 SFG_Menu* menu = window->ActiveMenu;
\r
694 SFG_MenuEntry *menuEntry;
\r
696 /* Did we find an active window? */
\r
697 freeglut_return_if_fail( menu );
\r
699 parent_window = menu->ParentWindow;
\r
701 /* Hide the present menu's window */
\r
702 fgSetWindow( menu->Window );
\r
705 /* Forget about having that menu active anymore, now: */
\r
706 menu->Window->ActiveMenu = NULL;
\r
707 menu->ParentWindow->ActiveMenu = NULL;
\r
708 fghSetMenuParentWindow ( NULL, menu );
\r
709 menu->IsActive = GL_FALSE;
\r
710 menu->ActiveEntry = NULL;
\r
712 fgState.ActiveMenus--;
\r
714 /* Hide all submenu windows, and the root menu's window. */
\r
715 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
\r
717 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
\r
719 menuEntry->IsActive = GL_FALSE;
\r
721 /* Is that an active submenu by any case? */
\r
722 if( menuEntry->SubMenu )
\r
723 fghDeactivateSubMenu( menuEntry );
\r
726 fgSetWindow ( parent_window ) ;
\r
730 * Recalculates current menu's box size
\r
732 void fghCalculateMenuBoxSize( void )
\r
734 SFG_MenuEntry* menuEntry;
\r
735 int width = 0, height = 0;
\r
737 /* Make sure there is a current menu set */
\r
738 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
740 /* The menu's box size depends on the menu entries: */
\r
741 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
\r
743 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
\r
745 /* Update the menu entry's width value */
\r
746 menuEntry->Width = glutBitmapLength(
\r
747 FREEGLUT_MENU_FONT,
\r
748 (unsigned char *)menuEntry->Text
\r
752 * If the entry is a submenu, then it needs to be wider to
\r
753 * accomodate the arrow. JCJ 31 July 2003
\r
755 if (menuEntry->SubMenu )
\r
756 menuEntry->Width += glutBitmapLength(
\r
757 FREEGLUT_MENU_FONT,
\r
758 (unsigned char *)"_"
\r
761 /* Check if it's the biggest we've found */
\r
762 if( menuEntry->Width > width )
\r
763 width = menuEntry->Width;
\r
765 height += FREEGLUT_MENU_HEIGHT;
\r
768 /* Store the menu's box size now: */
\r
769 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
\r
770 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
\r
774 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
\r
777 * Creates a new menu object, adding it to the freeglut structure
\r
779 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
\r
781 /* The menu object creation code resides in freeglut_structure.c */
\r
782 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
\r
783 return fgCreateMenu( callback )->ID;
\r
786 #if TARGET_HOST_MS_WINDOWS
\r
787 int FGAPIENTRY __glutCreateMenuWithExit( void(* callback)( int ), void (__cdecl *exit_function)(int) )
\r
789 __glutExitFunc = exit_function;
\r
790 return glutCreateMenu( callback );
\r
795 * Destroys a menu object, removing all references to it
\r
797 void FGAPIENTRY glutDestroyMenu( int menuID )
\r
801 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
\r
802 menu = fgMenuByID( menuID );
\r
804 freeglut_return_if_fail( menu );
\r
806 /* The menu object destruction code resides in freeglut_structure.c */
\r
807 fgDestroyMenu( menu );
\r
811 * Returns the ID number of the currently active menu
\r
813 int FGAPIENTRY glutGetMenu( void )
\r
815 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
\r
817 if( fgStructure.CurrentMenu )
\r
818 return fgStructure.CurrentMenu->ID;
\r
824 * Sets the current menu given its menu ID
\r
826 void FGAPIENTRY glutSetMenu( int menuID )
\r
830 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
\r
831 menu = fgMenuByID( menuID );
\r
833 freeglut_return_if_fail( menu );
\r
835 fgStructure.CurrentMenu = menu;
\r
839 * Adds a menu entry to the bottom of the current menu
\r
841 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
\r
843 SFG_MenuEntry* menuEntry;
\r
844 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
\r
845 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
\r
846 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
848 menuEntry->Text = strdup( label );
\r
849 menuEntry->ID = value;
\r
851 /* Have the new menu entry attached to the current menu */
\r
852 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
\r
854 fghCalculateMenuBoxSize( );
\r
858 * Add a sub menu to the bottom of the current menu
\r
860 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
\r
862 SFG_MenuEntry *menuEntry;
\r
865 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
\r
866 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
\r
867 subMenu = fgMenuByID( subMenuID );
\r
869 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
870 freeglut_return_if_fail( subMenu );
\r
872 menuEntry->Text = strdup( label );
\r
873 menuEntry->SubMenu = subMenu;
\r
874 menuEntry->ID = -1;
\r
876 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
\r
877 fghCalculateMenuBoxSize( );
\r
881 * Changes the specified menu item in the current menu into a menu entry
\r
883 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
\r
885 SFG_MenuEntry* menuEntry = NULL;
\r
887 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
\r
888 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
890 /* Get n-th menu entry in the current menu, starting from one: */
\r
891 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
\r
893 freeglut_return_if_fail( menuEntry );
\r
895 /* We want it to become a normal menu entry, so: */
\r
896 if( menuEntry->Text )
\r
897 free( menuEntry->Text );
\r
899 menuEntry->Text = strdup( label );
\r
900 menuEntry->ID = value;
\r
901 menuEntry->SubMenu = NULL;
\r
902 fghCalculateMenuBoxSize( );
\r
906 * Changes the specified menu item in the current menu into a sub-menu trigger.
\r
908 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
\r
912 SFG_MenuEntry* menuEntry;
\r
914 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
\r
915 subMenu = fgMenuByID( subMenuID );
\r
918 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
919 freeglut_return_if_fail( subMenu );
\r
921 /* Get n-th menu entry in the current menu, starting from one: */
\r
922 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
\r
924 freeglut_return_if_fail( menuEntry );
\r
926 /* We want it to become a sub menu entry, so: */
\r
927 if( menuEntry->Text )
\r
928 free( menuEntry->Text );
\r
930 menuEntry->Text = strdup( label );
\r
931 menuEntry->SubMenu = subMenu;
\r
932 menuEntry->ID = -1;
\r
933 fghCalculateMenuBoxSize( );
\r
937 * Removes the specified menu item from the current menu
\r
939 void FGAPIENTRY glutRemoveMenuItem( int item )
\r
941 SFG_MenuEntry* menuEntry;
\r
943 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
\r
944 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
946 /* Get n-th menu entry in the current menu, starting from one: */
\r
947 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
\r
949 freeglut_return_if_fail( menuEntry );
\r
951 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
\r
952 if ( menuEntry->Text )
\r
953 free( menuEntry->Text );
\r
956 fghCalculateMenuBoxSize( );
\r
960 * Attaches a menu to the current window
\r
962 void FGAPIENTRY glutAttachMenu( int button )
\r
964 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
\r
966 freeglut_return_if_fail( fgStructure.CurrentWindow );
\r
967 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
969 freeglut_return_if_fail( button >= 0 );
\r
970 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
\r
972 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
\r
976 * Detaches a menu from the current window
\r
978 void FGAPIENTRY glutDetachMenu( int button )
\r
980 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
\r
982 freeglut_return_if_fail( fgStructure.CurrentWindow );
\r
983 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
985 freeglut_return_if_fail( button >= 0 );
\r
986 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
\r
988 fgStructure.CurrentWindow->Menu[ button ] = NULL;
\r
992 * A.Donev: Set and retrieve the menu's user data
\r
994 void* FGAPIENTRY glutGetMenuData( void )
\r
996 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
\r
997 return fgStructure.CurrentMenu->UserData;
\r
1000 void FGAPIENTRY glutSetMenuData(void* data)
\r
1002 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
\r
1003 fgStructure.CurrentMenu->UserData=data;
\r
1006 /*** END OF FILE ***/
\r