Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
8.0 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: ctxmenu.cxx
  3. *
  4. * Copyright (c) 1997 Microsoft Corporation
  5. *
  6. \**************************************************************************/
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <math.h>
  10. #include <sys/types.h>
  11. #include <stdlib.h>
  12. #include "uidemo.hxx"
  13. #include "util.hxx"
  14. #include "context.hxx"
  15. static MTKWIN *mtkMenuWin;
  16. static TEXTURE *pMenuTex;
  17. static ISIZE winSize; // main window cached size and position
  18. static IPOINT2D winPos;
  19. static GLIRECT glRect;
  20. static float bgColor[4] = {0.0f, 0.3f, 0.9f, 0.0f};
  21. static BOOL bLighting, bDepth;
  22. static VIEW view;
  23. // Forwards
  24. static void CleanUp();
  25. static MTKWIN *CreateMenuWindow();
  26. /**************************************************************************\
  27. *
  28. \**************************************************************************/
  29. static void Clear()
  30. {
  31. #if 1
  32. int clearMask = GL_COLOR_BUFFER_BIT;
  33. #else
  34. int clearMask = 0;
  35. #endif
  36. if( bDepth )
  37. clearMask |= GL_DEPTH_BUFFER_BIT;
  38. glClear( clearMask );
  39. }
  40. static void DrawMenu()
  41. {
  42. static FSIZE fSize = { 1.0f, 2.0f };
  43. // Draw next iteration of menu
  44. Clear();
  45. //mf: while testin'
  46. AddSwapHintRect( &glRect );
  47. DrawRect( &fSize );
  48. mtkMenuWin->Flush();
  49. }
  50. static void
  51. CalcWindowSquareRect( float radius, GLIRECT *pRect )
  52. {
  53. float ctr[3] = {0.0f, 0.0f, 0.0f};
  54. POINT3D blObj, trObj, blWin, trWin;
  55. float fCurSize = radius;
  56. blObj.x = ctr[0] - fCurSize;
  57. blObj.y = ctr[1] - fCurSize;
  58. blObj.z = ctr[2];
  59. TransformObjectToWindow( &blObj, &blWin, 1 );
  60. trObj.x = ctr[0] + fCurSize;
  61. trObj.y = ctr[1] + fCurSize;
  62. trObj.z = ctr[2];
  63. TransformObjectToWindow( &trObj, &trWin, 1 );
  64. //mf: this bloats the rect for the line case...
  65. CalcRect( &blWin, &trWin, pRect );
  66. // mf: so I'll reduce it :
  67. pRect->x ++;
  68. pRect->y ++;
  69. pRect->width -= 2;
  70. pRect->height -= 2;
  71. //mf: When compare pixels drawn with what clear draws, the squares aren't
  72. // exact - clear 0 or 1 pixels bigger around each edge. Or maybe swap rect
  73. // problem, who knows...
  74. }
  75. static void
  76. SetView( ISIZE *pWinSize )
  77. {
  78. glViewport(0, 0, pWinSize->width, pWinSize->height);
  79. view.fViewDist = 10.0f; // viewing distance
  80. view.fovy = 45.0f; // field of view in y direction (degrees)
  81. view.fAspect = (float) pWinSize->width / (float) pWinSize->height;
  82. // We'll assume width >= height
  83. glMatrixMode(GL_PROJECTION);
  84. glLoadIdentity();
  85. gluPerspective(view.fovy, view.fAspect, 0.1, 100.0);
  86. // glOrtho( -5.0, 5.0, -5.0, 5.0, 0.0, 100.0 ); no look as good
  87. glMatrixMode(GL_MODELVIEW);
  88. glLoadIdentity();
  89. gluLookAt(0,0,view.fViewDist, 0,0,0, 0,1,0); // from, to, up...
  90. }
  91. static void Reshape(int width, int height)
  92. {
  93. // Need to update main window info
  94. ISIZE *pSize = &winSize;
  95. pSize->width = width;
  96. pSize->height = height;
  97. if( !width || !height )
  98. return;
  99. SetView( pSize );
  100. //mf: test
  101. UpdateLocalTransforms( UPDATE_ALL );
  102. CalcWindowSquareRect( QUAD_SIZE, &glRect );
  103. GLIRECT *pRect = &glRect;
  104. glScissor( pRect->x, pRect->y, pRect->width, pRect->height );
  105. Clear();
  106. }
  107. static BOOL EscKey(int key, GLenum mask)
  108. {
  109. if( key == TK_ESCAPE ) {
  110. mtkMenuWin->Return();
  111. }
  112. return FALSE;
  113. }
  114. /******************** MAIN LOGON SEQUENCE *********************************\
  115. *
  116. \**************************************************************************/
  117. static MTKWIN *CreateMenuWindow()
  118. {
  119. MTKWIN *win;
  120. // Set window size and position
  121. UINT winConfig = 0;
  122. #if 0
  123. winConfig |= MTK_NOBORDER | MTK_TRANSPARENT;
  124. #else
  125. //mf: for testin'
  126. // winConfig |= MTK_TRANSPARENT;
  127. //mf: when specify just TRANSPARENT here, resizing screws everything up - draws
  128. // all over the place, even over borders,etc...
  129. #endif
  130. win = new MTKWIN();
  131. if( !win )
  132. return NULL;
  133. // Create the window
  134. // if( ! win->Create( "Demo", &winSize, &winPos, winConfig, menuWndProc ) ) {
  135. if( ! win->Create( "Demo", &winSize, &winPos, winConfig, NULL) ) {
  136. delete win;
  137. return NULL;
  138. }
  139. // Configure the window for OpenGL, setting ReshapeFunc to catch the
  140. // resize (can't set it before in this case, since we do various gl
  141. // calculations in the Reshape func.
  142. UINT glConfig = MTK_RGB | MTK_DOUBLE | MTK_DEPTH16;
  143. win->SetReshapeFunc(Reshape);
  144. if( ! win->Config( glConfig ) )
  145. {
  146. delete win;
  147. return NULL;
  148. }
  149. return win;
  150. }
  151. static void InitGL(void)
  152. {
  153. static float lmodel_ambient[] = {0.2f, 0.2f, 0.2f, 0.0f};
  154. static float lmodel_twoside[] = {(float)GL_TRUE};
  155. static float lmodel_local[] = {(float)GL_FALSE};
  156. static float light0_ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
  157. static float light0_diffuse[] = {1.0f, 1.0f, 1.0f, 0.0f};
  158. #if 1
  159. // static float light0_position[] = {-1.0f, 1.0f, 1.0f, 0.0f};
  160. static float light0_position[] = {-1.0f, 0.8f, 4.0f, 0.0f};
  161. #else
  162. static float light0_position[] = {-1.0f, 1.0f, 1.0f, 0.0f};
  163. #endif
  164. static float light0_specular[] = {1.0f, 1.0f, 1.0f, 0.0f};
  165. static float bevel_mat_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
  166. static float bevel_mat_shininess[] = {40.0f};
  167. static float bevel_mat_specular[] = {1.0f, 1.0f, 1.0f, 0.0f};
  168. static float bevel_mat_diffuse[] = {1.0f, 1.0f, 1.0f, 0.0f};
  169. // Set GL attributes
  170. // glEnable( GL_SCISSOR_TEST );
  171. // glClearDepth(1.0f);
  172. glClearColor( bgColor[0], bgColor[1], bgColor[2], bgColor[3] );
  173. glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  174. glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  175. glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
  176. glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  177. glEnable(GL_LIGHT0);
  178. glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local);
  179. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  180. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  181. glEnable(GL_LIGHTING);
  182. glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient);
  183. glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess);
  184. glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular);
  185. glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse);
  186. glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  187. glEnable(GL_COLOR_MATERIAL);
  188. glEnable( GL_TEXTURE_2D );
  189. // Setup viewing parameters
  190. SetView( &winSize );
  191. // Rendering attributes
  192. #if 1
  193. bDepth = TRUE;
  194. glDepthFunc( GL_LEQUAL );
  195. glEnable( GL_DEPTH_TEST );
  196. #endif
  197. bLighting = TRUE;
  198. glEnable( GL_LIGHTING );
  199. glShadeModel(GL_FLAT);
  200. }
  201. static TEXTURE *LoadTexture( LPTSTR bmpFile )
  202. {
  203. TEXTURE *pTex;
  204. pTex = new TEXTURE( (char *) bmpFile );
  205. return pTex;
  206. }
  207. /******************** RunLogonSequence ************************************\
  208. *
  209. \**************************************************************************/
  210. BOOL Draw3DContextMenu( IPOINT2D *pPos, ISIZE *pSize, LPTSTR bmpFile )
  211. {
  212. // Update local copies of window position and size
  213. winPos = *pPos;
  214. winSize = *pSize;
  215. // Create context menu window
  216. mtkMenuWin = CreateMenuWindow();
  217. if( !mtkMenuWin )
  218. return FALSE;
  219. // Do any GL init for the window
  220. InitGL();
  221. pMenuTex = LoadTexture( bmpFile );
  222. if( !pMenuTex ) {
  223. delete mtkMenuWin;
  224. return FALSE;
  225. }
  226. pMenuTex->MakeCurrent();
  227. //mf: temp
  228. UpdateLocalTransforms( UPDATE_ALL );
  229. CalcWindowSquareRect( QUAD_SIZE, &glRect );
  230. glEnable( GL_SCISSOR_TEST );
  231. GLIRECT *pRect = &glRect;
  232. glScissor( pRect->x, pRect->y, pRect->width, pRect->height );
  233. // Set mtk callbacks
  234. mtkMenuWin->SetKeyDownFunc( EscKey );
  235. mtkMenuWin->SetDisplayFunc( DrawMenu );
  236. mtkMenuWin->SetAnimateFunc( DrawMenu );
  237. // Start the message loop
  238. mtkMenuWin->Exec();
  239. CleanUp();
  240. return TRUE;
  241. }
  242. static void CleanUp()
  243. {
  244. #if 0
  245. //mf: this won't work yet
  246. if( mtkMenuWin )
  247. delete mtkMenuWin;
  248. #else
  249. mtkMenuWin->Close(); // this will call destructor for mtkWin
  250. #endif
  251. }
  252. //mf: this can be called during debug mode, to terminate prematurely
  253. static void Quit()
  254. {
  255. CleanUp();
  256. ExitProcess( 0 );
  257. }