// mainfrm.cpp : implementation of the CMainFrame class
//

#define  __cplusplus
#include "stdafx.h"
#include "tutorial.h"
#include "direct.h"
#include "contact.h"

#include "tmouse.h"
#include "mainfrm.h"    
#include "srcwnd.h"

#include "dos.h"

/*
#ifndef _SPEAKER_H
#define _SPEAKER_H
#include "speaker.h"
#endif _SPEAKER_H
*/

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

BOOL       g_bSBOpen;
BOOL       g_mbCanDoNext;
BOOL       g_bCanDoExit; 
BOOL       g_bAuto=0;
BOOL       g_bAutoBtn=1;
UINT       g_uTick=0;
HINSTANCE  g_hExpInst=0;
extern void Delay(int nDelay,BOOL bShow=0);

FARPROC lpClientProc=0;            
LONG  CALLBACK _export WndProc(HWND ,WORD ,WORD ,LONG );
void BrushFirstBk(HWND hWnd);
BOOL g_bBasicFirst=1; 
//const UINT  TM_TIP_ACTIVE = RegisterWindowMessage("TM_TIP_ACTIVE");
void  OVERMSGBOX::SetPaintRect(CRect r,BOOL bNewCase /*= 1*/)
{     
  rectOld =(((rectOld.Width()==0)&&(rectOld.Height()==0))||bNewCase) ? r : rectDes;
  rectDes = r;
}                


/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_MESSAGE(TM_TIP_ACTIVE, OnTMTIPActive)
	ON_MESSAGE(TM_WIN_CHANGE,OnWinChange)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_NCPAINT()
	ON_WM_TIMER()
	ON_WM_DESTROY()
    ON_WM_NCLBUTTONDOWN()
	ON_WM_NCLBUTTONUP()
	ON_WM_LBUTTONUP()
	ON_WM_NCLBUTTONDBLCLK()
	ON_WM_NCACTIVATE()
	ON_WM_CLOSE()
	ON_WM_MOUSEMOVE()
	ON_WM_NCMOUSEMOVE()
	ON_COMMAND(ID_CASE0_BASIC, OnCase0Basic)
	ON_COMMAND(ID_CASE0_FIRST_LOOK, OnCase0FirstLook)
	ON_COMMAND(ID_CASE0_ADVANCE, OnCase0Advance)
	ON_WM_LBUTTONDOWN()
	ON_WM_ACTIVATE()
	ON_WM_MOUSEACTIVATE()
	ON_COMMAND(ID_CASE2_EXIT, OnCase2Exit)
	ON_COMMAND(ID_NEXT,OnNext)     
	ON_WM_SETFOCUS()
	ON_WM_INITMENUPOPUP()
	ON_COMMAND(ID_GROUP_MAP, OnGroupMap)
	ON_COMMAND(ID_FILE_LOAD, OnFileLoad)
	ON_COMMAND(ID_SHOW_SRC,OnShowSrcWnd)
	ON_COMMAND(ID_LOAD_STATUS , OnLoadStatus)
	ON_COMMAND(ID_WINDOW_VARIABLE, OnWindowVariable)
	ON_COMMAND(ID_START,OnStartBtn)
	ON_COMMAND(ID_BTN_START,OnStartCmd)
	ON_COMMAND(ID_FIRLOOK_EXIT,OnFirLookExt)
	ON_COMMAND_EX(ID_FIRST_SOURCE,OnShowFLook)
	ON_COMMAND(ID_STOP_DOME,OnStopDome)
	ON_COMMAND(ID_NEXT_PLAY,OnNextPlay)
	ON_COMMAND(ID_AUTO_PLAY,OnAutoPlay)
	ON_WM_CTLCOLOR()
	ON_WM_SETCURSOR()
	ON_COMMAND(ID_EXIT_ADVANCE,OnExitAdvance)
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_TRACE,OnTrace)
	ON_COMMAND(ID_MEMORY,OnMemory)
	ON_COMMAND(ID_CANCEL,OnCancel)
	ON_COMMAND(ID_FIRST_LOOK_EXIT,OnFirLookExt)
	ON_COMMAND_EX(ID_FIRST_MEMORY,OnShowFLook)
	ON_COMMAND_EX(ID_FIRST_BITMEMORY,OnShowFLook)
	ON_COMMAND_EX(ID_FIRST_STACK,OnShowFLook)
	ON_COMMAND_EX(ID_FIRST_VAR,OnShowFLook)
	ON_COMMAND_EX(ID_FIRST_PER,OnShowFLook)
	ON_COMMAND_EX(ID_FIRST_TRACE,OnShowFLook)
	ON_COMMAND_EX(ID_FIRST_SHELL,OnShowFLook)
	ON_COMMAND_EX(ID_FIRST_CPU,OnShowFLook)
	ON_COMMAND_EX(ID_FIRST_AUTO,OnShowFLook)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////////                                            
////for use message 
HBRUSH CMainFrame::OnCtlColor(CDC *pDC,CWnd *pWnd,UINT nCtl)
{                              
 /*
 if(pWnd==m_pExplainEdit && (nCtl==CTLCOLOR_EDIT ||nCtl==CTLCOLOR_MSGBOX))
   {                          
    if(nCtl==CTLCOLOR_EDIT)
      {          
       pDC->SetTextColor(RGB(0,0,255));
       pDC->SetBkColor(RGB(0,128,128));
      }    
    
    return CreateSolidBrush(RGB(0,128,128));  
   } 
  */ 
 return CMDIFrameWnd::OnCtlColor(pDC,pWnd,nCtl);  
}

LONG CMainFrame::OnTMTIPActive(UINT wParam, LONG )
{  
 m_bTipActive = 1;//wParam == 0 ? 0:1;
 return 1;
}           

LONG CMainFrame::OnWinChange(UINT wParam, LONG )                                         
{
     m_wndToolBar.Del();   
    
    // Add New Tool Bar
    if(wParam) 
    {
        switch(wParam) 
        {
            case WID_SOURCE:
                m_wndToolBar.NewSource();
                break;
            case WID_CPU:
                m_wndToolBar.NewCpu();
                break;
            case WID_PERI:
                m_wndToolBar.NewPeri();
                break;
            case WID_SHELL:
                m_wndToolBar.NewShell();
                break;               
            case WID_BMEM:
                m_wndToolBar.NewBMem();
                break;               
            case WID_TRACE:
                m_wndToolBar.NewTrace();
                break;               
            case WID_STACK:
                m_wndToolBar.NewStack();
                break;               
            case WID_MEMORY:
                m_wndToolBar.NewMemory();
                break;               
            case WID_VARIABLE:
                m_wndToolBar.NewVariable();
                break;               
            default:
                m_wndToolBar.NewMain();
                break;               
        }                                  
    } else m_wndToolBar.NewMain();
    
    return 1;
}


/////////////////////////////////////////////////////////////////////////////
// arrays of IDs used to initialize control bars
// toolbar buttons - IDs are command buttons
static UINT BASED_CODE indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

static UINT BASED_CODE buttons[40];      
static UINT BASED_CODE TipContents[40];  

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
void CMainFrame::InitItem()
{
	bPaint=0;
	bClose=0;
	bDrawBkg=0;
	bDrawBtn=0;
	bCase2Paint=0;
	bPaintMsg=1;
	bByUser=0;
	bBusy=0;
	g_mbCanDoNext=1;
	g_uTick=0;
	m_bUpArrow=1;
	m_uOldMsgMapID=0;
	m_pExitBtn=0;
	m_pCancelBtn=0;
    m_pNextBtn=0;
    m_uMenuAct=0;
    m_pWnd=0; 
    m_bTipActive=0;
    m_bPostToMain=0;
    m_bTimeActive=0;
    m_bCanPal=1;                 
    m_pMsgWnd=0;
    m_nCursorStep=8;
    bByUser=0;
    m_pDlg=0;
    m_pVarWnd=0;
    m_StopDome.m_hWnd = 0;
    m_NextBtn.m_hWnd  = 0;
    m_bPlayStop=0;
    m_hFlookInst=0;
    m_bSetMap_Action=1;
    m_bTrace=1;
    m_bMemory=1;
    g_bCanDoExit=1;
    g_bAuto=0;
    g_bAutoBtn=1;
    OverMsg.bOverMsg=0;
    StepMsg.bOverMsg=0;
    m_IconsClk=1;
    SetSrcWnd(0);
}

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
                 
//initial data	
    InitItem();
	m_uCase=ID_CASE_START;
//  find *.omf  
    _searchenv("prime.omf","PATH",m_szLoadFilePath);
    _fstrlwr(m_szLoadFilePath);  
//	Create(NULL,"EasyPack/SLD",WS_OVERLAPPEDWINDOW,rectDefault,NULL,
//	       MAKEINTRESOURCE(IDR_MAINFRAME));//WS_CAPTION|WS_SYSMENU);//WS_OVERLAPPEDWINDOW);
//	LoadAccelTable((LPCSTR)MAKEINTRESOURCE(IDR_MAINFRAME));
//	m_pAppMenu = new CMenu;
	m_pBitmap = new CBitmap;
	m_pBitmap->LoadBitmap(IDB_BITMAP5);
	m_pTBitmap = new CBitmap;
	m_pTBitmap->LoadBitmap(IDB_BITMAP6);
	hCursor1=AfxGetApp()->LoadCursor(IDC_CURSOR1);
	hCursor2=AfxGetApp()->LoadCursor(IDC_CURSOR2);
	hArrow=::LoadCursor(NULL,IDC_ARROW);
    
    m_pStoreBit =  new CBitmap;           
    StepMsg.pStoreBit = new CBitmap;
    OverMsg.rectOld.SetRect(0,0,0,0);
    StepMsg.rectOld.SetRect(0,0,0,0);
    SetOverMsgBox(FALSE);
    SetStepMsgBox(FALSE);           
    
    m_pStart    = new CBitmapButton;
    m_pBkGroundS= new CBitmap;
    m_pBkGroundM= new CBitmap;
    m_pMainMenu = new CMenu;
    m_pMainMenu->LoadMenu(IDR_MAINFRAME);
    g_bSBOpen = 1;
    g_bSBOpen =PlaySound((LPSTR)NULL);
}

CMainFrame::~CMainFrame()
{   delete m_pMainMenu;                  
    m_pBitmap->DeleteObject();
    delete m_pBitmap;
    m_pTBitmap->DeleteObject();
    delete m_pTBitmap;
    m_pStoreBit->DeleteObject();
    delete m_pStoreBit;  
    delete StepMsg.pStoreBit;
 // case 0     
    if(m_pBkGroundS)
       delete m_pBkGroundS;
    if(m_pBkGroundM)
       delete m_pBkGroundM;
       
    if(m_pStart)
      delete m_pStart;
    if(m_pExitBtn)
      delete m_pExitBtn;
    if(m_pCancelBtn)
      delete m_pCancelBtn;
    if(m_pNextBtn)
      delete m_pNextBtn;
  
}          

BOOL CMainFrame::MouseButtonDown(WORD Message,WORD wParam,LONG lParam,BOOL bCan/*=0*/)
{                 
 static BOOL bDo=1;  
 if(bCan) 
   {bDo=1;
    return 1;}
 if(g_mbCanDoNext==0)
    return 1;
 POINT point;
 point.x=LOWORD(lParam);
 point.y=HIWORD(lParam);
 ::ClientToScreen(m_hWndMDIClient,&point);
 CRect rect;
 switch(m_uCase)
  {
    case ID_CASE_START:
         m_pExitBtn->GetClientRect(&rect);
         m_pExitBtn->ClientToScreen(&rect);
         if(!ClickBtn(Message,wParam,lParam,rect,point,m_pExitBtn))
             return 0;
         break;   
    case ID_CASE_ADVANCE:      
         m_pStart->GetClientRect(&rect);
         m_pStart->ClientToScreen(&rect);
         if(m_pStart->IsWindowEnabled())
           if(ClickBtn(Message,wParam,lParam,rect,point,m_pStart))
             return 1;

         if(m_NextBtn.IsWindowVisible()&& g_bAuto==0)
           {m_NextBtn.GetClientRect(&rect);
            m_NextBtn.ClientToScreen(&rect);
            if(ClickBtn(Message,wParam,lParam,rect,point,&m_NextBtn))
               return 1;  
           }
                     
         if(m_Trace.IsWindowVisible()&& g_bAuto==0)
           {m_Trace.GetClientRect(&rect);
            m_Trace.ClientToScreen(&rect);
            if(ClickBtn(Message,wParam,lParam,rect,point,&m_Trace))
               return 1;  
           }           
         if(m_Memory.IsWindowVisible()&& g_bAuto==0)
           {m_Memory.GetClientRect(&rect);
            m_Memory.ClientToScreen(&rect);
            if(ClickBtn(Message,wParam,lParam,rect,point,&m_Memory))
               return 1;  
           }           
       
         break;
    case ID_CASE_FIRST_LOOK:
          if(m_pMenu95)
	        if(m_pMenu95->m_hWnd && 
	            m_pMenu95->IsWindowVisible())
	            {m_pMenu95->PostMessage(WM_COMMAND,IDCANCEL);
	             return 1;}

         if(WndIconAction(lParam))
            return 1;
         
         if(m_pStart==0)
            break;   
            
         m_pStart->GetClientRect(&rect);
         m_pStart->ClientToScreen(&rect);
         
         if(m_pStart->IsWindowEnabled())
           if(ClickBtn(Message,wParam,lParam,rect,point,m_pStart))
             return 1;
         if(m_StopDome.IsWindowVisible())
           {m_StopDome.GetClientRect(&rect);
            m_StopDome.ClientToScreen(&rect);
            if(ClickBtn(Message,wParam,lParam,rect,point,&m_StopDome))
               return 1;
           }
              
         if(m_NextBtn.IsWindowVisible())
           {m_NextBtn.GetClientRect(&rect);
            m_NextBtn.ClientToScreen(&rect);
            if(ClickBtn(Message,wParam,lParam,rect,point,&m_NextBtn))
               return 1;
           }           
         
         break;
              
    case ID_CASE_SET_MAP: 
         if(!g_mbCanDoNext || !m_bSetMap_Action)
            return 0;
         m_pNextBtn->GetClientRect(&rect);
         m_pNextBtn->ClientToScreen(&rect);
         if(!ClickBtn(Message,wParam,lParam,rect,point,m_pNextBtn))
           {
	         m_pCancelBtn->GetClientRect(&rect);
	         m_pCancelBtn->ClientToScreen(&rect);
	         if(!ClickBtn(Message,wParam,lParam,rect,point,m_pCancelBtn))
	             return 0;
           }
         break;
    default:
         return 0;   
  }     
 return 1; 
}

BOOL CMainFrame::ClickBtn(WORD Message,WORD wParam,DWORD lParam,CRect rect,POINT p,CButton *pBtn)
{   
 if(pBtn==0 )
    return 0;
 if(rect.PtInRect(p))
   {
    POINT point;
    point.x=rect.left+rect.Width()/2;
    point.y=rect.top+rect.Height()/2;
    pBtn->ScreenToClient(&point);
    if(pBtn->IsWindowEnabled())
       pBtn->PostMessage(Message,wParam,MAKELONG(point.x,point.y));
    return 1;
   } 
 PostMessage(Message,wParam,lParam);    
 return 0;  
} 

LONG CALLBACK _export WndProc(HWND hWnd,WORD Message,
                              WORD wParam,LONG lParam)
{               
 int nCaseAction=((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetCase();
 CWindowDC dc(AfxGetApp()->m_pMainWnd);
 PAINTSTRUCT PtSt;
 BOOL bPaint;
 switch(Message)
  {
   case WM_PAINT:   
         bPaint=
         ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetTipActive();
         switch(nCaseAction)
              {
               case ID_CASE_ADVANCE:
		       case ID_CASE_START:
		       case ID_CASE_FIRST_LOOK:
		       		::BeginPaint(hWnd,&PtSt);
   		            ::EndPaint(hWnd,&PtSt);
		            return 0L;    
		       case ID_CASE_SET_MAP:                  
		            CallWindowProc((WNDPROC)lpClientProc,hWnd,Message,
		                            wParam,lParam);   
		            if(!bPaint)                
		             ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->                
		             PaintCase2(dc);                           
		             ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->
		             SetTipActive(0);
		            return 0L;
		       default:  
		            CallWindowProc((WNDPROC)lpClientProc,hWnd,Message,
		                            wParam,lParam);
		            return 0L;

              }
          return 0L;  
   case WM_ERASEBKGND:
        switch(nCaseAction)
            {case ID_CASE_ADVANCE:
             case ID_CASE_START:
                  return 0L;
             case ID_CASE_FIRST_LOOK:
                  //BrushFirstBk(hWnd);
                  return 0L;
             default:
                  break;
            }
        break;           
   case WM_LBUTTONDOWN:
        (((CMainFrame *)(AfxGetApp()->m_pMainWnd))->
             MouseButtonDown(Message,wParam,lParam));
        return 0L; 
        //break; 
   case WM_MOUSEMOVE:
        AfxGetApp()->m_pMainWnd->PostMessage(Message,wParam,lParam);
        return 0L;
   default:
        break;
  }
 return CallWindowProc((WNDPROC)lpClientProc,hWnd,Message,wParam,lParam);       
}                                           

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{                       
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;                             
	
	WORD nStyle=GetClassWord(m_hWnd,GCW_STYLE);
	SetClassWord(m_hWnd,GCW_STYLE,nStyle|CS_DBLCLKS);
//Set Mouse Hook           
    AddMouseHook(NULL);//GetCurrentTask());
    AddKeyBoardHook(NULL);//GetCurrentTask());

    SetKey(1);
    SetMouse(1);
	FilterLMClk(0,0);

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE("Failed to create status bar\n");
		return -1;      // fail to create
	}
    m_wndStatusBar.ShowWindow(SW_HIDE);

	if (!m_wndToolBar.CreateBar(this,&m_wndStatusBar,TM_TIP_ACTIVE))
	                             
	   {
        TRACE("Failed to create tool bar");
        return -1;
       }
    m_wndToolBar.ShowWindow(SW_HIDE);
    
    lpClientProc=
    (FARPROC)SetWindowLong(m_hWndMDIClient,GWL_WNDPROC,(LONG)WndProc);
    //ASSERT(SetWindowLong(m_hWnd,GWL_EXSTYLE,WS_EX_TOPMOST));                
//  	MoveWindow(WinCxy.x,WinCxy.y,640,480);
    return 0;
}

void CMainFrame::OnDestroy()
{           
    SetWindowLong(m_hWndMDIClient,GWL_WNDPROC,(LONG)lpClientProc);
    KillTimer(IDAUTOBTN);
	CMDIFrameWnd::OnDestroy();
	
	// TODO: Add your message handler code here
	RemoveMouseHook();
	RemoveKeyBoardHook();
	//KillTimer(TIMER);
	
}


/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnPaint()
{          
    bPaint=1;// NcPaint happen ,perhaps main window not create
    CPaintDC  paintDC(this);
    CWindowDC dc(this);    

    switch(m_uCase)
      {                 
       case ID_CASE_FIRST_LOOK:
            PaintFirstLook(dc);
            return;
       case ID_CASE_ADVANCE:
            PaintAdvance(dc);
            return;
       case ID_CASE_START:
            PaintCase0(dc);
            return;
       default:
            //CMDIFrameWnd::OnPaint();
            return;

      }
 }                     
            
void CMainFrame::OnNcPaint()            
{ 
                  
 switch(m_uCase)
   {
    case ID_CASE_SET_MAP:
         CMDIFrameWnd::OnNcPaint();
         return;
    default:
         return;
   }  
}

void CMainFrame::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
 static BOOL bFlash=0;	
 static int  nAutoCase=0;    
 static DWORD lTickCount=0;

	CWindowDC dc(this); 
	switch(nIDEvent)
	{              
	  case AUTOPLAY:
	     {                  
	       //MessageBeep(-1);
	       KillTimer(AUTOPLAY);   
	       if(!g_bAuto){
	          return;}
	       nAutoCase++; 
	       switch(m_uCase)
	       {
	         case ID_CASE_FIRST_LOOK:
	              nAutoCase=0;
	              OnFirstLookExit();
	              break;
	         case ID_CASE_START:
	              if(nAutoCase==1)
   	                 PostMessage(WM_COMMAND,ID_CASE0_BASIC,0L);
   	              else if(nAutoCase==2)   
   	              {
   	                 PostMessage(WM_COMMAND,ID_CASE0_ADVANCE,0L);
   	                 nAutoCase=0;
   	              }            
   	           /*   
   	              else if(nAutoCase>=3)
   	              {
   	                 PostMessage(WM_COMMAND, ID_APP_EXIT);
   	                 g_bAuto=0;
   	                 nAutoCase=0;
   	              }
   	          */    
   	              break;   
	       }
	     }  
	       break;
	  case IDAUTOBTN:
		{                         
		   KillTimer(IDAUTOBTN);
		  /* 
		  if(!g_bAutoBtn)
		     KillTimer(IDAUTOBTN);
		  else   
		  {      
		    
	        if(bFlash)
		       DrawTPmap(dc,m_AutoBtn,IDB_AUTOC,IDB_AUTOM);
		    else   
	           DrawTPmap(dc,m_AutoBtn,IDB_AUTOC,IDB_AUTOM);
	        bFlash=~bFlash;   
	        
	      } 
	     */     
		     
		}     
		break;
	  case IDAUTO:
		{                   
		   if(!g_bAuto){
		      KillTimer(IDAUTO);
		      
		   }   
		   else
		   if( m_pNextBtn!=NULL && 
		       m_pNextBtn->IsWindowEnabled() &&
		        g_mbCanDoNext)
		   {            
		    lTickCount=GetTickCount();     
		    m_pNextBtn->SetState(1);
		    Delay(200);
		    m_pNextBtn->SetState(0);
		    if(m_uCase==ID_CASE_ADVANCE)
		       m_pNextBtn->EnableWindow(FALSE);
		    PostMessage(WM_COMMAND,ID_NEXT);
		    
		   } 
		} 
		break;
	  default:	 
	    {  
	      switch(m_uCase)
	      {
	       case ID_CASE_START:
	            Timer(nIDEvent);
	            break;
	       case ID_CASE_SET_MAP:
	            MenuTimer(nIDEvent);
	            break;            
	       case ID_CASE_ADVANCE:    
	            switch(nIDEvent)
	             { case MENUTIMER:
	                 MenuAction();
	                 break;
	               default:
	                 SetSysTime(dc,nIDEvent);
	                 break;
	//            if(m_bMsgAction)
	//               OnMsgAction();
	              }
	            break;
	       case ID_CASE_FIRST_LOOK:
	            {     
	              switch(nIDEvent)
	               {
	                case MANTIMER:
	                     OnManTimer();
	                     break;
	                case AUTOTIMER:
	                     OnAutoTimer();
	                     break;
	                case TIMER2:
	                     OnBtnTimer();
	                     break;
	                default:   
	                     SetSysTime(dc,nIDEvent);
	                     break;
	               }      
	            }  
	            break;
	       default:
	            break;
	
	      }//switch  
	      
   }//default
   break;
}   
	CMDIFrameWnd::OnTimer(nIDEvent);
}

void CMainFrame::OnNcLButtonDown(UINT nFlags, CPoint point)
{
// CPoint p(116,31);     
  if(!bByUser && m_uCase!=ID_CASE_START)
    {
     MessageBeep(0);
     return;
    }

 switch(m_uCase)
	{             
	  
	  case ID_CASE_START:    
	       return;
	  case ID_CASE_SET_MAP:                              
	       if(Case2NcMLD(nFlags))
	          CMDIFrameWnd::OnNcLButtonDown(nFlags, point);
	       return;
	  default: return;
    }

}

void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	switch(m_uCase)
	 {
	  case ID_CASE_START:    
	       Case0MLD(nFlags,point);//Test left mouse down
	       return;         
	  case ID_CASE_FIRST_LOOK: 
	       if(m_pMenu95)
	         if(m_pMenu95->m_hWnd && 
	            m_pMenu95->IsWindowVisible())
	            {m_pMenu95->PostMessage(WM_COMMAND,IDCANCEL);
	             return;}

	       WndIconAction(MAKELONG(point.x,point.y));
	       break;   
	   case ID_CASE_SET_MAP:
	        break;
	  default: return;
     }
	
	CRect rect;
	m_wndToolBar.GetWindowRect(&rect);
	if(rect.PtInRect(point)==0 && GetByUser())
	   CMDIFrameWnd::OnLButtonDown(nFlags, point);
}

void CMainFrame::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	switch(m_uCase)
	 {
	  case ID_CASE_START: 
	       CMDIFrameWnd::OnLButtonUp(nFlags, point);
	       return;
	  case ID_CASE_SET_MAP:
	       if(Case2NcMLD(nFlags))
	         CMDIFrameWnd::OnLButtonUp(nFlags, point);
	       return;
	  default: return;
     }

	
}                 

void CMainFrame::OnNcLButtonUp(UINT nFlags, CPoint point)
{                 
  if(!bByUser && m_uCase!=ID_CASE_START)
    {
     return;
    }
    
  	switch(m_uCase)
	 {
	  case ID_CASE_START: 
	       CMDIFrameWnd::OnLButtonUp(nFlags, point);
	       return;
	  case ID_CASE_SET_MAP:
	       if(Case2NcMLD(nFlags))
	         CMDIFrameWnd::OnNcLButtonUp(nFlags, point);
	       return;
	  default: return;
     }

}

void CMainFrame::OnNcLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	//CMDIFrameWnd::OnLButtonDblClk(nFlags, point);
}


BOOL CMainFrame::OnNcActivate(BOOL bActive)
{     
    switch(m_uCase)
      {                       
       case ID_CASE_ADVANCE:
       case ID_CASE_FIRST_LOOK:
       case ID_CASE_START:
            return 1;
       default:
            break;
      }      
	return CMDIFrameWnd::OnNcActivate(bActive);
	
	// TODO: Add your message handler code here
	
}



void CMainFrame::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	if(!bClose)
	   return;
	CMDIFrameWnd::OnClose();
}

void CMainFrame::OnNcMouseMove(UINT nFlags, CPoint point)
{
 	switch(m_uCase)
	 {
	  case ID_CASE_START:    
     	   //CMDIFrameWnd::OnMouseMove(nFlags,point);
	       return;
	  case ID_CASE_SET_MAP:      
	       CMDIFrameWnd::OnNcMouseMove(nFlags,point);
	       return;
	  default: return;
     }
}                 

void CMainFrame::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	
	switch(m_uCase)
	 {
	  case ID_CASE_START:    
	       //Case0MouseMove(nFlags,point);
	       return;
	  default: break;//return;
	  
	 }
	CMDIFrameWnd::OnMouseMove(nFlags, point);  
}

void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{                  
  CMDIFrameWnd::OnActivate(nState, pWndOther, bMinimized);

  if(WA_ACTIVE)
  {
	CDC dc;      
	dc.CreateDC("DISPLAY",0,0,0);
	int cx=dc.GetDeviceCaps(HORZRES);
	int cy=dc.GetDeviceCaps(VERTRES);
	WinCxy.x=int((cx-640)/2);
	WinCxy.y=int((cy-480)/2);
	dc.DeleteDC();
//	MoveWindow(WinCxy.x,WinCxy.y,640,480);
	// TODO: Add your message handler code here
	char szMenuItem[50];
	CMenu *pMenu=GetSystemMenu(0);
	if(pMenu)
	for(int i=0;i<int(pMenu->GetMenuItemCount());i++)
	   {
	    pMenu->GetMenuString(i,szMenuItem,40,MF_BYPOSITION);
	    if(*szMenuItem!=NULL)
	      pMenu->ModifyMenu(i,MF_BYPOSITION,i,szMenuItem);
       }                         

    MoveWindow(WinCxy.x,WinCxy.y,640,480);           
 }     

/*
 }
 else
 {
   BringWindowToTop();
 }
*/       
}

/*
void CMainFrame::OnAppExit()
{
	// TODO: Add your command handler code here
	switch(m_uCase)
	 {
	  case ID_CASE_START:
	       SendMessage(WM_CLOSE,0,0);
	       return;
	  case ID_CASE_SET_MAP:
	       SendMessage(WM_COMMAND,ID_CASE2_EXIT,0);
	       return;
	  default:
	       return;
	 }
	
}
*/
void CMainFrame::OnCancel()
{
 switch(m_uCase)
     {
      case ID_CASE_SET_MAP:
           SetTipActive(0);
           SetMapCancel();
           return;
      default:   
           SendMessage(WM_SYSCOMMAND,SC_KEYMENU,0);                                     
	       ::DefWindowProc(GetSafeHwnd(),WM_SYSKEYDOWN,65+('C'-'A'),0L);
           return;
     }

}              

void CMainFrame::OnNext()
{                       
 static DWORD lDelay=0;
 if(g_mbCanDoNext==0)
   {
    lDelay=GetTickCount();
    return;   
   }
 if(!g_bAuto&&(lDelay+1000)>GetTickCount())   
    return;
                
 if(m_pNextBtn && g_bAuto)
 {               
    CRect rect;
    m_pNextBtn->GetWindowRect(&rect);            
    SetCursorPos(rect.left+rect.Width()/2,
                 rect.top+rect.Height()/2);   
 }            
   
 if(g_bAuto && g_uTick==10000)
 {
   KillTimer(IDAUTO);
   SetTimer(IDAUTO,5000,NULL);
   g_uTick=0;
 }               
 
 switch(m_uCase)
     {         
      case ID_CASE_ADVANCE:
           AdvanceNext();
           return;
      case ID_CASE_SET_MAP:
           SetTipActive(0);
           SetMapNext();
           return;
      default:   
           SendMessage(WM_SYSCOMMAND,SC_KEYMENU,0);                                     
	       ::DefWindowProc(GetSafeHwnd(),WM_SYSKEYDOWN,65+('N'-'A'),0L);
           return;
     }
}

void CMainFrame::OnSetFocus(CWnd* pOldWnd)
{
	CMDIFrameWnd::OnSetFocus(pOldWnd);
	
	// TODO: Add your message handler code here 
//	if(m_pWnd)
//	  m_pWnd->SetFocus();
	bPaintMsg=1;
}

void CMainFrame::MenuTimer(UINT nIDEvent)
{             
 if(nIDEvent!=TIMER)
    return;

 static int  y=0;
 static BOOL bHead=1;
 if(y==0)
  {
   g_mbCanDoNext=0;
  } 
 
 y++;           
 if(bHead)
  {if(y>5)
    {bHead=0;
     y=1;}
   else
    return; }
          
// PostMessage(WM_SYSCOMMAND,SC_KEYMENU,0L);
g_mbCanDoNext=0;              
 if((y)>=m_nMenuCount)
   { 
//    if((y)>=m_nMenuCount+5)
//      {
	    y=0;     
	    bHead=1;
        KillTimer(TIMER);         
        g_mbCanDoNext=1;    
        SetKey(1);
/*
        return;
      }        
    else
    if(y>=m_nMenuCount+1)
      return;
*/    
    ::PostMessage(m_hWndMDIClient,WM_LBUTTONDOWN,0,MAKELONG(500,200));                           
  //  ::PostMessage(m_hWndMDIClient,WM_LBUTTONUP,0,MAKELONG(500,300));                           
    
//    SetKey(1);
    UINT nMenuId=UINT(GetMenu()->GetSubMenu(m_uMenuPos)->
                 GetMenuItemID(m_nSeparator==0?m_nMenuCount-1:
                               m_nMenuCount-1+m_nSeparator));
    if(m_pWnd)
      { m_pWnd->PostMessage(WM_COMMAND,nMenuId);
        if(m_bPostToMain)
          { PostMessage(WM_COMMAND,nMenuId);                 
            m_bPostToMain=0;}
      }      
    else  
      PostMessage(WM_COMMAND,nMenuId);                 
    
    SetBusy(0);
    SetMouse(1);
    SetSysFilter(0);
      
    return;   
   }

  PostMessage(WM_KEYDOWN,VK_DOWN,0);
  
}

void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
	CMDIFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
	int nCount=pPopupMenu->GetMenuItemCount();
	for(int i=0;i<nCount;i++)
	   pPopupMenu->EnableMenuItem(i,MF_BYPOSITION|MF_ENABLED);
	if(m_uCase==ID_CASE_SET_MAP)
	  { 
	  	if(m_nSourceWnd>0)
	  	   pPopupMenu->CheckMenuItem(m_nSourceWnd,MF_BYCOMMAND|MF_CHECKED);
        if(m_nVariableWnd>0) 
           pPopupMenu->CheckMenuItem(m_nVariableWnd,MF_BYCOMMAND|MF_CHECKED);
      }
	// TODO: Add your message handler code here
	
}

int CMainFrame::OnMouseActivate(CWnd * pDesktopWnd,UINT nHitTest,UINT Msg)
{ 

  switch(m_uCase)
   {
	    case ID_CASE_FIRST_LOOK:
	    case ID_CASE_START:
	         break;        

	    case ID_CASE_ADVANCE:
	    default:     
		  if(bByUser==0  && m_pDlg!=0)
		    {                   
		     m_pDlg->SetFocus();
		     return MA_NOACTIVATE;
		    }
		   break;
	}    

  return CMDIFrameWnd::OnMouseActivate(pDesktopWnd,nHitTest,Msg);  
}
void CMainFrame::CloseDlg(CDialog *pDlg)
{
  if(pDlg)//if dialog Open, it sholud close it
    { 
     pDlg->SendMessage(WM_COMMAND,IDCANCEL,0L);
     delete pDlg;
     pDlg=0; 
     Delay(500);
    } 

}
void CMainFrame::CloseWnd(CMDIChildWnd *pWnd)
{
  if(pWnd)//if Window Open, it sholud close it
    {                  
     pWnd->SendMessage(WM_CLOSE,0,0L);
     pWnd=0;           
    } 

}                                   

BOOL  CMainFrame::CheckOperation(LPSTR lpDesString,
                                 LPSTR lpSrcString,
                                 UINT  ID,
                                 UINT  nCount)
{      
 int nExitID;  
 
 if(lpDesString==NULL)
    return 1;
 if(lstrlen(lpDesString)==0)   
    return 1;
    
 if(lstrcmpi(lpDesString,lpSrcString)!=0)
   {                
    MessageBeep(-1);
    MessageBeep(-1);
    CMsgDlg *pDlg = new CMsgDlg(1,nExitID,ID,1,this); 
    pDlg->DoModal(); 
    return 0;
   }
  
 return 1; 
}

BOOL CMainFrame::EndCase(BOOL *ByUser, //exercise do ?
                         UINT ID,     //string id
                         UINT *pCaseID)// next case step id
{
   if(*ByUser==1)
     {     
       SetMouse(1);
       int nExitID;
       CMsgDlg *pDlg = new CMsgDlg(0,nExitID,ID,1,this); 
       pDlg->DoModal();
       if(nExitID==IDYES)
          { 
           goto Label_Case2_11;
          }
       else
         {
	      (*pCaseID)--;//m_uNextAction--;
	      return 0;
	     } 
	      
      }//if(*ByUser==1)
    else 
      {    
Label_Case2_11:           
        if(GetStepMsgBox())
           RestoreBk(STEPMSG);
         *pCaseID=0; 
         *ByUser=0;  
       } 
    return 1;
}

void CMainFrame::OnLoadStatus()
{
	// TODO: Add your command handler code here
	CLoadStatusDlg *pDlg = new CLoadStatusDlg(this,1);
    pDlg->DoModal();
}


/*-------------------------------------------------------*/
/*                                                       */
/*-------------------------------------------------------*/
void CMainFrame::OnExitProcess()
{                         
    KillTimer(AUTOPLAY);   
    g_bAuto=0;             
/*    
    CContactDlg contactDlg;
	contactDlg.DoModal();  
*/
//  AfxMessageBox("fasdfjasdf");
  
 CRect rect;          
 GetWindowRect(&rect);
 StartScreen(0,0,rect.Width(),rect.Height(),1);
}

/*-------------------------------------------------------*/
/*          Brush FirstLook Brush                        */
/*-------------------------------------------------------*/ 
void BrushFirstBk(HWND hWnd)
{   
 HANDLE bCanDo=GetProp(AfxGetApp()->m_pMainWnd->m_hWnd,"CanBk");
 if(bCanDo==0) 
    return;         
 RECT r;              
 ValidateRect(hWnd,&r);
 HDC   hDC=GetDC(hWnd);
 HBRUSH hBrush,hOLdBrush;
 hBrush=::CreateSolidBrush(RGB(0,128,128));
 CRect rect;
 ::GetClientRect(AfxGetApp()->m_pMainWnd->m_hWnd,&rect);
 rect.bottom-=33;
 hOLdBrush=(HBRUSH)::SelectObject(hDC,hBrush);
 ::PatBlt(hDC,rect.left,rect.top,rect.Width(),rect.Height(),PATCOPY);
 ::SelectObject(hDC,hOLdBrush);
 DeleteObject(hBrush);
 ReleaseDC(hWnd,hDC);
}

BOOL CMainFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	// TODO: Add your message handler code here and/or call default
    BOOL x;
	if(m_uCase==ID_CASE_START)    
	{
	   CPoint point;
	   GetCursorPos(&point);
	   Case0MouseMove(0,point);
	   x=1;
	}             
	else
	{
		if(nHitTest==HTBORDER      ||
		   nHitTest==HTBOTTOM      ||
		   nHitTest==HTBOTTOMLEFT  ||
		   nHitTest==HTBOTTOMRIGHT ||
		   nHitTest==HTLEFT        ||
		   nHitTest==HTRIGHT       ||
		   nHitTest==HTTOP         ||
		   nHitTest==HTTOPLEFT     ||
		   nHitTest==HTTOPRIGHT    )
		  {SetCursor(hArrow);
		   x=1;}
		else  
		  x=CMDIFrameWnd::OnSetCursor(pWnd, nHitTest, message);
	}   
	return x;
}                 


BOOL CMainFrame::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	
	return 1;//CMDIFrameWnd::OnEraseBkgnd(pDC);
}



