/*****************************************************************************
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.
*****************************************************************************/

// WinDlg.cpp: implementation of the ZDlg class.
//
//////////////////////////////////////////////////////////////////////

//#include "Headers.h"
#include "ZWinDlg.h"

ZDlg::ZDlg(HINSTANCE hInst, LPCTSTR sCapt1, LPCTSTR sCapt2,
           ZWin * pOwner, ZWinAppData * pzwd)
	: ZWin(hInst, sCapt1, sCapt2, pOwner, pzwd)
{
	// Derived class must reset these
	m_pProc = NULL;
	m_uID = 0;
}

INT ZDlg::Run() // Usual modal dialog
{
	if (!m_pProc || !m_uID)
	{
		fnErr(_T("Lack DLGPROC or resource"));
		return -1;
	}

	return DialogBox(m_hInst, MAKEINTRESOURCE(m_uID),
											m_hOwner, (DLGPROC) m_pProc);
}

HWND ZDlg::RunModeless()
{
	if (!m_pProc || !m_uID)
	{
		fnErr(_T("Lack DLGPROC or resource"));
		return NULL;
	}

	m_hWnd = CreateDialog(m_hInst, MAKEINTRESOURCE(m_uID),
                        m_hOwner, (DLGPROC) m_pProc);
  return m_hWnd;
}

BOOL ZDlg::OwnProc(HWND hDlg, UINT uMsg, WPARAM wP, LPARAM lP)
{
	switch (uMsg)
	{
		case WM_INITDIALOG:
			m_hWnd = hDlg;
			if (!OnInitDialog()) // base virtual always returns TRUE
				EndDialog(m_hWnd, FALSE);
			return TRUE;

    DSEEMSG(WM_CLOSE, OnClose);
    DSEEMSG(WM_DESTROY, OnDestroy);

		case WM_COMMAND:
			switch(LOWORD(wP))
			{
				DTAKECMD(IDOK, OnOk);
				DTAKECMD(IDCANCEL, OnCancel);
				default : break;
			}

		default : break;
	}

	return FALSE;  // Didn't process a message
} // END OwnProc

BOOL ZDlg::OnInitDialog()
{
	SetWindowText(m_hWnd, m_sCaptLong);
	CenterWnd(m_hOwner);
	return TRUE;
}

void ZDlg::OnClose()  // Not called by OK
{
  // Does nothing, just to have virtual WM_CLOSE handler in place
  // Windows' default handling happens after this
}

void ZDlg::OnDestroy()
{
  // Does nothing, just to have virtual WM_DESTROY handler in place
  // Windows' default handling happens after this
}

void ZDlg::OnOk()
{
	EndDialog(m_hWnd, TRUE);
}

void ZDlg::OnCancel()
{
	EndDialog(m_hWnd, FALSE);
}

// Utility functions
void ZDlg::GetControlRect(HWND hCtl, RECT * pRect)
{
  POINT p;
  p.x = 0;
  p.y = 0; // Upper left point of m_hWnd's client area
  ClientToScreen(m_hWnd, &p); // Screen coordinates of client upper left

  GetWindowRect(hCtl, pRect); // hCtl's rectangle in screen coordinates
  pRect->left -= p.x;
  pRect->top -= p.y;
  pRect->right -= p.x;
  pRect->bottom -= p.y;
  // Now rectangle is in client coordinates
}
