// CASE2.cpp :
//
#define  __cplusplus
#include "stdafx.h"
#include "tutorial.h"
#include "mainfrm.h"

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

extern void Delay(int nDelay,BOOL bShow=0);
extern BOOL g_mbCanDoNext;
extern BOOL g_bAuto;
extern BOOL g_bBasicFirst;
//paint Case 2       
/*-------------------------------------------------------*/
/*                   case 2 paint                        */
/*-------------------------------------------------------*/
void CMainFrame::PaintCase2(CDC &dc)
{

 if(GetStepMsgBox())
    SetStepMsg(0,1,FALSE);
// if(GetOverMsgBox())   
 //   SetNoteMsg(0,1,FALSE);

 if(bCase2Paint==0) //prevate  the first paint flash
   {       
    bCase2Paint=1;
    return;
   }                 
//   bPaintMsg=0;
   g_bBasicFirst=1;
   PaintSetMapMsg(dc);
   if(m_pWnd) 
     {
      m_pWnd->ShowWindow(SW_SHOW);
      if(!m_pDlg)
          m_pWnd->SetFocus();
     } 


}

/*-------------------------------------------------------*/
/*                paint message broad cast               */
/*-------------------------------------------------------*/
void CMainFrame::PaintSetMapMsg(CDC &dc)
{
 CRect   rectMsg;
 rectMsg.left=WinCxy.x+335;
 rectMsg.top=WinCxy.y+263;
 rectMsg.right=rectMsg.left+300;
 rectMsg.bottom=rectMsg.top+194;
// ScreenToClient(&rectMsg);

 rectMsgBlock.SetRect(rectMsg.left+25,
                      rectMsg.top+36,
                      rectMsg.left+25+246,
                      rectMsg.top+36+100);
                      
 DrawTPmap(dc,rectMsg,IDB_BITMAP16,IDB_BITMAP17);
 DrawTitle(dc,szCaseTitle,RGB(255,255,0));    
 BOOL bDIP=0;
 if(m_uMsgID!=m_uOldMsgMapID)//if different MsgID Play door effection
   {
    bDIP=1;
    m_uOldMsgMapID=m_uMsgID;
   }
  DisplayMessage(dc,rectMsgBlock,m_uMsgID,1,bDIP);
  if(m_pCancelBtn)
    {  
     m_pCancelBtn->Invalidate(FALSE);
     m_pCancelBtn->UpdateWindow();   
    } 
  if(m_pNextBtn)
    {
     m_pNextBtn->Invalidate(FALSE);
     m_pNextBtn->UpdateWindow();   
    } 


}
/*-------------------------------------------------------*/
/*                 go back to case 0                     */
/*-------------------------------------------------------*/
void CMainFrame::OnCase2Exit()
{
	// TODO: Add your command handler code here
    if(g_mbCanDoNext==0)
       return;

    g_bBasicFirst=0;
    SetMouse(1);
    SetKey(1);
    RestoreBk(0);
    RestoreBk(1);      
    if(g_bAuto)
      KillTimer(IDAUTO);
    //SetSysMouse(0);
    //SetSysKey(0);
    bCase2Paint=0;
    bPaintMsg=1;
    m_pCancelBtn->DestroyWindow();
    delete m_pCancelBtn;
    m_pCancelBtn=0;               
    m_pNextBtn->DestroyWindow();
    delete m_pNextBtn;
    m_pNextBtn=0;
//    m_pStoreBit->DeleteObject();
//////////////////////////////////////
//    delete m_pStoreBit; 
//    m_pStoreBit=0;      
//////////////////////////////////////    
    m_uOldMsgMapID=0;
    CloseDlg(m_pDlg);
    CloseWnd(m_pWnd);
    CMDIChildWnd *pWnd=MDIGetActive();
    while(pWnd)
     {
      pWnd->MDIDestroy();
      pWnd=MDIGetActive();
     }
    SetCase(ID_CASE_START);
    ::ShowWindow(m_hWndMDIClient,SW_HIDE);
    SendMessage(WM_COMMAND,ID_VIEW_STATUS_BAR,0L);
    SendMessage(WM_COMMAND,ID_VIEW_TOOLBAR,0L);
    Invalidate();
    UpdateWindow();   
     
    bDrawBtn=1; 
    CRect rectWin;
    GetWindowRect(&rectWin);
    CRect rectBtn(
                  int(rectWin.Width()*3/4),
                  int(posLine.y-6),
                  int(rectWin.Width()*3/4)+100,
                  int(posLine.y-6+20) );
    ScreenToClient(&rectBtn);              
//    m_pExitBtn = new CButton;
    m_pExitBtn->Create("&Exit Tutorial",WS_CHILD|WS_VISIBLE|WS_TABSTOP,rectBtn,this,ID_APP_EXIT);
    g_mbCanDoNext=1;
  //  FilterNcMouse(0);
    FilterTaskChange(0);
    m_pVarWnd=0;
    if(g_bAuto)
     SetTimer(AUTOPLAY, 2000, NULL);
    SetSysKey(0); 
} 

/*-------------------------------------------------------*/
/*           Control not client msg                      */
/*-------------------------------------------------------*/
BOOL CMainFrame::Case2NcMLD(UINT nFlags)
{
  if(nFlags==HTCLIENT||nFlags==HTMENU)
    return 1;
  return 0;  
}
/*-------------------------------------------------------*/
/*               case  SetMap Next                       */
/*-------------------------------------------------------*/
void CMainFrame::SetMapNext()
{           

 if(GetBusy())
   {
    if(m_pDlg)
      m_pDlg->SetFocus();
    return;              
   } 
  
 //Restore background
 if(!g_mbCanDoNext || !m_bSetMap_Action)
   {
    return;
   }
 m_bSetMap_Action=0;
 RestoreBk(NOTEMSG);
 m_pCancelBtn->EnableWindow(FALSE);
 m_pNextBtn->EnableWindow(FALSE);
 
 m_uNextAction++;   
 SetCursor(LoadCursor(NULL, IDC_ARROW));      
 
 switch(m_uNextCaseID)
   {
	case ID_CASE_SET_MAP:      
	     //HowToSetBreakPoint(&m_uNextAction);
	     HowToUseSetmapDlg(&m_uNextAction);
         break;
	case ID_CASE_LOAD_FILE:	 
		 HowToUseDownLoadFile(&m_uNextAction);
		 //HowToSetBreakPoint(&m_uNextAction);
		 break;   
	case ID_CASE_SET_BP:
		 //HowToUseDownLoadFile(&m_uNextAction);
		 HowToSetBreakPoint(&m_uNextAction);
		 break;   
	case ID_CASE_GO:
		 HowToGo(&m_uNextAction);
		 break;
	case ID_CASE_MODE_SEL:
		 HowToSelMode(&m_uNextAction);
		 break;
	case ID_CASE_STEP:
	     HowToStep(&m_uNextAction);	 
	     break;
	case ID_CASE_VARIABLE:
	     HowToUseVarWnd(&m_uNextAction);
	     break;
	default:
         OnCase2Exit();
	     break;
   } 
 SetMouse(1);       
 m_bSetMap_Action=1;
 if(m_pCancelBtn)
    m_pCancelBtn->EnableWindow(TRUE);
 if(m_pNextBtn)   
    m_pNextBtn->EnableWindow(TRUE);
}
    
/*-------------------------------------------------------*/
/*            Cancel Set Map Tutorial                    */
/*-------------------------------------------------------*/
void CMainFrame::SetMapCancel()
{                  
   SetMouse(1);
   SetKey(1);
   CloseDlg(m_pDlg);
   SetBusy(FALSE);   
   SetNextAction(3000);   
  // UpdateWindow();
   PostMessage(WM_COMMAND,ID_NEXT,0L); 
};
/*-------------------------------------------------------*/
/*                 handle SetMap Menu Item Msg           */
/*-------------------------------------------------------*/
void CMainFrame::OnGroupMap()
{ 
 if(m_uMenuAct!=ID_GROUP_MAP)
   {
    MessageBeep(-1);
    return;
   }      
                      
 m_pDlg=0;                           
 if(bByUser)
   {
    CMapDlg *p = new  CMapDlg(this);
    p->DoModal();              
    CRect rect;
    GetClientRect(&rect);
    ::InvalidateRect(m_hWndMDIClient,&rect,1);
    ::UpdateWindow(m_hWndMDIClient);
    WriteMsg(IDS_SET_MAP_MSG2);   
   } 
 else
   { 
    CMapDlg *p = new  CMapDlg(this,0);
    p->ShowWindow(SW_SHOW);
    
   }
}

/*-------------------------------------------------------*/
/*              How to use Set Map Dlg                   */
/*-------------------------------------------------------*/
void CMainFrame::HowToUseSetmapDlg(UINT *nID)
{           
 CPoint    pointDest;
 CPoint    pointSrc;
 CMapDlg   *mapDlg;
 CRect     rect;
 
 SetMouse(0);    
 if(m_pDlg)
   {
    m_pDlg->SetFocus();  
    mapDlg=(CMapDlg *)m_pDlg;
   }
   
 switch(*nID)
   { 
    case 1:    
         bCase2Paint=0;
         rect.SetRect(WinCxy.x+6    ,WinCxy.y+375+2,
                      WinCxy.x+4+330,WinCxy.y+375+80-2);
         OverMsg.SetPaintRect(rect);
    
         SetKey(1);//setKeyFilter
		 //Choose Map Menu Item
         StepMsg.SetFont(12,5,400);
         OverMsg.SetFont(16,6,200);
		 pointDest.x=116;
		 pointDest.y=31;
		 GetCursorPos(&pointSrc);
		 MoveCursor(pointDest,pointSrc);
		 DrapMenu(2,'G',4);
		 m_uMenuAct = ID_GROUP_MAP;
		 bCase2Paint=1; 
		 if(g_bAuto)    
		   {
		    SetTimer(IDAUTO, 5000,NULL);
		    //SetSysMouse(1);
		    //SetSysKey(1);
		   } 
		 return;

     case 2:
         m_pStoreBit->DeleteObject();
         mapDlg->SetEditText1();
         break;
     case 3:
	     mapDlg->SetCombo1();
	     break;
     case 4:
         mapDlg->SetEditText2();
         break;
     case 5:  
         mapDlg->SetRadioButton(); 
         break;
     case 6:
         mapDlg->SetCombo2(); 
         break;
     case 7:
         mapDlg->SetSetBtn(1);
         mapDlg->SetListBox();
         ((CButton *)(mapDlg->GetDlgItem(IDC_MAP_ADD)))->SetState(0);
         SetSysFilter(0);
         break;
     case 8:
         mapDlg->SetSaveBtn(1);
         ((CButton *)(mapDlg->GetDlgItem(IDC_MAP_SAVE)))->SetState(0);
         break;
     case 9:      
         mapDlg->SetCloseBtn(1);
         ((CButton *)(mapDlg->GetDlgItem(IDC_MAP_CLOSE)))->SetState(0);
         break;
     case 10:   
	     SetKey(1);
	     CloseDlg(m_pDlg);
	     //m_wndToolBar.FlashWindow(1);    
/*	     
	     StepMsg.rectDes.SetRect(WinCxy.x+495,WinCxy.y+72,
	                             WinCxy.x+495+138,WinCxy.y+72+190);
	     SetStepMsg(IDS_SET_MAP_STEP,1);
	     SetByUser(1);
	     WriteMsg(IDS_SET_MAP_MSG2);   
*/
	     break;
	 case 110:      
	     if(EndCase(&bByUser,IDS_SET_MAP_TUT1,nID))
	       { 
	        m_uNextCaseID=ID_CASE_LOAD_FILE; 
            RestoreBk(MS_STEP);
            PostMessage(WM_COMMAND,ID_NEXT,0L);
           }
		 break;
     default:
         SetNextAction(0);
         RestoreBk(MS_STEP);
         m_uNextCaseID=ID_CASE_LOAD_FILE; 
	     PostMessage(WM_COMMAND,ID_NEXT,0L);
         break;
   }           
   
   SetMouse(1);    

}