#include "StdAfx.h"
#include "AplGuiEx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////
// The message hook map is derived from CMapPtrToPtr, which associates
// a pointer with another pointer. It maps an HWND to a CSubclassWnd, like
// the way MFC's internal maps map HWND's to CWnd's. The first CSubclassWnd
// attached to a window is stored in the map; all other CSubclassWnd's for that
// window are then chained via CSubclassWnd::m_pNext.
//
class CSubclassWndMap : private CMapPtrToPtr {
public:
	CSubclassWndMap();
	~CSubclassWndMap();
	static CSubclassWndMap& GetHookMap();
	void Add(HWND hwnd, CComboTreeSubclassWnd* pSubclassWnd);
	void Remove(CComboTreeSubclassWnd* pSubclassWnd);
	void RemoveAll(HWND hwnd);
	CComboTreeSubclassWnd* Lookup(HWND hwnd);
};

// This trick is used so the hook map isn't
// instantiated until someone actually requests it.
//
#define	theHookMap	(CSubclassWndMap::GetHookMap())

IMPLEMENT_DYNAMIC(CComboTreeSubclassWnd, CWnd);

CComboTreeSubclassWnd::CComboTreeSubclassWnd()
{
	m_pNext = NULL;
	m_pOldWndProc = NULL;	
	m_hWnd  = NULL;
}

CComboTreeSubclassWnd::~CComboTreeSubclassWnd()
{
	if (m_hWnd) 
		HookWindow((HWND)NULL);		// unhook window
}

//////////////////
// Hook a window.
// This installs a new window proc that directs messages to the CSubclassWnd.
// pWnd=NULL to remove.
//
BOOL CComboTreeSubclassWnd::HookWindow(HWND hwnd)
{
	ASSERT_VALID(this);
	if (hwnd) {
		// Hook the window
		ASSERT(m_hWnd==NULL);
		ASSERT(::IsWindow(hwnd));
		theHookMap.Add(hwnd, this);			// Add to map of hooks

	} else if (m_hWnd) {
		// Unhook the window
		theHookMap.Remove(this);				// Remove from map
		m_pOldWndProc = NULL;
	}
	m_hWnd = hwnd;
	return TRUE;
}

//////////////////
// Window proc-like virtual function which specific CSubclassWnds will
// override to do stuff. Default passes the message to the next hook; 
// the last hook passes the message to the original window.
// You MUST call this at the end of your WindowProc if you want the real
// window to get the message. This is just like CWnd::WindowProc, except that
// a CSubclassWnd is not a window.
//
LRESULT CComboTreeSubclassWnd::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
{
	//	ASSERT_VALID(this);  // removed for speed
	ASSERT(m_pOldWndProc);
	return m_pNext ? m_pNext->WindowProc(msg, wp, lp) :	
		::CallWindowProc(m_pOldWndProc, m_hWnd, msg, wp, lp);
}

//////////////////
// Like calling base class WindowProc, but with no args, so individual
// message handlers can do the default thing. Like CWnd::Default
//
LRESULT CComboTreeSubclassWnd::Default()
{
	// MFC stores current MSG in thread state
	MSG& curMsg = AfxGetThreadState()->m_lastSentMsg;
	// Note: must explicitly call CSubclassWnd::WindowProc to avoid infinte
	// recursion on virtual function
	return CComboTreeSubclassWnd::WindowProc(curMsg.message, curMsg.wParam, curMsg.lParam);
}

#ifdef _DEBUG
void CComboTreeSubclassWnd::AssertValid() const
{
	CObject::AssertValid();
	ASSERT(m_hWnd==NULL || ::IsWindow(m_hWnd));
	if (m_hWnd) {
		for (CComboTreeSubclassWnd* p = theHookMap.Lookup(m_hWnd); p; p=p->m_pNext) {
			if (p==this)
				break;
		}
		//ASSERT(p); // should have found it!
	}
}

void CComboTreeSubclassWnd::Dump(CDumpContext& dc) const
{
	CObject::Dump(dc);
}

#endif

//////////////////
// Subclassed window proc for message hooks. Replaces AfxWndProc (or whatever
// else was there before.)
//
LRESULT CALLBACK
HookWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
#ifdef _USRDLL
	// If this is a DLL, need to set up MFC state
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
#endif

	// Set up MFC message state just in case anyone wants it
	// This is just like AfxCallWindowProc, but we can't use that because
	// a CSubclassWnd is not a CWnd.
	//
	MSG& curMsg = AfxGetThreadState()->m_lastSentMsg;
	MSG  oldMsg = curMsg;   // save for nesting
	curMsg.hwnd		= hwnd;
	curMsg.message = msg;
	curMsg.wParam  = wp;
	curMsg.lParam  = lp;

	// Get hook object for this window. Get from hook map
	CComboTreeSubclassWnd* pSubclassWnd = theHookMap.Lookup(hwnd);
	ASSERT(pSubclassWnd);

	LRESULT lr;
	if (msg==WM_NCDESTROY) {
		// Window is being destroyed: unhook all hooks (for this window)
		// and pass msg to orginal window proc
		//
		WNDPROC wndproc = pSubclassWnd->m_pOldWndProc;
		theHookMap.RemoveAll(hwnd);
		lr = ::CallWindowProc(wndproc, hwnd, msg, wp, lp);

	} else {
		// pass to msg hook
		lr = pSubclassWnd->WindowProc(msg, wp, lp);
	}
	curMsg = oldMsg;			// pop state
	return lr;
}

////////////////////////////////////////////////////////////////
// CSubclassWndMap implementation
//
CSubclassWndMap::CSubclassWndMap()
{
}

CSubclassWndMap::~CSubclassWndMap()
{
	// This assert bombs when posting WM_QUIT, so I've deleted it.
	//	ASSERT(IsEmpty());	// all hooks should be removed!	
}

//////////////////
// Get the one and only global hook map
// 
CSubclassWndMap& CSubclassWndMap::GetHookMap()
{
	// By creating theMap here, C++ doesn't instantiate it until/unless
	// it's ever used! This is a good trick to use in C++, to
	// instantiate/initialize a static object the first time it's used.
	//
	static CSubclassWndMap theMap;
	return theMap;
}

/////////////////
// Add hook to map; i.e., associate hook with window
//
void CSubclassWndMap::Add(HWND hwnd, CComboTreeSubclassWnd* pSubclassWnd)
{
	ASSERT(hwnd && ::IsWindow(hwnd));

	// Add to front of list
	pSubclassWnd->m_pNext = Lookup(hwnd);
	SetAt(hwnd, pSubclassWnd);

	if (pSubclassWnd->m_pNext==NULL) {
		// If this is the first hook added, subclass the window
		pSubclassWnd->m_pOldWndProc = 
			(WNDPROC)SetWindowLong(hwnd, GWL_WNDPROC, (DWORD)HookWndProc);

	} else {
		// just copy wndproc from next hook
		pSubclassWnd->m_pOldWndProc = pSubclassWnd->m_pNext->m_pOldWndProc;
	}
	ASSERT(pSubclassWnd->m_pOldWndProc);
}

//////////////////
// Remove hook from map
//
void CSubclassWndMap::Remove(CComboTreeSubclassWnd* pUnHook)
{
	HWND hwnd = pUnHook->m_hWnd;
	ASSERT(hwnd && ::IsWindow(hwnd));

	CComboTreeSubclassWnd* pHook = Lookup(hwnd);
	ASSERT(pHook);
	if (pHook==pUnHook) {
		// hook to remove is the one in the hash table: replace w/next
		if (pHook->m_pNext)
			SetAt(hwnd, pHook->m_pNext);
		else {
			// This is the last hook for this window: restore wnd proc
			RemoveKey(hwnd);
			SetWindowLong(hwnd, GWL_WNDPROC, (DWORD)pHook->m_pOldWndProc);
		}
	} else {
		// Hook to remove is in the middle: just remove from linked list
		while (pHook->m_pNext!=pUnHook)
			pHook = pHook->m_pNext;
		ASSERT(pHook && pHook->m_pNext==pUnHook);
		pHook->m_pNext = pUnHook->m_pNext;
	}
}

//////////////////
// Remove all the hooks for a window
//
void CSubclassWndMap::RemoveAll(HWND hwnd)
{
	CComboTreeSubclassWnd* pSubclassWnd;
	while ((pSubclassWnd = Lookup(hwnd))!=NULL)
		pSubclassWnd->HookWindow((HWND)NULL);	// (unhook)
}

/////////////////
// Find first hook associate with window
//
CComboTreeSubclassWnd* CSubclassWndMap::Lookup(HWND hwnd)
{
	CComboTreeSubclassWnd* pFound = NULL;
	if (!CMapPtrToPtr::Lookup(hwnd, (void*&)pFound))
		return NULL;
	ASSERT_KINDOF(CComboTreeSubclassWnd, pFound);
	return pFound;
}




////////////////////////////////////////////////////////////////
CComboTreeHook::CComboTreeHook()
{
	m_pComboTree = NULL;
	m_hParent = NULL;
}

CComboTreeHook::~CComboTreeHook()
{
}

//////////////////
// Initialize hooked parent and tree
// Install hook (optional)
//
void CComboTreeHook::Init(HWND hParent, CComboTree* pComboTree, BOOL bHookNow)
{
	m_pComboTree = pComboTree;
	m_hParent = hParent;
	if (bHookNow)
	{
		CComboTreeSubclassWnd::HookWindow(hParent);
	}
}

//////////////////
// "Hook" function traps messages sent to parent
//
LRESULT CComboTreeHook::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
{
	BOOL bHideDropDown = FALSE;
	switch (msg)
	{
	case WM_COMMAND:
		bHideDropDown = bHideDropDown = !IsMsgOK( msg, wp, lp);	
		break;
	case WM_SYSCOMMAND:
	case WM_SYSKEYDOWN:
	case WM_LBUTTONDOWN:
	case WM_NCLBUTTONDOWN:
	case WM_RBUTTONDOWN:
	case WM_NCRBUTTONDOWN:
	case WM_MBUTTONDOWN:
	case WM_NCMBUTTONDOWN:
	case WM_WINDOWPOSCHANGING:
	case WM_WINDOWPOSCHANGED:
		bHideDropDown = TRUE;	
		break;
	default:
		break;
	};

	LRESULT ret = CComboTreeSubclassWnd::WindowProc(msg, wp, lp);
	if (bHideDropDown)
	{
//		TRACE("ParentWindowProc() Hiding Active Dropdown\n");
		if (m_pComboTree)
		{
			m_pComboTree->SendParentComboMessage(CBN_KILLFOCUS);
			m_pComboTree->OnCancel();
		}
	}
	return ret;
}

//////////////////
// Filter parent window messages. Hide tree if not a notification message from tree 
//
BOOL CComboTreeHook::IsMsgOK(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(wParam);

	BOOL bRet = FALSE;

	if (nMsg != WM_COMMAND)
	{
		return bRet;
	}

	if (!m_pComboTree)
	{
		return bRet;
	}


	if (lParam == NULL)
	{
		//message not from a control
		return bRet;
	}

	UINT nID = m_pComboTree->GetDlgCtrlID();
	if (LOWORD(wParam) == nID)
	{
		return TRUE;
	}

	return bRet;
}

//Must call this when tree is dropped down
void CComboTreeHook::HookParent()
{
	ASSERT( m_pComboTree && m_hParent);

	if (m_pComboTree && m_hParent)
	{
		CComboTreeSubclassWnd::HookWindow(m_hParent);
	}
}

//Must call this when tree is hidden
void CComboTreeHook::UnhookParent()   
{
	ASSERT( m_pComboTree && m_hParent);

	if (m_pComboTree && m_hParent)
	{
		CComboTreeSubclassWnd::Unhook();
	}
}
