comparison mupdf-source/thirdparty/freeglut/progs/demos/Resizer/Resizer.cpp @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 #include <stdio.h>
2
3 #include <GL/freeglut.h>
4
5 int nWindow, nChildWindow = -1;
6 int nLoopMain = 0;
7 GLboolean bChildPosDone = GL_FALSE, bChildSizeDone = GL_FALSE;
8
9 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY );
10 void Redisplay();
11 void Reshape(int width, int height);
12 void Position(int x, int y);
13 void WindowStatus(int state);
14
15
16
17
18 void DrawQuad()
19 {
20 int width = glutGet(GLUT_WINDOW_WIDTH);
21 int height = glutGet(GLUT_WINDOW_HEIGHT);
22
23 glBegin(GL_QUADS);
24 glVertex2d(width*.25, height*.75);
25 glVertex2d(width*.75, height*.75);
26 glVertex2d(width*.75, height*.25);
27 glVertex2d(width*.25, height*.25);
28 glEnd();
29 }
30
31 void UnhideTimer(int window)
32 {
33 glutSetWindow(window);
34 glutShowWindow();
35 }
36
37 void ChangeTitleTimer(int unused)
38 {
39 glutSetIconTitle("new icon title");
40 glutSetWindowTitle("new test title");
41 }
42
43 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
44 {
45 switch (cChar)
46 {
47 case 27:
48 glutLeaveMainLoop();
49
50 break;
51
52
53 case 'f':
54 case 'F':
55 printf("main window toggle fullscreen\n");
56 glutFullScreenToggle();
57
58 break;
59
60
61 case 'r':
62 case 'R':
63 if (nChildWindow!=-1 && cChar=='r') /* Capital R always resizes the main window*/
64 {
65 glutSetWindow(nChildWindow);
66 printf("child window resize\n");
67 if (!bChildSizeDone)
68 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);
69 else
70 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);
71 bChildSizeDone = !bChildSizeDone;
72 }
73 else
74 {
75 glutSetWindow(nWindow);
76 printf("main window resize\n");
77 if (glutGet(GLUT_WINDOW_WIDTH)<400)
78 glutReshapeWindow(600,300);
79 else
80 glutReshapeWindow(300,300);
81 }
82
83 break;
84
85
86 case 'm':
87 case 'M':
88 if (nChildWindow!=-1 && cChar=='m') /* Capital M always moves the main window*/
89 {
90 glutSetWindow(nChildWindow);
91 /* The window position you request is relative to the top-left
92 * corner of the client area of the parent window.
93 */
94 if (!bChildPosDone)
95 glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);
96 else
97 glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);
98 bChildPosDone = !bChildPosDone;
99 }
100 else
101 {
102 glutSetWindow(nWindow);
103 printf("main window position\n");
104 /* The window position you request is the outer top-left of the window,
105 * the client area is at a different position if the window has borders
106 * and/or a title bar.
107 */
108 if (glutGet(GLUT_WINDOW_X)<400)
109 glutPositionWindow(600,300);
110 else
111 glutPositionWindow(300,300);
112 }
113
114 break;
115
116
117 case 'd':
118 case 'D':
119 if (nChildWindow!=-1 && cChar=='d') /* Capital D always moves+resizes the main window*/
120 {
121 glutSetWindow(nChildWindow);
122 if (!bChildPosDone)
123 glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);
124 else
125 glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);
126 bChildPosDone = !bChildPosDone;
127 if (!bChildSizeDone)
128 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);
129 else
130 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);
131 bChildSizeDone = !bChildSizeDone;
132 }
133 else
134 {
135 if (glutGet(GLUT_WINDOW_X)<400)
136 glutPositionWindow(600,300);
137 else
138 glutPositionWindow(300,300);
139 if (glutGet(GLUT_WINDOW_WIDTH)<400)
140 glutReshapeWindow(600,300);
141 else
142 glutReshapeWindow(300,300);
143 }
144 break;
145
146
147 case 'c':
148 case 'C':
149 if (nChildWindow==-1)
150 {
151 int width = glutGet(GLUT_WINDOW_WIDTH);
152 int height = glutGet(GLUT_WINDOW_HEIGHT);
153
154 /* open child window */
155 printf("open child window\n");
156
157 nChildWindow = glutCreateSubWindow(nWindow,(int)(width*.35),(int)(height*.35),(int)(width*.3),(int)(height*.3));
158 glutKeyboardFunc( SampleKeyboard );
159 glutDisplayFunc( Redisplay );
160 glutReshapeFunc( Reshape );
161 glutPositionFunc( Position );
162 glutWindowStatusFunc( WindowStatus );
163 }
164 else
165 {
166 /* close child window */
167 printf("close child window\n");
168 glutSetWindow(nWindow);
169 glutDestroyWindow(nChildWindow);
170 nChildWindow = -1;
171 bChildSizeDone = GL_FALSE;
172 bChildPosDone = GL_FALSE;
173 }
174 break;
175
176
177 case 'i':
178 case 'I':
179 glutIconifyWindow();
180 glutTimerFunc(1500, ChangeTitleTimer, 0);
181 break;
182
183
184 case 'h':
185 case 'H':
186 if (nChildWindow!=-1 && cChar=='h') /* Capital H always hides the main window*/
187 {
188 glutSetWindow(nChildWindow);
189 glutTimerFunc(2000, UnhideTimer, nChildWindow);
190 }
191 else
192 {
193 glutSetWindow(nWindow);
194 glutTimerFunc(2000, UnhideTimer, nWindow);
195 }
196 glutHideWindow();
197 break;
198
199 case 'p':
200 case 'P':
201 if (nChildWindow!=-1 && cChar=='p') /* Capital P always changes pointer for the main window*/
202 {
203 glutSetWindow(nChildWindow);
204 if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_TOP_SIDE)
205 glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
206 else
207 glutSetCursor(GLUT_CURSOR_TOP_SIDE);
208 }
209 else
210 {
211 glutSetWindow(nWindow);
212 if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_CYCLE)
213 glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
214 else
215 glutSetCursor(GLUT_CURSOR_CYCLE);
216 }
217 break;
218
219 default:
220 break;
221 }
222 }
223
224 void Idle(void)
225 {
226 glutPostRedisplay();
227 }
228
229 void Reshape(int width, int height)
230 {
231 int win = glutGetWindow();
232
233 printf("reshape %s, client area: %dx%d\n",win==nWindow?"main":"child",
234 width, height);
235
236 glViewport(0,0,width,height);
237 glMatrixMode(GL_PROJECTION);
238 glLoadIdentity();
239 gluOrtho2D(0,width,0,height);
240
241 if (win==nWindow && nChildWindow!=-1)
242 {
243 /* Put child window in right place */
244 int x = (int)(width*.35), y=(int)(height*.35), w=(int)(width*.3), h = (int)(height*.3);
245 if (bChildPosDone)
246 {
247 x += 50;
248 y += 50;
249 }
250 if (bChildSizeDone)
251 {
252 w += 50;
253 h += 50;
254 }
255 glutSetWindow(nChildWindow);
256 glutPositionWindow(x,y);
257 glutReshapeWindow(w,h);
258 glutSetWindow(nWindow);
259 }
260 }
261
262 void Position(int x, int y)
263 {
264 int win = glutGetWindow();
265
266 printf("position, %s: (%d,%d)\n",win==nWindow?"top-left (non-client) of main":"top-left of child relative to parent",
267 x, y);
268 }
269
270 void WindowStatus(int state)
271 {
272 int win = glutGetWindow();
273 printf("windowstatus (win %i): %i\n",win,state);
274 }
275
276 void Redisplay(void)
277 {
278 int win = glutGetWindow();
279 int viewport[4];
280
281 if (win==nWindow)
282 {
283 glClearColor(.2f,0.f,0.f,0.f);
284 glColor3f(1,1,1);
285 }
286 else
287 {
288 /* child window */
289 glClearColor(.0f,.2f,0.f,0.f);
290 glColor3f(.5,.5,.5);
291 glutPostWindowRedisplay(nWindow);
292 }
293 glClear(GL_COLOR_BUFFER_BIT);
294 DrawQuad();
295
296 if (win==nWindow)
297 {
298 glColor3f(1, 1, 0);
299 glGetIntegerv(GL_VIEWPORT, viewport);
300 glRasterPos2i(2, -glutBitmapHeight(GLUT_BITMAP_9_BY_15)+3+viewport[3]);
301 glutBitmapString(GLUT_BITMAP_9_BY_15, (unsigned char*)"press f/r/m/d/c/i/h/p");
302 }
303
304 glutSwapBuffers();
305 glutPostWindowRedisplay(win);
306 }
307
308 void Timer(int unused)
309 {
310 int win = glutGetWindow();
311 int x, y;
312 int width, height;
313 int border, caption;
314
315 x = glutGet(GLUT_WINDOW_X);
316 y = glutGet(GLUT_WINDOW_Y);
317 width = glutGet(GLUT_WINDOW_WIDTH);
318 height = glutGet(GLUT_WINDOW_HEIGHT);
319 border = glutGet(GLUT_WINDOW_BORDER_WIDTH);
320 caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);
321 /* returned position is top-left of client area, to get top-left of
322 * of window you'll need to add the size of the border and caption
323 * of the current window (can be 0).
324 * Note that the window position is not necessarily positive (e.g.
325 * when the window is on a monitor to the left of the primary monitor
326 * or simply when maximized--try pressing the maximize button).
327 * the returned size is the size of the client area
328 * Note that the top-left of a child window is relative to the
329 * top-left of the client area of the parent.
330 */
331 /* printf("window border: %dpx, caption: %dpx\n",border,caption); */
332 if (win==nWindow)
333 printf("main window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",
334 width, height,
335 x ,y,
336 x-border,
337 y-caption);
338 else
339 printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",
340 width, height,
341 x ,y);
342
343 /* (re)set the timer callback and ask glut to call it in 500 ms */
344 glutTimerFunc(500, Timer, 0);
345 }
346
347
348 int main(int argc, char* argv[])
349 {
350 int border, caption;
351 glutInit( &argc, argv );
352 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE /*| GLUT_BORDERLESS*/); // do try as well with GLUT_BORDERLESS and GLUT_CAPTIONLESS
353 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
354
355 /* Get border and caption size of default window style */
356 border = glutGet(GLUT_WINDOW_BORDER_WIDTH);
357 caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);
358 printf("default window style border: %dpx, caption: %dpx\n",border,caption);
359
360 /* NB: The window position you request is the outer top-left of the
361 * window, the client area is at a different position if the window has
362 * borders and/or a title bar.
363 */
364 glutInitWindowPosition(150,250);
365 glutInitWindowSize(200,200);
366
367 nWindow = glutCreateWindow("test");
368 glutSetIconTitle("test icon title");
369 printf("main window id: %d\n", nWindow);
370
371 glutKeyboardFunc( SampleKeyboard );
372 glutDisplayFunc( Redisplay );
373 glutReshapeFunc( Reshape );
374 glutPositionFunc( Position );
375 glutWindowStatusFunc( WindowStatus );
376
377 glutTimerFunc(300, Timer, 0);
378
379 glutMainLoop();
380 printf("glutMainLoop returned\n");
381
382 return EXIT_SUCCESS;
383 }