/*==============================================*/
/*  Menus.c - How to work with menus            */
/*                                              */
/*  Author:  Bary W Pollack                     */
/*  Updated: Feb. 28, 2004                      */
/*==============================================*/

#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>

GLsizei ww = 500, wh = 500;         /* world width and height */
GLfloat fVSize = 60;
int     nSize = 40;

/*==============================================*/

void myReshape (int w, int h)       /* screen resized; update internal world accordingly */
{
    ww = w;  wh = h;
    glutPostRedisplay ();
}

/*==============================================*/

void display (void)         /* the display callback */
{
    int  nLim = 10 + nSize;

    glClear (GL_COLOR_BUFFER_BIT);

    /* draw a rectangle */
    glLineWidth (2.0);
    glBegin (GL_LINE_LOOP);
        glColor3f (0.0, 0.0, 0.0);
        glVertex2i (10, 10);      glVertex2i (nLim, 10);
        glVertex2i (nLim, nLim);  glVertex2i (10, nLim);
    glEnd ();

    glFlush ();
}

/*==============================================*/

void simpleMenu (int id)
{
    switch (id)
    {
      case 1:  exit (0);
      case 2:  nSize *= 2;
               break;
      case 3:  nSize /= 2;
               if (nSize <= 0)
                   nSize = 1;
               break;
    }
    glutPostRedisplay ();
}

/*==============================================*/

void sizeMenu (int id)
{
    switch (id)
    {
      case 2:  nSize *= 2;
               break;
      case 3:  nSize /= 2;
               if (nSize <= 0)
                   nSize = 1;
               break;
    }
    glutPostRedisplay ();
}

void topMenu (int id)
{
    if (id == 1)
        exit (0);
}

/*==============================================*/

void myinit ()      /* standard initialization for OpenGL and GLUT */
{
    int  subMenu;

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, fVSize, 0.0, fVSize);
    glMatrixMode (GL_MODELVIEW);
    glClearColor  (0.2, 0.9, 0.7, 1.0);

    /* define a simple menu */
    glutCreateMenu (simpleMenu);
    glutAddMenuEntry ("quit", 1);
    glutAddMenuEntry ("increase size x 2", 2);
    glutAddMenuEntry ("decrease size / 2", 3);
    glutAttachMenu (GLUT_LEFT_BUTTON);

    /* define a hierarchical menu */
    subMenu = glutCreateMenu (sizeMenu);
    glutAddMenuEntry ("increase size x 2", 2);
    glutAddMenuEntry ("decrease size / 2", 3);
    glutCreateMenu (topMenu);
    glutAddMenuEntry ("Quit", 1);
    glutAddSubMenu ("Resize", subMenu);
    glutAttachMenu (GLUT_RIGHT_BUTTON);
}

/*==============================================*/

int main (int argc, char **argv)
{
    printf ("\nDemonstrate Menus\n");
    printf ("\nPress the left mouse key for a simple pop-up menu\n");
    printf ("Press the right mouse key for a hierarchical pop-up menu\n\n");

    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB );
    glutInitWindowSize (wh, ww);
    glutInitWindowPosition (25, 50);
    glutCreateWindow ("Menus");

    glutReshapeFunc (myReshape);
    glutDisplayFunc (display);

    myinit ();

    glutMainLoop ();

    return 0;
}

/*==============================================*/

