3 #include <GL/freeglut.h>
\r
5 int nWindow, nChildWindow = -1;
\r
7 GLboolean bChildPosDone = GL_FALSE, bChildSizeDone = GL_FALSE;
\r
9 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY );
\r
11 void Reshape(int width, int height);
\r
12 void Position(int x, int y);
\r
13 void WindowStatus(int state);
\r
20 int width = glutGet(GLUT_WINDOW_WIDTH);
\r
21 int height = glutGet(GLUT_WINDOW_HEIGHT);
\r
24 glVertex2d(width*.25, height*.75);
\r
25 glVertex2d(width*.75, height*.75);
\r
26 glVertex2d(width*.75, height*.25);
\r
27 glVertex2d(width*.25, height*.25);
\r
31 void UnhideTimer(int window)
\r
33 glutSetWindow(window);
\r
37 void ChangeTitleTimer(int unused)
\r
39 glutSetIconTitle("new icon title");
\r
40 glutSetWindowTitle("new test title");
\r
43 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
\r
48 glutLeaveMainLoop();
\r
55 printf("main window toggle fullscreen\n");
\r
56 glutFullScreenToggle();
\r
63 if (nChildWindow!=-1 && cChar=='r') /* Capital R always resizes the main window*/
\r
65 glutSetWindow(nChildWindow);
\r
66 printf("child window resize\n");
\r
67 if (!bChildSizeDone)
\r
68 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);
\r
70 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);
\r
71 bChildSizeDone = !bChildSizeDone;
\r
75 glutSetWindow(nWindow);
\r
76 printf("main window resize\n");
\r
77 if (glutGet(GLUT_WINDOW_WIDTH)<400)
\r
78 glutReshapeWindow(600,300);
\r
80 glutReshapeWindow(300,300);
\r
88 if (nChildWindow!=-1 && cChar=='m') /* Capital M always moves the main window*/
\r
90 glutSetWindow(nChildWindow);
\r
91 /* The window position you request is relative to the top-left
\r
92 * corner of the client area of the parent window.
\r
95 glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);
\r
97 glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);
\r
98 bChildPosDone = !bChildPosDone;
\r
102 glutSetWindow(nWindow);
\r
103 printf("main window position\n");
\r
104 /* The window position you request is the outer top-left of the window,
\r
105 * the client area is at a different position if the window has borders
\r
106 * and/or a title bar.
\r
108 if (glutGet(GLUT_WINDOW_X)<400)
\r
109 glutPositionWindow(600,300);
\r
111 glutPositionWindow(300,300);
\r
119 if (nChildWindow!=-1 && cChar=='d') /* Capital D always moves+resizes the main window*/
\r
121 glutSetWindow(nChildWindow);
\r
122 if (!bChildPosDone)
\r
123 glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);
\r
125 glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);
\r
126 bChildPosDone = !bChildPosDone;
\r
127 if (!bChildSizeDone)
\r
128 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);
\r
130 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);
\r
131 bChildSizeDone = !bChildSizeDone;
\r
135 if (glutGet(GLUT_WINDOW_X)<400)
\r
136 glutPositionWindow(600,300);
\r
138 glutPositionWindow(300,300);
\r
139 if (glutGet(GLUT_WINDOW_WIDTH)<400)
\r
140 glutReshapeWindow(600,300);
\r
142 glutReshapeWindow(300,300);
\r
149 if (nChildWindow==-1)
\r
151 int width = glutGet(GLUT_WINDOW_WIDTH);
\r
152 int height = glutGet(GLUT_WINDOW_HEIGHT);
\r
154 /* open child window */
\r
155 printf("open child window\n");
\r
157 nChildWindow = glutCreateSubWindow(nWindow,(int)(width*.35),(int)(height*.35),(int)(width*.3),(int)(height*.3));
\r
158 glutKeyboardFunc( SampleKeyboard );
\r
159 glutDisplayFunc( Redisplay );
\r
160 glutReshapeFunc( Reshape );
\r
161 glutPositionFunc( Position );
\r
162 glutWindowStatusFunc( WindowStatus );
\r
166 /* close child window */
\r
167 printf("close child window\n");
\r
168 glutSetWindow(nWindow);
\r
169 glutDestroyWindow(nChildWindow);
\r
171 bChildSizeDone = GL_FALSE;
\r
172 bChildPosDone = GL_FALSE;
\r
179 glutIconifyWindow();
\r
180 glutTimerFunc(1500, ChangeTitleTimer, 0);
\r
186 if (nChildWindow!=-1 && cChar=='h') /* Capital H always hides the main window*/
\r
188 glutSetWindow(nChildWindow);
\r
189 glutTimerFunc(2000, UnhideTimer, nChildWindow);
\r
193 glutSetWindow(nWindow);
\r
194 glutTimerFunc(2000, UnhideTimer, nWindow);
\r
201 if (nChildWindow!=-1 && cChar=='p') /* Capital P always changes pointer for the main window*/
\r
203 glutSetWindow(nChildWindow);
\r
204 if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_TOP_SIDE)
\r
206 glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
\r
207 printf("reverting child window cursor\n");
\r
211 glutSetCursor(GLUT_CURSOR_TOP_SIDE);
\r
212 printf("changing child window cursor\n");
\r
217 glutSetWindow(nWindow);
\r
218 if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_CYCLE)
\r
220 glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
\r
221 printf("reverting main window cursor\n");
\r
225 glutSetCursor(GLUT_CURSOR_CYCLE);
\r
226 printf("changing main window cursor\n");
\r
238 glutPostRedisplay();
\r
241 void Reshape(int width, int height)
\r
243 int win = glutGetWindow();
\r
245 printf("reshape %s, client area: %dx%d\n",win==nWindow?"main":"child",
\r
248 glViewport(0,0,width,height);
\r
249 glMatrixMode(GL_PROJECTION);
\r
251 gluOrtho2D(0,width,0,height);
\r
253 if (win==nWindow && nChildWindow!=-1)
\r
255 /* Put child window in right place */
\r
256 int x = (int)(width*.35), y=(int)(height*.35), w=(int)(width*.3), h = (int)(height*.3);
\r
262 if (bChildSizeDone)
\r
267 glutSetWindow(nChildWindow);
\r
268 glutPositionWindow(x,y);
\r
269 glutReshapeWindow(w,h);
\r
270 glutSetWindow(nWindow);
\r
274 void Position(int x, int y)
\r
276 int win = glutGetWindow();
\r
278 printf("position, %s: (%d,%d)\n",win==nWindow?"top-left (non-client) of main":"top-left of child relative to parent",
\r
282 void WindowStatus(int state)
\r
284 int win = glutGetWindow();
\r
285 printf("windowstatus (win %i): %i\n",win,state);
\r
288 void Redisplay(void)
\r
290 int win = glutGetWindow();
\r
295 glClearColor(.2f,0.f,0.f,0.f);
\r
301 glClearColor(.0f,.2f,0.f,0.f);
\r
302 glColor3f(.5,.5,.5);
\r
303 glutPostWindowRedisplay(nWindow);
\r
305 glClear(GL_COLOR_BUFFER_BIT);
\r
310 glColor3f(1, 1, 0);
\r
311 glGetIntegerv(GL_VIEWPORT, viewport);
\r
312 glRasterPos2i(2, -glutBitmapHeight(GLUT_BITMAP_9_BY_15)+3+viewport[3]);
\r
313 glutBitmapString(GLUT_BITMAP_9_BY_15, (unsigned char*)"press f/r/m/d/c/i/h/p");
\r
317 glutPostWindowRedisplay(win);
\r
320 void Timer(int unused)
\r
322 int win = glutGetWindow();
\r
325 int border, caption;
\r
327 x = glutGet(GLUT_WINDOW_X);
\r
328 y = glutGet(GLUT_WINDOW_Y);
\r
329 width = glutGet(GLUT_WINDOW_WIDTH);
\r
330 height = glutGet(GLUT_WINDOW_HEIGHT);
\r
331 border = glutGet(GLUT_WINDOW_BORDER_WIDTH);
\r
332 caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);
\r
333 /* returned position is top-left of client area, to get top-left of
\r
334 * of window you'll need to add the size of the border and caption
\r
335 * of the current window (can be 0).
\r
336 * Note that the window position is not necessarily positive (e.g.
\r
337 * when the window is on a monitor to the left of the primary monitor
\r
338 * or simply when maximized--try pressing the maximize button).
\r
339 * the returned size is the size of the client area
\r
340 * Note that the top-left of a child window is relative to the
\r
341 * top-left of the client area of the parent.
\r
343 /* printf("window border: %dpx, caption: %dpx\n",border,caption); */
\r
345 printf("main window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",
\r
351 printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",
\r
355 /* (re)set the timer callback and ask glut to call it in 500 ms */
\r
356 glutTimerFunc(500, Timer, 0);
\r
360 int main(int argc, char* argv[])
\r
362 int border, caption;
\r
363 glutInit( &argc, argv );
\r
364 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE /*| GLUT_BORDERLESS*/); // do try as well with GLUT_BORDERLESS and GLUT_CAPTIONLESS
\r
365 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
\r
367 /* Get border and caption size of default window style */
\r
368 border = glutGet(GLUT_WINDOW_BORDER_WIDTH);
\r
369 caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);
\r
370 printf("default window style border: %dpx, caption: %dpx\n",border,caption);
\r
372 /* NB: The window position you request is the outer top-left of the
\r
373 * window, the client area is at a different position if the window has
\r
374 * borders and/or a title bar.
\r
376 glutInitWindowPosition(150,250);
\r
377 glutInitWindowSize(200,200);
\r
379 nWindow = glutCreateWindow("test");
\r
380 glutSetIconTitle("test icon title");
\r
381 printf("main window id: %d\n", nWindow);
\r
383 glutKeyboardFunc( SampleKeyboard );
\r
384 glutDisplayFunc( Redisplay );
\r
385 glutReshapeFunc( Reshape );
\r
386 glutPositionFunc( Position );
\r
387 glutWindowStatusFunc( WindowStatus );
\r
389 glutTimerFunc(300, Timer, 0);
\r
392 printf("glutMainLoop returned\n");
\r
394 return EXIT_SUCCESS;
\r