// memwnd.cpp : implementation file
//

#include "stdafx.h"
#include "tutorial.h"
#include "TrcWnd.h"
#include "memwnd.h"
#include "mainfrm.h"
#include "memdlg.h"

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

extern BOOL g_bAuto;
extern void Delay(int nDelay,BOOL bShow=0);
void Mem_PopupMenu(CMenu &menu);
void Mem_PopupMenu(CMenu &menu)
{ 
 menu.CreatePopupMenu();
 menu.AppendMenu(MF_STRING,0,"&Refresh");
 menu.AppendMenu(MF_SEPARATOR);
 menu.AppendMenu(MF_STRING,0,"&Memory...");
 menu.AppendMenu(MF_STRING,0,"&Map...");
 menu.AppendMenu(MF_STRING,0,"&Goto...");   
 menu.AppendMenu(MF_SEPARATOR);
 menu.AppendMenu(MF_STRING|MF_CHECKED,0,"Show &ASCII");
 menu.AppendMenu(MF_SEPARATOR);
 menu.AppendMenu(MF_STRING,0,"&Hexadeciemal");
 menu.AppendMenu(MF_STRING,0,"&Signed Decimal");
 menu.AppendMenu(MF_STRING,0,"&Unsigned Decimal");
 menu.AppendMenu(MF_STRING,0,"&Diassamble");
 menu.AppendMenu(MF_SEPARATOR);
 menu.AppendMenu(MF_STRING,0,"&Byte");
 menu.AppendMenu(MF_STRING,0,"&Word");
 menu.AppendMenu(MF_STRING,0,"&Long");
}                     

/////////////////////////////////////////////////////////////////////////////
// CMemWnd

CMemWnd::CMemWnd(CRect rect)
:CAdvanceWnd("memory.dll","MEM_000")
{  
  m_pCaret.x=37;
  m_pCaret.y=19; 
  
//  ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->ClientToScreen(&rect);
  
  CreateEx(NULL,
           AfxRegisterWndClass(0),
           "Program Memory:1",
           WS_CHILDWINDOW|WS_CAPTION|WS_THICKFRAME|
           WS_SYSMENU|WS_MAXIMIZEBOX|WS_MINIMIZEBOX|
           WS_VISIBLE|WS_HSCROLL|WS_VSCROLL,
           rect.left,rect.top,rect.Width(),rect.Height(),
           (AfxGetApp()->m_pMainWnd->GetSafeHwnd()),NULL,NULL);
}

CMemWnd::~CMemWnd()
{
}

void CMemWnd::ModifyContent()
{                          
 SetSysFilter(1);
 CPoint pointDes;
 CPoint pointSrc;
 CRect rectWin;

 pointDes.x=181;
 pointDes.y=60;                           
 ClientToScreen(&pointDes);
 GetCursorPos(&pointSrc);
 ::SetCursor(::LoadCursor(NULL,IDC_IBEAM));
 ((CMainFrame *)AfxGetApp()->m_pMainWnd)->
 MoveCursor(pointDes,pointSrc,0,55,8,1);
 
 POINT pos[]={181,54, 189,54, 199,54, 207,54, 217,54, 225,54, 235,54, 243,54,
              253,54, 261,54, 271,54, 279,54, 289,54, 297,54};
              
 char  content[]={"1203FFEF1AFFFF"};
 CFont font;           
 LOGFONT logFont;
 _fmemset(&logFont,0,sizeof(LOGFONT));
 logFont.lfHeight=12;
 logFont.lfWidth=7;
// logFont.lfWeight=600;
 lstrcpy(logFont.lfFaceName,"Courier");
 font.CreateFontIndirect(&logFont);
 CClientDC dc(this);
 dc.SelectObject(&font); 
 dc.SetTextColor(RGB(128,0,0));
 for(int i=0; i<14; i++)
   { 
    HideCaret();
    dc.TextOut(pos[i].x,pos[i].y,&content[i],1);
    ShowCaret();
    SetMemCaret(pos[i].x+8,pos[i].y);  
    Delay(400);
   } 
 font.DeleteObject();                     
 ((CMainFrame *)AfxGetApp()->m_pMainWnd)->
 MoveCursor(pointSrc,pointDes,0,55,8);        
 ::SetCursor(::LoadCursor(NULL,IDC_ARROW));
}
BEGIN_MESSAGE_MAP(CMemWnd, CAdvanceWnd)
	//{{AFX_MSG_MAP(CMemWnd)
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMemWnd message handlers
void CMemWnd::OnSetFocus(CWnd* pOldWnd)
{
	CAdvanceWnd::OnSetFocus(pOldWnd);
	
	// TODO: Add your message handler code here
	CreateSolidCaret(2,15);    
	ShowCaret();          
    SetCaretPos(m_pCaret);
}

void CMemWnd::OnKillFocus(CWnd* pNewWnd)
{
	CAdvanceWnd::OnKillFocus(pNewWnd);
	
	// TODO: Add your message handler code here
	HideCaret();
}

//////////////////////////////////////////////////////////////////////
//          
void CMainFrame::AdvanceMemEdit()
{
  switch(m_uNextAction){
            case 1:  
              m_Trace.EnableWindow(FALSE);
              //m_Memory.EnableWindow(FALSE);
              Mem_Advance();       
              if(g_bAuto)
              {
        	   KillTimer(IDAUTO);
	           SetTimer(IDAUTO,5000,NULL);
	           SetSysMouse(1);
               SetSysKey(1);
	          } 
              break; 
            case 2: 
              Mem_Menu_Goto();
              Mem_Goto();    
              break;
            case 3:        
              Mem_Search();
              Delay(1000);
              m_pAdvanceWnd->Invalidate();
              m_pAdvanceWnd->UpdateWindow();
              break;
            case 4:  
              Mem_Modify();
              break;   
            case 5:  
              Mem_Mode();
              break;
            case 6:
              Mem_Modify_Code();
              m_uNextCaseID=ID_ADVANCE_MEM_DLG;
              m_uNextAction=0; 
			  if(m_pAdvanceWnd)
			    { m_pAdvanceWnd->DestroyWindow();
			      delete m_pAdvanceWnd;//)->MDIDestroy();
			      m_pAdvanceWnd=0;}
              m_pDlg=0;   
              break;
            default:                        
			  if(m_pAdvanceWnd)
			    {
			     bCase2Paint=1;            
			     m_pAdvanceWnd->DestroyWindow();
			     delete m_pAdvanceWnd;//)->MDIDestroy();
			      m_pAdvanceWnd=0;}
              m_pDlg=0;   
              m_uNextCaseID=ID_ADVANCE_MEM_DLG;
              m_uNextAction=0;
              break;
    }
}
/*-------------------------------------------------------*/
/*               Modify Memory Code                      */
/*-------------------------------------------------------*/
void CMainFrame::Mem_Modify_Code()
{
 CPoint pointSrc;
 CPoint pointDes(200,60);
 ((CMemWnd *)m_pAdvanceWnd)->ClientToScreen(&pointDes);
 GetCursorPos(&pointSrc);
 ((CMainFrame *)AfxGetApp()->m_pMainWnd)->
 MoveCursor(pointDes,pointSrc);
 Delay(400);
 m_pDlg = (CDialog *)new CMemAsmDlg();
 Delay(100);
 ((CMemAsmDlg *)m_pDlg)->ModifyCode();
}                                    

void CMainFrame::MemModifyCode()                        
{
 if(m_pAdvanceWnd)
   ((CMemWnd *)m_pAdvanceWnd)->LoadMsg("MEM_MODIFY_CODE",4,23);
 
 ClearDlg();  
}
/*-------------------------------------------------------*/
/*               Memory Mode                             */
/*-------------------------------------------------------*/
void CMainFrame::Mem_Mode()
{
 SetSysFilter(1);
 SetMouse(1);
 SetKey(1);
 CMenu menu;
 Mem_PopupMenu(menu);

	 m_nMenuCount=7; 
	 SetCursorPoint(170,240-4,55,8);
	 SetTimer(MENUTIMER,400,NULL);
	 menu.TrackPopupMenu(TPM_CENTERALIGN,200,100,
	                     CWnd::FromHandle(m_hWndMDIClient));
	 ((CMemWnd *)m_pAdvanceWnd)->
	 LoadMsg("MEM_SDEC"); 
     Delay(1000);   
     
	 m_nMenuCount=8; 
	 SetCursorPoint(170,250-4,55,8);
	 SetTimer(MENUTIMER,400,NULL);
	 menu.TrackPopupMenu(TPM_CENTERALIGN,200,100,
	                     CWnd::FromHandle(m_hWndMDIClient));
	 ((CMemWnd *)m_pAdvanceWnd)->                    
	 LoadMsg("MEM_UDEC"); 
     Delay(1000);   
     
	 m_nMenuCount=9; 
	 SetCursorPoint(170,270-4,55,8);
	 SetTimer(MENUTIMER,400,NULL);
	 menu.TrackPopupMenu(TPM_CENTERALIGN,200,100,
	                     CWnd::FromHandle(m_hWndMDIClient));
	 ((CMemWnd *)m_pAdvanceWnd)->
	 LoadMsg("MEM_ASM"); 
	                     

 menu.DestroyMenu();
}
/*-------------------------------------------------------*/
/*               Modify mem item                         */
/*-------------------------------------------------------*/              
void CMainFrame::Mem_Modify()
{ 
 m_pAdvanceWnd->SetFocus();
 ((CMemWnd *)m_pAdvanceWnd)->ModifyContent();
} 
/*-------------------------------------------------------*/
/*                    Search specify item                */
/*-------------------------------------------------------*/
void CMainFrame::Mem_Search()
{
  SetSysFilter(1);
  m_pDlg=(CDialog *)new CMemSearchDlg();
  Delay(1000);
  ((CMemSearchDlg *)m_pDlg)->SearchItem();
  SetMouse(1);    
  SetSysFilter(0);

}

void CMainFrame::MemSearch()
{
  ((CMemWnd *)m_pAdvanceWnd)->SetMemCaret(164,36);   
  Delay(50);
  ((CMemWnd*)m_pAdvanceWnd)->SetArrow(180,22);   
  ClearDlg();
} 

/*-------------------------------------------------------*/
/*                    Goto specify address               */
/*-------------------------------------------------------*/
void CMainFrame::Mem_Goto()
{           
  SetSysFilter(1);
  m_pDlg=(CDialog *)new CMemGotoDlg();
  Delay(1000);
  ((CMemGotoDlg *)m_pDlg)->SetAddress();
  SetMouse(1);    
  SetSysFilter(0);
}              
void CMainFrame::Mem_Menu_Goto()
{                

 ((CAdvanceWnd *)m_pAdvanceWnd)->SetArrow(40,12); 
 Delay(1000);
 CMenu menu;
 Mem_PopupMenu(menu);
 SetSysFilter(1);
 SetMouse(1);
 SetKey(1);
 m_nMenuCount=4; 
 SetCursorPoint(170,160,55,8);
 SetTimer(MENUTIMER,400,NULL);
 menu.TrackPopupMenu(TPM_CENTERALIGN,200,100,
                     CWnd::FromHandle(m_hWndMDIClient));

 menu.DestroyMenu(); 

}             

void CMainFrame::MemGoto()
{
 if(m_pAdvanceWnd)
   ((CAdvanceWnd *)m_pAdvanceWnd)->LoadMsg("MEM_START",4,23);
 Delay(50);  
 ((CAdvanceWnd *)m_pAdvanceWnd)->SetArrow(40,12);   
 //Delay(100);
 ClearDlg();
}
void CMainFrame::Mem_Advance()
{   
  CRect r;

  m_pMsgWnd->GetWindowRect(&r);
  //if(r.Width()>200)
     {
	  m_pMsgWnd->ShowWindow(SW_HIDE);
	  m_pMsgWnd->SetTitle("MEMORY");
	  m_pMsgWnd->MoveWindow(WinCxy.x+430,WinCxy.y+50,200,350);
	  m_pMsgWnd->SetContent("memory.TXT");
	  m_pMsgWnd->Invalidate();
	  m_pMsgWnd->ShowWindow(SW_SHOW);
     }
  CRect rect(25-10,256,379+25+8,285+8);
  m_pAdvanceWnd=(CWnd *)new  CMemWnd(rect);
  ((CMemWnd*)m_pAdvanceWnd)->ShowWindow(SW_SHOW);     
  Delay(100);   
  for(;rect.top>10;rect.top-=30)
     { 
      if(rect.top>10 && rect.top<=30)
         rect.top=10;
         bCase2Paint=0;
         m_pAdvanceWnd->MoveWindow(rect.left,rect.top,
                                   rect.Width(),rect.Height());
         Delay(60);}
         
// m_pAdvanceWnd->SetFocus();
}


