X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=progs%2Fdemos%2FCallbackMaker%2FCallbackMaker.c;h=e280148298e3eed85fa05e3456cfb307e96248b7;hb=8f7014249e5f2b352569c7b22aa190125aeef9ab;hp=deb8f4b5b28461128a86c72fbf20c742d82f7df4;hpb=40b0f0fcfb691a3b2f5f1c4ee5c57de333113052;p=freeglut diff --git a/progs/demos/CallbackMaker/CallbackMaker.c b/progs/demos/CallbackMaker/CallbackMaker.c index deb8f4b..e280148 100644 --- a/progs/demos/CallbackMaker/CallbackMaker.c +++ b/progs/demos/CallbackMaker/CallbackMaker.c @@ -8,27 +8,43 @@ #include #include #include +#include static int sequence_number = 0 ; -int reshape_called = 0, key_called = 0, special_called = 0, visibility_called = 0, - keyup_called = 0, specialup_called = 0, joystick_called = 0, mouse_called = 0, - mousewheel_called = 0, motion_called = 0, passivemotion_called = 0, entry_called = 0, - close_called = 0, overlaydisplay_called = 0, windowstatus_called = 0, - spacemotion_called = 0, spacerotation_called = 0, spacebutton_called = 0, - buttonbox_called = 0, dials_called = 0, tabletmotion_called = 0, tabletbutton_called = 0, - menudestroy_called = 0, menustatus_called = 0 ; -int reshape_width = -1, reshape_height = -1, reshape_seq = -1 ; -int key_key = -1, key_x = -1, key_y = -1, key_seq = -1 ; -int special_key = -1, special_x = -1, special_y = -1, special_seq = -1 ; -int visibility_vis = -1, visibility_seq = -1 ; -int keyup_key = -1, keyup_x = -1, keyup_y = -1, keyup_seq = -1 ; -int specialup_key = -1, specialup_x = -1, specialup_y = -1, specialup_seq = -1 ; -int joystick_a = -1, joystick_b = -1, joystick_c = -1, joystick_d = -1, joystick_seq = -1 ; /* Need meaningful names */ -int mouse_button = -1, mouse_updown = -1, mouse_x = -1, mouse_y = -1, mouse_seq = -1 ; -int mousewheel_number = -1, mousewheel_direction = -1, mousewheel_x = -1, mousewheel_y = -1, mousewheel_seq = -1 ; -int motion_x = -1, motion_y = -1, motion_seq = -1 ; -int passivemotion_x = -1, passivemotion_y = -1, passivemotion_seq = -1 ; +#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[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 static void bitmapPrintf (const char *fmt, ...) @@ -46,11 +62,48 @@ bitmapPrintf (const char *fmt, ...) glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ; } +static int +getWindowAndIdx(int *winIdx) +{ + int window = glutGetWindow(); + + if (winIdx) + (*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) { - int window = glutGetWindow () ; + int winIdx; + int window = getWindowAndIdx(&winIdx); glClear ( GL_COLOR_BUFFER_BIT ); glDisable ( GL_DEPTH_TEST ); @@ -63,61 +116,92 @@ Display(void) glPushMatrix (); glLoadIdentity (); glColor3ub ( 0, 0, 0 ); - glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 ); + glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 20 ); /* 10pt margin above 10pt letters */ - if ( reshape_called ) + if ( entry_called[winIdx] ) { - bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height ); + 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 ( key_called ) + if ( reshape_called[winIdx] ) { - bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y ); + bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq[winIdx], reshape_width[winIdx], reshape_height[winIdx] ); } - if ( special_called ) + if ( position_called[winIdx] ) { - bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y ); + bitmapPrintf ( "Position %d: %d %d\n", position_seq[winIdx], position_left[winIdx], position_top[winIdx] ); } - if ( visibility_called ) + if ( key_called[winIdx] ) { - bitmapPrintf ( "Visibility %d: %d\n", visibility_seq, visibility_vis ); + 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 ( keyup_called ) + if ( keyup_called[winIdx] ) { - bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y ); + 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 ( specialup_called ) + if ( special_called[winIdx] ) { - bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y ); + 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 ( joystick_called ) + if ( specialup_called[winIdx] ) { - bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d ); + 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 ( mouse_called ) + if ( joystick_called[winIdx] ) { - bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y ); + bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq[winIdx], joystick_a[winIdx], joystick_b[winIdx], joystick_c[winIdx], joystick_d[winIdx] ); } - if ( mousewheel_called ) + if ( mouse_called[winIdx] ) { - bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y ); + 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 ( motion_called ) + if ( mousewheel_called[winIdx] ) { - bitmapPrintf ( "Motion %d: %d %d\n", motion_seq, motion_x, motion_y ); + 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 ( passivemotion_called ) + if ( motion_called[winIdx] ) { - bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y ); + 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] ) + { + 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 ); @@ -134,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); @@ -143,195 +227,213 @@ Warning(const char *fmt, va_list ap) static void Error(const char *fmt, va_list ap) { -#define STRING_LENGTH 10 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"); - /* deInitialize the freeglut state */ - /* This all goes haywire of course when the error occurs during deinitialize, - * one might want to call exit directly as its possible freeglut is messed - * up internally when error is called. - */ - printf("Error callback: calling glutExit() to prepare for clean exit\n"); - glutExit(); - - /* terminate program */ + /* terminate program, after pause for input so user can see */ printf ( "Please enter something to exit: " ); fgets ( dummy_string, STRING_LENGTH, stdin ); + /* Call exit directly as freeglut is messed + * up internally when an error is called. + */ exit(1); } static void Reshape(int width, int height) { - int window = glutGetWindow () ; + int winIdx; + int window = getWindowAndIdx(&winIdx); printf ( "%6d Window %d Reshape Callback: %d %d\n", ++sequence_number, window, width, height ) ; - reshape_called = 1 ; - reshape_width = width ; - reshape_height = height ; - reshape_seq = sequence_number ; + reshape_called[winIdx] = 1 ; + reshape_width[winIdx] = width ; + reshape_height[winIdx] = height ; + reshape_seq[winIdx] = sequence_number ; + glViewport(0,0,width,height); glutPostRedisplay () ; } static void -Key(unsigned char key, int x, int y) +Position(int left, int top) { - int window = glutGetWindow () ; - printf ( "%6d Window %d Keyboard Callback: %d %d %d\n", - ++sequence_number, window, key, x, y ) ; - key_called = 1 ; - key_key = key ; - key_x = x ; - key_y = y ; - key_seq = sequence_number ; + 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 -Special(int key, int x, int y) +Key(unsigned char key, int x, int y) { - int window = glutGetWindow () ; - printf ( "%6d Window %d Special Key Callback: %d %d %d\n", + int winIdx; + int window = getWindowAndIdx(&winIdx); + printf ( "%6d Window %d Keyboard Callback: %d %d %d\n", ++sequence_number, window, key, x, y ) ; - special_called = 1 ; - special_key = key ; - special_x = x ; - special_y = y ; - special_seq = sequence_number ; + key_called[winIdx] = 1 ; + key_key[winIdx] = key ; + key_x[winIdx] = x ; + key_y[winIdx] = y ; + key_seq[winIdx] = sequence_number ; + key_mod[winIdx] = glutGetModifiers() ; glutPostRedisplay () ; } static void -Visibility(int vis) +KeyUp(unsigned char key, int x, int y) { - int window = glutGetWindow () ; - printf ( "%6d Window %d Visibility Callback: %d\n", - ++sequence_number, window, vis ) ; - visibility_called = 1 ; - visibility_vis = vis ; - visibility_seq = sequence_number ; + int winIdx; + int window = getWindowAndIdx(&winIdx); + printf ( "%6d Window %d Key Release 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 ; + keyup_mod[winIdx] = glutGetModifiers() ; glutPostRedisplay () ; } static void -KeyUp(unsigned char key, int x, int y) +Special(int key, int x, int y) { - int window = glutGetWindow () ; - printf ( "%6d Window %d Key Release Callback: %d %d %d\n", + int winIdx; + int window = getWindowAndIdx(&winIdx); + printf ( "%6d Window %d Special Key Callback: %d %d %d\n", ++sequence_number, window, key, x, y ) ; - keyup_called = 1 ; - keyup_key = key ; - keyup_x = x ; - keyup_y = y ; - keyup_seq = 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 () ; } static void SpecialUp(int key, int x, int y) { - int window = glutGetWindow () ; + int winIdx; + int window = getWindowAndIdx(&winIdx); printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n", ++sequence_number, window, key, x, y ) ; - specialup_called = 1 ; - specialup_key = key ; - specialup_x = x ; - specialup_y = y ; - specialup_seq = sequence_number ; + specialup_called[winIdx] = 1 ; + specialup_key[winIdx] = key ; + specialup_x[winIdx] = x ; + specialup_y[winIdx] = y ; + specialup_seq[winIdx] = sequence_number ; + specialup_mod[winIdx] = glutGetModifiers() ; glutPostRedisplay () ; } static void Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */ { - int window = glutGetWindow () ; + int winIdx; + int window = getWindowAndIdx(&winIdx); printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n", ++sequence_number, window, a, b, c, d ) ; - joystick_called = 1 ; - joystick_a = a ; - joystick_b = b ; - joystick_c = c ; - joystick_d = d ; - joystick_seq = sequence_number ; + joystick_called[winIdx] = 1 ; + joystick_a[winIdx] = a ; + joystick_b[winIdx] = b ; + joystick_c[winIdx] = c ; + joystick_d[winIdx] = d ; + joystick_seq[winIdx] = sequence_number ; glutPostRedisplay () ; } static void Mouse(int button, int updown, int x, int y) { - int window = glutGetWindow () ; + int winIdx; + int window = getWindowAndIdx(&winIdx); printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n", ++sequence_number, window, button, updown, x, y ) ; - mouse_called = 1 ; - mouse_button = button ; - mouse_updown = updown ; - mouse_x = x ; - mouse_y = y ; - mouse_seq = sequence_number ; + mouse_called[winIdx] = 1 ; + mouse_button[winIdx] = button ; + mouse_updown[winIdx] = updown ; + mouse_x[winIdx] = x ; + mouse_y[winIdx] = y ; + mouse_seq[winIdx] = sequence_number ; + mouse_mod[winIdx] = glutGetModifiers() ; glutPostRedisplay () ; } static void MouseWheel(int wheel_number, int direction, int x, int y) { - int window = glutGetWindow () ; + int winIdx; + int window = getWindowAndIdx(&winIdx); printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n", ++sequence_number, window, wheel_number, direction, x, y ) ; - mousewheel_called = 1 ; - mousewheel_number = wheel_number ; - mousewheel_direction = direction ; - mousewheel_x = x ; - mousewheel_y = y ; - mousewheel_seq = sequence_number ; + mousewheel_called[winIdx] = 1 ; + mousewheel_number[winIdx] = wheel_number ; + mousewheel_direction[winIdx] = direction ; + mousewheel_x[winIdx] = x ; + mousewheel_y[winIdx] = y ; + mousewheel_seq[winIdx] = sequence_number ; + mousewheel_mod[winIdx] = glutGetModifiers() ; glutPostRedisplay () ; } static void Motion(int x, int y) { - int window = glutGetWindow () ; + int winIdx; + int window = getWindowAndIdx(&winIdx); printf ( "%6d Window %d Mouse Motion Callback: %d %d\n", ++sequence_number, window, x, y ) ; - motion_called = 1 ; - motion_x = x ; - motion_y = y ; - motion_seq = sequence_number ; + motion_called[winIdx] = 1 ; + motion_x[winIdx] = x ; + motion_y[winIdx] = y ; + motion_seq[winIdx] = sequence_number ; + motion_mod[winIdx] = glutGetModifiers() ; glutPostRedisplay () ; } static void PassiveMotion(int x, int y) { - int window = glutGetWindow () ; + int winIdx; + int window = getWindowAndIdx(&winIdx); printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n", ++sequence_number, window, x, y ) ; - passivemotion_called = 1 ; - passivemotion_x = x ; - passivemotion_y = y ; - passivemotion_seq = sequence_number ; + passivemotion_called[winIdx] = 1 ; + 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 = glutGetWindow () ; - entry_called = 1 ; + 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 () ; } static void Close(void) { - int window = glutGetWindow () ; - close_called = 1 ; + int window = getWindowAndIdx(NULL); printf ( "%6d Window %d Close Callback\n", ++sequence_number, window ) ; } @@ -339,28 +441,42 @@ Close(void) static void OverlayDisplay(void) { - int window = glutGetWindow () ; - overlaydisplay_called = 1 ; + int window = getWindowAndIdx(NULL); printf ( "%6d Window %d OverlayDisplay Callback\n", ++sequence_number, window ) ; 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 ; + glutPostRedisplay () ; +} + +static void WindowStatus(int state) { - int window = glutGetWindow () ; - windowstatus_called = 1 ; + 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 () ; } static void SpaceMotion(int x, int y, int z) { - int window = glutGetWindow () ; - spacemotion_called = 1 ; + int window = getWindowAndIdx(NULL); printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n", ++sequence_number, window, x, y, z ) ; glutPostRedisplay () ; @@ -369,8 +485,7 @@ SpaceMotion(int x, int y, int z) static void SpaceRotation(int x, int y, int z) { - int window = glutGetWindow () ; - spacerotation_called = 1 ; + int window = getWindowAndIdx(NULL); printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n", ++sequence_number, window, x, y, z ) ; glutPostRedisplay () ; @@ -379,8 +494,7 @@ SpaceRotation(int x, int y, int z) static void SpaceButton(int button, int updown) { - int window = glutGetWindow () ; - spacebutton_called = 1 ; + int window = getWindowAndIdx(NULL); printf ( "%6d Window %d SpaceButton Callback: %d %d\n", ++sequence_number, window, button, updown ) ; glutPostRedisplay () ; @@ -389,8 +503,7 @@ SpaceButton(int button, int updown) static void Dials(int x, int y) { - int window = glutGetWindow () ; - dials_called = 1 ; + int window = getWindowAndIdx(NULL); printf ( "%6d Window %d Dials Callback: %d %d\n", ++sequence_number, window, x, y ) ; glutPostRedisplay () ; @@ -399,8 +512,7 @@ Dials(int x, int y) static void ButtonBox(int button, int updown) { - int window = glutGetWindow () ; - buttonbox_called = 1 ; + int window = getWindowAndIdx(NULL); printf ( "%6d Window %d ButtonBox Callback: %d %d\n", ++sequence_number, window, button, updown ) ; glutPostRedisplay () ; @@ -409,8 +521,7 @@ ButtonBox(int button, int updown) static void TabletMotion(int x, int y) { - int window = glutGetWindow () ; - tabletmotion_called = 1 ; + int window = getWindowAndIdx(NULL); printf ( "%6d Window %d TabletMotion Callback: %d %d\n", ++sequence_number, window, x, y ) ; glutPostRedisplay () ; @@ -419,40 +530,52 @@ TabletMotion(int x, int y) static void TabletButton(int button, int updown, int x, int y) { - int window = glutGetWindow () ; - tabletbutton_called = 1 ; + int window = getWindowAndIdx(NULL); printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n", ++sequence_number, window, button, updown, x, y ) ; glutPostRedisplay () ; } static void -MenuCallback ( int menuID ) +MenuCallback ( int value ) { - int window = glutGetWindow () ; - printf( "%6d Window %d MenuCallback - menuID is %d\n", - ++sequence_number, window, menuID ); + int menu = glutGetMenu(); + int window = getWindowAndIdx(NULL); + 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 = glutGetWindow () ; + 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 ) { - int window = glutGetWindow () ; - menudestroy_called = 1 ; - printf ( "%6d Window %d MenuStatus Callback: %d %d %d\n", - ++sequence_number, window, status, x, y ) ; + /* Menu and window for which this event is triggered are current when the callback is called */ + int menu = glutGetMenu(); + int window = getWindowAndIdx(NULL); + 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 () ; } @@ -461,11 +584,42 @@ 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[]) { -#define STRING_LENGTH 10 - int freeglut_window, aux_window ; char dummy_string[STRING_LENGTH]; int menuID, subMenuA, subMenuB; @@ -476,95 +630,101 @@ 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. */ - freeglut_window = glutCreateWindow( "Callback Demo" ); - printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ; + + /* 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 */ 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 */ 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 ); - aux_window = glutCreateWindow( "Second Window" ); - printf ( "Creating window %d as 'Second Window'\n", aux_window ) ; + + /* 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 ); @@ -573,5 +733,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; }