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.

352 lines
8.6 KiB

  1. /*
  2. **------------------------------------------------------------------------------
  3. ** Module: Disk Cleanup Applet
  4. ** File: textout.cpp
  5. **
  6. ** Purpose: Print functions
  7. ** Notes:
  8. ** Mod Log: Created by Jason Cobb (2/97)
  9. **
  10. ** Copyright (c)1997 Microsoft Corporation, All Rights Reserved
  11. **------------------------------------------------------------------------------
  12. */
  13. /*
  14. **------------------------------------------------------------------------------
  15. ** Project include files
  16. **------------------------------------------------------------------------------
  17. */
  18. #include "common.h"
  19. #include "textout.h"
  20. /*
  21. * DEFINITIONS ________________________________________________________________
  22. *
  23. */
  24. typedef struct
  25. {
  26. HWND hWnd;
  27. LONG style;
  28. TCHAR *pszText; // Buffer for text
  29. size_t cbMaxText; // Length of text buffer
  30. HFONT hf; // Current font
  31. HBITMAP bmp; // Off-screen bitmap for NO FLICKER
  32. RECT rBmp; // Size of 'bmp'
  33. } TextOutInfo;
  34. /*
  35. * VARIABLES __________________________________________________________________
  36. *
  37. */
  38. static HINSTANCE l_hInst = NULL;
  39. /*
  40. * PROTOTYPES _________________________________________________________________
  41. *
  42. */
  43. LRESULT APIENTRY TextOutProc (HWND, UINT, WPARAM, LPARAM);
  44. void TextOutPaint (TextOutInfo *, HDC, RECT *);
  45. BOOL TextOutMakeBitmap (TextOutInfo *);
  46. BOOL TextOutSetText (TextOutInfo *, LPCTSTR);
  47. void TextOutRedraw (TextOutInfo *, BOOL = TRUE);
  48. /*
  49. * ROUTINES ___________________________________________________________________
  50. *
  51. */
  52. /*
  53. void RegisterTextOutClass (HINSTANCE hInst)
  54. {
  55. WNDCLASS wc;
  56. if (hInst == NULL)
  57. return;
  58. l_hInst = hInst;
  59. wc.style = 0;
  60. wc.lpfnWndProc = TextOutProc;
  61. wc.cbClsExtra = 0;
  62. wc.cbWndExtra = sizeof (void *);
  63. wc.hInstance = hInst;
  64. wc.hIcon = NULL;
  65. wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  66. wc.hbrBackground = NULL;
  67. wc.lpszMenuName = NULL;
  68. wc.lpszClassName = szTextOutCLASS;
  69. RegisterClass (&wc);
  70. }
  71. void UnregisterTextOutClass (void)
  72. {
  73. // Unregister from windows
  74. if (l_hInst)
  75. UnregisterClass (szTextOutCLASS, l_hInst);
  76. l_hInst = NULL;
  77. }
  78. LRESULT APIENTRY TextOutProc (HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
  79. {
  80. TextOutInfo *ptoi;
  81. PAINTSTRUCT ps;
  82. HDC hdc;
  83. if (msg == WM_CREATE)
  84. {
  85. if ((ptoi = (TextOutInfo *)GlobalAlloc (GMEM_FIXED, sizeof(*ptoi))) == 0)
  86. {
  87. //OutOfMemory();
  88. return -1;
  89. }
  90. SetWindowLongPtr (hWnd, 0, (LPARAM)ptoi);
  91. ptoi->hWnd = hWnd;
  92. ptoi->style = ((LPCREATESTRUCT)lp)->style;
  93. ptoi->pszText = NULL;
  94. ptoi->cbMaxText = 0;
  95. ptoi->hf = (HFONT) GetStockObject (DEFAULT_GUI_FONT);
  96. ptoi->bmp = NULL;
  97. if (!TextOutMakeBitmap (ptoi))
  98. return -1;
  99. if (!TextOutSetText (ptoi, ((LPCREATESTRUCT)lp)->lpszName ))
  100. return -1;
  101. TextOutRedraw (ptoi);
  102. }
  103. else if ((ptoi = (TextOutInfo *)GetWindowLongPtr (hWnd, 0)) == NULL)
  104. {
  105. return DefWindowProc (hWnd, msg, wp, lp);
  106. }
  107. switch (msg)
  108. {
  109. case WM_DESTROY: if (ptoi->pszText != NULL)
  110. GlobalFree (ptoi->pszText);
  111. if (ptoi->bmp != NULL)
  112. DeleteObject (ptoi->bmp);
  113. GlobalFree (ptoi);
  114. SetWindowLongPtr (hWnd, 0, 0L);
  115. break;
  116. case WM_USER: TextOutRedraw (ptoi, FALSE);
  117. break;
  118. case WM_PAINT: hdc = BeginPaint (hWnd, &ps);
  119. TextOutPaint (ptoi, hdc, &ps.rcPaint);
  120. EndPaint (hWnd, &ps);
  121. return 0;
  122. break;
  123. case WM_SETTEXT: TextOutSetText (ptoi, (LPCTSTR)lp);
  124. TextOutRedraw (ptoi);
  125. break;
  126. case WM_GETFONT: return (LRESULT)ptoi->hf;
  127. break;
  128. case WM_SETFONT: if ((ptoi->hf = (HFONT)wp) == NULL)
  129. {
  130. ptoi->hf = (HFONT) GetStockObject (DEFAULT_GUI_FONT);
  131. }
  132. if (LOWORD(lp) != 0)
  133. {
  134. TextOutRedraw (ptoi);
  135. }
  136. break;
  137. case WM_SIZE: TextOutMakeBitmap (ptoi);
  138. break;
  139. case WM_ERASEBKGND:
  140. return (LRESULT)1;
  141. break;
  142. }
  143. return DefWindowProc (hWnd, msg, wp, lp);
  144. }
  145. void TextOutPaint (TextOutInfo *ptoi, HDC hdcTrg, RECT *pr)
  146. {
  147. RECT r;
  148. HDC hdcSrc;
  149. HBITMAP bmpSrc;
  150. if (ptoi->bmp == NULL) // No bitmap?
  151. return; // No paint.
  152. if (pr == NULL)
  153. {
  154. GetClientRect (ptoi->hWnd, pr = &r);
  155. }
  156. hdcSrc = CreateCompatibleDC (hdcTrg);
  157. bmpSrc = (HBITMAP) SelectObject (hdcSrc, (HGDIOBJ)ptoi->bmp);
  158. BitBlt (hdcTrg, pr->left, pr->top, pr->right -pr->left, pr->bottom -pr->top,
  159. hdcSrc, pr->left, pr->top, SRCCOPY);
  160. SelectObject (hdcSrc, (HGDIOBJ)bmpSrc);
  161. DeleteDC (hdcSrc);
  162. }
  163. BOOL TextOutMakeBitmap (TextOutInfo *ptoi)
  164. {
  165. HDC hdc, hdcMem;
  166. RECT r;
  167. GetClientRect (ptoi->hWnd, &r);
  168. if (ptoi->bmp != NULL)
  169. {
  170. if ( ((ptoi->rBmp.right - ptoi->rBmp.left) > (r.right - r.left)) ||
  171. ((ptoi->rBmp.bottom - ptoi->rBmp.top) > (r.bottom - r.top)) )
  172. {
  173. DeleteObject (ptoi->bmp);
  174. ptoi->bmp = NULL;
  175. }
  176. }
  177. if (ptoi->bmp == NULL)
  178. {
  179. hdc = GetDC (ptoi->hWnd);
  180. hdcMem = CreateCompatibleDC (hdc);
  181. ptoi->bmp = CreateCompatibleBitmap (hdc, r.right, r.bottom);
  182. ptoi->rBmp = r;
  183. DeleteDC (hdcMem);
  184. ReleaseDC (ptoi->hWnd, hdc);
  185. }
  186. if (ptoi->bmp == NULL)
  187. {
  188. //OutOfMemory();
  189. return FALSE;
  190. }
  191. return TRUE;
  192. }
  193. BOOL TextOutSetText (TextOutInfo *ptoi, LPCTSTR psz)
  194. {
  195. size_t cb;
  196. if (psz == NULL)
  197. {
  198. if (ptoi->pszText != NULL)
  199. ptoi->pszText[0] = 0;
  200. return TRUE;
  201. }
  202. cb = 1+ lstrlen(psz);
  203. if (cb > (ptoi->cbMaxText))
  204. {
  205. if (ptoi->pszText != NULL)
  206. {
  207. GlobalFree (ptoi->pszText);
  208. ptoi->pszText = NULL;
  209. }
  210. if ((ptoi->pszText = (TCHAR *)GlobalAlloc (GMEM_FIXED, cb * sizeof( TCHAR ))) == NULL)
  211. {
  212. ptoi->cbMaxText = 0;
  213. //OutOfMemory();
  214. return FALSE;
  215. }
  216. ptoi->cbMaxText = cb;
  217. }
  218. StrCpy(ptoi->pszText, psz);
  219. return TRUE;
  220. }
  221. void TextOutRedraw (TextOutInfo *ptoi, BOOL fRepaint)
  222. {
  223. HBRUSH hbr;
  224. HDC hdc, hdcMem;
  225. HBITMAP bmpOld;
  226. RECT r;
  227. WPARAM wp;
  228. LPARAM lp;
  229. HFONT hfOld = NULL;
  230. if (ptoi->bmp == NULL)
  231. return;
  232. hdc = GetDC (ptoi->hWnd);
  233. hdcMem = CreateCompatibleDC (hdc);
  234. bmpOld = (HBITMAP) SelectObject (hdcMem, (HGDIOBJ)ptoi->bmp);
  235. wp = (WPARAM)hdcMem;
  236. lp = (LPARAM)ptoi->hWnd;
  237. hbr = (HBRUSH)SendMessage (GetParent(ptoi->hWnd), WM_CTLCOLORSTATIC, wp,lp);
  238. if (hbr == NULL)
  239. {
  240. SetTextColor (hdc, GetSysColor (COLOR_BTNTEXT));
  241. SetBkColor (hdc, GetSysColor (COLOR_BTNFACE));
  242. hbr = CreateSolidBrush (GetSysColor (COLOR_BTNFACE));
  243. }
  244. GetClientRect (ptoi->hWnd, &r);
  245. FillRect (hdcMem, &r, hbr);
  246. if (ptoi->pszText != NULL)
  247. {
  248. if (ptoi->hf != NULL)
  249. hfOld = (HFONT) SelectObject (hdcMem, ptoi->hf);
  250. UINT fDrawFlags = DT_EXPANDTABS | DT_WORDBREAK;
  251. if (ptoi->style & SS_RIGHT)
  252. fDrawFlags |= DT_RIGHT;
  253. else if (ptoi->style & SS_CENTER)
  254. fDrawFlags |= DT_CENTER;
  255. else
  256. fDrawFlags |= DT_LEFT;
  257. DrawText ( hdcMem,
  258. ptoi->pszText,
  259. lstrlen(ptoi->pszText),
  260. &r,
  261. fDrawFlags );
  262. if (hfOld != NULL)
  263. SelectObject (hdcMem, hfOld);
  264. }
  265. DeleteObject (hbr);
  266. SelectObject (hdcMem, bmpOld);
  267. DeleteDC (hdcMem);
  268. ReleaseDC (ptoi->hWnd, hdc);
  269. if (fRepaint)
  270. {
  271. InvalidateRect (ptoi->hWnd, NULL, TRUE);
  272. UpdateWindow (ptoi->hWnd);
  273. }
  274. }
  275. */