/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

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.
////////////////////////////////////////////////////////////////////////////////
#Title: xbWinBase
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<17/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

#include "xbWinBase.h"
#include "_xbVer.h"

xbWinEngine g_xbWinEngine("xdcBase xbWinEngine");
xbWinClass g_xbWinClassDefault("xdcBase xbWinClassDefault",CS_CLASSDC);

int xbHWNDfind(xbWinBase*const &file1,const void *pFile2)
{
	xbHWND hWnd1 = file1->GetHandle();
	xbHWND hWnd2 = (xbHWND)pFile2;

	if (hWnd1 > hWnd2)
		return 1;
	if (hWnd1 < hWnd2)
		return -1;
	return 0;
}

bool xbWinEngine::onStop(bool) 
{
	xbWinBase *pOld = NULL;
	while(m_arrWin.IsValid())
	{
		xbWinBase *pToClose = getWin(0);
		if (pToClose == pOld)
			break;
		pOld = pToClose;
		eProtectTrace(pToClose->Destroy(););
	}
	return m_arrWin.IsEmpty();
}

xbWinBase *xbWinEngine::WinFromHwnd(xbHWND hWnd,xbOpMode opm)
{
	xbLock lckStart(this);

	xbSectLock lck(&m_sect);
	int pos;
	xbWinBase **ppWin = m_arrWin.SortedFind(hWnd,xbHWNDfind,&pos);
	if (ppWin)
		return *ppWin;
	if (opm == xbOp_Optional)
		return NULL;

	xbTctxGet<sIxbWinBaseInCreate> winInCreate(false);
	xbWinBase *pWin = winInCreate.Get()->GetWinBaseInCreate();
	xbRdbgAssert(pWin);

	m_arrWin.Insert(pos,pWin);
	pWin->setHwnd(hWnd);

	return pWin;
}

void xbWinEngine::WinRemove(xbWinBase *pWin,xbHWND hWnd)
{
	xbSectLock lck(&m_sect);
	int pos;
	xbWinBase **ppWin = m_arrWin.SortedFind(hWnd,xbHWNDfind,&pos);
	xbRdbgAssert(ppWin && *ppWin == pWin);
	if (!ppWin)
		return;
	m_arrWin.Delete(pos);
	pWin->unsetHwnd();
}

xbWinBase *xbWinEngine::getWin(int idx)
{
	xbSectLock lck(&m_sect);
	if (idx < 0 || idx >= m_arrWin.Length)
		return NULL;
	return m_arrWin.GetAt(idx);
}

LRESULT WINAPI xbWinEngine::DefWindowProcW(
	HWND hWnd,
	UINT msg,
	WPARAM wParam,
	LPARAM lParam)
{
	eProtectBegin();
	xbWinBase *pWin = g_xbWinEngine.WinFromHwnd(hWnd);
	return pWin->WindowProc(msg,wParam,lParam);
	eProtectEndTrace();
	return 0;
}

xbWinClass::xbWinClass(const char *pLabel,UINT style,WNDPROC lpfnWndProc,
	int cbClsExtra,int cbWndExtra,HICON hIcon,HCURSOR hCursor,HBRUSH hbrBackground,
	LPCWSTR lpszMenuName,const xbEngines &engUsed)
	:xbEngine(xbEngineStart_OnUse,pLabel,engUsed)
{
	m_hReg = 0;
	memset(&m_wndclass,0,sizeof(m_wndclass));
	m_wndclass.cbSize = sizeof(m_wndclass);
	m_wndclass.style = style;
	m_wndclass.lpfnWndProc = lpfnWndProc;
	m_wndclass.cbClsExtra = cbClsExtra;
	m_wndclass.cbWndExtra = cbWndExtra;
	m_wndclass.hIcon = hIcon;
	m_wndclass.hCursor = hCursor;
	m_wndclass.hbrBackground = hbrBackground;
	m_wndclass.lpszMenuName = lpszMenuName;
	m_clsName = xbStringW(pLabel) + xbStringW(" V" XDCBASE_HSVER);
}

xbWinClass::xbWinClass(const char *pLabel,UINT style,WNDPROC lpfnWndProc,
	const xbEngines &engUsed)
	:xbEngine(xbEngineStart_OnUse,pLabel,engUsed)
{
	m_hReg = 0;
	memset(&m_wndclass,0,sizeof(m_wndclass));
	m_wndclass.cbSize = sizeof(m_wndclass);
	m_wndclass.style = style;
	m_wndclass.lpfnWndProc = lpfnWndProc;

	m_clsName = xbStringW(pLabel) + xbStringW(" V" XDCBASE_HSVER);
}

void xbWinBase::onBeforeDestruction(bool /*bInDestroy*/)
{
	if (m_hWnd == NULL)
		return;
	if (!::DestroyWindow(m_hWnd))
		xbThrowEsys(xbHR_LAST,"xbWinBase::Destroy failure");
	xbRdbgAssert(m_hWnd == NULL);
	m_hWnd = NULL;
}

void xbWinBase::CreateEx(DWORD dwExStyle, LPCWSTR lpszClassName,
	LPCWSTR lpszWindowName, DWORD dwStyle,
	int x, int y, int width, int height,
	HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)
{
	xbObjectBase::Create();
	// allow modification of several common create parameters
	CREATESTRUCTW cs;
	cs.dwExStyle = dwExStyle;
	cs.lpszClass = lpszClassName;
	cs.lpszName = lpszWindowName;
	cs.style = dwStyle;
	cs.x = x;
	cs.y = y;
	cs.cx = width;
	cs.cy = height;
	cs.hwndParent = hWndParent;
	cs.hMenu = nIDorHMenu;
	cs.hInstance = xbGetInstanceHandle();
	cs.lpCreateParams = lpParam;

	Create(cs);
}

void xbWinBase::Create(CREATESTRUCTW& cs)
{
	PreCreate(cs);

	xbCtxWinBaseInCreate winInCreate(this);
	xbHWND hWnd = ::CreateWindowExW(cs.dwExStyle, cs.lpszClass,
			cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,
			cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);
	winInCreate.RemoveFromThreadCtx();

	// m_hWnd should have been set in send msg hook
	if (!hWnd)
		xbThrowEsys(xbHR_LAST,"xbWinBase::Create failure");
	if (m_hWnd != hWnd)
		xbThrowEsys("xbWinBase::Create failure (2)");
}

void xbWinBase::PreCreate(CREATESTRUCTW& cs) 
{
	if (!cs.lpszClass)
		g_xbWinClassDefault.InitCs(cs);
}

LRESULT xbWinBase::WindowProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
	LRESULT ret = ::DefWindowProcW(m_hWnd,msg,wParam,lParam);

	if (msg == WM_DESTROY)
		g_xbWinEngine.WinRemove(this,m_hWnd);

	return ret;
}

