resizer demo: adding a bit more output to console to indicate what is going on upon...
[freeglut] / progs / demos / Resizer / Resizer.cpp
1 #include <stdio.h>\r
2 \r
3 #include <GL/freeglut.h>\r
4 \r
5 int nWindow, nChildWindow = -1;\r
6 int nLoopMain = 0;\r
7 GLboolean bChildPosDone = GL_FALSE, bChildSizeDone = GL_FALSE;\r
8 \r
9 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY );\r
10 void Redisplay();\r
11 void Reshape(int width, int height);\r
12 void Position(int x, int y);\r
13 void WindowStatus(int state);\r
14 \r
15 \r
16 \r
17 \r
18 void DrawQuad()\r
19 {\r
20     int width  = glutGet(GLUT_WINDOW_WIDTH);\r
21     int height = glutGet(GLUT_WINDOW_HEIGHT);\r
22 \r
23     glBegin(GL_QUADS);\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
28     glEnd();\r
29 }\r
30 \r
31 void UnhideTimer(int window)\r
32 {\r
33     glutSetWindow(window);\r
34     glutShowWindow();\r
35 }\r
36 \r
37 void ChangeTitleTimer(int unused)\r
38 {\r
39     glutSetIconTitle("new icon title");\r
40     glutSetWindowTitle("new test title");\r
41 }\r
42 \r
43 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )\r
44 {\r
45     switch (cChar)\r
46     {\r
47     case 27:\r
48         glutLeaveMainLoop();\r
49 \r
50         break;\r
51 \r
52 \r
53     case 'f':\r
54     case 'F':\r
55         printf("main window toggle fullscreen\n");\r
56         glutFullScreenToggle();\r
57 \r
58         break;\r
59 \r
60 \r
61     case 'r':\r
62     case 'R':\r
63         if (nChildWindow!=-1 && cChar=='r') /* Capital R always resizes the main window*/\r
64         {\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
69             else\r
70                 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);\r
71             bChildSizeDone = !bChildSizeDone;\r
72         }\r
73         else\r
74         {\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
79             else\r
80                 glutReshapeWindow(300,300);\r
81         }\r
82 \r
83         break;\r
84 \r
85 \r
86     case 'm':\r
87     case 'M':\r
88         if (nChildWindow!=-1 && cChar=='m') /* Capital M always moves the main window*/\r
89         {\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
93              */\r
94             if (!bChildPosDone)\r
95                 glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);\r
96             else\r
97                 glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);\r
98             bChildPosDone = !bChildPosDone;\r
99         }\r
100         else\r
101         {\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
107              */\r
108             if (glutGet(GLUT_WINDOW_X)<400)\r
109                 glutPositionWindow(600,300);\r
110             else\r
111                 glutPositionWindow(300,300);\r
112         }\r
113 \r
114         break;\r
115 \r
116 \r
117     case 'd':\r
118     case 'D':\r
119         if (nChildWindow!=-1 && cChar=='d') /* Capital D always moves+resizes the main window*/\r
120         {\r
121             glutSetWindow(nChildWindow);\r
122             if (!bChildPosDone)\r
123                 glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);\r
124             else\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
129             else\r
130                 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);\r
131             bChildSizeDone = !bChildSizeDone;\r
132         }\r
133         else\r
134         {\r
135             if (glutGet(GLUT_WINDOW_X)<400)\r
136                 glutPositionWindow(600,300);\r
137             else\r
138                 glutPositionWindow(300,300);\r
139             if (glutGet(GLUT_WINDOW_WIDTH)<400)\r
140                 glutReshapeWindow(600,300);\r
141             else\r
142                 glutReshapeWindow(300,300);\r
143         }\r
144         break;\r
145 \r
146 \r
147     case 'c':\r
148     case 'C':\r
149         if (nChildWindow==-1)\r
150         {\r
151             int width  = glutGet(GLUT_WINDOW_WIDTH);\r
152             int height = glutGet(GLUT_WINDOW_HEIGHT);\r
153 \r
154             /* open child window */\r
155             printf("open child window\n");\r
156 \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
163         }\r
164         else\r
165         {\r
166             /* close child window */\r
167             printf("close child window\n");\r
168             glutSetWindow(nWindow);\r
169             glutDestroyWindow(nChildWindow);\r
170             nChildWindow = -1;\r
171             bChildSizeDone = GL_FALSE;\r
172             bChildPosDone  = GL_FALSE;\r
173         }\r
174         break;\r
175 \r
176 \r
177     case 'i':\r
178     case 'I':\r
179         glutIconifyWindow();\r
180         glutTimerFunc(1500, ChangeTitleTimer, 0);\r
181         break;\r
182 \r
183 \r
184     case 'h':\r
185     case 'H':\r
186         if (nChildWindow!=-1 && cChar=='h') /* Capital H always hides the main window*/\r
187         {\r
188             glutSetWindow(nChildWindow);\r
189             glutTimerFunc(2000, UnhideTimer, nChildWindow);\r
190         }\r
191         else\r
192         {\r
193             glutSetWindow(nWindow);\r
194             glutTimerFunc(2000, UnhideTimer, nWindow);\r
195         }\r
196         glutHideWindow();\r
197         break;\r
198 \r
199     case 'p':\r
200     case 'P':\r
201         if (nChildWindow!=-1 && cChar=='p') /* Capital P always changes pointer for the main window*/\r
202         {\r
203             glutSetWindow(nChildWindow);\r
204                         if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_TOP_SIDE)\r
205                         {\r
206                                 glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);\r
207                                 printf("reverting child window cursor\n");\r
208                         }\r
209             else\r
210                         {\r
211                                 glutSetCursor(GLUT_CURSOR_TOP_SIDE);\r
212                                 printf("changing child window cursor\n");\r
213                         }\r
214         }\r
215         else\r
216         {\r
217             glutSetWindow(nWindow);\r
218             if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_CYCLE)\r
219                         {\r
220                                 glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);\r
221                                 printf("reverting main window cursor\n");\r
222                         }\r
223             else\r
224                         {\r
225                                 glutSetCursor(GLUT_CURSOR_CYCLE);\r
226                                 printf("changing main window cursor\n");\r
227                         }\r
228         }\r
229         break;\r
230 \r
231     default:\r
232         break;\r
233     }\r
234 }\r
235 \r
236 void Idle(void)\r
237 {\r
238     glutPostRedisplay();\r
239 }\r
240 \r
241 void Reshape(int width, int height)\r
242 {\r
243     int win = glutGetWindow();\r
244 \r
245     printf("reshape %s, client area: %dx%d\n",win==nWindow?"main":"child",\r
246         width, height);\r
247 \r
248     glViewport(0,0,width,height);\r
249     glMatrixMode(GL_PROJECTION);\r
250     glLoadIdentity();\r
251     gluOrtho2D(0,width,0,height);\r
252 \r
253     if (win==nWindow && nChildWindow!=-1)\r
254     {\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
257         if (bChildPosDone)\r
258         {\r
259             x += 50;\r
260             y += 50;\r
261         }\r
262         if (bChildSizeDone)\r
263         {\r
264             w += 50;\r
265             h += 50;\r
266         }\r
267         glutSetWindow(nChildWindow);\r
268         glutPositionWindow(x,y);\r
269         glutReshapeWindow(w,h);\r
270         glutSetWindow(nWindow);\r
271     }\r
272 }\r
273 \r
274 void Position(int x, int y)\r
275 {\r
276     int win = glutGetWindow();\r
277 \r
278     printf("position, %s: (%d,%d)\n",win==nWindow?"top-left (non-client) of main":"top-left of child relative to parent",\r
279         x, y);\r
280 }\r
281 \r
282 void WindowStatus(int state)\r
283 {\r
284     int win = glutGetWindow();\r
285     printf("windowstatus (win %i): %i\n",win,state);\r
286 }\r
287 \r
288 void Redisplay(void)\r
289 {\r
290     int win = glutGetWindow();\r
291     int viewport[4];\r
292 \r
293     if (win==nWindow)\r
294     {\r
295         glClearColor(.2f,0.f,0.f,0.f);\r
296         glColor3f(1,1,1);\r
297     }\r
298     else\r
299     {\r
300         /* child window */\r
301         glClearColor(.0f,.2f,0.f,0.f);\r
302         glColor3f(.5,.5,.5);\r
303         glutPostWindowRedisplay(nWindow);\r
304     }\r
305     glClear(GL_COLOR_BUFFER_BIT);\r
306     DrawQuad();\r
307 \r
308     if (win==nWindow)\r
309     {\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
314     }\r
315 \r
316     glutSwapBuffers();\r
317     glutPostWindowRedisplay(win);\r
318 }\r
319 \r
320 void Timer(int unused)\r
321 {\r
322     int win = glutGetWindow();\r
323     int x, y;\r
324     int width, height;\r
325     int border, caption;\r
326 \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
342      */\r
343     /* printf("window border: %dpx, caption: %dpx\n",border,caption); */\r
344     if (win==nWindow)\r
345         printf("main  window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
346             width, height,\r
347             x ,y,\r
348             x-border,\r
349             y-caption);\r
350     else\r
351         printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",\r
352         width, height,\r
353         x ,y);\r
354 \r
355     /* (re)set the timer callback and ask glut to call it in 500 ms */\r
356     glutTimerFunc(500, Timer, 0);\r
357 }\r
358 \r
359 \r
360 int main(int argc, char* argv[])\r
361 {\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
366     \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
371 \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
375      */\r
376     glutInitWindowPosition(150,250);\r
377     glutInitWindowSize(200,200);\r
378 \r
379     nWindow = glutCreateWindow("test");\r
380     glutSetIconTitle("test icon title");\r
381     printf("main window id: %d\n", nWindow);\r
382 \r
383     glutKeyboardFunc( SampleKeyboard );\r
384     glutDisplayFunc( Redisplay );\r
385     glutReshapeFunc( Reshape );\r
386     glutPositionFunc( Position );\r
387     glutWindowStatusFunc( WindowStatus );\r
388 \r
389     glutTimerFunc(300, Timer, 0);\r
390 \r
391     glutMainLoop();\r
392     printf("glutMainLoop returned\n");\r
393 \r
394     return EXIT_SUCCESS;\r
395 }\r