MTTTY.C 27 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
/*-----------------------------------------------------------------------------
    This is a part of the Microsoft Source Code Samples. 
    Copyright (C) 1995 Microsoft Corporation.
    All rights reserved. 
    This source code is only intended as a supplement to 
    Microsoft Development Tools and/or WinHelp documentation.
    See these sources for detailed information regarding the 
    Microsoft samples programs.

    MODULE: mttty.c

    PURPOSE: Program entry point and window management

    FUNCTIONS:
        WinMain            - 程序入口点
        VersionCheck       - 检查操作系统版本以确保我们可以运行
        InitializeApp      - 全局程序初始化和窗口类创建
        MTTTYWndProc       - 主窗口程序
        CmdDispatch        - 执行菜单命令
        OpenTTYChildWindow - 创建tty子窗口
        ScrollTTYVert      - 垂直滚动TTY窗口
        ScrollTTYHorz      - 水平滚动TTY窗口
        PaintTTY           - 在TTY窗口中绘制
        MoveTTYCursor      - 移动tty光标
        SetTTYFocus        - 响应tty窗口获得焦点
        KillTTYFocus       - 响应tty窗口失去焦点
        SizeTTY            - 响应tty窗口大小的变化
        TTYChildProc       - 用于TTY子窗口的窗口程序

-----------------------------------------------------------------------------*/

#include <windows.h>
#include "mttty.h"

/*
    Prototypes for functions called only within this file
*/
BOOL InitializeApp( HINSTANCE, int);
int WINAPI MTTTYWndProc( HWND, UINT, WPARAM, LPARAM );
int WINAPI TTYChildProc( HWND, UINT, WPARAM, LPARAM );
void CmdDispatch( int, HWND, LPARAM );
void OpenTTYChildWindow( HWND );
BOOL ScrollTTYVert( HWND, WORD, WORD );
BOOL ScrollTTYHorz( HWND, WORD, WORD );
BOOL VersionCheck();
BOOL PaintTTY( HWND );


/*-----------------------------------------------------------------------------

FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
 
PURPOSE: Start application and process all window messages

PARAMETERS:
    hInstance     - this apps hinstance
    hPrevInstance - previous instance of this app - always NULL
    lpCmdLine     - command line parameters
    nCmdShow      - code for showing window

RETURN:
    1 for success
    0 for failure to start app

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

/*-----------------------------------------------------------------------------*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    MSG msg;

    if (!VersionCheck()) {
        MessageBox(NULL, "BlueFlashTool can't run on this version of Windows.", NULL, MB_OK);
        return 0;
    }

    if (!InitializeApp(hInstance, nShowCmd)) {
        MessageBox(NULL, "BlueFlashTool couldn't start!", NULL, MB_OK);
        return 0;
    }

    while (GetMessage(&msg, NULL, 0, 0)) {
        if (!TranslateAccelerator( ghwndMain, ghAccel, &msg )) {
            TranslateMessage( &msg ) ;
            DispatchMessage( &msg ) ;
        }
    }

    return 1;
}


/*-----------------------------------------------------------------------------

FUNCTION: VersionCheck(void)

PURPOSE: Verifies that the correct version of Windows is running

RETURN:
    TRUE  - success version for running this app
    FALSE - correct version not verified

HISTORY:   Date:      Author:     Comment:
           11/20/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
BOOL VersionCheck()
{
    gOSV.dwOSVersionInfoSize = sizeof(gOSV);
    if (!GetVersionEx(&gOSV))
        return FALSE;

    if (gOSV.dwPlatformId == VER_PLATFORM_WIN32s)
        return FALSE;

    return TRUE ;
}


/*-----------------------------------------------------------------------------

FUNCTION: InitializeApp(HINSTANCE, int)

PURPOSE: GlobalInitialize, Register window classes 
         and create main window

PARAMETERS:
    hInst    - HINSTANCE of this app
    nShowCmd - code for showing this window

RETURN:
    TRUE  - successful inititialization of this app
    FALSE - failure to init app

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
BOOL InitializeApp(HINSTANCE hInst, int nShowCmd)
{
    WNDCLASS wc = {0};

    GlobalInitialize();     // get all global variables initialized to defaults

    //
    // setup program's main window class
    //
    wc.lpfnWndProc      = (WNDPROC) MTTTYWndProc;
    wc.hInstance        = hInst;
    wc.hIcon            = LoadIcon(hInst,MAKEINTRESOURCE(IDI_APPICON));
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.lpszMenuName     = MAKEINTRESOURCE(IDR_MTTTYMENU);
    wc.hbrBackground    = (HBRUSH) (COLOR_WINDOW + 1) ;
    wc.lpszClassName    = "MTTTYClass";

    if (!RegisterClass(&wc)) {
        GlobalCleanup();
        return FALSE;
    }

	QuerySerialPortStatusEx(TTYInfo.unOpenedPortAry,2,255);
	TTYInfo.rgbBGColor = RGB(255,255,255);

    //
    // setup program's tty child window class
    //
    wc.lpfnWndProc      = (WNDPROC) TTYChildProc;
    wc.hInstance        = hInst;
    wc.hCursor          = LoadCursor(NULL, IDC_IBEAM);
    wc.hbrBackground    = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszClassName    = "MTTTYChildClass";
    wc.lpszMenuName     = NULL;
    wc.hIcon            = NULL;

    if (!RegisterClass(&wc)) {
        GlobalCleanup();
        return FALSE;
    }

    //
    // create main window
    //
    ghwndMain = CreateWindow("MTTTYClass", "BlueFlashTool  鼎维尔科技",
            WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
            STARTXWINDOW, STARTYWINDOW,
            MAXXWINDOW, MAXYWINDOW,
            NULL, NULL, hInst, NULL);

    if (ghwndMain == NULL) {
        GlobalCleanup();
        return FALSE;
    }

    ShowWindow( ghwndMain, nShowCmd ) ;   
    UpdateWindow( ghwndMain ) ;

    ghInst = hInst;
    ghAccel = LoadAccelerators( hInst, MAKEINTRESOURCE( IDR_MTTTYACCELERATOR) ) ;

    return TRUE;
}


/*-----------------------------------------------------------------------------

FUNCTION: MTTTYWndProc(HWND, UINT, WPARAM, LPARAM)

PURPOSE: Window Procedure for main window

PARAMETERS:
    hwnd    - window handle
    message - window message
    wParam  - window message parameter (depends on message)
    lParam  - window message parameter (depends on message)

RETURN:
    If message is process, return value is 0
    If message is not processed, then it is passed to DefWindowProc
    and the return value from that function is returned
    
HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) 
    {
        case WM_CREATE:
            //
            // since main window is created, I can now open all other windows
            //
            InitTTYInfo();
            OpenTTYChildWindow(hwnd);
            OpenSettingsToolbar(hwnd);
            OpenStatusToolbar(hwnd);
            ChangeConnection(hwnd, TTYInfo.fConnected);
            break;

        case WM_DESTROY: 
            //
            // since main windows is being destroyed, so same to other windows
            //
            DestroyTTYInfo();
            DestroyWindow(ghWndToolbarDlg);
            DestroyWindow(ghWndStatusDlg);
            DestroyWindow(ghWndTTY);
				
            GlobalCleanup();
            PostQuitMessage(0);
            break;

        case WM_GETMINMAXINFO:
            {
                //
                // make sure that main window doesn't get smaller than
                // the minimum child windows.
                //
                LPMINMAXINFO lpTemp;
                POINT        ptTemp;

                lpTemp = (LPMINMAXINFO) lParam;
      
                ptTemp.x = (long) lpTemp->ptMinTrackSize.x;
                ptTemp.y = (long) gcyMinimumWindowHeight;

                lpTemp->ptMinTrackSize = ptTemp;
            }
        
            break;

        case WM_SIZE:
            {
                //
                // main window size has changed,
                // so I need to change the positions of child windows
                //
                WORD wTop;
                WORD wHeight;
                WORD wWidth = LOWORD(lParam);

                //
                // put Settings window at top
                //
                wHeight = SETTINGSFACTOR*gwBaseY;
                wTop = 0;
                MoveWindow(ghWndToolbarDlg, 0,wTop, wWidth, wHeight, TRUE);

                //
                // put Status window at bottom
                //
                wHeight = STATUSFACTOR*gwBaseY;
                wTop = HIWORD(lParam) - wHeight;
                MoveWindow(ghWndStatusDlg, 0, wTop, wWidth, wHeight, TRUE);

                //
                // put TTTY window right in the middle
                // height = whole window - height of two previous windows
                //
                wHeight = HIWORD(lParam) - ((STATUSFACTOR + SETTINGSFACTOR)*gwBaseY);
                wTop = SETTINGSFACTOR*gwBaseY;
                MoveWindow(ghWndTTY, 0, wTop, wWidth, wHeight, TRUE);
            }
         
            break;

      case WM_COMMAND:       
            CmdDispatch(LOWORD(wParam), hwnd, lParam);        
            break;

      case WM_CHAR:
            SetFocus(ghWndTTY);
            SendMessage(ghWndTTY, WM_CHAR, wParam, lParam);
            break;

      case WM_CLOSE:
            if (DisconnectOK()) {
                if (TTYInfo.fConnected) {
                    if (TTYInfo.fTransferring)
                        TransferFileTextEnd();
                    BreakDownCommPort();
                }
                DestroyWindow(hwnd);
            }
            break;

      default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }

    return 0L;
}

/*-----------------------------------------------------------------------------

FUNCTION: CmdDispatch(int, HWND)

PURPOSE: Responds to menu selections

PARAMETERS:
    iMenuChoice - ID of menu choice (from resource file)
    hwnd        - window handle of menu owner

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam)
{
    static char szFileName[MAX_PATH] = {0};

    switch (iMenuChoice) 
    {
        case ID_HELP_ABOUTMTTTY:
            CmdAbout(hwnd);
            break;

        case ID_TRANSFER_SENDFILETEXT:
            {
				if (strlen(TTYInfo.szFileName) == 0)
				{
					char * szFilter = "Text Files\0*.*\0";
					OPENFILENAME ofn = {0};

					ofn.lStructSize = sizeof(OPENFILENAME);
					ofn.hwndOwner = hwnd;
					ofn.lpstrFilter = szFilter;
					ofn.lpstrFile = szFileName;
					ofn.nMaxFile = MAX_PATH;
					ofn.lpstrTitle = "Send File";
					ofn.Flags = OFN_FILEMUSTEXIST;

					if (!GetOpenFileName(&ofn))
						break;
					strcpy(TTYInfo.szFileName,szFileName);				}

                
                if (TRUE)
                    TransferFileTextStart(TTYInfo.szFileName);
            }
            break;

        case ID_TRANSFER_RECEIVEFILETEXT:
            {
                char * szFilter = "Text Files\0*.TXT\0";
                OPENFILENAME ofn = {0};

                ofn.lStructSize = sizeof(OPENFILENAME);
                ofn.hwndOwner = hwnd;
                ofn.lpstrFilter = szFilter;
                ofn.lpstrFile = szFileName;
                ofn.nMaxFile = MAX_PATH;
                ofn.lpstrTitle = "Receive File";
                ofn.Flags = OFN_OVERWRITEPROMPT;

                if (!GetSaveFileName(&ofn))
                    break;

                ReceiveFileText(szFileName);
            }
            break;

        case ID_TRANSFER_ABORTSENDING:
            // was abort sent from the abort button?
            if (LOWORD(lParam) == IDC_ABORTBTN) {
                // am I in a transfer repeat?
                if (TTYInfo.fRepeating)
                    TransferRepeatDestroy();
                // am I in a normal recieve state, then stop sending
                else if (gdwReceiveState == RECEIVE_TTY)
                    TransferFileTextEnd();
                // if I am not in a normal receive state, then stop capturing
                else
                    gfAbortTransfer = TRUE;
            }
            else
                // transfer abort was sent by transfer thread   
                TransferFileTextEnd();
            break;

        case ID_TRANSFER_SENDREPEATEDLY:
            {
                DWORD dwFreq;
                char * szFilter = "Text Files\0*.TXT\0";
                OPENFILENAME ofn = {0};

                ofn.lStructSize = sizeof(OPENFILENAME);
                ofn.hwndOwner = hwnd;
                ofn.lpstrFilter = szFilter;
                ofn.lpstrFile = szFileName;
                ofn.nMaxFile = MAX_PATH;
                ofn.lpstrTitle = "Send File Repeatedly";
                ofn.Flags = OFN_FILEMUSTEXIST;

                if (!GetOpenFileName(&ofn))
                    break;

                dwFreq = GetAFrequency();
                
                TransferRepeatCreate(szFileName, dwFreq);
            }
            break;

        case ID_TRANSFER_ABORTREPEATEDSENDING:
            TransferRepeatDestroy();
            break;

        case ID_TTY_CLEAR:
            ClearTTYContents();
            InvalidateRect(ghWndTTY, NULL, TRUE);
            break;

        // The following correspond to menu choices and buttons in the settings dlog
        case IDC_FONTBTN:
        case IDC_COMMEVENTSBTN:
        case IDC_FLOWCONTROLBTN:
        case IDC_TIMEOUTSBTN:
            SendMessage(ghWndToolbarDlg, WM_COMMAND, (WPARAM) iMenuChoice, (LPARAM) GetDlgItem(ghWndToolbarDlg, iMenuChoice));
            break;

        case ID_FILE_CONNECT:
            if (SetupCommPort() != NULL)
                ChangeConnection(hwnd, TTYInfo.fConnected);
            break;

        case ID_FILE_DISCONNECT:
            if (BreakDownCommPort())
                ChangeConnection(hwnd, TTYInfo.fConnected);
            break;

        case ID_FILE_EXIT:
            PostMessage(hwnd, WM_CLOSE, 0, 0);
            break;
    }
    return;
}


/*-----------------------------------------------------------------------------

FUNCTION: OpenTTYChildWindow(HWND)

PURPOSE: Creates the TTY Child Window

PARAMETERS:
    hWnd - parent window handle of TTY child window

COMMENTS: This window is actually the TTY Screen

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

/*-----------------------------------------------------------------------------*/
void OpenTTYChildWindow(HWND hWnd)
{
    ghWndTTY = CreateWindow( "MTTTYChildClass", "TTY Window", 
                             WS_CHILD | WS_VISIBLE | WS_VSCROLL,
                             0,0,
                             0,0,
                             hWnd, (HMENU)ID_TTYWINDOW, ghInst, NULL);
    if (ghWndTTY == NULL)
        ErrorReporter("Can't Create TTY Child Window");

    return;
}


//---------------------------------------------------------------------------
//  BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
//
//  Description:
//     Scrolls TTY window vertically.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//     WORD wScrollCmd
//        type of scrolling we're doing
//
//     WORD wScrollPos
//        scroll position
//
//  History:   Date       Author      Comment
//              5/ 8/91   BryanW      Wrote it.
//             10/27/95   AllenD      Included it for MTTTY Sample.
//
//---------------------------------------------------------------------------
BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
{
   int  nScrollAmt ;

   switch (wScrollCmd)
   {
      case SB_TOP:
         nScrollAmt = -TTYInfo.yOffset ;
         break ;

      case SB_BOTTOM:
         nScrollAmt = TTYInfo.yScroll - TTYInfo.yOffset ;
         break ;

      case SB_PAGEUP:
         nScrollAmt = -TTYInfo.ySize ;
         break ;

      case SB_PAGEDOWN:
       nScrollAmt = TTYInfo.ySize ;
         break ;

      case SB_LINEUP:
         nScrollAmt = -TTYInfo.yChar ;
         break ;

      case SB_LINEDOWN:
         nScrollAmt = TTYInfo.yChar ;
         break ;

      case SB_THUMBPOSITION:
         nScrollAmt = wScrollPos - TTYInfo.yOffset ;
         break ;

      default:
         return ( FALSE ) ;
   }

   if ((TTYInfo.yOffset + nScrollAmt) > TTYInfo.yScroll)
      nScrollAmt = TTYInfo.yScroll - TTYInfo.yOffset ;

   if ((TTYInfo.yOffset + nScrollAmt) < 0)
      nScrollAmt = -TTYInfo.yOffset ;

   ScrollWindowEx( hWnd, 0, -nScrollAmt, NULL, NULL, NULL, NULL, SW_INVALIDATE | SW_ERASE) ;

   TTYInfo.yOffset = TTYInfo.yOffset + nScrollAmt ;

   SetScrollPos( hWnd, SB_VERT, TTYInfo.yOffset, TRUE ) ;

   return ( TRUE ) ;

} // end of ScrollTTYVert()

//---------------------------------------------------------------------------
//  BOOL NEAR ScrollTTYHorz( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
//
//  Description:
//     Scrolls TTY window horizontally.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//     WORD wScrollCmd
//        type of scrolling we're doing
//
//     WORD wScrollPos
//        scroll position
//
//  History:   Date       Author      Comment
//              5/ 8/91   BryanW      Wrote it.
//             10/27/95   AllenD      Included it for MTTTY Sample.
//
//---------------------------------------------------------------------------
BOOL NEAR ScrollTTYHorz( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
{
   int  nScrollAmt ;

   switch (wScrollCmd)
   {
      case SB_TOP:
         nScrollAmt = -TTYInfo.xOffset ;
         break ;

      case SB_BOTTOM:
         nScrollAmt = TTYInfo.xScroll - TTYInfo.xOffset ;
         break ;

      case SB_PAGEUP:
         nScrollAmt = -TTYInfo.xSize ;
         break ;

      case SB_PAGEDOWN:
         nScrollAmt = TTYInfo.xSize ;
         break ;

      case SB_LINEUP:
         nScrollAmt = -TTYInfo.xChar ;
         break ;

      case SB_LINEDOWN:
         nScrollAmt = TTYInfo.xChar ;
         break ;

      case SB_THUMBPOSITION:
         nScrollAmt = wScrollPos - TTYInfo.xOffset ;
         break ;

      default:
         return ( FALSE ) ;
   }
   if ((TTYInfo.xOffset + nScrollAmt) > TTYInfo.xScroll)
      nScrollAmt = TTYInfo.xScroll - TTYInfo.xOffset ;
   if ((TTYInfo.xOffset + nScrollAmt) < 0)
      nScrollAmt = -TTYInfo.xOffset ;
   ScrollWindowEx( hWnd, -nScrollAmt, 0, NULL, NULL, NULL, NULL, SW_INVALIDATE | SW_ERASE) ;
   TTYInfo.xOffset = TTYInfo.xOffset + nScrollAmt ;
   SetScrollPos( hWnd, SB_HORZ, TTYInfo.xOffset, TRUE ) ;

   return ( TRUE ) ;

} // end of ScrollTTYHorz()

//---------------------------------------------------------------------------
//  BOOL NEAR PaintTTY( HWND hWnd )
//
//  Description:
//     Paints the rectangle determined by the paint struct of
//     the DC.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window (as always)
//
//  History:   Date       Author      Comment
//              5/ 9/91   BryanW      Wrote it.
//             10/22/91   BryanW      Problem with background color
//                                    and "off by one" fixed.
//
//              2/25/92   BryanW      Off-by-one not quite fixed...
//                                    also resolved min/max problem
//                                    for windows extended beyond
//                                    the "TTY display".
//
//             10/27/95   AllenD      Included it for MTTTY Sample.
//
//---------------------------------------------------------------------------
BOOL NEAR PaintTTY( HWND hWnd )
{
   PAINTSTRUCT  ps ;
   HFONT        hOldFont ;
   RECT         rect ;
   HDC          hDC ;
   int          nRow, nCol, nEndRow, nEndCol;
   int          nCount, nHorzPos, nVertPos ;

   hDC = BeginPaint( hWnd, &ps ) ;
   hOldFont = SelectObject( hDC, TTYInfo.hTTYFont ) ;
   SetTextColor( hDC, TTYInfo.rgbFGColor ) ;
   SetBkColor( hDC, TTYInfo.rgbBGColor ) ;
   rect = ps.rcPaint ;
   nRow =
      min( MAXROWS - 1,
           max( 0, (rect.top + TTYInfo.yOffset) / TTYInfo.yChar ) ) ;
   nEndRow =
      min( MAXROWS - 1,
           ((rect.bottom + TTYInfo.yOffset - 1) / TTYInfo.yChar ) ) ;
   nCol =
      min( MAXCOLS - 1,
           max( 0, (rect.left + TTYInfo.xOffset) / TTYInfo.xChar ) ) ;
   nEndCol =
      min( MAXCOLS - 1,
           ((rect.right + TTYInfo.xOffset - 1) / TTYInfo.xChar ) ) ;
   nCount = nEndCol - nCol + 1 ;
   for (; nRow <= nEndRow; nRow++)
   {
      nVertPos = (nRow * TTYInfo.yChar) - TTYInfo.yOffset ;
      nHorzPos = (nCol * TTYInfo.xChar) - TTYInfo.xOffset ;
      rect.top = nVertPos ;
      rect.bottom = nVertPos + TTYInfo.yChar ;
      rect.left = nHorzPos ;
      rect.right = nHorzPos + TTYInfo.xChar * nCount ;
      SetBkMode( hDC, TRANSPARENT ) ;
      ExtTextOut( hDC, nHorzPos, nVertPos, ETO_OPAQUE | ETO_CLIPPED, &rect,
                  (LPSTR)( TTYInfo.Screen + nRow * MAXCOLS + nCol ),
                  nCount, NULL ) ;
   }
   SelectObject( hDC, hOldFont ) ;
   EndPaint( hWnd, &ps ) ;
   MoveTTYCursor( hWnd ) ;
   return ( TRUE ) ;

} // end of PaintTTY()

//---------------------------------------------------------------------------
//  BOOL NEAR MoveTTYCursor( HWND hWnd )
//
//  Description:
//     Moves caret to current position.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//  History:   Date       Author      Comment
//              5/ 9/91   BryanW      Wrote it.
//             10/27/95   AllenD      Included it for MTTTY Sample.
//
//---------------------------------------------------------------------------
BOOL NEAR MoveTTYCursor( HWND hWnd )
{
   if (TTYInfo.fConnected && (TTYInfo.wCursorState& CS_SHOW))
      SetCaretPos( (TTYInfo.nColumn * TTYInfo.xChar) -
                   TTYInfo.xOffset,
                   (TTYInfo.nRow * TTYInfo.yChar) -
                   TTYInfo.yOffset ) ;
   
   return ( TRUE ) ;

} // end of MoveTTYCursor()

//---------------------------------------------------------------------------
//  BOOL NEAR SetTTYFocus( HWND hWnd )
//
//  Description:
//     Sets the focus to the TTY window also creates caret.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//  History:   Date       Author      Comment
//              5/ 9/91   BryanW      Wrote it.
//             10/27/95   AllenD      Included it for MTTTY Sample.
//
//---------------------------------------------------------------------------
BOOL NEAR SetTTYFocus( HWND hWnd )
{
   if (TTYInfo.fConnected && (TTYInfo.wCursorState!= CS_SHOW) )
   {
      CreateCaret( hWnd, NULL, TTYInfo.xChar, TTYInfo.yChar ) ;
      ShowCaret( hWnd ) ;
      TTYInfo.wCursorState= CS_SHOW ;
   }

   MoveTTYCursor( hWnd ) ;
   return ( TRUE ) ;

} // end of SetTTYFocus()

//---------------------------------------------------------------------------
//  BOOL NEAR KillTTYFocus( HWND hWnd )
//
//  Description:
//     Kills TTY focus and destroys the caret.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//  History:   Date       Author      Comment
//              5/ 9/91   BryanW      Wrote it.
//             10/27/95   AllenD      Included it for MTTTY Sample.
//
//---------------------------------------------------------------------------
BOOL NEAR KillTTYFocus( HWND hWnd )
{
   if (TTYInfo.wCursorState!= CS_HIDE)
   {
      HideCaret( hWnd ) ;
      DestroyCaret() ;
      TTYInfo.wCursorState= CS_HIDE ;
   }
   return ( TRUE ) ;

} // end of KillTTYFocus()


//---------------------------------------------------------------------------
//  BOOL NEAR SizeTTY( HWND hWnd, WORD wVertSize, WORD wHorzSize )
//
//  Description:
//     Sizes TTY and sets up scrolling regions.
//
//  Parameters:
//     HWND hWnd
//        handle to TTY window
//
//     WORD wVertSize
//        new vertical size
//
//     WORD wHorzSize
//        new horizontal size
//
//  History:   Date       Author      Comment
//              5/ 8/ 91  BryanW      Wrote it
//             10/27/95   AllenD      Included it for MTTTY Sample.
//
//---------------------------------------------------------------------------
BOOL NEAR SizeTTY( HWND hWnd, WORD wWidth, WORD wHeight )
{
   int nScrollAmt ;

   //
   // adjust vert settings
   //
   TTYInfo.ySize = (int) wHeight ;
   TTYInfo.yScroll = max( 0, (MAXROWS * TTYInfo.yChar) -
                               TTYInfo.ySize ) ;
   nScrollAmt = min( TTYInfo.yScroll, TTYInfo.yOffset ) -
                     TTYInfo.yOffset ;
   ScrollWindow( hWnd, 0, -nScrollAmt, NULL, NULL ) ;

   TTYInfo.yOffset = TTYInfo.yOffset + nScrollAmt ;
   SetScrollPos( hWnd, SB_VERT, TTYInfo.yOffset, FALSE ) ;
   SetScrollRange( hWnd, SB_VERT, 0, TTYInfo.yScroll, TRUE ) ;

   //
   // adjust horz settings
   //
   TTYInfo.xSize = (int) wHeight ;
   TTYInfo.xScroll = max( 0, (MAXCOLS * TTYInfo.xChar) -
                                TTYInfo.xSize ) ;
   nScrollAmt = min( TTYInfo.xScroll, TTYInfo.xOffset) -
                     TTYInfo.xOffset ;
   ScrollWindow( hWnd, nScrollAmt, 0, NULL, NULL );
   TTYInfo.xOffset = TTYInfo.xOffset + nScrollAmt ;
   SetScrollRange( hWnd, SB_HORZ, 0, TTYInfo.xScroll, FALSE ) ;
   SetScrollPos( hWnd, SB_HORZ, TTYInfo.xOffset, TRUE ) ;

   InvalidateRect( hWnd, NULL, FALSE ) ;   // redraw entire window

   return ( TRUE ) ;

} // end of SizeTTY()

/*-----------------------------------------------------------------------------

FUNCTION: TTYChildProc(HWND, UINT, WPARAM, LPARAM)

PURPOSE: Window Procedure to process message for the TTY Child Window

PARAMETERS:
    hwnd    - window handle
    message - window message
    wParam  - window message parameter (depends on message)
    lParam  - window message parameter (depends on message)

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
int WINAPI TTYChildProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
    switch(uMessage)
    {
        case WM_VSCROLL:
            ScrollTTYVert( hWnd, LOWORD( wParam ), HIWORD( wParam ) ) ;
            break ;

        case WM_HSCROLL:
            ScrollTTYHorz( hWnd, LOWORD( wParam ), HIWORD( wParam ) ) ;
            break ;

        case WM_SIZE:
            SizeTTY(hWnd, LOWORD(lParam), HIWORD(lParam));
            break;

        case WM_PAINT:
            PaintTTY(hWnd);
            break;

        case WM_CHAR:
            {
                //
                // keyboard activity in TTY Window 
                //
                if (TTYInfo.fConnected) {

                    if (!WriterAddNewNode(WRITE_CHAR, 0, (char) wParam, NULL, NULL, NULL))
                        return FALSE;

                    if (TTYInfo.fLocalEcho)
                        OutputABufferToWindow(ghWndTTY, (CHAR *) &wParam, 1);
                }
            }
            break;

        case WM_SETFOCUS:
            SetTTYFocus( ghWndTTY ) ;
            break ;

        case WM_KILLFOCUS:
            KillTTYFocus( ghWndTTY ) ;
            break ;

        case WM_MOUSEACTIVATE:
            /*
                If mouse is clicked in me (the tty child window)
                then I need to get the focus.
            */
            SetFocus(hWnd);
            return MA_ACTIVATE;
            break;

        default:
            return DefWindowProc(hWnd, uMessage, wParam, lParam);
    }
    return 0L;
}