bv_text.c
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 11k
Category:

Windows Develop

Development Platform:

Visual C++

  1. /* File: C:WACKERTDLLBV_TEXT.C (Created: 11-Jan-1994)
  2.  * Created from:
  3.  * File: C:HA5Gha5gstxtproc.c (Created: 27-SEP-1991)
  4.  *
  5.  * Copyright 1994 by Hilgraeve Inc. -- Monroe, MI
  6.  * All rights reserved
  7.  *
  8.  * $Revision: 1 $
  9.  * $Date: 10/05/98 12:37p $
  10.  */
  11. // #define DEBUGSTR 1
  12. #define WE_DRAW_EDGE 1
  13. #include <windows.h>
  14. #include <tdllstdtyp.h>
  15. #include <tdllmc.h>
  16. #include <tdllassert.h>
  17. #include <termxfer_dlg.h>
  18. #include "bv_text.h"
  19. #include "bv_text.hh"
  20. /*
  21.  * This stuff can probably go away as soon as the two following styles are
  22.  * supported:
  23.  *
  24.  * SS_SUNKEN 0x00001000L
  25.  * SS_RAISED 0x00002000L
  26.  */
  27. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  28.  * FUNCTION:
  29.  * RegisterBeveledTextClass
  30.  *
  31.  * DESCRIPTION:
  32.  * Registers the VU Meter window class.  (No kidding!)
  33.  *
  34.  * PARAMETERS:
  35.  * Hinstance -- the instance handle
  36.  *
  37.  * RETURNS:
  38.  * Whatever RegisterClass returns.
  39.  */
  40. BOOL RegisterBeveledTextClass(HANDLE hInstance)
  41. {
  42. BOOL            bRetVal = TRUE;
  43. WNDCLASS        wndclass;
  44. if (bRetVal)
  45. {
  46. wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  47. wndclass.lpfnWndProc    = BeveledTextWndProc;
  48. wndclass.cbClsExtra     = 0;
  49. wndclass.cbWndExtra     = sizeof(VOID FAR *);
  50. wndclass.hIcon          = NULL;
  51. wndclass.hInstance      = hInstance;
  52. wndclass.hCursor        = NULL;
  53. wndclass.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
  54. wndclass.lpszMenuName   = NULL;
  55. wndclass.lpszClassName  = BV_TEXT_CLASS;
  56. bRetVal = RegisterClass(&wndclass);
  57. }
  58. return bRetVal;
  59. }
  60. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  61.  * FUNCTION: stxtDrawBeveledText
  62.  *
  63.  * DESCRIPTION:
  64.  *
  65.  * ARGUEMENTS:
  66.  *
  67.  * RETURNS:
  68.  *
  69.  */
  70. VOID stxtDrawBeveledText(
  71.  HDC hdc,
  72.  HFONT hNewFont,
  73.  LPRECT  lpRc,
  74.  USHORT  usWidth,
  75.  DWORD dwStyle,
  76.  ULONG FAR *pulColors,
  77.  LPTSTR pszText
  78. )
  79. {
  80. //ULONG  ulOldBkColor;
  81. //ULONG  ulOldTextColor;
  82. #if defined(WE_DRAW_EDGE)
  83. HBRUSH hBrush;
  84. #endif
  85. HFONT  hFont;
  86. //WORD  wFlags;
  87. WORD   wStyle;
  88. INT    nWidth;
  89. INT    nHeight;
  90. INT    nIndex;
  91. RECT   rcE;
  92. TEXTMETRIC tm;
  93. LPTSTR  lpstr;
  94. /*
  95.  * We redraw the edging completely, every time
  96.  */
  97. nHeight = (int)GetSystemMetrics(SM_CYBORDER) * (int)usWidth;
  98. nWidth = (int)GetSystemMetrics(SM_CXBORDER) * (int)usWidth;
  99. #if defined(WE_DRAW_EDGE)
  100. /* Draw the top edge */
  101. hBrush = CreateSolidBrush(pulColors[2]);
  102. for (nIndex = 0; nIndex < nHeight; nIndex += 1)
  103. {
  104. rcE = *lpRc;
  105. rcE.top = nIndex;
  106. rcE.bottom = nIndex + 1;
  107. rcE.right -= nIndex;
  108. FillRect(hdc, &rcE, hBrush);
  109. }
  110. /* Draw the left edge */
  111. for (nIndex = 0; nIndex < nWidth; nIndex += 1)
  112. {
  113. rcE = *lpRc;
  114. rcE.left = nIndex;
  115. rcE.right = nIndex + 1;
  116. rcE.bottom -= nIndex;
  117. FillRect(hdc, &rcE, hBrush);
  118. }
  119. DeleteObject(hBrush);
  120. /* Draw the bottom edge */
  121. hBrush = CreateSolidBrush(pulColors[3]);
  122. for (nIndex = 0; nIndex < nHeight; nIndex += 1)
  123. {
  124. rcE = *lpRc;
  125. rcE.top = rcE.bottom - nIndex - 1;
  126. rcE.bottom = rcE.bottom - nIndex;
  127. rcE.left += nIndex + 1;
  128. FillRect(hdc, &rcE, hBrush);
  129. }
  130. /* Draw the right edge */
  131. for (nIndex = 0; nIndex < nWidth; nIndex += 1)
  132. {
  133. rcE = *lpRc;
  134. rcE.left = rcE.right - nIndex - 1;
  135. rcE.right = rcE.right - nIndex;
  136. rcE.top += nIndex + 1;
  137. FillRect(hdc, &rcE, hBrush);
  138. }
  139. DeleteObject(hBrush);
  140. #else
  141. DrawEdge(hdc, lpRc,
  142.  EDGE_SUNKEN,
  143.  BF_SOFT | BF_RECT);
  144. #endif
  145. /*
  146.  * We redraw the text completely every time
  147.  */
  148. rcE = *lpRc;
  149. #if defined(WE_DRAW_EDGE)
  150. InflateRect(&rcE, -nWidth, -nHeight);
  151. #else
  152. InflateRect(&rcE, -2, -2);
  153. #endif
  154. /* -------------- Must have something to paint ------------- */
  155. if (pszText)
  156. lpstr = pszText;
  157. else
  158. lpstr = " ";
  159. /* -------------- Figure where to place it ------------- */
  160. nIndex = (int)lstrlen(lpstr);
  161. wStyle = (WORD)dwStyle;
  162. if (wStyle & SS_RIGHT)
  163. {
  164. SetTextAlign(hdc, TA_RIGHT);
  165. nWidth = rcE.right - (2 * nWidth);
  166. }
  167. else if (wStyle & SS_CENTER)
  168. {
  169. SetTextAlign(hdc, TA_CENTER);
  170. nWidth = ((rcE.right - rcE.left) / 2) + nWidth;
  171. }
  172. else
  173. {
  174. nWidth = 3 * nWidth;
  175. }
  176. SetBkColor(hdc, pulColors[0]);
  177. SetTextColor(hdc, pulColors[1]);
  178. if (hNewFont != (HFONT)0)
  179. hFont = SelectObject(hdc, hNewFont);
  180. GetTextMetrics(hdc, &tm);
  181. nHeight += ((rcE.bottom - rcE.top) - tm.tmHeight - 1) / 2;
  182. ExtTextOut(hdc, nWidth, nHeight, ETO_OPAQUE | ETO_CLIPPED, &rcE,
  183. lpstr, nIndex, (LPINT)0);
  184. if (hNewFont != (HFONT)0)
  185. SelectObject(hdc, hFont);
  186. }
  187. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  188.  * FUNCTION: StaticTextWndProc
  189.  *
  190.  * DESCRIPTION:
  191.  *
  192.  * ARGUEMENTS:
  193.  *
  194.  * RETURNS:
  195.  *
  196.  */
  197. LONG CALLBACK BeveledTextWndProc(HWND hWnd,
  198.   UINT wMsg,
  199.   WPARAM wPar,
  200.   LPARAM lPar)
  201. {
  202. LPSTEXT psT;
  203. switch (wMsg)
  204. {
  205. case WM_CREATE:
  206. psT = (LPSTEXT)malloc(sizeof(STEXT));
  207. SetWindowLong(hWnd, 0, (LONG)psT);
  208. psT->ulCheck = STEXT_VALID;
  209. psT->pszText = NULL;
  210. psT->hFont  = (HFONT)0;
  211. psT->fpOwnerDraw = (STXT_OWNERDRAW)0;
  212. switch (GetWindowLong(hWnd, GWL_STYLE) & 0x0000FF00)
  213. {
  214. case 0:
  215. default:
  216. psT->cBackGround = GetSysColor(COLOR_BTNFACE);
  217. psT->cTextColor  = GetSysColor(COLOR_BTNTEXT);
  218. psT->cUpperEdge  = GetSysColor(COLOR_BTNSHADOW);
  219. psT->cLowerEdge = GetSysColor(COLOR_BTNHIGHLIGHT);
  220. break;
  221. case BVS_ALTCLR:
  222. // case 0x100:
  223. psT->cBackGround = 0x00000000;
  224. psT->cTextColor  = 0x0000FF00;
  225. psT->cUpperEdge  = GetSysColor(COLOR_BTNSHADOW);
  226. psT->cLowerEdge  = GetSysColor(COLOR_BTNHIGHLIGHT);
  227. break;
  228. }
  229. psT->usDepth  = STXT_DEF_DEPTH;
  230. break;
  231. case WM_DESTROY:
  232. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  233. if (STEXT_OK(psT))
  234. {
  235. if (psT->pszText != NULL)
  236. free(psT->pszText);
  237. if (psT->fpOwnerDraw)
  238. FreeProcInstance((FARPROC)psT->fpOwnerDraw);
  239. free(psT);
  240. }
  241. SetWindowLong(hWnd, 0, 0L);
  242. break;
  243. case WM_GETDLGCODE:
  244. /* Static controls don't want any of these */
  245. return DLGC_STATIC;
  246. case WM_GETTEXTLENGTH:
  247. {
  248. ULONG ulLength = 0;
  249. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  250. if (STEXT_OK(psT))
  251. {
  252. if (psT->pszText != NULL)
  253. ulLength = (ULONG)lstrlen(psT->pszText);
  254. }
  255. return ((LONG)ulLength);
  256. }
  257. case WM_GETTEXT:
  258. {
  259. WORD wLength = 0;
  260. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  261. if (STEXT_OK(psT))
  262. {
  263. if (psT->pszText != NULL)
  264. {
  265. wLength = (WORD)lstrlen(psT->pszText);
  266. if (wPar < wLength)
  267. wLength = wPar;
  268. memcpy((LPTSTR)lPar, psT->pszText, wLength);
  269. }
  270. }
  271. return ((LONG)wLength);
  272. }
  273. case WM_SETTEXT:
  274. {
  275. WORD wLength;
  276. LPTSTR pszStr;
  277. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  278. if (STEXT_OK(psT))
  279. {
  280. CHAR  ach[128];
  281. INT   i, len;
  282. SIZE  sz;
  283. RECT  rc;
  284. HDC   hDC;
  285. HFONT hFont;
  286. DWORD dwStyle;
  287. ach[0] = 0;
  288. if (psT->pszText != NULL)
  289. {
  290. lstrcpy(ach, psT->pszText);
  291. free(psT->pszText);
  292. }
  293. psT->pszText = NULL;
  294. pszStr = (LPTSTR)lPar;
  295. if (pszStr != NULL)
  296. {
  297. wLength = (WORD)lstrlen(pszStr);
  298. if (wLength > 0)
  299. {
  300. psT->pszText = (LPTSTR)malloc(wLength + 1);
  301. lstrcpy(psT->pszText, pszStr);
  302. dwStyle = (DWORD)GetWindowLong(hWnd, GWL_STYLE);
  303. GetClientRect(hWnd, &rc);
  304. // Microsoft defined SS_LEFT to be zero!  
  305. // So the only way I can test SS_LEFT is
  306. // to see if bit 1 is zero
  307. switch (dwStyle & 3)
  308. {
  309. default:
  310. break;
  311. case SS_LEFT:
  312. {
  313. // Invalidate only from the point where the
  314. // string has changed.
  315. for (i=0, len=lstrlen(ach) ; i < len  ; ++i)
  316. {
  317. if (ach[i] != *pszStr++)
  318. break;
  319. }
  320. ach[i] = '';
  321. hDC = GetDC(hWnd);
  322. hFont = (HFONT)SendMessage(hWnd, WM_GETFONT, 0, 0L);
  323. if (hFont)
  324. hFont = SelectObject(hDC, hFont);
  325. GetTextExtentPoint(hDC, ach, i, &sz);
  326. if (hFont)
  327. SelectObject(hDC, hFont);
  328. ReleaseDC(hWnd, hDC);
  329. rc.left += sz.cx;
  330. /*
  331.  * We fudge because the font isn't fixed pitch
  332.  */
  333. rc.left -= min(sz.cx, 8);
  334. }
  335. break;
  336. case SS_RIGHT:
  337. {
  338. // Invalidate to the longer of the two strings.
  339. hDC = GetDC(hWnd);
  340. hFont = (HFONT)SendMessage(hWnd, WM_GETFONT, 0, 0L);
  341. if (hFont)
  342. hFont = SelectObject(hDC, hFont);
  343. GetTextExtentPoint(hDC, pszStr, lstrlen(pszStr), &sz);
  344. len = sz.cx;
  345. GetTextExtentPoint(hDC, ach, lstrlen(ach), &sz);
  346. if (hFont)
  347. SelectObject(hDC, hFont);
  348. ReleaseDC(hWnd, hDC);
  349. // rc.left += max(0, rc.right - ((int)GetSystemMetrics(SM_CXBORDER) * (int)psT->usDepth) - max(len, sz.cx) - 1 - 8);
  350. }
  351. break;
  352. }
  353. InvalidateRect(hWnd, &rc, FALSE);
  354. }
  355. }
  356. }
  357. }
  358. break;
  359. case WM_PAINT:
  360. {
  361. RECT rcC;
  362. PAINTSTRUCT ps;
  363. BeginPaint(hWnd, &ps);
  364. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  365. if (STEXT_OK(psT))
  366. {
  367. GetClientRect(hWnd, &rcC);
  368. stxtDrawBeveledText(
  369. ps.hdc,
  370. psT->hFont,
  371. (LPRECT)&rcC,
  372. psT->usDepth,
  373. (DWORD)GetWindowLong(hWnd, GWL_STYLE),
  374. (ULONG FAR *)&psT->cBackGround,
  375. psT->pszText
  376. );
  377. if (psT->fpOwnerDraw)
  378. (*psT->fpOwnerDraw)(hWnd, ps.hdc);
  379. }
  380. EndPaint(hWnd, &ps);
  381. }
  382. break;
  383. case WM_STXT_SET_BK:
  384. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  385. if (STEXT_OK(psT))
  386. {
  387. psT->cBackGround = (ULONG)lPar;
  388. InvalidateRect(hWnd, NULL, FALSE);
  389. }
  390. break;
  391. case WM_STXT_SET_TXT:
  392. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  393. if (STEXT_OK(psT))
  394. {
  395. psT->cTextColor = (ULONG)lPar;
  396. InvalidateRect(hWnd, NULL, FALSE);
  397. }
  398. break;
  399. case WM_STXT_SET_UE:
  400. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  401. if (STEXT_OK(psT))
  402. {
  403. psT->cUpperEdge = (ULONG)lPar;
  404. InvalidateRect(hWnd, NULL, FALSE);
  405. }
  406. break;
  407. case WM_STXT_SET_LE:
  408. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  409. if (STEXT_OK(psT))
  410. {
  411. psT->cLowerEdge = (ULONG)lPar;
  412. InvalidateRect(hWnd, NULL, FALSE);
  413. }
  414. break;
  415. case WM_STXT_SET_DEPTH:
  416. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  417. if (STEXT_OK(psT))
  418. {
  419. if (wPar < 7)
  420. {
  421. psT->usDepth = wPar;
  422. InvalidateRect(hWnd, NULL, TRUE);
  423. }
  424. }
  425. break;
  426. case WM_STXT_OWNERDRAW:
  427. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  428. psT->fpOwnerDraw = (STXT_OWNERDRAW)lPar;
  429. break;
  430. case WM_SETFONT:
  431. psT = (LPSTEXT)GetWindowLong(hWnd, 0);
  432. if (STEXT_OK(psT))
  433. psT->hFont = (HFONT)wPar;
  434. return 0L;
  435. default:
  436. break;
  437. }
  438. return DefWindowProc(hWnd, wMsg, wPar, (LONG)lPar);
  439. }