// CONTROL.cpp :
//
#include "stdafx.h"
#include "tutorial.h"
#include "mainfrm.h"
//#include "control.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_bCanDoExit;
/////////////////////////////////////////////////////////////////////////////
// CTEdit
CTEdit::CTEdit(UINT  *puFocus,        //
               BOOL  bUserOperation, // is the time for user exercise
               LPSTR lpContent,     // window content that user should be set
               UINT  TextID,         // note string tab id
               UINT  nCount)         // how many ids;
{                   
 m_bRepeate=0;
 m_puFocus=puFocus;
 m_bKillFocus=0;
 m_bUserOperation = bUserOperation;
 
 if(lpContent==NULL)
    *szContent=NULL;
 else   
    lstrcpy(szContent,lpContent);
    
 m_TextID=TextID;
 m_nCount=nCount;
}

CTEdit::~CTEdit()
{
 *m_puFocus=CLOSEFOCUS;
}

BEGIN_MESSAGE_MAP(CTEdit, CEdit)
	//{{AFX_MSG_MAP(CTEdit)
	ON_WM_TIMER()
	ON_WM_KILLFOCUS()
	ON_WM_SETFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTEdit message handlers
void CTEdit::OnKillFocus(CWnd* pNewWnd)
{
	// TODO: Add your message handler code here
  CEdit::OnKillFocus(pNewWnd);	
/*
  static  int nCount=0;
  if(*m_puFocus==CLOSEFOCUS || *m_puFocus == SHOWFOCUSMSG)
      {
       return;
      }

  char szText[50];	

  if(m_bUserOperation && m_bKillFocus)
    {   

        ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->RestoreBk();
		 GetWindowText(szText,48);
		 if(!((CMainFrame *)(AfxGetApp()->m_pMainWnd))->
					                     CheckOperation(szContent,
					                                    szText,
					                                    IDS_SET_MAP_TUT2,
					                                    1))
		    {  
		       nCount++;
		       m_bRepeate=1;         
		       *m_puFocus=KILLFOCUS; 
		       if(nCount>=3)
		         {     
		          nCount=0;
		          MessageBeep(100);
		          SetWindowText(szContent);
		          m_bRepeate=0;
		          *m_puFocus=SETFOCUS;
		         }
		       
		       SetFocus();
		    } 
		  else
		    {
		       *m_puFocus=SETFOCUS; 
		       m_bRepeate=0;
		    } 
		
    }//if(m_bUserOperation)	       
*/    
}

void CTEdit::OnSetFocus(CWnd* pOldWnd)
{
	CEdit::OnSetFocus(pOldWnd);
/*	
	// TODO: Add your message handler code here
    if(*m_puFocus==CLOSEFOCUS && *m_puFocus!=SHOWFOCUSMSG)
      {
       m_bKillFocus=0;
       return;
      }
    
	if(m_bUserOperation)
	  if(*m_puFocus == SETSUCCESS || 
	     *m_puFocus == SETFOCUS || 
	     *m_puFocus == SHOWFOCUSMSG||
	     m_bRepeate )
	    {
	     if(!((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetOverMsgBox()) 
	       ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetNoteMsg(m_TextID,m_nCount);
	       *m_puFocus==KILLFOCUS; 
	       m_bKillFocus=1;
	    }                 
	  else
	    m_bKillFocus=0;
*/
}

void CTEdit::SetEditText(LPSTR lpText,    // edit control content
                         UINT  ID,         // Note box Content string id
                         int   nCount,     // how many id
                         BOOL  bDo/*=1*/,  // if show note box
                         BOOL  bMoveCursor/*=1*/,
                         BOOL  bFilter/*=1*/,
                         int   nDelay,//=55,
                         int   nStep)//=8)
                         
{ 
 CPoint pointDes;
 CPoint pointSrc;
 uCase=lstrlen(lpText);  
 lstrcpy(szText,lpText);
 CRect rectWin;
 GetWindowRect(&rectWin);
 pointDes.x=rectWin.right-10;
 pointDes.y=rectWin.top+rectWin.Height()/2;
 GetCursorPos(&pointSrc);                                               
 ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->
   MoveCursor(pointDes,pointSrc,bMoveCursor,nDelay,nStep,bMoveCursor);

 if(bDo)
   ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetNoteMsg(ID,nCount);

 ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetBusy(1);
              
 SetMouse(0);
 SetSysFilter(1);
 g_mbCanDoNext=0;          
 SetProp(m_hWnd,"Filter",(HANDLE)bFilter);
 SetTimer(TIMER2,100,NULL);
}  

void CTEdit::OnTimer(UINT nIDEvent)
{
  CEdit::OnTimer(nIDEvent);
 // TODO: Add your message handler code here and/or call default
 static int times=-1;

 int nVK;                   
 int i=0;
 g_bCanDoExit=0;            
 times++;
 i=times;            
 if((times)>int(uCase+10))
   {
     KillTimer(TIMER2);   
     g_bCanDoExit=1;
     g_mbCanDoNext=1;
     times=-1;   
     return; 
   }
 else{
   if((times)>int(uCase+1)){
     SetWindowText(szText);
     return;  }
 }         
   if(szText[i]=='\0')
      goto label;
      
   if((szText[i]>='a'&&
       szText[i]<='z'))
      nVK=65+szText[i]-'a';
   else
   if((szText[i]>='A'&&
       szText[i]<='Z')) 
     {  
      nVK=65+ szText[i]-'A';
      PostMessage(WM_KEYDOWN,VK_SHIFT,0);
     } 
   else 
   if((szText[i]>='0'&&
       szText[i]<='9')) 
      nVK=48+ szText[i]-'0';
   else
   if(szText[i]=='\\')
     {nVK=220;}
   else
   if(szText[i]==':')
     {nVK=110;}
   else  
     goto label;

   PostMessage(WM_KEYDOWN,nVK,0);

label:   
   if(times>int(uCase))
     {
       SetWindowText(szText);
      ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetBusy(0);
      if((BOOL)GetProp(m_hWnd,"Filter"))
         { SetMouse(1);  
           SetSysFilter(0);
           RemoveProp(m_hWnd,"Filter");}
     } 
}

/////////////////////////////////////////////////////////////////////////////
// CTComboBox

CTComboBox::CTComboBox(UINT  *puFocus,        // default is 1 focus action
                       BOOL  bUserOperation, // is the time for user exercise
                       LPSTR lpContent,     // window content that user should be set
                       UINT  TextID,         // note string tab id
                       UINT  nCount)         // how many ids;
{                   
     m_bRepeate=0;       
     m_bKillFocus=0;
     m_puFocus=puFocus;
	 m_bUserOperation = bUserOperation;
	 if(lpContent==NULL)
	    *szContent=NULL;
	 else   
	    lstrcpy(szContent,lpContent);
	 m_TextID=TextID;
	 m_nCount=nCount;
}

CTComboBox::~CTComboBox()
{ 
 if(m_puFocus)         
   *m_puFocus=CLOSEFOCUS;
}

BEGIN_MESSAGE_MAP(CTComboBox, CComboBox)
	//{{AFX_MSG_MAP(CTComboBox)
	ON_WM_TIMER()
	ON_WM_KILLFOCUS()
	ON_WM_SETFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTComboBox message handlers
void CTComboBox::OnKillFocus(CWnd* pNewWnd)
{
  CComboBox::OnKillFocus(pNewWnd);
	
	// TODO: Add your message handler code here
/*
  char szText[50];	
  static int nCount=0;
  
  //if(!m_puFocus)
     return;
     
   if(*m_puFocus==CLOSEFOCUS)
      {
       return;
      }


  if( m_bUserOperation && m_bKillFocus )
    {
      ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->RestoreBk();
	   GetWindowText(szText,48);
	   if(!((CMainFrame *)(AfxGetApp()->m_pMainWnd))->
	                                   CheckOperation( szContent,
			                                           szText,
			                                           IDS_SET_MAP_TUT2,
			                                           1))
	       {
	         nCount++;
	         m_bRepeate=1; 
	         if(m_puFocus)
	           *m_puFocus=KILLFOCUS; 
	         
		     if(nCount>=3)
		         {
		          MessageBeep(100);
		          if(SelectString(-1,szContent)==CB_ERR)
		             {
		              InsertString(0,szContent); 
		              SetCurSel(0);
		             }
		          SetWindowText(szContent);
		          nCount=0;
		          m_bRepeate=0;
		          if(m_puFocus)
		            *m_puFocus=SETFOCUS;
		         }

             SetFocus(); 
	       } 
	    else
	       {        
	         if(m_puFocus)
	           *m_puFocus=SETSUCCESS; 
	         m_bRepeate=0;
	       } 
		       
	}//  if(m_bUserOperation)
*/
}

void CTComboBox::OnSetFocus(CWnd* pOldWnd)
{
	CComboBox::OnSetFocus(pOldWnd);
/*	
	// TODO: Add your message handler code here
   // if(m_puFocus=NULL)
       return;
       
    if(*m_puFocus==CLOSEFOCUS)
      {
       return;
      }

    if(m_bUserOperation) 
      if( *m_puFocus==SETSUCCESS || *m_puFocus == SETFOCUS || m_bRepeate)
		{              
		 if(!((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetOverMsgBox())
	       ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetNoteMsg(m_TextID,m_nCount);
	     *m_puFocus==KILLFOCUS;
	     m_bKillFocus=1;
	    } 
       else
         m_bKillFocus=0;
*/
}

void CTComboBox::SetCombo(int   nID,        // which item to select in combobox
                          UINT  ID,         // Note box Content string id
                          int   nCount,     // how many id
                          BOOL  bDo/*=1*/,
                          BOOL  bMoveCursor/*=1*/,
                          BOOL  bFilter/*=1*/,
                          int   nDelay,//55
                          int   nStep) //8
{
 CPoint pointDes;
 CPoint pointSrc;
 CRect rectWin;
 GetWindowRect(&rectWin);
 pointDes.x=rectWin.right-10;
 pointDes.y=rectWin.top+rectWin.Height()/2;
 GetCursorPos(&pointSrc);                              
 
 ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->
   MoveCursor(pointDes,pointSrc,bMoveCursor,nDelay,nStep,bMoveCursor);

 if(bDo)
	((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetNoteMsg(ID,nCount);
   
 ShowDropDown();                 
 
 uCase=nID;         
 ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetBusy(1);

 SetSysFilter(1);
 g_mbCanDoNext=0;

 SetProp(m_hWnd,"Filter",(HANDLE)bFilter);
    
 SetTimer(TIMER2,400,NULL);
 SetMouse(0);
}

void CTComboBox::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
    static times=-1;
    
   
   CComboBox::OnTimer(nIDEvent);
   times++;
   g_bCanDoExit=0;         
   
   if(times>int(uCase))
     {  
      if(times>int(uCase+3))
        {
	      KillTimer(TIMER2);
	      times=-1;
	      g_bCanDoExit=1;          
          g_mbCanDoNext=1;
          return;
        }        
      else
        if(times>int(uCase+1)) 
          return; 
          
      ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetBusy(0);
      ShowDropDown(0);
      SetCurSel(uCase); 
      if(GetProp(m_hWnd,"Filter"))
        { 
          SetSysFilter(0);
          SetMouse(1);
          RemoveProp(m_hWnd,"Filter");
        }  
      return;
     }

   PostMessage(WM_KEYDOWN,VK_DOWN,0);
}

/////////////////////////////////////////////////////////////////////////////
// CTListBox

CTListBox::CTListBox()
{
}

CTListBox::~CTListBox()
{
}

BEGIN_MESSAGE_MAP(CTListBox, CListBox)
	//{{AFX_MSG_MAP(CTListBox)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTListBox message handlers
void CTListBox::SetListBox(UINT stringID,   //list box content
                           int  nCount,
                           BOOL bDo/*=1*/)
{     
   if(bDo)
       ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetNoteMsg(0,0);
    
    CString listMsg;            
    for(int i=0;i<nCount;i++)
       {
        listMsg.LoadString(stringID+i);
	    AddString(listMsg.GetBuffer(listMsg.GetLength())); 
       }
}       


/////////////////////////////////////////////////////////////////////////////
// CTButton

CTButton::CTButton(UINT id, //=0  string id
                   int n,   //=0
                   BOOL bCommand)//=1 When operate by user for different fcous check
{ 
 ID=id;
 nCount=n;
 m_bCommand = bCommand;
}

CTButton::~CTButton()
{
}

BEGIN_MESSAGE_MAP(CTButton, CButton)
	//{{AFX_MSG_MAP(CTButton)
	ON_WM_TIMER()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_KILLFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTButton message handlers
void CTButton::SetRadioMsg(BOOL bUp/*=1*/,
                           CWnd *pParent/*=NULL*/,
                           int nLen/*=200*/,
                           int nHeight/*=100*/)
{           
 CDialog *pDlg=((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetAMsgDlg();          
     if(pDlg)
        pDlg->DestroyWindow();       
	 CRect rect,rectWin;
	 GetWindowRect(&rectWin);
	 rect.SetRect(rectWin.right,rectWin.top+rectWin.Height()/2,
	              rectWin.right+nLen,rectWin.top+rectWin.Height()/2+nHeight);
	 CArrowMsgDlg *pADlg=new CArrowMsgDlg(rect,ID,bUp,pParent);
	 pADlg->ShowWindow(SW_SHOW); 
}

void CTButton::SetRadioButton(BOOL  bDo/*=1*/,
                              BOOL  bCheck/*=1*/,
                              int   nDelay/*=55*/,
                              int   nStep /*=8*/)
{
             
 if(nCount==0 && ID==0)
   return;          
 CPoint pointDes;
 CPoint pointSrc;
 CRect rectWin;
 GetClientRect(&rectWin);
 ClientToScreen(&rectWin);
 pointDes.x=rectWin.left+rectWin.Width()/2;
 pointDes.y=rectWin.top+rectWin.Height()/2;
 GetCursorPos(&pointSrc);
 ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->
 MoveCursor(pointDes,pointSrc,1,nDelay,nStep);//,60,4);
 
 //SetCheck(1);
 if(bDo)
     ((CMainFrame *)(GetParent()->GetParent()))->SetNoteMsg(ID,nCount);

 PostMessage(WM_LBUTTONDOWN,0,0); 
 PostMessage(WM_LBUTTONUP,0,0); 

}                   

void CTButton::SetButtonDown(BOOL  bDo/*=1*/,
                             BOOL  bDown/*=1*/,
                             BOOL  bMoveCursor/*=1*/,
                             int   nDelay,//55
                             int   nStep)//8
{
 CPoint pointDes;
 CPoint pointSrc;
 CRect rectWin;
 GetWindowRect(&rectWin);
 pointDes.x=rectWin.left+rectWin.Width()/2;
 pointDes.y=rectWin.top+rectWin.Height()/2;
 GetCursorPos(&pointSrc);                           
 ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->
   MoveCursor(pointDes,pointSrc,bMoveCursor,nDelay,nStep,bMoveCursor);

  if(bDo && nCount && ID) 
     ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SetNoteMsg(ID,nCount);

 if(bDown==1)
   {
    for(int i=0;i<300;i++)
      SetState(bDown);
   }
 SetState(0);
 SetSysFilter(0);
}

void CTButton::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	CButton::OnTimer(nIDEvent);
}
void CTButton::OnKillFocus(CWnd* pNewWnd)
{      
	CButton::OnKillFocus(pNewWnd);
	
	// TODO: Add your message handler code here
	
}

void CTButton::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
/*	
	if(((CTDialog *)GetParent())->GetModal() && m_bCommand)
	   if(!((CTDialog *)GetParent())->Check())
	     {
	      MessageBeep(-1); 
	      SetState(0);
	      GetParent()->SetFocus();
	      return;
	     } 
*/
	  CButton::OnLButtonDown(nFlags, point);
}

void CTButton::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
/*
	if(((CTDialog *)GetParent())->GetModal() && m_bCommand)
	   if(!((CTDialog *)GetParent())->Check())
	      return;
*/
	CButton::OnLButtonUp(nFlags, point);
}



void CTButton::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	//if(((CTDialog *)GetParent())->GetModal() && m_bCommand)
	//   if(!((CTDialog *)GetParent())->Check())
	  //    return;
	
	CButton::OnMouseMove(nFlags, point);
}
/////////////////////////////////////////////////////////////////////////////
// CTStatic

CTStatic::CTStatic(UINT uID,  //string or bitmap
                   BOOL bText,/*=NULL*/
                   BOOL bTime)/*=NULL*/
          :CStatic(),ID(uID),m_bText(bText),m_bTimeMovie(bTime)
{
           
 m_bMovieStatus = m_bTimeMovie==TRUE ? 0 : 1;

 if(m_bText==ST_TEXT)       
   {
    m_pString  = new CString;
    if( m_bTimeMovie!=ST_TIME && uID)
      m_pString->LoadString(uID);
   }  
 else  
 if(m_bText==ST_BITMAP)  
   {
     m_pBitmap  = new CBitmap; 
     if( m_bTimeMovie!=ST_TIME && uID)
       {
        m_pBitmap->LoadBitmap(uID);
       } 
   }   
}

CTStatic::~CTStatic()
{         
 if( m_bText==ST_BITMAP )
   {
    // if(! m_bTimeMovie)
     m_pBitmap->DeleteObject();
     delete m_pBitmap;    
   }
 else    
     delete m_pString;
 
}


BEGIN_MESSAGE_MAP(CTStatic, CStatic)
	//{{AFX_MSG_MAP(CTStatic)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTStatic message handlers
void CTStatic::Paint(CString *pString,CDC *pPaintDC/*=NULL*/)
{
    CClientDC clientDC(this);

    CDC *pDC;
	    
	if( m_bTimeMovie)
	  {
	   *m_pString=*pString; 
	   pDC=(CDC *)&clientDC;
	  } 
	else
	if(pPaintDC)
	   pDC=pPaintDC;
	else
	   return;
	
	 CRect   rectWin;
	 GetClientRect(&rectWin);
	 pDC->SaveDC();
	 pDC->SetBkMode(TRANSPARENT);
	 pDC->DrawText(m_pString->GetBuffer(m_pString->GetLength()),
	             m_pString->GetLength(),&rectWin,DT_LEFT);
	 pDC->RestoreDC(-1);            
}

void CTStatic::Paint(CBitmap *pBitmap,CDC *pPaintDC/*=NULL*/)
{    
    CClientDC clientDC(this);

    CDC *pDC;
	    
	if( m_bTimeMovie)
	   pDC=(CDC *)&clientDC;
	else             
	if(pPaintDC)
	   pDC=pPaintDC;
	else
	   return;

 	CRect   rectWin;
	GetClientRect(&rectWin);
        
	 CDC *pMemDC = new CDC;
	 pMemDC->CreateCompatibleDC(pDC);
	 pMemDC->SaveDC();
	 pMemDC->SelectObject(pBitmap);
	 pDC->BitBlt(0,0,rectWin.Width(),rectWin.Height(),
	             pMemDC,0,0,SRCCOPY);
	 pMemDC->RestoreDC(-1);
	 pMemDC->DeleteDC();
	 delete pMemDC;
}

void CTStatic::OnPaint()
{
	 // device context for painting
	 CPaintDC dc(this);
	// TODO: Add your message handler code here
	if(m_bText==ST_TEXT)
       Paint(m_pString,&dc);
	else
	if(GetMovieStatus()==TRUE)
	  {
	   Paint(m_pBitmap,&dc);
	  } 
	// Do not call CStatic::OnPaint() for painting messages
}



