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