Windows NT 4.0 source code leak
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.

533 lines
12 KiB

4 years ago
  1. /******************************Module*Header*******************************\
  2. * Module Name: cutils.c
  3. *
  4. * Common utilities for common controls
  5. *
  6. * Created: dd-mm-94
  7. * Author: [Unkown]
  8. *
  9. * Copyright (c) 1994 Microsoft Corporation
  10. \**************************************************************************/
  11. #include "ctlspriv.h"
  12. int iDitherCount = 0;
  13. HBRUSH hbrDither = NULL;
  14. int nSysColorChanges = 0;
  15. DWORD rgbFace; // globals used a lot
  16. DWORD rgbShadow;
  17. DWORD rgbHilight;
  18. DWORD rgbFrame;
  19. #ifdef WIN32
  20. extern HINSTANCE ghInst; // pick up the one from Winmm
  21. #else
  22. HINSTANCE ghInst;
  23. #endif
  24. int iThumbCount = 0;
  25. HBITMAP hbmThumb = NULL; // the thumb bitmap
  26. int g_cxEdge;
  27. int g_cyEdge;
  28. int g_cxScreen;
  29. int g_cyScreen;
  30. COLORREF g_clrWindow;
  31. COLORREF g_clrWindowText;
  32. HBRUSH g_hbrWindowFrame;
  33. #define CCS_ALIGN (CCS_TOP|CCS_NOMOVEY|CCS_BOTTOM)
  34. /*****************************Private*Routine******************************\
  35. * NewSize
  36. *
  37. * Note that the default alignment is CCS_BOTTOM
  38. *
  39. *
  40. * History:
  41. * 18-11-93 - StephenE - Created
  42. *
  43. \**************************************************************************/
  44. VOID
  45. NewSize(
  46. HWND hWnd,
  47. int nHeight,
  48. LONG style,
  49. int left,
  50. int top,
  51. int width,
  52. int height
  53. )
  54. {
  55. RECT rc, rcWindow, rcBorder;
  56. /* Resize the window unless the user said not to
  57. */
  58. if (!(style & CCS_NORESIZE))
  59. {
  60. /* Calculate the borders around the client area of the status bar
  61. */
  62. GetWindowRect(hWnd, &rcWindow);
  63. rcWindow.right -= rcWindow.left;
  64. rcWindow.bottom -= rcWindow.top;
  65. GetClientRect(hWnd, &rc);
  66. ClientToScreen(hWnd, (LPPOINT)&rc);
  67. rcBorder.left = rc.left - rcWindow.left;
  68. rcBorder.top = rc.top - rcWindow.top ;
  69. rcBorder.right = rcWindow.right - rc.right - rcBorder.left;
  70. rcBorder.bottom = rcWindow.bottom - rc.bottom - rcBorder.top ;
  71. nHeight += (int)(rcBorder.top + rcBorder.bottom);
  72. /* Check whether to align to the parent window
  73. */
  74. if (style & CCS_NOPARENTALIGN)
  75. {
  76. /* Check out whether this bar is top aligned or bottom aligned
  77. */
  78. switch ((style&CCS_ALIGN))
  79. {
  80. case CCS_TOP:
  81. case CCS_NOMOVEY:
  82. break;
  83. default:
  84. top = top + height - nHeight;
  85. }
  86. }
  87. else
  88. {
  89. /* It is assumed there is a parent by default
  90. */
  91. GetClientRect(GetParent(hWnd), &rc);
  92. /* Don't forget to account for the borders
  93. */
  94. left = -(int)rcBorder.left;
  95. width = (int)(rc.right + rcBorder.left + rcBorder.right);
  96. if ((style&CCS_ALIGN) == CCS_TOP)
  97. top = -(int)rcBorder.top;
  98. else if ((style&CCS_ALIGN) != CCS_NOMOVEY)
  99. top = (int)(rc.bottom - nHeight + rcBorder.bottom);
  100. }
  101. SetWindowPos(hWnd, NULL, left, top, width, nHeight, SWP_NOZORDER);
  102. }
  103. }
  104. /*****************************Private*Routine******************************\
  105. * CreateDitherBitmap
  106. *
  107. *
  108. *
  109. * History:
  110. * dd-mm-94 - Unkown - Created
  111. *
  112. \**************************************************************************/
  113. STATICFN HBITMAP NEAR PASCAL
  114. CreateDitherBitmap(
  115. void
  116. )
  117. {
  118. PBITMAPINFO pbmi;
  119. HBITMAP hbm;
  120. HDC hdc;
  121. int i;
  122. long patGray[8];
  123. DWORD rgb;
  124. pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD) * 16));
  125. if (!pbmi)
  126. return NULL;
  127. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  128. pbmi->bmiHeader.biWidth = 8;
  129. pbmi->bmiHeader.biHeight = 8;
  130. pbmi->bmiHeader.biPlanes = 1;
  131. pbmi->bmiHeader.biBitCount = 1;
  132. pbmi->bmiHeader.biCompression = BI_RGB;
  133. rgb = GetSysColor(COLOR_BTNFACE);
  134. pbmi->bmiColors[0].rgbBlue = GetBValue(rgb);
  135. pbmi->bmiColors[0].rgbGreen = GetGValue(rgb);
  136. pbmi->bmiColors[0].rgbRed = GetRValue(rgb);
  137. pbmi->bmiColors[0].rgbReserved = 0;
  138. rgb = GetSysColor(COLOR_BTNHIGHLIGHT);
  139. pbmi->bmiColors[1].rgbBlue = GetBValue(rgb);
  140. pbmi->bmiColors[1].rgbGreen = GetGValue(rgb);
  141. pbmi->bmiColors[1].rgbRed = GetRValue(rgb);
  142. pbmi->bmiColors[1].rgbReserved = 0;
  143. /* initialize the brushes */
  144. for (i = 0; i < 8; i++)
  145. if (i & 1)
  146. patGray[i] = 0xAAAA5555L; // 0x11114444L; // lighter gray
  147. else
  148. patGray[i] = 0x5555AAAAL; // 0x11114444L; // lighter gray
  149. hdc = GetDC(NULL);
  150. hbm = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT, patGray, pbmi, DIB_RGB_COLORS);
  151. ReleaseDC(NULL, hdc);
  152. LocalFree(pbmi);
  153. return hbm;
  154. }
  155. /*****************************Private*Routine******************************\
  156. * MySetObjectOwner
  157. *
  158. * Purpose: Call SetObjectOwner in GDI, eliminating "<Object> not released"
  159. * error messages when an app terminates.
  160. * Returns: Yep
  161. *
  162. * History:
  163. * dd-mm-94 - Unkown - Created
  164. *
  165. \**************************************************************************/
  166. STATICFN void
  167. MySetObjectOwner(
  168. HANDLE hObject
  169. )
  170. {
  171. #ifndef WIN32
  172. VOID (FAR PASCAL *lpSetObjOwner)(HANDLE, HANDLE);
  173. HMODULE hMod;
  174. hMod = GetModuleHandle("GDI");
  175. if (hMod)
  176. {
  177. (FARPROC)lpSetObjOwner = GetProcAddress(hMod, MAKEINTRESOURCE(461));
  178. if (lpSetObjOwner)
  179. {
  180. (lpSetObjOwner)(hObject, ghInst);
  181. }
  182. }
  183. #endif
  184. }
  185. /*****************************Private*Routine******************************\
  186. * CreateDitherBrush
  187. *
  188. * initialize the hbrDither global brush
  189. * Call this with bIgnoreCount == TRUE if you just want to update the
  190. * current dither brush.
  191. *
  192. * History:
  193. * dd-mm-94 - Unkown - Created
  194. *
  195. \**************************************************************************/
  196. BOOL FAR PASCAL
  197. CreateDitherBrush(
  198. BOOL bIgnoreCount
  199. )
  200. {
  201. HBITMAP hbmGray;
  202. HBRUSH hbrSave;
  203. if (bIgnoreCount && !iDitherCount)
  204. {
  205. return TRUE;
  206. }
  207. if (iDitherCount>0 && !bIgnoreCount)
  208. {
  209. iDitherCount++;
  210. return TRUE;
  211. }
  212. hbmGray = CreateDitherBitmap();
  213. if (hbmGray)
  214. {
  215. hbrSave = hbrDither;
  216. hbrDither = CreatePatternBrush(hbmGray);
  217. DeleteObject(hbmGray);
  218. if (hbrDither)
  219. {
  220. MySetObjectOwner(hbrDither);
  221. if (hbrSave)
  222. {
  223. DeleteObject(hbrSave);
  224. }
  225. if (!bIgnoreCount)
  226. {
  227. iDitherCount = 1;
  228. }
  229. return TRUE;
  230. }
  231. else
  232. {
  233. hbrDither = hbrSave;
  234. }
  235. }
  236. return FALSE;
  237. }
  238. /*****************************Public**Routine******************************\
  239. * GetDitherBrush
  240. *
  241. *
  242. *
  243. * History:
  244. * dd-mm-94 - Unkown - Created
  245. *
  246. \**************************************************************************/
  247. HBRUSH WINAPI
  248. GetDitherBrush(
  249. void
  250. )
  251. {
  252. return hbrDither;
  253. }
  254. /*****************************Private*Routine******************************\
  255. * FreeDitherBrush
  256. *
  257. *
  258. *
  259. * History:
  260. * dd-mm-94 - Unkown - Created
  261. *
  262. \**************************************************************************/
  263. BOOL FAR PASCAL
  264. FreeDitherBrush(
  265. void
  266. )
  267. {
  268. iDitherCount--;
  269. if (iDitherCount > 0)
  270. return FALSE;
  271. if (hbrDither)
  272. DeleteObject(hbrDither);
  273. hbrDither = NULL;
  274. return TRUE;
  275. }
  276. /*****************************Private*Routine******************************\
  277. * CreateThumb
  278. *
  279. * initialize the hbmThumb global bitmap
  280. * Call this with bIgnoreCount == TRUE if you just want to update the
  281. * current bitmap.
  282. *
  283. * History:
  284. * dd-mm-94 - Unkown - Created
  285. *
  286. \**************************************************************************/
  287. void FAR PASCAL
  288. CreateThumb(
  289. BOOL bIgnoreCount
  290. )
  291. {
  292. HBITMAP hbmSave;
  293. #ifdef WIN32
  294. if ( ghInst == NULL ) {
  295. ghInst = GetModuleHandle( MODULENAME );
  296. }
  297. #endif
  298. if (bIgnoreCount && !iThumbCount)
  299. {
  300. return;
  301. }
  302. if (iThumbCount && !bIgnoreCount)
  303. {
  304. ++iThumbCount;
  305. return;
  306. }
  307. hbmSave = hbmThumb;
  308. hbmThumb = CreateMappedBitmap(ghInst, IDB_THUMB, CMB_MASKED, NULL, 0);
  309. if (hbmThumb)
  310. {
  311. if (hbmSave)
  312. {
  313. DeleteObject(hbmSave);
  314. }
  315. if (!bIgnoreCount)
  316. {
  317. iThumbCount = 1;
  318. }
  319. }
  320. else
  321. {
  322. hbmThumb = hbmSave;
  323. }
  324. }
  325. /*****************************Private*Routine******************************\
  326. * DestroyThumb
  327. *
  328. *
  329. *
  330. * History:
  331. * dd-mm-94 - Unkown - Created
  332. *
  333. \**************************************************************************/
  334. void FAR PASCAL
  335. DestroyThumb(
  336. void
  337. )
  338. {
  339. iThumbCount--;
  340. if (iThumbCount <= 0)
  341. {
  342. if (hbmThumb)
  343. {
  344. DeleteObject(hbmThumb);
  345. }
  346. hbmThumb = NULL;
  347. iThumbCount = 0;
  348. }
  349. }
  350. /*****************************Private*Routine******************************\
  351. * CheckSysColors
  352. *
  353. * Note that the trackbar will pass in NULL for pTBState, because it
  354. * just wants the dither brush to be updated.
  355. *
  356. *
  357. * History:
  358. * dd-mm-94 - Unkown - Created
  359. *
  360. \**************************************************************************/
  361. void FAR PASCAL
  362. CheckSysColors(
  363. void
  364. )
  365. {
  366. static COLORREF rgbSaveFace = 0xffffffffL,
  367. rgbSaveShadow = 0xffffffffL,
  368. rgbSaveHilight = 0xffffffffL,
  369. rgbSaveFrame = 0xffffffffL;
  370. rgbFace = GetSysColor(COLOR_BTNFACE);
  371. rgbShadow = GetSysColor(COLOR_BTNSHADOW);
  372. rgbHilight = GetSysColor(COLOR_BTNHIGHLIGHT);
  373. rgbFrame = GetSysColor(COLOR_WINDOWFRAME);
  374. g_clrWindow = (COLORREF)GetSysColor(COLOR_WINDOW);
  375. g_clrWindowText = (COLORREF)GetSysColor(COLOR_WINDOWTEXT);
  376. if (rgbSaveFace!=rgbFace || rgbSaveShadow!=rgbShadow
  377. || rgbSaveHilight!=rgbHilight || rgbSaveFrame!=rgbFrame)
  378. {
  379. ++nSysColorChanges;
  380. // Update the brush for pushed-in buttons
  381. CreateDitherBrush(TRUE);
  382. CreateThumb(TRUE);
  383. rgbSaveFace = rgbFace;
  384. rgbSaveShadow = rgbShadow;
  385. rgbSaveHilight = rgbHilight;
  386. rgbSaveFrame = rgbFrame;
  387. if (g_hbrWindowFrame) {
  388. DeleteObject(g_hbrWindowFrame);
  389. }
  390. g_hbrWindowFrame = CreateSolidBrush( rgbFrame );
  391. }
  392. }
  393. /******************************Public*Routine******************************\
  394. * InitGlobalMetrics
  395. *
  396. *
  397. *
  398. * History:
  399. * dd-mm-94 - StephenE - Created
  400. *
  401. \**************************************************************************/
  402. void FAR PASCAL
  403. InitGlobalMetrics(
  404. void
  405. )
  406. {
  407. g_cxEdge = GetSystemMetrics(SM_CXBORDER);
  408. g_cyEdge = GetSystemMetrics(SM_CYBORDER);
  409. g_cxScreen = GetSystemMetrics(SM_CXSCREEN);
  410. g_cyScreen = GetSystemMetrics(SM_CYSCREEN);
  411. }
  412. /******************************Public*Routine******************************\
  413. * MGetTextExtent
  414. *
  415. *
  416. *
  417. * History:
  418. * dd-mm-94 - StephenE - Created
  419. *
  420. \**************************************************************************/
  421. BOOL FAR PASCAL
  422. MGetTextExtent(
  423. HDC hdc, LPCTSTR lpstr,
  424. int cnt,
  425. int FAR * pcx,
  426. int FAR * pcy
  427. )
  428. {
  429. BOOL fSuccess;
  430. SIZE size = {0,0};
  431. fSuccess=GetTextExtentPoint(hdc, lpstr, cnt, &size);
  432. if (pcx)
  433. *pcx=size.cx;
  434. if (pcy)
  435. *pcy=size.cy;
  436. return fSuccess;
  437. }
  438. /******************************Public*Routine******************************\
  439. * RelayToToolTips
  440. *
  441. *
  442. *
  443. * History:
  444. * dd-mm-94 - StephenE - Created
  445. *
  446. \**************************************************************************/
  447. void FAR PASCAL
  448. RelayToToolTips(
  449. HWND hwndToolTips,
  450. HWND hWnd,
  451. UINT wMsg,
  452. WPARAM wParam,
  453. LPARAM lParam
  454. )
  455. {
  456. if(hwndToolTips) {
  457. MSG msg;
  458. msg.lParam = lParam;
  459. msg.wParam = wParam;
  460. msg.message = wMsg;
  461. msg.hwnd = hWnd;
  462. SendMessage(hwndToolTips, TTM_RELAYEVENT, 0, (LPARAM)(LPMSG)&msg);
  463. }
  464. }