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.

244 lines
6.2 KiB

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