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.

499 lines
14 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: dialog.c
  3. *
  4. * Dialog box functions for the OpenGL-based 3D Text screen saver.
  5. *
  6. * Created: 12-24-94 -by- Marc Fortier [marcfo]
  7. *
  8. * Copyright (c) 1994 Microsoft Corporation
  9. \**************************************************************************/
  10. #include <windows.h>
  11. #include <commdlg.h>
  12. #include <scrnsave.h>
  13. #include <GL\gl.h>
  14. #include <math.h>
  15. #include <memory.h>
  16. #include <string.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <malloc.h>
  21. #include <sys\timeb.h>
  22. #include <time.h>
  23. #include "sscommon.h"
  24. #include "sstext3d.h"
  25. // Global screen saver settings.
  26. static int idsStyles[NUM_DEMOS] =
  27. {IDS_DEMO_STRING, IDS_DEMO_CLOCK};
  28. static int idsRotStyles[NUM_ROTSTYLES] =
  29. { IDS_ROTSTYLE_NONE,
  30. IDS_ROTSTYLE_SEESAW,
  31. IDS_ROTSTYLE_WOBBLE,
  32. IDS_ROTSTYLE_RANDOM
  33. };
  34. // local funtions
  35. LONG WndProc(HWND, UINT, WPARAM, LPARAM);
  36. static void InitAttrContext( AttrContext *pac );
  37. static void getFont( HWND hDlg);
  38. static void updateDialogControls(HWND hDlg);
  39. /******************************Public*Routine******************************\
  40. * getIniSettings
  41. *
  42. * Get the screen saver configuration options from .INI file/registry.
  43. *
  44. * History:
  45. * Jan. 95 -by- Marc Fortier [marcfo]
  46. * - Wrote it.
  47. * Apr. 28, 95 : [marcfo]
  48. * - Call common function ss_GetDefaultBmp to get a default bmp file
  49. * Nov. 95 [marcfo]
  50. * - Use ss registry helper functions
  51. *
  52. \**************************************************************************/
  53. void
  54. getIniSettings()
  55. {
  56. int options;
  57. int optMask = 1;
  58. TCHAR szDefaultBitmap[MAX_PATH];
  59. int iPos;
  60. TCHAR szDefaultFont[256];
  61. TCHAR szDefCharSet[16];
  62. int iDefCharSet;
  63. // Initialize the global AttrContext
  64. InitAttrContext( &gac );
  65. // Load resources
  66. LoadString(hMainInstance, IDS_GENNAME, szScreenSaver,
  67. sizeof(szScreenSaver) / sizeof(TCHAR));
  68. if (LoadString(hMainInstance, IDS_DEFFONT, szDefaultFont,
  69. sizeof(szDefaultFont) / sizeof(TCHAR)) == 0)
  70. {
  71. #ifdef UNICODE
  72. wcscpy(szDefaultFont, TEXT("Tahoma"));
  73. #else
  74. strcpy(szDefaultFont, TEXT("Tahoma"));
  75. #endif
  76. }
  77. iDefCharSet = ANSI_CHARSET;
  78. if (LoadString(hMainInstance, IDS_DEFCHARSET, szDefCharSet,
  79. sizeof(szDefCharSet) / sizeof(TCHAR)) != 0)
  80. {
  81. #ifdef UNICODE
  82. iDefCharSet = _wtoi(szDefCharSet);
  83. #else
  84. iDefCharSet = atoi(szDefCharSet);
  85. #endif
  86. }
  87. // Get registry settings
  88. if( ss_RegistrySetup( hMainInstance, IDS_SAVERNAME, IDS_INIFILE ) )
  89. {
  90. // get demo type
  91. gac.demoType = ss_GetRegistryInt( IDS_DEMOTYPE, DEMO_STRING );
  92. SS_CLAMP_TO_RANGE2( gac.demoType, 0, MAX_DEMO);
  93. // get rotation style
  94. gac.rotStyle = ss_GetRegistryInt( IDS_ROTSTYLE, ROTSTYLE_RANDOM );
  95. SS_CLAMP_TO_RANGE2( gac.rotStyle, 0, NUM_ROTSTYLES-1 );
  96. // get tesselation
  97. iPos = ss_GetRegistryInt( IDS_TESSELATION, 10 );
  98. SS_CLAMP_TO_RANGE2( iPos, MIN_SLIDER, MAX_SLIDER );
  99. gac.fTesselFact = (float)iPos / 100.0f;
  100. // get size
  101. gac.uSize = ss_GetRegistryInt( IDS_SIZE, 50 );
  102. if ( gac.uSize > MAX_SLIDER )
  103. gac.uSize = MAX_SLIDER;
  104. // MIN_SLIDER is 0 so the unsigned uSize cannot ever be less than MIN_SLIDER
  105. // get speed
  106. gac.iSpeed = ss_GetRegistryInt( IDS_SPEED, 50 );
  107. SS_CLAMP_TO_RANGE2( gac.iSpeed, MIN_SLIDER, MAX_SLIDER );
  108. // get surface style
  109. gac.surfStyle = ss_GetRegistryInt( IDS_SURFSTYLE, SURFSTYLE_SOLID );
  110. SS_CLAMP_TO_RANGE2(gac.surfStyle, 0, SURFSTYLE_TEX);
  111. // get font, attributes, and charset
  112. ss_GetRegistryString( IDS_FONT, szDefaultFont,
  113. gac.szFontName, LF_FACESIZE );
  114. options = ss_GetRegistryInt( IDS_FONT_ATTRIBUTES, 0 );
  115. if( options >= 0 ) {
  116. optMask = 1;
  117. gac.bBold = ((options & optMask) != 0);
  118. optMask <<=1;
  119. gac.bItalic = ((options & optMask) != 0);
  120. }
  121. gac.charSet = (BYTE)ss_GetRegistryInt( IDS_CHARSET, iDefCharSet );
  122. // get display string
  123. ss_GetRegistryString( IDS_TEXT, TEXT("OpenGL"), gac.szText, TEXT_LIMIT+1);
  124. // Determine the default .bmp file
  125. ss_GetDefaultBmpFile( szDefaultBitmap );
  126. // Is there a texture specified in the registry that overrides the
  127. // default?
  128. ss_GetRegistryString( IDS_TEXTURE, szDefaultBitmap, gac.texFile.szPathName,
  129. MAX_PATH);
  130. gac.texFile.nOffset = ss_GetRegistryInt( IDS_TEXTURE_FILE_OFFSET, 0 );
  131. }
  132. }
  133. /**************************************************************************\
  134. * ConfigInit
  135. *
  136. \**************************************************************************/
  137. BOOL
  138. ss_ConfigInit( HWND hDlg )
  139. {
  140. return TRUE;
  141. }
  142. /**************************************************************************\
  143. * InitAttrContext
  144. *
  145. * Initialize some of the values in the attribute context
  146. *
  147. * History:
  148. * Jan. 95 -by- Marc Fortier [marcfo]
  149. * Wrote it.
  150. \**************************************************************************/
  151. static void
  152. InitAttrContext( AttrContext *pac )
  153. {
  154. // set some default values
  155. pac->demoType = DEMO_STRING;
  156. pac->surfStyle = SURFSTYLE_SOLID;
  157. pac->fTesselFact = 1.0f;
  158. pac->uSize = 50;
  159. pac->iSpeed = 50;
  160. pac->texFile.szPathName[0] = '\0';
  161. pac->texFile.nOffset = 0;
  162. }
  163. /******************************Public*Routine******************************\
  164. * saveIniSettings
  165. *
  166. * Save the screen saver configuration option to the .INI file/registry.
  167. *
  168. * History:
  169. * Jan. 95 -by- Marc Fortier [marcfo]
  170. * Wrote it.
  171. * Nov. 95 [marcfo]
  172. * - Use ss registry helper functions
  173. \**************************************************************************/
  174. static void
  175. saveIniSettings(HWND hDlg)
  176. {
  177. int options;
  178. int optMask = 1;
  179. GetWindowText( GetDlgItem(hDlg, DLG_TEXT_ENTER), gac.szText, TEXT_LIMIT+1);
  180. if( ss_RegistrySetup( hMainInstance, IDS_SAVERNAME, IDS_INIFILE ) )
  181. {
  182. ss_WriteRegistryInt( IDS_DEMOTYPE, gac.demoType );
  183. ss_WriteRegistryInt( IDS_ROTSTYLE, gac.rotStyle );
  184. ss_WriteRegistryInt( IDS_TESSELATION,
  185. ss_GetTrackbarPos(hDlg, DLG_SETUP_TESSEL) );
  186. ss_WriteRegistryInt( IDS_SIZE,
  187. ss_GetTrackbarPos(hDlg, DLG_SETUP_SIZE) );
  188. ss_WriteRegistryInt( IDS_SPEED,
  189. ss_GetTrackbarPos(hDlg, DLG_SETUP_SPEED) );
  190. ss_WriteRegistryInt( IDS_SURFSTYLE, gac.surfStyle );
  191. ss_WriteRegistryString( IDS_FONT, gac.szFontName );
  192. optMask = 1;
  193. options = gac.bBold ? optMask : 0;
  194. optMask <<= 1;
  195. options |= gac.bItalic ? optMask : 0;
  196. ss_WriteRegistryInt( IDS_FONT_ATTRIBUTES, options );
  197. ss_WriteRegistryInt( IDS_CHARSET, gac.charSet );
  198. ss_WriteRegistryString( IDS_TEXT, gac.szText );
  199. ss_WriteRegistryString( IDS_TEXTURE, gac.texFile.szPathName );
  200. ss_WriteRegistryInt( IDS_TEXTURE_FILE_OFFSET, gac.texFile.nOffset );
  201. }
  202. }
  203. /******************************Public*Routine******************************\
  204. * setupDialogControls
  205. *
  206. * Do initial setup of dialog controls.
  207. *
  208. * History:
  209. * Jan. 95 -by- Marc Fortier [marcfo]
  210. * Wrote it.
  211. \**************************************************************************/
  212. static void
  213. setupDialogControls(HWND hDlg)
  214. {
  215. int pos;
  216. InitCommonControls();
  217. // initialize sliders
  218. // tesselation slider
  219. pos = (int)(gac.fTesselFact * 100.0f);
  220. ss_SetupTrackbar( hDlg, DLG_SETUP_TESSEL, MIN_SLIDER, MAX_SLIDER, 1, 9,
  221. pos );
  222. // size slider
  223. ss_SetupTrackbar( hDlg, DLG_SETUP_SIZE, MIN_SLIDER, MAX_SLIDER, 1, 9,
  224. gac.uSize );
  225. // speed slider
  226. ss_SetupTrackbar( hDlg, DLG_SETUP_SPEED, MIN_SLIDER, MAX_SLIDER, 1, 9,
  227. gac.iSpeed);
  228. // set state of other controls
  229. updateDialogControls(hDlg);
  230. }
  231. /******************************Public*Routine******************************\
  232. * updateDialogControls
  233. *
  234. * Updates dialog controls according to current state
  235. *
  236. * History:
  237. * Jan. 95 -by- Marc Fortier [marcfo]
  238. * Wrote it.
  239. \**************************************************************************/
  240. static void
  241. updateDialogControls(HWND hDlg)
  242. {
  243. int pos;
  244. BOOL bTexSurf;
  245. BOOL bText;
  246. bTexSurf = (gac.surfStyle == SURFSTYLE_TEX );
  247. CheckDlgButton(hDlg, IDC_RADIO_SOLID, !bTexSurf );
  248. CheckDlgButton(hDlg, IDC_RADIO_TEX , bTexSurf );
  249. // set up demo-specific configure button
  250. bText = (gac.demoType == DEMO_STRING) ? TRUE : FALSE;
  251. EnableWindow(GetDlgItem(hDlg, DLG_TEXT_ENTER), bText );
  252. CheckDlgButton(hDlg, IDC_DEMO_STRING, bText );
  253. CheckDlgButton(hDlg, IDC_DEMO_CLOCK, !bText );
  254. // texture: only enable if surfStyle is texture
  255. EnableWindow(GetDlgItem(hDlg, DLG_SETUP_TEX), bTexSurf );
  256. }
  257. /******************************Public*Routine******************************\
  258. * getFont
  259. *
  260. * Calls ChooseFont dialog
  261. *
  262. * History:
  263. * Jan. 95 -by- Marc Fortier [marcfo]
  264. * Wrote it.
  265. \**************************************************************************/
  266. static void
  267. getFont( HWND hDlg)
  268. {
  269. CHOOSEFONT cf = {0};
  270. LOGFONT lf = {0};
  271. HFONT hfont, hfontOld;
  272. HDC hdc;
  273. hdc = GetDC( hDlg );
  274. // Create and select a font.
  275. cf.lStructSize = sizeof(CHOOSEFONT);
  276. cf.hwndOwner = hDlg;
  277. cf.lpLogFont = &lf;
  278. cf.hInstance = hMainInstance;
  279. cf.lpTemplateName = (LPTSTR) MAKEINTRESOURCE(IDD_FONT);
  280. cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT | CF_TTONLY |
  281. CF_ENABLETEMPLATE | CF_NOSIMULATIONS;
  282. // setup logfont with current settings
  283. lstrcpy(lf.lfFaceName, gac.szFontName);
  284. lf.lfWeight = (gac.bBold) ? FW_BOLD : FW_NORMAL;
  285. lf.lfItalic = (gac.bItalic) ? (BYTE) 1 : 0;
  286. lf.lfCharSet = gac.charSet;
  287. lf.lfHeight = -37; // value ???
  288. if( ChooseFont(&cf) ) {
  289. // retrieve settings into gac
  290. lstrcpy( gac.szFontName, lf.lfFaceName );
  291. gac.bBold = (lf.lfWeight == FW_NORMAL) ? FALSE : TRUE;
  292. gac.bItalic = (lf.lfItalic) ? TRUE : FALSE;
  293. gac.charSet = lf.lfCharSet;
  294. }
  295. }
  296. BOOL WINAPI RegisterDialogClasses(HANDLE hinst)
  297. {
  298. return TRUE;
  299. }
  300. /******************************Public*Routine******************************\
  301. * ScreenSaverConfigureDialog
  302. *
  303. * Processes messages for the configuration dialog box.
  304. *
  305. * History:
  306. * Jan. 95 -by- Marc Fortier [marcfo]
  307. * - Wrote it.
  308. * Apr. 28, 95 : [marcfo]
  309. * - Call common function ss_GetTextureBitmap to load bmp texture
  310. *
  311. \**************************************************************************/
  312. BOOL ScreenSaverConfigureDialog(HWND hDlg, UINT message,
  313. WPARAM wParam, LPARAM lParam)
  314. {
  315. int wTmp;
  316. TCHAR szStr[GEN_STRING_SIZE];
  317. HANDLE hInst;
  318. BOOL bEnable;
  319. HWND hText;
  320. switch (message) {
  321. case WM_INITDIALOG:
  322. getIniSettings();
  323. setupDialogControls(hDlg);
  324. // setup rotStyle combo box
  325. for (wTmp = 0; wTmp < NUM_ROTSTYLES; wTmp++) {
  326. LoadString(hMainInstance, idsRotStyles[wTmp], szStr,
  327. GEN_STRING_SIZE);
  328. SendDlgItemMessage(hDlg, DLG_SETUP_ROTSTYLE, CB_ADDSTRING, 0,
  329. (LPARAM) szStr);
  330. }
  331. SendDlgItemMessage(hDlg, DLG_SETUP_ROTSTYLE, CB_SETCURSEL,
  332. gac.rotStyle, 0);
  333. // display current string in box
  334. SendDlgItemMessage( hDlg, DLG_TEXT_ENTER, EM_LIMITTEXT, TEXT_LIMIT,0);
  335. SetWindowText( GetDlgItem(hDlg, DLG_TEXT_ENTER), gac.szText );
  336. return TRUE;
  337. case WM_COMMAND:
  338. switch (LOWORD(wParam)) {
  339. case DLG_SETUP_TYPES:
  340. switch (HIWORD(wParam))
  341. {
  342. case CBN_EDITCHANGE:
  343. case CBN_SELCHANGE:
  344. gac.demoType =
  345. (int)SendDlgItemMessage(hDlg, DLG_SETUP_TYPES,
  346. CB_GETCURSEL, 0, 0);
  347. updateDialogControls(hDlg);
  348. break;
  349. default:
  350. break;
  351. }
  352. return FALSE;
  353. case DLG_SETUP_ROTSTYLE:
  354. switch (HIWORD(wParam))
  355. {
  356. case CBN_EDITCHANGE:
  357. case CBN_SELCHANGE:
  358. gac.rotStyle =
  359. (int)SendDlgItemMessage(hDlg, DLG_SETUP_ROTSTYLE,
  360. CB_GETCURSEL, 0, 0);
  361. updateDialogControls(hDlg);
  362. break;
  363. default:
  364. break;
  365. }
  366. return FALSE;
  367. case DLG_SETUP_TEX:
  368. // Run choose texture dialog
  369. ss_SelectTextureFile( hDlg, &gac.texFile );
  370. break;
  371. case DLG_SETUP_FONT:
  372. getFont(hDlg);
  373. break;
  374. case IDC_RADIO_SOLID:
  375. case IDC_RADIO_TEX:
  376. gac.surfStyle = IDC_TO_SURFSTYLE( LOWORD(wParam) );
  377. updateDialogControls(hDlg);
  378. break;
  379. case IDC_DEMO_STRING:
  380. case IDC_DEMO_CLOCK:
  381. gac.demoType = IDC_TO_DEMOTYPE( LOWORD(wParam) );
  382. updateDialogControls(hDlg);
  383. if( LOWORD(wParam) == IDC_DEMO_STRING ) {
  384. // set selected text focus
  385. SetFocus( GetDlgItem(hDlg, DLG_TEXT_ENTER) );
  386. SendDlgItemMessage( hDlg, DLG_TEXT_ENTER,
  387. EM_SETSEL, 0, -1 );
  388. }
  389. break;
  390. case IDOK:
  391. saveIniSettings(hDlg);
  392. EndDialog(hDlg, TRUE);
  393. break;
  394. case IDCANCEL:
  395. EndDialog(hDlg, FALSE);
  396. break;
  397. default:
  398. break;
  399. }
  400. return TRUE;
  401. break;
  402. default:
  403. return 0;
  404. }
  405. return 0;
  406. }