--[[   _                                

	( )                               

   _| |   __   _ __   ___ ___     _ _ 

 /'_` | /'__`\( '__)/' _ ` _ `\ /'_` )

( (_| |(  ___/| |   | ( ) ( ) |( (_| |

`\__,_)`\____)(_)   (_) (_) (_)`\__,_) 



--]]

local PANEL = {}



AccessorFunc( PANEL, "Padding", 	"Padding" )

AccessorFunc( PANEL, "pnlCanvas", 	"Canvas" )



--[[---------------------------------------------------------

   Name: Init

-----------------------------------------------------------]]

function PANEL:Init()



	self.pnlCanvas 	= vgui.Create( "Panel", self )

	self.pnlCanvas.OnMousePressed = function( self, code ) self:GetParent():OnMousePressed( code ) end

	self.pnlCanvas:SetMouseInputEnabled( true )

	self.pnlCanvas.PerformLayout = function( pnl )

	

		self:PerformLayout()

		self:InvalidateParent()

	

	end

	

	-- Create the scroll bar

	self.VBar = vgui.Create( "DVScrollBar", self )

	self.VBar:Dock( RIGHT )



	self:SetPadding( 0 )

	self:SetMouseInputEnabled( true )

	

	-- This turns off the engine drawing

	self:SetPaintBackgroundEnabled( false )

	self:SetPaintBorderEnabled( false )

	self:SetPaintBackground( false )



end



--[[---------------------------------------------------------

   Name: AddItem

-----------------------------------------------------------]]

function PANEL:AddItem( pnl )



	pnl:SetParent( self:GetCanvas() )

	

end



function PANEL:OnChildAdded( child )



	self:AddItem( child )



end



--[[---------------------------------------------------------

   Name: SizeToContents

-----------------------------------------------------------]]

function PANEL:SizeToContents()



	self:SetSize( self.pnlCanvas:GetSize() )

	

end



--[[---------------------------------------------------------

   Name: GetVBar

-----------------------------------------------------------]]

function PANEL:GetVBar()



	return self.VBar

	

end



--[[---------------------------------------------------------

   Name: GetCanvas

-----------------------------------------------------------]]

function PANEL:GetCanvas()



	return self.pnlCanvas



end



function PANEL:InnerWidth()



	return self:GetCanvas():GetWide()



end



--[[---------------------------------------------------------

   Name: Rebuild

-----------------------------------------------------------]]

function PANEL:Rebuild()



	self:GetCanvas():SizeToChildren( false, true )

		

	-- Although this behaviour isn't exactly implied, center vertically too

	if ( self.m_bNoSizing && self:GetCanvas():GetTall() < self:GetTall() ) then



		self:GetCanvas():SetPos( 0, (self:GetTall()-self:GetCanvas():GetTall()) * 0.5 )

	

	end

	

end



--[[---------------------------------------------------------

   Name: OnMouseWheeled

-----------------------------------------------------------]]

function PANEL:OnMouseWheeled( dlta )



	return self.VBar:OnMouseWheeled( dlta )

	

end



--[[---------------------------------------------------------

   Name: OnVScroll

-----------------------------------------------------------]]

function PANEL:OnVScroll( iOffset )



	self.pnlCanvas:SetPos( 0, iOffset )

	

end



--[[---------------------------------------------------------

   Name: ScrollToChild

-----------------------------------------------------------]]

function PANEL:ScrollToChild( panel )



	self:PerformLayout()

	

	local x, y = self.pnlCanvas:GetChildPosition( panel )

	local w, h = panel:GetSize()

	

	y = y + h * 0.5;

	y = y - self:GetTall() * 0.5;



	self.VBar:AnimateTo( y, 0.5, 0, 0.5 );

	

end





--[[---------------------------------------------------------

   Name: PerformLayout

-----------------------------------------------------------]]

function PANEL:PerformLayout()



	local Wide = self:GetWide()

	local YPos = 0

	

	self:Rebuild()

	

	self.VBar:SetUp( self:GetTall(), self.pnlCanvas:GetTall() )

	YPos = self.VBar:GetOffset()

		

	if ( self.VBar.Enabled ) then Wide = Wide - self.VBar:GetWide() end



	self.pnlCanvas:SetPos( 0, YPos )

	self.pnlCanvas:SetWide( Wide )

	

	self:Rebuild()





end



function PANEL:Clear()



	return self.pnlCanvas:Clear()



end





derma.DefineControl( "DScrollPanel", "", PANEL, "DPanel" )

