/* * * * * * * * * * * * * * * * 
 * 	 ______     __  __     ______     __  __     ______     ______     __  __    
 *	/\  __ \   /\ \/\ \   /\  == \   /\ \_\ \   /\  __ \   /\  ___\   /\ \/ /    
 *	\ \ \/\ \  \ \ \_\ \  \ \  __<   \ \  __ \  \ \  __ \  \ \ \____  \ \  _"-.  
 *	 \ \_____\  \ \_____\  \ \_____\  \ \_\ \_\  \ \_\ \_\  \ \_____\  \ \_\ \_\ 
 *	  \/_____/   \/_____/   \/_____/   \/_/\/_/   \/_/\/_/   \/_____/   \/_/\/_/ 
 *
 *  - Version : 3L
 *  - By Oubliette
 * * * * * * * * * * * * * * * */

--[[   _                                
    ( )                               
   _| |   __   _ __   ___ ___     _ _ 
 /'_` | /'__`\( '__)/' _ ` _ `\ /'_` )
( (_| |(  ___/| |   | ( ) ( ) |( (_| |
`\__,_)`\____)(_)   (_) (_) (_)`\__,_) 

--]]

--
local arg = { ... };
local g = arg[ 1 ];
local VGUI = arg[ 2 ];
--

local PANEL = {}

--[[---------------------------------------------------------
   Name: Init
-----------------------------------------------------------]]
function PANEL:Init()

	self:SetTextInset( 5, 0 )

end

function PANEL:UpdateColours( skin )

	if ( self:GetParent():IsLineSelected() )	then return self:SetTextStyleColor( skin.Colours.Label.Bright ) end
	
	return self:SetTextStyleColor( skin.Colours.Label.Dark )

end

VGUI.RegisterElement( "DListViewLabel", PANEL, "DLabel" );

local PANEL = {}

g.AccessorFunc( PANEL, "m_iID", 				"ID" )
g.AccessorFunc( PANEL, "m_pListView", 		"ListView" )
g.AccessorFunc( PANEL, "m_bAlt", 				"AltLine" )

--[[---------------------------------------------------------
   Name: Init
-----------------------------------------------------------]]
function PANEL:Init()

	self:SetSelectable( true )
	self:SetMouseInputEnabled( true )
	
	self.Columns = {}

end

--[[---------------------------------------------------------
   Name: OnSelect
-----------------------------------------------------------]]
function PANEL:OnSelect()

	-- For override
	
end

--[[---------------------------------------------------------
   Name: OnRightClick
-----------------------------------------------------------]]
function PANEL:OnRightClick()

	-- For override
	
end

--[[---------------------------------------------------------
   Name: OnMousePressed
-----------------------------------------------------------]]
function PANEL:OnMousePressed( mcode )

	if ( mcode == g.MOUSE_RIGHT ) then
	
		-- This is probably the expected behaviour..
		if ( !self:IsLineSelected() ) then
		
			self:GetListView():OnClickLine( self, true )
			self:OnSelect()

		end
		
		self:GetListView():OnRowRightClick( self:GetID(), self )
		self:OnRightClick()
		
		return
		
	end

	self:GetListView():OnClickLine( self, true )
	self:OnSelect()
	
end

--[[---------------------------------------------------------
   Name: OnMousePressed
-----------------------------------------------------------]]
function PANEL:OnCursorMoved()

	if ( g.input.IsMouseDown( g.MOUSE_LEFT ) ) then
		self:GetListView():OnClickLine( self )
	end
	
end

--[[---------------------------------------------------------
   Name: IsLineSelected
-----------------------------------------------------------]]
function PANEL:IsLineSelected()

	return self.m_bSelected

end

--[[---------------------------------------------------------
   Name: SetColumnText
-----------------------------------------------------------]]
function PANEL:SetColumnText( i, strText )

	if ( g.type( strText ) == "Panel" ) then
	
		if ( g.IsValid( self.Columns[ i ] ) ) then self.Columns[ i ]:Remove() end
	
		strText:SetParent( self )
		self.Columns[ i ] = strText
		self.Columns[ i ].Value = strText
		return
		
	end

	if ( !g.IsValid( self.Columns[ i ] ) ) then
	
		self.Columns[ i ] = VGUI.CreateElement( "DListViewLabel", self )
		self.Columns[ i ]:SetMouseInputEnabled( false )
		
	end
	
	self.Columns[ i ]:SetText( g.tostring( strText ) )
	self.Columns[ i ].Value = strText
	return self.Columns[ i ]

end

PANEL.SetValue = PANEL.SetColumnText


--[[---------------------------------------------------------
   Name: SetColumnText
-----------------------------------------------------------]]
function PANEL:GetColumnText( i )

	if ( !self.Columns[ i ] ) then return "" end
	
	return self.Columns[ i ].Value

end

PANEL.GetValue = PANEL.GetColumnText

--[[---------------------------------------------------------
   Name: SetColumnText
-----------------------------------------------------------]]
function PANEL:DataLayout( ListView )

	self:ApplySchemeSettings()

	local height = self:GetTall()
	
	local x = 0
	for k, Column in g.pairs( self.Columns ) do
	
		local w = ListView:ColumnWidth( k )
		Column:SetPos( x, 0 )
		Column:SetSize( w, height )
		x = x + w
	
	end	

end

function PANEL:Paint( w, h )

	g.surface.SetDrawColor( VGUI.Cols.BGDark );

	if ( self:IsSelected() ) then

		g.surface.SetDrawColor( VGUI.Cols.Highlight );
	 
	elseif ( self.Hovered ) then

		g.surface.SetDrawColor( VGUI.Cols.ButtonHovered );
	         
	end

	g.surface.DrawRect( 0, 0, w, h );

end

function PANEL:ApplySchemeSettings()

	// Nothing

end

function PANEL:PerformLayout()

	// Nothing

end

VGUI.RegisterElement( "DListView_Line", PANEL, "Panel" );
VGUI.RegisterElement( "DListViewLine", PANEL, "Panel" );