void WgPopupLayer::_stealKeyboardFocus()
{
	// Get event handler, verify that we have a root

	if( !Hook() )
		return;

	WgEventHandlerPtr pHandler = Hook()->EventHandler();
	if( !pHandler )
		return;

	// Save old keyboard focus, which we assume belonged to previous menu in hierarchy.

	if( m_popupHooks.Size() < 2 )
		m_pKeyFocus = pHandler->KeyboardFocus().RawPtr();
	else
		m_popupHooks.Last()->Prev()->m_pKeyFocus = pHandler->KeyboardFocus().RawPtr();

	// Steal keyboard focus to top menu

	WgWidget * pWidget = m_popupHooks.Last()->_widget();

	if( pWidget->IsInstanceOf( WgPanel::CLASSNAME ) && static_cast<WgPanel*>(pWidget)->IsFocusGroup() )
		pHandler->SetFocusGroup(static_cast<WgPanel*>(pWidget));
	else
		pHandler->SetKeyboardFocus(pWidget);
}
WgWidget * WgTablePanel::FindWidget( const WgCoord& ofs, WgSearchMode mode )
{
	// Check cell content

	int	xOfs;
	int yOfs;
	WgTableRow* pRow;

	int col = _getMarkedColumn( ofs.x, xOfs );
	int	row = _getMarkedRow( ofs.y, pRow, yOfs );

	WgWidget * pWidget = 0;

	if( col != -1 && row != -1 && pRow->GetHook(col)->IsVisible() )
	{
		if( xOfs < m_cellPadding.left || xOfs >= m_pColumns[col].m_realWidth - m_cellPadding.right ||
			yOfs < m_cellPadding.top || yOfs >= pRow->Height() - m_cellPadding.bottom )
		{
			pWidget = pRow->GetWidget(col);
			if( mode == WG_SEARCH_GEOMETRY || pWidget->MarkTest( WgCoord( xOfs-m_cellPadding.left, yOfs-m_cellPadding.top) )  )
				return pWidget;
		}
	}

	// Check ourselves

	if( mode == WG_SEARCH_GEOMETRY || MarkTest( ofs ) )
		return this;
	else
		return 0;
}
void WgPackList::_onRender( WgGfxDevice * pDevice, const WgRect& _canvas, const WgRect& _window, const WgRect& _clip )
{
	WgRect contentRect = _listCanvas() + _canvas.Pos();

	if( m_pSkin )
	{
		m_pSkin->Render( pDevice, contentRect, m_state, _clip );
		contentRect = m_pSkin->ContentRect( contentRect, m_state );
	}

	int startOfs = m_bHorizontal ? _clip.x-contentRect.x : _clip.y-contentRect.y;
	if( startOfs < 0 )
		startOfs = 0;

	for( int i = _getEntryAt( startOfs ) ; i < m_hooks.Size() ; i++ )
	{
		WgPackListHook * pHook = m_hooks.Hook(i);
		WgWidget * pChild = pHook->_widget();

		// Get entry geometry, skin and state

		WgRect entryGeo( contentRect );
		if( m_bHorizontal )
		{
			if( pHook->m_ofs >= contentRect.w )
				break;

			entryGeo.x += pHook->m_ofs;
			entryGeo.w = pHook->m_length;
		}
		else
		{
			if( pHook->m_ofs >= contentRect.h )
				break;

			entryGeo.y += pHook->m_ofs;
			entryGeo.h = pHook->m_length;
		}
		
		WgSkin * pEntrySkin	= m_pEntrySkin[i&0x1].RawPtr();
		WgState	state		= pChild->State();
//		WgRect	childGeo( entryGeo );

		// Render entry skin, shrink child geo

		if( pEntrySkin )
		{
			pEntrySkin->Render( pDevice, entryGeo, state, _clip );
//			childGeo = pEntrySkin->ContentRect( entryGeo, state );
		}

		// Render child

//		pChild->_onRender( pDevice, childGeo, childGeo, _clip );

	}
}
WgWidget *  WgPopupLayer::_findWidget( const WgCoord& ofs, WgSearchMode mode )
{
	// MenuPanel has its own _findWidget() method since we need special treatment of
	// searchmode ACTION_TARGET when a menu is open.

	if( mode == WG_SEARCH_ACTION_TARGET && !m_popupHooks.IsEmpty() )
	{
		// In search mode ACTION_TARGET we limit our target to us, our menu-branches and the menu-opener if a menu is open.

		WgPopupHook * pHook = m_popupHooks.Last();
		WgWidget * pResult = 0;

		while( pHook && !pResult )
		{
			if( pHook->m_geo.Contains( ofs ) )
			{
				if( pHook->_widget()->IsContainer() )
					pResult = static_cast<WgContainer*>(pHook->_widget())->_findWidget( ofs - pHook->m_geo.Pos(), mode );
				else if( pHook->_widget()->MarkTest( ofs - pHook->m_geo.Pos() ) )
					pResult = pHook->_widget();
			}
			pHook = pHook->_prev();
		}

		if( pResult == 0 )
		{
			// Check the first opener
			
			WgPopupHook * pHook = m_popupHooks.First();
			if( pHook && pHook->m_pOpener )
			{
				WgWidget * pOpener = pHook->m_pOpener.RawPtr();

				WgCoord absPos 		= ofs + GlobalPos();
				WgRect	openerGeo 	= pOpener->GlobalGeo();

				if( openerGeo.Contains(absPos) && pOpener->MarkTest( absPos - openerGeo.Pos() ) )
					pResult = pOpener;
			}
			
			// Fall back to us.
			
			if( pResult == 0 )
				pResult = this;
		}
		return pResult;
	}
	else
	{
		// For the rest of the modes we can rely on the default method.

		return WgContainer::_findWidget( ofs, mode );
	}
}
void WgScrollbarTarget::_updateScrollbar( float pos, float size )
{
		WgWidget * pWidget = m_pScrollbar.RawPtr();

		if( pWidget )
		{
			if( pWidget->IsInstanceOf(WgScrollbar::CLASSNAME) )
					static_cast<WgScrollbar*>(pWidget)->_setHandle(pos,size);
		}

}
void WgPackList::_onNewSize( const WgSize& size )
{
	m_size = size;
	int newContentBreadth;

	if( m_bHorizontal )
		newContentBreadth = size.h;
	else
		newContentBreadth = size.w;

	if( newContentBreadth != m_contentBreadth )
	{
		m_contentBreadth = newContentBreadth;
		int ofs = 0;

		for( int i = 0 ; i < m_hooks.Size() ; i++ )
		{
			WgPackListHook * pHook = m_hooks.Hook(i);
			WgWidget * pWidget = pHook->_widget();

			if( m_bHorizontal )
			{
				int newEntryLength = _paddedLimitedMatchingWidth(pWidget, newContentBreadth );
				pHook->m_ofs = ofs;
				pHook->m_length = newEntryLength;
				ofs += newEntryLength;

				pWidget->_onNewSize( WgSize(newEntryLength, newContentBreadth) );				//TODO: Should be able to do a _onNewSize() that prevents child from doing a _requestRender().
			}
			else
			{
				int newEntryLength = _paddedLimitedMatchingHeight(pWidget, newContentBreadth );
				pHook->m_ofs = ofs;
				pHook->m_length = newEntryLength;
				ofs += newEntryLength;

				pWidget->_onNewSize( WgSize(newContentBreadth, newEntryLength) );				//TODO: Should be able to do a _onNewSize() that prevents child from doing a _requestRender().
			}
		}
		m_contentLength = ofs;
	}

	_requestRender();
}