163123f8d31cd3012cfdf3d04c69e5269626db8d
[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 \r
198     default:\r
199         break;\r
200     }\r
201 }\r
202 \r
203 void Idle(void)\r
204 {\r
205     glutPostRedisplay();\r
206 }\r
207 \r
208 void Reshape(int width, int height)\r
209 {\r
210     int win = glutGetWindow();\r
211 \r
212     printf("reshape %s, client area: %dx%d\n",win==nWindow?"main":"child",\r
213         width, height);\r
214 \r
215     glViewport(0,0,width,height);\r
216     glMatrixMode(GL_PROJECTION);\r
217     glLoadIdentity();\r
218     gluOrtho2D(0,width,0,height);\r
219 \r
220     if (win==nWindow && nChildWindow!=-1)\r
221     {\r
222         /* Put child window in right place */\r
223         int x = (int)(width*.35), y=(int)(height*.35), w=(int)(width*.3), h = (int)(height*.3);\r
224         if (bChildPosDone)\r
225         {\r
226             x += 50;\r
227             y += 50;\r
228         }\r
229         if (bChildSizeDone)\r
230         {\r
231             w += 50;\r
232             h += 50;\r
233         }\r
234         glutSetWindow(nChildWindow);\r
235         glutPositionWindow(x,y);\r
236         glutReshapeWindow(w,h);\r
237         glutSetWindow(nWindow);\r
238     }\r
239 }\r
240 \r
241 void Position(int x, int y)\r
242 {\r
243     int win = glutGetWindow();\r
244 \r
245     printf("position, %s: (%d,%d)\n",win==nWindow?"top-left (non-client) of main":"top-left of child relative to parent",\r
246         x, y);\r
247 }\r
248 \r
249 void WindowStatus(int state)\r
250 {\r
251     int win = glutGetWindow();\r
252     printf("windowstatus (win %i): %i\n",win,state);\r
253 }\r
254 \r
255 void Redisplay(void)\r
256 {\r
257     int win = glutGetWindow();\r
258 \r
259     if (win==nWindow)\r
260     {\r
261         glClearColor(.2f,0.f,0.f,0.f);\r
262         glColor3f(1,1,1);\r
263     }\r
264     else\r
265     {\r
266         /* child window */\r
267         glClearColor(.0f,.2f,0.f,0.f);\r
268         glColor3f(.5,.5,.5);\r
269         glutPostWindowRedisplay(nWindow);\r
270     }\r
271     glClear(GL_COLOR_BUFFER_BIT);\r
272     DrawQuad();\r
273 \r
274     glutSwapBuffers();\r
275     glutPostWindowRedisplay(win);\r
276 }\r
277 \r
278 void Timer(int unused)\r
279 {\r
280     int win = glutGetWindow();\r
281     int x, y;\r
282     int width, height;\r
283     int border, caption;\r
284 \r
285     x       = glutGet(GLUT_WINDOW_X);\r
286     y       = glutGet(GLUT_WINDOW_Y);\r
287     width   = glutGet(GLUT_WINDOW_WIDTH);\r
288     height  = glutGet(GLUT_WINDOW_HEIGHT);\r
289     border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
290     caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
291     /* returned position is top-left of client area, to get top-left of\r
292      * of window you'll need to add the size of the border and caption\r
293      * of the current window (can be 0).\r
294      * Note that the window position is not necessarily positive (e.g.\r
295      * when the window is on a monitor to the left of the primary monitor\r
296      * or simply when maximized--try pressing the maximize button).\r
297      * the returned size is the size of the client area\r
298      * Note that the top-left of a child window is relative to the\r
299      * top-left of the client area of the parent.\r
300      */\r
301     /* printf("window border: %dpx, caption: %dpx\n",border,caption); */\r
302     if (win==nWindow)\r
303         printf("main  window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
304             width, height,\r
305             x ,y,\r
306             x-border,\r
307             y-border-caption);\r
308     else\r
309         printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",\r
310         width, height,\r
311         x ,y);\r
312 \r
313     /* (re)set the timer callback and ask glut to call it in 500 ms */\r
314     glutTimerFunc(500, Timer, 0);\r
315 }\r
316 \r
317 \r
318 int main(int argc, char* argv[])\r
319 {\r
320     int border, caption;\r
321     glutInit( &argc, argv );\r
322     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE /*| GLUT_BORDERLESS*/); // do try as well with GLUT_BORDERLESS and GLUT_CAPTIONLESS\r
323     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);\r
324     \r
325     /* Get border and caption size of default window style */\r
326     border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
327     caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
328     printf("default window style border: %dpx, caption: %dpx\n",border,caption);\r
329 \r
330     /* NB: The window position you request is the outer top-left of the\r
331      * window, the client area is at a different position if the window has\r
332      * borders and/or a title bar.\r
333      */\r
334     glutInitWindowPosition(150,250);\r
335     glutInitWindowSize(200,200);\r
336 \r
337     nWindow = glutCreateWindow("test");\r
338     glutSetIconTitle("test icon title");\r
339     printf("main window id: %d\n", nWindow);\r
340 \r
341     glutKeyboardFunc( SampleKeyboard );\r
342     glutDisplayFunc( Redisplay );\r
343     glutReshapeFunc( Reshape );\r
344     glutPositionFunc( Position );\r
345     glutWindowStatusFunc( WindowStatus );\r
346 \r
347     glutTimerFunc(300, Timer, 0);\r
348 \r
349     glutMainLoop();\r
350     printf("glutMainLoop returned\n");\r
351 \r
352     return EXIT_SUCCESS;\r
353 }