
#include "pch.h"

#include "DisasmView.h"
#include "BKMessageBox.h"
#include "Config.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDisasmView
IMPLEMENT_DYNAMIC(CDisasmView, CDockablePane)

CDisasmView::CDisasmView()
    = default;

CDisasmView::~CDisasmView()
    = default;

BEGIN_MESSAGE_MAP(CDisasmView, CDockablePane)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_SETFOCUS()
	ON_WM_PAINT()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// обработчики сообщений CDisasmView

int CDisasmView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
	{
		return -1;
	}

	if (!m_dlgDisasm.Create(IDD_BKDISASM_DLG, this))
	{
		g_BKMsgBox.Show(IDS_BK_ERROR_NOCDISASMDLGINIT);
		TRACE0("Не удалось создать диалог Дизассемблера\n");
		return -1;      // не удалось создать
	}

	AdjustLayout();
	return 0;
}

void CDisasmView::OnAfterFloat()
{
	CDockablePane::OnAfterFloat();
	AdjustLayout();
}

void CDisasmView::OnAfterDockFromMiniFrame()
{
	CDockablePane::OnAfterDockFromMiniFrame();
	AdjustLayout();
}

void CDisasmView::OnSize(UINT nType, int cx, int cy)
{
	AdjustLayout();
	CDockablePane::OnSize(nType, cx, cy);
}

void CDisasmView::OnSetFocus(CWnd *pOldWnd)
{
	CDockablePane::OnSetFocus(pOldWnd);
	m_dlgDisasm.SetFocus();
}

void CDisasmView::AdjustLayout()
{
	if (GetSafeHwnd())
	{
		CSize size=m_dlgDisasm.GetMinSize();
		int c = GetCaptionHeight();
		size.cy += c;
		SetMinSize(size);
		CRect rectWnd;
		GetWindowRect(rectWnd);
		int minx = max(size.cx, rectWnd.Width());
		int miny = max(size.cy, rectWnd.Height());
		m_dlgDisasm.SetWindowPos(nullptr, 0, 0, minx, miny, SWP_NOACTIVATE | SWP_NOZORDER);
	}

	CDockablePane::AdjustLayout();
}

void CDisasmView::OnPaint()
{
	CPaintDC dc(this); // если эту штуку закомментировать, то перестаёт работать перерисовка окна

	// TODO: добавьте свой код обработчика сообщений
	// Не вызывать CDockablePane::OnPaint() для сообщений рисования
	if (IsVisible())
	{
		m_dlgDisasm.Invalidate(FALSE);
	}
}
