home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / win100b.zip / wkt100.c < prev    next >
C/C++ Source or Header  |  1991-10-20  |  9KB  |  383 lines

  1. /* 
  2.  * Resident segment for the Windows Win100/Win600 emulator
  3.  *
  4.  * Copyright (c) 1989, 1990, 1991 by
  5.  * William S. Hall
  6.  * 3665 Benton Street, #66
  7.  * Santa Clara, CA 95051
  8.  *
  9.  * $Date$
  10.  * $Revision$
  11.  * $RCSfile$
  12.  * $Source$
  13.  * $State$
  14.  */
  15.  
  16. #define NOKANJI
  17. #define NOSOUND
  18. #define NOMINMAX
  19. #include <windows.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <process.h>
  23. #include <io.h>
  24. #include <ascii.h>
  25. #ifdef COLUMBIA
  26. #include "wktsmt.h"
  27. #else
  28. #include "smterm.h"
  29. #endif
  30.  
  31. #ifdef WIN600
  32. #include "grterm.h"
  33. #endif
  34.  
  35. #define EXTERN
  36. #define INITIALIZE
  37. #ifdef COLUMBIA
  38. #include "wkt100.h"
  39. #else
  40. #include "win600.h"
  41. #endif
  42.  
  43. #if defined(KERMIT)
  44. #define KERMITEXTERN
  45. #ifdef COLUMBIA
  46. #include "wkkerm.h"
  47. #else
  48. #include "wnkerm.h"
  49. #endif
  50. #endif
  51.  
  52. static int NEAR ProcessComm(void);
  53.  
  54. /* Entry point for program */
  55. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, 
  56.            LPSTR lpszCmdLine, int cmdShow)
  57. {
  58.  
  59.   /* If initialization is not successful then exit */
  60.     if (!InitProgram(hInstance,hPrevInstance, lpszCmdLine, cmdShow))
  61.     return FALSE;
  62.  
  63.   /* Retrieve messages from Windows */
  64.  
  65.     while (TRUE)
  66.     (*DoMessage)();
  67.  
  68. }
  69.  
  70. void NEAR DoGetMessage()
  71. {
  72.     MSG msg;
  73.  
  74.     if (GetMessage((LPMSG)&msg,NULL,0,0)) {
  75.     if ((!TranslateAccelerator(MWnd.hWnd,hAccel,(LPMSG)&msg))
  76.         && (!IsWindow(Kermit.hWndXfer) || 
  77.         !IsDialogMessage(Kermit.hWndXfer, &msg))) {
  78.         TranslateMessage((LPMSG)&msg);
  79.         DispatchMessage((LPMSG)&msg);
  80.     }
  81.     }
  82.     else
  83.     exit((int)msg.wParam);
  84. }
  85.  
  86. void NEAR DoPeekMessage()
  87. {
  88.     MSG msg;
  89.  
  90.     if (PeekMessage((LPMSG)&msg,NULL,0,0,PM_REMOVE)) {
  91.         if (msg.message == WM_QUIT)
  92.         exit((int)msg.wParam);
  93. #if defined(KERMIT)
  94.     if (IsWindow(Kermit.hWndXfer) && IsDialogMessage(Kermit.hWndXfer,&msg))
  95.         ;
  96.     else 
  97. #endif
  98.     if (TranslateAccelerator(MWnd.hWnd,hAccel,(LPMSG)&msg))
  99.         ;
  100.     else {
  101.             TranslateMessage((LPMSG)&msg);
  102.             DispatchMessage((LPMSG)&msg);
  103.     }
  104.     }
  105.     else if (!ScrollLock) {
  106.     ProcessComm();
  107.     PostMessage(MWnd.hWnd, WM_USER, 0, 0L);
  108.     }
  109. }
  110.  
  111. /* All messages are processed here */
  112. long FAR PASCAL MainWndProc(HWND hWnd,unsigned message,WORD wParam,LONG lParam)
  113. {
  114.  
  115.     PAINTSTRUCT ps;
  116.     FARPROC fp;
  117.     short result;
  118.     int minpos, maxpos;
  119.     
  120.     switch(message) {
  121.  
  122.     case WM_USER:
  123. #if defined(KERMIT)
  124.         if (Kermit.InTransfer) {
  125.         int startlen = ComBuf.len;
  126.         wart();
  127.         ComBuf.ptr += (startlen - ComBuf.len);
  128.             break;
  129.         }
  130. #endif
  131.         if (ComBuf.len > 0) {
  132.             pBuf = &ComBuf;
  133.         while ((*curproto.pf)())
  134.             ;
  135.             if (hLogFile > 0)
  136.                 result = write(hLogFile, Buffer, ComBuf.ptr - Buffer);
  137.             }
  138.         else if (GB.hBuf)
  139.             WriteClipData();
  140.         break;
  141.  
  142.        case WM_CHANGECBCHAIN:
  143.         if (wParam == hWndNext)
  144.         hWndNext = LOWORD(lParam);
  145.         SendMessage(hWndNext, message, wParam, lParam);
  146.         break;
  147.  
  148.     case WM_DRAWCLIPBOARD:
  149.         if (IsWindow(hWndNext))
  150.         SendMessage(hWndNext, message, wParam, lParam);
  151.         if (OpenClipboard(hWnd)) {
  152.         HMENU hMenu = GetMenu(hWnd);
  153.         if (GetClipboardData(CF_TEXT) != NULL)
  154.             EnableMenuItem(hMenu, IDM_PASTE, MF_ENABLED);
  155.         else
  156.             EnableMenuItem(hMenu, IDM_PASTE, MF_GRAYED);
  157.         CloseClipboard();
  158.         }
  159.         break;
  160.  
  161.     case WM_TIMER:
  162.         WndTimer(hWnd, wParam);
  163.         break;
  164.  
  165.     case WM_CREATE:
  166.         MainWndCreate(hWnd, lParam);
  167. #if defined(KERMIT)
  168.         if (!krmInit(hWnd, Buffer, &ComBuf.len, &cid,
  169.              GetMenuItemCount(GetMenu(hWnd)) - 1))
  170.         PostMessage(MWnd.hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
  171. #endif
  172.         break;
  173.  
  174.     case WM_KEYDOWN:
  175.         if (wParam == VK_SCROLL)
  176.         ScrollLock = (ScrollLock == TRUE) ? FALSE : TRUE;
  177.         break;
  178.  
  179.     case WM_ERASEBKGND:
  180.         SelectObject((HDC)wParam, MWnd.hBrush);
  181.         FillRect((HDC)wParam, (LPRECT)&MWnd.rect, MWnd.hBrush);
  182.         return (LONG)TRUE;
  183.  
  184.     case WM_COMMAND:
  185. #if defined(KERMIT)
  186.         if (krmWndCommand(hWnd, wParam))
  187.         break;
  188. #endif
  189.         WndCommand(hWnd, wParam, lParam);
  190.         break;
  191.  
  192.     case WM_CHAR:
  193. #ifdef WIN600
  194.         if ((curproto.type == 'G') && (curproto.gtype == XC)) {
  195.         SetGraphicsParams(US);
  196.         AnswerEnq((BYTE)wParam, XC);
  197.         }
  198.         else
  199. #endif
  200.             ProcessChars((BYTE)wParam, (int)LOWORD(lParam));
  201.         break;
  202.         
  203.     case WM_DESTROY:
  204.         PostQuitMessage(0);
  205.         break;
  206.  
  207.     case WM_CLOSE:
  208. #if defined(KERMIT)
  209.         if (krmShutdown() != IDYES)
  210.             break;
  211. #endif
  212.         if (MWnd.hBrush)
  213.             DeleteObject(MWnd.hBrush);
  214.         CloseCommPort(hWnd, &cid);
  215.         if (IsWindow(hWndNext))
  216.             ChangeClipboardChain(hWnd, hWndNext);
  217.         if (hLogFile > 0)
  218.         CloseLogFile();
  219.         if (GB.hBuf)
  220.         LocalFree(GB.hBuf);
  221.         DestroyWindow(hWnd);
  222.         break;
  223.  
  224.     case WM_QUERYENDSESSION:
  225. #if defined(KERMIT)
  226.         if (krmShutdown() != IDYES)
  227.             break;
  228. #endif
  229.         return ((LONG)TRUE);
  230.  
  231.     case WM_ENDSESSION:
  232.         if (wParam) {
  233.             CloseCommPort(hWnd, &cid);
  234.         if (hLogFile > 0)
  235.             CloseLogFile();
  236.         }
  237.         break;
  238.  
  239.     case WM_PAINT:
  240.         BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  241. #if defined(KERMIT)
  242.         if (IsIconic(hWnd)) {
  243.         if (Kermit.InTransfer) {
  244.             SetTextColor(ps.hdc, ColorTable[MWnd.TextColor]);
  245.             SetBkColor(ps.hdc, ColorTable[MWnd.BGColor]);
  246.             krmPaint(hWnd, ps.hdc);
  247.         }
  248.         else {
  249.             RECT rect;
  250.             GetClientRect(hWnd, &rect);
  251.             DrawIcon(ps.hdc, 
  252.                  (rect.right - GetSystemMetrics(SM_CXICON)) / 2,
  253.                  (rect.bottom - GetSystemMetrics(SM_CYICON)) / 2,
  254.                  krmIcon);
  255.         }
  256.         }
  257.         else
  258. #endif        
  259.         MainWndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  260.         EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  261.         break;
  262.  
  263. #if defined(KERMIT)
  264.     case WM_QUERYDRAGICON:
  265.         return MAKELONG(krmIcon, 0);
  266. #endif
  267.  
  268.         case WM_SIZE:
  269.         if (wParam == SIZEICONIC) {
  270. //        DoMessage = DoGetMessage;
  271.         break;
  272.         }
  273. //        DoMessage = DoPeekMessage;
  274.         MWnd.rect.right = LOWORD(lParam);    /* save these values */
  275.         MWnd.rect.bottom = HIWORD(lParam);
  276.  
  277.         fp = MakeProcInstance((FARPROC)MainHasResized, hInst);
  278.         EnumChildWindows(hWnd, fp, lParam);
  279.         FreeProcInstance(fp);
  280.  
  281.         if (hWnd == GetFocus())
  282.         MakeCaret(TRUE);
  283.  
  284.         if (IsWindow(hWndActive)) {
  285.         if (curproto.type == 'A') {
  286.                 LONG range = SendMessage(hWndActive, SMT_SIZEPARENT, 
  287.                         (WORD)hWndActive, lParam);
  288.             SetScrollPos(MWnd.hWnd, SB_HORZ, 0, FALSE);
  289.             SetScrollRange(MWnd.hWnd, SB_HORZ, 0, LOWORD(range), TRUE);
  290.         }
  291. #ifdef WIN600
  292.         else
  293.             SetScrollRange(MWnd.hWnd, SB_HORZ, 0, 0, TRUE);
  294. #endif
  295.         }
  296.         break;
  297.  
  298.     case WM_HSCROLL:
  299.          switch (wParam) {
  300.         case SB_LINEUP:
  301.             GetScrollRange(hWnd, SB_HORZ, &minpos, &maxpos);
  302.             if ((result = GetScrollPos(hWnd, SB_HORZ)) > minpos) {
  303.                 SetScrollPos(hWnd, SB_HORZ, result - 1, TRUE);
  304.                 SendMessage(hWndActive, SMT_COMMAND, SM_HSCROLL,
  305.                         MAKELONG(result - 1, 0));
  306.             }
  307.             break;
  308.         case SB_LINEDOWN:
  309.             GetScrollRange(hWnd, SB_HORZ, &minpos, &maxpos);
  310.             if ((result = GetScrollPos(hWnd, SB_HORZ)) < maxpos) {
  311.                 SetScrollPos(hWnd, SB_HORZ, result + 1, TRUE);
  312.                 SendMessage(hWndActive, SMT_COMMAND, SM_HSCROLL,
  313.                         MAKELONG(result + 1, 0));
  314.             }
  315.             break;
  316.         case SB_THUMBPOSITION:
  317.             result = LOWORD(lParam);
  318.             SetScrollPos(hWnd, SB_HORZ, result, TRUE);
  319.             SendMessage(hWndActive, SMT_COMMAND, SM_HSCROLL,
  320.                             MAKELONG(result, 0));
  321.             break;
  322.         }
  323.         break;
  324.  
  325.     /* window has focus, so tty window can create a caret */
  326.     case WM_SETFOCUS:
  327.         MakeCaret(TRUE);
  328.         break;
  329.  
  330.     /* window has lost focus, so it can tell tty to kill the caret */
  331.     case WM_KILLFOCUS:
  332.         MakeCaret(FALSE);
  333.         break;
  334.  
  335.     default:
  336.         return ((long)DefWindowProc(hWnd,message,wParam,lParam));
  337.         break;
  338.     }
  339.     return(0L);
  340. }
  341.  
  342. BOOL FAR PASCAL MainHasResized(HWND hWnd, LONG lParam)
  343. {
  344.     LONG range;
  345.  
  346.     if (IsWindow(hWnd))
  347.         range = SendMessage(hWnd, SMT_SIZEPARENT, (WORD)hWndActive, lParam);
  348.     return TRUE;
  349. }
  350.  
  351.  
  352. BOOL FAR PASCAL ColorChange(HWND hWnd, LONG lParam)
  353. {
  354.     if (IsWindow(hWnd)) {
  355.         SendMessage(hWnd, SMT_SETATTRIBUTE, SM_COLORCHANGE, lParam);
  356.     return TRUE;
  357.     }
  358.     return TRUE;
  359. }
  360.  
  361. /* come here to read the communications buffer if no messages to process */
  362. static int NEAR ProcessComm(void)
  363. {
  364.  
  365.     COMSTAT ComStatus;
  366.     register int result;
  367.     register WORD room = BUFSIZE - ComBuf.len;
  368.  
  369.     if ((ComBuf.len > 0) && (ComBuf.ptr > Buffer))
  370.     memmove(Buffer, ComBuf.ptr, ComBuf.len);
  371.     ComBuf.ptr = Buffer;
  372.     if (room) {
  373.         if (GetCommError(cid, (COMSTAT FAR *)&ComStatus) == 0) {
  374.             if (ComStatus.cbInQue) {
  375.             result = ReadComm(cid, Buffer + ComBuf.len, 
  376.                   min(ComStatus.cbInQue, room));
  377.             ComBuf.len += abs(result);
  378.         }
  379.     }
  380.     }
  381.     return (ComBuf.len);
  382. }
  383.