home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 17.ddi / SAMPLES / TTY / TTY.C_ / TTY.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  54.2 KB  |  1,797 lines

  1. //---------------------------------------------------------------------------
  2. //
  3. //  Module: tty.c
  4. //
  5. //  Purpose:
  6. //     The sample application demonstrates the usage of the COMM
  7. //     API.  It implements the new COMM API of Windows 3.1.
  8. //
  9. //     NOTE:  no escape sequences are translated, only
  10. //            the necessary control codes (LF, CR, BS, etc.)
  11. //
  12. //  Description of functions:
  13. //     Descriptions are contained in the function headers.
  14. //
  15. //---------------------------------------------------------------------------
  16. //
  17. //  Written by Microsoft Product Support Services, Windows Developer Support.
  18. //  Copyright (c) 1991 Microsoft Corporation.  All Rights Reserved.
  19. //
  20. //---------------------------------------------------------------------------
  21.  
  22. #include "tty.h"
  23.  
  24. //---------------------------------------------------------------------------
  25. //  int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance,
  26. //                      LPSTR lpszCmdLine, int nCmdShow )
  27. //
  28. //  Description:
  29. //     This is the main window loop!
  30. //
  31. //  Parameters:
  32. //     As documented for all WinMain() functions.
  33. //
  34. //---------------------------------------------------------------------------
  35.  
  36. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  37.                     LPSTR lpszCmdLine, int nCmdShow )
  38. {
  39.    HWND  hTTYWnd ;
  40.    MSG   msg ;
  41.  
  42.    if (!hPrevInstance)
  43.       if (!InitApplication( hInstance ))
  44.          return ( FALSE ) ;
  45.  
  46.    if (NULL == (hTTYWnd = InitInstance( hInstance, nCmdShow )))
  47.       return ( FALSE ) ;
  48.  
  49.    while (GetMessage( &msg, NULL, 0, 0 ))
  50.    {
  51.       if (!TranslateAccelerator( hTTYWnd, ghAccel, &msg ))
  52.       {
  53.          TranslateMessage( &msg ) ;
  54.          DispatchMessage( &msg ) ;
  55.       }
  56.    }
  57.    return ( (int) msg.wParam ) ;
  58.  
  59. } // end of WinMain()
  60.  
  61. //---------------------------------------------------------------------------
  62. //  BOOL NEAR InitApplication( HANDLE hInstance )
  63. //
  64. //  Description:
  65. //     First time initialization stuff.  This registers information
  66. //     such as window classes.
  67. //
  68. //  Parameters:
  69. //     HANDLE hInstance
  70. //        Handle to this instance of the application.
  71. //
  72. //---------------------------------------------------------------------------
  73.  
  74. BOOL NEAR InitApplication( HANDLE hInstance )
  75. {
  76.    WNDCLASS  wndclass ;
  77.  
  78.    // register tty window class
  79.  
  80.    wndclass.style =         NULL ;
  81.    wndclass.lpfnWndProc =   TTYWndProc ;
  82.    wndclass.cbClsExtra =    0 ;
  83.    wndclass.cbWndExtra =    sizeof( WORD ) ;
  84.    wndclass.hInstance =     hInstance ;
  85.    wndclass.hIcon =         LoadIcon( hInstance, MAKEINTRESOURCE( TTYICON ) );
  86.    wndclass.hCursor =       LoadCursor( NULL, IDC_ARROW ) ;
  87.    wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
  88.    wndclass.lpszMenuName =  MAKEINTRESOURCE( TTYMENU ) ;
  89.    wndclass.lpszClassName = gszTTYClass ;
  90.  
  91.    return( RegisterClass( &wndclass ) ) ;
  92.  
  93. } // end of InitApplication()
  94.  
  95. //---------------------------------------------------------------------------
  96. //  HWND NEAR InitInstance( HANDLE hInstance, int nCmdShow )
  97. //
  98. //  Description:
  99. //     Initializes instance specific information.
  100. //
  101. //  Parameters:
  102. //     HANDLE hInstance
  103. //        Handle to instance
  104. //
  105. //     int nCmdShow
  106. //        How do we show the window?
  107. //
  108. //---------------------------------------------------------------------------
  109.  
  110. HWND NEAR InitInstance( HANDLE hInstance, int nCmdShow )
  111. {
  112.    HWND  hTTYWnd ;
  113.  
  114.    // load accelerators
  115.    ghAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE( TTYACCEL ) ) ;
  116.  
  117.    // create the TTY window
  118.    hTTYWnd = CreateWindow( gszTTYClass, gszAppName,
  119.                            WS_OVERLAPPEDWINDOW,
  120.                            CW_USEDEFAULT, CW_USEDEFAULT,
  121.                            CW_USEDEFAULT, CW_USEDEFAULT,
  122.                            NULL, NULL, hInstance, NULL ) ;
  123.  
  124.    if (NULL == hTTYWnd)
  125.       return ( NULL ) ;
  126.  
  127.    ShowWindow( hTTYWnd, nCmdShow ) ;
  128.    UpdateWindow( hTTYWnd ) ;
  129.  
  130.    return ( hTTYWnd ) ;
  131.  
  132. } // end of InitInstance()
  133.  
  134. //---------------------------------------------------------------------------
  135. //  LRESULT FAR PASCAL __export TTYWndProc( HWND hWnd, UINT uMsg,
  136. //                                 WPARAM wParam, LPARAM lParam )
  137. //
  138. //  Description:
  139. //     This is the TTY Window Proc.  This handles ALL messages
  140. //     to the tty window.
  141. //
  142. //  Parameters:
  143. //     As documented for Window procedures.
  144. //
  145. //---------------------------------------------------------------------------
  146.  
  147. LRESULT FAR PASCAL __export TTYWndProc( HWND hWnd, UINT uMsg,
  148.                                WPARAM wParam, LPARAM lParam )
  149. {
  150.    switch (uMsg)
  151.    {
  152.       case WM_CREATE:
  153.          return ( CreateTTYInfo( hWnd ) ) ;
  154.  
  155.       case WM_COMMAND:
  156.       {
  157.          switch ((WORD) wParam)
  158.          {
  159.             case IDM_CONNECT:
  160.                if (!OpenConnection( hWnd ))
  161.                   MessageBox( hWnd, "Connection failed.", gszAppName,
  162.                               MB_ICONEXCLAMATION ) ;
  163.                break ;
  164.  
  165.             case IDM_DISCONNECT:
  166.                CloseConnection( hWnd ) ;
  167.                break ;
  168.  
  169.             case IDM_SETTINGS:
  170.             {
  171.                NPTTYINFO  npTTYInfo ;
  172.  
  173.                if (NULL == (npTTYInfo =
  174.                                (NPTTYINFO) GetWindowWord( hWnd,
  175.                                                           GWW_NPTTYINFO )))
  176.                   return ( FALSE ) ;
  177.                GoModalDialogBoxParam( GETHINST( hWnd ),
  178.                                       MAKEINTRESOURCE( SETTINGSDLGBOX ), hWnd,
  179.                                       SettingsDlgProc,
  180.                                       (LPARAM) (LPSTR) npTTYInfo ) ;
  181.  
  182.                // if fConnected, set new COM parameters
  183.  
  184.                if (CONNECTED( npTTYInfo ))
  185.                {
  186.                   if (!SetupConnection( hWnd ))
  187.                      MessageBox( hWnd, "Settings failed!", gszAppName,
  188.                                  MB_ICONEXCLAMATION ) ;
  189.                }
  190.             }
  191.             break ;
  192.  
  193.             case IDM_ABOUT:
  194.                GoModalDialogBoxParam ( GETHINST( hWnd ),
  195.                                        MAKEINTRESOURCE( ABOUTDLGBOX ),
  196.                                        hWnd,
  197.                                        AboutDlgProc, NULL ) ;
  198.                break;
  199.  
  200.             case IDM_EXIT:
  201.                PostMessage( hWnd, WM_CLOSE, NULL, 0L ) ;
  202.                break ;
  203.          }
  204.       }
  205.       break ;
  206.  
  207.       case WM_COMMNOTIFY:
  208.          ProcessCOMMNotification( hWnd, (WORD) wParam, (LONG) lParam ) ;
  209.          break ;
  210.  
  211.       case WM_PAINT:
  212.          PaintTTY( hWnd ) ;
  213.          break ;
  214.  
  215.       case WM_SIZE:
  216.          SizeTTY( hWnd, HIWORD( lParam ), LOWORD( lParam ) ) ;
  217.          break ;
  218.  
  219.       case WM_HSCROLL:
  220.          ScrollTTYHorz( hWnd, (WORD) wParam, LOWORD( lParam ) ) ;
  221.          break ;
  222.  
  223.       case WM_VSCROLL:
  224.          ScrollTTYVert( hWnd, (WORD) wParam, LOWORD( lParam ) ) ;
  225.          break ;
  226.  
  227.       case WM_CHAR:
  228.          ProcessTTYCharacter( hWnd, LOBYTE( wParam ) ) ;
  229.          break ;
  230.  
  231.       case WM_SETFOCUS:
  232.          SetTTYFocus( hWnd ) ;
  233.          break ;
  234.  
  235.       case WM_KILLFOCUS:
  236.          KillTTYFocus( hWnd ) ;
  237.          break ;
  238.  
  239.       case WM_DESTROY:
  240.          DestroyTTYInfo( hWnd ) ;
  241.          PostQuitMessage( 0 ) ;
  242.          break ;
  243.  
  244.       case WM_CLOSE:
  245.          if (IDOK != MessageBox( hWnd, "OK to close window?", "TTY Sample",
  246.                                  MB_ICONQUESTION | MB_OKCANCEL ))
  247.             break ;
  248.  
  249.          // fall through
  250.  
  251.       default:
  252.          return( DefWindowProc( hWnd, uMsg, wParam, lParam ) ) ;
  253.    }
  254.    return 0L ;
  255.  
  256. } // end of TTYWndProc()
  257.  
  258. //---------------------------------------------------------------------------
  259. //  LRESULT NEAR CreateTTYInfo( HWND hWnd )
  260. //
  261. //  Description:
  262. //     Creates the tty information structure and sets
  263. //     menu option availability.  Returns -1 if unsuccessful.
  264. //
  265. //  Parameters:
  266. //     HWND  hWnd
  267. //        Handle to main window.
  268. //
  269. //---------------------------------------------------------------------------
  270.  
  271. LRESULT NEAR CreateTTYInfo( HWND hWnd )
  272. {
  273.    HMENU       hMenu ;
  274.    NPTTYINFO   npTTYInfo ;
  275.  
  276.    if (NULL == (npTTYInfo =
  277.                    (NPTTYINFO) LocalAlloc( LPTR, sizeof( TTYINFO ) )))
  278.       return ( (LRESULT) -1 ) ;
  279.  
  280.    // initialize TTY info structure
  281.  
  282.    COMDEV( npTTYInfo )        = 0 ;
  283.    CONNECTED( npTTYInfo )     = FALSE ;
  284.    CURSORSTATE( npTTYInfo )   = CS_HIDE ;
  285.    LOCALECHO( npTTYInfo )     = FALSE ;
  286.    AUTOWRAP( npTTYInfo )      = TRUE ;
  287.    PORT( npTTYInfo )          = 1 ;
  288.    BAUDRATE( npTTYInfo )      = CBR_9600 ;
  289.    BYTESIZE( npTTYInfo )      = 8 ;
  290.    FLOWCTRL( npTTYInfo )      = FC_RTSCTS ;
  291.    PARITY( npTTYInfo )        = NOPARITY ;
  292.    STOPBITS( npTTYInfo )      = ONESTOPBIT ;
  293.    XONXOFF( npTTYInfo )       = FALSE ;
  294.    XSIZE( npTTYInfo )         = 0 ;
  295.    YSIZE( npTTYInfo )         = 0 ;
  296.    XSCROLL( npTTYInfo )       = 0 ;
  297.    YSCROLL( npTTYInfo )       = 0 ;
  298.    XOFFSET( npTTYInfo )       = 0 ;
  299.    YOFFSET( npTTYInfo )       = 0 ;
  300.    COLUMN( npTTYInfo )        = 0 ;
  301.    ROW( npTTYInfo )           = 0 ;
  302.    HTTYFONT( npTTYInfo )      = NULL ;
  303.    FGCOLOR( npTTYInfo )       = RGB( 0, 0, 0 ) ;
  304.    USECNRECEIVE( npTTYInfo )  = TRUE ;
  305.    DISPLAYERRORS( npTTYInfo ) = TRUE ;
  306.  
  307.    // clear screen space
  308.  
  309.    _fmemset( SCREEN( npTTYInfo ), ' ', MAXROWS * MAXCOLS ) ;
  310.  
  311.    // setup default font information
  312.  
  313.    LFTTYFONT( npTTYInfo ).lfHeight =         12 ;
  314.    LFTTYFONT( npTTYInfo ).lfWidth =          0 ;
  315.    LFTTYFONT( npTTYInfo ).lfEscapement =     0 ;
  316.    LFTTYFONT( npTTYInfo ).lfOrientation =    0 ;
  317.    LFTTYFONT( npTTYInfo ).lfWeight =         0 ;
  318.    LFTTYFONT( npTTYInfo ).lfItalic =         0 ;
  319.    LFTTYFONT( npTTYInfo ).lfUnderline =      0 ;
  320.    LFTTYFONT( npTTYInfo ).lfStrikeOut =      0 ;
  321.    LFTTYFONT( npTTYInfo ).lfCharSet =        OEM_CHARSET ;
  322.    LFTTYFONT( npTTYInfo ).lfOutPrecision =   OUT_DEFAULT_PRECIS ;
  323.    LFTTYFONT( npTTYInfo ).lfClipPrecision =  CLIP_DEFAULT_PRECIS ;
  324.    LFTTYFONT( npTTYInfo ).lfQuality =        DEFAULT_QUALITY ;
  325.    LFTTYFONT( npTTYInfo ).lfPitchAndFamily = FIXED_PITCH | FF_MODERN ;
  326.    LFTTYFONT( npTTYInfo ).lfFaceName[0] =    NULL ;
  327.  
  328.    // set TTYInfo handle before any further message processing.
  329.  
  330.    SetWindowWord( hWnd, GWW_NPTTYINFO, (WPARAM) npTTYInfo ) ;
  331.  
  332.    // reset the character information, etc.
  333.  
  334.    ResetTTYScreen( hWnd, npTTYInfo ) ;
  335.  
  336.    hMenu = GetMenu( hWnd ) ;
  337.    EnableMenuItem( hMenu, IDM_DISCONNECT,
  338.                    MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;
  339.    EnableMenuItem( hMenu, IDM_CONNECT, MF_ENABLED | MF_BYCOMMAND ) ;
  340.  
  341.    return ( (LRESULT) TRUE ) ;
  342.  
  343. } // end of CreateTTYInfo()
  344.  
  345. //---------------------------------------------------------------------------
  346. //  BOOL NEAR DestroyTTYInfo( HWND hWnd )
  347. //
  348. //  Description:
  349. //     Destroys block associated with TTY window handle.
  350. //
  351. //  Parameters:
  352. //     HWND hWnd
  353. //        handle to TTY window
  354. //
  355. //---------------------------------------------------------------------------
  356.  
  357. BOOL NEAR DestroyTTYInfo( HWND hWnd )
  358. {
  359.    NPTTYINFO npTTYInfo ;
  360.  
  361.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  362.       return ( FALSE ) ;
  363.  
  364.    // force connection closed (if not already closed)
  365.  
  366.    if (CONNECTED( npTTYInfo ))
  367.       CloseConnection( hWnd ) ;
  368.  
  369.    DeleteObject( HTTYFONT( npTTYInfo ) ) ;
  370.  
  371.    LocalFree( npTTYInfo ) ;
  372.    return ( TRUE ) ;
  373.  
  374. } // end of DestroyTTYInfo()
  375.  
  376. //---------------------------------------------------------------------------
  377. //  BOOL NEAR ResetTTYScreen( HWND hWnd, NPTTYINFO npTTYInfo )
  378. //
  379. //  Description:
  380. //     Resets the TTY character information and causes the
  381. //     screen to resize to update the scroll information.
  382. //
  383. //  Parameters:
  384. //     NPTTYINFO  npTTYInfo
  385. //        pointer to TTY info structure
  386. //
  387. //---------------------------------------------------------------------------
  388.  
  389. BOOL NEAR ResetTTYScreen( HWND hWnd, NPTTYINFO npTTYInfo )
  390. {
  391.    HDC         hDC ;
  392.    TEXTMETRIC  tm ;
  393.    RECT        rcWindow ;
  394.  
  395.    if (NULL == npTTYInfo)
  396.       return ( FALSE ) ;
  397.  
  398.    if (NULL != HTTYFONT( npTTYInfo ))
  399.       DeleteObject( HTTYFONT( npTTYInfo ) ) ;
  400.  
  401.    HTTYFONT( npTTYInfo ) = CreateFontIndirect( &LFTTYFONT( npTTYInfo ) ) ;
  402.  
  403.    hDC = GetDC( hWnd ) ;
  404.    SelectObject( hDC, HTTYFONT( npTTYInfo ) ) ;
  405.    GetTextMetrics( hDC, &tm ) ;
  406.    ReleaseDC( hWnd, hDC ) ;
  407.  
  408.    XCHAR( npTTYInfo ) = tm.tmAveCharWidth  ;
  409.    YCHAR( npTTYInfo ) = tm.tmHeight + tm.tmExternalLeading ;
  410.  
  411.    // a slimy hack to force the scroll position, region to
  412.    // be recalculated based on the new character sizes
  413.  
  414.    GetWindowRect( hWnd, &rcWindow ) ;
  415.    SendMessage( hWnd, WM_SIZE, SIZENORMAL,
  416.                 (LPARAM) MAKELONG( rcWindow.right - rcWindow.left,
  417.                                    rcWindow.bottom - rcWindow.top ) ) ;
  418.  
  419.    return ( TRUE ) ;
  420.  
  421. } // end of ResetTTYScreen()
  422.  
  423. //---------------------------------------------------------------------------
  424. //  BOOL NEAR PaintTTY( HWND hWnd )
  425. //
  426. //  Description:
  427. //     Paints the rectangle determined by the paint struct of
  428. //     the DC.
  429. //
  430. //  Parameters:
  431. //     HWND hWnd
  432. //        handle to TTY window (as always)
  433. //
  434. //---------------------------------------------------------------------------
  435.  
  436. BOOL NEAR PaintTTY( HWND hWnd )
  437. {
  438.    int          nRow, nCol, nEndRow, nEndCol, nCount, nHorzPos, nVertPos ;
  439.    HDC          hDC ;
  440.    HFONT        hOldFont ;
  441.    NPTTYINFO    npTTYInfo ;
  442.    PAINTSTRUCT  ps ;
  443.    RECT         rect ;
  444.  
  445.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  446.       return ( FALSE ) ;
  447.  
  448.    hDC = BeginPaint( hWnd, &ps ) ;
  449.    hOldFont = SelectObject( hDC, HTTYFONT( npTTYInfo ) ) ;
  450.    SetTextColor( hDC, FGCOLOR( npTTYInfo ) ) ;
  451.    SetBkColor( hDC, GetSysColor( COLOR_WINDOW ) ) ;
  452.    rect = ps.rcPaint ;
  453.    nRow =
  454.       min( MAXROWS - 1,
  455.            max( 0, (rect.top + YOFFSET( npTTYInfo )) / YCHAR( npTTYInfo ) ) ) ;
  456.    nEndRow =
  457.       min( MAXROWS - 1,
  458.            ((rect.bottom + YOFFSET( npTTYInfo ) - 1) / YCHAR( npTTYInfo ) ) ) ;
  459.    nCol =
  460.       min( MAXCOLS - 1,
  461.            max( 0, (rect.left + XOFFSET( npTTYInfo )) / XCHAR( npTTYInfo ) ) ) ;
  462.    nEndCol =
  463.       min( MAXCOLS - 1,
  464.            ((rect.right + XOFFSET( npTTYInfo ) - 1) / XCHAR( npTTYInfo ) ) ) ;
  465.    nCount = nEndCol - nCol + 1 ;
  466.    for (; nRow <= nEndRow; nRow++)
  467.    {
  468.       nVertPos = (nRow * YCHAR( npTTYInfo )) - YOFFSET( npTTYInfo ) ;
  469.       nHorzPos = (nCol * XCHAR( npTTYInfo )) - XOFFSET( npTTYInfo ) ;
  470.       rect.top = nVertPos ;
  471.       rect.bottom = nVertPos + YCHAR( npTTYInfo ) ;
  472.       rect.left = nHorzPos ;
  473.       rect.right = nHorzPos + XCHAR( npTTYInfo ) * nCount ;
  474.       SetBkMode( hDC, OPAQUE ) ;
  475.       ExtTextOut( hDC, nHorzPos, nVertPos, ETO_OPAQUE | ETO_CLIPPED, &rect,
  476.                   (LPSTR)( SCREEN( npTTYInfo ) + nRow * MAXCOLS + nCol ),
  477.                   nCount, NULL ) ;
  478.    }
  479.    SelectObject( hDC, hOldFont ) ;
  480.    EndPaint( hWnd, &ps ) ;
  481.    MoveTTYCursor( hWnd ) ;
  482.    return ( TRUE ) ;
  483.  
  484. } // end of PaintTTY()
  485.  
  486. //---------------------------------------------------------------------------
  487. //  BOOL NEAR SizeTTY( HWND hWnd, WORD wVertSize, WORD wHorzSize )
  488. //
  489. //  Description:
  490. //     Sizes TTY and sets up scrolling regions.
  491. //
  492. //  Parameters:
  493. //     HWND hWnd
  494. //        handle to TTY window
  495. //
  496. //     WORD wVertSize
  497. //        new vertical size
  498. //
  499. //     WORD wHorzSize
  500. //        new horizontal size
  501. //
  502. //---------------------------------------------------------------------------
  503.  
  504. BOOL NEAR SizeTTY( HWND hWnd, WORD wVertSize, WORD wHorzSize )
  505. {
  506.    int        nScrollAmt ;
  507.    NPTTYINFO  npTTYInfo ;
  508.  
  509.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  510.       return ( FALSE ) ;
  511.  
  512.    YSIZE( npTTYInfo ) = (int) wVertSize ;
  513.    YSCROLL( npTTYInfo ) = max( 0, (MAXROWS * YCHAR( npTTYInfo )) -
  514.                                YSIZE( npTTYInfo ) ) ;
  515.    nScrollAmt = min( YSCROLL( npTTYInfo ), YOFFSET( npTTYInfo ) ) -
  516.                      YOFFSET( npTTYInfo ) ;
  517.    ScrollWindow( hWnd, 0, -nScrollAmt, NULL, NULL ) ;
  518.  
  519.    YOFFSET( npTTYInfo ) = YOFFSET( npTTYInfo ) + nScrollAmt ;
  520.    SetScrollPos( hWnd, SB_VERT, YOFFSET( npTTYInfo ), FALSE ) ;
  521.    SetScrollRange( hWnd, SB_VERT, 0, YSCROLL( npTTYInfo ), TRUE ) ;
  522.  
  523.    XSIZE( npTTYInfo ) = (int) wHorzSize ;
  524.    XSCROLL( npTTYInfo ) = max( 0, (MAXCOLS * XCHAR( npTTYInfo )) -
  525.                                 XSIZE( npTTYInfo ) ) ;
  526.    nScrollAmt = min( XSCROLL( npTTYInfo ), XOFFSET( npTTYInfo )) -
  527.                      XOFFSET( npTTYInfo ) ;
  528.    ScrollWindow( hWnd, 0, -nScrollAmt, NULL, NULL ) ;
  529.    XOFFSET( npTTYInfo ) = XOFFSET( npTTYInfo ) + nScrollAmt ;
  530.    SetScrollPos( hWnd, SB_HORZ, XOFFSET( npTTYInfo ), FALSE ) ;
  531.    SetScrollRange( hWnd, SB_HORZ, 0, XSCROLL( npTTYInfo ), TRUE ) ;
  532.  
  533.    InvalidateRect( hWnd, NULL, TRUE ) ;
  534.  
  535.    return ( TRUE ) ;
  536.  
  537. } // end of SizeTTY()
  538.  
  539. //---------------------------------------------------------------------------
  540. //  BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
  541. //
  542. //  Description:
  543. //     Scrolls TTY window vertically.
  544. //
  545. //  Parameters:
  546. //     HWND hWnd
  547. //        handle to TTY window
  548. //
  549. //     WORD wScrollCmd
  550. //        type of scrolling we're doing
  551. //
  552. //     WORD wScrollPos
  553. //        scroll position
  554. //
  555. //---------------------------------------------------------------------------
  556.  
  557. BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
  558. {
  559.    int        nScrollAmt ;
  560.    NPTTYINFO  npTTYInfo ;
  561.  
  562.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  563.       return ( FALSE ) ;
  564.  
  565.    switch (wScrollCmd)
  566.    {
  567.       case SB_TOP:
  568.          nScrollAmt = -YOFFSET( npTTYInfo ) ;
  569.          break ;
  570.  
  571.       case SB_BOTTOM:
  572.          nScrollAmt = YSCROLL( npTTYInfo ) - YOFFSET( npTTYInfo ) ;
  573.          break ;
  574.  
  575.       case SB_PAGEUP:
  576.          nScrollAmt = -YSIZE( npTTYInfo ) ;
  577.          break ;
  578.  
  579.       case SB_PAGEDOWN:
  580.          nScrollAmt = YSIZE( npTTYInfo ) ;
  581.          break ;
  582.  
  583.       case SB_LINEUP:
  584.          nScrollAmt = -YCHAR( npTTYInfo ) ;
  585.          break ;
  586.  
  587.       case SB_LINEDOWN:
  588.          nScrollAmt = YCHAR( npTTYInfo ) ;
  589.          break ;
  590.  
  591.       case SB_THUMBPOSITION:
  592.          nScrollAmt = wScrollPos - YOFFSET( npTTYInfo ) ;
  593.          break ;
  594.  
  595.       default:
  596.          return ( FALSE ) ;
  597.    }
  598.    if ((YOFFSET( npTTYInfo ) + nScrollAmt) > YSCROLL( npTTYInfo ))
  599.       nScrollAmt = YSCROLL( npTTYInfo ) - YOFFSET( npTTYInfo ) ;
  600.    if ((YOFFSET( npTTYInfo ) + nScrollAmt) < 0)
  601.       nScrollAmt = -YOFFSET( npTTYInfo ) ;
  602.    ScrollWindow( hWnd, 0, -nScrollAmt, NULL, NULL ) ;
  603.    YOFFSET( npTTYInfo ) = YOFFSET( npTTYInfo ) + nScrollAmt ;
  604.    SetScrollPos( hWnd, SB_VERT, YOFFSET( npTTYInfo ), TRUE ) ;
  605.  
  606.    return ( TRUE ) ;
  607.  
  608. } // end of ScrollTTYVert()
  609.  
  610. //---------------------------------------------------------------------------
  611. //  BOOL NEAR ScrollTTYHorz( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
  612. //
  613. //  Description:
  614. //     Scrolls TTY window horizontally.
  615. //
  616. //  Parameters:
  617. //     HWND hWnd
  618. //        handle to TTY window
  619. //
  620. //     WORD wScrollCmd
  621. //        type of scrolling we're doing
  622. //
  623. //     WORD wScrollPos
  624. //        scroll position
  625. //
  626. //---------------------------------------------------------------------------
  627.  
  628. BOOL NEAR ScrollTTYHorz( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
  629. {
  630.    int        nScrollAmt ;
  631.    NPTTYINFO  npTTYInfo ;
  632.  
  633.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  634.       return ( FALSE ) ;
  635.  
  636.    switch (wScrollCmd)
  637.    {
  638.       case SB_TOP:
  639.          nScrollAmt = -XOFFSET( npTTYInfo ) ;
  640.          break ;
  641.  
  642.       case SB_BOTTOM:
  643.          nScrollAmt = XSCROLL( npTTYInfo ) - XOFFSET( npTTYInfo ) ;
  644.          break ;
  645.  
  646.       case SB_PAGEUP:
  647.          nScrollAmt = -XSIZE( npTTYInfo ) ;
  648.          break ;
  649.  
  650.       case SB_PAGEDOWN:
  651.          nScrollAmt = XSIZE( npTTYInfo ) ;
  652.          break ;
  653.  
  654.       case SB_LINEUP:
  655.          nScrollAmt = -XCHAR( npTTYInfo ) ;
  656.          break ;
  657.  
  658.       case SB_LINEDOWN:
  659.          nScrollAmt = XCHAR( npTTYInfo ) ;
  660.          break ;
  661.  
  662.       case SB_THUMBPOSITION:
  663.          nScrollAmt = wScrollPos - XOFFSET( npTTYInfo ) ;
  664.          break ;
  665.  
  666.       default:
  667.          return ( FALSE ) ;
  668.    }
  669.    if ((XOFFSET( npTTYInfo ) + nScrollAmt) > XSCROLL( npTTYInfo ))
  670.       nScrollAmt = XSCROLL( npTTYInfo ) - XOFFSET( npTTYInfo ) ;
  671.    if ((XOFFSET( npTTYInfo ) + nScrollAmt) < 0)
  672.       nScrollAmt = -XOFFSET( npTTYInfo ) ;
  673.    ScrollWindow( hWnd, -nScrollAmt, 0, NULL, NULL ) ;
  674.    XOFFSET( npTTYInfo ) = XOFFSET( npTTYInfo ) + nScrollAmt ;
  675.    SetScrollPos( hWnd, SB_HORZ, XOFFSET( npTTYInfo ), TRUE ) ;
  676.  
  677.    return ( TRUE ) ;
  678.  
  679. } // end of ScrollTTYHorz()
  680.  
  681. //---------------------------------------------------------------------------
  682. //  BOOL NEAR SetTTYFocus( HWND hWnd )
  683. //
  684. //  Description:
  685. //     Sets the focus to the TTY window also creates caret.
  686. //
  687. //  Parameters:
  688. //     HWND hWnd
  689. //        handle to TTY window
  690. //
  691. //---------------------------------------------------------------------------
  692.  
  693. BOOL NEAR SetTTYFocus( HWND hWnd )
  694. {
  695.    NPTTYINFO  npTTYInfo ;
  696.  
  697.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  698.       return ( FALSE ) ;
  699.  
  700.    if (CONNECTED( npTTYInfo ) && (CURSORSTATE( npTTYInfo ) != CS_SHOW))
  701.    {
  702.       CreateCaret( hWnd, NULL, XCHAR( npTTYInfo ), YCHAR( npTTYInfo ) ) ;
  703.       ShowCaret( hWnd ) ;
  704.       CURSORSTATE( npTTYInfo ) = CS_SHOW ;
  705.    }
  706.    MoveTTYCursor( hWnd ) ;
  707.    return ( TRUE ) ;
  708.  
  709. } // end of SetTTYFocus()
  710.  
  711. //---------------------------------------------------------------------------
  712. //  BOOL NEAR KillTTYFocus( HWND hWnd )
  713. //
  714. //  Description:
  715. //     Kills TTY focus and destroys the caret.
  716. //
  717. //  Parameters:
  718. //     HWND hWnd
  719. //        handle to TTY window
  720. //
  721. //---------------------------------------------------------------------------
  722.  
  723. BOOL NEAR KillTTYFocus( HWND hWnd )
  724. {
  725.    NPTTYINFO  npTTYInfo ;
  726.  
  727.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  728.       return ( FALSE ) ;
  729.  
  730.    if (CONNECTED( npTTYInfo ) && (CURSORSTATE( npTTYInfo ) != CS_HIDE))
  731.    {
  732.       HideCaret( hWnd ) ;
  733.       DestroyCaret() ;
  734.       CURSORSTATE( npTTYInfo ) = CS_HIDE ;
  735.    }
  736.    return ( TRUE ) ;
  737.  
  738. } // end of KillTTYFocus()
  739.  
  740. //---------------------------------------------------------------------------
  741. //  BOOL NEAR MoveTTYCursor( HWND hWnd )
  742. //
  743. //  Description:
  744. //     Moves caret to current position.
  745. //
  746. //  Parameters:
  747. //     HWND hWnd
  748. //        handle to TTY window
  749. //
  750. //---------------------------------------------------------------------------
  751.  
  752. BOOL NEAR MoveTTYCursor( HWND hWnd )
  753. {
  754.    NPTTYINFO  npTTYInfo ;
  755.  
  756.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  757.       return ( FALSE ) ;
  758.  
  759.    if (CONNECTED( npTTYInfo ) && (CURSORSTATE( npTTYInfo ) & CS_SHOW))
  760.       SetCaretPos( (COLUMN( npTTYInfo ) * XCHAR( npTTYInfo )) -
  761.                    XOFFSET( npTTYInfo ),
  762.                    (ROW( npTTYInfo ) * YCHAR( npTTYInfo )) -
  763.                    YOFFSET( npTTYInfo ) ) ;
  764.  
  765.    return ( TRUE ) ;
  766.  
  767. } // end of MoveTTYCursor()
  768.  
  769. //---------------------------------------------------------------------------
  770. //  BOOL NEAR ProcessCOMMNotification( HWND hWnd, WORD wParam, LONG lParam )
  771. //
  772. //  Description:
  773. //     Processes the WM_COMMNOTIFY message from the COMM.DRV.
  774. //
  775. //  Parameters:
  776. //     HWND hWnd
  777. //        handle to TTY window
  778. //
  779. //     WORD wParam
  780. //        specifes the device (nCid)
  781. //
  782. //     LONG lParam
  783. //        LOWORD contains event trigger
  784. //        HIWORD is NULL
  785. //
  786. //---------------------------------------------------------------------------
  787.  
  788. BOOL NEAR ProcessCOMMNotification( HWND hWnd, WORD wParam, LONG lParam )
  789. {
  790.    char       szError[ 10 ] ;
  791.    int        nError, nLength ;
  792.    BYTE       abIn[ MAXBLOCK + 1] ;
  793.    COMSTAT    ComStat ;
  794.    NPTTYINFO  npTTYInfo ;
  795.    MSG        msg ;
  796.  
  797.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  798.       return ( FALSE ) ;
  799.  
  800.    if (!USECNRECEIVE( npTTYInfo ))
  801.    {
  802.       // verify that it is a COMM event specified by our mask
  803.  
  804.       if (CN_EVENT & LOWORD( lParam ) != CN_EVENT)
  805.          return ( FALSE ) ;
  806.  
  807.       // reset the event word so we are notified
  808.       // when the next event occurs
  809.  
  810.       GetCommEventMask( COMDEV( npTTYInfo ), EV_RXCHAR ) ;
  811.  
  812.       // We loop here since it is highly likely that the buffer
  813.       // can been filled while we are reading this block.  This
  814.       // is especially true when operating at high baud rates
  815.       // (e.g. >= 9600 baud).
  816.  
  817.       do
  818.       {
  819.          if (nLength = ReadCommBlock( hWnd, (LPSTR) abIn, MAXBLOCK ))
  820.          {
  821.             WriteTTYBlock( hWnd, (LPSTR) abIn, nLength ) ;
  822.  
  823.             // force a paint
  824.  
  825.             UpdateWindow( hWnd ) ;
  826.          }
  827.       }
  828.       while (!PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE) ||
  829.              (nLength > 0)) ;
  830.  
  831.    }
  832.    else
  833.    {
  834.       // verify that it is a receive event
  835.  
  836.       if (CN_RECEIVE & LOWORD( lParam ) != CN_RECEIVE)
  837.          return ( FALSE ) ;
  838.  
  839.       do
  840.       {
  841.          if (nLength = ReadCommBlock( hWnd, (LPSTR) abIn, MAXBLOCK ))
  842.          {
  843.             WriteTTYBlock( hWnd, (LPSTR) abIn, nLength ) ;
  844.  
  845.             // force a paint
  846.  
  847.             UpdateWindow( hWnd ) ;
  848.          }
  849.          if (nError = GetCommError( COMDEV( npTTYInfo ), &ComStat ))
  850.          {
  851.             if (DISPLAYERRORS( npTTYInfo ))
  852.             {
  853.                wsprintf( szError, "<CE-%d>", nError ) ;
  854.                WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
  855.             }
  856.          }
  857.       }
  858.       while ((!PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE )) ||
  859.             (ComStat.cbInQue >= MAXBLOCK)) ;
  860.    }
  861.    return ( TRUE ) ;
  862.  
  863. } // end of ProcessCOMMNotification()
  864.  
  865. //---------------------------------------------------------------------------
  866. //  BOOL NEAR ProcessTTYCharacter( HWND hWnd, BYTE bOut )
  867. //
  868. //  Description:
  869. //     This simply writes a character to the port and echos it
  870. //     to the TTY screen if fLocalEcho is set.  Some minor
  871. //     keyboard mapping could be performed here.
  872. //
  873. //  Parameters:
  874. //     HWND hWnd
  875. //        handle to TTY window
  876. //
  877. //     BYTE bOut
  878. //        byte from keyboard
  879. //
  880. //---------------------------------------------------------------------------
  881.  
  882. BOOL NEAR ProcessTTYCharacter( HWND hWnd, BYTE bOut )
  883. {
  884.    NPTTYINFO  npTTYInfo ;
  885.  
  886.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  887.       return ( FALSE ) ;
  888.  
  889.    if (!CONNECTED( npTTYInfo ))
  890.       return ( FALSE ) ;
  891.  
  892.    WriteCommByte( hWnd, bOut ) ;
  893.    if (LOCALECHO( npTTYInfo ))
  894.       WriteTTYBlock( hWnd, &bOut, 1 ) ;
  895.  
  896.    return ( TRUE ) ;
  897.  
  898. } // end of ProcessTTYCharacter()
  899.  
  900. //---------------------------------------------------------------------------
  901. //  BOOL NEAR OpenConnection( HWND hWnd )
  902. //
  903. //  Description:
  904. //     Opens communication port specified in the TTYINFO struct.
  905. //     It also sets the CommState and notifies the window via
  906. //     the fConnected flag in the TTYINFO struct.
  907. //
  908. //  Parameters:
  909. //     HWND hWnd
  910. //        handle to TTY window
  911. //
  912. //---------------------------------------------------------------------------
  913.  
  914. BOOL NEAR OpenConnection( HWND hWnd )
  915. {
  916.    char       szPort[ 10 ], szTemp[ 10 ] ;
  917.    BOOL       fRetVal ;
  918.    HCURSOR    hOldCursor, hWaitCursor ;
  919.    HMENU      hMenu ;
  920.    NPTTYINFO  npTTYInfo ;
  921.  
  922.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  923.       return ( FALSE ) ;
  924.  
  925.    // show the hourglass cursor
  926.    hWaitCursor = LoadCursor( NULL, IDC_WAIT ) ;
  927.    hOldCursor = SetCursor( hWaitCursor ) ;
  928.  
  929.    // load the COM prefix string and append port number
  930.  
  931.    LoadString( GETHINST( hWnd ), IDS_COMPREFIX, szTemp, sizeof( szTemp ) ) ;
  932.    wsprintf( szPort, "%s%d", (LPSTR) szTemp, PORT( npTTYInfo ) ) ;
  933.  
  934.    // open COMM device
  935.  
  936.    if ((COMDEV( npTTYInfo ) = OpenComm( szPort, RXQUEUE, TXQUEUE )) < 0)
  937.       return ( FALSE ) ;
  938.  
  939.    fRetVal = SetupConnection( hWnd ) ;
  940.  
  941.    if (fRetVal)
  942.    {
  943.       CONNECTED( npTTYInfo ) = TRUE ;
  944.  
  945.       // set up notifications from COMM.DRV
  946.  
  947.       if (!USECNRECEIVE( npTTYInfo ))
  948.       {
  949.          // In this case we really are only using the notifications
  950.          // for the received characters - it could be expanded to
  951.          // cover the changes in CD or other status lines.
  952.  
  953.          SetCommEventMask( COMDEV( npTTYInfo ), EV_RXCHAR ) ;
  954.  
  955.          // Enable notifications for events only.
  956.  
  957.          // NB:  This method does not use the specific
  958.          // in/out queue triggers.
  959.  
  960.          EnableCommNotification( COMDEV( npTTYInfo ), hWnd, -1, -1 ) ;
  961.       }
  962.       else
  963.       {
  964.          // Enable notification for CN_RECEIVE events.
  965.  
  966.          EnableCommNotification( COMDEV( npTTYInfo ), hWnd, MAXBLOCK, -1 ) ;
  967.       }
  968.  
  969.       // assert DTR
  970.  
  971.       EscapeCommFunction( COMDEV( npTTYInfo ), SETDTR ) ;
  972.  
  973.  
  974.       SetTTYFocus( hWnd ) ;
  975.  
  976.       hMenu = GetMenu( hWnd ) ;
  977.       EnableMenuItem( hMenu, IDM_DISCONNECT,
  978.                       MF_ENABLED | MF_BYCOMMAND ) ;
  979.       EnableMenuItem( hMenu, IDM_CONNECT,
  980.                       MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;
  981.    }
  982.    else
  983.    {
  984.       CONNECTED( npTTYInfo ) = FALSE ;
  985.       CloseComm( COMDEV( npTTYInfo ) ) ;
  986.    }
  987.  
  988.    // restore cursor
  989.    SetCursor( hOldCursor ) ;
  990.  
  991.    return ( fRetVal ) ;
  992.  
  993. } // end of OpenConnection()
  994.  
  995. //---------------------------------------------------------------------------
  996. //  BOOL NEAR SetupConnection( HWND hWnd )
  997. //
  998. //  Description:
  999. //     This routines sets up the DCB based on settings in the
  1000. //     TTY info structure and performs a SetCommState().
  1001. //
  1002. //  Parameters:
  1003. //     HWND hWnd
  1004. //        handle to TTY window
  1005. //
  1006. //---------------------------------------------------------------------------
  1007.  
  1008. BOOL NEAR SetupConnection( HWND hWnd )
  1009. {
  1010.    BOOL       fRetVal ;
  1011.    BYTE       bSet ;
  1012.    DCB        dcb ;
  1013.    NPTTYINFO  npTTYInfo ;
  1014.  
  1015.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  1016.       return ( FALSE ) ;
  1017.  
  1018.    GetCommState( COMDEV( npTTYInfo ), &dcb ) ;
  1019.  
  1020.    dcb.BaudRate = BAUDRATE( npTTYInfo ) ;
  1021.    dcb.ByteSize = BYTESIZE( npTTYInfo ) ;
  1022.    dcb.Parity = PARITY( npTTYInfo ) ;
  1023.    dcb.StopBits = STOPBITS( npTTYInfo ) ;
  1024.  
  1025.    // setup hardware flow control
  1026.  
  1027.    bSet = (BYTE) ((FLOWCTRL( npTTYInfo ) & FC_DTRDSR) != 0) ;
  1028.    dcb.fOutxDsrFlow = dcb.fDtrflow = bSet ;
  1029.    dcb.DsrTimeout = (bSet) ? 30 : 0 ;
  1030.  
  1031.    bSet = (BYTE) ((FLOWCTRL( npTTYInfo ) & FC_RTSCTS) != 0) ;
  1032.    dcb.fOutxCtsFlow = dcb.fRtsflow = bSet ;
  1033.    dcb.CtsTimeout = (bSet) ? 30 : 0 ;
  1034.  
  1035.    // setup software flow control
  1036.  
  1037.    bSet = (BYTE) ((FLOWCTRL( npTTYInfo ) & FC_XONXOFF) != 0) ;
  1038.  
  1039.    dcb.fInX = dcb.fOutX = bSet ;
  1040.    dcb.XonChar = ASCII_XON ;
  1041.    dcb.XoffChar = ASCII_XOFF ;
  1042.    dcb.XonLim = 100 ;
  1043.    dcb.XoffLim = 100 ;
  1044.  
  1045.    // other various settings
  1046.  
  1047.    dcb.fBinary = TRUE ;
  1048.    dcb.fParity = TRUE ;
  1049.    dcb.fRtsDisable = FALSE ;
  1050.    dcb.fDtrDisable = FALSE ;
  1051.  
  1052.    fRetVal = !(SetCommState( &dcb ) < 0) ;
  1053.  
  1054.    return ( fRetVal ) ;
  1055.  
  1056. } // end of SetupConnection()
  1057.  
  1058. //---------------------------------------------------------------------------
  1059. //  BOOL NEAR CloseConnection( HWND hWnd )
  1060. //
  1061. //  Description:
  1062. //     Closes the connection to the port.  Resets the connect flag
  1063. //     in the TTYINFO struct.
  1064. //
  1065. //  Parameters:
  1066. //     HWND hWnd
  1067. //        handle to TTY window
  1068. //
  1069. //---------------------------------------------------------------------------
  1070.  
  1071. BOOL NEAR CloseConnection( HWND hWnd )
  1072. {
  1073.    HMENU      hMenu ;
  1074.    NPTTYINFO  npTTYInfo ;
  1075.  
  1076.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  1077.       return ( FALSE ) ;
  1078.  
  1079.    // Disable event notification.  Using a NULL hWnd tells
  1080.    // the COMM.DRV to disable future notifications.
  1081.  
  1082.    EnableCommNotification( COMDEV( npTTYInfo ), NULL, -1, -1 ) ;
  1083.  
  1084.    // kill the focus
  1085.  
  1086.    KillTTYFocus( hWnd ) ;
  1087.  
  1088.    // drop DTR
  1089.  
  1090.    EscapeCommFunction( COMDEV( npTTYInfo ), CLRDTR ) ;
  1091.  
  1092.    // close comm connection
  1093.  
  1094.    CloseComm( COMDEV( npTTYInfo ) ) ;
  1095.    CONNECTED( npTTYInfo ) = FALSE ;
  1096.  
  1097.    // change the selectable items in the menu
  1098.  
  1099.    hMenu = GetMenu( hWnd ) ;
  1100.    EnableMenuItem( hMenu, IDM_DISCONNECT,
  1101.                    MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;
  1102.    EnableMenuItem( hMenu, IDM_CONNECT,
  1103.                    MF_ENABLED | MF_BYCOMMAND ) ;
  1104.  
  1105.    return ( TRUE ) ;
  1106.  
  1107. } // end of CloseConnection()
  1108.  
  1109. //---------------------------------------------------------------------------
  1110. //  int NEAR ReadCommBlock( HWND hWnd, LPSTR lpszBlock, int nMaxLength )
  1111. //
  1112. //  Description:
  1113. //     Reads a block from the COM port and stuffs it into
  1114. //     the provided block.
  1115. //
  1116. //  Parameters:
  1117. //     HWND hWnd
  1118. //        handle to TTY window
  1119. //
  1120. //     LPSTR lpszBlock
  1121. //        block used for storage
  1122. //
  1123. //     int nMaxLength
  1124. //        max length of block to read
  1125. //
  1126. //---------------------------------------------------------------------------
  1127.  
  1128. int NEAR ReadCommBlock( HWND hWnd, LPSTR lpszBlock, int nMaxLength )
  1129. {
  1130.    char       szError[ 10 ] ;
  1131.    int        nLength, nError ;
  1132.    NPTTYINFO  npTTYInfo ;
  1133.  
  1134.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  1135.       return ( FALSE ) ;
  1136.  
  1137.    nLength = ReadComm( COMDEV( npTTYInfo ), lpszBlock, nMaxLength ) ;
  1138.  
  1139.    if (nLength < 0)
  1140.    {
  1141.       nLength *= -1 ;
  1142.       while (nError = GetCommError( COMDEV( npTTYInfo ), NULL ))
  1143.       {
  1144.          if (DISPLAYERRORS( npTTYInfo ))
  1145.          {
  1146.             wsprintf( szError, "<CE-%d>", nError ) ;
  1147.             WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
  1148.          }
  1149.       }
  1150.    }
  1151.  
  1152.    return ( nLength ) ;
  1153.  
  1154. } // end of ReadCommBlock()
  1155.  
  1156. //---------------------------------------------------------------------------
  1157. //  BOOL NEAR WriteCommByte( HWND hWnd, BYTE bByte )
  1158. //
  1159. //  Description:
  1160. //     Writes a byte to the COM port specified in the associated
  1161. //     TTY info structure.
  1162. //
  1163. //  Parameters:
  1164. //     HWND hWnd
  1165. //        handle to TTY window
  1166. //
  1167. //     BYTE bByte
  1168. //        byte to write to port
  1169. //
  1170. //---------------------------------------------------------------------------
  1171.  
  1172. BOOL NEAR WriteCommByte( HWND hWnd, BYTE bByte )
  1173. {
  1174.    NPTTYINFO  npTTYInfo ;
  1175.  
  1176.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  1177.       return ( FALSE ) ;
  1178.  
  1179.    WriteComm( COMDEV( npTTYInfo ), (LPSTR) &bByte, 1 ) ;
  1180.  
  1181.    return ( TRUE ) ;
  1182.  
  1183. } // end of WriteCommByte()
  1184.  
  1185. //---------------------------------------------------------------------------
  1186. //  BOOL NEAR WriteTTYBlock( HWND hWnd, LPSTR lpBlock, int nLength )
  1187. //
  1188. //  Description:
  1189. //     Writes block to TTY screen.  Nothing fancy - just
  1190. //     straight TTY.
  1191. //
  1192. //  Parameters:
  1193. //     HWND hWnd
  1194. //        handle to TTY window
  1195. //
  1196. //     LPSTR lpBlock
  1197. //        far pointer to block of data
  1198. //
  1199. //     int nLength
  1200. //        length of block
  1201. //
  1202. //---------------------------------------------------------------------------
  1203.  
  1204. BOOL NEAR WriteTTYBlock( HWND hWnd, LPSTR lpBlock, int nLength )
  1205. {
  1206.    int        i ;
  1207.    NPTTYINFO  npTTYInfo ;
  1208.    RECT       rect ;
  1209.  
  1210.    if (NULL == (npTTYInfo = (NPTTYINFO) GetWindowWord( hWnd, GWW_NPTTYINFO )))
  1211.       return ( FALSE ) ;
  1212.  
  1213.    for (i = 0 ; i < nLength; i++)
  1214.    {
  1215.       switch (lpBlock[ i ])
  1216.       {
  1217.          case ASCII_BEL:
  1218.             // Bell
  1219.             MessageBeep( 0 ) ;
  1220.             break ;
  1221.  
  1222.          case ASCII_BS:
  1223.             // Backspace
  1224.             if (COLUMN( npTTYInfo ) > 0)
  1225.                COLUMN( npTTYInfo ) -- ;
  1226.             MoveTTYCursor( hWnd ) ;
  1227.             break ;
  1228.  
  1229.          case ASCII_CR:
  1230.             // Carriage return
  1231.             COLUMN( npTTYInfo ) = 0 ;
  1232.             MoveTTYCursor( hWnd ) ;
  1233.             if (!NEWLINE( npTTYInfo ))
  1234.                break;
  1235.  
  1236.             // fall through
  1237.  
  1238.          case ASCII_LF:
  1239.             // Line feed
  1240.             if (ROW( npTTYInfo )++ == MAXROWS - 1)
  1241.             {
  1242.                _fmemmove( (LPSTR) (SCREEN( npTTYInfo )),
  1243.                           (LPSTR) (SCREEN( npTTYInfo ) + MAXCOLS),
  1244.                           (MAXROWS - 1) * MAXCOLS ) ;
  1245.                _fmemset( (LPSTR) (SCREEN( npTTYInfo ) + (MAXROWS - 1) * MAXCOLS),
  1246.                          ' ', MAXCOLS ) ;
  1247.                InvalidateRect( hWnd, NULL, FALSE ) ;
  1248.                ROW( npTTYInfo )-- ;
  1249.             }
  1250.             MoveTTYCursor( hWnd ) ;
  1251.             break ;
  1252.  
  1253.          default:
  1254.             *(SCREEN( npTTYInfo ) + ROW( npTTYInfo ) * MAXCOLS +
  1255.                 COLUMN( npTTYInfo )) = lpBlock[ i ] ;
  1256.             rect.left = (COLUMN( npTTYInfo ) * XCHAR( npTTYInfo )) -
  1257.                         XOFFSET( npTTYInfo ) ;
  1258.             rect.right = rect.left + XCHAR( npTTYInfo ) ;
  1259.             rect.top = (ROW( npTTYInfo ) * YCHAR( npTTYInfo )) -
  1260.                        YOFFSET( npTTYInfo ) ;
  1261.             rect.bottom = rect.top + YCHAR( npTTYInfo ) ;
  1262.             InvalidateRect( hWnd, &rect, FALSE ) ;
  1263.  
  1264.             // Line wrap
  1265.             if (COLUMN( npTTYInfo ) < MAXCOLS - 1)
  1266.                COLUMN( npTTYInfo )++ ;
  1267.             else if (AUTOWRAP( npTTYInfo ))
  1268.                WriteTTYBlock( hWnd, "\r\n", 2 ) ;
  1269.             break;
  1270.       }
  1271.    }
  1272.    return ( TRUE ) ;
  1273.  
  1274. } // end of WriteTTYBlock()
  1275.  
  1276. //---------------------------------------------------------------------------
  1277. //  VOID NEAR GoModalDialogBoxParam( HINSTANCE hInstance,
  1278. //                                   LPCSTR lpszTemplate, HWND hWnd,
  1279. //                                   DLGPROC lpDlgProc, LPARAM lParam )
  1280. //
  1281. //  Description:
  1282. //     It is a simple utility function that simply performs the
  1283. //     MPI and invokes the dialog box with a DWORD paramter.
  1284. //
  1285. //  Parameters:
  1286. //     similar to that of DialogBoxParam() with the exception
  1287. //     that the lpDlgProc is not a procedure instance
  1288. //
  1289. //---------------------------------------------------------------------------
  1290.  
  1291. VOID NEAR GoModalDialogBoxParam( HINSTANCE hInstance, LPCSTR lpszTemplate,
  1292.                                  HWND hWnd, DLGPROC lpDlgProc, LPARAM lParam )
  1293. {
  1294.    DLGPROC  lpProcInstance ;
  1295.  
  1296.    lpProcInstance = (DLGPROC) MakeProcInstance( (FARPROC) lpDlgProc,
  1297.                                                 hInstance ) ;
  1298.    DialogBoxParam( hInstance, lpszTemplate, hWnd, lpProcInstance, lParam ) ;
  1299.    FreeProcInstance( (FARPROC) lpProcInstance ) ;
  1300.  
  1301. } // end of GoModalDialogBoxParam()
  1302.  
  1303. //---------------------------------------------------------------------------
  1304. //  BOOL FAR PASCAL __export AboutDlgProc( HWND hDlg, UINT uMsg,
  1305. //                                WPARAM wParam, LPARAM lParam )
  1306. //
  1307. //  Description:
  1308. //     Simulates the Windows System Dialog Box.
  1309. //
  1310. //  Parameters:
  1311. //     Same as standard dialog procedures.
  1312. //
  1313. //---------------------------------------------------------------------------
  1314.  
  1315. BOOL FAR PASCAL __export AboutDlgProc( HWND hDlg, UINT uMsg,
  1316.                               WPARAM wParam, LPARAM lParam )
  1317. {
  1318.    switch (uMsg)
  1319.    {
  1320.       case WM_INITDIALOG:
  1321.       {
  1322.          int          idModeString ;
  1323.          char         szBuffer[ MAXLEN_TEMPSTR ], szTemp[ MAXLEN_TEMPSTR ] ;
  1324.          DWORD        dwFreeMemory, dwWinFlags ;
  1325.          WORD         wFreeResources, wRevision, wVersion ;
  1326.  
  1327. #ifdef ABOUTDLG_USEBITMAP
  1328.          // if we are using the bitmap, hide the icon
  1329.  
  1330.          ShowWindow( GetDlgItem( hDlg, IDD_ABOUTICON ), SW_HIDE ) ;
  1331. #endif
  1332.          // sets up the version number for Windows
  1333.  
  1334.          wVersion = LOWORD( GetVersion() ) ;
  1335.          switch (HIBYTE( wVersion ))
  1336.          {
  1337.             case 10:
  1338.                wRevision = 1 ;
  1339.                break ;
  1340.  
  1341.             default:
  1342.                wRevision = 0 ;
  1343.                break;
  1344.          }
  1345.          wVersion &= 0xFF ;
  1346.  
  1347.          GetDlgItemText( hDlg, IDD_TITLELINE, szTemp, sizeof( szTemp ) ) ;
  1348.          wsprintf( szBuffer, szTemp, wVersion, wRevision ) ;
  1349.          SetDlgItemText( hDlg, IDD_TITLELINE, szBuffer ) ;
  1350.  
  1351.          // sets up version number for TTY
  1352.  
  1353.          GetDlgItemText( hDlg, IDD_VERSION, szTemp, sizeof( szTemp ) ) ;
  1354.          wsprintf( szBuffer, szTemp, VER_MAJOR, VER_MINOR, VER_BUILD ) ;
  1355.          SetDlgItemText( hDlg, IDD_VERSION, (LPSTR) szBuffer ) ;
  1356.  
  1357.          // get by-line
  1358.  
  1359.          LoadString( GETHINST( hDlg ), IDS_BYLINE, szBuffer,
  1360.                      sizeof( szBuffer ) ) ;
  1361.          SetDlgItemText( hDlg, IDD_BYLINE, szBuffer ) ;
  1362.  
  1363.          // set windows mode information
  1364.  
  1365.          dwWinFlags = GetWinFlags() ;
  1366.          if (dwWinFlags & WF_ENHANCED)
  1367.             idModeString = IDS_MODE_ENHANCED ;
  1368.          else if (dwWinFlags & WF_STANDARD)
  1369.             idModeString = IDS_MODE_STANDARD ;
  1370.          else if (dwWinFlags & WF_WLO)
  1371.             idModeString = IDS_MODE_WLO ;
  1372.          else
  1373.             idModeString = IDS_MODE_UNDEF ;
  1374.  
  1375.          LoadString( GETHINST( hDlg ), idModeString, szBuffer,
  1376.                      sizeof( szBuffer ) ) ;
  1377.          SetDlgItemText( hDlg, IDD_WINDOWSMODE, szBuffer ) ;
  1378.  
  1379.          // get free memory information
  1380.  
  1381.          dwFreeMemory = GetFreeSpace( 0 ) / 1024L ;
  1382.          GetDlgItemText( hDlg, IDD_FREEMEM, szTemp, sizeof( szTemp ) ) ;
  1383.          wsprintf( szBuffer, szTemp, dwFreeMemory ) ;
  1384.          SetDlgItemText( hDlg, IDD_FREEMEM, (LPSTR) szBuffer ) ;
  1385.  
  1386.          // get free resources information
  1387.  
  1388.          wFreeResources = GetFreeSystemResources( 0 ) ;
  1389.          GetDlgItemText( hDlg, IDD_RESOURCES, szTemp, sizeof( szTemp ) ) ;
  1390.          wsprintf( szBuffer, szTemp, wFreeResources ) ;
  1391.          SetDlgItemText( hDlg, IDD_RESOURCES, (LPSTR) szBuffer ) ;
  1392.       }
  1393.       return ( TRUE ) ;
  1394.  
  1395. #ifdef ABOUTDLG_USEBITMAP
  1396.       // used to paint the bitmap
  1397.  
  1398.       case WM_PAINT:
  1399.       {
  1400.          HBITMAP      hBitMap ;
  1401.          HDC          hDC, hMemDC ;
  1402.          PAINTSTRUCT  ps ;
  1403.  
  1404.          // load bitmap and display it
  1405.  
  1406.          hDC = BeginPaint( hDlg, &ps ) ;
  1407.          if (NULL != (hMemDC = CreateCompatibleDC( hDC )))
  1408.          {
  1409.             hBitMap = LoadBitmap( GETHINST( hDlg ),
  1410.                                   MAKEINTRESOURCE( TTYBITMAP ) ) ;
  1411.             hBitMap = SelectObject( hMemDC, hBitMap ) ;
  1412.             BitBlt( hDC, 10, 10, 64, 64, hMemDC, 0, 0, SRCCOPY ) ;
  1413.             DeleteObject( SelectObject( hMemDC, hBitMap ) ) ;
  1414.             DeleteDC( hMemDC ) ;
  1415.          }
  1416.          EndPaint( hDlg, &ps ) ;
  1417.       }
  1418.       break ;
  1419. #endif
  1420.  
  1421.       case WM_COMMAND:
  1422.          if ((WORD) wParam == IDD_OK)
  1423.          {
  1424.             EndDialog( hDlg, TRUE ) ;
  1425.             return ( TRUE ) ;
  1426.          }
  1427.          break;
  1428.    }
  1429.    return ( FALSE ) ;
  1430.  
  1431. } // end of AboutDlgProc()
  1432.  
  1433. //---------------------------------------------------------------------------
  1434. //  VOID NEAR FillComboBox( HINSTANCE hInstance, HWND hCtrlWnd, int nIDString,
  1435. //                          WORD NEAR *npTable, WORD wTableLen,
  1436. //                          WORD wCurrentSetting )
  1437. //
  1438. //  Description:
  1439. //     Fills the given combo box with strings from the resource
  1440. //     table starting at nIDString.  Associated items are
  1441. //     added from given table.  The combo box is notified of
  1442. //     the current setting.
  1443. //
  1444. //  Parameters:
  1445. //     HINSTANCE hInstance
  1446. //        handle to application instance
  1447. //
  1448. //     HWND hCtrlWnd
  1449. //        handle to combo box control
  1450. //
  1451. //     int nIDString
  1452. //        first resource string id
  1453. //
  1454. //     WORD NEAR *npTable
  1455. //        near point to table of associated values
  1456. //
  1457. //     WORD wTableLen
  1458. //        length of table
  1459. //
  1460. //     WORD wCurrentSetting
  1461. //        current setting (for combo box selection)
  1462. //
  1463. //---------------------------------------------------------------------------
  1464.  
  1465. VOID NEAR FillComboBox( HINSTANCE hInstance, HWND hCtrlWnd, int nIDString,
  1466.                         WORD NEAR *npTable, WORD wTableLen,
  1467.                         WORD wCurrentSetting )
  1468. {
  1469.    char  szBuffer[ MAXLEN_TEMPSTR ] ;
  1470.    WORD  wCount, wPosition ;
  1471.  
  1472.    for (wCount = 0; wCount < wTableLen; wCount++)
  1473.    {
  1474.       // load the string from the string resources and
  1475.       // add it to the combo box
  1476.  
  1477.       LoadString( hInstance, nIDString + wCount, szBuffer, sizeof( szBuffer ) ) ;
  1478.       wPosition = LOWORD( SendMessage( hCtrlWnd, CB_ADDSTRING, NULL,
  1479.                                        (LPARAM) (LPSTR) szBuffer ) ) ;
  1480.  
  1481.       // use item data to store the actual table value
  1482.  
  1483.       SendMessage( hCtrlWnd, CB_SETITEMDATA, (WPARAM) wPosition,
  1484.                    (LPARAM) (LONG) *(npTable + wCount) ) ;
  1485.  
  1486.       // if this is our current setting, select it
  1487.  
  1488.       if (*(npTable + wCount) == wCurrentSetting)
  1489.          SendMessage( hCtrlWnd, CB_SETCURSEL, (WPARAM) wPosition, NULL ) ;
  1490.    }
  1491.  
  1492. } // end of FillComboBox()
  1493.  
  1494. //---------------------------------------------------------------------------
  1495. //  BOOL NEAR SettingsDlgInit( HWND hDlg )
  1496. //
  1497. //  Description:
  1498. //     Puts current settings into dialog box (via CheckRadioButton() etc.)
  1499. //
  1500. //  Parameters:
  1501. //     HWND hDlg
  1502. //        handle to dialog box
  1503. //
  1504. //---------------------------------------------------------------------------
  1505.  
  1506. BOOL NEAR SettingsDlgInit( HWND hDlg )
  1507. {
  1508.    char       szBuffer[ MAXLEN_TEMPSTR ], szTemp[ MAXLEN_TEMPSTR ] ;
  1509.    NPTTYINFO  npTTYInfo ;
  1510.    WORD       wCount, wMaxCOM, wPosition ;
  1511.  
  1512.    if (NULL == (npTTYInfo = (NPTTYINFO) GET_PROP( hDlg, ATOM_TTYINFO )))
  1513.       return ( FALSE ) ;
  1514.  
  1515.    wMaxCOM = LOWORD( EscapeCommFunction( NULL, GETMAXCOM ) ) + 1 ;
  1516.  
  1517.    // load the COM prefix from resources
  1518.  
  1519.    LoadString( GETHINST( hDlg ), IDS_COMPREFIX, szTemp, sizeof( szTemp ) ) ;
  1520.  
  1521.    // fill port combo box and make initial selection
  1522.  
  1523.    for (wCount = 0; wCount < wMaxCOM; wCount++)
  1524.    {
  1525.       wsprintf( szBuffer, "%s%d", (LPSTR) szTemp, wCount + 1 ) ;
  1526.       SendDlgItemMessage( hDlg, IDD_PORTCB, CB_ADDSTRING, NULL,
  1527.                           (LPARAM) (LPSTR) szBuffer ) ;
  1528.    }
  1529.    SendDlgItemMessage( hDlg, IDD_PORTCB, CB_SETCURSEL,
  1530.                        (WPARAM) (PORT( npTTYInfo ) - 1), NULL ) ;
  1531.  
  1532.    // disable COM port combo box if connection has already been
  1533.    // established (e.g. OpenComm() already successful)
  1534.  
  1535.    EnableWindow( GetDlgItem( hDlg, IDD_PORTCB ), !CONNECTED( npTTYInfo ) ) ;
  1536.  
  1537.    // fill baud combo box and make initial selection
  1538.  
  1539.    FillComboBox( GETHINST( hDlg ), GetDlgItem( hDlg, IDD_BAUDCB ),
  1540.                  IDS_BAUD110, gawBaudTable,
  1541.                  sizeof( gawBaudTable ) / sizeof( WORD ),
  1542.                  BAUDRATE( npTTYInfo ) ) ;
  1543.  
  1544.    // fill data bits combo box and make initial selection
  1545.  
  1546.    for (wCount = 5; wCount < 9; wCount++)
  1547.    {
  1548.       wsprintf( szBuffer, "%d", wCount ) ;
  1549.       wPosition = LOWORD( SendDlgItemMessage( hDlg, IDD_DATABITSCB,
  1550.                                               CB_ADDSTRING, NULL,
  1551.                                               (LPARAM) (LPSTR) szBuffer ) ) ;
  1552.  
  1553.       // if current selection, tell the combo box
  1554.  
  1555.       if (wCount == (WORD)BYTESIZE( npTTYInfo ))
  1556.          SendDlgItemMessage( hDlg, IDD_DATABITSCB, CB_SETCURSEL,
  1557.                              (WPARAM) wPosition, NULL ) ;
  1558.    }
  1559.  
  1560.    // fill parity combo box and make initial selection
  1561.  
  1562.    FillComboBox( GETHINST( hDlg ), GetDlgItem( hDlg, IDD_PARITYCB ),
  1563.                  IDS_PARITYNONE, gawParityTable,
  1564.                  sizeof( gawParityTable ) / sizeof( WORD ),
  1565.                  PARITY( npTTYInfo ) ) ;
  1566.  
  1567.    // fill stop bits combo box and make initial selection
  1568.  
  1569.    FillComboBox( GETHINST( hDlg ), GetDlgItem( hDlg, IDD_STOPBITSCB ),
  1570.                  IDS_ONESTOPBIT, gawStopBitsTable,
  1571.                  sizeof( gawStopBitsTable ) / sizeof ( WORD ),
  1572.                  STOPBITS( npTTYInfo ) ) ;
  1573.  
  1574.    // initalize the flow control settings
  1575.  
  1576.    CheckDlgButton( hDlg, IDD_DTRDSR,
  1577.                    (FLOWCTRL( npTTYInfo ) & FC_DTRDSR) > 0 ) ;
  1578.    CheckDlgButton( hDlg, IDD_RTSCTS,
  1579.                    (FLOWCTRL( npTTYInfo ) & FC_RTSCTS) > 0 ) ;
  1580.    CheckDlgButton( hDlg, IDD_XONXOFF,
  1581.                    (FLOWCTRL( npTTYInfo ) & FC_XONXOFF) > 0 ) ;
  1582.  
  1583.    // other TTY settings
  1584.  
  1585.    CheckDlgButton( hDlg, IDD_AUTOWRAP, AUTOWRAP( npTTYInfo ) ) ;
  1586.    CheckDlgButton( hDlg, IDD_NEWLINE, NEWLINE( npTTYInfo ) ) ;
  1587.    CheckDlgButton( hDlg, IDD_LOCALECHO, LOCALECHO( npTTYInfo ) ) ;
  1588.  
  1589.    // control options
  1590.  
  1591.    CheckDlgButton( hDlg, IDD_USECNRECEIVE, USECNRECEIVE( npTTYInfo ) ) ;
  1592.  
  1593.    // disable Use CN_RECEIVE option if connection has already been
  1594.    // established (e.g. OpenComm() already successful)
  1595.  
  1596.    EnableWindow( GetDlgItem( hDlg, IDD_USECNRECEIVE ),
  1597.                  !CONNECTED( npTTYInfo ) ) ;
  1598.  
  1599.    CheckDlgButton( hDlg, IDD_DISPLAYERRORS, DISPLAYERRORS( npTTYInfo ) ) ;
  1600.  
  1601.    return ( TRUE ) ;
  1602.  
  1603. } // end of SettingsDlgInit()
  1604.  
  1605. //---------------------------------------------------------------------------
  1606. //  BOOL NEAR SelectTTYFont( HWND hDlg )
  1607. //
  1608. //  Description:
  1609. //     Selects the current font for the TTY screen.
  1610. //     Uses the Common Dialog ChooseFont() API.
  1611. //
  1612. //  Parameters:
  1613. //     HWND hDlg
  1614. //        handle to settings dialog
  1615. //
  1616. //---------------------------------------------------------------------------
  1617.  
  1618. BOOL NEAR SelectTTYFont( HWND hDlg )
  1619. {
  1620.    CHOOSEFONT  cfTTYFont ;
  1621.    NPTTYINFO   npTTYInfo ;
  1622.  
  1623.    if (NULL == (npTTYInfo = (NPTTYINFO) GET_PROP( hDlg, ATOM_TTYINFO )))
  1624.       return ( FALSE ) ;
  1625.  
  1626.    cfTTYFont.lStructSize    = sizeof( CHOOSEFONT ) ;
  1627.    cfTTYFont.hwndOwner      = hDlg ;
  1628.    cfTTYFont.hDC            = NULL ;
  1629.    cfTTYFont.rgbColors      = FGCOLOR( npTTYInfo ) ;
  1630.    cfTTYFont.lpLogFont      = &LFTTYFONT( npTTYInfo ) ;
  1631.    cfTTYFont.Flags          = CF_SCREENFONTS | CF_FIXEDPITCHONLY |
  1632.                               CF_EFFECTS | CF_INITTOLOGFONTSTRUCT ;
  1633.    cfTTYFont.lCustData      = NULL ;
  1634.    cfTTYFont.lpfnHook       = NULL ;
  1635.    cfTTYFont.lpTemplateName = NULL ;
  1636.    cfTTYFont.hInstance      = GETHINST( hDlg ) ;
  1637.  
  1638.    if (ChooseFont( &cfTTYFont ))
  1639.    {
  1640.      FGCOLOR( npTTYInfo ) = cfTTYFont.rgbColors ;
  1641.      ResetTTYScreen( GetParent( hDlg ), npTTYInfo ) ;
  1642.    }
  1643.  
  1644.    return ( TRUE ) ;
  1645.  
  1646. } // end of SelectTTYFont()
  1647.  
  1648. //---------------------------------------------------------------------------
  1649. //  BOOL NEAR SettingsDlgTerm( HWND hDlg )
  1650. //
  1651. //  Description:
  1652. //     Puts dialog contents into TTY info structure.
  1653. //
  1654. //  Parameters:
  1655. //     HWND hDlg
  1656. //        handle to settings dialog
  1657. //
  1658. //---------------------------------------------------------------------------
  1659.  
  1660. BOOL NEAR SettingsDlgTerm( HWND hDlg )
  1661. {
  1662.    NPTTYINFO  npTTYInfo ;
  1663.    WORD       wSelection ;
  1664.  
  1665.    if (NULL == (npTTYInfo = (NPTTYINFO) GET_PROP( hDlg, ATOM_TTYINFO )))
  1666.       return ( FALSE ) ;
  1667.  
  1668.    // get port selection
  1669.  
  1670.    PORT( npTTYInfo ) =
  1671.       LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_PORTCB,
  1672.                                           CB_GETCURSEL,
  1673.                                           NULL, NULL ) ) + 1 ) ;
  1674.    // get baud rate selection
  1675.  
  1676.    wSelection =
  1677.       LOWORD( SendDlgItemMessage( hDlg, IDD_BAUDCB, CB_GETCURSEL,
  1678.                                   NULL, NULL ) ) ;
  1679.    BAUDRATE( npTTYInfo ) =
  1680.       LOWORD( SendDlgItemMessage( hDlg, IDD_BAUDCB, CB_GETITEMDATA,
  1681.                                   (WPARAM) wSelection, NULL ) ) ;
  1682.  
  1683.    // get data bits selection
  1684.  
  1685.    BYTESIZE( npTTYInfo ) =
  1686.       LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_DATABITSCB,
  1687.                                           CB_GETCURSEL,
  1688.                                           NULL, NULL ) ) + 5 ) ;
  1689.  
  1690.    // get parity selection
  1691.  
  1692.    wSelection =
  1693.       LOWORD( SendDlgItemMessage( hDlg, IDD_PARITYCB, CB_GETCURSEL,
  1694.                                   NULL, NULL ) ) ;
  1695.    PARITY( npTTYInfo ) =
  1696.       LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_PARITYCB,
  1697.                                           CB_GETITEMDATA,
  1698.                                           (WPARAM) wSelection,
  1699.                                            NULL ) ) ) ;
  1700.  
  1701.    // get stop bits selection
  1702.  
  1703.    wSelection =
  1704.       LOWORD( SendDlgItemMessage( hDlg, IDD_STOPBITSCB, CB_GETCURSEL,
  1705.                                   NULL, NULL ) ) ;
  1706.    STOPBITS( npTTYInfo ) =
  1707.       LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_STOPBITSCB,
  1708.                                           CB_GETITEMDATA,
  1709.                                           (WPARAM) wSelection, NULL ) ) ) ;
  1710.  
  1711.    // get flow control settings
  1712.  
  1713.    FLOWCTRL( npTTYInfo ) = 0 ;
  1714.    if (IsDlgButtonChecked( hDlg, IDD_DTRDSR ))
  1715.       FLOWCTRL( npTTYInfo ) |= FC_DTRDSR ;
  1716.    if (IsDlgButtonChecked( hDlg, IDD_RTSCTS ))
  1717.       FLOWCTRL( npTTYInfo ) |= FC_RTSCTS ;
  1718.    if (IsDlgButtonChecked( hDlg, IDD_XONXOFF ))
  1719.       FLOWCTRL( npTTYInfo ) |= FC_XONXOFF ;
  1720.  
  1721.    // get other various settings
  1722.  
  1723.    AUTOWRAP( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_AUTOWRAP ) ;
  1724.    NEWLINE( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_NEWLINE ) ;
  1725.    LOCALECHO( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_LOCALECHO ) ;
  1726.  
  1727.    // control options
  1728.  
  1729.    USECNRECEIVE( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_USECNRECEIVE ) ;
  1730.    DISPLAYERRORS( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_DISPLAYERRORS ) ;
  1731.  
  1732.    return ( TRUE ) ;
  1733.  
  1734. } // end of SettingsDlgTerm()
  1735.  
  1736. //---------------------------------------------------------------------------
  1737. //  BOOL FAR PASCAL __export SettingsDlgProc( HWND hDlg, UINT uMsg,
  1738. //                                   WPARAM wParam, LPARAM lParam )
  1739. //
  1740. //  Description:
  1741. //     This handles all of the user preference settings for
  1742. //     the TTY.
  1743. //
  1744. //  Parameters:
  1745. //     same as all dialog procedures
  1746. //
  1747. //---------------------------------------------------------------------------
  1748.  
  1749. BOOL FAR PASCAL __export SettingsDlgProc( HWND hDlg, UINT uMsg,
  1750.                                  WPARAM wParam, LPARAM lParam )
  1751. {
  1752.    switch (uMsg)
  1753.    {
  1754.       case WM_INITDIALOG:
  1755.       {
  1756.          NPTTYINFO  npTTYInfo ;
  1757.  
  1758.          // get & save pointer to TTY info structure
  1759.  
  1760.          npTTYInfo = (NPTTYINFO) LOWORD( lParam ) ;
  1761.          SET_PROP( hDlg, ATOM_TTYINFO, (HANDLE) npTTYInfo ) ;
  1762.  
  1763.          return ( SettingsDlgInit( hDlg ) ) ;
  1764.       }
  1765.  
  1766.       case WM_COMMAND:
  1767.          switch ((WORD) wParam)
  1768.          {
  1769.             case IDD_FONT:
  1770.                return ( SelectTTYFont( hDlg ) ) ;
  1771.  
  1772.             case IDD_OK:
  1773.                // Copy stuff into structure
  1774.                SettingsDlgTerm( hDlg ) ;
  1775.                EndDialog( hDlg, TRUE ) ;
  1776.                return ( TRUE ) ;
  1777.  
  1778.             case IDD_CANCEL:
  1779.                // Just end
  1780.                EndDialog( hDlg, TRUE ) ;
  1781.                return ( TRUE ) ;
  1782.          }
  1783.          break;
  1784.  
  1785.       case WM_DESTROY:
  1786.          REMOVE_PROP( hDlg, ATOM_TTYINFO ) ;
  1787.          break ;
  1788.    }
  1789.    return ( FALSE ) ;
  1790.  
  1791. } // end of SettingsDlgProc()
  1792.  
  1793. //---------------------------------------------------------------------------
  1794. //  End of File: tty.c
  1795. //---------------------------------------------------------------------------
  1796.  
  1797.