\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 width, int height);\r
+void Position(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
\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 && cChar=='r') /* Capital R always resizes the main window*/\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
+ glutSetWindow(nWindow);\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 && cChar=='m') /* Capital M always moves the main window*/\r
+ {\r
+ glutSetWindow(nChildWindow);\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
+ {\r
+ glutSetWindow(nWindow);\r
+ printf("main window position\n");\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
+ glutPositionFunc( Position );\r
+ }\r
else\r
- glutPositionWindow(300,300);\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
glutPostRedisplay();\r
}\r
\r
-void Reshape(int x, int y)\r
+void Reshape(int width, int height)\r
{\r
- nWidth = glutGet(GLUT_WINDOW_WIDTH);\r
- nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
+ int win = glutGetWindow();\r
\r
- glViewport(0,0,nWidth,nHeight);\r
+ printf("reshape %s, %dx%d\n",win==nWindow?"main":"child",\r
+ width, height);\r
+\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)(width*.35),(int)(height*.35));\r
+ glutReshapeWindow((int)(width*.3),(int)(height*.3));\r
+ glutSetWindow(nWindow);\r
+ }\r
+}\r
+\r
+void Position(int x, int y)\r
+{\r
+ int win = glutGetWindow();\r
+\r
+ printf("position, %s: (%d,%d)\n",win==nWindow?"top-left of main":"top-left of child relative to parent",\r
+ x, y);\r
}\r
\r
void Redisplay(void)\r
{\r
- if (nLoopMain++%6==0)\r
+ int win = glutGetWindow();\r
+\r
+ if (win==nWindow)\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
- printf("window now %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
+ 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
-\r
- glClearColor(.2f,0.f,0.f,0.f);\r
glClear(GL_COLOR_BUFFER_BIT);\r
- glColor3f(1,1,1);\r
DrawQuad();\r
\r
glutSwapBuffers();\r
- glutPostRedisplay();\r
+ 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
+ /* 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
- /* 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
+ /* 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
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
\r
- return 1;\r
+ return EXIT_SUCCESS;\r
}
\ No newline at end of file