//-----------------------------------------------------------------------------
// Purpose: Callback for when the panel size has been changed
//-----------------------------------------------------------------------------
void EditablePanel::OnSizeChanged(int wide, int tall)
{
	BaseClass::OnSizeChanged(wide, tall);
	InvalidateLayout();

	int dx = wide - _baseWide;
	int dy = tall - _baseTall;

	for (int i = 0; i < GetChildCount(); i++)
	{
		// perform auto-layout on the child panel
		Panel *child = GetChild(i);

		int x, y, w, t;
		child->GetBounds(x, y, w, t);

		if (child->GetPinCorner() == PIN_TOPRIGHT || child->GetPinCorner() == PIN_BOTTOMRIGHT)
		{
			// move along with the right edge
			x += dx;
		}

		if (child->GetPinCorner() == PIN_BOTTOMLEFT || child->GetPinCorner() == PIN_BOTTOMRIGHT)
		{
			// move along with the lower edge
			y += dy;
		}

		// check for resize
		if (child->GetAutoResize() == AUTORESIZE_RIGHT || child->GetAutoResize() == AUTORESIZE_DOWNANDRIGHT)
		{
			w += dx;
		}

		if (child->GetAutoResize() == AUTORESIZE_DOWN || child->GetAutoResize() == AUTORESIZE_DOWNANDRIGHT)
		{
			t += dy;
		}

		// make sure the child isn't too big...
		if(x+w>wide)
		{
			continue;
		}
		
		if(y+t>tall)
		{
			continue;
		}

		child->SetBounds(x, y, w, t);
		child->InvalidateLayout();
	}
	Repaint();

	// update the baselines
	_baseWide = wide;
	_baseTall = tall;
}
//-----------------------------------------------------------------------------
// Creates a widget using editor attribute info
//-----------------------------------------------------------------------------
void CBaseAttributePanel::PerformLayout()
{
	BaseClass::PerformLayout();
	
	CUtlVector< Panel * >	vispanels;

	if ( HasFlag( HIDETYPE ) )
	{
		m_pType->SetVisible( false );
	}
	else
	{
		vispanels.AddToTail( m_pType );
	}

	vgui::Panel *dataPanel = GetDataPanel();
	if ( dataPanel )
	{
		if ( HasFlag( HIDEVALUE ) )
		{
			dataPanel->SetVisible( false );
		}
		else
		{
			vispanels.AddToTail( dataPanel );
		}
	}
	
	int c = vispanels.Count();

	Assert( c >= 0 );
	if ( c == 0 )
	{
		return;
	}

	int w, h;
	GetSize( w, h );

	int x = 1;
	int y = 0;
	w-= 2;

	for ( int i = 0; i < c; ++i )
	{
		Panel *panel = vispanels[ i ];
		int width = GetSizeForColumn( panel );
		if ( i == c - 1 )
		{
			width = w - x;
		}

		panel->SetBounds( x, y, width, h );
		x += width;
	}
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
SampleTabs::SampleTabs(Panel *parent, const char *name) : DemoPage(parent, name)
{
	m_pPropertySheet = new PropertySheet(this, "Tabs");
	m_pPropertySheet->SetBounds(90,25, 375, 200);
	m_pPropertySheet->SetTabWidth(75);

	Panel *testPanel = new PropertyPage (this, "tab1");
	testPanel->SetBounds(90,50, 375, 200);
	m_pPropertySheet->AddPage(testPanel, "Keyboard");

	Panel *testPanel2 = new PropertyPage (this, "tab2");
	testPanel->SetBounds(90,50,375,200);
	m_pPropertySheet->AddPage(testPanel2, "Mouse");

	Panel *testPanel3 = new PropertyPage (this, "tab3");
	testPanel->SetBounds(90,50,375,200);
	m_pPropertySheet->AddPage(testPanel3, "Audio");

	Panel *testPanel4 = new PropertyPage (this, "tab4");
	testPanel->SetBounds(90,50, 375,200);
	m_pPropertySheet->AddPage(testPanel4, "Video");

}
void CGrid::RepositionContents()
{
	for(int x=0; x < m_xCols; x++)
	{
		for(int y=0; y < m_yRows; y++)
		{
			Panel *pPanel = GridEntry(x,y)->m_pPanel;
			if(!pPanel)
				continue;

			pPanel->SetBounds(
				m_ColOffsets[x], 
				m_RowOffsets[y],
				m_Widths[x], 
				m_Heights[y]);
		}
	}

	m_bDirty = false;
}
//-----------------------------------------------------------------------------
// Purpose: Re-aligns background image panels so they are touching.
//-----------------------------------------------------------------------------
static void FixupBackgroundPanels( EditablePanel *pWindow, int offsetX, int offsetY )
{
	if ( !pWindow )
		return;

	int screenWide, screenTall;
	pWindow->GetSize( screenWide, screenTall );

	int inset = GetAlternateProportionalValueFromNormal( 20 );
	int cornerSize = GetAlternateProportionalValueFromNormal( 10 );

	int titleHeight = GetAlternateProportionalValueFromNormal( 42 );
	int mainHeight = GetAlternateProportionalValueFromNormal( 376 );

	int logoSize = titleHeight;

	int captionInset = GetAlternateProportionalValueFromNormal( 76 );

	Panel *pPanel;

	// corners --------------------------------------------
	pPanel = pWindow->FindChildByName( "TopLeftPanel" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( offsetX + inset, offsetY + inset, cornerSize, cornerSize );
	}

	pPanel = pWindow->FindChildByName( "TopRightPanel" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( screenWide - offsetX - inset - cornerSize, offsetY + inset, cornerSize, cornerSize );
	}

	pPanel = pWindow->FindChildByName( "BottomLeftPanel" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( offsetX + inset, screenTall - offsetY - inset - cornerSize, cornerSize, cornerSize );
	}

	pPanel = pWindow->FindChildByName( "BottomRightPanel" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( screenWide - offsetX - inset - cornerSize, screenTall - offsetY - inset - cornerSize, cornerSize, cornerSize );
	}

	// background -----------------------------------------
	pPanel = pWindow->FindChildByName( "TopSolid" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( offsetX + inset + cornerSize, offsetY + inset, screenWide - 2*offsetX - 2*inset - 2*cornerSize, cornerSize );
	}

	pPanel = pWindow->FindChildByName( "UpperMiddleSolid" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( offsetX + inset, offsetY + inset + cornerSize, screenWide - 2*offsetX - 2*inset, titleHeight );
	}

	pPanel = pWindow->FindChildByName( "LowerMiddleSolid" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( offsetX + inset + cornerSize, screenTall - offsetY - inset - cornerSize, screenWide - 2*offsetX - 2*inset - 2*cornerSize, cornerSize );
	}

	pPanel = pWindow->FindChildByName( "BottomSolid" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( offsetX + inset, screenTall - offsetY - inset - cornerSize - mainHeight, screenWide - 2*offsetX - 2*inset, mainHeight );
	}

	// transparent border ---------------------------------
	pPanel = pWindow->FindChildByName( "TopClear" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( 0, 0, screenWide, offsetY + inset );
	}

	pPanel = pWindow->FindChildByName( "BottomClear" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( 0, screenTall - offsetY - inset, screenWide, offsetY + inset );
	}

	pPanel = pWindow->FindChildByName( "LeftClear" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( 0, offsetY + inset, offsetX + inset, screenTall - 2*offsetY - 2*inset );
	}

	pPanel = pWindow->FindChildByName( "RightClear" );
	if ( pPanel )
	{
		pPanel->SetZPos( -20 );
		pPanel->SetBounds( screenWide - offsetX - inset, offsetY + inset, offsetX + inset, screenTall - 2*offsetY - 2*inset );
	}

	// Logo -----------------------------------------------
	int logoInset = (cornerSize + titleHeight - logoSize)/2;
	pPanel = pWindow->FindChildByName( "ExclamationPanel" );
	if ( pPanel )
	{
		pPanel->SetZPos( -19 ); // higher than the background
		pPanel->SetBounds( offsetX + inset + logoInset, offsetY + inset + logoInset, logoSize, logoSize );
	}

	// Title caption --------------------------------------
	pPanel = dynamic_cast< Label * >(pWindow->FindChildByName( "CaptionLabel" ));
	if ( pPanel )
	{
		pPanel->SetZPos( -19 ); // higher than the background
		pPanel->SetBounds( offsetX + captionInset/*inset + 2*logoInset + logoSize*/, offsetY + inset + logoInset, screenWide, logoSize );
	}
}
//-----------------------------------------------------------------------------
// Purpose: Callback for when the panel size has been changed
//-----------------------------------------------------------------------------
void EditablePanel::OnSizeChanged(int wide, int tall)
{
	BaseClass::OnSizeChanged(wide, tall);
	InvalidateLayout();

	for (int i = 0; i < GetChildCount(); i++)
	{
		// perform auto-layout on the child panel
		Panel *child = GetChild(i);
		if ( !child )
			continue;

		int x, y, w, h;
		child->GetBounds( x, y, w, h );

		int px, py;
		child->GetPinOffset( px, py );

		int ox, oy;
		child->GetResizeOffset( ox, oy );

		int ex;
		int ey;

		AutoResize_e resize = child->GetAutoResize(); 
		bool bResizeHoriz = ( resize == AUTORESIZE_RIGHT || resize == AUTORESIZE_DOWNANDRIGHT );
		bool bResizeVert = ( resize == AUTORESIZE_DOWN || resize == AUTORESIZE_DOWNANDRIGHT );

		PinCorner_e pinCorner = child->GetPinCorner();
		if ( pinCorner == PIN_TOPRIGHT || pinCorner == PIN_BOTTOMRIGHT )
		{
			// move along with the right edge
			ex = wide + px;
			x = bResizeHoriz ? ox : ex - w;
		}
		else
		{
			x = px;
			ex = bResizeHoriz ? wide + ox : px + w;
		}

		if ( pinCorner == PIN_BOTTOMLEFT || pinCorner == PIN_BOTTOMRIGHT )
		{
			// move along with the right edge
			ey = tall + py;
			y = bResizeVert ? oy : ey - h;
		}
		else
		{
			y = py;
			ey = bResizeVert ? tall + oy : py + h;
		}

		// Clamp..
		if ( ex < x )
		{
			ex = x;
		}
		if ( ey < y )
		{
			ey = y;
		}

		child->SetBounds( x, y, ex - x, ey - y );
		child->InvalidateLayout();
	}
	Repaint();
}