#include "stdafx.h"
#include "GenericBkResizableMFC.h"
#include "GenericBkResizableMFCDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define IDS_MAILADDR	_T("mailto:info@softechsoftware.it")
#define IDS_WEBADDR		_T("http://www.softechsoftware.it")
#define	IDS_ICONSADDR	_T("http://www.softechsoftware.it/iconsdll.html")

#define	IDS_ICONS_REQVER	_T("1.0.0.1")

CGenericBkResizableMFCDlg::CGenericBkResizableMFCDlg(CWnd* pParent /*=NULL*/)
	: CBkResizableDialogST(CGenericBkResizableMFCDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGenericBkResizableMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CBkResizableDialogST::DoDataExchange(pDX);

	DDX_Control(pDX, IDOK, m_btnExit);

	DDX_Control(pDX, IDC_STCTITLE1, m_stcTitle1);
	DDX_Control(pDX, IDC_STCTITLE2, m_stcTitle2);
	DDX_Control(pDX, IDC_BUTTON1, m_btnButton1);
	DDX_Control(pDX, IDC_BUTTON2, m_btnButton2);
	DDX_Control(pDX, IDC_BUTTON3, m_btnButton3);
	DDX_Control(pDX, IDC_BUTTON4, m_btnButton4);

	DDX_Control(pDX, IDC_EMAILLINK, m_EMailLink);
	DDX_Control(pDX, IDC_HOMEPAGELINK, m_HomePageLink);
}

BEGIN_MESSAGE_MAP(CGenericBkResizableMFCDlg, CBkResizableDialogST)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CGenericBkResizableMFCDlg::OnInitDialog()
{
	BOOL	bIconsFound = TRUE;
	DWORD	dwIconsDLLresult = 0;

	CBkResizableDialogST::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// Registry home
	m_Registry.SetKeys(HKEY_CURRENT_USER, _T("Software\\SoftechSoftware\\GenericBkResizableMFC"));

	dwIconsDLLresult = LoadIconsDLL(&m_IconsDLL, IDS_ICONS_REQVER);
	switch (dwIconsDLLresult)
	{
		case 0:
			break;
		case 1:
			if (NotifyIconsNotFound(IDS_ICONS_REQVER) == TRUE)
			{
				PostQuitMessage(0);
				return TRUE;
			} // if
			break;
		case 2:
			if (NotifyIconsVersionMismatch(IDS_ICONS_REQVER) == TRUE)
			{
				PostQuitMessage(0);
				return TRUE;
			} // if
			break;
	} // if

	ShowSizeGrip(FALSE);
	SetBitmap(m_IconsDLL.LoadBitmap(DLL_IDB_BACKGROUND01, LR_SHARED));
	ActivateEasyMoveMode(TRUE);

	// Set HyperLink for E-Mail
	m_EMailLink.SetURL(IDS_MAILADDR);
	m_EMailLink.SetUnderline(CHyperLink::ulAlways);
	m_EMailLink.DrawTransparent(FALSE);

	// Set HyperLink for Home Page
	m_HomePageLink.SetURL(IDS_WEBADDR);
	m_HomePageLink.SetUnderline(CHyperLink::ulAlways);
	m_HomePageLink.DrawTransparent(FALSE);

	// Button exit
	//m_btnExit.SetThemeHelper(&m_Theme);
	m_btnExit.SetIcon(m_IconsDLL.LoadIcon(DLL_IDI_SET03_CANCEL, 32, 32));
	m_btnExit.SetTooltipText(_T("Close the application"));
	m_btnExit.SetBackgroundColor(RGB(145, 156, 187), FALSE);
	m_btnExit.SetBorderColors(RGB(255,255,255), RGB(10, 36, 106), FALSE);
	m_btnExit.SetOpacityPercentage(50, FALSE);
	m_btnExit.DrawTransparent();

	if (m_Theme.IsAppThemed() == FALSE)
	{
	} // if

	// Create font
	m_fontTitle.CreateFont(-20, 0, 0, 0, FW_NORMAL, FALSE, FALSE,0,0,0,0,0,0, _T("Verdana"));

	// Statics
	m_stcTitle1.SetFont(&m_fontTitle);
	m_stcTitle1.SetWindowText(_T("Normal CButtonST buttons"));
	m_stcTitle1.SetBackgroundColor(RGB(145, 156, 187), FALSE);
	m_stcTitle1.SetOpacityPercentage(50, FALSE);
	m_stcTitle1.DrawTransparent(FALSE);
	//
	m_stcTitle2.SetFont(&m_fontTitle);
	m_stcTitle2.SetWindowText(_T("CBlendedButtonST buttons"));
	m_stcTitle2.SetBackgroundColor(RGB(145, 156, 187), FALSE);
	m_stcTitle2.SetOpacityPercentage(50, FALSE);
	m_stcTitle2.DrawTransparent(FALSE);

	// Buttons
	m_btnButton1.SetIcon(m_IconsDLL.LoadIcon(DLL_IDI_SET04_OK, 48, 48));
	m_btnButton1.DrawTransparent();
	//
	m_btnButton2.SetIcon(m_IconsDLL.LoadIcon(DLL_IDI_SET04_CANCEL, 48, 48));
	m_btnButton2.DrawTransparent();
	//
	m_btnButton3.SetIcon(m_IconsDLL.LoadIcon(DLL_IDI_SET04_OK, 48, 48));
	m_btnButton3.SetBackgroundColor(RGB(69, 162, 54), FALSE);
	m_btnButton3.SetBorderColors(RGB(255,255,255), RGB(10, 36, 106), FALSE);
	m_btnButton3.SetOpacityPercentage(50, FALSE);
	m_btnButton3.DrawTransparent();
	//
	m_btnButton4.SetIcon(m_IconsDLL.LoadIcon(DLL_IDI_SET04_CANCEL, 48, 48));
	m_btnButton4.SetBackgroundColor(RGB(196, 60, 63), FALSE);
	m_btnButton4.SetBorderColors(RGB(255,255,255), RGB(10, 36, 106), FALSE);
	m_btnButton4.SetOpacityPercentage(50, FALSE);
	m_btnButton4.DrawTransparent();

	// Preset layout
	AddAnchor(m_btnExit.m_hWnd, BOTTOM_RIGHT);
	AddAnchor(m_EMailLink.m_hWnd, BOTTOM_LEFT);
	AddAnchor(m_HomePageLink.m_hWnd, BOTTOM_LEFT);

	return TRUE;
} // End of OnInitDialog

void CGenericBkResizableMFCDlg::OnPostEraseBkgnd(CDC* pDC)
{
	if (m_Theme.IsAppThemed() == FALSE)
	{
	} // if
	m_btnExit.SetBk(pDC);
	m_btnButton1.SetBk(pDC);

	m_EMailLink.SetBk(pDC);
	m_HomePageLink.SetBk(pDC);
} // End of OnPostEraseBkgnd

//	Return values:
//	0	= DLL loaded successfully
//	1	= DLL not found
//	2	= DLL version mismatch
//
DWORD CGenericBkResizableMFCDlg::LoadIconsDLL(CResourceServerST* pIconsDLL, LPCTSTR lpszReqVersion)
{
	TCHAR	szFilename[_MAX_PATH];
	TCHAR	szDrive[_MAX_PATH];   
	TCHAR	szDir[_MAX_PATH];   
	TCHAR	szPathToDLL[_MAX_PATH];
	DWORD	dwRetValue = 0;
	DWORD	dwRetValueFunc = 0;

	::GetModuleFileName(NULL, szFilename, _MAX_PATH);

	// Split path into components
	_tsplitpath_s(szFilename, szDrive, _MAX_PATH, szDir, _MAX_PATH, NULL, 0, NULL, 0);

	// Full path to DLL
	_stprintf_s(szPathToDLL, _MAX_PATH, _T("%s%s%s"), szDrive, szDir, _T("Icons.dll"));

	CFileVersionInfo fvi;
	TCHAR szVer[256] = { 0 };

	if (fvi.Open(szPathToDLL))
	{
		_stprintf_s(	szVer, 256,
						_T( "%d.%d.%d.%d" ),
						fvi.GetFileVersionMajor(),  // Major version
						fvi.GetFileVersionMinor(),  // Minor version
						fvi.GetFileVersionBuild(),  // Build number
						fvi.GetFileVersionQFE());     // QFE

		if (_tcscmp(szVer, lpszReqVersion) != 0)
		{
			dwRetValueFunc = 2;
			return dwRetValueFunc;
		} // if
	} // if

	if (pIconsDLL)
	{
		//AfxMessageBox(szPathToDLL);

		dwRetValue = pIconsDLL->Load(szPathToDLL);
		if (dwRetValue != RESSRVST_OK)
		{
			dwRetValueFunc = 1;
		} // if
	} // if

	return dwRetValueFunc;
} // End of LoadIconsDLL

//	Return values:
//	TRUE	= Open download link
//	FALSE	= Continue without icons
//
BOOL CGenericBkResizableMFCDlg::NotifyIconsNotFound(LPCTSTR lpszReqVersion)
{
	CHyperLink	hlWeb;
	CString		sMessage;
	BOOL		bRetValue = FALSE;
	int			nRetValue = 0;

	sMessage =	_T("Icons.dll not found.\r\n");
	sMessage +=	_T("This application requires Icons.dll version ");
	sMessage +=	lpszReqVersion;
	sMessage +=	_T("\r\n\r\n");
	sMessage +=	_T("This DLL is required by the application but is not ");
	sMessage +=	_T("essential to properly work.\r\n");
	sMessage +=	_T("Do you want to download the DLL ?\r\n\r\n");
	sMessage +=	_T("Press 'Yes' to go to download page on SoftechSoftware site.\r\n");
	sMessage +=	_T("Press 'No' to continue without icons and bitmaps.\r\n");

	nRetValue = MessageBox(	sMessage,
							_T("SoftechSoftware"),
							MB_ICONQUESTION | MB_YESNO);
	if (nRetValue == IDYES)
	{
		hlWeb.GotoURL(IDS_ICONSADDR, SW_SHOW);
		bRetValue = TRUE;
	} // if

	return bRetValue;
} // End of NotifyIconsNotFound

//	Return values:
//	TRUE	= Open download link
//	FALSE	= Continue with found DLL
//
BOOL CGenericBkResizableMFCDlg::NotifyIconsVersionMismatch(LPCTSTR lpszReqVersion)
{
	CHyperLink	hlWeb;
	CString		sMessage;
	BOOL		bRetValue = FALSE;
	int			nRetValue = 0;

	sMessage =	_T("This application requires Icons.dll version ");
	sMessage +=	lpszReqVersion;
	sMessage +=	_T("\r\n\r\n");
	sMessage +=	_T("A different version has been found installed with this application.\r\n");
	sMessage +=	_T("Do you want to download the correct version ?\r\n\r\n");
	sMessage +=	_T("Press 'Yes' to go to download page on SoftechSoftware site.\r\n");
	sMessage +=	_T("Press 'No' to continue with the installed version.\r\n");

	nRetValue = MessageBox(	sMessage,
							_T("SoftechSoftware"),
							MB_ICONQUESTION | MB_YESNO);
	if (nRetValue == IDYES)
	{
		hlWeb.GotoURL(IDS_ICONSADDR, SW_SHOW);
		bRetValue = TRUE;
	} // if

	return bRetValue;
} // End of NotifyIconsVersionMismatch
