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.

409 lines
9.6 KiB

  1. /*++
  2. Copyright (c) 1994-1998, Microsoft Corporation All rights reserved.
  3. Module Name:
  4. mousectl.c
  5. Abstract:
  6. This module contains the routines for the Mouse control.
  7. Revision History:
  8. --*/
  9. //
  10. // Include Files.
  11. //
  12. #include "main.h"
  13. #include "mousectl.h"
  14. #include "rc.h"
  15. //
  16. // Constant Declarations.
  17. //
  18. #define MOUSECTL_BITMAP_NAME MAKEINTRESOURCE(IDB_MOUSE)
  19. #define MOUSECTL_DATA 0
  20. //
  21. // These are indices into the VGA palette!
  22. //
  23. #define MOUSECTL_BKGND_INDEX 14 // light cyan
  24. #define MOUSECTL_LBUTTON_INDEX 13 // light magenta
  25. #define MOUSECTL_RBUTTON_INDEX 11 // light yellow
  26. #define MOUSECTL_LBUTTON_ALTERNATE 5 // magenta
  27. #define MOUSECTL_RBUTTON_ALTERNATE 3 // yellow
  28. #define MOUSECTL_NORMAL_COLOR RGB(255, 255, 255)
  29. #define HALFRGB(c) RGB(GetRValue(c) / 2, GetGValue(c) / 2, GetBValue(c) / 2)
  30. //
  31. // Typedef Declarations.
  32. //
  33. typedef struct tagMOUSECTLDATA
  34. {
  35. HWND window; // window handle for this control
  36. BOOL swapped; // are the buttons swapped?
  37. HBITMAP image; // DIB section image of mouse
  38. HDC imagedc; // DC where image lives
  39. HBITMAP olddcimage; // previous inhabitant of imagedc
  40. POINT offset; // offset of bitmap in control
  41. SIZE size; // size of bitmap
  42. } MOUSECTLDATA, *PMOUSECTLDATA;
  43. //
  44. // Forward Declarations.
  45. //
  46. LRESULT CALLBACK
  47. MouseControlWndProc(
  48. HWND window,
  49. UINT message,
  50. WPARAM wParam,
  51. LPARAM lParam);
  52. void
  53. MouseControlShowSwap(
  54. PMOUSECTLDATA ctldata);
  55. ////////////////////////////////////////////////////////////////////////////
  56. //
  57. // SetMouseControlData
  58. //
  59. ////////////////////////////////////////////////////////////////////////////
  60. __inline void SetMouseControlData(
  61. HWND window,
  62. PMOUSECTLDATA ctldata)
  63. {
  64. SetWindowLongPtr(window, MOUSECTL_DATA, (LONG_PTR)ctldata);
  65. }
  66. ////////////////////////////////////////////////////////////////////////////
  67. //
  68. // GetMouseControlData
  69. //
  70. ////////////////////////////////////////////////////////////////////////////
  71. __inline PMOUSECTLDATA GetMouseControlData(
  72. HWND window)
  73. {
  74. return ((PMOUSECTLDATA)GetWindowLongPtr(window, MOUSECTL_DATA));
  75. }
  76. ////////////////////////////////////////////////////////////////////////////
  77. //
  78. // RegisterMouseControlStuff
  79. //
  80. ////////////////////////////////////////////////////////////////////////////
  81. BOOL RegisterMouseControlStuff(
  82. HINSTANCE instance)
  83. {
  84. WNDCLASS wc;
  85. wc.style = 0;
  86. wc.lpfnWndProc = MouseControlWndProc;
  87. wc.cbClsExtra = 0;
  88. wc.cbWndExtra = sizeof(PMOUSECTLDATA);
  89. wc.hInstance = instance;
  90. wc.hIcon = NULL;
  91. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  92. wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  93. wc.lpszMenuName = NULL;
  94. wc.lpszClassName = MOUSECTL_CLASSNAME;
  95. return (RegisterClass(&wc));
  96. }
  97. ////////////////////////////////////////////////////////////////////////////
  98. //
  99. // CreateMouseControlData
  100. //
  101. ////////////////////////////////////////////////////////////////////////////
  102. PMOUSECTLDATA CreateMouseControlData(
  103. HWND window)
  104. {
  105. PMOUSECTLDATA ctldata = (PMOUSECTLDATA)LocalAlloc(LPTR, sizeof(MOUSECTLDATA));
  106. if (ctldata)
  107. {
  108. ctldata->window = window;
  109. SetMouseControlData(window, ctldata);
  110. }
  111. return (ctldata);
  112. }
  113. ////////////////////////////////////////////////////////////////////////////
  114. //
  115. // DestroyMouseControlData
  116. //
  117. ////////////////////////////////////////////////////////////////////////////
  118. void DestroyMouseControlData(
  119. PMOUSECTLDATA ctldata)
  120. {
  121. if (ctldata)
  122. {
  123. SetMouseControlData(ctldata->window, 0);
  124. LocalFree(ctldata);
  125. }
  126. }
  127. ////////////////////////////////////////////////////////////////////////////
  128. //
  129. // MouseControlNCCreate
  130. //
  131. ////////////////////////////////////////////////////////////////////////////
  132. BOOL MouseControlNCCreate(
  133. PMOUSECTLDATA ctldata,
  134. LPCREATESTRUCT cs)
  135. {
  136. if (!ctldata)
  137. {
  138. return (FALSE);
  139. }
  140. ctldata->image = LoadImage( cs->hInstance,
  141. MOUSECTL_BITMAP_NAME,
  142. IMAGE_BITMAP,
  143. 0,
  144. 0,
  145. LR_CREATEDIBSECTION );
  146. if (ctldata->image)
  147. {
  148. ctldata->imagedc = CreateCompatibleDC(NULL);
  149. if (ctldata->imagedc)
  150. {
  151. BITMAP bm;
  152. ctldata->olddcimage =
  153. SelectBitmap(ctldata->imagedc, ctldata->image);
  154. ctldata->offset.x = ctldata->offset.y = 0;
  155. if (GetObject(ctldata->image, sizeof(BITMAP), &bm))
  156. {
  157. ctldata->size.cx = bm.bmWidth;
  158. ctldata->size.cy = bm.bmHeight;
  159. ctldata->offset.x = (cs->cx - ctldata->size.cx) / 2;
  160. ctldata->offset.y = (cs->cy - ctldata->size.cy) / 2;
  161. ctldata->swapped = FALSE;
  162. MouseControlShowSwap(ctldata);
  163. return (TRUE);
  164. }
  165. SelectBitmap(ctldata->imagedc, ctldata->olddcimage);
  166. ctldata->olddcimage = NULL;
  167. }
  168. DeleteBitmap(ctldata->image);
  169. ctldata->image = NULL;
  170. }
  171. ctldata->olddcimage = NULL;
  172. ctldata->imagedc = NULL;
  173. return (FALSE);
  174. }
  175. ////////////////////////////////////////////////////////////////////////////
  176. //
  177. // MouseControlNCDestroy
  178. //
  179. ////////////////////////////////////////////////////////////////////////////
  180. void MouseControlNCDestroy(
  181. PMOUSECTLDATA ctldata)
  182. {
  183. if (ctldata)
  184. {
  185. if (ctldata->olddcimage)
  186. {
  187. SelectBitmap(ctldata->imagedc, ctldata->olddcimage);
  188. ctldata->olddcimage = NULL;
  189. }
  190. if (ctldata->imagedc)
  191. {
  192. DeleteDC(ctldata->imagedc);
  193. ctldata->imagedc = NULL;
  194. }
  195. if (ctldata->image)
  196. {
  197. DeleteBitmap(ctldata->image);
  198. ctldata->image = NULL;
  199. }
  200. DestroyMouseControlData(ctldata);
  201. }
  202. }
  203. ////////////////////////////////////////////////////////////////////////////
  204. //
  205. // MouseControlPaint
  206. //
  207. ////////////////////////////////////////////////////////////////////////////
  208. void MouseControlPaint(
  209. PMOUSECTLDATA ctldata)
  210. {
  211. PAINTSTRUCT ps;
  212. HDC dc = BeginPaint(ctldata->window, &ps);
  213. BitBlt( dc,
  214. ctldata->offset.x,
  215. ctldata->offset.y,
  216. ctldata->size.cx,
  217. ctldata->size.cy,
  218. ctldata->imagedc,
  219. 0,
  220. 0,
  221. SRCCOPY|NOMIRRORBITMAP);
  222. EndPaint(ctldata->window, &ps);
  223. }
  224. ////////////////////////////////////////////////////////////////////////////
  225. //
  226. // MouseControlSetColor
  227. //
  228. ////////////////////////////////////////////////////////////////////////////
  229. __inline void MouseControlSetColor(
  230. PMOUSECTLDATA ctldata,
  231. UINT index,
  232. COLORREF color)
  233. {
  234. RGBQUAD rgbq = { GetBValue(color), GetGValue(color), GetRValue(color), 0 };
  235. SetDIBColorTable(ctldata->imagedc, index, 1, &rgbq);
  236. }
  237. ////////////////////////////////////////////////////////////////////////////
  238. //
  239. // MouseControlShowSwap
  240. //
  241. ////////////////////////////////////////////////////////////////////////////
  242. void MouseControlShowSwap(
  243. PMOUSECTLDATA ctldata)
  244. {
  245. MouseControlSetColor( ctldata,
  246. MOUSECTL_BKGND_INDEX,
  247. GetSysColor(COLOR_BTNFACE) );
  248. MouseControlSetColor( ctldata,
  249. ctldata->swapped
  250. ? MOUSECTL_RBUTTON_INDEX
  251. : MOUSECTL_LBUTTON_INDEX,
  252. GetSysColor(COLOR_HIGHLIGHT) );
  253. MouseControlSetColor( ctldata,
  254. ctldata->swapped
  255. ? MOUSECTL_RBUTTON_ALTERNATE
  256. : MOUSECTL_LBUTTON_ALTERNATE,
  257. HALFRGB(GetSysColor(COLOR_HIGHLIGHT)) );
  258. MouseControlSetColor( ctldata,
  259. ctldata->swapped
  260. ? MOUSECTL_LBUTTON_INDEX
  261. : MOUSECTL_RBUTTON_INDEX,
  262. MOUSECTL_NORMAL_COLOR );
  263. MouseControlSetColor( ctldata,
  264. ctldata->swapped
  265. ? MOUSECTL_LBUTTON_ALTERNATE
  266. : MOUSECTL_RBUTTON_ALTERNATE,
  267. HALFRGB(MOUSECTL_NORMAL_COLOR) );
  268. InvalidateRect(ctldata->window, NULL, FALSE);
  269. }
  270. ////////////////////////////////////////////////////////////////////////////
  271. //
  272. // MouseControlSetSwap
  273. //
  274. ////////////////////////////////////////////////////////////////////////////
  275. void MouseControlSetSwap(
  276. HWND window,
  277. BOOL swap)
  278. {
  279. PMOUSECTLDATA ctldata = GetMouseControlData(window);
  280. if (ctldata->swapped != swap)
  281. {
  282. ctldata->swapped = swap;
  283. MouseControlShowSwap(ctldata);
  284. }
  285. }
  286. ////////////////////////////////////////////////////////////////////////////
  287. //
  288. // MouseControlWndProc
  289. //
  290. ////////////////////////////////////////////////////////////////////////////
  291. LRESULT CALLBACK MouseControlWndProc(
  292. HWND window,
  293. UINT message,
  294. WPARAM wParam,
  295. LPARAM lParam)
  296. {
  297. PMOUSECTLDATA ctldata = (message == WM_NCCREATE)
  298. ? CreateMouseControlData(window)
  299. : GetMouseControlData(window);
  300. switch (message)
  301. {
  302. HANDLE_MSG(ctldata, WM_NCCREATE, MouseControlNCCreate );
  303. HANDLE_MSG(ctldata, WM_NCDESTROY, MouseControlNCDestroy);
  304. HANDLE_MSG(ctldata, WM_PAINT, MouseControlPaint );
  305. }
  306. return (DefWindowProc(window, message, wParam, lParam));
  307. }