normalizing line endings..
[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 \r
8 int nPosX,  nPosY;\r
9 int nWidth, nHeight;\r
10 \r
11 GLboolean bChildPosDone = GL_FALSE, bChildSizeDone = GL_FALSE;\r
12 \r
13 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY );\r
14 void Redisplay();\r
15 void Reshape(int width, int height);\r
16 void Position(int x, int y);\r
17 void WindowStatus(int state);\r
18 \r
19 \r
20 \r
21 \r
22 void DrawQuad()\r
23 {\r
24     nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
25     nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
26 \r
27     glBegin(GL_QUADS);\r
28         glVertex2d(nWidth*.25, nHeight*.75);\r
29         glVertex2d(nWidth*.75, nHeight*.75);\r
30         glVertex2d(nWidth*.75, nHeight*.25);\r
31         glVertex2d(nWidth*.25, nHeight*.25);\r
32     glEnd();\r
33 }\r
34 \r
35 void UnhideTimer(int window)\r
36 {\r
37     glutSetWindow(window);\r
38     glutShowWindow();\r
39 }\r
40 \r
41 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )\r
42 {\r
43     switch (cChar)\r
44     {\r
45     case 27:\r
46         glutLeaveMainLoop();\r
47 \r
48         break;\r
49 \r
50 \r
51     case 'f':\r
52     case 'F':\r
53         printf("main window toggle fullscreen\n");\r
54         glutFullScreenToggle();\r
55 \r
56         break;\r
57 \r
58 \r
59     case 'r':\r
60     case 'R':\r
61         if (nChildWindow!=-1 && cChar=='r') /* Capital R always resizes the main window*/\r
62         {\r
63             glutSetWindow(nChildWindow);\r
64             printf("child window resize\n");\r
65             if (!bChildSizeDone)\r
66                 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);\r
67             else\r
68                 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);\r
69             bChildSizeDone = !bChildSizeDone;\r
70         }\r
71         else\r
72         {\r
73             glutSetWindow(nWindow);\r
74             printf("main window resize\n");\r
75             if (nWidth<400)\r
76                 glutReshapeWindow(600,300);\r
77             else\r
78                 glutReshapeWindow(300,300);\r
79         }\r
80 \r
81         break;\r
82 \r
83 \r
84     case 'm':\r
85     case 'M':\r
86         if (nChildWindow!=-1 && cChar=='m') /* Capital M always moves the main window*/\r
87         {\r
88             glutSetWindow(nChildWindow);\r
89             /* The window position you request is relative to the top-left\r
90              * corner of the client area of the parent window.\r
91              */\r
92             if (!bChildPosDone)\r
93                 glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);\r
94             else\r
95                 glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);\r
96             bChildPosDone = !bChildPosDone;\r
97         }\r
98         else\r
99         {\r
100             glutSetWindow(nWindow);\r
101             printf("main window position\n");\r
102             /* The window position you request is the outer top-left of the window,\r
103              * the client area is at a different position if the window has borders\r
104              * and/or a title bar.\r
105              */\r
106             if (nPosX<400)\r
107                 glutPositionWindow(600,300);\r
108             else\r
109                 glutPositionWindow(300,300);\r
110         }\r
111 \r
112         break;\r
113 \r
114 \r
115     case 'c':\r
116     case 'C':\r
117         if (nChildWindow==-1)\r
118         {\r
119             /* open child window */\r
120             printf("open child window\n");\r
121             nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
122             nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
123 \r
124             nChildWindow = glutCreateSubWindow(nWindow,(int)(nWidth*.35),(int)(nHeight*.35),(int)(nWidth*.3),(int)(nHeight*.3));\r
125             glutKeyboardFunc( SampleKeyboard );\r
126             glutDisplayFunc( Redisplay );\r
127             glutReshapeFunc( Reshape );\r
128             glutPositionFunc( Position );\r
129             glutWindowStatusFunc( WindowStatus );\r
130         }\r
131         else\r
132         {\r
133             /* close child window */\r
134             printf("close child window\n");\r
135             glutSetWindow(nWindow);\r
136             glutDestroyWindow(nChildWindow);\r
137             nChildWindow = -1;\r
138             bChildSizeDone = GL_FALSE;\r
139             bChildPosDone  = GL_FALSE;\r
140         }\r
141         break;\r
142 \r
143 \r
144     case 'i':\r
145     case 'I':\r
146         glutIconifyWindow();\r
147         break;\r
148 \r
149 \r
150     case 'h':\r
151     case 'H':\r
152         if (nChildWindow!=-1 && cChar=='h') /* Capital H always hides the main window*/\r
153         {\r
154             glutSetWindow(nChildWindow);\r
155             glutTimerFunc(2000, UnhideTimer, nChildWindow);\r
156         }\r
157         else\r
158         {\r
159             glutSetWindow(nWindow);\r
160             glutTimerFunc(2000, UnhideTimer, nWindow);\r
161         }\r
162         glutHideWindow();\r
163 \r
164     default:\r
165         break;\r
166     }\r
167 }\r
168 \r
169 void Idle(void)\r
170 {\r
171     glutPostRedisplay();\r
172 }\r
173 \r
174 void Reshape(int width, int height)\r
175 {\r
176     int win = glutGetWindow();\r
177 \r
178     printf("reshape %s, %dx%d\n",win==nWindow?"main":"child",\r
179         width, height);\r
180 \r
181     glViewport(0,0,width,height);\r
182     glMatrixMode(GL_PROJECTION);\r
183     glLoadIdentity();\r
184     gluOrtho2D(0,width,0,height);\r
185 \r
186     if (win==nWindow && nChildWindow!=-1)\r
187     {\r
188         glutSetWindow(nChildWindow);\r
189         glutPositionWindow((int)(width*.35),(int)(height*.35));\r
190         glutReshapeWindow((int)(width*.3),(int)(height*.3));\r
191         glutSetWindow(nWindow);\r
192     }\r
193 }\r
194 \r
195 void Position(int x, int y)\r
196 {\r
197     int win = glutGetWindow();\r
198 \r
199     printf("position, %s: (%d,%d)\n",win==nWindow?"top-left of main":"top-left of child relative to parent",\r
200         x, y);\r
201 }\r
202 \r
203 void WindowStatus(int state)\r
204 {\r
205     int win = glutGetWindow();\r
206     printf("windowstatus (win %i): %i\n",win,state);\r
207 }\r
208 \r
209 void Redisplay(void)\r
210 {\r
211     int win = glutGetWindow();\r
212 \r
213     if (win==nWindow)\r
214     {\r
215         glClearColor(.2f,0.f,0.f,0.f);\r
216         glColor3f(1,1,1);\r
217     }\r
218     else\r
219     {\r
220         /* child window */\r
221         glClearColor(.0f,.2f,0.f,0.f);\r
222         glColor3f(.5,.5,.5);\r
223         glutPostWindowRedisplay(nWindow);\r
224     }\r
225     glClear(GL_COLOR_BUFFER_BIT);\r
226     DrawQuad();\r
227 \r
228     glutSwapBuffers();\r
229     glutPostWindowRedisplay(win);\r
230 }\r
231 \r
232 void Timer(int unused)\r
233 {\r
234     int win = glutGetWindow();\r
235     int border, caption;\r
236 \r
237     nPosX   = glutGet(GLUT_WINDOW_X);\r
238     nPosY   = glutGet(GLUT_WINDOW_Y);\r
239     nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
240     nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
241     border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
242     caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
243     /* returned position is top-left of client area, to get top-left of\r
244      * of window you'll need to add the size of the border and caption\r
245      * of the current window (can be 0).\r
246      * Note that the window position is not necessarily positive (e.g.\r
247      * when the window is on a monitor to the left of the primary monitor\r
248      * or simply when maximized--try pressing the maximize button).\r
249      * the returned size is the size of the client area\r
250      * Note that the top-left of a child window is relative to the\r
251      * top-left of the client area of the parent.\r
252      */\r
253     /* printf("window border: %dpx, caption: %dpx\n",border,caption); */\r
254     if (win==nWindow)\r
255         printf("main  window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
256             nWidth, nHeight,\r
257             nPosX ,nPosY,\r
258             nPosX-border,\r
259             nPosY-border-caption);\r
260     else\r
261         printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",\r
262         nWidth, nHeight,\r
263         nPosX ,nPosY);\r
264 \r
265     /* (re)set the timer callback and ask glut to call it in 500 ms */\r
266     glutTimerFunc(500, Timer, 0);\r
267 }\r
268 \r
269 \r
270 int main(int argc, char* argv[])\r
271 {\r
272     int border, caption;\r
273     glutInit( &argc, argv );\r
274     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE /*| GLUT_BORDERLESS*/); // do try as well with GLUT_BORDERLESS and GLUT_CAPTIONLESS\r
275     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);\r
276     \r
277     /* Get border and caption size of default window style */\r
278     border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
279     caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
280     printf("default window style border: %dpx, caption: %dpx\n",border,caption);\r
281 \r
282     /* NB: The window position you request is the outer top-left of the\r
283      * window, the client area is at a different position if the window has\r
284      * borders and/or a title bar.\r
285      */\r
286     glutInitWindowPosition(150,250);\r
287     glutInitWindowSize(200,200);\r
288 \r
289     nWindow = glutCreateWindow("test");\r
290     printf("main window id: %d\n", nWindow);\r
291 \r
292     glutKeyboardFunc( SampleKeyboard );\r
293     glutDisplayFunc( Redisplay );\r
294     glutReshapeFunc( Reshape );\r
295     glutPositionFunc( Position );\r
296     glutWindowStatusFunc( WindowStatus );\r
297 \r
298     glutTimerFunc(300, Timer, 0);\r
299 \r
300     glutMainLoop();\r
301     printf("glutMainLoop returned\n");\r
302 \r
303     return EXIT_SUCCESS;\r
304 }