resizer demo now uses timer to display window information
[freeglut] / progs / demos / Resizer / Resizer.cpp
index 76d55f7..c2ca76d 100644 (file)
@@ -50,7 +50,7 @@ void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
 \r
     case 'r':\r
     case 'R':\r
-        if (nChildWindow!=-1)\r
+        if (nChildWindow!=-1 && cChar=='r') /* Capital R always resizes the main window*/\r
         {\r
             glutSetWindow(nChildWindow);\r
             printf("child window resize\n");\r
@@ -62,6 +62,7 @@ void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
         }\r
         else\r
         {\r
+            glutSetWindow(nWindow);\r
             printf("main window resize\n");\r
             if (nWidth<400)\r
                 glutReshapeWindow(600,300);\r
@@ -74,10 +75,9 @@ void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
 \r
     case 'm':\r
     case 'M':\r
-        if (nChildWindow!=-1)\r
+        if (nChildWindow!=-1 && cChar=='m') /* Capital M always moves the main window*/\r
         {\r
             glutSetWindow(nChildWindow);\r
-\r
             /* The window position you request is relative to the top-left\r
              * corner of the client area of the parent window.\r
              */\r
@@ -89,8 +89,8 @@ void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
         }\r
         else\r
         {\r
+            glutSetWindow(nWindow);\r
             printf("main window position\n");\r
-\r
             /* The window position you request is the outer top-left of the window,\r
              * the client area is at a different position if the window has borders\r
              * and/or a title bar.\r
@@ -141,62 +141,38 @@ void Idle(void)
     glutPostRedisplay();\r
 }\r
 \r
-void Reshape(int x, int y)\r
+void Reshape(int width, int height)\r
 {\r
     int win = glutGetWindow();\r
 \r
-    nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
-    nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
     printf("reshape %s, %dx%d\n",win==nWindow?"main":"child",\r
-        nWidth, nHeight);\r
+        width, height);\r
 \r
-    glViewport(0,0,nWidth,nHeight);\r
+    glViewport(0,0,width,height);\r
     glMatrixMode(GL_PROJECTION);\r
     glLoadIdentity();\r
-    gluOrtho2D(0,nWidth,0,nHeight);\r
+    gluOrtho2D(0,width,0,height);\r
 \r
     if (win==nWindow && nChildWindow!=-1)\r
     {\r
         glutSetWindow(nChildWindow);\r
-        glutPositionWindow((int)(nWidth*.35),(int)(nHeight*.35));\r
-        glutReshapeWindow((int)(nWidth*.3),(int)(nHeight*.3));\r
+        glutPositionWindow((int)(width*.35),(int)(height*.35));\r
+        glutReshapeWindow((int)(width*.3),(int)(height*.3));\r
         glutSetWindow(nWindow);\r
     }\r
 }\r
 \r
-void Redisplay(void)\r
+void Position(int x, int y)\r
 {\r
     int win = glutGetWindow();\r
 \r
-    if (nLoopMain++%20==0)\r
-    {\r
-        int border, caption;\r
-\r
-        nPosX   = glutGet(GLUT_WINDOW_X);\r
-        nPosY   = glutGet(GLUT_WINDOW_Y);\r
-        nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
-        nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
-        border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
-        caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
-        /* returned position is top-left of client area, to get top-left of\r
-         * of window you'll need to add the size of the border and caption\r
-         * of the current window (can be 0).\r
-         * Note that the window position is not necessarily positive (e.g.\r
-         * when the window is on a monitor to the left of the primary monitor\r
-         * or simply when maximized--try pressing the maximize button).\r
-         * the returned size is the size of the client area\r
-         */\r
-        if (win==nWindow)\r
-            printf("main  window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
-                nWidth, nHeight,\r
-                nPosX ,nPosY,\r
-                nPosX-border,\r
-                nPosY-border-caption);\r
-        else\r
-            printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",\r
-            nWidth, nHeight,\r
-            nPosX ,nPosY);\r
-    }\r
+    printf("position %s, %dx%d\n",win==nWindow?"main":"child",\r
+        x, y);\r
+}\r
+\r
+void Redisplay(void)\r
+{\r
+    int win = glutGetWindow();\r
 \r
     if (win==nWindow)\r
     {\r
@@ -217,16 +193,59 @@ void Redisplay(void)
     glutPostWindowRedisplay(win);\r
 }\r
 \r
+void Timer(int unused)
+{
+    int win = glutGetWindow();
+    int border, caption;\r
+\r
+    nPosX   = glutGet(GLUT_WINDOW_X);\r
+    nPosY   = glutGet(GLUT_WINDOW_Y);\r
+    nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
+    nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
+    border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
+    caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
+    /* returned position is top-left of client area, to get top-left of\r
+     * of window you'll need to add the size of the border and caption\r
+     * of the current window (can be 0).\r
+     * Note that the window position is not necessarily positive (e.g.\r
+     * when the window is on a monitor to the left of the primary monitor\r
+     * or simply when maximized--try pressing the maximize button).\r
+     * the returned size is the size of the client area\r
+     * Note that the top-left of a child window is relative to the\r
+     * top-left of the client area of the parent.\r
+     */\r
+    /* printf("window border: %dpx, caption: %dpx\n",border,caption); */\r
+    if (win==nWindow)\r
+        printf("main  window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
+            nWidth, nHeight,\r
+            nPosX ,nPosY,\r
+            nPosX-border,\r
+            nPosY-border-caption);\r
+    else\r
+        printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",\r
+        nWidth, nHeight,\r
+        nPosX ,nPosY);
+
+    /* (re)set the timer callback and ask glut to call it in 1 second */
+    glutTimerFunc(300, Timer, 0);
+}\r
+\r
 \r
 int main(int argc, char* argv[])\r
 {\r
+    int border, caption;\r
     glutInit( &argc, argv );\r
     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE /*| GLUT_BORDERLESS*/); // do try as well with GLUT_BORDERLESS and GLUT_CAPTIONLESS\r
     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);\r
-\r
-    /* The window position you request is the outer top-left of the window,\r
-     * the client area is at a different position if the window has borders\r
-     * and/or a title bar.\r
+    \r
+    /* Get border and caption size of default window style */\r
+    border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
+    caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
+    printf("default window style border: %dpx, caption: %dpx\n",border,caption);\r
+\r
+    /* NB: The window position you request is the outer top-left of the\r
+     * window, the client area is at a different position if the window has\r
+     * borders and/or a title bar.\r
      */\r
     glutInitWindowPosition(150,250);\r
     glutInitWindowSize(200,200);\r
@@ -237,6 +256,9 @@ int main(int argc, char* argv[])
     glutKeyboardFunc( SampleKeyboard );\r
     glutDisplayFunc( Redisplay );\r
     glutReshapeFunc( Reshape );\r
+    glutPositionFunc( Position );\r
+\r
+    glutTimerFunc(300, Timer, 0);\r
 \r
     glutMainLoop();\r
     printf("glutMainLoop returned\n");\r