/*****************************************************************************
Open Source MIT License

Copyright (c) 2017 Matthew Ahmed

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*****************************************************************************/

// AboutDlg.cpp: implementation of the CAboutDlg class.
//
//////////////////////////////////////////////////////////////////////

#include "Headers.h"
#include "resource.h"
#include "Settings.h"
#include "AboutDlg.h"

CAboutDlg * pAboutDlg;

BOOL AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	return pAboutDlg->OwnProc(hDlg, message, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAboutDlg::CAboutDlg(HINSTANCE hInst, CSettings * pSet,
								 LPCTSTR sCapt1, LPCTSTR sCapt2, ZWin * pOwner)
	: ZDlg(hInst, sCapt1, sCapt2, pOwner)
{
	ini = pSet;
	pAboutDlg = this;
	m_pProc = AboutDlgProc;
	m_uID = IDD_ABOUT;
  m_bLinkCpic = FALSE;
  m_bLinkHome = FALSE;
  m_bLinkCursor = FALSE;
  m_hImage = NULL;
}

CAboutDlg::~CAboutDlg()
{

}

void CAboutDlg::OnDestroy()
{
  if (m_ulpOldCursor) // restore default, else won't show if About again
    SetClassLongPtr(m_hWnd, GCLP_HCURSOR, m_ulpOldCursor);
  if (m_hImage) DeleteObject(m_hImage);
}

BOOL CAboutDlg::OwnProc(HWND hDlg, UINT uMsg, WPARAM wP, LPARAM lP)
{
	switch (uMsg)
	{
		// Insert DTAKEMSG(WM_WHATEVER, OnWhatever); except for WM_COMMAND
    DSEEMSG(WM_PAINT, OnPaint);
    DRETMSGPARMS(WM_CTLCOLORSTATIC, OnCtlColorStatic);
    DSEEMSGPARMS(WM_MOUSEMOVE, OnMouseMove);
    DSEEMSG(WM_LBUTTONDOWN, OnMouseLBnDown);

// Don't bitch about no cases in WM_COMMAND's switch(wP) (warning 4065)
#pragma warning(push)
#pragma warning(disable : 4065)
		case WM_COMMAND:
			switch(LOWORD(wP))
			{
				// Insert DTAKECMD(IDC_BN_ETC, OnEtc);

				default : break;

			} // END switch(LOWORD(wP))
// Restore settings in effect at warning(push) above
#pragma warning(pop)

		default : break;
	}

	// ZDlg already calls OnInitDialog, OnOK, OnCancel, or returns FALSE
	return ZDlg::OwnProc(hDlg, uMsg, wP, lP);
}


BOOL CAboutDlg::OnInitDialog()
{
	ZDlg::OnInitDialog(); // always TRUE

	SendMessage(m_hWnd, WM_SETICON, TRUE,
							(LPARAM) LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_MAIN)));

  m_hImage = LoadImage(m_hInst, MAKEINTRESOURCE(IDB_ABOUT), IMAGE_BITMAP,
                        0, 0, LR_DEFAULTCOLOR); // LR_LOADTRANSPARENT);
  // Don't include LR_LOADTRANSPARENT if painting with TransparentBlt

  // Client coordinates for painting image
  GetControlRect(GetItem(IDCA_RECT_IMG), &m_rcImage);

  // For blue link text
  m_hTxtHome = GetItem(IDCA_TX_URL);
  m_hTxtMail = GetItem(IDCA_TX_MAIL);
  m_hTxtCredit = GetItem(IDCA_TX_CPIC);

  // For link mouse cursor
  GetControlRect(m_hTxtHome, &m_rcHome);
  GetControlRect(m_hTxtMail, &m_rcMail);
  GetControlRect(m_hTxtCredit, &m_rcCredit);

	m_ulpOldCursor = SetClassLongPtr(m_hWnd, GCLP_HCURSOR, NULL);
	if (m_ulpOldCursor)
	{
		m_bEnableCursor = TRUE;
    //m_hcLink = LoadCursor(m_hInst, IDC_HAND);
    m_hcLink = LoadCursor(NULL, IDC_HAND); // NULL for system supplied cursor
    m_bEnableCursor = (BOOL) m_hcLink;
	}
	else m_bEnableCursor = FALSE; // error, can't change cursors

// Leave version number set in RC dialog file:
  //if (!GetVersionNumStr(sBS))
  //  _tcscpy(sBS, _T("3")); // Just be close enough; no point showing error msg

  //_stprintf(sBS2, _T("Version %s"), sBS);
  //SetItemText(IDCA_TX_VERSION, sBS2);

  // Don't mess with extracting from VERSION_INFO, hard code copyright
//  if (!GetCopyrightStr(sBS))
//  {
    _tcscpy(sBS, _T("Copyright © 2017 Matthew Ahmed"));
//  }
  SetItemText(IDCA_TX_COPYRIGHT, sBS);

  return TRUE;
}

void CAboutDlg::OnPaint()
{
  HDC hdc = GetDC(m_hWnd);
  HDC image_dc = CreateCompatibleDC(hdc);
  SelectObject(image_dc, m_hImage);

  BOOL bRet = TransparentBlt(
                  hdc,        // handle to destination DC
                  //15,   // x-coord of destination upper-left corner
                  m_rcImage.left,   // x-coord of destination upper-left corner
                  //10,   // y-coord of destination upper-left corner
                  m_rcImage.top,   // y-coord of destination upper-left corner
                  80,     // width of destination rectangle
                  80,    // height of destination rectangle
                  image_dc,         // handle to source DC
                  0,    // x-coord of source upper-left corner
                  0,    // y-coord of source upper-left corner
                  80,      // width of source rectangle
                  80,     // height of source rectangle
                  //0xFF0000  // color to make transparent
                  RGB(255, 0, 0)  // color to make transparent
              );

  DeleteDC(image_dc); // Won't paint on subsequent calls without this
  ReleaseDC(m_hWnd, hdc); // Mem usage grows without this
}

BOOL CAboutDlg::OnCtlColorStatic(WPARAM wp_hDC, LPARAM lp_hCtl)
{
  if ((HWND) lp_hCtl == m_hTxtHome || (HWND)lp_hCtl == m_hTxtMail || (HWND)lp_hCtl == m_hTxtCredit)
  {
    SetTextColor((HDC)wp_hDC, RGB(0, 0, 255));
    SetBkColor((HDC)wp_hDC, GetSysColor(COLOR_3DFACE));

    // Restore default for others
    return (BOOL) GetSysColorBrush(COLOR_3DFACE); 
  }
  return FALSE; // Default handling
}

void CAboutDlg::OnMouseMove(WPARAM, LPARAM lp_XloYhi)
{
  static LONG x;
  static LONG y;

  // point is client units, will compare to client unit rectangles
	x = (LONG) LOWORD(lp_XloYhi);
	y = (LONG) HIWORD(lp_XloYhi);

  if (x >= m_rcHome.left && x <= m_rcHome.right
			&& y >= m_rcHome.top && y <= m_rcHome.bottom) // In "link" static
	{
    m_bLinkHome = TRUE;
    m_bLinkMail = FALSE;
    m_bLinkCpic = FALSE;
  }
  else if (x >= m_rcMail.left && x <= m_rcMail.right
    && y >= m_rcMail.top && y <= m_rcMail.bottom)
  {
    m_bLinkHome = FALSE;
    m_bLinkMail = TRUE;
    m_bLinkCpic = FALSE;
  }
  else if (x >= m_rcCredit.left && x <= m_rcCredit.right
    && y >= m_rcCredit.top && y <= m_rcCredit.bottom)
  {
    m_bLinkHome = FALSE;
    m_bLinkMail = FALSE;
    m_bLinkCpic = TRUE;
  }
  else
	{
    m_bLinkHome = FALSE;
    m_bLinkMail = FALSE;
    m_bLinkCpic = FALSE;
  }

  if (m_bEnableCursor)
  {
    if (m_bLinkHome || m_bLinkMail || m_bLinkCpic)
    {
      if (!m_bLinkCursor)
      {
        m_bLinkCursor = TRUE;
        SetCursor(m_hcLink);
      }
    }
    else // ! (m_bLinkHome || m_bLinkMail ||  m_bLinkCpic)
    {
      if (m_bLinkCursor)
      {
        m_bLinkCursor = FALSE;
        SetCursor((HCURSOR) m_ulpOldCursor);
      }
    }
  } // if (m_bEnableCur)
} // void CAboutDlg::OnMouseMove(WPARAM wIgnore, LPARAM lp_XloYhi)

void CAboutDlg::OnMouseLBnDown()
{
  // Only matters if cursur is in link text rectangle.
  // Don't bother calculating with LPARAM, use boolean set in OnMouseMove
  if (m_bLinkHome)
    fnShellExecOpen(_T("http://www.geocities.ws/shaken/"));

  else if (m_bLinkMail)
    fnShellExecOpen(_T("mailto:MatthewFoggAhmed@gmail.com?subject=Shaken,%20not%20stirred"));

  else if (m_bLinkCpic)
    fnShellExecOpen(_T("https://www.codeproject.com/Articles/915/Simple-class-for-drawing-pictures-JPG-TIFF-GIF-etc"));
}

BOOL CAboutDlg::fnShellExecOpen(LPCTSTR lpFile)
{
  // lpFile can be a URL
  nBS = (int) ShellExecute(m_hWnd, _T("open"), lpFile,
                           NULL, NULL, SW_SHOWNORMAL);
  if (nBS < 33)
  {
    _stprintf(sBS, _T("ShellExecute failed, returned %d"), nBS);
    return fnSysErr(sBS);
  }
  return TRUE;
}

void CAboutDlg::OnOk()
{
	// Delete here and in .h if not adding anything

	ZDlg::OnOk();
}

