Emit the correct LIBNAME to .pc files
[freeglut] / progs / demos / CallbackMaker / CallbackMaker.c
index 88cc3d3..a8cd18b 100644 (file)
@@ -8,52 +8,41 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <string.h>
 
 static int sequence_number = 0 ;
 
-int windows[2] = {0};
+#define CALLBACKMAKER_N_WINDOWS 4
+int windows[CALLBACKMAKER_N_WINDOWS] = {0};
 
 /* define status vars showing whether given callback has been called for given window */
-#define CALLBACK_CALLED_VAR(name) int name##_called[2] = {0};
-#define CALLBACK_1_VAR(name,field) int name##_##field[2], name##_seq[2] = {-1};
-#define CALLBACK_2_VARS(name,field1,field2) int name##_##field1[2], name##_##field2[2], name##_seq[2] = {-1};
-#define CALLBACK_3_VARS(name,field1,field2,field3) int name##_##field1[2], name##_##field2[2], name##_##field3[2], name##_seq[2] = {-1};
-#define CALLBACK_4_VARS(name,field1,field2,field3,field4) int name##_##field1[2], name##_##field2[2], name##_##field3[2], name##_##field4[2], name##_seq[2] = {-1};
-CALLBACK_CALLED_VAR(reshape);
-CALLBACK_2_VARS(reshape,width,height);
-CALLBACK_CALLED_VAR(key);
-CALLBACK_3_VARS(key,key,x,y);
-CALLBACK_CALLED_VAR(keyup);
-CALLBACK_3_VARS(keyup,key,x,y);
-CALLBACK_CALLED_VAR(special);
-CALLBACK_3_VARS(special,key,x,y);
-CALLBACK_CALLED_VAR(specialup);
-CALLBACK_3_VARS(specialup,key,x,y);
-CALLBACK_CALLED_VAR(visibility);
-CALLBACK_1_VAR(visibility,vis);
-CALLBACK_CALLED_VAR(joystick);
-CALLBACK_4_VARS(joystick,a,b,c,d);
-CALLBACK_CALLED_VAR(mouse);
-CALLBACK_4_VARS(mouse,button,updown,x,y);
-CALLBACK_CALLED_VAR(mousewheel);
-CALLBACK_4_VARS(mousewheel,number,direction,x,y);
-CALLBACK_CALLED_VAR(motion);
-CALLBACK_2_VARS(motion,x,y);
-CALLBACK_CALLED_VAR(passivemotion);
-CALLBACK_2_VARS(passivemotion,x,y);
-CALLBACK_CALLED_VAR(entry);
-CALLBACK_CALLED_VAR(close);
-/*CALLBACK_CALLED_VAR(overlaydisplay);
-CALLBACK_CALLED_VAR(windowstatus);
-CALLBACK_CALLED_VAR(spacemotion);
-CALLBACK_CALLED_VAR(spacerotation);
-CALLBACK_CALLED_VAR(spacebutton);
-CALLBACK_CALLED_VAR(buttonbox);
-CALLBACK_CALLED_VAR(dials);
-CALLBACK_CALLED_VAR(tabletmotion);
-CALLBACK_CALLED_VAR(tabletbutton);
-CALLBACK_CALLED_VAR(menustatus);*/
-int menudestroy_called = 0 ;    /* menudestroy is registered on menu, so don't use above */
+#define CALLBACK_CALLED_VAR(name)                            int name##_called[CALLBACKMAKER_N_WINDOWS]   = {0}
+#define CALLBACK_0V(name)                                    int name##_seq[CALLBACKMAKER_N_WINDOWS]      = {-1}; CALLBACK_CALLED_VAR(name)
+#define CALLBACK_1V(name,field)                              int name##_##field[CALLBACKMAKER_N_WINDOWS]  = {-1}; CALLBACK_0V(name)
+#define CALLBACK_2V(name,field1,field2)                      int name##_##field2[CALLBACKMAKER_N_WINDOWS] = {-1}; CALLBACK_1V(name,field1)
+#define CALLBACK_3V(name,field1,field2,field3)               int name##_##field3[CALLBACKMAKER_N_WINDOWS] = {-1}; CALLBACK_2V(name,field1,field2)
+#define CALLBACK_4V(name,field1,field2,field3,field4)        int name##_##field4[CALLBACKMAKER_N_WINDOWS] = {-1}; CALLBACK_3V(name,field1,field2,field3)
+#define CALLBACK_5V(name,field1,field2,field3,field4,field5) int name##_##field5[CALLBACKMAKER_N_WINDOWS] = {-1}; CALLBACK_4V(name,field1,field2,field3,field4)
+CALLBACK_2V(reshape,width,height);
+CALLBACK_2V(position,top,left);
+CALLBACK_1V(visibility,vis);
+CALLBACK_1V(windowStatus,state);
+CALLBACK_4V(key,key,x,y,mod);
+CALLBACK_4V(keyup,key,x,y,mod);
+CALLBACK_4V(special,key,x,y,mod);
+CALLBACK_4V(specialup,key,x,y,mod);
+CALLBACK_4V(joystick,a,b,c,d);
+CALLBACK_5V(mouse,button,updown,x,y,mod);
+CALLBACK_5V(mousewheel,number,direction,x,y,mod);
+CALLBACK_3V(motion,x,y,mod);
+CALLBACK_3V(passivemotion,x,y,mod);
+CALLBACK_1V(entry,state);
+CALLBACK_0V(close);
+/* menudestroy is registered on each menu, not a window */
+int menudestroy_called = 0;
+/* menustatus and menustate are global callbacks, set for all menus at the same time */
+int menustatus_called = 0;
+int menustate_called = 0;
 
 #define STRING_LENGTH   10
 
@@ -79,11 +68,37 @@ getWindowAndIdx(int *winIdx)
     int window = glutGetWindow();
 
     if (winIdx)
-        (*winIdx) = window==windows[0]?0:1;
+        (*winIdx) = window==windows[0] ? 0 :
+                    window==windows[1] ? 1 :
+                    window==windows[2] ? 2 : 3;
 
     return window;
 }
 
+static void
+Mod2Text(int mods, char *text)
+{
+    if (mods&GLUT_ACTIVE_CTRL)
+        strcat(text,"CTRL");
+    if (mods&GLUT_ACTIVE_SHIFT)
+    {
+        if (text[0])
+            strcat(text,"+SHIFT");
+        else
+            strcat(text,"SHIFT");
+    }
+    if (mods&GLUT_ACTIVE_ALT)
+    {
+        if (text[0])
+            strcat(text,"+ALT");
+        else
+            strcat(text,"ALT");
+    }
+
+    if (!text[0])
+        strcat(text,"none");
+}
+
 static void 
 Display(void)
 {
@@ -103,34 +118,57 @@ Display(void)
   glColor3ub ( 0, 0, 0 );
   glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 20 );   /* 10pt margin above 10pt letters */
 
+  if ( entry_called[winIdx] )
+  {
+    bitmapPrintf ( "Entry %d:  %d\n", entry_seq[winIdx], entry_state[winIdx] );
+  }
+  
+  if ( visibility_called[winIdx] )
+  {
+    bitmapPrintf ( "Visibility %d:  %d\n", visibility_seq[winIdx], visibility_vis[winIdx] );
+  }
+
+  if ( windowStatus_called[winIdx] )
+  {
+    bitmapPrintf ( "WindowStatus %d:  %d\n", windowStatus_seq[winIdx], windowStatus_state[winIdx] );
+  }
+
   if ( reshape_called[winIdx] )
   {
     bitmapPrintf ( "Reshape %d:  %d %d\n", reshape_seq[winIdx], reshape_width[winIdx], reshape_height[winIdx] );
   }
 
-  if ( key_called[winIdx] )
+  if ( position_called[winIdx] )
   {
-    bitmapPrintf ( "Key %d:  %d(%c) %d %d\n", key_seq[winIdx], key_key[winIdx], key_key[winIdx], key_x[winIdx], key_y[winIdx] );
+    bitmapPrintf ( "Position %d:  %d %d\n", position_seq[winIdx], position_left[winIdx], position_top[winIdx] );
   }
 
-  if ( special_called[winIdx] )
+  if ( key_called[winIdx] )
   {
-    bitmapPrintf ( "Special %d:  %d(%c) %d %d\n", special_seq[winIdx], special_key[winIdx], special_key[winIdx], special_x[winIdx], special_y[winIdx] );
+    char mods[50] = {0};
+    Mod2Text(key_mod[winIdx],mods);
+    bitmapPrintf ( "Key %d:  %d(%c) %d %d (mod: %s)\n", key_seq[winIdx], key_key[winIdx], key_key[winIdx], key_x[winIdx], key_y[winIdx], mods );
   }
 
-  if ( visibility_called[winIdx] )
+  if ( keyup_called[winIdx] )
   {
-    bitmapPrintf ( "Visibility %d:  %d\n", visibility_seq[winIdx], visibility_vis[winIdx] );
+    char mods[50] = {0};
+    Mod2Text(keyup_mod[winIdx],mods);
+    bitmapPrintf ( "Key Up %d:  %d(%c) %d %d (mod: %s)\n", keyup_seq[winIdx], keyup_key[winIdx], keyup_key[winIdx], keyup_x[winIdx], keyup_y[winIdx], mods );
   }
 
-  if ( keyup_called[winIdx] )
+  if ( special_called[winIdx] )
   {
-    bitmapPrintf ( "Key Up %d:  %d(%c) %d %d\n", keyup_seq[winIdx], keyup_key[winIdx], keyup_key[winIdx], keyup_x[winIdx], keyup_y[winIdx] );
+    char mods[50] = {0};
+    Mod2Text(special_mod[winIdx],mods);
+    bitmapPrintf ( "Special %d:  %d(%c) %d %d (mod: %s)\n", special_seq[winIdx], special_key[winIdx], special_key[winIdx], special_x[winIdx], special_y[winIdx], mods );
   }
 
   if ( specialup_called[winIdx] )
   {
-    bitmapPrintf ( "Special Up %d:  %d(%c) %d %d\n", specialup_seq[winIdx], specialup_key[winIdx], specialup_key[winIdx], specialup_x[winIdx], specialup_y[winIdx] );
+    char mods[50] = {0};
+    Mod2Text(specialup_mod[winIdx],mods);
+    bitmapPrintf ( "Special Up %d:  %d(%c) %d %d (mod: %s)\n", specialup_seq[winIdx], specialup_key[winIdx], specialup_key[winIdx], specialup_x[winIdx], specialup_y[winIdx], mods );
   }
 
   if ( joystick_called[winIdx] )
@@ -140,22 +178,30 @@ Display(void)
 
   if ( mouse_called[winIdx] )
   {
-    bitmapPrintf ( "Mouse %d:  %d %d %d %d\n", mouse_seq[winIdx], mouse_button[winIdx], mouse_updown[winIdx], mouse_x[winIdx], mouse_y[winIdx] );
+    char mods[50] = {0};
+    Mod2Text(mouse_mod[winIdx],mods);
+    bitmapPrintf ( "Mouse %d:  %d %d %d %d (mod: %s)\n", mouse_seq[winIdx], mouse_button[winIdx], mouse_updown[winIdx], mouse_x[winIdx], mouse_y[winIdx], mods );
   }
 
   if ( mousewheel_called[winIdx] )
   {
-    bitmapPrintf ( "Mouse Wheel %d:  %d %d %d %d\n", mousewheel_seq[winIdx], mousewheel_number[winIdx], mousewheel_direction[winIdx], mousewheel_x[winIdx], mousewheel_y[winIdx] );
+    char mods[50] = {0};
+    Mod2Text(mousewheel_mod[winIdx],mods);
+    bitmapPrintf ( "Mouse Wheel %d:  %d %d %d %d (mod: %s)\n", mousewheel_seq[winIdx], mousewheel_number[winIdx], mousewheel_direction[winIdx], mousewheel_x[winIdx], mousewheel_y[winIdx], mods );
   }
 
   if ( motion_called[winIdx] )
   {
-    bitmapPrintf ( "Motion %d:  %d %d\n", motion_seq[winIdx], motion_x[winIdx], motion_y[winIdx] );
+    char mods[50] = {0};
+    Mod2Text(motion_mod[winIdx],mods);
+    bitmapPrintf ( "Motion %d:  %d %d (mod: %s)\n", motion_seq[winIdx], motion_x[winIdx], motion_y[winIdx], mods );
   }
 
   if ( passivemotion_called[winIdx] )
   {
-    bitmapPrintf ( "Passive Motion %d:  %d %d\n", passivemotion_seq[winIdx], passivemotion_x[winIdx], passivemotion_y[winIdx] );
+    char mods[50] = {0};
+    Mod2Text(passivemotion_mod[winIdx],mods);
+    bitmapPrintf ( "Passive Motion %d:  %d %d (mod: %s)\n", passivemotion_seq[winIdx], passivemotion_x[winIdx], passivemotion_y[winIdx], mods );
   }
 
   glMatrixMode ( GL_PROJECTION );
@@ -172,7 +218,7 @@ Display(void)
 static void
 Warning(const char *fmt, va_list ap)
 {
-    printf("%6d Warning callback:\n");
+    printf("%6d Warning callback:\n",++sequence_number);
 
     /* print warning message */
     vprintf(fmt, ap);
@@ -182,10 +228,11 @@ static void
 Error(const char *fmt, va_list ap)
 {
     char dummy_string[STRING_LENGTH];
-    printf("%6d Error callback:\n");
+    printf("%6d Error callback:\n",++sequence_number);
 
     /* print warning message */
     vprintf(fmt, ap);
+    printf("\n");
 
     /* terminate program, after pause for input so user can see */
     printf ( "Please enter something to exit: " );
@@ -213,6 +260,20 @@ Reshape(int width, int height)
 }
 
 static void 
+Position(int left, int top)
+{
+  int winIdx;
+  int window = getWindowAndIdx(&winIdx);
+  printf ( "%6d Window %d Position Callback:  %d %d\n",
+            ++sequence_number, window, left, top ) ;
+  position_called[winIdx] = 1 ;
+  position_left[winIdx] = left ;
+  position_top[winIdx] = top ;
+  position_seq[winIdx] = sequence_number ;
+  glutPostRedisplay () ;
+}
+
+static void 
 Key(unsigned char key, int x, int y)
 {
   int winIdx;
@@ -224,49 +285,39 @@ Key(unsigned char key, int x, int y)
   key_x[winIdx] = x ;
   key_y[winIdx] = y ;
   key_seq[winIdx] = sequence_number ;
+  key_mod[winIdx] = glutGetModifiers() ;
   glutPostRedisplay () ;
 }
 
 static void 
-Special(int key, int x, int y)
+KeyUp(unsigned char key, int x, int y)
 {
   int winIdx;
   int window = getWindowAndIdx(&winIdx);
-  printf ( "%6d Window %d Special Key Callback:  %d %d %d\n",
+  printf ( "%6d Window %d Key Release Callback:  %d %d %d\n",
             ++sequence_number, window, key, x, y ) ;
-  special_called[winIdx] = 1 ;
-  special_key[winIdx] = key ;
-  special_x[winIdx] = x ;
-  special_y[winIdx] = y ;
-  special_seq[winIdx] = sequence_number ;
-  glutPostRedisplay () ;
-}
-
-static void 
-Visibility(int vis)
-{
-  int winIdx;
-  int window = getWindowAndIdx(&winIdx);
-  printf ( "%6d Window %d Visibility Callback:  %d\n",
-            ++sequence_number, window, vis ) ;
-  visibility_called[winIdx] = 1 ;
-  visibility_vis[winIdx] = vis ;
-  visibility_seq[winIdx] = sequence_number ;
+  keyup_called[winIdx] = 1 ;
+  keyup_key[winIdx] = key ;
+  keyup_x[winIdx] = x ;
+  keyup_y[winIdx] = y ;
+  keyup_seq[winIdx] = sequence_number ;
+  keyup_mod[winIdx] = glutGetModifiers() ;
   glutPostRedisplay () ;
 }
 
 static void 
-KeyUp(unsigned char key, int x, int y)
+Special(int key, int x, int y)
 {
   int winIdx;
   int window = getWindowAndIdx(&winIdx);
-  printf ( "%6d Window %d Key Release Callback:  %d %d %d\n",
+  printf ( "%6d Window %d Special Key Callback:  %d %d %d\n",
             ++sequence_number, window, key, x, y ) ;
-  keyup_called[winIdx] = 1 ;
-  keyup_key[winIdx] = key ;
-  keyup_x[winIdx] = x ;
-  keyup_y[winIdx] = y ;
-  keyup_seq[winIdx] = sequence_number ;
+  special_called[winIdx] = 1 ;
+  special_key[winIdx] = key ;
+  special_x[winIdx] = x ;
+  special_y[winIdx] = y ;
+  special_seq[winIdx] = sequence_number ;
+  special_mod[winIdx] = glutGetModifiers() ;
   glutPostRedisplay () ;
 }
 
@@ -282,6 +333,7 @@ SpecialUp(int key, int x, int y)
   specialup_x[winIdx] = x ;
   specialup_y[winIdx] = y ;
   specialup_seq[winIdx] = sequence_number ;
+  specialup_mod[winIdx] = glutGetModifiers() ;
   glutPostRedisplay () ;
 }
 
@@ -314,6 +366,7 @@ Mouse(int button, int updown, int x, int y)
   mouse_x[winIdx] = x ;
   mouse_y[winIdx] = y ;
   mouse_seq[winIdx] = sequence_number ;
+  mouse_mod[winIdx] = glutGetModifiers() ;
   glutPostRedisplay () ;
 }
 
@@ -330,6 +383,7 @@ MouseWheel(int wheel_number, int direction, int x, int y)
   mousewheel_x[winIdx] = x ;
   mousewheel_y[winIdx] = y ;
   mousewheel_seq[winIdx] = sequence_number ;
+  mousewheel_mod[winIdx] = glutGetModifiers() ;
   glutPostRedisplay () ;
 }
 
@@ -344,6 +398,7 @@ Motion(int x, int y)
   motion_x[winIdx] = x ;
   motion_y[winIdx] = y ;
   motion_seq[winIdx] = sequence_number ;
+  motion_mod[winIdx] = glutGetModifiers() ;
   glutPostRedisplay () ;
 }
 
@@ -358,15 +413,20 @@ PassiveMotion(int x, int y)
   passivemotion_x[winIdx] = x ;
   passivemotion_y[winIdx] = y ;
   passivemotion_seq[winIdx] = sequence_number ;
+  passivemotion_mod[winIdx] = glutGetModifiers() ;
   glutPostRedisplay () ;
 }
 
 static void 
 Entry(int state)
 {
-  int window = getWindowAndIdx(NULL);
+  int winIdx;
+  int window = getWindowAndIdx(&winIdx);
   printf ( "%6d Window %d Entry Callback:  %d\n",
             ++sequence_number, window, state ) ;
+  entry_called[winIdx] = 1 ;
+  entry_seq[winIdx] = sequence_number;
+  entry_state[winIdx] = state;
   glutPostRedisplay () ;
 }
 
@@ -388,11 +448,28 @@ OverlayDisplay(void)
 }
 
 static void 
+Visibility(int vis)
+{
+  int winIdx;
+  int window = getWindowAndIdx(&winIdx);
+  printf ( "%6d Window %d Visibility Callback:  %d\n",
+            ++sequence_number, window, vis ) ;
+  visibility_called[winIdx] = 1 ;
+  visibility_vis[winIdx] = vis ;
+  visibility_seq[winIdx] = sequence_number ;
+  glutPostRedisplay () ;
+}
+
+static void 
 WindowStatus(int state)
 {
-  int window = getWindowAndIdx(NULL);
+  int winIdx;
+  int window = getWindowAndIdx(&winIdx);
   printf ( "%6d Window %d WindowStatus Callback:  %d\n",
             ++sequence_number, window, state ) ;
+  windowStatus_called[winIdx] = 1 ;
+  windowStatus_state[winIdx] = state ;
+  windowStatus_seq[winIdx] = sequence_number ;
   glutPostRedisplay () ;
 }
 
@@ -460,32 +537,45 @@ TabletButton(int button, int updown, int x, int y)
 }
 
 static void
-MenuCallback ( int menuID )
+MenuCallback ( int value )
 {
+  int menu = glutGetMenu();
   int window = getWindowAndIdx(NULL);
-  printf( "%6d Window %d MenuCallback - menuID is %d\n",
-          ++sequence_number, window, menuID );
+  printf( "%6d Menu %d MenuCallback for menu opened in Window %d - value is %d\n",
+          ++sequence_number, menu, window, value );
 }
 
 static void 
 MenuDestroy( void )
 {
-  int window = getWindowAndIdx(NULL);
+  int menu = glutGetMenu();
   menudestroy_called = 1 ;
-  printf ( "%6d Window %d MenuDestroy Callback\n",
-            ++sequence_number, window ) ;
-
-  if (window)   /* When destroyed when shutting down, not always a window defined... */
-    glutPostRedisplay () ;
+  printf ( "%6d Menu %d MenuDestroy Callback\n",
+            ++sequence_number, menu ) ;
 }
 
 static void 
 MenuStatus( int status, int x, int y )
 {
+  /* Menu and window for which this event is triggered are current when the callback is called */
+  int menu = glutGetMenu();
   int window = getWindowAndIdx(NULL);
-  menudestroy_called = 1 ;
-  printf ( "%6d Window %d MenuStatus Callback:  %d %d %d\n",
-            ++sequence_number, window, status, x, y ) ;
+  menustatus_called = 1 ;
+  printf ( "%6d Menu %d MenuStatus Callback in Window %d:  %d %d %d\n",
+            ++sequence_number, menu, window, status, x, y ) ;
+  glutPostRedisplay () ;
+}
+
+
+static void 
+MenuState( int status )
+{
+  /* Menu and window for which this event is triggered are current when the callback is called */
+  int menu = glutGetMenu();
+  int window = getWindowAndIdx(NULL);
+  menustate_called = 1 ;
+  printf ( "%6d Menu %d MenuState Callback in Window %d:  %d\n",
+            ++sequence_number, menu, window, status) ;
   glutPostRedisplay () ;
 }
 
@@ -494,6 +584,39 @@ static void Idle ( void )
   ++sequence_number ;
 }
 
+static void SetWindowCallbacks( int first )
+{
+    /* All these callbacks are set for only the current window */
+    glutDisplayFunc( Display );
+    glutReshapeFunc( Reshape );
+    glutPositionFunc( Position );
+    glutKeyboardFunc( Key );
+    glutSpecialFunc( Special );
+    glutKeyboardUpFunc( KeyUp );
+    glutSpecialUpFunc( SpecialUp );
+    if (first)
+        glutJoystickFunc( Joystick, 100 );
+    glutMouseFunc ( Mouse ) ;
+    glutMouseWheelFunc ( MouseWheel ) ;
+    glutMotionFunc ( Motion ) ;
+    glutPassiveMotionFunc ( PassiveMotion ) ;
+    glutEntryFunc ( Entry ) ;
+    glutCloseFunc ( Close ) ;
+    glutOverlayDisplayFunc ( OverlayDisplay ) ;
+    glutSpaceballMotionFunc ( SpaceMotion ) ;
+    glutSpaceballRotateFunc ( SpaceRotation ) ;
+    glutSpaceballButtonFunc ( SpaceButton ) ;
+    glutButtonBoxFunc ( ButtonBox ) ;
+    glutDialsFunc ( Dials ) ;
+    glutTabletMotionFunc ( TabletMotion ) ;
+    glutTabletButtonFunc ( TabletButton ) ;
+    /* glutVisibilityFunc is deprecated in favor of glutWindowStatusFunc, which provides more detail.
+     * Setting one of these overwrites the other (see docs).
+     */
+    glutVisibilityFunc ( Visibility );  /* This will thus never be called, as glutWindowStatusFunc is set afterwards */
+    glutWindowStatusFunc ( WindowStatus ) ;
+}
+
 int 
 main(int argc, char *argv[])
 {
@@ -507,97 +630,104 @@ main(int argc, char *argv[])
   glutInitWindowPosition ( 140, 140 );
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
   glutInit(&argc, argv);
+  /* global setting: mainloop does not return when a window is closed, only returns when all windows are closed */
   glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
+  /* global setting: repeated keys generating by keeping the key pressed down are passed on to the keyboard callback */
+  /* There are two functions to control this behavior, glutSetKeyRepeat to set it globally, and glutIgnoreKeyRepeat to set it per window.
+   * These two interact however. If key repeat is globally switched off (glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF)), it cannot be overridden
+   * (switched back on) for a specific window with glutIgnoreKeyRepeat. However, if key repeat is globally switched on
+   * (glutSetKeyRepeat(GLUT_KEY_REPEAT_ON)), it can be overridden (switched off) with glutIgnoreKeyRepeat on a per-window basis. That is
+   * what we demonstrate here.
+   */
+  glutSetKeyRepeat(GLUT_KEY_REPEAT_ON);
+
+  /* Set other global callback (global as in not associated with any specific menu or window) */
+  glutIdleFunc ( Idle );
+  glutMenuStatusFunc ( MenuStatus );
+  glutMenuStateFunc  ( MenuState ); /* Note that glutMenuStatusFunc is an enhanced version of the deprecated glutMenuStateFunc. */
+
 
+  /* Open first window */
   windows[0] = glutCreateWindow( "Callback Demo" );
   printf ( "Creating window %d as 'Callback Demo'\n", windows[0] ) ;
 
   glClearColor(1.0, 1.0, 1.0, 1.0);
 
-  glutDisplayFunc( Display );
-  glutReshapeFunc( Reshape );
-  glutKeyboardFunc( Key );
-  glutSpecialFunc( Special );
-  glutVisibilityFunc( Visibility );
-  glutKeyboardUpFunc( KeyUp );
-  glutSpecialUpFunc( SpecialUp );
-  glutJoystickFunc( Joystick, 100 );
-  glutMouseFunc ( Mouse ) ;
-  glutMouseWheelFunc ( MouseWheel ) ;
-  glutMotionFunc ( Motion ) ;
-  glutPassiveMotionFunc ( PassiveMotion ) ;
-  glutEntryFunc ( Entry ) ;
-  glutCloseFunc ( Close ) ;
-  glutOverlayDisplayFunc ( OverlayDisplay ) ;
-  glutWindowStatusFunc ( WindowStatus ) ;
-  glutSpaceballMotionFunc ( SpaceMotion ) ;
-  glutSpaceballRotateFunc ( SpaceRotation ) ;
-  glutSpaceballButtonFunc ( SpaceButton ) ;
-  glutButtonBoxFunc ( ButtonBox ) ;
-  glutDialsFunc ( Dials ) ;
-  glutTabletMotionFunc ( TabletMotion ) ;
-  glutTabletButtonFunc ( TabletButton ) ;
-  glutMenuStatusFunc ( MenuStatus );
-  glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
+  /* callbacks, settings and menus for this window */
+  SetWindowCallbacks( 1 );
+  glutIgnoreKeyRepeat(GL_TRUE);
+  glutSetIconTitle("Icon Test - Callback Demo");
 
   subMenuA = glutCreateMenu( MenuCallback );
-  glutAddMenuEntry( "Sub menu A1 (01)", 1 );
-  glutAddMenuEntry( "Sub menu A2 (02)", 2 );
-  glutAddMenuEntry( "Sub menu A3 (03)", 3 );
-  glutMenuDestroyFunc ( MenuDestroy );
+  glutAddMenuEntry( "Sub menu A1 (01)", 11 );
+  glutAddMenuEntry( "Sub menu A2 (02)", 12 );
+  glutAddMenuEntry( "Sub menu A3 (03)", 13 );
+  glutMenuDestroyFunc ( MenuDestroy );  /* callback specific to this menu */
+  /* Change font for this menu */
+  glutSetMenuFont(subMenuA, GLUT_BITMAP_HELVETICA_12);
 
   subMenuB = glutCreateMenu( MenuCallback );
-  glutAddMenuEntry( "Sub menu B1 (04)", 4 );
-  glutAddMenuEntry( "Sub menu B2 (05)", 5 );
-  glutAddMenuEntry( "Sub menu B3 (06)", 6 );
+  glutAddMenuEntry( "Sub menu B1 (04)", 14 );
+  glutAddMenuEntry( "Sub menu B2 (05)", 15 );
+  glutAddMenuEntry( "Sub menu B3 (06)", 16 );
   glutAddSubMenu( "Going to sub menu A", subMenuA );
-  glutMenuDestroyFunc ( MenuDestroy );
+  glutMenuDestroyFunc ( MenuDestroy );  /* callback specific to this menu */
+  glutSetMenuFont(subMenuB, GLUT_BITMAP_9_BY_15);
 
   menuID = glutCreateMenu( MenuCallback );
-  glutAddMenuEntry( "Entry one",   1 );
-  glutAddMenuEntry( "Entry two",   2 );
-  glutAddMenuEntry( "Entry three", 3 );
-  glutAddMenuEntry( "Entry four",  4 );
-  glutAddMenuEntry( "Entry five",  5 );
+  glutAddMenuEntry( "Entry one",   21 );
+  glutAddMenuEntry( "Entry two",   22 );
+  glutAddMenuEntry( "Entry three", 23 );
+  glutAddMenuEntry( "Entry four",  24 );
+  glutAddMenuEntry( "Entry five",  25 );
   glutAddSubMenu( "Enter sub menu A", subMenuA );
   glutAddSubMenu( "Enter sub menu B", subMenuB );
-  glutMenuDestroyFunc ( MenuDestroy );
+  glutMenuDestroyFunc ( MenuDestroy );  /* callback specific to this menu */
 
   glutAttachMenu( GLUT_LEFT_BUTTON );
 
-  glutInitWindowPosition ( 140+500+2*glutGet(GLUT_WINDOW_BORDER_WIDTH), 140 );
+
   /* Position second window right next to the first */
+  glutInitWindowPosition ( 140+500+2*glutGet(GLUT_WINDOW_BORDER_WIDTH), 140 );
+  glutInitWindowSize(600, 600);
   windows[1] = glutCreateWindow( "Second Window" );
   printf ( "Creating window %d as 'Second Window'\n", windows[1] ) ;
 
   glClearColor(1.0, 1.0, 1.0, 1.0);
 
-  glutDisplayFunc( Display );
-  glutReshapeFunc( Reshape );
-  glutKeyboardFunc( Key );
-  glutSpecialFunc( Special );
-  glutVisibilityFunc( Visibility );
-  glutKeyboardUpFunc( KeyUp );
-  glutSpecialUpFunc( SpecialUp );
-  /*  glutJoystickFunc( Joystick, 100 ); */
-  glutMouseFunc ( Mouse ) ;
-  glutMouseWheelFunc ( MouseWheel ) ;
-  glutMotionFunc ( Motion ) ;
-  glutPassiveMotionFunc ( PassiveMotion ) ;
-  glutEntryFunc ( Entry ) ;
-  glutCloseFunc ( Close ) ;
-  glutOverlayDisplayFunc ( OverlayDisplay ) ;
-  glutWindowStatusFunc ( WindowStatus ) ;
-  glutSpaceballMotionFunc ( SpaceMotion ) ;
-  glutSpaceballRotateFunc ( SpaceRotation ) ;
-  glutSpaceballButtonFunc ( SpaceButton ) ;
-  glutButtonBoxFunc ( ButtonBox ) ;
-  glutDialsFunc ( Dials ) ;
-  glutTabletMotionFunc ( TabletMotion ) ;
-  glutTabletButtonFunc ( TabletButton ) ;
-  glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
+  /* callbacks, settings and menus for this window */
+  SetWindowCallbacks( 0 );
+  glutIgnoreKeyRepeat(GL_TRUE);
+
+  glutSetMenu(subMenuB);
+  glutAttachMenu( GLUT_RIGHT_BUTTON );
+
+
+  /* position a third window as a subwindow of the second */
+  windows[2] = glutCreateSubWindow(windows[1],0,300,600,300);
+  printf ( "Creating window %d as subwindow to 'Second Window'\n", windows[2] ) ;
+
+  glClearColor(0.7f, 0.7f, 0.7f, 1.0);
+
+  /* callbacks, settings and menus for this window */
+  SetWindowCallbacks( 0 );
+  glutSetCursor(GLUT_CURSOR_CROSSHAIR); /* Cursors are per window */
+
+  glutSetMenu(subMenuA);
+  glutAttachMenu( GLUT_RIGHT_BUTTON );
+
+
+  /* position a fourth window as a subsubwindow (grandchild) of the second */
+  windows[3] = glutCreateSubWindow(windows[2],300,0,300,300);
+  printf ( "Creating window %d as subsubwindow to 'Second Window'\n", windows[3] ) ;
+
+  glClearColor(0.4f, 0.4f, 0.4f, 1.0);
+
+  /* callbacks and menus for this window */
+  SetWindowCallbacks( 0 );
+  glutSetCursor(GLUT_CURSOR_INHERIT);   /* Inherit cursor look from parent (this is default on window creation) - comment the below to see in action */
+  glutSetCursor(GLUT_CURSOR_CYCLE);
 
-  glutIdleFunc ( Idle );
 
   printf ( "Please enter something to continue: " );
   fgets ( dummy_string, STRING_LENGTH, stdin );
@@ -606,5 +736,5 @@ main(int argc, char *argv[])
 
   printf ( "Back from the 'freeglut' main loop\n" ) ;
 
-  return 0;             /* ANSI C requires main to return int. */
+  return EXIT_SUCCESS;
 }