//-----------------------------------------------------------------------------
// Purpose: Calculates which panel the cursor is currently over and sets it up
//			as the current mouse focus.
//-----------------------------------------------------------------------------
void CInputWin32::UpdateMouseFocus(int x, int y)
{
	// find the panel that has the focus
	VPanel *focus = NULL; 

	InputContext_t *pContext = GetInputContext( m_hContext );

	if (!pContext->_rootPanel)
	{
		if (surface()->IsCursorVisible() && surface()->IsWithin(x, y))
		{
			// faster version of code below
			// checks through each popup in order, top to bottom windows
			for (int i = surface()->GetPopupCount() - 1; i >= 0; i--)
			{
				VPanel *popup = (VPanel *)surface()->GetPopup(i);
				VPanel *panel = popup;
				bool wantsMouse = panel->IsMouseInputEnabled();
				bool isVisible;

				do 
				{
					isVisible = panel->IsVisible();
					panel = panel->GetParent();
				}
				while ( isVisible && panel && panel->GetParent() ); // only consider panels that want mouse input

				if ( wantsMouse && isVisible ) 
				{
					focus = (VPanel *)popup->Client()->IsWithinTraverse(x, y, false);
					if (focus)
						break;
				}
			}
			if (!focus)
			{
				focus = (VPanel *)((VPanel *)surface()->GetEmbeddedPanel())->Client()->IsWithinTraverse(x, y, false);
			}
		}
	}
	else
	{
		focus = (VPanel *)((VPanel *)(pContext->_rootPanel))->Client()->IsWithinTraverse(x, y, false);
	}

	// mouse focus debugging code
	/*
	static VPanel *oldFocus = (VPanel *)0x0001;
	if (oldFocus != focus)
	{
		oldFocus = focus;
		if (focus)
		{
			ivgui()->DPrintf2("mouse over: (%s, %s)\n", focus->GetName(), focus->GetClassName());
		}
		else
		{
			ivgui()->DPrintf2("mouse over: (NULL)\n");
		}
	}
	*/

	// check if we are in modal state, 
	// and if we are make sure this panel is a child of us.
	if (!IsChildOfModalPanel((VPANEL)focus))
	{	
		// should this be _appModalPanel?
		focus = NULL;
	}

	SetMouseFocus((VPANEL)focus);
}
//-----------------------------------------------------------------------------
// Purpose: Calculate the new key focus
//-----------------------------------------------------------------------------
VPanel *CInputWin32::CalculateNewKeyFocus()
{
	InputContext_t *pContext = GetInputContext(m_hContext);

	// get the top-order panel
	VPanel *top = NULL;
	VPanel *wantedKeyFocus = NULL;

	if (!pContext->_rootPanel)
	{
		if (surface()->GetPopupCount() > 0)
		{
			// find the highest-level window that is both visible and a popup
			int index = surface()->GetPopupCount();
			
			while (index)
			{
				
				top = (VPanel *)surface()->GetPopup(--index);

				// traverse the heirachy and check if the popup really is visible
				if (top->IsPopup() && top->IsVisible() && top->IsKeyBoardInputEnabled() && !surface()->IsMinimized((VPANEL)top))
				{
					bool IsVisible = top->IsVisible();
					VPanel *p = top->GetParent();
					// drill down the heirachy checking that everything is visible
					while(p && IsVisible)
					{
						if( p->IsVisible()==false)
						{
							IsVisible=false;
							break;
						}
						p=p->GetParent();
					}

					if (IsVisible && !surface()->IsMinimized((VPANEL)top) )
					{
						break;
					}
				}
				
				top = NULL;
			} 
		}
	}
	else
	{
		top = (VPanel *)pContext->_rootPanel;
	}

	if (top)
	{
		// ask the top-level panel for what it considers to be the current focus
		wantedKeyFocus = (VPanel *)top->Client()->GetCurrentKeyFocus();
		if (!wantedKeyFocus)
		{
			wantedKeyFocus = top;

		}
	}

	// check to see if any of this surfaces panels have the focus
	if (!surface()->HasFocus())
	{
		wantedKeyFocus=NULL;
	}

	// check if we are in modal state, 
	// and if we are make sure this panel is a child of us.
	if (!IsChildOfModalPanel((VPANEL)wantedKeyFocus))
	{	
		wantedKeyFocus=NULL;
	}

	return wantedKeyFocus;
}