Leaked source code of windows server 2003
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.

462 lines
14 KiB

  1. // Not sure which of these includes are/will be needed - t-gregti
  2. #include <windows.h>
  3. //#include <port1632.h>
  4. #include <commdlg.h>
  5. // CRT includes
  6. #include <stdio.h>
  7. #include <stddef.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <tchar.h>
  11. #include <io.h>
  12. #include <time.h>
  13. #include <sys\types.h>
  14. #include <sys\stat.h>
  15. #include <ctype.h>
  16. // RL TOOLS SET includes
  17. #include "windefs.h"
  18. #include "restok.h"
  19. #include "wincomon.h"
  20. int LoadStrIntoAnsiBuf(
  21. HINSTANCE hinst,
  22. UINT idResource,
  23. LPSTR lpszBuffer,
  24. int cbBuffer )
  25. {
  26. int rc;
  27. #ifdef RLRES32
  28. WCHAR tszTmpBuf[256];
  29. rc = LoadString( hinst, idResource, tszTmpBuf, TCHARSIN( sizeof( tszTmpBuf)));
  30. _WCSTOMBS( lpszBuffer,
  31. tszTmpBuf,
  32. cbBuffer,
  33. lstrlen( tszTmpBuf ) + 1 );
  34. #else
  35. rc = LoadString( hinst, idResource, lpszBuffer, cbBuffer );
  36. #endif
  37. return( rc);
  38. }
  39. /**
  40. * Function: GetFileNameFromBrowse.
  41. * Uses the commdlg.dll GetOpenFileName function to prompt the user
  42. * file name.
  43. *
  44. * Arguments:
  45. * hDlg, Owner for browse dialog.
  46. * pszFileName, // buffer to insert file name
  47. * cbFilePath, // Max length of file path buffer.
  48. * szTitle, // Working directory
  49. * szFilter, // filters string.
  50. * szDefExt // Default extension to file name
  51. *
  52. * Returns:
  53. * TRUE, pszFileName contains file name.
  54. * FALSE, GetFileName aborted.
  55. *
  56. * History:
  57. * 9/91 Copied from NotePad sources. TerryRu.
  58. **/
  59. BOOL GetFileNameFromBrowse(HWND hDlg,
  60. PSTR pszFileName,
  61. UINT cbFilePath,
  62. PSTR szTitle,
  63. PSTR szFilter,
  64. PSTR szDefExt)
  65. {
  66. OPENFILENAMEA ofn; // Structure used to init dialog.
  67. CHAR szBrowserDir[128]; // Directory to start browsing from.
  68. szBrowserDir[0] = '\0'; // By default use CWD.
  69. // initalize ofn to NULLS
  70. memset( (void *)&ofn, 0, sizeof( OPENFILENAMEA ) );
  71. /* fill in non-variant fields of OPENFILENAMEA struct. */
  72. ofn.lStructSize = sizeof(OPENFILENAMEA);
  73. ofn.lpstrCustomFilter = szCustFilterSpec;
  74. ofn.nMaxCustFilter = MAXCUSTFILTER;
  75. ofn.nFilterIndex = 1;
  76. ofn.lpstrFile = pszFileName;
  77. ofn.nMaxFile = MAXFILENAME;
  78. ofn.lpstrFileTitle = szFileTitle;
  79. ofn.nMaxFileTitle = MAXFILENAME;
  80. ofn.lpTemplateName = NULL;
  81. ofn.lpfnHook = NULL;
  82. // Setup info for comm dialog.
  83. ofn.hwndOwner = hDlg;
  84. ofn.lpstrInitialDir = szBrowserDir;
  85. ofn.Flags = OFN_HIDEREADONLY;
  86. ofn.lpstrDefExt = szDefExt;
  87. ofn.lpstrFileTitle = szFileTitle;
  88. ofn.lpstrTitle = szTitle;
  89. ofn.lpstrFilter = szFilter;
  90. // Get a filename from the dialog.
  91. return GetOpenFileNameA(&ofn);
  92. }
  93. #define MAX_STATUS_FIELDS 5
  94. #define MAXBUFFERSIZE 80
  95. /****************************************************************************
  96. *Procedure: StatusWndProc
  97. *
  98. *Inputs:
  99. *
  100. *Returns:
  101. * depends on message
  102. *
  103. *History:
  104. * 7/92 - created - t-gregti
  105. *
  106. *Comments:
  107. * More general than strictly neccesary for the RL tools, but
  108. * it makes adding new fields to the status line really easy.
  109. * For WM_FMTSTATLINE the lParam should be a string with length/type pairs
  110. * much like a printf format, e.g. "10s5i10s20i".
  111. * For WM_UPDSTATLINE the wParam contains the field to change and the lParam
  112. * contains a pointer to a string or int to display.
  113. *
  114. *****************************************************************************/
  115. INT_PTR APIENTRY StatusWndProc( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
  116. {
  117. PAINTSTRUCT ps;
  118. HDC hDC;
  119. static HFONT hFontCourier;
  120. static UINT cFields = 0;
  121. static UINT aiSize[MAX_STATUS_FIELDS];
  122. static TCHAR aszStatusStrings[MAX_STATUS_FIELDS][MAXBUFFERSIZE];
  123. static BOOL abIntegers[MAX_STATUS_FIELDS];
  124. switch( wMsg )
  125. {
  126. case WM_CREATE:
  127. {
  128. LOGFONT lf;
  129. memset( (void *)&lf, 0, sizeof(LOGFONT) );
  130. // Intialize font info
  131. lf.lfWeight = 400; //Normal
  132. lf.lfCharSet = ANSI_CHARSET;
  133. lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  134. lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  135. lf.lfQuality = PROOF_QUALITY;
  136. lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
  137. lf.lfHeight = 14;
  138. lf.lfWidth = 0;
  139. lf.lfUnderline = 0;
  140. lstrcpy ( lf.lfFaceName, TEXT("Courier"));
  141. // Get a handle to the courier font
  142. hFontCourier = CreateFontIndirect( (void *)& lf );
  143. break;
  144. }
  145. case WM_DESTROY:
  146. DeleteObject((HGDIOBJ)hFontCourier);
  147. break;
  148. case WM_FMTSTATLINE:
  149. {
  150. TCHAR *psz;
  151. #ifdef RLRES32
  152. CHAR sz[MAXBUFFERSIZE];
  153. #endif
  154. cFields = 0;
  155. for (psz = (LPTSTR)lParam; *psz; psz++)
  156. {
  157. cFields++;
  158. #ifdef RLRES32
  159. _WCSTOMBS( sz,
  160. psz,
  161. ACHARSIN( sizeof( sz)),
  162. lstrlen( psz) + 1);
  163. aiSize[cFields-1] = atoi(sz);
  164. #else
  165. aiSize[cFields-1] = atoi(psz);
  166. #endif
  167. while(_istdigit(*psz))
  168. {
  169. psz++;
  170. }
  171. switch(*psz)
  172. {
  173. case 'i':
  174. abIntegers[cFields-1] = TRUE;
  175. break;
  176. case 's':
  177. abIntegers[cFields-1] = FALSE;
  178. break;
  179. default:
  180. cFields = 0;
  181. return(FALSE);
  182. }
  183. }
  184. return(TRUE);
  185. }
  186. case WM_UPDSTATLINE:
  187. // intialize status line info, and force it to be painted
  188. if (wParam > cFields)
  189. {
  190. return(FALSE);
  191. }
  192. if (abIntegers[wParam]) // Is it for an integer field?
  193. {
  194. #ifdef RLRES32
  195. char sz[MAXBUFFERSIZE] = "";
  196. _itoa((INT)lParam, sz, 10);
  197. _MBSTOWCS( aszStatusStrings[ wParam],
  198. sz,
  199. WCHARSIN( sizeof( sz)),
  200. ACHARSIN( lstrlenA( sz)+1));
  201. #else
  202. _itoa(lParam, aszStatusStrings[wParam], 10);
  203. #endif
  204. }
  205. else
  206. {
  207. #ifdef RLWIN32
  208. CopyMemory( aszStatusStrings[ wParam],
  209. (LPTSTR)lParam,
  210. min( MAXBUFFERSIZE, MEMSIZE( lstrlen( (LPTSTR)lParam) + 1)));
  211. aszStatusStrings[ wParam][ MAXBUFFERSIZE - 1] = TEXT('\0');
  212. #else
  213. _fstrncpy(aszStatusStrings[wParam], (LPTSTR)lParam, MAXBUFFERSIZE-1);
  214. #endif
  215. aszStatusStrings[wParam][MAXBUFFERSIZE-1] = 0;
  216. }
  217. InvalidateRect( hWnd, NULL, TRUE );
  218. break;
  219. case WM_PAINT:
  220. {
  221. RECT r;
  222. HFONT hOldFont;
  223. HBRUSH hbrOld, hbrFace, hbrHilite, hbrShadow;
  224. TEXTMETRIC tm;
  225. int iWidth, iHeight;
  226. UINT i;
  227. /* Obtain a handle to the device context */
  228. memset((void *)&ps, 0x00, sizeof(PAINTSTRUCT));
  229. hDC = BeginPaint(hWnd, &ps);
  230. GetTextMetrics(hDC, &tm);
  231. GetClientRect( hWnd, &r );
  232. iWidth = r.right - r.left;
  233. iHeight = r.bottom - r.top;
  234. // Create the brushes for the 3D effect
  235. hbrFace = CreateSolidBrush(RGB(0xC0, 0xC0, 0xC0));
  236. hbrHilite = CreateSolidBrush(RGB(0xFF, 0xFF, 0xFF));
  237. hbrShadow = CreateSolidBrush(RGB(0x80, 0x80, 0x80));
  238. // Paint outer 3D effect for raised slab
  239. hbrOld = (HBRUSH)SelectObject(hDC, (HGDIOBJ)hbrHilite);
  240. PatBlt(hDC, r.left, r.top, iWidth, 1, PATCOPY);
  241. PatBlt(hDC, r.left, r.top+1, 1, iHeight-2, PATCOPY);
  242. SelectObject(hDC, (HGDIOBJ)hbrShadow);
  243. PatBlt(hDC, r.left, r.bottom-1, iWidth, 1, PATCOPY);
  244. PatBlt(hDC, r.right-1, r.top+1, 1, iHeight-2, PATCOPY);
  245. // Paint surface of slab
  246. r.left += 1;
  247. r.top += 1;
  248. r.right -= 1;
  249. r.bottom -= 1;
  250. iWidth -= 2;
  251. iHeight -= 2;
  252. SelectObject(hDC, (HGDIOBJ)hbrFace);
  253. PatBlt(hDC, r.left, r.top, iWidth, iHeight, PATCOPY);
  254. // Get Courier font
  255. hOldFont = (HFONT)SelectObject( hDC, (HGDIOBJ)hFontCourier );
  256. SetBkColor(hDC, RGB(0xC0, 0xC0, 0xC0));
  257. // Paint inner 3D effect for tray carved into slab and write text
  258. r.left += 9;
  259. r.right -= 9;
  260. r.top += 3;
  261. r.bottom -= 3;
  262. iHeight = r.bottom - r.top;
  263. for (i = 0; i < cFields; i++)
  264. {
  265. iWidth = tm.tmMaxCharWidth * aiSize[i];
  266. r.right = r.left + iWidth - 2;
  267. SelectObject(hDC, (HGDIOBJ)hbrShadow);
  268. PatBlt(hDC, r.left-1, r.top-1, iWidth, 1, PATCOPY);
  269. PatBlt(hDC, r.left-1, r.top, 1, iHeight-2, PATCOPY);
  270. SelectObject(hDC, (HGDIOBJ)hbrHilite);
  271. PatBlt(hDC, r.left-1, r.bottom, iWidth, 1, PATCOPY);
  272. PatBlt(hDC, r.left + iWidth-1, r.top, 1, iHeight-2, PATCOPY);
  273. DrawText(hDC, aszStatusStrings[i],
  274. STRINGSIZE( lstrlen( aszStatusStrings[i])),
  275. &r, DT_SINGLELINE);
  276. r.left += iWidth + 8;
  277. }
  278. // Put old brush back and delete the rest
  279. SelectObject(hDC, (HGDIOBJ)hbrOld);
  280. DeleteObject((HGDIOBJ)hbrFace);
  281. DeleteObject((HGDIOBJ)hbrHilite);
  282. DeleteObject((HGDIOBJ)hbrShadow);
  283. SelectObject(hDC,(HGDIOBJ)hOldFont);
  284. EndPaint ( hWnd, (CONST PAINTSTRUCT *)&ps );
  285. break; /* End of WM_PAINT */
  286. }
  287. }
  288. return( DefWindowProc( hWnd, wMsg, wParam, lParam ));
  289. }
  290. /**
  291. * Function: cwCenter
  292. * Centers Dialog boxes in main window.
  293. *
  294. **/
  295. void cwCenter( HWND hWnd, int top )
  296. {
  297. POINT pt;
  298. RECT swp;
  299. RECT rParent;
  300. int iwidth;
  301. int iheight;
  302. GetWindowRect(hWnd, &swp);
  303. GetWindowRect(hMainWnd, &rParent);
  304. /* calculate the height and width for MoveWindow */
  305. iwidth = swp.right - swp.left;
  306. iheight = swp.bottom - swp.top;
  307. /* find the center point */
  308. pt.x = (rParent.right - rParent.left) / 2;
  309. pt.y = (rParent.bottom - rParent.top) / 2;
  310. /* calculate the new x, y starting point */
  311. pt.x = pt.x - (iwidth / 2);
  312. pt.y = pt.y - (iheight / 2);
  313. ClientToScreen(hMainWnd,&pt);
  314. /* top will adjust the window position, up or down */
  315. if(top)
  316. pt.y = pt.y + top;
  317. if (pt.x < 0)
  318. pt.x=0;
  319. else
  320. if (pt.x + iwidth > GetSystemMetrics(SM_CXSCREEN))
  321. pt.x = GetSystemMetrics(SM_CXSCREEN)-iwidth;
  322. /* move the window */
  323. MoveWindow(hWnd, pt.x, pt.y, iwidth, iheight, FALSE);
  324. }
  325. /**
  326. * Function: szFilterSpecFromSz1Sz2
  327. * Returns a filter spec with the format "%s\0%s\0\0" suitable for
  328. * use with the Windows 3.1 standard load dialog box.
  329. *
  330. * Arguments:
  331. * sz, destination buffer
  332. * sz1, first string
  333. * sz2, second string
  334. *
  335. * Returns:
  336. * result in sz
  337. *
  338. * Error Codes:
  339. * none
  340. *
  341. * Comments:
  342. * Performs no bounds checking. sz is assumed to be large enough to
  343. * accomidate the filter string.
  344. *
  345. * History:
  346. * 2/92, Implemented SteveBl
  347. */
  348. void szFilterSpecFromSz1Sz2(CHAR *sz,CHAR *sz1, CHAR *sz2)
  349. {
  350. int i1 = 0;
  351. int i2 = 0;
  352. while (sz[i1++] = sz1[i2++]);
  353. i2 = 0;
  354. while (sz[i1++] = sz2[i2++]);
  355. sz[i1]=0;
  356. }
  357. /**
  358. * Function: CatSzFilterSpecs
  359. * Concatonates two szFilterSpecs (double null terminated strings)
  360. * and returns a buffer with the result.
  361. *
  362. * Arguments:
  363. * sz, destination buffer
  364. * sz1, first Filter Spec
  365. * sz2, second Filter Spec
  366. *
  367. * Returns:
  368. * result in sz
  369. *
  370. * Error Codes:
  371. * none
  372. *
  373. * Comments:
  374. * performs no bounds checking
  375. *
  376. * History:
  377. * 3/92, initial implementation -- SteveBl
  378. */
  379. void CatSzFilterSpecs(CHAR *sz,CHAR *sz1,CHAR *sz2)
  380. {
  381. int i1 = 0;
  382. int i2 = 0;
  383. while (sz1[i2] || sz1[i2+1]) // looking for double byte
  384. {
  385. sz[i1++]=sz1[i2++];
  386. }
  387. sz[i1++] = '\0';
  388. i2 = 0;
  389. while (sz2[i2] || sz2[i2+1])
  390. {
  391. sz[i1++]=sz2[i2++];
  392. }
  393. sz[i1++] = '\0';
  394. sz[i1++] = '\0';
  395. }