// StaticLink.cpp : implementation file
//

#include "pch.h"
#include "StaticLink.h"

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

/////////////////////////////////////////////////////////////////////////////
// CStaticLink

IMPLEMENT_DYNAMIC(CStaticLink, CStatic)

CStaticLink::CStaticLink()
	: m_bHit(false)
{
}

CStaticLink::~CStaticLink()
{
	m_Font.DeleteObject();
	m_UnderlineFont.DeleteObject();
}

BEGIN_MESSAGE_MAP(CStaticLink, CStatic)
	ON_WM_CTLCOLOR_REFLECT()
	ON_WM_TIMER()
	ON_CONTROL_REFLECT(STN_CLICKED, &CStaticLink::OnStnClicked)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStaticLink message handlers


void CStaticLink::PreSubclassWindow()
{
	LOGFONT log;
	CFont *pf = GetFont();
	pf->GetLogFont(&log);
	m_Font.CreateFontIndirect(&log);
	log.lfUnderline = 1;
	m_UnderlineFont.CreateFontIndirect(&log);
	ModifyStyle(0, SS_NOTIFY);
	SetTimer(0, 100, nullptr);
	CStatic::PreSubclassWindow();
}


HBRUSH CStaticLink::CtlColor(CDC *pDC, UINT nCtlColor)
{
	pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHT));
	pDC->SetBkColor(::GetSysColor(COLOR_BTNFACE));
	// transparent text.
	//pDC->SetBkMode (TRANSPARENT);
	return (HBRUSH)GetStockObject(NULL_BRUSH);
}


void CStaticLink::OnTimer(UINT_PTR nIDEvent)
{
	CPoint ptCursor;
	GetCursorPos(&ptCursor);
	CRect rcClient;
	GetClientRect(&rcClient);
	ClientToScreen(&rcClient);

	if (rcClient.PtInRect(ptCursor))
	{
		if (!m_bHit)
		{
			m_bHit = true;
			SetCursor(::LoadCursor(nullptr, IDC_HAND));
			SetFont(&m_UnderlineFont);
		}
	}
	else
	{
		if (m_bHit)
		{
			m_bHit = false;
			SetCursor(::LoadCursor(nullptr, IDC_ARROW));
			SetFont(&m_Font);
		}
	}

	CStatic::OnTimer(nIDEvent);
}


void CStaticLink::OnStnClicked()
{
	CString strURL;
	GetWindowText(strURL);
	ShellExecute(nullptr, _T("open"), strURL, nullptr, nullptr, SW_SHOW);
}


