#include "pch.h"
#include "framework.h"
#include "PreviewPane.h"

BEGIN_MESSAGE_MAP(CPreviewPane, CWnd)
    ON_WM_PAINT()
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONDBLCLK()
    ON_WM_RBUTTONUP()
END_MESSAGE_MAP()

bool CPreviewPane::CreatePane(CWnd* parent, int index)
{
    m_index = index;
    CString cls = AfxRegisterWndClass(CS_DBLCLKS, ::LoadCursor(NULL, IDC_ARROW), (HBRUSH)::GetStockObject(BLACK_BRUSH), NULL);
    return CreateEx(0, cls, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
                    CRect(0, 0, 100, 100), parent, 2000 + index) != FALSE;
}

void CPreviewPane::OnPaint()
{
    CPaintDC dc(this);
    CRect rc; GetClientRect(&rc);
    dc.FillSolidRect(rc, RGB(15, 15, 15));
    CPen pen(PS_SOLID, m_selected ? 2 : 1, m_selected ? RGB(255,0,0) : RGB(255,255,255));
    CPen* oldPen = dc.SelectObject(&pen);
    dc.SelectStockObject(NULL_BRUSH);
    dc.Rectangle(rc);
    dc.SelectObject(oldPen);
    if (!m_active) {
        dc.SetTextColor(RGB(180,180,180));
        dc.SetBkMode(TRANSPARENT);
        dc.DrawTextW(L"No Video Source", rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    }
}


void CPreviewPane::OnLButtonDown(UINT nFlags, CPoint point)
{
    CWnd* parent = GetParent();
    if (parent) {
        ClientToScreen(&point);
        parent->ScreenToClient(&point);
        parent->SendMessage(WM_HWS_PREVIEW_CLICK, (WPARAM)m_index, MAKELPARAM(point.x, point.y));
    }
    CWnd::OnLButtonDown(nFlags, point);
}

void CPreviewPane::OnLButtonDblClk(UINT nFlags, CPoint point)
{
    CWnd* parent = GetParent();
    if (parent) {
        ClientToScreen(&point);
        parent->ScreenToClient(&point);
        parent->SendMessage(WM_HWS_PREVIEW_DBLCLICK, (WPARAM)m_index, MAKELPARAM(point.x, point.y));
    }
    CWnd::OnLButtonDblClk(nFlags, point);
}

void CPreviewPane::OnRButtonUp(UINT nFlags, CPoint point)
{
    CWnd* parent = GetParent();
    if (parent) {
        ClientToScreen(&point);
        parent->ScreenToClient(&point);
        parent->SendMessage(WM_HWS_PREVIEW_RBUTTON, (WPARAM)m_index, MAKELPARAM(point.x, point.y));
    }
    CWnd::OnRButtonUp(nFlags, point);
}
