// protovr.cpp : main source file for protovr.exe
//

#include "stdafx.h"

#include <atlframe.h>

#include "resource.h"
#include "mainfrm.h"


CExeModule _Module;     // reserved name, DO NOT change


int CExeModule::Run(LPTSTR pCmdLine, int )
{
    HRESULT hr    = S_OK;
    int nExitCode = -1;
    m_IsResults   = false; // no results initially are available

 /*
    m_TaskObject = NULL;
    
    try
    {
        m_TaskObject->ProcessMessage(NULL, WM_CLOSE, 0, 0, NULL);
    } 
    catch(CException* e) 
    {
        int ret = e->ReportError();
        e->Delete();
    }
*/
    if (FAILED(hr = LoadPreferences()))
    {
        ATLTRACE(_T("CExeModule::LoadPreferences() failed. Program terminated.\n"
                    "Error code %08lx"), hr);
#if !defined(_DEBUG)
        Error((LPCTSTR)IDS_E_PREFERENCE, hr);
#endif
        return nExitCode;
    }

    CMainFrame MainWindow;
    CAppMessageLoop loop;

    do
    {
        _Module.AddMessageLoop(&loop);

        RECT rc = {10, 10, 640, 480};

        if (MainWindow.CreateEx(NULL, rc) == NULL)
        {
            ATLTRACE(_T("Main window creation failed.\n"));
            nExitCode = -1;
            break;
        }

        nExitCode = Setup();
        ATLTRACE(_T("CExeModule::Setup() exit code %d\n"), nExitCode);

        if (IDOK != nExitCode)
        {
            MainWindow.DestroyWindow();
            _Module.RemoveMessageLoop();
            break;
        }

        if (FAILED(hr = Initialize(MainWindow)))
        {
            ATLTRACE(_T("CExeModule::Initialize() failed. Program terminated.\n"
                        "Error code %08lx"), hr);
#if !defined(_DEBUG)
            Error((LPCTSTR)IDS_E_INITIALIZE);
#endif
            MainWindow.DestroyWindow();
            break;
        }
#if defined(_DEBUG)
//#if 0
        else
            MainWindow.ShowWindow(SW_SHOW);
#endif
        // @NOTE: just a case. we begin test session 
        //        after successful initialization
        m_IsTestStarted = true;

        loop.Run();

        Terminate();
        _Module.RemoveMessageLoop();

    } while (true);

    return nExitCode;
}


int WINAPI _tWinMain( HINSTANCE hInstance
                    , HINSTANCE /* hPrevInstance */
                    , LPTSTR lpszCmdLine
                    , int nCmdShow )
{
    INITCOMMONCONTROLSEX iccx;
    iccx.dwSize = sizeof(iccx);
    iccx.dwICC = ICC_WIN95_CLASSES;     // to support status bar and up-down controls
    BOOL bRet = ::InitCommonControlsEx(&iccx);
    ATLASSERT(bRet);

#if defined(_ATL_EXCEPTIONS)
#pragma message(">>> _ATL_EXCEPTIONS Used")
    TkInitialize();
#endif

 
    HRESULT hr = _Module.Init(NULL, hInstance);
    ATLASSERT(SUCCEEDED(hr));


    int nExitCode = _Module.Run(lpszCmdLine, nCmdShow);

    _Module.Term();

#if defined(_ATL_EXCEPTIONS)
    TkTerminate();
#endif

    return nExitCode;
}
