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 "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
52 #define FREEGLUT_MENU_BORDER 8
55 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
58 * Private static function to find a menu entry by index
60 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
65 for( entry = menu->Entries.First; entry; entry = entry->Node.Next)
76 * Private static function to check for the current menu/sub menu activity state
78 static GLboolean fghCheckMenuStatus( SFG_Window* window, SFG_Menu* menu )
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 * OK, 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( window, 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 its contents updated, in case it's a sub menu.
151 * Also, ignore the return value of the check function:
153 if( menuEntry->SubMenu != NULL )
156 * Set up the initial menu position now...
159 menuEntry->SubMenu->X = menu->X + menu->Width ;
160 menuEntry->SubMenu->Y = menu->Y + menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT ;
163 * Make sure the submenu stays within the window
165 if ( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > glutGet ( GLUT_WINDOW_WIDTH ) )
167 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width ;
168 if ( menuEntry->SubMenu->X < 0 )
169 menuEntry->SubMenu->X = glutGet ( GLUT_WINDOW_WIDTH ) - menuEntry->SubMenu->Width ;
173 * ...then check the submenu's state:
175 fghCheckMenuStatus( window, menuEntry->SubMenu );
178 * Even if the submenu turned up inactive, activate it because its parent entry is active
180 menuEntry->SubMenu->IsActive = TRUE ;
184 * Report back that we have caught the menu cursor
190 * Looks like the menu cursor is somewhere else...
196 * Displays a menu box and all of its submenus (if they are active)
198 static void fghDisplayMenuBox( SFG_Menu* menu )
200 SFG_MenuEntry *menuEntry;
204 * Have the menu box drawn first. The +- values are
205 * here just to make it more nice-looking...
207 glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );
209 glVertex2i( menu->X , menu->Y - 1 );
210 glVertex2i( menu->X + menu->Width, menu->Y - 1 );
211 glVertex2i( menu->X + menu->Width, menu->Y + 4 + menu->Height );
212 glVertex2i( menu->X , menu->Y + 4 + menu->Height );
215 glColor4f( 0.3f, 0.4f, 0.5f, 1.0f );
217 glVertex2i( menu->X - 2 , menu->Y + 1 );
218 glVertex2i( menu->X - 2 + menu->Width, menu->Y + 1 );
219 glVertex2i( menu->X - 2 + menu->Width, menu->Y + 2 + menu->Height );
220 glVertex2i( menu->X - 2 , menu->Y + 2 + menu->Height );
224 * Check if any of the submenus is currently active...
226 for( menuEntry = menu->Entries.First; menuEntry;
227 menuEntry = menuEntry->Node.Next )
230 * Has the menu been marked as active, maybe?
232 if( menuEntry->IsActive == TRUE )
235 * That's truly right, and we need to have it highlighted.
236 * There is an assumption that mouse cursor didn't move
237 * since the last check of menu activity state:
239 int menuID = menuEntry->Ordinal;
242 * So have the highlight drawn...
244 glColor4f( 0.2f, 0.3f, 0.4f, 1.0f );
246 glVertex2i( menu->X - 2 , menu->Y + (menuID + 0)*FREEGLUT_MENU_HEIGHT + 1 );
247 glVertex2i( menu->X - 2 + menu->Width, menu->Y + (menuID + 0)*FREEGLUT_MENU_HEIGHT + 1 );
248 glVertex2i( menu->X - 2 + menu->Width, menu->Y + (menuID + 1)*FREEGLUT_MENU_HEIGHT + 2 );
249 glVertex2i( menu->X - 2 , menu->Y + (menuID + 1)*FREEGLUT_MENU_HEIGHT + 2 );
255 * Print the menu entries now...
257 glColor4f( 1, 1, 1, 1 );
259 for( menuEntry = menu->Entries.First, i=0; menuEntry;
260 menuEntry = menuEntry->Node.Next, ++i )
263 * Move the raster into position...
266 menu->X + FREEGLUT_MENU_BORDER,
267 menu->Y + (i + 1)*FREEGLUT_MENU_HEIGHT
271 * Have the label drawn, character after character:
273 glutBitmapString( FREEGLUT_MENU_FONT, menuEntry->Text);
276 * If it's a submenu, draw a right arrow
278 if ( menuEntry->SubMenu != NULL )
280 GLubyte arrow_char [] = { 0, 0, 32, 48, 56, 60, 62, 63, 62, 60, 56, 48, 32, 0, 0 } ;
281 int width = glutBitmapWidth ( FREEGLUT_MENU_FONT, ' ' ) ;
283 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
286 * Set up the pixel unpacking ways
288 glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
289 glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
290 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
291 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
292 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
293 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
295 glRasterPos2i ( menu->X + menu->Width - 2 - width,
296 menu->Y + (i + 1)*FREEGLUT_MENU_HEIGHT ) ;
297 glBitmap ( width, FREEGLUT_MENU_HEIGHT, 0, 0, 0.0, 0.0, arrow_char ) ;
303 * Now we are ready to check if any of our children needs to be redrawn:
305 for( menuEntry = menu->Entries.First; menuEntry;
306 menuEntry = menuEntry->Node.Next )
309 * Is that an active sub menu by any case?
311 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
314 * Yeah, indeed. Have it redrawn now:
316 fghDisplayMenuBox( menuEntry->SubMenu );
322 * Displays the currently active menu for the current window
324 void fgDisplayMenu( void )
326 SFG_Window* window = fgStructure.Window;
327 SFG_Menu* menu = NULL;
330 * Make sure there is a current window available
332 freeglut_assert_window;
335 * Check if there is an active menu attached to this window...
337 menu = window->ActiveMenu;
340 * Did we find an active window?
342 freeglut_return_if_fail( menu != NULL );
344 * Prepare the OpenGL state to do the rendering first:
346 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT );
348 glDisable( GL_DEPTH_TEST );
349 glDisable( GL_TEXTURE_2D );
350 glDisable( GL_LIGHTING );
351 glDisable( GL_CULL_FACE );
354 * We'll use an orthogonal projection matrix to draw the menu:
356 glMatrixMode( GL_PROJECTION );
360 0, glutGet( GLUT_WINDOW_WIDTH ),
361 glutGet( GLUT_WINDOW_HEIGHT ), 0,
366 * Model-view matix gets reset to identity:
368 glMatrixMode( GL_MODELVIEW );
373 * First of all, have the exact menu status check:
375 fghCheckMenuStatus( window, menu );
378 * The status has been updated and we're ready to have the menu drawn now:
380 fghDisplayMenuBox( menu );
383 * Restore the old OpenGL settings now
387 glMatrixMode( GL_PROJECTION );
389 glMatrixMode( GL_MODELVIEW );
394 * Activates a menu pointed by the function argument
396 void fgActivateMenu( SFG_Window* window, int button )
401 * We'll be referencing this menu a lot, so remember its address:
403 SFG_Menu* menu = window->Menu[ button ];
406 * Mark the menu as active, so that it gets displayed:
408 window->ActiveMenu = menu;
409 menu->IsActive = TRUE ;
412 * Grab the mouse cursor position respective to the current window
414 x = window->State.MouseX;
415 y = window->State.MouseY;
418 * Set up the initial menu position now:
423 fgSetWindow ( window ) ;
425 if( x > ( glutGet( GLUT_WINDOW_WIDTH ) - menu->Width ) )
426 menu->X = glutGet( GLUT_WINDOW_WIDTH ) - menu->Width;
427 if( y > ( glutGet( GLUT_WINDOW_HEIGHT ) - menu->Height) )
428 menu->Y = glutGet( GLUT_WINDOW_HEIGHT ) - menu->Height;
432 * Check whether an active menu absorbs a mouse click
434 GLboolean fgCheckActiveMenu ( SFG_Window *window, SFG_Menu *menu )
437 * Near as I can tell, this is the active menu behaviour:
438 * - Down-click any button outside the menu, menu active: deactivate the menu
439 * - Down-click any button inside the menu, menu active: select the menu entry and deactivate the menu
440 * - Up-click the menu button outside the menu, menu active: nothing happens
441 * - Up-click the menu button inside the menu, menu active: select the menu entry and deactivate the menu
442 * Since menus can have submenus, we need to check this recursively.
444 return fghCheckMenuStatus ( window, menu ) ;
448 * Function to check for menu entry selection on menu deactivation
450 void fgExecuteMenuCallback( SFG_Menu* menu )
452 SFG_MenuEntry *menuEntry;
455 * First of all check any of the active sub menus...
457 for( menuEntry = menu->Entries.First; menuEntry; menuEntry = menuEntry->Node.Next)
460 * Is this menu entry active?
462 if( menuEntry->IsActive == TRUE )
465 * If there is not a sub menu, execute the menu callback and return...
467 if( menuEntry->SubMenu == NULL )
470 * ...certainly given that there is one...
472 if( menu->Callback != NULL )
473 menu->Callback( menuEntry->ID );
479 * Otherwise recurse into the submenu.
481 fgExecuteMenuCallback( menuEntry->SubMenu );
484 * There is little sense in dwelling the search on
492 * Deactivates a menu pointed by the function argument.
494 void fgDeactivateMenu( SFG_Window *window )
497 * Check if there is an active menu attached to this window...
499 SFG_Menu* menu = window->ActiveMenu;
502 * Did we find an active window?
504 freeglut_return_if_fail( menu != NULL );
507 * Forget about having that menu active anymore, now:
509 window->ActiveMenu = NULL;
510 menu->IsActive = FALSE ;
514 * Recalculates current menu's box size
516 void fghCalculateMenuBoxSize( void )
518 SFG_MenuEntry* menuEntry;
519 int width = 0, height = 0;
522 * Make sure there is a current menu set
524 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
527 * The menu's box size depends on the menu entries:
529 for( menuEntry = fgStructure.Menu->Entries.First; menuEntry;
530 menuEntry = menuEntry->Node.Next)
533 * Update the menu entry's width value
535 menuEntry->Width = glutBitmapLength( FREEGLUT_MENU_FONT, menuEntry->Text );
538 * Check if it's the biggest we've found
540 if( menuEntry->Width > width )
541 width = menuEntry->Width;
543 height += FREEGLUT_MENU_HEIGHT;
547 * Store the menu's box size now:
549 fgStructure.Menu->Height = height;
550 fgStructure.Menu->Width = width + 2 * FREEGLUT_MENU_BORDER ;
554 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
557 * Creates a new menu object, adding it to the freeglut structure
559 int FGAPIENTRY glutCreateMenu( void (* callback)( int ) )
562 * The menu object creation code resides in freeglut_structure.c
564 return( fgCreateMenu( callback )->ID );
568 * Destroys a menu object, removing all references to it
570 void FGAPIENTRY glutDestroyMenu( int menuID )
572 SFG_Menu* menu = fgMenuByID( menuID );
574 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
577 * The menu object destruction code resides in freeglut_structure.c
579 fgDestroyMenu( menu );
583 * Returns the ID number of the currently active menu
585 int FGAPIENTRY glutGetMenu( void )
587 freeglut_assert_ready;
590 * Is there a current menu set?
592 if( fgStructure.Menu != NULL )
595 * Yes, there is indeed...
597 return( fgStructure.Menu->ID );
601 * No, there is no current menu at all
607 * Sets the current menu given its menu ID
609 void FGAPIENTRY glutSetMenu( int menuID )
611 SFG_Menu* menu = fgMenuByID( menuID );
613 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
616 * The current menu pointer is stored in fgStructure.Menu
618 fgStructure.Menu = menu;
622 * Adds a menu entry to the bottom of the current menu
624 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
626 SFG_MenuEntry* menuEntry = calloc( sizeof(SFG_MenuEntry), 1 );
629 * Make sure there is a current menu set
631 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
634 * Fill in the appropriate values...
636 menuEntry->Text = strdup( label );
637 menuEntry->ID = value;
640 * Have the new menu entry attached to the current menu
642 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
645 * Update the menu's dimensions now
647 fghCalculateMenuBoxSize();
651 * Add a sub menu to the bottom of the current menu
653 void FGAPIENTRY glutAddSubMenu( const char* label, int subMenuID )
655 SFG_MenuEntry* menuEntry = calloc( sizeof(SFG_MenuEntry), 1 );
656 SFG_Menu* subMenu = fgMenuByID( subMenuID );
659 * Make sure there is a current menu and the sub menu
660 * we want to attach actually exists...
662 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
663 freeglut_return_if_fail( subMenu != NULL );
666 * Fill in the appropriate values
668 menuEntry->Text = strdup( label );
669 menuEntry->SubMenu = subMenu;
673 * Have the new menu entry attached to the current menu
675 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
678 * Update the menu's dimensions now
680 fghCalculateMenuBoxSize();
684 * Changes the specified menu item in the current menu into a menu entry
686 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
688 SFG_MenuEntry* menuEntry = NULL;
691 * Make sure there is a current menu set...
693 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
696 * Get n-th menu entry in the current menu, starting from one:
698 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
701 * Make sure the menu entry exists
703 freeglut_return_if_fail( menuEntry != NULL );
706 * We want it to become a normal menu entry, so:
708 if( menuEntry->Text != NULL )
709 free( menuEntry->Text );
711 menuEntry->Text = strdup( label );
712 menuEntry->ID = value;
713 menuEntry->SubMenu = NULL;
716 * Update the menu's dimensions now
718 fghCalculateMenuBoxSize();
722 * Changes the specified menu item in the current menu into a sub-menu trigger.
724 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID )
726 SFG_Menu* subMenu = fgMenuByID( subMenuID );
727 SFG_MenuEntry* menuEntry = NULL;
730 * Make sure there is a current menu set and the sub menu exists...
732 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
733 freeglut_return_if_fail( subMenu != NULL );
736 * Get n-th menu entry in the current menu, starting from one:
738 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
741 * Make sure the menu entry exists
743 freeglut_return_if_fail( menuEntry != NULL );
746 * We want it to become a sub menu entry, so:
748 if( menuEntry->Text != NULL )
749 free( menuEntry->Text );
751 menuEntry->Text = strdup( label );
752 menuEntry->SubMenu = subMenu;
756 * Update the menu's dimensions now
758 fghCalculateMenuBoxSize();
762 * Removes the specified menu item from the current menu
764 void FGAPIENTRY glutRemoveMenuItem( int item )
766 SFG_MenuEntry* menuEntry;
769 * Make sure there is a current menu set
771 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
774 * Get n-th menu entry in the current menu, starting from one:
776 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
779 * Make sure the menu entry exists
781 freeglut_return_if_fail( menuEntry != NULL );
784 * Removing a menu entry is quite simple...
786 fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
789 * Free the entry label string, too
791 free( menuEntry->Text );
796 * Update the menu's dimensions now
798 fghCalculateMenuBoxSize();
802 * Attaches a menu to the current window
804 void FGAPIENTRY glutAttachMenu( int button )
806 freeglut_assert_ready;
809 * There must be a current window and a current menu set:
811 freeglut_return_if_fail( fgStructure.Window != NULL || fgStructure.Menu != NULL );
814 * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
816 freeglut_return_if_fail( button == GLUT_LEFT_BUTTON || button == GLUT_MIDDLE_BUTTON || button == GLUT_RIGHT_BUTTON );
819 * It is safe now to attach the menu
821 fgStructure.Window->Menu[ button ] = fgStructure.Menu;
825 * Detaches a menu from the current window
827 void FGAPIENTRY glutDetachMenu( int button )
829 freeglut_assert_ready;
832 * There must be a current window set:
834 freeglut_return_if_fail( fgStructure.Window != NULL );
837 * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
839 freeglut_return_if_fail( button != 0 && button != 1 && button != 2 );
842 * It is safe now to detach the menu
844 fgStructure.Window->Menu[ button ] = NULL;
848 * A.Donev: Set and retrieve the menu's user data
850 void* FGAPIENTRY glutGetMenuData( void )
852 return(fgStructure.Menu->UserData);
855 void FGAPIENTRY glutSetMenuData(void* data)
857 fgStructure.Menu->UserData=data;
860 /*** END OF FILE ***/