home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / wmseri / wtty.c < prev   
Encoding:
C/C++ Source or Header  |  1993-06-05  |  3.5 KB  |  141 lines

  1. //
  2. //     W M S E R I A L - Windows Modem Example
  3. //
  4. // This is only part of a multiple source project.  To build the
  5. // executable please use the makefile provided or construct
  6. // a project file containing the files and settings listed in
  7. // PROJECT.TXT.
  8. //
  9. // Remember to modify the directories to reflect your configuration.
  10. //
  11. //                                                     - J A A
  12.  
  13. // WTTY.C
  14.  
  15. #include <windows.h>
  16. #include "wmserial.h"
  17.  
  18.  
  19. static HWND hTTYWnd;
  20. static int nCurrRow, nCurrCol;
  21. static int nRows, nCols;
  22. static int nXChar, nYChar;
  23.  
  24.  
  25.  
  26. void TTYInit(HWND hWnd)
  27.    {
  28.    HDC hDC;
  29.    TEXTMETRIC TextMetric;
  30.    RECT TTYRect;
  31.  
  32.    // TTY Window Handle
  33.    hTTYWnd = hWnd;
  34.  
  35.    // Window Position
  36.    nCurrRow = 0;
  37.    nCurrCol = 0;
  38.  
  39.    // Window Text Metrics
  40.    hDC = GetDC( hTTYWnd );
  41.    SelectObject( hDC,
  42.                  GetStockObject( OEM_FIXED_FONT ) );
  43.    GetTextMetrics( hDC, &TextMetric );
  44.    ReleaseDC( hTTYWnd, hDC );
  45.    nXChar = TextMetric.tmAveCharWidth;
  46.    nYChar = TextMetric.tmHeight +
  47.             TextMetric.tmExternalLeading;
  48.  
  49.    GetClientRect(hTTYWnd,&TTYRect);
  50.    nRows = (TTYRect.bottom - TTYRect.top)  /  nYChar;
  51.    nCols = (TTYRect.right - TTYRect.left) /  nXChar;
  52.  
  53.    return;
  54.    }
  55.  
  56.  
  57.  
  58.  
  59. void TTYWriteScreen( LPSTR lpOutString )
  60.    {
  61.    LPSTR lpCurrChar;
  62.    HDC hDC;
  63.  
  64.    // get a device context for the client area
  65.    hDC = GetDC( hTTYWnd );
  66.    SelectObject( hDC, GetStockObject( OEM_FIXED_FONT ) );
  67.  
  68.    // hide caret
  69.    HideCaret( hTTYWnd );
  70.  
  71.    for ( lpCurrChar = lpOutString; *lpCurrChar; lpCurrChar++ ) 
  72.        {
  73.        switch ( *lpCurrChar ) 
  74.            {
  75.  
  76.            // BACKSPACE
  77.            case '\b':
  78.                if (nCurrCol)
  79.                    nCurrCol--;
  80.                break;
  81.  
  82.            // CARRAIGE RETURN
  83.            case '\r':         
  84.                nCurrCol = 0;
  85.                break;
  86.  
  87.  
  88.  
  89.            // NEW LINE
  90.            case '\n':               
  91.                nCurrRow++;
  92.                if ( nCurrRow == ( nRows - 1 ) ) 
  93.                    {
  94.                    ValidateRect( hTTYWnd, NULL );
  95.                    ScrollWindow( hTTYWnd, 0, -nYChar, NULL, NULL );
  96.                    UpdateWindow( hTTYWnd );
  97.                    nCurrRow = nRows - 2;
  98.                    }
  99.                break;
  100.  
  101.  
  102.            // OTHERS...
  103.            default:    
  104.                TextOut( hDC, nCurrCol * nXChar, nCurrRow * nYChar,
  105.                        lpCurrChar, 1 );
  106.                nCurrCol++;
  107.                if ( nCurrCol == ( nCols - 1 ) ) 
  108.                    {
  109.                    // move to the next line
  110.                    nCurrCol = 0;
  111.                    nCurrRow++;
  112.  
  113.                    // check whether window needs to be scrolled
  114.                    if ( nCurrRow == ( nRows - 1 ) ) 
  115.                        {
  116.                        ValidateRect( hTTYWnd, NULL );
  117.                        ScrollWindow( hTTYWnd, 0, -nYChar, NULL, NULL );
  118.                        UpdateWindow( hTTYWnd );
  119.                        nCurrRow = nRows - 2;
  120.                        }
  121.                    }
  122.                break;
  123.            }
  124.        }
  125.  
  126.    // update the caret position and show it
  127.    SetCaretPos( nCurrCol * nXChar, nCurrRow * nYChar );
  128.    ShowCaret( hTTYWnd );
  129.  
  130.    // release the device context
  131.    ReleaseDC( hTTYWnd, hDC );
  132.    }
  133.  
  134.  
  135. void TTYSetFocus(void)
  136.    {
  137.    CreateCaret( hTTYWnd, NULL, nXChar, nYChar );
  138.    SetCaretPos( nCurrCol * nXChar, nCurrRow * nYChar );
  139.    ShowCaret( hTTYWnd );
  140.    }
  141.