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
87 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
\r
90 * Private function to find a menu entry by index
\r
92 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
\r
94 SFG_MenuEntry *entry;
\r
97 for( entry = (SFG_MenuEntry *)menu->Entries.First;
\r
99 entry = (SFG_MenuEntry *)entry->Node.Next )
\r
110 * Deactivates a menu pointed by the function argument.
\r
112 static void fghDeactivateSubMenu( SFG_MenuEntry *menuEntry )
\r
114 SFG_MenuEntry *subMenuIter;
\r
115 /* Hide the present menu's window */
\r
116 fgSetWindow( menuEntry->SubMenu->Window );
\r
119 /* Forget about having that menu active anymore, now: */
\r
120 menuEntry->SubMenu->Window->ActiveMenu = NULL;
\r
121 menuEntry->SubMenu->IsActive = GL_FALSE;
\r
122 menuEntry->SubMenu->ActiveEntry = NULL;
\r
124 /* Hide all submenu windows, and the root menu's window. */
\r
125 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
\r
127 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
\r
129 subMenuIter->IsActive = GL_FALSE;
\r
131 /* Is that an active submenu by any case? */
\r
132 if( subMenuIter->SubMenu )
\r
133 fghDeactivateSubMenu( subMenuIter );
\r
136 fgSetWindow ( menuEntry->SubMenu->ParentWindow ) ;
\r
140 * Private function to get the virtual maximum screen extent
\r
142 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
\r
144 if( fgStructure.GameModeWindow )
\r
146 #if TARGET_HOST_POSIX_X11
\r
150 XTranslateCoordinates(
\r
152 window->Window.Handle,
\r
153 fgDisplay.RootWindow,
\r
154 0, 0, &wx, &wy, &w);
\r
156 *x = fgState.GameModeSize.X + wx;
\r
157 *y = fgState.GameModeSize.Y + wy;
\r
159 *x = glutGet ( GLUT_SCREEN_WIDTH );
\r
160 *y = glutGet ( GLUT_SCREEN_HEIGHT );
\r
165 *x = fgDisplay.ScreenWidth;
\r
166 *y = fgDisplay.ScreenHeight;
\r
171 * Private function to check for the current menu/sub menu activity state
\r
173 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
\r
175 SFG_MenuEntry* menuEntry;
\r
178 /* First of all check any of the active sub menus... */
\r
179 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
\r
181 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
\r
183 if( menuEntry->SubMenu && menuEntry->IsActive )
\r
186 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
\r
187 * will mean that it caught the mouse cursor and we do not need
\r
188 * to regenerate the activity list, and so our parents do...
\r
190 GLboolean return_status;
\r
192 menuEntry->SubMenu->Window->State.MouseX =
\r
193 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
\r
194 menuEntry->SubMenu->Window->State.MouseY =
\r
195 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
\r
196 return_status = fghCheckMenuStatus( menuEntry->SubMenu );
\r
198 if ( return_status )
\r
203 /* That much about our sub menus, let's get to checking the current menu: */
\r
204 x = menu->Window->State.MouseX;
\r
205 y = menu->Window->State.MouseY;
\r
207 /* Check if the mouse cursor is contained within the current menu box */
\r
208 if( ( x >= FREEGLUT_MENU_BORDER ) &&
\r
209 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
\r
210 ( y >= FREEGLUT_MENU_BORDER ) &&
\r
211 ( y < menu->Height - FREEGLUT_MENU_BORDER ) )
\r
213 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
\r
215 /* The mouse cursor is somewhere over our box, check it out. */
\r
216 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
\r
217 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
\r
218 "fghCheckMenuStatus" );
\r
220 menuEntry->IsActive = GL_TRUE;
\r
221 menuEntry->Ordinal = menuID;
\r
224 * If this is not the same as the last active menu entry, deactivate
\r
225 * the previous entry. Specifically, if the previous active entry
\r
226 * was a submenu then deactivate it.
\r
228 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
\r
229 if( menu->ActiveEntry->SubMenu )
\r
230 fghDeactivateSubMenu( menu->ActiveEntry );
\r
232 if( menuEntry != menu->ActiveEntry )
\r
234 menu->Window->State.Redisplay = GL_TRUE;
\r
235 if( menu->ActiveEntry )
\r
236 menu->ActiveEntry->IsActive = GL_FALSE;
\r
239 menu->ActiveEntry = menuEntry;
\r
240 menu->IsActive = GL_TRUE; /* XXX Do we need this? */
\r
243 * OKi, we have marked that entry as active, but it would be also
\r
244 * nice to have its contents updated, in case it's a sub menu.
\r
245 * Also, ignore the return value of the check function:
\r
247 if( menuEntry->SubMenu )
\r
249 if ( ! menuEntry->SubMenu->IsActive )
\r
252 SFG_Window *current_window = fgStructure.CurrentWindow;
\r
254 /* Set up the initial menu position now... */
\r
255 menuEntry->SubMenu->IsActive = GL_TRUE;
\r
257 /* Set up the initial submenu position now: */
\r
258 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
\r
259 menuEntry->SubMenu->X = menu->X + menu->Width;
\r
260 menuEntry->SubMenu->Y = menu->Y +
\r
261 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
\r
263 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
\r
264 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
\r
266 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
\r
268 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
\r
269 FREEGLUT_MENU_HEIGHT -
\r
270 2 * FREEGLUT_MENU_BORDER );
\r
271 if( menuEntry->SubMenu->Y < 0 )
\r
272 menuEntry->SubMenu->Y = 0;
\r
275 fgSetWindow( menuEntry->SubMenu->Window );
\r
276 glutPositionWindow( menuEntry->SubMenu->X,
\r
277 menuEntry->SubMenu->Y );
\r
278 glutReshapeWindow( menuEntry->SubMenu->Width,
\r
279 menuEntry->SubMenu->Height );
\r
282 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
\r
283 fgSetWindow( current_window );
\r
284 menuEntry->SubMenu->Window->State.MouseX =
\r
285 x + menu->X - menuEntry->SubMenu->X;
\r
286 menuEntry->SubMenu->Window->State.MouseY =
\r
287 y + menu->Y - menuEntry->SubMenu->Y;
\r
288 fghCheckMenuStatus( menuEntry->SubMenu );
\r
291 /* Activate it because its parent entry is active */
\r
292 menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */
\r
295 /* Report back that we have caught the menu cursor */
\r
299 /* Looks like the menu cursor is somewhere else... */
\r
300 if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
\r
301 ( !menu->ActiveEntry->SubMenu ||
\r
302 !menu->ActiveEntry->SubMenu->IsActive ) )
\r
304 menu->Window->State.Redisplay = GL_TRUE;
\r
305 menu->ActiveEntry->IsActive = GL_FALSE;
\r
306 menu->ActiveEntry = NULL;
\r
313 * Displays a menu box and all of its submenus (if they are active)
\r
315 static void fghDisplayMenuBox( SFG_Menu* menu )
\r
317 SFG_MenuEntry *menuEntry;
\r
319 int border = FREEGLUT_MENU_BORDER;
\r
322 * Have the menu box drawn first. The +- values are
\r
323 * here just to make it more nice-looking...
\r
325 /* a non-black dark version of the below. */
\r
326 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
\r
327 glBegin( GL_QUAD_STRIP );
\r
328 glVertex2i( menu->Width , 0 );
\r
329 glVertex2i( menu->Width - border, border);
\r
330 glVertex2i( 0 , 0 );
\r
331 glVertex2i( border, border);
\r
332 glVertex2i( 0 , menu->Height );
\r
333 glVertex2i( border, menu->Height - border);
\r
336 /* a non-black dark version of the below. */
\r
337 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
\r
338 glBegin( GL_QUAD_STRIP );
\r
339 glVertex2i( 0 , menu->Height );
\r
340 glVertex2i( border, menu->Height - border);
\r
341 glVertex2i( menu->Width , menu->Height );
\r
342 glVertex2i( menu->Width - border, menu->Height - border);
\r
343 glVertex2i( menu->Width , 0 );
\r
344 glVertex2i( menu->Width - border, border);
\r
347 glColor4fv( menu_pen_back );
\r
348 glBegin( GL_QUADS );
\r
349 glVertex2i( border, border);
\r
350 glVertex2i( menu->Width - border, border);
\r
351 glVertex2i( menu->Width - border, menu->Height - border);
\r
352 glVertex2i( border, menu->Height - border);
\r
355 /* Check if any of the submenus is currently active... */
\r
356 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
\r
358 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
\r
360 /* Has the menu been marked as active, maybe? */
\r
361 if( menuEntry->IsActive )
\r
364 * That's truly right, and we need to have it highlighted.
\r
365 * There is an assumption that mouse cursor didn't move
\r
366 * since the last check of menu activity state:
\r
368 int menuID = menuEntry->Ordinal;
\r
370 /* So have the highlight drawn... */
\r
371 glColor4fv( menu_pen_hback );
\r
372 glBegin( GL_QUADS );
\r
373 glVertex2i( border,
\r
374 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
\r
375 glVertex2i( menu->Width - border,
\r
376 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
\r
377 glVertex2i( menu->Width - border,
\r
378 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
\r
379 glVertex2i( border,
\r
380 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
\r
385 /* Print the menu entries now... */
\r
387 glColor4fv( menu_pen_fore );
\r
389 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
\r
391 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
\r
393 /* If the menu entry is active, set the color to white */
\r
394 if( menuEntry->IsActive )
\r
395 glColor4fv( menu_pen_hfore );
\r
397 /* Move the raster into position... */
\r
398 /* Try to center the text - JCJ 31 July 2003*/
\r
401 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
\r
402 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
\r
405 /* Have the label drawn, character after character: */
\r
406 glutBitmapString( FREEGLUT_MENU_FONT,
\r
407 (unsigned char *)menuEntry->Text);
\r
409 /* If it's a submenu, draw a right arrow */
\r
410 if( menuEntry->SubMenu )
\r
412 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
\r
413 int x_base = menu->Width - 2 - width;
\r
414 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
\r
415 glBegin( GL_TRIANGLES );
\r
416 glVertex2i( x_base, y_base + 2*border);
\r
417 glVertex2i( menu->Width - 2, y_base +
\r
418 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
\r
419 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
\r
423 /* If the menu entry is active, reset the color */
\r
424 if( menuEntry->IsActive )
\r
425 glColor4fv( menu_pen_fore );
\r
430 * Private static function to set the parent window of a submenu and all
\r
433 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
\r
435 SFG_MenuEntry *menuEntry;
\r
437 menu->ParentWindow = window;
\r
439 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
\r
441 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
\r
442 if( menuEntry->SubMenu )
\r
443 fghSetMenuParentWindow( window, menuEntry->SubMenu );
\r
447 * Function to check for menu entry selection on menu deactivation
\r
449 static void fghExecuteMenuCallback( SFG_Menu* menu )
\r
451 SFG_MenuEntry *menuEntry;
\r
453 /* First of all check any of the active sub menus... */
\r
454 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
\r
456 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
\r
458 if( menuEntry->IsActive )
\r
460 if( menuEntry->SubMenu )
\r
461 fghExecuteMenuCallback( menuEntry->SubMenu );
\r
463 if( menu->Callback )
\r
465 SFG_Menu *save_menu = fgStructure.CurrentMenu;
\r
466 fgStructure.CurrentMenu = menu;
\r
467 menu->Callback( menuEntry->ID );
\r
468 fgStructure.CurrentMenu = save_menu;
\r
478 * Displays the currently active menu for the current window
\r
480 void fgDisplayMenu( void )
\r
482 SFG_Window* window = fgStructure.CurrentWindow;
\r
483 SFG_Menu* menu = NULL;
\r
485 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
\r
488 /* Check if there is an active menu attached to this window... */
\r
489 menu = window->ActiveMenu;
\r
490 freeglut_return_if_fail( menu );
\r
492 fgSetWindow( menu->Window );
\r
494 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
\r
497 glDisable( GL_DEPTH_TEST );
\r
498 glDisable( GL_TEXTURE_2D );
\r
499 glDisable( GL_LIGHTING );
\r
500 glDisable( GL_CULL_FACE );
\r
502 glMatrixMode( GL_PROJECTION );
\r
506 0, glutGet( GLUT_WINDOW_WIDTH ),
\r
507 glutGet( GLUT_WINDOW_HEIGHT ), 0,
\r
511 glMatrixMode( GL_MODELVIEW );
\r
515 fghDisplayMenuBox( menu );
\r
519 glMatrixMode( GL_PROJECTION );
\r
521 glMatrixMode( GL_MODELVIEW );
\r
524 glutSwapBuffers( );
\r
526 fgSetWindow ( window );
\r
530 * Activates a menu pointed by the function argument
\r
532 static void fghActivateMenu( SFG_Window* window, int button )
\r
536 /* We'll be referencing this menu a lot, so remember its address: */
\r
537 SFG_Menu* menu = window->Menu[ button ];
\r
538 SFG_Window* current_window = fgStructure.CurrentWindow;
\r
540 /* If the menu is already active in another window, deactivate it there */
\r
541 if ( menu->ParentWindow )
\r
542 menu->ParentWindow->ActiveMenu = NULL ;
\r
544 /* Mark the menu as active, so that it gets displayed: */
\r
545 window->ActiveMenu = menu;
\r
546 menu->IsActive = GL_TRUE;
\r
547 fghSetMenuParentWindow ( window, menu );
\r
548 fgState.ActiveMenus++;
\r
550 /* Set up the initial menu position now: */
\r
551 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
\r
552 fgSetWindow( window );
\r
553 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
\r
554 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
\r
556 if( menu->X + menu->Width > max_x )
\r
557 menu->X -=menu->Width;
\r
559 if( menu->Y + menu->Height > max_y )
\r
561 menu->Y -=menu->Height;
\r
566 menu->Window->State.MouseX =
\r
567 window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
\r
568 menu->Window->State.MouseY =
\r
569 window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
\r
571 fgSetWindow( menu->Window );
\r
572 glutPositionWindow( menu->X, menu->Y );
\r
573 glutReshapeWindow( menu->Width, menu->Height );
\r
576 menu->Window->ActiveMenu = menu;
\r
577 fghCheckMenuStatus( menu );
\r
578 fgSetWindow( current_window );
\r
582 * Update Highlight states of the menu
\r
584 * Current mouse position is in menu->Window->State.MouseX/Y.
\r
586 void fgUpdateMenuHighlight ( SFG_Menu *menu )
\r
588 fghCheckMenuStatus( menu );
\r
592 * Check whether an active menu absorbs a mouse click
\r
594 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
\r
595 int mouse_x, int mouse_y )
\r
598 * Near as I can tell, this is the menu behaviour:
\r
599 * - Down-click the menu button, menu not active: activate
\r
600 * the menu with its upper left-hand corner at the mouse
\r
602 * - Down-click any button outside the menu, menu active:
\r
603 * deactivate the menu
\r
604 * - Down-click any button inside the menu, menu active:
\r
605 * select the menu entry and deactivate the menu
\r
606 * - Up-click the menu button, menu not active: nothing happens
\r
607 * - Up-click the menu button outside the menu, menu active:
\r
609 * - Up-click the menu button inside the menu, menu active:
\r
610 * select the menu entry and deactivate the menu
\r
611 * Since menus can have submenus, we need to check this recursively.
\r
613 if( window->ActiveMenu )
\r
615 if( window == window->ActiveMenu->ParentWindow )
\r
617 window->ActiveMenu->Window->State.MouseX =
\r
618 mouse_x - window->ActiveMenu->X;
\r
619 window->ActiveMenu->Window->State.MouseY =
\r
620 mouse_y - window->ActiveMenu->Y;
\r
623 /* In the menu, invoke the callback and deactivate the menu */
\r
624 if( fghCheckMenuStatus( window->ActiveMenu ) )
\r
627 * Save the current window and menu and set the current
\r
628 * window to the window whose menu this is
\r
630 SFG_Window *save_window = fgStructure.CurrentWindow;
\r
631 SFG_Menu *save_menu = fgStructure.CurrentMenu;
\r
632 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
\r
633 fgSetWindow( parent_window );
\r
634 fgStructure.CurrentMenu = window->ActiveMenu;
\r
636 /* Execute the menu callback */
\r
637 fghExecuteMenuCallback( window->ActiveMenu );
\r
638 fgDeactivateMenu( parent_window );
\r
640 /* Restore the current window and menu */
\r
641 fgSetWindow( save_window );
\r
642 fgStructure.CurrentMenu = save_menu;
\r
646 * Outside the menu, deactivate if it's a downclick
\r
648 * XXX This isn't enough. A downclick outside of
\r
649 * XXX the interior of our freeglut windows should also
\r
650 * XXX deactivate the menu. This is more complicated.
\r
652 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
\r
655 * XXX Why does an active menu require a redisplay at
\r
656 * XXX this point? If this can come out cleanly, then
\r
657 * XXX it probably should do so; if not, a comment should
\r
660 if( ! window->IsMenu )
\r
661 window->State.Redisplay = GL_TRUE;
\r
666 /* No active menu, let's check whether we need to activate one. */
\r
667 if( ( 0 <= button ) &&
\r
668 ( FREEGLUT_MAX_MENUS > button ) &&
\r
669 ( window->Menu[ button ] ) &&
\r
672 /* XXX Posting a requisite Redisplay seems bogus. */
\r
673 window->State.Redisplay = GL_TRUE;
\r
674 fghActivateMenu( window, button );
\r
682 * Deactivates a menu pointed by the function argument.
\r
684 void fgDeactivateMenu( SFG_Window *window )
\r
686 SFG_Window *parent_window = NULL;
\r
688 /* Check if there is an active menu attached to this window... */
\r
689 SFG_Menu* menu = window->ActiveMenu;
\r
690 SFG_MenuEntry *menuEntry;
\r
692 /* Did we find an active window? */
\r
693 freeglut_return_if_fail( menu );
\r
695 parent_window = menu->ParentWindow;
\r
697 /* Hide the present menu's window */
\r
698 fgSetWindow( menu->Window );
\r
701 /* Forget about having that menu active anymore, now: */
\r
702 menu->Window->ActiveMenu = NULL;
\r
703 menu->ParentWindow->ActiveMenu = NULL;
\r
704 fghSetMenuParentWindow ( NULL, menu );
\r
705 menu->IsActive = GL_FALSE;
\r
706 menu->ActiveEntry = NULL;
\r
708 fgState.ActiveMenus--;
\r
710 /* Hide all submenu windows, and the root menu's window. */
\r
711 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
\r
713 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
\r
715 menuEntry->IsActive = GL_FALSE;
\r
717 /* Is that an active submenu by any case? */
\r
718 if( menuEntry->SubMenu )
\r
719 fghDeactivateSubMenu( menuEntry );
\r
722 fgSetWindow ( parent_window ) ;
\r
726 * Recalculates current menu's box size
\r
728 void fghCalculateMenuBoxSize( void )
\r
730 SFG_MenuEntry* menuEntry;
\r
731 int width = 0, height = 0;
\r
733 /* Make sure there is a current menu set */
\r
734 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
736 /* The menu's box size depends on the menu entries: */
\r
737 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
\r
739 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
\r
741 /* Update the menu entry's width value */
\r
742 menuEntry->Width = glutBitmapLength(
\r
743 FREEGLUT_MENU_FONT,
\r
744 (unsigned char *)menuEntry->Text
\r
748 * If the entry is a submenu, then it needs to be wider to
\r
749 * accomodate the arrow. JCJ 31 July 2003
\r
751 if (menuEntry->SubMenu )
\r
752 menuEntry->Width += glutBitmapLength(
\r
753 FREEGLUT_MENU_FONT,
\r
754 (unsigned char *)"_"
\r
757 /* Check if it's the biggest we've found */
\r
758 if( menuEntry->Width > width )
\r
759 width = menuEntry->Width;
\r
761 height += FREEGLUT_MENU_HEIGHT;
\r
764 /* Store the menu's box size now: */
\r
765 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
\r
766 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
\r
770 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
\r
773 * Creates a new menu object, adding it to the freeglut structure
\r
775 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
\r
777 /* The menu object creation code resides in freeglut_structure.c */
\r
778 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
\r
779 return fgCreateMenu( callback )->ID;
\r
782 #if TARGET_HOST_MS_WINDOWS
\r
783 int FGAPIENTRY __glutCreateMenuWithExit( void(* callback)( int ), void (__cdecl *exit_function)(int) )
\r
785 __glutExitFunc = exit_function;
\r
786 return glutCreateMenu( callback );
\r
791 * Destroys a menu object, removing all references to it
\r
793 void FGAPIENTRY glutDestroyMenu( int menuID )
\r
797 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
\r
798 menu = fgMenuByID( menuID );
\r
800 freeglut_return_if_fail( menu );
\r
802 /* The menu object destruction code resides in freeglut_structure.c */
\r
803 fgDestroyMenu( menu );
\r
807 * Returns the ID number of the currently active menu
\r
809 int FGAPIENTRY glutGetMenu( void )
\r
811 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
\r
813 if( fgStructure.CurrentMenu )
\r
814 return fgStructure.CurrentMenu->ID;
\r
820 * Sets the current menu given its menu ID
\r
822 void FGAPIENTRY glutSetMenu( int menuID )
\r
826 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
\r
827 menu = fgMenuByID( menuID );
\r
829 freeglut_return_if_fail( menu );
\r
831 fgStructure.CurrentMenu = menu;
\r
835 * Adds a menu entry to the bottom of the current menu
\r
837 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
\r
839 SFG_MenuEntry* menuEntry;
\r
840 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
\r
841 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
\r
842 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
844 menuEntry->Text = strdup( label );
\r
845 menuEntry->ID = value;
\r
847 /* Have the new menu entry attached to the current menu */
\r
848 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
\r
850 fghCalculateMenuBoxSize( );
\r
854 * Add a sub menu to the bottom of the current menu
\r
856 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
\r
858 SFG_MenuEntry *menuEntry;
\r
861 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
\r
862 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
\r
863 subMenu = fgMenuByID( subMenuID );
\r
865 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
866 freeglut_return_if_fail( subMenu );
\r
868 menuEntry->Text = strdup( label );
\r
869 menuEntry->SubMenu = subMenu;
\r
870 menuEntry->ID = -1;
\r
872 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
\r
873 fghCalculateMenuBoxSize( );
\r
877 * Changes the specified menu item in the current menu into a menu entry
\r
879 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
\r
881 SFG_MenuEntry* menuEntry = NULL;
\r
883 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
\r
884 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
886 /* Get n-th menu entry in the current menu, starting from one: */
\r
887 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
\r
889 freeglut_return_if_fail( menuEntry );
\r
891 /* We want it to become a normal menu entry, so: */
\r
892 if( menuEntry->Text )
\r
893 free( menuEntry->Text );
\r
895 menuEntry->Text = strdup( label );
\r
896 menuEntry->ID = value;
\r
897 menuEntry->SubMenu = NULL;
\r
898 fghCalculateMenuBoxSize( );
\r
902 * Changes the specified menu item in the current menu into a sub-menu trigger.
\r
904 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
\r
908 SFG_MenuEntry* menuEntry;
\r
910 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
\r
911 subMenu = fgMenuByID( subMenuID );
\r
914 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
915 freeglut_return_if_fail( subMenu );
\r
917 /* Get n-th menu entry in the current menu, starting from one: */
\r
918 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
\r
920 freeglut_return_if_fail( menuEntry );
\r
922 /* We want it to become a sub menu entry, so: */
\r
923 if( menuEntry->Text )
\r
924 free( menuEntry->Text );
\r
926 menuEntry->Text = strdup( label );
\r
927 menuEntry->SubMenu = subMenu;
\r
928 menuEntry->ID = -1;
\r
929 fghCalculateMenuBoxSize( );
\r
933 * Removes the specified menu item from the current menu
\r
935 void FGAPIENTRY glutRemoveMenuItem( int item )
\r
937 SFG_MenuEntry* menuEntry;
\r
939 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
\r
940 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
942 /* Get n-th menu entry in the current menu, starting from one: */
\r
943 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
\r
945 freeglut_return_if_fail( menuEntry );
\r
947 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
\r
948 if ( menuEntry->Text )
\r
949 free( menuEntry->Text );
\r
952 fghCalculateMenuBoxSize( );
\r
956 * Attaches a menu to the current window
\r
958 void FGAPIENTRY glutAttachMenu( int button )
\r
960 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
\r
962 freeglut_return_if_fail( fgStructure.CurrentWindow );
\r
963 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
965 freeglut_return_if_fail( button >= 0 );
\r
966 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
\r
968 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
\r
972 * Detaches a menu from the current window
\r
974 void FGAPIENTRY glutDetachMenu( int button )
\r
976 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
\r
978 freeglut_return_if_fail( fgStructure.CurrentWindow );
\r
979 freeglut_return_if_fail( fgStructure.CurrentMenu );
\r
981 freeglut_return_if_fail( button >= 0 );
\r
982 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
\r
984 fgStructure.CurrentWindow->Menu[ button ] = NULL;
\r
988 * A.Donev: Set and retrieve the menu's user data
\r
990 void* FGAPIENTRY glutGetMenuData( void )
\r
992 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
\r
993 return fgStructure.CurrentMenu->UserData;
\r
996 void FGAPIENTRY glutSetMenuData(void* data)
\r
998 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
\r
999 fgStructure.CurrentMenu->UserData=data;
\r
1002 /*** END OF FILE ***/
\r