menu_test.c
上传用户:xk288cn
上传日期:2007-05-28
资源大小:4876k
文件大小:4k
- /* Copyright (c) Mark J. Kilgard, 1997. */
- /* This program is freely distributable without licensing fees
- and is provided without guarantee or warrantee expressed or
- implied. This program is -not- in the public domain. */
- #include <stdlib.h>
- #include <stdio.h>
- #include <GL/glut.h>
- int win, subwin;
- int mainmenu, submenu;
- int item = 666;
- void
- display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glFlush();
- }
- /* ARGSUSED1 */
- void
- gokey(unsigned char key, int x, int y)
- {
- char str[100];
- int mods;
- mods = glutGetModifiers();
- printf("key = %d, mods = 0x%xn", key, mods);
- if (mods & GLUT_ACTIVE_ALT) {
- switch (key) {
- case '1':
- printf("Change to sub menu 1n");
- glutChangeToSubMenu(1, "sub 1", submenu);
- break;
- case '2':
- printf("Change to sub menu 2n");
- glutChangeToSubMenu(2, "sub 2", submenu);
- break;
- case '3':
- printf("Change to sub menu 3n");
- glutChangeToSubMenu(3, "sub 3", submenu);
- break;
- case '4':
- printf("Change to sub menu 4n");
- glutChangeToSubMenu(4, "sub 4", submenu);
- break;
- case '5':
- printf("Change to sub menu 5n");
- glutChangeToSubMenu(5, "sub 5", submenu);
- break;
- }
- } else {
- switch (key) {
- case '1':
- printf("Change to menu entry 1n");
- glutChangeToMenuEntry(1, "entry 1", 1);
- break;
- case '2':
- printf("Change to menu entry 2n");
- glutChangeToMenuEntry(2, "entry 2", 2);
- break;
- case '3':
- printf("Change to menu entry 3n");
- glutChangeToMenuEntry(3, "entry 3", 3);
- break;
- case '4':
- printf("Change to menu entry 4n");
- glutChangeToMenuEntry(4, "entry 4", 4);
- break;
- case '5':
- printf("Change to menu entry 5n");
- glutChangeToMenuEntry(5, "entry 5", 5);
- break;
- case 'a':
- case 'A':
- printf("Adding menu entry %dn", item);
- sprintf(str, "added entry %d", item);
- glutAddMenuEntry(str, item);
- item++;
- break;
- case 's':
- case 'S':
- printf("Adding submenu %dn", item);
- sprintf(str, "added submenu %d", item);
- glutAddSubMenu(str, submenu);
- item++;
- break;
- case 'q':
- printf("Remove 1n");
- glutRemoveMenuItem(1);
- break;
- case 'w':
- printf("Remove 2n");
- glutRemoveMenuItem(2);
- break;
- case 'e':
- printf("Remove 3n");
- glutRemoveMenuItem(3);
- break;
- case 'r':
- printf("Remove 4n");
- glutRemoveMenuItem(4);
- break;
- case 't':
- printf("Remove 5n");
- glutRemoveMenuItem(5);
- break;
- }
- }
- }
- void
- keyboard(unsigned char key, int x, int y)
- {
- glutSetMenu(mainmenu);
- gokey(key, x, y);
- }
- void
- keyboard2(unsigned char key, int x, int y)
- {
- glutSetMenu(submenu);
- gokey(key, x, y);
- }
- void
- menu(int value)
- {
- printf("menu: entry = %dn", value);
- }
- void
- menu2(int value)
- {
- printf("menu2: entry = %dn", value);
- }
- int
- main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- win = glutCreateWindow("menu test");
- glClearColor(0.3, 0.3, 0.3, 0.0);
- glutDisplayFunc(display);
- glutKeyboardFunc(keyboard);
- submenu = glutCreateMenu(menu2);
- glutAddMenuEntry("Sub menu 1", 1001);
- glutAddMenuEntry("Sub menu 2", 1002);
- glutAddMenuEntry("Sub menu 3", 1003);
- mainmenu = glutCreateMenu(menu);
- glutAddMenuEntry("First", -1);
- glutAddMenuEntry("Second", -2);
- glutAddMenuEntry("Third", -3);
- glutAddSubMenu("Submenu init", submenu);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- subwin = glutCreateSubWindow(win, 50, 50, 50, 50);
- glClearColor(0.7, 0.7, 0.7, 0.0);
- glutDisplayFunc(display);
- glutKeyboardFunc(keyboard2);
- glutMainLoop();
- return 0; /* ANSI C requires main to return int. */
- }