// tutorial.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "tutorial.h"

#include "mainfrm.h"
//#include "tutordoc.h"
#include "tutorvw.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTutorialApp
BEGIN_MESSAGE_MAP(CTutorialApp, CWinApp)
	//{{AFX_MSG_MAP(CTutorialApp)
	ON_COMMAND(ID_APP_EXIT, OnAppExit)
	ON_COMMAND(ID_APP_ALT_A, OnAppAltA)
	ON_COMMAND(ID_APP_ALT_B, OnAppAltB)
	ON_COMMAND(ID_APP_ALT_E, OnAppAltE)
	ON_COMMAND(ID_APP_ALT_F, OnAppAltF)
	//}}AFX_MSG_MAP
	// Standard file based document commands
//	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
//	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTutorialApp construction

CTutorialApp::CTutorialApp()
{                      
  m_bWndValidate=0;
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTutorialApp object

CTutorialApp NEAR theApp;

/////////////////////////////////////////////////////////////////////////////
// CTutorialApp initialization
BOOL CTutorialApp::PreTranslateMessage(MSG *pMsg)
{                    
 if(m_bWndValidate)
   {
	 if(((CMainFrame *)m_pMainWnd)->GetCase()==ID_CASE_FIRST_LOOK ||
	    ((CMainFrame *)m_pMainWnd)->GetCase()==ID_CASE_START      ||
	    ((CMainFrame *)m_pMainWnd)->GetCase()==ID_CASE_ADVANCE ) 
	    if(pMsg->message==WM_SYSKEYUP)
	       return 1;              
	 if(pMsg->wParam==VK_ESCAPE)
	   {  
	    if(pMsg->message==WM_KEYDOWN)                      
	      { 
	       OnAppExit();
	       return 1;}
	   }            
	
   }	
 return CWinApp::PreTranslateMessage(pMsg);      
}


BOOL CTutorialApp::InitInstance()
{
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.


	SetDialogBkColor();        // Set dialog background color to gray
	LoadStdProfileSettings();  // Load standard INI file options (including MRU)
	EnableVBX();               // Initialize VBX support
    
    DoWaitCursor(1);
 /*   
    if(LoadVBXFile("TABPRO11.VBX")>HINSTANCE_ERROR)
       UnloadVBXFile("TABPRO11.VBX");
    else
      {
       AfxMessageBox("Can not Load TABPRO11.VBX\nPlace the file on the path",
						MB_OK | MB_ICONEXCLAMATION);
		return FALSE;

      } 
*/          
     if(int(g_hExpInst = ::LoadLibrary("explain.dll"))<32)
     {
        AfxMessageBox("Can not find explain.dll library",MB_OK | MB_ICONEXCLAMATION);
        return FALSE;
     }             

     if(int(m_hLibrary = ::LoadLibrary("resource.dll"))<32)
     {
        AfxMessageBox("Can not find resource.dll library",MB_OK | MB_ICONEXCLAMATION);
        return FALSE;
     }             
       
     HINSTANCE hInst; 
     
     if(int(hInst = ::LoadLibrary("firstres.dll"))<32)
       {
        AfxMessageBox("Can not find firstres.dll library",MB_OK | MB_ICONEXCLAMATION);
        return FALSE;
       }   
     FreeLibrary(hInst);

     if(int(hInst = ::LoadLibrary("others.dll"))<32)
       {
        AfxMessageBox("Can not find others.dll library",MB_OK | MB_ICONEXCLAMATION);
        return FALSE;
       }
      FreeLibrary(hInst);
      
     if(int(hInst = ::LoadLibrary("memory.dll"))<32)
       {
        AfxMessageBox("Can not find memory.dll library",MB_OK | MB_ICONEXCLAMATION);
        return FALSE;
       }
      FreeLibrary(hInst);

     if(int(hInst = ::LoadLibrary("stack.dll"))<32)
       {
        AfxMessageBox("Can not find stack.dll library",MB_OK | MB_ICONEXCLAMATION);
        return FALSE;
       }
      FreeLibrary(hInst);
  
   // Testing dispaly mode                   
    CDC pDC;
    pDC.CreateDC("DISPLAY",0,0,0);
    int cx=pDC.GetDeviceCaps(HORZRES);
    pDC.DeleteDC();
    if(cx>650)    
      if(MessageBox(NULL,"Tutorial can run best in VGA model(640x480)!\n Would you want continue, yes or no ?",
                    "EasyPack/SLD Message",MB_OK))//MB_ICONINFORMATION|MB_YESNO)==IDNO)
        {            
         return FALSE;
        }         

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.
/*
	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,//TUTTYPE,
		RUNTIME_CLASS(CDocument),
		RUNTIME_CLASS(CMDIChildWnd),        // standard MDI child frame
		RUNTIME_CLASS(CTutorialView));
	AddDocTemplate(pDocTemplate);
*/  
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;

    if(*(((CMainFrame *)m_pMainWnd)->GetLoadFilePath())==0)
     {
      AfxMessageBox("Can not find prime.omf file.\nSet path with EasyPack/SLD work direction.");
      return FALSE;
     }

    m_pMainWnd->ShowWindow(SW_SHOW);
    m_pMainWnd->UpdateWindow();
        
    
    
	if (m_lpCmdLine[0] != '\0')
	{
		// TODO: add command line processing here
	}
                     
    m_bWndValidate=1;
    //DoWaitCursor(-1);
	return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CTutorialApp exit
BOOL CTutorialApp::ExitInstance()
{
  if(int(m_hLibrary)>=32)
     FreeLibrary((HINSTANCE)m_hLibrary);

  if(int(g_hExpInst)>=32)
     FreeLibrary(g_hExpInst);
     
  return CWinApp::ExitInstance();   
}
/////////////////////////////////////////////////////////////////////////////
// VB-Event registration
// (calls to AfxRegisterVBEvent will be placed here by ClassWizard)

//{{AFX_VBX_REGISTER_MAP()
	UINT NEAR VBN_TABPAGESHOWN = AfxRegisterVBEvent("TABPAGESHOWN");
	//UINT  WM_TIP_ACTIVE = RegisterWindowMessage("TIPACTIVE");
	UINT NEAR VBN_TABACTIVATE = AfxRegisterVBEvent("TABACTIVATE");
//}}AFX_VBX_REGISTER_MAP

/////////////////////////////////////////////////////////////////////////////
// CTutorialApp commands

void CTutorialApp::OnAppExit()
{
	// TODO: Add your command handler code here
    switch(((CMainFrame *)(m_pMainWnd))->GetCase())
	      {
	       case ID_CASE_START:  
	            ((CMainFrame *)m_pMainWnd)->OnExitProcess();
	            m_pMainWnd->SendMessage(WM_CLOSE,NULL,NULL);
	            return;       
	       case ID_CASE_FIRST_LOOK:
	            m_pMainWnd->SendMessage(WM_COMMAND,ID_FIRST_LOOK_EXIT,0);
	            return;   
	       case ID_CASE_ADVANCE:   
	            m_pMainWnd->SendMessage(WM_COMMAND,ID_EXIT_ADVANCE,0);
	            return;
	       case ID_CASE_SET_MAP:
	            m_pMainWnd->SendMessage(WM_COMMAND,ID_CASE2_EXIT,0);
	            return;
	       default:
	            return;
	       
	      }
	
}

void CTutorialApp::OnAppAltA()
{
	// TODO: Add your command handler code here      
	switch(((CMainFrame *)(m_pMainWnd))->GetCase())
	 {
	  case 0:
	       m_pMainWnd->SendMessage(WM_COMMAND,ID_CASE0_ADVANCE,0);
	       return;
	  default:                                        
	       ::DefWindowProc(m_pMainWnd->GetSafeHwnd(),WM_SYSKEYDOWN,65,0L);
	       return;
	       
	 }

	
}

void CTutorialApp::OnAppAltB()
{
	// TODO: Add your command handler code here      
	switch(((CMainFrame *)(m_pMainWnd))->GetCase())
	 {
	  case 0:
	       m_pMainWnd->SendMessage(WM_COMMAND,ID_CASE0_BASIC,0);
	       return;
	  default:
	       ::DefWindowProc(m_pMainWnd->GetSafeHwnd(),WM_SYSKEYDOWN,65+('B'-'A'),0L);
	       return;
	       
	 }

	
}

void CTutorialApp::OnAppAltE()
{
	// TODO: Add your command handler code here
	switch(((CMainFrame *)(m_pMainWnd))->GetCase())
	 {
	  case 0:
	       m_pMainWnd->SendMessage(WM_COMMAND,ID_APP_EXIT,0);
	       return;
	  default:                  
	       ::DefWindowProc(m_pMainWnd->GetSafeHwnd(),WM_SYSKEYDOWN,65+('E'-'A'),0L);
	       return;
	       
	 }

}

void CTutorialApp::OnAppAltF()
{
	// TODO: Add your command handler code here
	switch(((CMainFrame *)(m_pMainWnd))->GetCase())
	 {
	  case ID_CASE_START:
	       m_pMainWnd->SendMessage(WM_COMMAND,ID_CASE0_FIRST_LOOK,0);
	       return;
	  default:
	       m_pMainWnd->SendMessage(WM_SYSCOMMAND,SC_KEYMENU,0);                                     
	       ::DefWindowProc(m_pMainWnd->GetSafeHwnd(),WM_SYSKEYDOWN,65+('F'-'A'),0L);
	       return;
	       
	 }
}
