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