#namespace eval Icons {
#	image create photo closed -file closed.gif

#}

namespace eval List {

	variable posy
	variable cnv
	variable cpath
	variable maxy
	variable maxw
	variable ibg 
	variable ibgw
	variable ibgh
	variable tabs

	proc create {w current_path bg} {
		variable posy 0
		variable cnv 
		variable index 0
		variable maxy 0
	variable maxw 600
	variable ibg 
	variable ibgw
	variable ibgh
		
			frame $w.left
			canvas $w.left.title -bg black -height 18 
			$w.left.title create rectangle 0 0 $maxw 16 -fill black 
			pack $w.left.title -side top -fill x 
#			title $w.left.title  {Num owner x x x title place} {10 50 140 150 160 190 340}
			pack $w.left.title -fill both -expand no
			canvas $w.left.cent -bg white -yscrollcommand "$w.sb set" -yscrollincrement 16 -width 512
			pack $w.left.cent  -expand yes -fill both

			scrollbar $w.sb -command "$w.left.cent yview" 
			pack $w.sb -side right -fill y -expand no

			pack $w.left -side left -fill both -expand yes

			bind $w.left.cent <Button-4> "$w.left.cent yview scroll -5 units"
			bind $w.left.cent <Button-5> "$w.left.cent yview scroll 5 units"
	

			set cnv $w.left.cent
			set cpath ${current_path}/images
			#image create photo ibg -file ${cpath}/$bg
			set ibg $bg
			set ibgw [ image width $ibg]
			set ibgh [ image height $ibg]

			add_bg 
			add_bg 
			add_bg 
	}
	
	proc add_bg {} {
		
		variable cnv 
		variable ibg 
		variable ibgw
		variable ibgh
		variable maxy
	$cnv create image 0 $maxy -anchor nw -image $ibg
	$cnv create image $ibgw $maxy -anchor nw -image $ibg
	$cnv create image [expr $ibgw * 2] $maxy -anchor nw -image $ibg
	$cnv create image [expr $ibgw * 3] $maxy -anchor nw -image $ibg
	incr maxy $ibgh	
			
	}

	proc set_title {w l t} {
		variable tabs
		variable posy 
		set tabs $t
		set posy 0
		$w.left.title delete line
		for {set i 0} { $i < [llength $tabs] } {incr i} {
			$w.left.title create text  [expr 6*[lindex $tabs $i]]  4  -anchor nw -text [lindex $l $i] -fill white -tag line
		}
	}

proc open_menu {x y l} {
	destroy .pl
	set name [lindex $l 0]
	menu .pl -tearoff false
	.pl add command -label $name -state disabled
	.pl add separator
	for {set i 1} { $i < [llength $l]} { incr i} {
		set cmd [lindex $l $i]
		.pl add command -label $cmd -command " ruby {cmd_menu(\"$cmd\",\"$name\")}"
	}

	.pl post $x $y
}

	proc add {l cmd} {
		variable posy 
		variable maxy 
		variable cnv 
		variable tabs
		variable maxw
		
#conversion arbitraire caractères->pixels
		for {set i 0} { $i < [llength $tabs] } {incr i} {
			$cnv create text [expr 6*[lindex $tabs $i]] $posy -anchor nw -text [lindex $l $i] -tag line
			}
			set base [expr $posy - 2]
			set bottom [expr $posy + 14]
#optimisation: mettre avec le fond
			set id [$cnv create polygon 0  $base  $maxw  $base $maxw  $bottom 0 $bottom -outline white -activeoutline gray -fill "" -tag line]
		
			if {[llength $cmd] == 1} {
				$cnv bind $id <Button-1> "ruby $cmd"
			} else {
				$cnv bind $id <Button-1> "List::open_menu %X %Y {$cmd}"
			}	
			
			incr posy 16
			if { $posy >= $maxy } add_bg

	}





	proc clear {} {
		variable cnv 
		$cnv delete line
	}


	proc refresh {} {
		variable cnv 
		variable posy
			$cnv configure -scrollregion "0 0 0 $posy"
	}

}


