resizer demo: adding a bit more output to console to indicate what is going on upon...
[freeglut] / progs / demos / Resizer / Resizer.cpp
index bd19ffe..85ce4b8 100644 (file)
@@ -4,32 +4,42 @@
 \r
 int nWindow, nChildWindow = -1;\r
 int nLoopMain = 0;\r
-\r
-int nPosX,  nPosY;\r
-int nWidth, nHeight;\r
-\r
 GLboolean bChildPosDone = GL_FALSE, bChildSizeDone = GL_FALSE;\r
 \r
 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY );\r
 void Redisplay();\r
-void Reshape(int x, int y);\r
+void Reshape(int width, int height);\r
+void Position(int x, int y);\r
+void WindowStatus(int state);\r
 \r
 \r
 \r
 \r
 void DrawQuad()\r
 {\r
-    nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
-    nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
+    int width  = glutGet(GLUT_WINDOW_WIDTH);\r
+    int height = glutGet(GLUT_WINDOW_HEIGHT);\r
 \r
     glBegin(GL_QUADS);\r
-        glVertex2d(nWidth*.25, nHeight*.75);\r
-        glVertex2d(nWidth*.75, nHeight*.75);\r
-        glVertex2d(nWidth*.75, nHeight*.25);\r
-        glVertex2d(nWidth*.25, nHeight*.25);\r
+        glVertex2d(width*.25, height*.75);\r
+        glVertex2d(width*.75, height*.75);\r
+        glVertex2d(width*.75, height*.25);\r
+        glVertex2d(width*.25, height*.25);\r
     glEnd();\r
 }\r
 \r
+void UnhideTimer(int window)\r
+{\r
+    glutSetWindow(window);\r
+    glutShowWindow();\r
+}\r
+\r
+void ChangeTitleTimer(int unused)\r
+{\r
+    glutSetIconTitle("new icon title");\r
+    glutSetWindowTitle("new test title");\r
+}\r
+\r
 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )\r
 {\r
     switch (cChar)\r
@@ -50,7 +60,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,8 +72,9 @@ 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
+            if (glutGet(GLUT_WINDOW_WIDTH)<400)\r
                 glutReshapeWindow(600,300);\r
             else\r
                 glutReshapeWindow(300,300);\r
@@ -74,10 +85,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,13 +99,13 @@ 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
              */\r
-            if (nPosX<400)\r
+            if (glutGet(GLUT_WINDOW_X)<400)\r
                 glutPositionWindow(600,300);\r
             else\r
                 glutPositionWindow(300,300);\r
@@ -104,19 +114,52 @@ void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
         break;\r
 \r
 \r
+    case 'd':\r
+    case 'D':\r
+        if (nChildWindow!=-1 && cChar=='d') /* Capital D always moves+resizes the main window*/\r
+        {\r
+            glutSetWindow(nChildWindow);\r
+            if (!bChildPosDone)\r
+                glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);\r
+            else\r
+                glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);\r
+            bChildPosDone = !bChildPosDone;\r
+            if (!bChildSizeDone)\r
+                glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);\r
+            else\r
+                glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);\r
+            bChildSizeDone = !bChildSizeDone;\r
+        }\r
+        else\r
+        {\r
+            if (glutGet(GLUT_WINDOW_X)<400)\r
+                glutPositionWindow(600,300);\r
+            else\r
+                glutPositionWindow(300,300);\r
+            if (glutGet(GLUT_WINDOW_WIDTH)<400)\r
+                glutReshapeWindow(600,300);\r
+            else\r
+                glutReshapeWindow(300,300);\r
+        }\r
+        break;\r
+\r
+\r
     case 'c':\r
     case 'C':\r
         if (nChildWindow==-1)\r
         {\r
+            int width  = glutGet(GLUT_WINDOW_WIDTH);\r
+            int height = glutGet(GLUT_WINDOW_HEIGHT);\r
+\r
             /* open child window */\r
             printf("open child window\n");\r
-            nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
-            nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
 \r
-            nChildWindow = glutCreateSubWindow(nWindow,(int)(nWidth*.35),(int)(nHeight*.35),(int)(nWidth*.3),(int)(nHeight*.3));\r
+            nChildWindow = glutCreateSubWindow(nWindow,(int)(width*.35),(int)(height*.35),(int)(width*.3),(int)(height*.3));\r
             glutKeyboardFunc( SampleKeyboard );\r
             glutDisplayFunc( Redisplay );\r
             glutReshapeFunc( Reshape );\r
+            glutPositionFunc( Position );\r
+            glutWindowStatusFunc( WindowStatus );\r
         }\r
         else\r
         {\r
@@ -131,6 +174,60 @@ void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
         break;\r
 \r
 \r
+    case 'i':\r
+    case 'I':\r
+        glutIconifyWindow();\r
+        glutTimerFunc(1500, ChangeTitleTimer, 0);\r
+        break;\r
+\r
+\r
+    case 'h':\r
+    case 'H':\r
+        if (nChildWindow!=-1 && cChar=='h') /* Capital H always hides the main window*/\r
+        {\r
+            glutSetWindow(nChildWindow);\r
+            glutTimerFunc(2000, UnhideTimer, nChildWindow);\r
+        }\r
+        else\r
+        {\r
+            glutSetWindow(nWindow);\r
+            glutTimerFunc(2000, UnhideTimer, nWindow);\r
+        }\r
+        glutHideWindow();\r
+        break;\r
+\r
+    case 'p':\r
+    case 'P':\r
+        if (nChildWindow!=-1 && cChar=='p') /* Capital P always changes pointer for the main window*/\r
+        {\r
+            glutSetWindow(nChildWindow);\r
+                       if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_TOP_SIDE)\r
+                       {\r
+                               glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);\r
+                               printf("reverting child window cursor\n");\r
+                       }\r
+            else\r
+                       {\r
+                               glutSetCursor(GLUT_CURSOR_TOP_SIDE);\r
+                               printf("changing child window cursor\n");\r
+                       }\r
+        }\r
+        else\r
+        {\r
+            glutSetWindow(nWindow);\r
+            if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_CYCLE)\r
+                       {\r
+                               glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);\r
+                               printf("reverting main window cursor\n");\r
+                       }\r
+            else\r
+                       {\r
+                               glutSetCursor(GLUT_CURSOR_CYCLE);\r
+                               printf("changing main window cursor\n");\r
+                       }\r
+        }\r
+        break;\r
+\r
     default:\r
         break;\r
     }\r
@@ -141,62 +238,57 @@ 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
+    printf("reshape %s, client area: %dx%d\n",win==nWindow?"main":"child",\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
+        /* Put child window in right place */\r
+        int x = (int)(width*.35), y=(int)(height*.35), w=(int)(width*.3), h = (int)(height*.3);\r
+        if (bChildPosDone)\r
+        {\r
+            x += 50;\r
+            y += 50;\r
+        }\r
+        if (bChildSizeDone)\r
+        {\r
+            w += 50;\r
+            h += 50;\r
+        }\r
         glutSetWindow(nChildWindow);\r
-        glutPositionWindow((int)(nWidth*.35),(int)(nHeight*.35));\r
-        glutReshapeWindow((int)(nWidth*.3),(int)(nHeight*.3));\r
+        glutPositionWindow(x,y);\r
+        glutReshapeWindow(w,h);\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: (%d,%d)\n",win==nWindow?"top-left (non-client) of main":"top-left of child relative to parent",\r
+        x, y);\r
+}\r
+\r
+void WindowStatus(int state)\r
+{\r
+    int win = glutGetWindow();\r
+    printf("windowstatus (win %i): %i\n",win,state);\r
+}\r
+\r
+void Redisplay(void)\r
+{\r
+    int win = glutGetWindow();\r
+    int viewport[4];\r
 \r
     if (win==nWindow)\r
     {\r
@@ -213,10 +305,57 @@ void Redisplay(void)
     glClear(GL_COLOR_BUFFER_BIT);\r
     DrawQuad();\r
 \r
+    if (win==nWindow)\r
+    {\r
+        glColor3f(1, 1, 0);\r
+        glGetIntegerv(GL_VIEWPORT, viewport);\r
+        glRasterPos2i(2, -glutBitmapHeight(GLUT_BITMAP_9_BY_15)+3+viewport[3]);\r
+        glutBitmapString(GLUT_BITMAP_9_BY_15, (unsigned char*)"press f/r/m/d/c/i/h/p");\r
+    }\r
+\r
     glutSwapBuffers();\r
     glutPostWindowRedisplay(win);\r
 }\r
 \r
+void Timer(int unused)\r
+{\r
+    int win = glutGetWindow();\r
+    int x, y;\r
+    int width, height;\r
+    int border, caption;\r
+\r
+    x       = glutGet(GLUT_WINDOW_X);\r
+    y       = glutGet(GLUT_WINDOW_Y);\r
+    width   = glutGet(GLUT_WINDOW_WIDTH);\r
+    height  = 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
+            width, height,\r
+            x ,y,\r
+            x-border,\r
+            y-caption);\r
+    else\r
+        printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",\r
+        width, height,\r
+        x ,y);\r
+\r
+    /* (re)set the timer callback and ask glut to call it in 500 ms */\r
+    glutTimerFunc(500, Timer, 0);\r
+}\r
+\r
 \r
 int main(int argc, char* argv[])\r
 {\r
@@ -238,14 +377,19 @@ int main(int argc, char* argv[])
     glutInitWindowSize(200,200);\r
 \r
     nWindow = glutCreateWindow("test");\r
+    glutSetIconTitle("test icon title");\r
     printf("main window id: %d\n", nWindow);\r
 \r
     glutKeyboardFunc( SampleKeyboard );\r
     glutDisplayFunc( Redisplay );\r
     glutReshapeFunc( Reshape );\r
+    glutPositionFunc( Position );\r
+    glutWindowStatusFunc( WindowStatus );\r
+\r
+    glutTimerFunc(300, Timer, 0);\r
 \r
     glutMainLoop();\r
     printf("glutMainLoop returned\n");\r
 \r
     return EXIT_SUCCESS;\r
-}
\ No newline at end of file
+}\r