/***************************************************************************
**
**    $Header$
**
**    $Log$
** 
****************************************************************************/

/////////////////////////////////////////////////////////////////////////////
//
//  File name:  DSMEDIT1.CPP
//
//  Description:The implement file for the class: CDsmEdit1
//
//  Author:     Lane Li		&		Daniel Lin
//
//  Date:       09/10/95
//
//  Modification:
//               
//		1. 10/25/95, 0.1d version  
//					modified function	OnDsmedit1Button1()
//										OnDsmedit1Button2()
//      1. 09/10/95, Initial version
//
//
//  Copyright (C) 1995 Microtek International. All rights reserved.
//
/////////////////////////////////////////////////////////////////////////////

// dsmedit1.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
//#include "ep196.h"
#include "dsmedit1.h"
#include "address.h"
#include "uicom2.h"
#include "memsvr.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDsmEdit1 dialog


CDsmEdit1::CDsmEdit1(CWnd* pParent /*=NULL*/)
    : CDialog(CDsmEdit1::IDD, pParent)
{
    //{{AFX_DATA_INIT(CDsmEdit1)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    m_pParent = pParent;
}

void CDsmEdit1::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDsmEdit1)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDsmEdit1, CDialog)
    //{{AFX_MSG_MAP(CDsmEdit1)
    ON_BN_CLICKED(IDC_DSMEDIT1_BUTTON1, OnDsmedit1Button1)
    ON_BN_CLICKED(IDC_DSMEDIT1_BUTTON2, OnDsmedit1Button2)
    ON_BN_CLICKED(IDC_DSMEDIT1_BUTTON3, OnDsmedit1Button3)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CDsmEdit1 message handlers

BOOL CDsmEdit1::OnInitDialog()
{
    CDialog::OnInitDialog();
    CenterWindow();
    
    // TODO: Add extra initialization here
    char text[20];
    CWnd* ctrl;
    
    sprintf(text,"%04lX",m_dwAddress);
    SetDlgItemText(IDC_DSMEDIT1_EDIT1,text);
    
    ctrl=GetDlgItem(IDC_DSMEDIT1_EDIT2);
    ctrl->SetFocus();
    
    return FALSE;  // return TRUE  unless you set the focus to a control
}

void CDsmEdit1::OnDsmedit1Button1()
{
    // TODO: Add your control notification handler code here
    char text[20];
    char text2[20];
    long l;
    BYTE b;
    int i;
    BOOL bOk;
    
    bOk = TRUE;
    
    GetDlgItemText(IDC_DSMEDIT1_EDIT2,text,4);
    l = strtol(text,NULL,16);
    if ((text[0]=='0') && (strlen(text)==2)) 
       sprintf(text2,"%02lX",l);
    else sprintf(text2,"%lX",l);
    if (_stricmp(text,text2)!=0)
        bOk=FALSE;
    i = strlen(text);
    if ((i<=0)||(i>2))
        bOk=FALSE;
    if (!bOk)
    {
        MessageBeep(-1);
        MessageBox("Input Error",NULL,MB_OK|MB_ICONEXCLAMATION);        
        return;
    }

    b = (BYTE)l;   
	text[3] = '\0';    
    //MemServerFill(m_dwAddress,b,m_wSpaceType);
    ADDRESS adr1, adr2;
    adr1.adrSpace=adr2.adrSpace=(::ADDR_SPACE)m_wSpaceType;
    adr1.adrAddress=m_dwAddress;
    adr2.adrAddress=m_dwAddress;
    MemServerFill (adr1, b, 2);
    EndDialog(1);
}

void CDsmEdit1::OnDsmedit1Button2()
{
    // TODO: Add your control notification handler code here
    char text[20];
    char text2[20];
    long l;
    BYTE b;
    int i;
    BOOL bOk;
    
    bOk = TRUE;
    
    GetDlgItemText(IDC_DSMEDIT1_EDIT2,text,4);
    l = strtol(text,NULL,16);
    if ((text[0]=='0')&&(strlen(text)==2)) sprintf(text2,"%02lX",l);
    else sprintf(text2,"%lX",l);
    if (_stricmp(text,text2)!=0)
        bOk=FALSE;
    i = strlen(text);
    if ((i<=0)||(i>2))
        bOk=FALSE;
    if (!bOk)
    {
        MessageBeep(-1); 
        MessageBox("Input Error",NULL,MB_OK|MB_ICONEXCLAMATION);                
        return;
    }
    
    b = (BYTE)l;
    //MemServerFill(m_dwAddress,b,m_wSpaceType);
    text[3] = '\0';
	ADDRESS adr1, adr2;
    adr1.adrSpace=adr2.adrSpace=(::ADDR_SPACE)m_wSpaceType;
    adr1.adrAddress=m_dwAddress;
    adr2.adrAddress=m_dwAddress;
    MemServerFill (adr1, b, 2);
    //modify by chris, 3/31/97
    //m_pParent->InvalidateRect(NULL,FALSE);
    RepaintMemory();
    ///RepaintSource();
    m_dwAddress++;
    sprintf(text,"%04lX",m_dwAddress);
    SetDlgItemText(IDC_DSMEDIT1_EDIT1,text);
    SetDlgItemText(IDC_DSMEDIT1_EDIT2,"");
}

void CDsmEdit1::OnDsmedit1Button3()
{
    // TODO: Add your control notification handler code here
    EndDialog(0);
}
