Resizer.cpp 11 KB

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