Counter Strike : Global Offensive Source Code
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.

266 lines
6.9 KiB

  1. /*------------------------------------------------------------------------------
  2. Rule2 - a simple WinTab program: test of spec. addenda
  3. RICO 10/17/91
  4. ------------------------------------------------------------------------------*/
  5. #include <string.h>
  6. #include <windows.h>
  7. #include "msgpack.h"
  8. #include <stdlib.h>
  9. #include <wintab.h>
  10. #ifdef USE_X_LIB
  11. #include <wintabx.h>
  12. #endif
  13. #define PACKETDATA (PK_X | PK_Y | PK_BUTTONS)
  14. #define PACKETMODE PK_BUTTONS
  15. #include <pktdef.h>
  16. #include "rule2.h"
  17. /* -------------------------------------------------------------------------- */
  18. #define Inch2Cm CASTFIX32(2.54)
  19. #define Cm2Inch CASTFIX32(1.0/2.54)
  20. /* -------------------------------------------------------------------------- */
  21. char _szAppName[] = "Rule2";
  22. HANDLE __hInstance = NULL; /* Our instance handle */
  23. LRESULT FAR PASCAL RuleAppWndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);
  24. BOOL NEAR PASCAL RegisterAppWndClass (HANDLE hInstance);
  25. /* -------------------------------------------------------------------------- */
  26. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  27. LPSTR lpszCmdLine, int nCmdShow)
  28. {
  29. MSG msg;
  30. HWND hWnd;
  31. __hInstance = hInstance;
  32. if (hPrevInstance == NULL)
  33. if (!RegisterAppWndClass(hInstance))
  34. return(0);
  35. hWnd = CreateDialog( hInstance, _szAppName, NULL, NULL);
  36. if (hWnd == NULL)
  37. return(0);
  38. ShowWindow(hWnd, nCmdShow);
  39. while (GetMessage(&msg, NULL, 0, 0)) {
  40. TranslateMessage(&msg);
  41. DispatchMessage(&msg);
  42. }
  43. #ifdef USE_X_LIB
  44. _UnlinkWintab();
  45. #endif
  46. return(0);
  47. }
  48. /* -------------------------------------------------------------------------- */
  49. BOOL NEAR PASCAL RegisterAppWndClass (HANDLE hInstance)
  50. {
  51. WNDCLASS WndClass;
  52. WndClass.style = 0;
  53. WndClass.lpfnWndProc = RuleAppWndProc;
  54. WndClass.cbClsExtra = 0;
  55. WndClass.cbWndExtra = DLGWINDOWEXTRA;
  56. WndClass.hInstance = hInstance;
  57. WndClass.hIcon = LoadIcon(hInstance, _szAppName);
  58. WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  59. WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  60. WndClass.lpszMenuName = NULL;
  61. WndClass.lpszClassName = _szAppName;
  62. return(RegisterClass(&WndClass));
  63. }
  64. /* -------------------------------------------------------------------------- */
  65. HCTX static NEAR TabletInit(HWND hWnd, FIX32 scale[])
  66. {
  67. LOGCONTEXT lcMine;
  68. /* get default region */
  69. WTInfo(WTI_DEFCONTEXT, 0, &lcMine);
  70. /* modify the digitizing region */
  71. strcpy(lcMine.lcName, "Rule2 Digitizing");
  72. lcMine.lcOptions |= CXO_MESSAGES;
  73. lcMine.lcPktData = PACKETDATA;
  74. lcMine.lcPktMode = PACKETMODE;
  75. lcMine.lcMoveMask = PACKETDATA;
  76. lcMine.lcBtnUpMask = lcMine.lcBtnDnMask;
  77. /* output in 1000ths of cm */
  78. lcMine.lcOutOrgX = lcMine.lcOutOrgY = 0;
  79. lcMine.lcOutExtX = INT(scale[0] * lcMine.lcInExtX);
  80. lcMine.lcOutExtY = INT(scale[1] * lcMine.lcInExtY);
  81. /* open the region */
  82. return WTOpen(hWnd, &lcMine, TRUE);
  83. }
  84. /* -------------------------------------------------------------------------- */
  85. /* return scaling factors in thousandths of cm per axis unit */
  86. static void TabletScaling(FIX32 scale[])
  87. {
  88. AXIS aXY[2];
  89. int i;
  90. UINT wDevice;
  91. /* get the data */
  92. WTInfo(WTI_DEFCONTEXT, CTX_DEVICE, &wDevice);
  93. WTInfo(WTI_DEVICES+wDevice, DVC_X, &aXY[0]);
  94. WTInfo(WTI_DEVICES+wDevice, DVC_Y, &aXY[1]);
  95. /* calculate the scaling factors */
  96. for (i = 0; i < 2; i++) {
  97. FIX_DIV(scale[i], CASTFIX32(1000), aXY[i].axResolution);
  98. if (aXY[i].axUnits == TU_INCHES) {
  99. FIX_MUL(scale[i], scale[i], Inch2Cm);
  100. }
  101. }
  102. }
  103. /* -------------------------------------------------------------------------- */
  104. DWORD nsqrt(DWORD x)
  105. {
  106. /* integer square root via Newton's method. */
  107. DWORD guess, oguess;
  108. if (x <= 1)
  109. return x;
  110. guess = 1;
  111. do
  112. {
  113. oguess = guess;
  114. guess = (guess + x/guess)/2;
  115. }
  116. while (labs(guess - oguess) > 1);
  117. if (guess == oguess)
  118. guess++;
  119. if (labs((guess * guess) - x) > labs((oguess * oguess) - x))
  120. guess = oguess;
  121. return guess;
  122. }
  123. /* -------------------------------------------------------------------------- */
  124. LRESULT FAR PASCAL RuleAppWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
  125. {
  126. static int inMode = ID_CLICK;
  127. static LONG x1 = 0, x2 = 0, y1 = 0, y2 = 0;
  128. static HCTX hTab = NULL;
  129. static FIX32 scale[2];
  130. PAINTSTRUCT psPaint;
  131. HDC hDC;
  132. PACKET pkt;
  133. switch (wMsg) {
  134. case WM_CREATE:
  135. TabletScaling(scale);
  136. hTab = TabletInit(hWnd, scale);
  137. WTEnable(hTab, FALSE);
  138. break;
  139. case WM_LBUTTONDOWN:
  140. case WM_KEYDOWN:
  141. if (inMode == ID_CLICK && hTab) {
  142. inMode = ID_PRESS;
  143. WTEnable(hTab, TRUE);
  144. WTOverlap(hTab, TRUE);
  145. InvalidateRect(hWnd, NULL, TRUE);
  146. }
  147. break;
  148. case WM_PAINT:
  149. hDC = BeginPaint(hWnd, &psPaint);
  150. ShowWindow(GetDlgItem(hWnd, ID_CLICK), inMode == ID_CLICK);
  151. ShowWindow(GetDlgItem(hWnd, ID_PRESS), inMode == ID_PRESS);
  152. ShowWindow(GetDlgItem(hWnd, ID_RELEASE), inMode == ID_RELEASE);
  153. if (inMode == ID_CLICK) {
  154. LONG delta[3]; /* horz/vert/diag */
  155. int i;
  156. delta[0] = labs(x2 - x1);
  157. delta[1] = labs(y2 - y1);
  158. delta[2] = nsqrt(delta[0] * delta[0] + delta[1] * delta[1]);
  159. for (i = 0; i < 3; i++) { /* direction */
  160. char buf[20];
  161. /* print result in cm */
  162. wsprintf(buf, "%d.%3.3d", (UINT)delta[i]/1000,
  163. (UINT)delta[i]%1000);
  164. SetWindowText(GetDlgItem(hWnd, ID_HC + i), buf);
  165. /* convert to inches */
  166. delta[i] = INT(delta[i] * Cm2Inch);
  167. /* print result in inches */
  168. wsprintf(buf, "%d.%3.3d", (UINT)delta[i]/1000,
  169. (UINT)delta[i]%1000);
  170. SetWindowText(GetDlgItem(hWnd, ID_HI + i), buf);
  171. }
  172. }
  173. EndPaint(hWnd, &psPaint);
  174. break;
  175. case WM_DESTROY:
  176. if (hTab)
  177. WTClose(hTab);
  178. PostQuitMessage(0);
  179. break;
  180. case WM_ACTIVATE:
  181. /* if switching in the middle, disable the region */
  182. if (hTab && inMode != ID_CLICK) {
  183. WTEnable(hTab, GET_WM_ACTIVATE_STATE(wParam, lParam));
  184. if (hTab && GET_WM_ACTIVATE_STATE(wParam, lParam))
  185. WTOverlap(hTab, TRUE);
  186. }
  187. break;
  188. case WT_PACKET:
  189. if (WTPacket((HCTX)lParam, wParam, &pkt)) {
  190. static DWORD bmap;
  191. /* handle it */
  192. if (HIWORD(pkt.pkButtons)) {
  193. DWORD bit = 1 << LOWORD(pkt.pkButtons);
  194. if (HIWORD(pkt.pkButtons) == TBN_DOWN) {
  195. bmap |= bit;
  196. }
  197. else {
  198. bmap &= ~bit;
  199. }
  200. }
  201. if (inMode == ID_PRESS && HIWORD(pkt.pkButtons) == TBN_DOWN) {
  202. x1 = pkt.pkX;
  203. y1 = pkt.pkY;
  204. inMode = ID_RELEASE;
  205. InvalidateRect(hWnd, NULL, TRUE);
  206. }
  207. if (inMode == ID_RELEASE && bmap == 0) {
  208. WTEnable(hTab, FALSE);
  209. x2 = pkt.pkX;
  210. y2 = pkt.pkY;
  211. inMode = ID_CLICK;
  212. InvalidateRect(hWnd, NULL, TRUE);
  213. }
  214. }
  215. break;
  216. default:
  217. return DefWindowProc(hWnd, wMsg, wParam, lParam);
  218. }
  219. return (LRESULT)0;
  220. }
  221. /* -------------------------------------------------------------------------- */