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.

438 lines
10 KiB

  1. /*------------------------------------------------------------------------------
  2. CadTest - example of how a cad program might use WinTab.
  3. RICO 4/1/92
  4. ------------------------------------------------------------------------------*/
  5. #include <string.h>
  6. #include <windows.h>
  7. #include "msgpack.h"
  8. #include <commdlg.h>
  9. #include <wintab.h>
  10. #define PACKETDATA (PK_X | PK_Y | PK_BUTTONS)
  11. #define PACKETMODE PK_BUTTONS
  12. #include <pktdef.h>
  13. #include "cadtest.h"
  14. #ifdef USE_X_LIB
  15. #include <wintabx.h>
  16. #endif
  17. HANDLE hInst;
  18. #ifdef WIN32
  19. #define GetID() GetCurrentProcessId()
  20. #else
  21. #define GetID() hInst
  22. #endif
  23. /* -------------------------------------------------------------------------- */
  24. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  25. HANDLE hInstance;
  26. HANDLE hPrevInstance;
  27. LPSTR lpCmdLine;
  28. int nCmdShow;
  29. {
  30. MSG msg;
  31. if (!hPrevInstance)
  32. if (!InitApplication(hInstance))
  33. return (FALSE);
  34. /* Perform initializations that apply to a specific instance */
  35. if (!InitInstance(hInstance, nCmdShow))
  36. return (FALSE);
  37. /* Acquire and dispatch messages until a WM_QUIT message is received. */
  38. while (GetMessage(&msg,
  39. NULL,
  40. 0,
  41. 0))
  42. {
  43. TranslateMessage(&msg);
  44. DispatchMessage(&msg);
  45. }
  46. #ifdef USE_X_LIB
  47. _UnlinkWintab();
  48. #endif
  49. return (msg.wParam);
  50. }
  51. /* -------------------------------------------------------------------------- */
  52. BOOL InitApplication(hInstance)
  53. HANDLE hInstance;
  54. {
  55. WNDCLASS wc;
  56. /* Fill in window class structure with parameters that describe the */
  57. /* main window. */
  58. wc.style = 0;
  59. wc.lpfnWndProc = MainWndProc;
  60. wc.cbClsExtra = 0;
  61. wc.cbWndExtra = 0;
  62. wc.hInstance = hInstance;
  63. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  64. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  65. wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
  66. wc.lpszMenuName = "CadTestMenu";
  67. wc.lpszClassName = "CadTestWClass";
  68. /* Register the window class and return success/failure code. */
  69. return (RegisterClass(&wc));
  70. }
  71. /* -------------------------------------------------------------------------- */
  72. BOOL InitInstance(hInstance, nCmdShow)
  73. HANDLE hInstance;
  74. int nCmdShow;
  75. {
  76. HWND hWnd;
  77. char buf[50];
  78. /* Save the instance handle in static variable, which will be used in */
  79. /* many subsequence calls from this application to Windows. */
  80. hInst = hInstance;
  81. /* check if WinTab available. */
  82. if (!WTInfo(0, 0, NULL)) {
  83. MessageBox(NULL, "WinTab Services Not Available.", "WinTab",
  84. MB_OK | MB_ICONHAND);
  85. return FALSE;
  86. }
  87. /* Create a main window for this application instance. */
  88. wsprintf(buf, "CadTest:%x", GetID());
  89. hWnd = CreateWindow(
  90. "CadTestWClass",
  91. buf,
  92. WS_OVERLAPPEDWINDOW,
  93. CW_USEDEFAULT,
  94. CW_USEDEFAULT,
  95. CW_USEDEFAULT,
  96. CW_USEDEFAULT,
  97. NULL,
  98. NULL,
  99. hInstance,
  100. NULL
  101. );
  102. /* If window could not be created, return "failure" */
  103. if (!hWnd)
  104. return (FALSE);
  105. /* Make the window visible; update its client area; and return "success" */
  106. ShowWindow(hWnd, nCmdShow);
  107. UpdateWindow(hWnd);
  108. return (TRUE);
  109. }
  110. /* -------------------------------------------------------------------------- */
  111. /* open and save file data. */
  112. char szFile[256] = "cadtest.ctx";
  113. /* -------------------------------------------------------------------------- */
  114. /* enforces context rules even if we didn't save the context. */
  115. void static NEAR TabletSetup(PLOGCONTEXT pLc)
  116. {
  117. /* modify the digitizing region */
  118. wsprintf(pLc->lcName, "CadTest Digitizing %x", GetID());
  119. pLc->lcOptions |= CXO_MESSAGES;
  120. pLc->lcMsgBase = WT_DEFBASE;
  121. pLc->lcPktData = PACKETDATA;
  122. pLc->lcPktMode = PACKETMODE;
  123. pLc->lcMoveMask = PACKETDATA;
  124. pLc->lcBtnUpMask = pLc->lcBtnDnMask;
  125. /* output in 10000 x 10000 grid */
  126. pLc->lcOutOrgX = pLc->lcOutOrgY = 0;
  127. pLc->lcOutExtX = 10000;
  128. pLc->lcOutExtY = 10000;
  129. }
  130. /* -------------------------------------------------------------------------- */
  131. HCTX static TabletRestore(HCTX hCtx, HWND hWnd)
  132. {
  133. void *save_buf;
  134. UINT save_size;
  135. HFILE hFile;
  136. OFSTRUCT ofs;
  137. LOGCONTEXT lc;
  138. /* alloc a save buffer. */
  139. WTInfo(WTI_INTERFACE, IFC_CTXSAVESIZE, &save_size);
  140. if (save_buf = (void *)LocalAlloc(LPTR, save_size))
  141. {
  142. OPENFILENAME ofn;
  143. /* do open file box. */
  144. memset(&ofn, 0, sizeof(ofn));
  145. ofn.lStructSize = sizeof(ofn);
  146. ofn.hwndOwner = hWnd;
  147. ofn.lpstrFilter = "Context Files\0*.ctx\0";
  148. ofn.lpstrFile = szFile;
  149. ofn.nMaxFile = sizeof(szFile);
  150. ofn.Flags = OFN_FILEMUSTEXIST |OFN_PATHMUSTEXIST;
  151. ofn.lpstrDefExt = "ctx";
  152. if (GetOpenFileName(&ofn)) {
  153. /* open the file. */
  154. if ((hFile = OpenFile(szFile, &ofs, OF_READ)) != HFILE_ERROR)
  155. {
  156. /* read in the data. */
  157. _lread(hFile, save_buf, save_size);
  158. /* close the file. */
  159. _lclose(hFile);
  160. }
  161. /* restore the context, disabled. */
  162. if (hCtx)
  163. WTClose(hCtx);
  164. hCtx = WTRestore(hWnd, save_buf, FALSE);
  165. /* re-init the context. */
  166. if (hCtx) {
  167. WTGet(hCtx, &lc);
  168. TabletSetup(&lc);
  169. WTSet(hCtx, &lc);
  170. /* open for real. */
  171. WTEnable(hCtx, TRUE);
  172. }
  173. }
  174. /* bag the save buffer. */
  175. LocalFree((HLOCAL)save_buf);
  176. }
  177. return hCtx;
  178. }
  179. /* -------------------------------------------------------------------------- */
  180. void TabletSave(HCTX hCtx, HWND hWnd, BOOL fAs)
  181. {
  182. void *save_buf;
  183. UINT save_size;
  184. HFILE hFile;
  185. OFSTRUCT ofs;
  186. /* alloc a save buffer. */
  187. WTInfo(WTI_INTERFACE, IFC_CTXSAVESIZE, &save_size);
  188. if (save_buf = (void *)LocalAlloc(LPTR, save_size))
  189. {
  190. /* save the data. */
  191. if (WTSave(hCtx, save_buf)) {
  192. /* if setting file name... */
  193. if (fAs) {
  194. OPENFILENAME ofn;
  195. /* do save file box. */
  196. memset(&ofn, 0, sizeof(ofn));
  197. ofn.lStructSize = sizeof(ofn);
  198. ofn.hwndOwner = hWnd;
  199. ofn.lpstrFilter = "Context Files\0*.ctx\0";
  200. ofn.lpstrFile = szFile;
  201. ofn.nMaxFile = sizeof(szFile);
  202. ofn.Flags = OFN_PATHMUSTEXIST;
  203. ofn.lpstrDefExt = "ctx";
  204. fAs = GetSaveFileName(&ofn);
  205. }
  206. else
  207. /* assume file name good. */
  208. fAs = TRUE;
  209. /* if good file name... */
  210. if (fAs) {
  211. /* open the file. */
  212. if ((hFile = OpenFile(szFile, &ofs, OF_WRITE | OF_CREATE)) !=
  213. HFILE_ERROR)
  214. {
  215. /* read in the data. */
  216. _lwrite(hFile, save_buf, save_size);
  217. /* close the file. */
  218. _lclose(hFile);
  219. }
  220. }
  221. }
  222. /* bag the save buffer. */
  223. LocalFree((HLOCAL)save_buf);
  224. }
  225. }
  226. /* -------------------------------------------------------------------------- */
  227. HCTX static NEAR TabletInit(HWND hWnd)
  228. {
  229. LOGCONTEXT lcMine;
  230. /* get default region */
  231. WTInfo(WTI_DEFCONTEXT, 0, &lcMine);
  232. /* init the data structure */
  233. TabletSetup(&lcMine);
  234. /* open the region */
  235. return WTOpen(hWnd, &lcMine, TRUE);
  236. }
  237. /* -------------------------------------------------------------------------- */
  238. LRESULT FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  239. HWND hWnd;
  240. unsigned message;
  241. WPARAM wParam;
  242. LPARAM lParam;
  243. {
  244. FARPROC lpProcAbout;
  245. static HCTX hTab = NULL;
  246. static POINT ptOld, ptNew;
  247. static RECT rcClient;
  248. PAINTSTRUCT psPaint;
  249. HDC hDC;
  250. PACKET pkt;
  251. BOOL fHandled = TRUE;
  252. LRESULT lResult = 0L;
  253. static int count;
  254. static BOOL fPersist;
  255. switch (message) {
  256. case WM_CREATE:
  257. hTab = TabletInit(hWnd);
  258. if (!hTab) {
  259. MessageBox(NULL, " Could Not Open Tablet Context.", "WinTab",
  260. MB_OK | MB_ICONHAND);
  261. SendMessage(hWnd, WM_DESTROY, 0, 0L);
  262. }
  263. break;
  264. case WM_SIZE:
  265. GetClientRect(hWnd, &rcClient);
  266. InvalidateRect(hWnd, NULL, TRUE);
  267. break;
  268. case WM_COMMAND:
  269. switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  270. case IDM_ABOUT:
  271. lpProcAbout = MakeProcInstance(About, hInst);
  272. DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  273. FreeProcInstance(lpProcAbout);
  274. break;
  275. case IDM_OPEN:
  276. hTab = TabletRestore(hTab, hWnd);
  277. break;
  278. case IDM_SAVE:
  279. TabletSave(hTab, hWnd, FALSE);
  280. break;
  281. case IDM_SAVE_AS:
  282. TabletSave(hTab, hWnd, TRUE);
  283. break;
  284. case IDM_CONFIG:
  285. WTConfig(hTab, hWnd);
  286. break;
  287. case IDM_PERSIST:
  288. fPersist = !fPersist;
  289. CheckMenuItem(GetSubMenu(GetMenu(hWnd), IDM_EDIT),
  290. IDM_PERSIST, (fPersist ? MF_CHECKED : MF_UNCHECKED));
  291. break;
  292. default:
  293. fHandled = FALSE;
  294. break;
  295. }
  296. break;
  297. case WT_PACKET:
  298. if (WTPacket((HCTX)lParam, wParam, &pkt)) {
  299. if (HIWORD(pkt.pkButtons)==TBN_DOWN) {
  300. MessageBeep(0);
  301. }
  302. ptOld = ptNew;
  303. ptNew.x = MulDiv((UINT)pkt.pkX, rcClient.right, 10000);
  304. ptNew.y = MulDiv((UINT)pkt.pkY, rcClient.bottom, 10000);
  305. if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
  306. InvalidateRect(hWnd, NULL, TRUE);
  307. if (count++ == 4) {
  308. count = 0;
  309. UpdateWindow(hWnd);
  310. }
  311. }
  312. }
  313. break;
  314. case WM_ACTIVATE:
  315. if (GET_WM_ACTIVATE_STATE(wParam, lParam))
  316. InvalidateRect(hWnd, NULL, TRUE);
  317. /* if switching in the middle, disable the region */
  318. if (hTab) {
  319. if (!fPersist)
  320. WTEnable(hTab, GET_WM_ACTIVATE_STATE(wParam, lParam));
  321. if (hTab && GET_WM_ACTIVATE_STATE(wParam, lParam))
  322. WTOverlap(hTab, TRUE);
  323. }
  324. break;
  325. case WM_DESTROY:
  326. if (hTab)
  327. WTClose(hTab);
  328. PostQuitMessage(0);
  329. break;
  330. case WM_PAINT:
  331. count = 0;
  332. hDC = BeginPaint(hWnd, &psPaint);
  333. /* redo horz */
  334. PatBlt(hDC, rcClient.left, rcClient.bottom - ptNew.y,
  335. rcClient.right, 1, BLACKNESS);
  336. /* redo vert */
  337. PatBlt(hDC, ptNew.x, rcClient.top,
  338. 1, rcClient.bottom, BLACKNESS);
  339. EndPaint(hWnd, &psPaint);
  340. break;
  341. default:
  342. fHandled = FALSE;
  343. break;
  344. }
  345. if (fHandled)
  346. return (lResult);
  347. else
  348. return (DefWindowProc(hWnd, message, wParam, lParam));
  349. }
  350. /* -------------------------------------------------------------------------- */
  351. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  352. HWND hDlg;
  353. unsigned message;
  354. WPARAM wParam;
  355. LPARAM lParam;
  356. {
  357. switch (message) {
  358. case WM_INITDIALOG:
  359. return (TRUE);
  360. case WM_COMMAND:
  361. if (GET_WM_COMMAND_ID(wParam, lParam) == IDOK
  362. || GET_WM_COMMAND_ID(wParam, lParam) == IDCANCEL) {
  363. EndDialog(hDlg, TRUE);
  364. return (TRUE);
  365. }
  366. break;
  367. }
  368. return (FALSE);
  369. }
  370. /* -------------------------------------------------------------------------- */
  371.