/*************************************************
*           BK Emulator v3.9 for Windows         *
*************************************************/


// DisasmDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DisasmDlg.h"
#include "afxdialogex.h"
#include "Globals.h"
#include "Debugger.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDisasmDlg dialog

CDisasmDlg::CDisasmDlg(CWnd *pParent /*=nullptr*/)
	: CDialogEx(CDisasmDlg::IDD, pParent)
	, m_hAccelTable(nullptr)
{
}

CDisasmDlg::~CDisasmDlg()
{
}

void CDisasmDlg::DoDataExchange(CDataExchange *pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_DISASM_LIST, m_listDisasm);
	DDX_Control(pDX, IDC_DA_DEBUG_ADDRESS, m_AddrDisasm);
}


BEGIN_MESSAGE_MAP(CDisasmDlg, CDialogEx)
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
	ON_WM_SETFOCUS()
	ON_MESSAGE(WM_ENTEREDIT_VALUE_UPDATE, &OnDisasmAddressChange)
	ON_MESSAGE(WM_DEBUG_ADDRESS_UPDATE, &OnDisasmAddressUpdate)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDisasmDlg message handlers

BOOL CDisasmDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));
	m_Resizer.Init(this);
	m_Resizer.AddCtrl(IDC_DISASM_LIST, DT_LEFT | DT_TOP | DT_RIGHT | DT_BOTTOM, FALSE, FALSE);
	m_Resizer.AddCtrl(IDC_DA_DEBUG_ADDRESS, DT_LEFT | DT_TOP, TRUE, TRUE);
	m_AddrDisasm.SetLimitText(6);
	m_listDisasm.Init();
	return TRUE; // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDisasmDlg::AttachDebugger(CDebugger *pDebugger)
{
	m_pDebugger = pDebugger;
	m_listDisasm.AttachDebugger(pDebugger);
	m_pDebugger->AttachWnd(&m_listDisasm);
	SetDlgItemText(IDC_DA_DEBUG_ADDRESS, WordToOctString(m_pDebugger->GetCurrentAddress()));
}

//отменяем действие кнопок enter и esc в диалоге
void CDisasmDlg::OnOK()
{
}

void CDisasmDlg::OnCancel()
{
}


void CDisasmDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);
	m_Resizer.OnSize(cx, cy);
}

void CDisasmDlg::OnGetMinMaxInfo(MINMAXINFO *lpMMI)
{
	m_Resizer.GetMinMaxInfo(lpMMI);
}


LRESULT CDisasmDlg::OnDisasmAddressChange(WPARAM wParam, LPARAM lParam)
{
	CString strBuf;
	GetDlgItemText(IDC_DA_DEBUG_ADDRESS, strBuf.GetBufferSetLength(10), 10);
	strBuf.ReleaseBuffer();
	WORD nAddr = OctStringToWord(strBuf);
	//обновим значение, чтобы оно было всегда 6 значным
	SetDlgItemText(IDC_DA_DEBUG_ADDRESS, WordToOctString(nAddr));
	m_pDebugger->SetCurrentAddress(nAddr);
	return S_OK;
}

LRESULT CDisasmDlg::OnDisasmAddressUpdate(WPARAM wParam, LPARAM lParam)
{
	SetDlgItemText(IDC_DA_DEBUG_ADDRESS, WordToOctString(m_pDebugger->GetCurrentAddress()));
	return S_OK;
}


void CDisasmDlg::OnSetFocus(CWnd *pOldWnd)
{
	CDialogEx::OnSetFocus(pOldWnd);
	m_listDisasm.SetFocus();
}


//для того, чтобы акселераторы работали, их надо загрузить и в этот диалог
//и передавать главному фрейму.
// способа проще найти не получилось.
BOOL CDisasmDlg::PreTranslateMessage(MSG *pMsg)
{
	if (m_hAccelTable)
	{
		if (::TranslateAccelerator(AfxGetMainWnd()->GetSafeHwnd(), m_hAccelTable, pMsg))
		{
			return TRUE;
		}
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}
