
#include "pch.h"

#include "DocPaneDlgViewBase.h"
#include "BKMessageBox.h"
#include "Config.h"
#include "Screen_Sizes.h"

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


// Диалоговое окно CDocPaneDlg

IMPLEMENT_DYNAMIC(CDocPaneDlg, CDialogEx)

CDocPaneDlg::CDocPaneDlg(UINT IDD, CWnd *pParent /*=nullptr*/)
	: CDialogEx(IDD, pParent)
	, m_IDD(IDD)
	, m_hAccelTable(nullptr)
{
}

CDocPaneDlg::~CDocPaneDlg()
{
	m_hFont.DeleteObject();
}


BEGIN_MESSAGE_MAP(CDocPaneDlg, CDialogEx)
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

BOOL CDocPaneDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	m_hAccelTable = ::LoadAccelerators(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));
	::SetMonospaceFont(GetSafeHwnd(), &m_hFont);
	// Подсказки
	m_ToolTip.Create(this, WS_POPUP | TTS_NOPREFIX | TTS_USEVISUALSTYLE | TTS_ALWAYSTIP/* | TTS_BALLOON*/);
	m_ToolTip.SetDelayTime(TTDT_AUTOPOP, 20000);
	m_ToolTip.SetDelayTime(TTDT_INITIAL, 300);
	// *** you could change the default settings ***
	m_ToolTip.SetMaxTipWidth(400);
	// m_ToolTip.SetTipBkColor(RGB(255, 255, 255));
	// m_ToolTip.SetTipTextColor(RGB(255, 0, 0));
	// Create layout
	m_Resizer.Init(this);
	return TRUE;
}

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

	if (pMsg->message >= WM_MOUSEFIRST && pMsg->message <= WM_MOUSELAST)
	{
		m_ToolTip.RelayEvent(pMsg);
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}


CSize CDocPaneDlg::GetMinSize()
{
	CSize size;
	MINMAXINFO mmi;
	m_Resizer.GetMinMaxInfo(&mmi);
	size.cx = mmi.ptMinTrackSize.x;
	size.cy = mmi.ptMinTrackSize.y;
	return size;
}
// отменяем действие кнопок enter и esc в диалоге
void CDocPaneDlg::OnOK()
{}

void CDocPaneDlg::OnCancel()
{}

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

void CDocPaneDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);
	// Resize layout to current window size
	m_Resizer.OnSize(cx, cy);
}

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


/////////////////////////////////////////////////////////////////////////////
// CDocPaneDlgViewBase
IMPLEMENT_DYNAMIC(CDocPaneDlgViewBase, CDockablePane)

CDocPaneDlgViewBase::CDocPaneDlgViewBase(UINT nErrID)
	: m_pDlg(nullptr)
	, m_nErrID(nErrID)
{}

CDocPaneDlgViewBase::~CDocPaneDlgViewBase()
    = default;

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

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

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

	if (!m_pDlg->Create(m_pDlg->m_IDD, this))
	{
		g_BKMsgBox.Show(m_nErrID);
		TRACE0("Не удалось создать диалог.\n");
		return -1;      // не удалось создать
	}

	AdjustLayout();
	return 0;
}

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

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

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

void CDocPaneDlgViewBase::OnSetFocus(CWnd *pOldWnd)
{
	CDockablePane::OnSetFocus(pOldWnd);
	m_pDlg->SetFocus();
}

void CDocPaneDlgViewBase::AdjustLayout()
{
	CDockablePane::AdjustLayout();

	if (GetSafeHwnd())
	{
		CSize size = m_pDlg->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_pDlg->MoveWindow(0, 0, minx, miny, FALSE);
		RecalcLayout();
	}
}

void CDocPaneDlgViewBase::OnPaint()
{
	CPaintDC dc(this); // device context for painting

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