/*****************************************************************************

    testapp.c

    Main Program file.

    This file contains an example test application for WINPIPE.DLL and
    stdio.exe.

    This program's operation is described in winpipe.wri.


*****************************************************************************/

#include <windows.h>
#include "winpipe.h"
#include "testapp.h"


#define PIPE_IN 10

#define toupper(a)  ( ( (a >= 'a') && (a <= 'z') )  ? (a - ('a'-'A')) : a)


PIPE	Pipe;

HANDLE  hThisInstance;
HANDLE  hThisWnd;

char szAppName[]="Entry";


#define DEBUG_STRLEN	120

char szDebug[DEBUG_STRLEN];  /*	Place for debug commands    */
static int iDebug=0;


/*  Quick'n easy dialog routine */

BOOL PopupDLG( hParentWnd, lpszTemplate, lpfnDlgProc )
HWND        hParentWnd;
LPSTR       lpszTemplate;
FARPROC     lpfnDlgProc;
{
    BOOL           bResult;
    FARPROC        lpProc;                 

    lpProc = MakeProcInstance( lpfnDlgProc, INSTANCE(hParentWnd) );

    bResult = DialogBox( INSTANCE(hParentWnd), lpszTemplate, hParentWnd, lpProc );          

    FreeProcInstance( lpProc );

    return( bResult );

}

/*  Aboutbox proc   */

BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
    switch (message) {
        case WM_INITDIALOG:
      return (TRUE);

        case WM_COMMAND:
            switch(wParam){
                case IDOK:
                    EndDialog(hDlg, TRUE);
                    break;

                case IDCANCEL:
                    EndDialog(hDlg, TRUE);
                    break;

                default:
                    break;

            }

    }
    return (FALSE);
}


/*  See if szString matches szTemplate */

match(char *szString, char *szTemplate)
{
    while(*szTemplate){
        if(toupper(*szString) != *szTemplate)
            return(0);
        szString++;
        szTemplate++;

    }

    return(1);

}

/*  Parse debugging dialog  */

Parse(char *szString)
{
    char buffer[120];

    if(match(szString, "WHO:"))
        Wputs("Testapp.exe\n");

    if(match(szString, "WHAT:"))
        Wputs("Test of Winpipes and Stdio\n");

    if(match(szString, "STAT:")){
        wsprintf((LPSTR)buffer, (LPSTR) "status:\niDebug=%d\nPipe=%d\nStdin=%d\n", iDebug, Pipe, Stdin);
        Wputs((LPSTR)buffer);
    }

}

/*  interact with debugging dialog from stdio */

Debugger()  /*	called on a WM_USER, wNotify	*/
{
    int c;
    while((c=Wgetc()) >= 0){			/*  non-error	*/
	Wputc((char) c);                        /*  echo to terminal */
	szDebug[iDebug] = (char)c;		/*  save strlen */
	switch(c){
	    case    13:                         /*  enter           */
	    case    10:                         /*  line feed       */
		szDebug[iDebug] = 0;            /*  Null terminate	*/
		Parse(szDebug);
		iDebug=0;		        /*  Reset next	*/
                break;

            case    8:                          /*  Backspace   */
                iDebug--;
                iDebug = (iDebug <= 0) ? 0 : iDebug;
                break;

            default:                            /*  every other character */
                iDebug++;
	        iDebug = (iDebug >= DEBUG_STRLEN) ? (DEBUG_STRLEN - 1): iDebug;
                break;

	}

    }

}



/*  Winproc for testapp */


long FAR PASCAL WndProc(hWnd, message, wParam, lParam)
HWND hWnd;
unsigned message;
WORD wParam;
LONG lParam;
{

    switch (message) {
        case WM_COMMAND:
	    switch(wParam){

		case IDM_ABOUT:
		    Wputs("About Box Call\n");
		    PopupDLG(hWnd, "AboutBox", About);
                    break;

		default:
		    Wputs("Unused WM_COMMAND\n");
                    return (DefWindowProc(hWnd, message, wParam, lParam));
            }
            break;

	case WM_DESTROY:
	    ReleasePipe(Stdin);
	    Wputs("WM_DESTROY\n");
	    PostQuitMessage(0);
	    break;

	case	WM_USER:
	    if(wParam == PIPE_IN)
		Debugger();
	    break;


	case	WM_CHAR:
	    if(wParam == 13)
		Wputc(10);
	    else
		Wputc(wParam);
	    break;

	case	WM_CREATE:
	    if(OpenPipe(hWnd, "Stdin", PIPE_READ, PIPE_IN)<0)
                Wputs("Can't open pipe\n");

	    Wputs("WM_CREATE\n");

	    return (DefWindowProc(hWnd, message, wParam, lParam));
            break;

	case	WM_PAINT:
	    Wputs("Paint Message\n");

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

}


/*  Winmain for testapp */


int PASCAL WinMain(hInst, hPrevInstance, lpszCmdLine, nCmdShow)
    HANDLE  hInst;
    HANDLE  hPrevInstance;
    LPSTR   lpszCmdLine;
    int     nCmdShow;
    {
        MSG         msg;
        BOOL        bStat;

        hThisInstance = hInst;

        if(!hPrevInstance){
            WNDCLASS    wc;
            wc.style            =   CS_HREDRAW | CS_VREDRAW;
            wc.lpfnWndProc      =   WndProc;
            wc.cbClsExtra       =   0;
            wc.cbWndExtra       =   0;
            wc.hInstance        =   hInst;
            wc.hIcon            =   LoadIcon(hInst,"testIcon");
            wc.hCursor          =   LoadCursor (NULL, IDC_ARROW);
            wc.hbrBackground    =   GetStockObject(WHITE_BRUSH);
            wc.lpszMenuName     =   "Testio";
            wc.lpszClassName    =   "stdio_test";

            if(!RegisterClass(&wc))
                return(FALSE);

	    }


    hThisWnd = CreateWindow ("stdio_test", "Testapp", WS_OVERLAPPEDWINDOW, 25, 25, 300, 200, NULL, NULL, hInst, NULL);

    ShowWindow(hThisWnd, nCmdShow);
    UpdateWindow(hThisWnd);


    while(GetMessage(&msg, NULL, 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        }

    return(msg.wParam);

}
