//  testcout.cpp
//  Program to see how cout and cerr can be
//  directed to a window

#include    "stdhdr.h"
#include    "test_r.h"
#include    "testcout.h"

App local_app;

BOOL App::InitInstance ()
{
cerrbuf = new winstreambuf;
cerr = cerrbuf;
cerr.setf (ios::unitbuf);
coutbuf = new winstreambuf;
cout = coutbuf;
#if defined (_DEBUG)
    exit_code = 0;
    start.Checkpoint ();
#endif
m_pMainWnd = new CMainWindow ();
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();

cerr_window = new ostreamWnd ("cout/cerr");

cerrbuf->set_stream_window (cerr_window);
coutbuf->set_stream_window (cerr_window);
return TRUE;
}

int App::ExitInstance ()
{
#if defined (_DEBUG)
    end.Checkpoint ();
    if (difference.Difference (start, end))
        {
        difference.DumpStatistics ();
        difference.DumpAllObjectsSince ();
        set_exit_code (100);
        }
#endif
delete cerrbuf;
delete coutbuf;
#if defined (_DEBUG)
    return exit_code;
#else
    return 0;
#endif
}

CMainWindow::CMainWindow ()
{
VERIFY (LoadAccelTable ("MainAccelTable"));
VERIFY (Create (NULL, "Test iostream and windows",
        WS_OVERLAPPEDWINDOW, rectDefault, NULL,
        "MainMenu"));
}

BEGIN_MESSAGE_MAP (CMainWindow, CMDIFrameWnd)
    ON_WM_CLOSE ()
    ON_COMMAND (IDM_ABOUT, OnAbout)
    ON_COMMAND (IDM_EXIT, OnClose)
    ON_COMMAND (IDM_TEST, OnTest)
END_MESSAGE_MAP ()

void CMainWindow::OnClose ()
{
VERIFY (DestroyWindow ());
}

void CMainWindow::OnAbout ()
{
MessageBox ("cout/cerr test interface to iostream\n"
            "Copyright Singleton Systems Ltd, 1993",
            "About");
}

void CMainWindow::OnTest ()
{
static int count;
cerr << "This is a test cerr message.  ";
cerr << "count = " << count++;
cerr << "  Followed by another cerr message";
cout << "\nThis is some output via cout.  ";
cerr << "\nfollowed by some more cerr text on "
        "another line, designed ";
cout << "And some more cout output and endl" << endl;
cerr << "to more (cerr) than fill the buffer, "
        "which is 128 bytes long";
cerr << "\nOK";
cerr << endl;
}
