menu is the tool that we often use, very convenient, intuitive. This section discusses how menu management is done in OpenGL.
Example 17: This example draws a white square on a blue background, clicks the right mouse button in the window, pops the menu, and when you select a different menu item, the Command window shows which menu is activated. This example is a main menu with 3 menu entries and two submenus in the main menu. All two submenus have 3 menu entries.
#include <GL/glut.h> #include <stdio.h> int menu,submenu1,submenu2; * * Draw a white square on the blue background. void Mydraw (void) { Glclearcolor (0.0,0.0,1.0,0.0); Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit); glcolor3f (1.0,1.0,1.0); Glortho ( -1.0,1.0,-1.0,1.0,-1.0,1.0); Glbegin (Gl_polygon); GLVERTEX2F ( -0.5,-0.5); GLVERTEX2F ( -0.5,0.5); GLVERTEX2F (0.5,0.5); GLVERTEX2F (0.5,-0.5); Glend (); Glutswapbuffers (); } /* Write out which menu is currently void Getcurrentmenu (void) { int nmenu; Nmenu = Glutgetmenu (); if (Nmenu = = menu) printf ("The current menu is Main menu.\n"); if (Nmenu = = subMenu1) printf ("The current menu is submenu1.\n"); if (Nmenu = = subMenu2) printf ("The current menu is submenu2.\n"); } /* submenu 1*/ void SubMenuFunc1 (int data) { Getcurrentmenu (); Switch (data) { Case 1: printf ("SubMenu1 ' s item 1 is triggered.\n"); Break Case 2: printf ("SubMenu1 ' s item 2 is triggered.\n"); Break Case 3: printf ("SubMenu1 ' s item 3 is triggered.\n"); Break } } /* submenu 2*/ void SubMenuFunc2 (int data) { Getcurrentmenu (); Switch (data) { Case 1: printf ("SubMenu2 ' s item 1 is triggered.\n"); Break Case 2: printf ("SubMenu2 ' s item 2 is triggered.\n"); Break Case 3: printf ("SubMenu2 ' s item 3 is triggered.\n"); Break } } /* Main Menu/* void Menufunc (int data) { Getcurrentmenu (); Switch (data) { Case 1: printf ("MainMenu ' s item 1 is triggered.\n"); Break Case 2: printf ("MainMenu ' s item 2 is triggered.\n"); Break Case 3: printf ("MainMenu ' s item 3 is triggered.\n"); Break } } int main (int argc,char * * argv) { /* Initialize * * Glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_double| glut_rgb| Glut_depth); Glutinitwindowsize (500,400); Glutinitwindowposition (100,100); /* Create window * * Glutcreatewindow ("menu"); Glutdisplayfunc (Mydraw); /* Create submenu 1 and Add menu entries/* subMenu1 = Glutcreatemenu (SUBMENUFUNC1); Glutaddmenuentry ("SubMenu1 ' s Item1", 1); Glutaddmenuentry ("SubMenu1 ' s Item2", 2); Glutaddmenuentry ("SubMenu1 ' s Item3", 3); Glutattachmenu (Glut_right_button); /* Create submenu 2 and Add menu entries/* SUBMENU2 = Glutcreatemenu (SUBMENUFUNC2); Glutaddmenuentry ("SubMenu2 ' s Item1", 1); Glutaddmenuentry ("SubMenu2 ' s Item2", 2); Glutaddmenuentry ("SubMenu2 ' s Item3", 3); Glutattachmenu (Glut_right_button); * * Create the main menu and add menu items and submenus * * menu = Glutcreatemenu (Menufunc); Glutaddmenuentry ("Item1", 1); Glutaddmenuentry ("Item2", 2); Glutaddmenuentry ("Item3", 3); Glutaddsubmenu ("submenu 1", subMenu1); Glutaddsubmenu ("submenu 2", SUBMENU2); Glutattachmenu (Glut_right_button); Glutmainloop ()//Enter GLUT event handling loop return 0; } |
int Glutcreatemenu (void (*func) (int value) creates a new pop-up menu and returns an integer identifier that uniquely identifies the menu. Func indicates the functionality of this menu.
int Glutaddmenuentry (char *name,int value) adds a menu entry at the bottom of the current menu.
Name Specifies the ASCII string that is displayed on the new menu entry.
value specifies the numeric value that is passed to the menu callback function when the menu entry is selected.
void Glutaddsubmenu (char *name,int menu) adds a submenu trigger entry at the bottom of the current menu.
Name Specifies the ASCII string that is displayed on the new menu trigger entry.
Meun identifier of the submenu that pops up when you select the submenu to trigger the entry.
The void glutattachmenu (int button) relates a mouse button in the current window to the identifier of the current menu.
The button indicates which button on the mouse. Glut_left_button, Glut_middle_button and Glut_right_button, respectively, indicate the left, middle and right mouse keys.
int glutgetmenu (void) Gets the identifier for the current menu, and Glutgetmenu returns a value of 0 if no menu exists or the previous current menu is deleted.