4 * Pull-down menu creation and handling.
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
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
32 #define G_LOG_DOMAIN "freeglut-menu"
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
40 * It would be cool if the submenu entries were somehow marked, for example with a dings
41 * on the right menu border or something like that. Think about the possibility of doing
42 * the menu on layers *or* using the native window system instead of OpenGL.
45 /* -- DEFINITIONS ---------------------------------------------------------- */
48 * We'll be using freeglut fonts to draw the menu
50 #define FREEGLUT_MENU_FONT GLUT_BITMAP_8_BY_13
51 #define FREEGLUT_MENU_HEIGHT 15
54 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
57 * Private static function to find a menu entry by index
59 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
64 for( entry = menu->Entries.First; entry; entry = entry->Node.Next)
75 * Private static function to check for the current menu/sub menu activity state
77 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
79 SFG_Window* window = fgStructure.Window;
80 SFG_MenuEntry* menuEntry;
84 * First of all check any of the active sub menus...
86 for( menuEntry = menu->Entries.First; menuEntry;
87 menuEntry = menuEntry->Node.Next )
90 * Is that an active sub menu by any case?
92 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
95 * OKi, have the sub-menu checked, too. If it returns TRUE, it will mean
96 * that it caught the mouse cursor and we do not need to regenerate
97 * the activity list, and so our parents do...
99 if( fghCheckMenuStatus( menuEntry->SubMenu ) == TRUE )
105 * That much about our sub menus, let's get to checking the current menu:
107 x = window->State.MouseX - menu->X;
108 y = window->State.MouseY - menu->Y;
111 * Mark all menu entries inactive...
113 for( menuEntry = menu->Entries.First; menuEntry;
114 menuEntry = menuEntry->Node.Next )
116 menuEntry->IsActive = FALSE;
119 menu->IsActive = FALSE;
122 * Check if the mouse cursor is contained within the current menu box
124 if( x >= 0 && x < menu->Width && y >= 0 && y < menu->Height )
127 * Calculation of the highlighted menu item is easy enough now:
129 int menuID = y / FREEGLUT_MENU_HEIGHT;
132 * The mouse cursor is somewhere over our box, check it out.
134 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
135 assert( menuEntry != NULL );
138 * Mark the menu as active...
140 menuEntry->IsActive = TRUE;
141 menuEntry->Ordinal = menuID;
144 * Don't forget about marking the current menu as active, too:
146 menu->IsActive = TRUE;
149 * OKi, we have marked that entry as active, but it would be also
150 * nice to have it's contents updated, in case it's a sub menu.
151 * Also, ignore the return value of the check function:
153 if( menuEntry->SubMenu != NULL )
155 int x = window->State.MouseX;
156 int y = window->State.MouseY;
159 * Set up the initial menu position now...
162 if( x > 15 ) menuEntry->SubMenu->X = x - 15; else menuEntry->SubMenu->X = 15;
163 if( y > 15 ) menuEntry->SubMenu->Y = y - 15; else menuEntry->SubMenu->Y = 15;
165 if( x > (glutGet( GLUT_WINDOW_WIDTH ) - menuEntry->SubMenu->Width - 15) )
166 menuEntry->SubMenu->X = glutGet( GLUT_WINDOW_WIDTH ) - menuEntry->SubMenu->Width - 15;
167 if( y > (glutGet( GLUT_WINDOW_HEIGHT ) - menuEntry->SubMenu->Height - 15) )
168 menuEntry->SubMenu->Y = glutGet( GLUT_WINDOW_HEIGHT ) - menuEntry->SubMenu->Height - 15;
171 * ...then check the submenu's state:
173 fghCheckMenuStatus( menuEntry->SubMenu );
177 * Report back that we have caught the menu cursor
183 * Looks like the menu cursor is somewhere else...
189 * Displays a menu box and all of it's submenus (if they are active)
191 static void fghDisplayMenuBox( SFG_Menu* menu )
193 SFG_MenuEntry *menuEntry;
197 * Have the menu box drawn first. The +- values are
198 * here just to make it more nice-looking...
200 glColor4f( 0.0, 0.0, 0.0, 1.0 );
202 glVertex2f( menu->X - 8 , menu->Y - 1 );
203 glVertex2f( menu->X + 8 + menu->Width, menu->Y - 1 );
204 glVertex2f( menu->X + 8 + menu->Width, menu->Y + 4 + menu->Height );
205 glVertex2f( menu->X - 8 , menu->Y + 4 + menu->Height );
208 glColor4f( 0.3, 0.4, 0.5, 1.0 );
210 glVertex2f( menu->X - 6 , menu->Y + 1 );
211 glVertex2f( menu->X + 6 + menu->Width, menu->Y + 1 );
212 glVertex2f( menu->X + 6 + menu->Width, menu->Y + 2 + menu->Height );
213 glVertex2f( menu->X - 6 , menu->Y + 2 + menu->Height );
217 * Check if any of the submenus is currently active...
219 for( menuEntry = menu->Entries.First; menuEntry;
220 menuEntry = menuEntry->Node.Next )
223 * Has the menu been marked as active, maybe?
225 if( menuEntry->IsActive == TRUE )
228 * That's truly right, and we need to have it highlighted.
229 * There is an assumption that mouse cursor didn't move
230 * since the last check of menu activity state:
232 int menuID = menuEntry->Ordinal;
235 * So have the highlight drawn...
237 glColor4f( 0.2, 0.3, 0.4, 1.0 );
239 glVertex2f( menu->X - 6 , menu->Y + (menuID + 0)*FREEGLUT_MENU_HEIGHT + 1 );
240 glVertex2f( menu->X + 6 + menu->Width, menu->Y + (menuID + 0)*FREEGLUT_MENU_HEIGHT + 1 );
241 glVertex2f( menu->X + 6 + menu->Width, menu->Y + (menuID + 1)*FREEGLUT_MENU_HEIGHT + 2 );
242 glVertex2f( menu->X - 6 , menu->Y + (menuID + 1)*FREEGLUT_MENU_HEIGHT + 2 );
248 * Print the menu entries now...
250 glColor4f( 1, 1, 1, 1 );
252 for( menuEntry = menu->Entries.First, i=0; menuEntry;
253 menuEntry = menuEntry->Node.Next, ++i )
256 * Move the raster into position...
260 menu->Y + (i + 1)*FREEGLUT_MENU_HEIGHT
264 * Have the label drawn, character after character:
266 glutBitmapString( FREEGLUT_MENU_FONT, menuEntry->Text);
270 * Now we are ready to check if any of our children needs to be redrawn:
272 for( menuEntry = menu->Entries.First; menuEntry;
273 menuEntry = menuEntry->Node.Next )
276 * Is that an active sub menu by any case?
278 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
281 * Yeah, indeed. Have it redrawn now:
283 fghDisplayMenuBox( menuEntry->SubMenu );
289 * Displays the currently active menu for the current window
291 void fgDisplayMenu( void )
293 SFG_Window* window = fgStructure.Window;
294 SFG_Menu* menu = NULL;
298 * Make sure there is a current window available
300 freeglut_assert_window;
303 * Check if there is an active menu attached to this window...
305 for( i=0; i<FREEGLUT_MAX_MENUS; i++ )
307 if( window->Menu[ i ] != NULL && window->MenuActive[ i ] == TRUE )
308 menu = window->Menu[ i ];
312 * Did we find an active window?
314 freeglut_return_if_fail( menu != NULL );
317 * Prepare the OpenGL state to do the rendering first:
319 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT );
321 glDisable( GL_DEPTH_TEST );
322 glDisable( GL_TEXTURE_2D );
323 glDisable( GL_LIGHTING );
324 glDisable( GL_CULL_FACE );
327 * We'll use an orthogonal projection matrix to draw the menu:
329 glMatrixMode( GL_PROJECTION );
333 0, glutGet( GLUT_WINDOW_WIDTH ),
334 glutGet( GLUT_WINDOW_HEIGHT ), 0,
339 * Model-view matix gets reset to identity:
341 glMatrixMode( GL_MODELVIEW );
346 * First of all, have the exact menu status check:
348 fghCheckMenuStatus( menu );
351 * The status has been updated and we're ready to have the menu drawn now:
353 fghDisplayMenuBox( menu );
356 * Restore the old OpenGL settings now
360 glMatrixMode( GL_MODELVIEW );
362 glMatrixMode( GL_PROJECTION );
367 * Activates a menu pointed by the function argument
369 void fgActivateMenu( int button )
371 SFG_Window* window = fgStructure.Window;
372 SFG_Menu* menu = NULL;
375 freeglut_assert_window;
378 * Mark the menu as active, so that it gets displayed:
380 window->MenuActive[ button ] = TRUE;
383 * We'll be referencing this menu a lot, so remember it's address:
385 menu = window->Menu[ button ];
388 * Grab the mouse cursor position respective to the current window
390 x = window->State.MouseX;
391 y = window->State.MouseY;
394 * Set up the initial menu position now:
396 if( x > 10 ) menu->X = x - 10; else menu->X = 5;
397 if( y > 10 ) menu->Y = y - 10; else menu->Y = 5;
399 if( x > (glutGet( GLUT_WINDOW_WIDTH ) - menu->Width ) )
400 menu->X = glutGet( GLUT_WINDOW_WIDTH ) - menu->Width;
401 if( y > (glutGet( GLUT_WINDOW_HEIGHT ) - menu->Height) )
402 menu->Y = glutGet( GLUT_WINDOW_HEIGHT ) - menu->Height;
406 * Private static function to check for menu entry selection on menu deactivation
408 static void fghCheckMenuSelect( SFG_Menu* menu )
410 SFG_MenuEntry *menuEntry;
413 * First of all check any of the active sub menus...
415 for( menuEntry = menu->Entries.First; menuEntry;
416 menuEntry = menuEntry->Node.Next)
419 * Is this menu entry active?
421 if( menuEntry->IsActive == TRUE )
424 * If this is not a sub menu, execute the menu callback and return...
426 if( menuEntry->SubMenu == NULL )
429 * ...certainly given that there is one...
431 if( menu->Callback != NULL )
432 menu->Callback( menuEntry->ID );
438 * Otherwise recurse into the submenu.
440 fghCheckMenuSelect( menuEntry->SubMenu );
443 * There is little sense in dwelling the search on
451 * Deactivates a menu pointed by the function argument.
453 void fgDeactivateMenu( int button )
455 SFG_Window* window = fgStructure.Window;
456 SFG_Menu* menu = NULL;
460 * Make sure there is a current window available...
462 freeglut_assert_window;
465 * Check if there is an active menu attached to this window...
467 for( i=0; i<FREEGLUT_MAX_MENUS; i++ )
469 if( window->Menu[ i ] != NULL && window->MenuActive[ i ] == TRUE )
470 menu = window->Menu[ i ];
474 * Did we find an active window?
476 freeglut_return_if_fail( menu != NULL );
479 * Check if there was any menu entry active. This would
480 * mean the user has selected a menu entry...
482 fghCheckMenuSelect( menu );
485 * Forget about having that menu active anymore, now:
487 fgStructure.Window->MenuActive[ button ] = FALSE;
491 * Recalculates current menu's box size
493 void fghCalculateMenuBoxSize( void )
495 SFG_MenuEntry* menuEntry;
496 int width = 0, height = 0;
499 * Make sure there is a current menu set
501 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
504 * The menu's box size depends on the menu entries:
506 for( menuEntry = fgStructure.Menu->Entries.First; menuEntry;
507 menuEntry = menuEntry->Node.Next)
510 * Update the menu entry's width value
512 menuEntry->Width = glutBitmapLength( FREEGLUT_MENU_FONT, menuEntry->Text );
515 * Check if it's the biggest we've found
517 if( menuEntry->Width > width )
518 width = menuEntry->Width;
520 height += FREEGLUT_MENU_HEIGHT;
524 * Store the menu's box size now:
526 fgStructure.Menu->Height = height;
527 fgStructure.Menu->Width = width;
531 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
534 * Creates a new menu object, adding it to the freeglut structure
536 int FGAPIENTRY glutCreateMenu( void (* callback)( int ) )
539 * The menu object creation code resides in freeglut_structure.c
541 return( fgCreateMenu( callback )->ID );
545 * Destroys a menu object, removing all references to it
547 void FGAPIENTRY glutDestroyMenu( int menuID )
549 SFG_Menu* menu = fgMenuByID( menuID );
551 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
554 * The menu object destruction code resides in freeglut_structure.c
556 fgDestroyMenu( menu );
560 * Returns the ID number of the currently active menu
562 int FGAPIENTRY glutGetMenu( void )
564 freeglut_assert_ready;
567 * Is there a current menu set?
569 if( fgStructure.Menu != NULL )
572 * Yes, there is indeed...
574 return( fgStructure.Menu->ID );
578 * No, there is no current menu at all
584 * Sets the current menu given it's menu ID
586 void FGAPIENTRY glutSetMenu( int menuID )
588 SFG_Menu* menu = fgMenuByID( menuID );
590 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
593 * The current menu pointer is stored in fgStructure.Menu
595 fgStructure.Menu = menu;
599 * Adds a menu entry to the bottom of the current menu
601 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
603 SFG_MenuEntry* menuEntry = calloc( sizeof(SFG_MenuEntry), 1 );
606 * Make sure there is a current menu set
608 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
611 * Fill in the appropriate values...
613 menuEntry->Text = strdup( label );
614 menuEntry->ID = value;
617 * Have the new menu entry attached to the current menu
619 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
622 * Update the menu's dimensions now
624 fghCalculateMenuBoxSize();
628 * Add a sub menu to the bottom of the current menu
630 void FGAPIENTRY glutAddSubMenu( const char* label, int subMenuID )
632 SFG_MenuEntry* menuEntry = calloc( sizeof(SFG_MenuEntry), 1 );
633 SFG_Menu* subMenu = fgMenuByID( subMenuID );
636 * Make sure there is a current menu and the sub menu
637 * we want to attach actually exists...
639 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
640 freeglut_return_if_fail( subMenu != NULL );
643 * Fill in the appropriate values
645 menuEntry->Text = strdup( label );
646 menuEntry->SubMenu = subMenu;
650 * Have the new menu entry attached to the current menu
652 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
655 * Update the menu's dimensions now
657 fghCalculateMenuBoxSize();
661 * Changes the specified menu item in the current menu into a menu entry
663 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
665 SFG_MenuEntry* menuEntry = NULL;
668 * Make sure there is a current menu set...
670 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
673 * Get n-th menu entry in the current menu, starting from one:
675 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
678 * Make sure the menu entry exists
680 freeglut_return_if_fail( menuEntry != NULL );
683 * We want it to become a normal menu entry, so:
685 if( menuEntry->Text != NULL )
686 free( menuEntry->Text );
688 menuEntry->Text = strdup( label );
689 menuEntry->ID = value;
690 menuEntry->SubMenu = NULL;
693 * Update the menu's dimensions now
695 fghCalculateMenuBoxSize();
699 * Changes the specified menu item in the current menu into a sub-menu trigger.
701 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID )
703 SFG_Menu* subMenu = fgMenuByID( subMenuID );
704 SFG_MenuEntry* menuEntry = NULL;
707 * Make sure there is a current menu set and the sub menu exists...
709 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
710 freeglut_return_if_fail( subMenu != NULL );
713 * Get n-th menu entry in the current menu, starting from one:
715 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
718 * Make sure the menu entry exists
720 freeglut_return_if_fail( menuEntry != NULL );
723 * We want it to become a sub menu entry, so:
725 if( menuEntry->Text != NULL )
726 free( menuEntry->Text );
728 menuEntry->Text = strdup( label );
729 menuEntry->SubMenu = subMenu;
733 * Update the menu's dimensions now
735 fghCalculateMenuBoxSize();
739 * Removes the specified menu item from the current menu
741 void FGAPIENTRY glutRemoveMenuItem( int item )
743 SFG_MenuEntry* menuEntry;
746 * Make sure there is a current menu set
748 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
751 * Get n-th menu entry in the current menu, starting from one:
753 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
756 * Make sure the menu entry exists
758 freeglut_return_if_fail( menuEntry != NULL );
761 * Removing a menu entry is quite simple...
763 fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
766 * Free the entry label string, too
768 free( menuEntry->Text );
773 * Update the menu's dimensions now
775 fghCalculateMenuBoxSize();
779 * Attaches a menu to the current window
781 void FGAPIENTRY glutAttachMenu( int button )
783 freeglut_assert_ready;
786 * There must be a current window and a current menu set:
788 freeglut_return_if_fail( fgStructure.Window != NULL || fgStructure.Menu != NULL );
791 * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
793 freeglut_return_if_fail( button == GLUT_LEFT_BUTTON || button == GLUT_MIDDLE_BUTTON || button == GLUT_RIGHT_BUTTON );
796 * It is safe now to attach the menu
798 fgStructure.Window->Menu[ button ] = fgStructure.Menu;
802 * Detaches a menu from the current window
804 void FGAPIENTRY glutDetachMenu( int button )
806 freeglut_assert_ready;
809 * There must be a current window set:
811 freeglut_return_if_fail( fgStructure.Window != NULL );
814 * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
816 freeglut_return_if_fail( button != 0 && button != 1 && button != 2 );
819 * It is safe now to detach the menu
821 fgStructure.Window->Menu[ button ] = NULL;
824 /*** END OF FILE ***/