glutGet(GLUT_WINDOW_X) and glutGet(GLUT_WINDOW_Y) on a child window now
authorDiederick Niehorster <dcnieho@gmail.com>
Mon, 23 Jul 2012 13:52:55 +0000 (13:52 +0000)
committerDiederick Niehorster <dcnieho@gmail.com>
Mon, 23 Jul 2012 13:52:55 +0000 (13:52 +0000)
return top-left of child w.r.t. parent's client area
resizer demo now also has child window button 'c'. doesn't work with
fullscreen for some reason....

git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@1367 7f0cb862-5218-0410-a997-914c9d46530a

progs/demos/Resizer/Resizer.cpp
src/mswin/fg_state_mswin.c

index 8252d7d..76d55f7 100644 (file)
@@ -2,21 +2,26 @@
 \r
 #include <GL/freeglut.h>\r
 \r
-int nWindow;\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
-\r
+void Reshape(int x, int y);\r
 \r
 \r
 \r
 \r
 void DrawQuad()\r
 {\r
+    nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
+    nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
+\r
     glBegin(GL_QUADS);\r
         glVertex2d(nWidth*.25, nHeight*.75);\r
         glVertex2d(nWidth*.75, nHeight*.75);\r
@@ -27,36 +32,107 @@ void DrawQuad()
 \r
 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )\r
 {\r
-    if (cChar == 27)\r
+    switch (cChar)\r
+    {\r
+    case 27:\r
         glutLeaveMainLoop();\r
 \r
-    else if (cChar=='f')\r
-    {\r
-        printf("main window toggle fullscreen\n");\r
+        break;\r
+\r
 \r
+    case 'f':\r
+    case 'F':\r
+        printf("main window toggle fullscreen\n");\r
         glutFullScreenToggle();\r
-    }\r
-    else if (cChar=='r')\r
-    {\r
-        printf("main window resize\n");\r
 \r
-        if (nWidth<400)\r
-            glutReshapeWindow(600,300);\r
+        break;\r
+\r
+\r
+    case 'r':\r
+    case 'R':\r
+        if (nChildWindow!=-1)\r
+        {\r
+            glutSetWindow(nChildWindow);\r
+            printf("child window resize\n");\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
-            glutReshapeWindow(300,300);\r
-    }\r
-    else if (cChar=='m')\r
-    {\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
-            glutPositionWindow(600,300);\r
+        {\r
+            printf("main window resize\n");\r
+            if (nWidth<400)\r
+                glutReshapeWindow(600,300);\r
+            else\r
+                glutReshapeWindow(300,300);\r
+        }\r
+\r
+        break;\r
+\r
+\r
+    case 'm':\r
+    case 'M':\r
+        if (nChildWindow!=-1)\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
+            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
+        }\r
         else\r
-            glutPositionWindow(300,300);\r
+        {\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
+                glutPositionWindow(600,300);\r
+            else\r
+                glutPositionWindow(300,300);\r
+        }\r
+\r
+        break;\r
+\r
+\r
+    case 'c':\r
+    case 'C':\r
+        if (nChildWindow==-1)\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
+            glutKeyboardFunc( SampleKeyboard );\r
+            glutDisplayFunc( Redisplay );\r
+            glutReshapeFunc( Reshape );\r
+        }\r
+        else\r
+        {\r
+            /* close child window */\r
+            printf("close child window\n");\r
+            glutSetWindow(nWindow);\r
+            glutDestroyWindow(nChildWindow);\r
+            nChildWindow = -1;\r
+            bChildSizeDone = GL_FALSE;\r
+            bChildPosDone  = GL_FALSE;\r
+        }\r
+        break;\r
+\r
+\r
+    default:\r
+        break;\r
     }\r
 }\r
 \r
@@ -67,18 +143,32 @@ void Idle(void)
 \r
 void Reshape(int x, int y)\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
 \r
     glViewport(0,0,nWidth,nHeight);\r
     glMatrixMode(GL_PROJECTION);\r
     glLoadIdentity();\r
     gluOrtho2D(0,nWidth,0,nHeight);\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
+        glutSetWindow(nWindow);\r
+    }\r
 }\r
 \r
 void Redisplay(void)\r
 {\r
-    if (nLoopMain++%6==0)\r
+    int win = glutGetWindow();\r
+\r
+    if (nLoopMain++%20==0)\r
     {\r
         int border, caption;\r
 \r
@@ -96,20 +186,35 @@ void Redisplay(void)
          * or simply when maximized--try pressing the maximize button).\r
          * the returned size is the size of the client area\r
          */\r
-        printf("window now %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\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
-            nPosX-border,\r
-            nPosY-border-caption);\r
+            nPosX ,nPosY);\r
     }\r
 \r
-    glClearColor(.2f,0.f,0.f,0.f);\r
+    if (win==nWindow)\r
+    {\r
+        glClearColor(.2f,0.f,0.f,0.f);\r
+        glColor3f(1,1,1);\r
+    }\r
+    else\r
+    {\r
+        /* child window */\r
+        glClearColor(.0f,.2f,0.f,0.f);\r
+        glColor3f(.5,.5,.5);\r
+        glutPostWindowRedisplay(nWindow);\r
+    }\r
     glClear(GL_COLOR_BUFFER_BIT);\r
-    glColor3f(1,1,1);\r
     DrawQuad();\r
 \r
     glutSwapBuffers();\r
-    glutPostRedisplay();\r
+    glutPostWindowRedisplay(win);\r
 }\r
 \r
 \r
@@ -136,5 +241,5 @@ int main(int argc, char* argv[])
     glutMainLoop();\r
     printf("glutMainLoop returned\n");\r
 \r
-    return 1;\r
+    return EXIT_SUCCESS;\r
 }
\ No newline at end of file
index d5f4419..91256a3 100644 (file)
@@ -183,6 +183,19 @@ int fgPlatformGlutGet ( GLenum eWhat )
         GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
 #else
         winRect = fghGetClientArea(fgStructure.CurrentWindow, FALSE);
+        if (fgStructure.CurrentWindow->Parent && (eWhat==GLUT_WINDOW_X || eWhat==GLUT_WINDOW_Y))
+        {
+            /* For child window, we should return relative to upper-left
+             *  of parent's client area.
+             */
+            POINT topleft;
+            topleft.x = winRect.left;
+            topleft.y = winRect.top;
+
+            ScreenToClient(fgStructure.CurrentWindow->Parent->Window.Handle,&topleft);
+            winRect.left = topleft.x;
+            winRect.top  = topleft.y;
+        }
 #endif /* defined(_WIN32_WCE) */
 
         switch( eWhat )