home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / pclw11 / simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-25  |  7.7 KB  |  315 lines

  1. /*
  2. **                    --- simple.c ---
  3. **
  4. **  EXAMPLE CODE: Very simple terminal emulator.
  5. **
  6. **  This example program (not the PCL4W library) is donated to
  7. **  the Public Domain by MarshallSoft Computing, Inc. It is
  8. **  provided as an example of the use of the PCL4W.
  9. **
  10. */
  11.  
  12. #include "windows.h"
  13. #include "simple.h"
  14. #include "pcl4w.h"
  15. #include "sioerror.h"
  16. #include "ascii.h"
  17. #include "simpl_io.h"
  18. #include "expect.h"
  19. #include "config.h"
  20. #include "paint.h"
  21. #include "line.h"
  22. #include "about.h"
  23.  
  24. /* public globals */
  25. HWND hMainWnd;            /* main window handle */
  26. HWND hInfoWnd;            /* popup handle */
  27. HANDLE hInstance;         /* program instance */
  28. int OnLineFlag = FALSE;   /* TRUE: online */
  29. int FatalFlag = FALSE;    /* TRUE: fatal error */
  30.  
  31. /* private globals */
  32. static int WinWidth = 8 * NCOLS;
  33. static int WinHeight = 12 * NROWS + 48;
  34.  
  35. /* miscellaneous functions */
  36. void ErrorCheck(int);
  37.  
  38. /*
  39. ** PostMainHandle() is required only for the
  40. ** Shareware version of PCL4W.
  41. */
  42.  
  43. #if __cplusplus
  44. extern "C" void FAR PASCAL PostMainHandle(HWND);
  45. #else
  46. extern void FAR PASCAL PostMainHandle(HWND);
  47. #endif
  48.  
  49. int PASCAL WinMain(HANDLE hInst,HANDLE hPrevInstance,
  50.                    LPSTR lpCmdLine,int nCmdShow)
  51. {WNDCLASS  wc;
  52.  MSG msg;
  53.  BOOL Result;
  54.  if(!hPrevInstance)
  55.    {/* register main window class */
  56.     wc.style = CS_HREDRAW | CS_VREDRAW;
  57.     wc.lpfnWndProc = MainWndProc;
  58.     wc.cbClsExtra = 0;
  59.     wc.cbWndExtra = 0;
  60.     wc.hInstance = hInst;
  61.     wc.hIcon = LoadIcon(hInst, "SimpleIcon");
  62.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  63.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  64.     wc.lpszMenuName =  "SimpleMenu";
  65.     wc.lpszClassName = "SimpleWClass";
  66.     Result = RegisterClass(&wc);
  67.     if(!Result) return FALSE;
  68.    }
  69.  
  70.  /* create main window */
  71.  hInstance = hInst;
  72.  hMainWnd = CreateWindow(
  73.         "SimpleWClass",   "Simple",       WS_OVERLAPPEDWINDOW,
  74.         CW_USEDEFAULT,    CW_USEDEFAULT,
  75.         WinWidth,         WinHeight,
  76.         NULL,             NULL,
  77.         hInstance,        NULL);
  78.  ShowWindow(hMainWnd, nCmdShow);
  79.  UpdateWindow(hMainWnd);
  80.  
  81.  /* window control loop */
  82.  
  83.  while(GetMessage(&msg,NULL,NULL,NULL))
  84.    {
  85.     TranslateMessage(&msg);
  86.     DispatchMessage(&msg);
  87.    }
  88.  return (msg.wParam);
  89. } /* end WinMain */
  90.  
  91. long FAR PASCAL MainWndProc(HWND hWindow,UINT message,WPARAM wParam,LPARAM lParam)
  92. {
  93.  UINT idTimer;
  94.  HDC hDC;
  95.  PAINTSTRUCT ps;
  96.  int i;
  97.  int TheChar;
  98.  char Temp[82];
  99.  HMENU hMenu;
  100.  int Count;
  101.  static FARPROC lpProcAbout;
  102.  static int ThePort;
  103.  
  104.  hMainWnd = hWindow;
  105.  switch (message)
  106.     {
  107.      case WM_COMMAND:
  108.          switch(wParam)
  109.            {case MSG_ABOUT:
  110.               DialogBox(hInstance,"AboutBox",hMainWnd,lpProcAbout);
  111.               break;
  112.  
  113.             case MSG_LOAD:
  114.               if(!ExpectOffLine()) break;
  115.               LoadConfig();
  116.               break;
  117.  
  118.             case MSG_SAVE:
  119.               if(!ExpectOffLine()) break;
  120.               SaveConfig();
  121.               SetTitle();
  122.               break;
  123.  
  124.             case MSG_ONLINE:
  125.               if(!ExpectOffLine()) break;
  126.               if(FatalFlag) ErrorMessage("Fatal Error");
  127.               else
  128.                 {/* try to go on-line */
  129.                  GoOnLine();
  130.                  if(!OnLineFlag) break;
  131.                  ThePort = GetPort();
  132.                 }
  133.               break;
  134.  
  135.             case MSG_OFFLINE:
  136.               GoOffLine();
  137.               break;
  138.  
  139.             case MSG_EXIT:
  140.               GoOffLine();
  141.               KillTimer(hMainWnd,idTimer);
  142.               PostQuitMessage(0);
  143.               break;
  144.  
  145.             case MSG_1200:
  146.               SetBaud(Baud1200);
  147.               break;
  148.  
  149.             case MSG_2400:
  150.               SetBaud(Baud2400);
  151.               break;
  152.  
  153.             case MSG_4800:
  154.               SetBaud(Baud4800);
  155.               break;
  156.  
  157.             case MSG_9600:
  158.               SetBaud(Baud9600);
  159.               break;
  160.  
  161.             case MSG_19200:
  162.               SetBaud(Baud19200);
  163.               break;
  164.  
  165.             case MSG_38400:
  166.               SetBaud(Baud38400);
  167.               break;
  168.  
  169.             case MSG_57600:
  170.               SetBaud(Baud57600);
  171.               break;
  172.  
  173.             case MSG_115200:
  174.               SetBaud(Baud115200);
  175.               break;
  176.  
  177.             case MSG_COM1:
  178.               SetPort(COM1);
  179.               break;
  180.  
  181.             case MSG_COM2:
  182.               SetPort(COM2);
  183.               break;
  184.  
  185.             case MSG_COM3:
  186.               SetPort(COM3);
  187.               break;
  188.  
  189.             case MSG_COM4:
  190.               SetPort(COM4);
  191.               break;
  192.  
  193.             case MSG_NONE:
  194.               SetParity(NoParity);
  195.               break;
  196.  
  197.             case MSG_EVEN:
  198.               SetParity(EvenParity);
  199.               break;
  200.  
  201.             case MSG_ODD:
  202.               SetParity(OddParity);
  203.               break;
  204.  
  205.             case MSG_1_SB:
  206.               SetStopBits(OneStopBit);
  207.               break;
  208.  
  209.             case MSG_2_SB:
  210.               SetStopBits(TwoStopBits);
  211.               break;
  212.  
  213.             case MSG_7_DB:
  214.               SetWordLength(WordLength7);
  215.               break;
  216.  
  217.             case MSG_8_DB:
  218.               SetWordLength(WordLength8);
  219.               break;
  220.  
  221.             default:
  222.               return (DefWindowProc(hMainWnd, message, wParam, lParam));
  223.            }
  224.          break;
  225.  
  226.     case WM_CREATE:
  227.  
  228. /*
  229. ** You must call PostMainHandle() before attemping to go online.
  230. ** This is required only for the Shareware version of PCL4W.
  231. */
  232.       PostMainHandle(hMainWnd);
  233.  
  234.       /* check "OFFLINE" menu item */
  235.       hMenu = GetMenu(hMainWnd);
  236.       CheckMenuItem(hMenu,MSG_OFFLINE,MF_BYCOMMAND | MF_CHECKED);
  237.       /* create AboutDlgProc() thunk */
  238.       lpProcAbout = MakeProcInstance(AboutDlgProc, hInstance);
  239.       /* init configuration */
  240.       InitConfig();
  241.       LoadConfig();
  242.       /* initialize paint module */
  243.       InitPaint();
  244.       /* start timer */
  245.       idTimer = SetTimer(hMainWnd,1,125,NULL);
  246.       if(idTimer==0)
  247.          {ErrorMessage("No timers remaining !");
  248.           FatalFlag = TRUE;
  249.          }
  250.       break;
  251.  
  252.     case WM_CHAR:
  253.       PutChar(ThePort, (char)wParam );
  254.       break;
  255.  
  256.     case WM_TIMER:
  257.       /* fatal error ? */
  258.       if(FatalFlag) break;
  259.       if(!OnLineFlag) break;
  260.       /* fetch line of up to 82 chars */
  261.       Count = 0;
  262.       for(i=0;i<82;i++)
  263.         {TheChar = GetChar(ThePort);
  264.          /* character available ? */
  265.          if(TheChar==-1) break;
  266.          Temp[Count++] = TheChar;
  267.          if((char)TheChar==(char)LF) break;
  268.         } /* end while */
  269.       if(Count>0) WriteTheString(Temp,Count);
  270.       break;
  271.  
  272.     case WM_SETFOCUS:
  273.       /* create client area caret */
  274.       CreateCaret(hMainWnd,NULL,3,10);
  275.       SetCaretPos(GetXposition(),GetYposition());
  276.       ShowCaret(hMainWnd);
  277.       ShowCaret(hMainWnd);
  278.       break;
  279.  
  280.     case WM_KILLFOCUS:
  281.       DestroyCaret();
  282.       break;
  283.  
  284.     case WM_PAINT:
  285.       HideCaret(hMainWnd);
  286.       hDC = BeginPaint(hMainWnd, &ps);
  287.       SetMapMode(hDC,MM_ANISOTROPIC);
  288.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  289.       PaintMain(hDC,&ps);
  290.       EndPaint(hMainWnd,&ps);
  291.       SetCaretPos(GetXposition(),GetYposition());
  292.       ShowCaret(hMainWnd);
  293.       break;
  294.  
  295.     case WM_DESTROY:
  296.       GoOffLine();
  297.       if(idTimer) KillTimer(hMainWnd,idTimer);
  298.       PostQuitMessage(0);
  299.       break;
  300.  
  301.     default:
  302.       return (DefWindowProc(hMainWnd, message, wParam, lParam));
  303.    }
  304.  return (NULL);
  305. } /* end MainWndProc */
  306.  
  307. void ErrorCheck(int Code)
  308. {/* trap PCL error codes */
  309.  if(Code<0)
  310.      {SioError(Code,"Sio Error");
  311.       SioDone(GetPort());
  312.       FatalFlag = TRUE;
  313.      }
  314. } /* end ErrorCheck */
  315.