#ifndef _xbWINBASE_
#define _xbWINBASE_
/*<<<***************************************************************************
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.
***************************************************************************>>>*/

#ifndef _xbENGINE_
#	include "xbEngine.h"
#endif

#ifndef _xbSTRING_
#	include "xbString.h"
#endif

#ifndef _xbPTR_
#	include "xbPtr.h"
#endif

class xbWinBase;
class xbEXPORT  __declspec(uuid("{1DFCAD68-F64A-45a6-9ED4-A713E1D0C477}"))
	xbWinEngine:public xbEngine
{
public:
	xbWinEngine(const char *pLabel,const xbEngines &engUsed = xbEngines())
		:xbEngine(xbEngineStart_OnUse,pLabel,engUsed)
	{
	}
	virtual ~xbWinEngine()
	{
		Destroy();
	}
	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(xbWinEngine);
		return xbEngine::GetInterface(riid,ppInterf);
	}

	static LRESULT WINAPI DefWindowProcW(
		HWND hWnd,
		UINT Msg,
		WPARAM wParam,
		LPARAM lParam);

	xbWinBase *WinFromHwnd(xbHWND hWnd,xbOpMode opm =xbOp_Required);
	void WinRemove(xbWinBase *pWin,xbHWND hWnd);

	xbWinBase *getWin(int idx);
	int	getWinLength() {return m_arrWin.Length;}

	xbSectShare	m_sect;
	virtual bool isLockedByMe() const {return false;}
protected:
	virtual void onStart() 
	{
		xbRdbgAssert(m_arrWin.IsEmpty());
	}
	virtual bool onStop(bool);
protected:
	xbArray<xbWinBase*>	m_arrWin;
private:
	xbDclNoCopyForObject(xbWinEngine);
	xbDclNoNewForObject(xbWinEngine);
};
extern xbEXPORT xbWinEngine g_xbWinEngine;

class xbEXPORT  __declspec(uuid("{3E606124-0170-49b9-BE1A-95640347EFF4}"))
	xbWinClass:public xbEngine
{
public:
	xbWinClass(const char *pLabel,UINT style,WNDPROC lpfnWndProc,
		int cbClsExtra,int cbWndExtra,HICON hIcon,HCURSOR hCursor,HBRUSH hbrBackground,
		LPCWSTR lpszMenuName,const xbEngines &engUsed = xbEngines());
	xbWinClass(const char *pLabel,UINT style,WNDPROC lpfnWndProc = xbWinEngine::DefWindowProcW,
		const xbEngines &engUsed = xbEngines());
	virtual ~xbWinClass()
	{
		Destroy();
	}
	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(xbWinClass);
		return xbEngine::GetInterface(riid,ppInterf);
	}
	operator LPCWSTR() const
	{
		xbLock lckStart(this);
		return m_clsName;
	};
	virtual void InitCs(CREATESTRUCTW& cs)
	{
		xbLock lckStart(this);
		cs.lpszClass = m_clsName;
		cs.hInstance = m_wndclass.hInstance;
	}
	virtual bool isLockedByMe() const {return false;}
protected:
	virtual void onStart() 
	{
		xbAssert(m_hReg == 0);
		m_wndclass.hInstance = xbGetInstanceHandle();
		m_wndclass.lpszClassName = m_clsName;
		m_hReg = ::RegisterClassExW(&m_wndclass);
		if (m_hReg == 0) 
			xbThrowEsys(xbHR_LAST,"xbWinClass("+xbString(Label)+")::onStart() : RegisterClassEx failure");
	}
	virtual bool onStop(bool) 
	{
		if (m_hReg != 0)
		{
			m_hReg = 0;
			if (!::UnregisterClassW(m_clsName,m_wndclass.hInstance))
			{
				xbHR err = xbHR_LAST;
				xbString serr(::xbStrFromHR(err));
				xbRelOutput("xbWinClass("+xbString(Label)+")::onStop() : error : "+xbStrFromUlong((ulong)err,16,true)+" : "+serr);
			}
		}
		return true;
	}

	WNDCLASSEXW	m_wndclass;

	ATOM		m_hReg;
	xbStringW	m_clsName;
private:
	xbDclNoCopyForObject(xbWinClass);
	xbDclNoNewForObject(xbWinClass);
};

class xbEXPORT __declspec(uuid("{258362E7-A3CA-4f69-9C25-76C47133B864}"))
	xbWinBase:public xbObjectBase
{
public:
	xbWinBase()
	{
		m_hWnd = NULL;
	}
	virtual ~xbWinBase()
	{
		eProtect(Destroy());
	}
	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(xbWinBase);
		return xbObjectBase::GetInterface(riid,ppInterf);
	}
	void CreateEx(DWORD dwExStyle,LPCWSTR lpClassName,LPCWSTR lpWindowName,DWORD dwStyle,
		int x,int y,int width,int height,xbHWND hWndParent,HMENU hMenu,LPVOID lpParam);
	bool IsValid() const
	{
		return m_hWnd != NULL;
	}
	bool IsEmpty() const
	{
		return m_hWnd == NULL;
	}

	xbHWND GetHandle() const {return m_hWnd;}
protected:
	virtual void onBeforeDestruction(bool /*bInDestroy*/);

	virtual void Create(CREATESTRUCTW& cs);
	virtual void PreCreate(CREATESTRUCTW& cs);
	virtual LRESULT WindowProc(UINT msg, WPARAM wParam, LPARAM lParam);

	void setHwnd(xbHWND hWnd) {xbRdbgAssert(!m_hWnd);m_hWnd = hWnd;}
	void unsetHwnd() {xbRdbgAssert(m_hWnd != NULL);m_hWnd = NULL;}
	xbHWND m_hWnd;
private:
	xbDclNoCopyForObject(xbWinBase);

	friend xbWinEngine;
};

interface xbEXPORT __declspec(uuid("{17FE69AE-0094-48a2-8D30-19B41B6D779F}")) 
	sIxbWinBaseInCreate:public sIxbThreadContextObject
{
	virtual xbWinBase *GetWinBaseInCreate() = 0;
}; 

class xbEXPORT xbCtxWinBaseInCreate:public xbCtxBase, public sIxbWinBaseInCreate
{
public:
	xbCtxWinBaseInCreate(xbWinBase *pWin,bool bAddToThreadCtx = true)
	{
		m_pWin = pWin;
		if (bAddToThreadCtx)
			AddToThreadCtx();
	}
	virtual ~xbCtxWinBaseInCreate()
	{
		if (!m_pThCtx)
			return;
		eProtect(RemoveFromThreadCtx());
	}
	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(sIxbWinBaseInCreate);
		return xbCtxBase::GetInterface(riid,ppInterf);
	}

//sIxbThreadContextObject methods
	virtual void	ThreadContextRelease()
	{
		xbCtxBase::ThreadContextRelease();
	}
	virtual sIxbThreadContextObject *ThreadContextOld()
	{
		return xbCtxBase::ThreadContextOld();
	}
	virtual xbThreadEventObject *ThreadEvent()
	{
		return xbCtxBase::ThreadEvent();
	}
	virtual bool onThreadShouldStop( ulong evMask ) const
	{
		return xbCtxBase::onThreadShouldStop(evMask);
	}
	virtual bool onThreadMsg(MSG *pMsg,bool *pbQuit)
	{
		return xbCtxBase::onThreadMsg(pMsg,pbQuit);
	}

//sIxbWinBaseInCreate methods
	virtual xbWinBase *GetWinBaseInCreate() {return m_pWin;}

protected:
	xbWinBase *m_pWin;
};
#endif
