// Advnext.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "tutorial.h"
#include "tmouse.h"
#include "mainfrm.h"
#include "time.h"
#include "TrcDlg.h"
                    
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif                       

extern BOOL g_mbCanDoNext;
extern BOOL g_bAuto;
void PopupMenu(CMenu &menu);
void Delay(int nDelay,BOOL bShow=0);
void Delay(int nDelay,BOOL bShow)
{                     
//    SetSysFilter(1);

    CPlayMeter *play =
       ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetMeter();

    int nYLED=nDelay/3*2;           
    int nOldLED=0;
    MSG msg; 
    LONG lDelay;
	LONG lBasicTime=GetTickCount();   
	int nYellow=3;
	
	if(play && bShow)
	   play->SetDelayScreen(3,nDelay);
	
	do{
       if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
          { 
            TranslateMessage(&msg);        
            DispatchMessage(&msg);
          }  
	   lDelay=GetTickCount(); 
	   
	   if(lDelay>=nYLED && nYellow==3)
	      nYellow=2;

	   if(play && bShow)
	     play->SetDelayScreen(nYellow,
	                          int(nDelay-lDelay+lBasicTime),
	                          (nOldLED==2));//0);

	     nOldLED=nYellow;
	 }while((lDelay-lBasicTime)<nDelay);
    
	if(play && bShow)
	   play->SetDelayScreen(1,0);//);
	
//    SetSysFilter(0);	
} 

void CMainFrame::MoveArrow( CDC   &dc,
                            CRect rect,
                            int   nCy,
                            BOOL  bUP/*=1*/,
                            int   nDelay/*=100*/)
{           
  CPlayMeter *play =
       ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetMeter();
                           
  int nYellow=3;                      
  int nOldLED=0;   
  if(play )
	 play->SetDelayScreen(3,60);
           
  CRect rectWin;      
  GetWindowRect(rectWin); 
  m_bUpArrow=bUP;
  if(!m_bUpArrow)
    {
       dc.SetMapMode(MM_ISOTROPIC);
       dc.SetWindowOrg(0,480);//rectWin.Height());
       dc.SetWindowExt(640,-480);
       dc.SetViewportOrg(rectWin.left,rectWin.top);
       dc.SetViewportExt(rectWin.right,rectWin.bottom);

    } 
    
  for(int i=0;i<nCy;i++)
     {      
       rect.top+=i;
       rect.bottom+=i;
       DrawTPmap(dc,rect,
                 "ARROW_LEFT_C",
                 "ARROW_LEFT_M",
                 m_hFlookInst,m_pStoreBit);  
       Delay(nDelay);  
       //if(i<nCy-1)   
	   if(i>=nCy/3*2)
	      nYellow=2;

	   if(play )
	     play->SetDelayScreen(nYellow,
	                          int(nCy-i),
	                          (nOldLED==2));//0);
       
         nOldLED=nYellow; 
     }            

	if(play)
	   play->SetDelayScreen(1,0);
} 
void CMainFrame::MenuAction()
{
 static int nCount=0;
 nCount++;      
 BOOL bHold= g_mbCanDoNext; 
 g_mbCanDoNext=0;
////////////////////////////////////////////////////////////////// 
 int nYellow= nCount> (m_nMenuCount/3*2) ? 2:3;
 CPlayMeter *play =
 ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetMeter();

 if(play )
   play->SetDelayScreen(nYellow,int(m_nMenuCount-nCount),(nYellow==2));//0);
/////////////////////////////////////////////////////////////////////

 PostMessage(WM_KEYDOWN,VK_DOWN,0);
 if((nCount)>=m_nMenuCount)
   {
     KillTimer(MENUTIMER);
     nCount=0;    
     if(m_pointD.y>=WinCxy.y && m_pointD.x>WinCxy.x)
        MoveCursor(m_pointD,m_pointS,1,m_Move_Count_Delay,m_nCursorStep);
     ::PostMessage(m_hWndMDIClient,WM_LBUTTONDOWN,0,MAKELONG(500,300));

      SetSysFilter(0);
      SetMouse(1);
      SetKey(1);
   }                         
 g_mbCanDoNext=bHold;
} 

void PopupMenu(CMenu &menu)
{ 
 menu.CreatePopupMenu();
 
 menu.AppendMenu(MF_STRING,0,"&Refresh");
 menu.AppendMenu(MF_STRING|MF_CHECKED,0,"&Bus");
 menu.AppendMenu(MF_STRING,0,"&Instruction");
 menu.AppendMenu(MF_STRING,0,"S&ource");   
 menu.AppendMenu(MF_SEPARATOR);
 menu.AppendMenu(MF_STRING,0,"Goto &Start Frame");
 menu.AppendMenu(MF_STRING,0,"Goto &End Frame");
 menu.AppendMenu(MF_STRING,0,"&Goto Frame");
 menu.AppendMenu(MF_SEPARATOR);
 menu.AppendMenu(MF_STRING,0,"S&ave Trace");
 menu.AppendMenu(MF_SEPARATOR);
 menu.AppendMenu(MF_STRING,0,"Trace &Control");
 menu.AppendMenu(MF_STRING,0,"E&vent");
 menu.AppendMenu(MF_STRING,0,"&Trigger");
 menu.AppendMenu(MF_STRING,0,"Trace &Filter");

}                     
                            
/*-------------------------------------------------------*/
/*             Arrow Indect                              */
/*-------------------------------------------------------*/
void CMainFrame::SetArrow(int ncy, 
                          int bup,
                          int top, 
                          int left,//210
                          int width,//200
                          int height,//50 
                          int delay//100
                          )
{
   CRect rect;  
   m_pMsgWnd->GetWindowRect(&rect); 
   rect.left-=left;
   rect.top+=top;
   rect.right=rect.left+width;
   rect.bottom=rect.top+height;
   CWindowDC dc(m_pDlg);//==0? m_pAdvanceWnd:m_pDlg);
   MoveArrow( dc,rect,ncy,bup,delay);  
   m_bDC=1;
}                             
/*-------------------------------------------------------*/
/*             process advance next                      */
/*-------------------------------------------------------*/
void CMainFrame::AdvanceNext()
{        
 SetKey(0);
// SetMouse(1);
/* 
 if(m_pArrow.IsWindowVisible())
   {SetPaintDO(0); 
    m_pArrow.ShowWindow(SW_HIDE);
   }          
   
*/          
 if(m_pAdvanceWnd)
    { m_pAdvanceWnd->SetFocus();
    }

 if(bByUser==0  && m_pDlg!=0)
   {                   
     m_pDlg->SetFocus();
   }

 if(!g_mbCanDoNext)
   {
    return;
   }
 SetSysMouse(1);
 g_mbCanDoNext=0;         
 m_NextBtn.EnableWindow(FALSE);
 m_pMeter->EnableWindow(FALSE);
 
 CloseDlg(GetAMsgDlg());
 
 AdvanceRestoreBk();

 m_uNextAction++;      
 SetCursor(LoadCursor(NULL, IDC_ARROW));         
 switch(m_uNextCaseID){
   case ID_ADVANCE_EVENT:
        InitButton(1);
        AdvanceEvent();   
        break;      
   case ID_ADVANCE_TRACE:      
        InitButton(1);
        AdvanceTrace();
        break;
   case ID_ADVANCE_TRIGGER:
        InitButton(1);
        AdvanceTrigger();
        break;
   case ID_ADVANCE_FILTER:
        InitButton(1);
        AdvanceFilter();
        break;
   case ID_ADVANCE_MEM:    
        InitButton(2);
//        AdvanceMemDlg();  
        AdvanceMemEdit();
        break;
   case ID_ADVANCE_MEM_DLG:
        InitButton(2);
        AdvanceMemDlg();  
        break;
   default:   
        SetSysMouse(0);
        g_mbCanDoNext=1;       
        OnExitAdvance();
        return;
  }

// SetMouse(1);
 
 Delay(1000,1);  
 SetKey(1);     
 SetSysMouse(0);
 g_mbCanDoNext=1;       
 if(m_NextBtn.GetSafeHwnd())// && g_bAuto==0)
    m_NextBtn.EnableWindow(TRUE);
 if(m_pMeter)
    m_pMeter->EnableWindow(TRUE);
}                                                          
        
/*-------------------------------------------------------*/
/*              Advance Filter                           */
/*-------------------------------------------------------*/ 
void CMainFrame::AdvanceFilter()
{ 
 switch(m_uNextAction){
    case 1: 
      Filter_Set();
      break; 
    case 2:
      Filter_Trace();                 
      break; 
    case 3:
      Filter_Item();
      break;
    case 4:
      Filter_State();
//    m_Trace.SetState(0);
      break;
    default: 
      ToMemCase();                     
      m_uNextCaseID=ID_ADVANCE_MEM;
      m_uNextAction=0;
      break;
} 

      //OnExitAdvance();
}     
/*-------------------------------------------------------*/
/*              Advance Filter                           */
/*-------------------------------------------------------*/ 
void CMainFrame::ToMemCase()
{ 
  if(m_pAdvanceWnd)
    {m_pAdvanceWnd->DestroyWindow();
     delete m_pAdvanceWnd;//->DestroyWindow();
     m_pAdvanceWnd=0;}
  CloseDlg(m_pDlg);
           
  CRect rect;
  m_pMsgWnd->GetWindowRect(&rect);         
  if(rect.Width()<400)
    {
	  Delay(55);
	  m_pMsgWnd->ShowWindow(SW_HIDE);
	  m_pMsgWnd->SetTitle("Memory");
	  m_pMsgWnd->MoveWindow(WinCxy.x+100,WinCxy.y+100,400,200);
	  m_pMsgWnd->SetContent("Advance.TXT");
	  m_pMsgWnd->Invalidate();
	  m_pMsgWnd->ShowWindow(SW_SHOW);
	}  
   //Delay(1000);
   m_Trace.EnableWindow(TRUE);
   m_Memory.EnableWindow(TRUE);
   m_Trace.SetState(0);
   m_Memory.SetState(0);

}  

void CMainFrame::Filter_State()
{
 m_pMsgWnd->SetContent("FILSTATE.TXT");
 SetArrow(2,0,160); 
}

void CMainFrame::Filter_Item()
{           
 m_pMsgWnd->SetContent("FILTIEM.TXT");
 SetArrow(2,1,60); 
}

void CMainFrame::Filter_Trace()
{
 ((CTrcDlg *)m_pDlg)->SetTrace();
 m_pMsgWnd->SetContent("FILTER.TXT");
 Delay(100);
 SetArrow(2,1,40); 
} 

void CMainFrame::Filter_Set()
{
 ((CTrcDlg *)m_pDlg)->SelFilter(); 
  
  m_pMsgWnd->SetTitleFont(24,10,1600);
  //int nHeight=36,int nWidth=18,int nWeight=1600);
  m_pMsgWnd->SetTitle("TRACE - FILTER");
  m_pMsgWnd->SetContent("TRCFILT.TXT");
}  

void CMainFrame::AdvanceRestoreBk()
{
 if(m_bDC && m_pDlg)   
  { CWindowDC dc(m_pDlg);
   if(!m_bUpArrow)
	   {         
	       CRect rectWin;
	       GetWindowRect(&rectWin); 
	       m_bUpArrow=1;
	       dc.SetMapMode(MM_ISOTROPIC);
	       dc.SetWindowOrg(0,480);//rectWin.Height());
	       dc.SetWindowExt(640,-480);
	       dc.SetViewportOrg(rectWin.left,rectWin.top);
	       dc.SetViewportExt(rectWin.right,rectWin.bottom);
	   }
    RestoreBk(dc,0);
    m_bDC=0;}
 else//if(m_bDC && m_pDlg)              
   {
    //CWindowDC dc(m_pAdvanceWnd);
    RestoreBk(0);
   } 
    
}
