#!/usr/bin/wishx




####################initialisations###############





set SFMPATH /export/home/mpavoine/sfm
set LIBPATH /export/home/mpavoine/sfm/libraries
set IMGPATH /export/home/mpavoine/sfm/images
set HLPPATH /export/home/mpavoine/sfm/help

set scrw [winfo screenwidth .]
set scrh [winfo screenheight .]


set lastX ""
set lastY ""
set listicons0 {}
set treestat {}
set listwindows {}
#set listcolor {#333333 #336633 #333366 #663333 #666633 #663366 #336666 #666666}
set listcolor {#333333 #339933 #333399 #993333 #999933 #993399 #339999 #999999}
set ct [catch {eval [read_file ~/.sfm/.sfmrc]}]
if {$ct > 0}\
{	
	tk_setPalette #DEDEDE
	set canbgcol #EFEFEF
}
set tmp ""
set dir ""
set y 0
set s ""
set flaghidden 0
set ichome "$IMGPATH/home.bmp"
set ictrash "$IMGPATH/trash.bmp"
set icdir "$IMGPATH/dir.bmp"
set icfile "$IMGPATH/file.bmp"
set iclink "$IMGPATH/link.bmp"
set icfifo "$IMGPATH/fifo.bmp"
set icchar "$IMGPATH/char.bmp"
set icblock "$IMGPATH/block.bmp"
set icsocket "$IMGPATH/socket.bmp"
set icexec "$IMGPATH/exec.bmp"
set icdef "$IMGPATH/def.bmp"
set icminidir "$IMGPATH/minidir.bmp"
set icfloppy "$IMGPATH/floppy.bmp"

set areaX1 0
set areaY1 0
set areaX2 0
set areaY2 0

pushd [pwd]
cd ~
set home [pwd]
popd

wm geometry . [expr $scrw - 50]x[expr $scrh - 50]+25+5
wm title . "SFM 1.5"
wm protocol . WM_DELETE_WINDOW "Dconfquit"
##################################################



####################################################
source $LIBPATH/window.tcl
source $LIBPATH/configure.tcl
source $LIBPATH/find.tcl
source $LIBPATH/help.tcl
#####################################################
toplevel .icon
image create photo about -file $IMGPATH/icsfm.gif
label .icon.img -image about
pack .icon.img
wm iconwindow . .icon
wm geometry .icon 50x50+100+100

##################################################
. configure -cursor top_left_arrow
#################menuBar############################
frame .menuBar 
pack .menuBar -side top -fill x

menubutton .menuBar.general -text General -menu .menuBar.general.m -underline 0
menu .menuBar.general.m -tearoff 0
	.menuBar.general.m add command -label "Empty trash"  -underline 4\
		-accelerator "Ctrl-y"\
		-command "emptyTrash .center.icons.can"
	.menuBar.general.m add separator
	.menuBar.general.m add command -label "Refresh" -underline 2\
		-accelerator "Ctrl-f"\
		-command {set dir [pwd]
			changeDir .center.icons.can}
	.menuBar.general.m add command -label "Select all" -underline 0\
		-accelerator "Ctrl-a"\
		-command "selectAll .center.icons.can"
	.menuBar.general.m add separator
	.menuBar.general.m add command -label "Configurations" -underline 6\
		-accelerator "Ctrl-u"\
		-command "_Dconfigure2 0 .center.icons.can"
	.menuBar.general.m add separator
	.menuBar.general.m add command -label "Quit"  -underline 0\
		-accelerator "Ctrl-q"\
		-command "Dconfquit"
pack .menuBar.general -side left

menubutton .menuBar.file -text File -menu .menuBar.file.m -underline 0
menu .menuBar.file.m -tearoff 0
	.menuBar.file.m add command -label "Copy" -underline 0\
		-accelerator "Ctrl-c"\
		-command "itemCopy 0 .center.icons.can"
	.menuBar.file.m add command -label "Paste" -underline 0\
		-accelerator "Ctrl-v"\
		-command "itemPaste 0 p .center.icons.can"
	.menuBar.file.m add command -label "Send to trash" -underline 0\
		-accelerator "Ctrl-e"\
		-command "sendTrash 0 .center.icons.can"
	.menuBar.file.m add command -label "Delete" -underline 1\
		-accelerator "Ctrl-x"\
		-command "deleteDirect 0 .center.icons.can"
	.menuBar.file.m add command -label "Rename" -underline 0\
		-accelerator "Ctrl-r"\
		-command "DitemRename 0 .center.icons.can"
	.menuBar.file.m add command -label "Link" -underline 0\
		-accelerator "Ctrl-l"\
		-command "DitemLink 0 .center.icons.can"
	.menuBar.file.m add command -label "Open" -underline 0\
		-accelerator "Ctrl-o"\
		-command "itemOpen .center.icons.can"
	.menuBar.file.m add command -label "Open in a new window" -underline 12\
		-accelerator "Ctrl-w"\
		-command "newOpen 0 .center.icons.can"
	.menuBar.file.m add separator
	.menuBar.file.m add command -label "Permissions" -underline 4\
		-accelerator "Ctrl-i"\
		-command "DitemPermissions 0 .center.icons.can"
	.menuBar.file.m add command -label "Properties" -underline 6\
		-accelerator "Ctrl-t"\
		-command "DitemProperties 0 .center.icons.can"
	.menuBar.file.m add separator
	.menuBar.file.m add command -label "Show hidden files" -underline 0\
		-accelerator "Ctrl-s"\
		-command "showHidden"
	.menuBar.file.m add command -label "Hide hidden files" -underline 0\
		-accelerator "Ctrl-h"\
		-command "hideHidden"
pack .menuBar.file -side left
	
menubutton .menuBar.directory -text Directory -menu .menuBar.directory.m \
	-underline 0
menu .menuBar.directory.m -tearoff 0
	.menuBar.directory.m add command -label "Home"  -underline 3\
		-accelerator "Ctrl-m"\
		-command {set dir ~
			changeDir .center.icons.can}
	.menuBar.directory.m add command -label "Trash" -underline 2\
		-accelerator "Ctrl-z"\
		-command {_newWindow [_initWindow ~/.sfm/ .trash]}
	.menuBar.directory.m add separator
	.menuBar.directory.m add command -label "New"  -underline 0\
		-accelerator "Ctrl-n"\
		-command "DnewDir 0 .center.icons.can"
	.menuBar.directory.m add command -label "Change"  -underline 0\
		-accelerator "Ctrl-g"\
		-command "DchangeDir 0 .center.icons.can"
pack .menuBar.directory -side left

menubutton .menuBar.tools -text Tools -menu .menuBar.tools.m -underline 0
menu .menuBar.tools.m -tearoff 0
	.menuBar.tools.m add command -label "Find" -underline 3\
		-accelerator "Ctrl-d" -command "_Dfind"
	.menuBar.tools.m add cascade -label "Floppy" \
		-menu .menuBar.tools.m.flop -underline 0
	menu .menuBar.tools.m.flop -tearoff 0
		.menuBar.tools.m.flop add command -label "Open /floppy" \
			-underline 0 -command {catch {exec mount /dev/fd0}
				_newWindow [_initWindow / floppy]
			}
		.menuBar.tools.m.flop add command -label "Mount" \
			-underline 0 -command {
				set ct [catch {exec mount /dev/fd0} errmsg]
				if {$ct > 0}\
				{	tk_dialog .baddir {Error}\
						$errmsg {} 0 OK
				}\
				else\
				{
					_newWindow [_initWindow / floppy]
				}
			}
		.menuBar.tools.m.flop add command -label "Umount" \
			-underline 0 -command {
				set ct [catch {exec umount /dev/fd0} errmsg]
				if {$ct > 0}\
				{	tk_dialog .baddir {Error}\
						$errmsg {} 0 OK
				}\
				else\
				{
					foreach key [keylget listwindows]\
					{	
						if {[regexp ^/floppy [keylget listwindows $key.cwd]]}\
						{	_closeWindow $key
						}
					}
				}
			}
		.menuBar.tools.m.flop add separator
		.menuBar.tools.m.flop add command -label "Start xflop" \
			-accelerator "F2"\
			-underline 0 -command "catch {exec xflop &}"
pack .menuBar.tools -side left

menubutton .menuBar.window -text Window -menu .menuBar.window.m -underline 0
menu .menuBar.window.m -tearoff 0
	.menuBar.window.m add command -label "Close all windows" -underline 0\
		-command "_closeAllWindows"
	.menuBar.window.m add command -label "Open /floppy" \
		-underline 0 -command {catch {exec mount /dev/fd0}
			_newWindow [_initWindow / floppy]
		}
	.menuBar.window.m add separator
pack .menuBar.window -side left

menubutton .menuBar.help -text Help -menu .menuBar.help.m -underline 0
menu .menuBar.help.m -tearoff 0
	.menuBar.help.m add command -label "About SFM" -underline 0\
		-accelerator "Ctrl-b"\
		-command "Dabout"
	.menuBar.help.m add separator
	.menuBar.help.m add command -label "Help" -underline 0\
		-accelerator "F1"\
		-command "Dhelp"
pack .menuBar.help -side right

menu .droit -tearoff 0
	.droit add command -label "Paste" \
		-command "itemPaste 0 p .center.icons.can"
	.droit add separator
	.droit add command -label "Show hidden files" \
		-command "showHidden"
	.droit add command -label "Hide hidden files" \
		-command "hideHidden"
	.droit add separator
	.droit add command -label "Home" \
		-command {set dir ~
			changeDir .center.icons.can}
	.droit add command -label "Trash" \
		-command {_newWindow [_initWindow ~/.sfm/ .trash]}
	.droit add separator
	.droit add command -label "New directory" \
		-command "DnewDir 0 .center,icons.can"
	.droit add command -label "Change directory" \
		-command "DchangeDir 0 .center.icons.can"
	.droit add separator
	.droit add command -label "Refresh" \
		-command {set dir [pwd]
			changeDir .center.icons.can}
	.droit add separator
	.droit add command -label "Start xflop" \
		-command "catch {exec xflop &}"
	.droit add separator
	.droit add command -label "Empty trash" \
		-command "emptyTrash .center.icons.can"

menu .droiticons -tearoff 0
	.droiticons add command -label "Copy" \
		-command "itemCopy 0 .center.icons.can"
	.droiticons add command -label "Send to trash" \
		-command "sendTrash 0 .center.icons.can"
	.droiticons add command -label "Delete" \
		-command "deleteDirect 0 .center.icons.can"
	.droiticons add command -label "Rename" \
		-command "DitemRename 0 .center.icons.can"
	.droiticons add command -label "Link" \
		-command "DitemLink 0 .center.icons.can"
	.droiticons add command -label "Open" \
		-command "itemOpen .center.icons.can"
	.droiticons add command -label "Open in a new window" \
		-command "newOpen 0 .center.icons.can"
	.droiticons add separator
	.droiticons add command -label "Permissions" \
		-command "DitemPermissions 0 .center.icons.can"
	.droiticons add command -label "Properties" \
		-command "DitemProperties 0 .center.icons.can"

#####################################################
bind . <Control-y> ".menuBar.general.m invoke 0"
bind . <Control-f> ".menuBar.general.m invoke 2"
bind . <Control-a> ".menuBar.general.m invoke 3"
bind . <Control-u> ".menuBar.general.m invoke 5"
bind . <Control-q> ".menuBar.general.m invoke 7"
bind . <Control-c> ".menuBar.file.m invoke 0"
bind . <Control-v> ".menuBar.file.m invoke 1"
bind . <Control-e> ".menuBar.file.m invoke 2"
bind . <Control-x> ".menuBar.file.m invoke 3"
bind . <Control-r> ".menuBar.file.m invoke 4"
bind . <Control-l> ".menuBar.file.m invoke 5"
bind . <Control-o> ".menuBar.file.m invoke 6"
bind . <Control-w> ".menuBar.file.m invoke 7"
bind . <Control-i> ".menuBar.file.m invoke 9"
bind . <Control-t> ".menuBar.file.m invoke 10"
bind . <Control-s> ".menuBar.file.m invoke 12"
bind . <Control-h> ".menuBar.file.m invoke 13"
bind . <Control-m> ".menuBar.directory.m invoke 0"
bind . <Control-z> ".menuBar.directory.m invoke 1"
bind . <Control-n> ".menuBar.directory.m invoke 3"
bind . <Control-g> ".menuBar.directory.m invoke 4"
bind . <Control-d> ".menuBar.tools.m invoke 0"
bind . <F2> ".menuBar.tools.m.flop invoke 4"
bind . <Control-b> ".menuBar.help.m invoke 0"
bind . <F1> ".menuBar.help.m invoke 2"
#######################################################




#################.center###############################
frame .center
pack .center -expand yes -fill both
#######################################################

##########################.center.tree#############################
frame .center.tree
pack .center.tree -side left -expand yes -fill both

###--------------.center.tree.hs/vs-------------------###
scrollbar .center.tree.hs -orient horiz -command ".center.tree.can xview"
pack .center.tree.hs -side bottom -fill x
scrollbar .center.tree.vs -command ".center.tree.can yview"
pack .center.tree.vs -side right -fill y
###----------------------------------------------------###
###------------------.center.tree.can-----------------###
canvas .center.tree.can -relief sunken -borderwidth 2 -width 150 -height 400 \
	-xscrollcommand ".center.tree.hs set"\
	-yscrollcommand ".center.tree.vs set"\
	-takefocus 1 \
	-background $canbgcol
pack .center.tree.can -expand yes -fill both



bind .center.tree.can <3> "treeShow .center.tree.can"
bind .center.tree.can <ButtonPress-1><ButtonRelease-1> {treeDouble .center.tree.can}
bind .center.tree.can <ButtonRelease-1> "itemStopDrag .center.tree.can %x %y"
###---------------------------------------------------###
####################################################################
###############.center.icons###############################
frame .center.icons
pack .center.icons -side right -expand yes -fill both

###------------.center.icons.buttons-------------------###
frame .center.icons.buttons 
pack .center.icons.buttons -side top -expand yes -fill x 

label .center.icons.buttons.lb -text "" -wraplength 300 -justify left
pack .center.icons.buttons.lb -side left -anchor s
button .center.icons.buttons.floppy -width 25 -height 25 \
	-bitmap @[file join $icfloppy] \
	-command  {catch {exec mount /dev/fd0}
		_newWindow [_initWindow / floppy]}
pack .center.icons.buttons.floppy -side right 
button .center.icons.buttons.trash -width 25 -height 25 \
	-bitmap @[file join $ictrash] \
	-command  {_newWindow [_initWindow ~/.sfm/ .trash]}
pack .center.icons.buttons.trash -side right 
button .center.icons.buttons.home -width 25 -height 25 \
	-bitmap @[file join $ichome] \
	-command  {
		set dir ~
		changeDir .center.icons.can
	}
pack .center.icons.buttons.home -side right 
###----------------------------------------------------###
###--------------.center.icons.hs/vs-------------------###
scrollbar .center.icons.hs -orient horiz -command ".center.icons.can xview"
pack .center.icons.hs -side bottom -fill x
scrollbar .center.icons.vs -command ".center.icons.can yview"
pack .center.icons.vs -side right -fill y
###----------------------------------------------------###
###------------------.center.icons.can-----------------###
canvas .center.icons.can -relief sunken -borderwidth 2\
	-width 400 -height 400 \
	-xscrollcommand ".center.icons.hs set"\
	-yscrollcommand ".center.icons.vs set"\
	-background $canbgcol
pack .center.icons.can -expand yes -fill both


bind .center.icons.can <Shift-B1-ButtonRelease> "itemDecideShift .center.icons.can %X %Y %x %y"
bind .center.icons.can <Shift-1> "itemLeftShift .center.icons.can %x %y"
bind .center.icons.can <Control-B1-ButtonRelease> "itemDecideShift .center.icons.can %X %Y %x %y"
bind .center.icons.can <Control-1> "itemLeftShift .center.icons.can %x %y"
bind .center.icons.can <1> "itemLeft .center.icons.can %x %y"
bind .center.icons.can <B1-ButtonRelease> "itemDecide .center.icons.can %X %Y %x %y"
bind .center.icons.can <Double-1> "itemOpen .center.icons.can"
bind .center.icons.can <3> "itemPopup .center.icons.can %X %Y"
###--------------------------------------------------------###
#################################################################




#########################procedures#############################
proc itemDecide {c gx gy x y}\
{
	set w [winfo containing $gx $gy]
	if {! [regexp can$ $w]}\
	{	
		$c dtag all selected
		itemLeave $c
		return
	}
	focus $w
	update
	if {[regexp .center.icons.can $w]}\
	{
		itemStopDrag $c $x $y 
	}\
	elseif {[regexp .center.tree.can $w]}\
	{	
		itemStopDrag .center.tree.can $x $y
	}\
	else\
	{
		set g [$c gettags selected]
		if {$g == "" || ! [regexp ^icone $g]}\
		{
			return
		}
		set sel {}
		foreach ic [$c find all]\
		{
			if {[regexp selected [$c gettags $ic]]}\
			{
				lappend sel [lindex [$c gettags $ic] 0]
			}
		}
		_itemStopDragWindow [crange $w 4 end-10] $w $c $sel $x $y
	}
	.center configure -cursor top_left_arrow
}


proc itemDecideShift {c gx gy x y}\
{
	set w [winfo containing $gx $gy]
	if {! [regexp can$ $w]}\
	{	
		$c dtag all selected
		itemLeave $c
		return
	}
	focus $w
	update
	if {[regexp .center.icons.can $w]}\
	{
		itemStopDrag $c $x $y 
	}\
	elseif {[regexp .center.tree.can $w]}\
	{	
		itemStopDrag .center.tree.can $x $y
	}\
	else\
	{
		set g [$c gettags selected]
		if {$g == "" || ! [regexp ^icone $g]}\
		{
			return
		}
		set sel {}
		foreach ic [$c find all]\
		{
			if {[regexp selected [$c gettags $ic]]}\
			{
				lappend sel [lindex [$c gettags $ic] 0]
			}
		}
		_itemStopDragWindow [crange $w 4 end-10] $w $c $sel $x $y
	}
}

proc disableMenu {}\
{
	.droiticons entryconfigure 0 -state disabled
	.droiticons entryconfigure 3 -state disabled
	.droiticons entryconfigure 4 -state disabled
	.droiticons entryconfigure 5 -state disabled
	.droiticons entryconfigure 6 -state disabled
	.droiticons entryconfigure 8 -state disabled
	.droiticons entryconfigure 9 -state disabled
	.menuBar.file.m entryconfigure 0 -state disabled
	.menuBar.file.m entryconfigure 1 -state disabled
	.menuBar.file.m entryconfigure 4 -state disabled
	.menuBar.file.m entryconfigure 5 -state disabled
	.menuBar.file.m entryconfigure 6 -state disabled
	.menuBar.file.m entryconfigure 7 -state disabled
	.menuBar.file.m entryconfigure 9 -state disabled
	.menuBar.file.m entryconfigure 10 -state disabled
}

proc enableMenu {}\
{
	.droiticons entryconfigure 0 -state normal
	.droiticons entryconfigure 3 -state normal
	.droiticons entryconfigure 4 -state normal
	.droiticons entryconfigure 5 -state normal
	.droiticons entryconfigure 6 -state normal
	.droiticons entryconfigure 8 -state normal
	.droiticons entryconfigure 9 -state normal
	.menuBar.file.m entryconfigure 0 -state normal
	.menuBar.file.m entryconfigure 1 -state normal
	.menuBar.file.m entryconfigure 4 -state normal
	.menuBar.file.m entryconfigure 5 -state normal
	.menuBar.file.m entryconfigure 6 -state normal
	.menuBar.file.m entryconfigure 7 -state normal
	.menuBar.file.m entryconfigure 9 -state normal
	.menuBar.file.m entryconfigure 10 -state normal
}

proc itemLeft {c x y}\
{
	set a [lindex [$c gettags current] 0]
	if {[regexp selected [$c gettags current]]}\
	{
		itemLeftShift $c $x $y
		return
	}
	enableMenu
	if {[regexp ^icone $a] || [regexp ^/ $a]}\
	{	
		$c dtag all selected
		$c addtag selected withtag $a
		itemLeave $c
		itemStartDrag $c $x $y
	}\
	else\
	{
		$c dtag all selected
		itemLeave $c
	}
}	

proc itemLeftShift {c x y}\
{
	disableMenu
	set a [lindex [$c gettags current] 0]
	if {[regexp ^icone $a] || [regexp ^/ $a]}\
	{	
		$c addtag selected withtag $a
		itemStartDrag $c $x $y
	}\
	else\
	{
		$c dtag all selected
		itemLeave $c
	}
}	

proc selectAll {c}\
{
	global listicons0
	disableMenu
	foreach ic [$c find all]\
	{
		if {[regexp ^icone [$c gettags $ic]]}\
		{
			if {[keylget listicons0 [lindex \
				[$c gettags $ic] 0]] != ".."}\
			{
				$c addtag selected withtag $ic
				$c itemconfig $ic -foreground SteelBlue2
				itemStartDrag $c 0 0
			}
		}
	}
}

proc itemPopup {c x y}\
{
	if {! [regexp selected [$c gettags current]]}\
	{
		$c dtag all selected
		itemLeave $c
		$c addtag selected withtag current
		enableMenu
	}		
	set a [$c gettags current]
	if {[regexp ^icone $a]}\
	{	
		itemEnter $c
		tk_popup .droiticons $x $y
	}\
	else\
	{	tk_popup .droit $x $y
	}
}


proc itemEnter {c} \
{
	set type [$c type current]
    	if {$type == "bitmap"} \
	{
		$c itemconfig current -foreground SteelBlue2
		return
    	}
}

proc itemLeave {c} \
{
	global listcolor
	$c itemconfig icones -foreground [lindex $listcolor 2]
}

proc showHidden {}\
{
	global flaghidden dir listwindows
	set flaghidden 1
	set dir [pwd]
	changeDir .center.icons.can
	foreach key [keylget listwindows]\
	{	_changeDirWindow $key
	}
}

proc hideHidden {}\
{
	global flaghidden dir listwindows
	set flaghidden 0
	set dir [pwd]
	changeDir .center.icons.can
	foreach key [keylget listwindows]\
	{	_changeDirWindow $key
	}
}

proc itemStartDrag {c x y} \
{
    	global s lastX lastY
    	set s [$c gettags selected] 
	if {$s == ""}\
	{
		return
	}
	if {! [regexp ^icone $s]}\
	{
		return
	}
	if {[regexp ^\.center $c]} \
	{
		.center configure -cursor dotbox
	}\
	else\
	{	
		$c configure -cursor dotbox
	}
	set lastX [$c canvasx $x]
	set lastY [$c canvasy $y]
	itemEnter $c
}

proc Dconfquit {}\
{
	set but [tk_dialog .baddir {Alert}\
		"Do you really want to quit SFM ?" {} 0 Yes No]
	if {$but == 0}\
	{	
		exit
	}
}

proc DitemMove {c}\
{
	catch {global listwindows}
	if {[winfo exists .itemMove]}\
	{	destroy .itemMove
		destroy .moveicon
	}
	global listicons0 s dir tmp sel treestat
	regsub // $tmp / tmp
	set sel {}
	foreach ic [$c find all]\
	{
		if {[regexp selected [$c gettags $ic]]}\
		{
			lappend sel [keylget listicons0 \
				[lindex [$c gettags $ic] 0]]
		}
	}
	toplevel .itemMove -cursor top_left_arrow
	wm protocol .itemMove WM_DELETE_WINDOW "catch {destroy .itemMove
							destroy .moveicon}"
	wm geometry .itemMove +100+100
	wm minsize .itemMove 250 90
	wm title .itemMove "Confirmation"
		toplevel .moveicon
		label .moveicon.img -image about
		pack .moveicon.img
		wm iconwindow .itemMove .moveicon
	wm geometry .moveicon 50x50+100+100
	frame .itemMove.ent
	pack .itemMove.ent
	label .itemMove.ent.txdir -justify left -wraplength 250\
		-text "\nMove \"$sel\" to \"$tmp\"?" 
	pack .itemMove.ent.txdir -side left
	frame .itemMove.but
	pack .itemMove.but -side bottom
	button .itemMove.but.ok -width 10 -text "OK" -command \
	{	
		catch {destroy .itemMove
			destroy .moveicon}
		foreach ic $sel\
		{
			set ct [catch {frename $ic $tmp/$ic} errmsg] 
			if {$ct > 0}\
			{	tk_dialog .baddir {Error}\
					"Cannot move $ic.\
				 	Permission denied."\
				 	{} 0 OK
				return
			}
			foreach key [keylget listwindows]\
			{
				if {[regexp [pwd]/$ic [keylget listwindows \
					$key.cwd]]}\
				{	_closeWindow $key
				}
			}
		}
		set dir [pwd]
		changeDir .center.icons.can
		foreach key [keylget listwindows]\
		{	
			_changeDirWindow $key
		}
	}
	pack .itemMove.but.ok -side left
	button .itemMove.but.cancel -width 10 -text "Cancel" -command \
	{
		catch {destroy .itemMove
			destroy .moveicon}
	}
	pack .itemMove.but.cancel -side right
	bind .itemMove <Return> \
	{	
		catch {destroy .itemMove
			destroy .moveicon}
		foreach ic $sel\
		{
			set ct [catch {frename $ic $tmp/$ic} errmsg] 
			if {$ct > 0}\
			{	tk_dialog .baddir {Error}\
					"Cannot move $ic.\
				 	Permission denied."\
				 	{} 0 OK
				return
			}
			foreach key [keylget listwindows]\
			{
				if {[regexp [pwd]/$ic [keylget listwindows \
					$key.cwd]]}\
				{	_closeWindow $key
				}
			}
		}
		set dir [pwd]
		changeDir .center.icons.can
		foreach key [keylget listwindows]\
		{	
			_changeDirWindow $key
		}
	}
	focus .itemMove
}

proc DtreeMove {c}\
{
	global listicons0 s dir tmp listwindows sel
	if {! [regexp ^/ $tmp]}\
	{
		return
	}
	set sel {}
	foreach ic [$c find all]\
	{
		if {[regexp selected [$c gettags $ic]]}\
		{
			lappend sel [keylget listicons0 \
				[lindex [$c gettags $ic] 0]]
		}
	}
	if {[winfo exists .treeMove]}\
	{	destroy .treeMove
		destroy .tmoveicon
	}
	toplevel .treeMove -cursor top_left_arrow
	wm protocol .treeMove WM_DELETE_WINDOW "catch {destroy .treeMove
							destroy .tmoveicon}"
	wm geometry .treeMove +100+100
	wm minsize .treeMove 250 90
	wm title .treeMove "Confirmation"
		toplevel .tmoveicon
		label .tmoveicon.img -image about
		pack .tmoveicon.img
		wm iconwindow .treeMove .tmoveicon
	wm geometry .tmoveicon 50x50+100+100
	frame .treeMove.ent
	pack .treeMove.ent
	label .treeMove.ent.txdir -justify left -wraplength 250\
		-text "\nMove \"$sel\" to \"$tmp\"?" 
	pack .treeMove.ent.txdir -side left
	frame .treeMove.but
	pack .treeMove.but -side bottom
	button .treeMove.but.ok -width 10 -text "OK" -command \
	{	
		catch {destroy .treeMove
			destroy .tmoveicon}
		foreach ic $sel\
		{
			file stat $ic a
			file stat $tmp b
			if {[regexp [array get a dev] [array get b dev]]}\
			{
				set ct [catch {frename \
					$ic \
					$tmp/$ic} errmsg]
				if {$ct > 0}\
				{	tk_dialog .baddir {Error}\
						$errmsg {} 0 OK
					return
				}
				set dir [pwd]
				changeDir .center.icons.can
			}\
			else\
			{
				itemCopy 0 .center.icons.can $ic
				itemPaste t $tmp .center.tree.can
				set dir [pwd]
				changeDir .center.icons.can
			}
			foreach key [keylget listwindows]\
			{
				if {[regexp [pwd]/$ic [keylget listwindows \
					$key.cwd]]}\
				{	_closeWindow $key
				}
			}
		}
		foreach key [keylget listwindows]\
		{
			
			_changeDirWindow $key
		}
	}
	pack .treeMove.but.ok -side left
	button .treeMove.but.cancel -width 10 -text "Cancel" -command \
	{
		catch {destroy .treeMove
			destroy .tmoveicon}
	}
	pack .treeMove.but.cancel -side right
	bind .treeMove <Return> \
	{	
		catch {destroy .treeMove
			destroy .tmoveicon}
		foreach ic $sel\
		{
			file stat $ic a
			file stat $tmp b
			if {[regexp [array get a dev] [array get b dev]]}\
			{
				set ct [catch {frename \
					$ic \
					$tmp/$ic} errmsg]
				if {$ct > 0}\
				{	tk_dialog .baddir {Error}\
						$errmsg {} 0 OK
					return
				}
				set dir [pwd]
				changeDir .center.icons.can
			}\
			else\
			{
				itemCopy 0 .center.icons.can $ic
				itemPaste t $tmp .center.tree.can
				set dir [pwd]
				changeDir .center.icons.can
			}
			foreach key [keylget listwindows]\
			{
				if {[regexp [pwd]/$ic [keylget listwindows \
					$key.cwd]]}\
				{	_closeWindow $key
				}
			}
		}
		foreach key [keylget listwindows]\
		{
			
			_changeDirWindow $key
		}
	}
	focus .treeMove
}

proc itemStopDrag {c x y}\
{
    	global s dir lastX lastY listicons0 listmini tmp
	set sel {}
	foreach ic [$c find all]\
	{
		if {[regexp selected [$c gettags $ic]]}\
		{
			lappend sel [keylget listicons0 \
				[lindex [$c gettags $ic] 0]]
		}
	}
	set d $c
	set c .center.icons.can
	if {$s == ""}\
	{	
		return
	}
	set t [$c gettags selected]
	if {$t == ""}\
	{
		return
	}
	if {! [regexp ^icone $t]}\
	{
		return
	}
    	set x [$d canvasx $x]
    	set y [$d canvasy $y]
	set n [$d gettags current]
	if {[regexp icon $d]}\
	{
		catch {set n [keylget listicons0 [lindex $n 0]]}
		foreach ic $sel\
		{	
			if {$ic == $n}\
			{
				return
			}
		}
		if {[file isdirectory $n]}\
		{
			set tmp $n
			DitemMove $c
		}\
		else\
		{    	
			foreach i [$c find all]\
			{
				set ic [$c gettags $i]
				if {[regexp ^icone $ic] && $ic!="icones" \
					&& [regexp selected $ic]}\
				{
					$c move [lindex $ic 0] \
						[expr $x-$lastX] \
						[expr $y-$lastY]
    					$c move [keylget listicons0 \
						[lindex $ic 0]] \
						[expr $x-$lastX] \
						[expr $y-$lastY]
				}
			}
		}
	}\
	else\
	{
		update
		set tmp [lindex [$d gettags current] 0]
		DtreeMove $c
	}
	if {[regexp .center.icons.can $c]}\
	{
		.center configure -cursor top_left_arrow
	}
}

proc itemCopy {id c args}\
{
	global listicons$id listwindows
	if {$id > 0}\
	{	set m [keylget listwindows $id.cwd]/
	}\
	else\
	{	set m [pwd]/
	}
	if {[regexp // $m]}\
	{	set m /
	}
	if {$args != ""}\
	{
		set fil $args
	}\
	else\
	{
		set s [$c gettags selected]
		if {$s == ""}\
		{	return
		}
		set fil [keylget listicons$id [lindex $s 0]]
	}
	set f "$m$fil"
	clipboard clear
	clipboard append -type FILE_NAME $f
}

proc pasteRecursive {id new w fol}\
{
	if {[file isdirectory "$fol"]}\
	{	set i ""
		set j 0
		set ct [catch {mkdir "$new/[file tail $fol]$i"} errmsg]
		if {$ct > 0}\
		{	tk_dialog .baddir {Error}\
				$errmsg {} 0 OK
			return
		}
		foreach f [readdir "$fol"]\
		{	
			pasteRecursive $id "$new/[file tail $fol]$i" $w "$fol/$f"
		}
		set i ""
	}\
	else\
	{
		clipboard clear
		clipboard append -type FILE_NAME "$fol"
		if {! [regexp [file type [selection get -type FILE_NAME \
			-selection CLIPBOARD]] file]}\
		{
			exec cp -l "[selection get -type FILE_NAME \
				-selection CLIPBOARD]" "$new/[file tail \
				[selection get -type FILE_NAME \
				-selection CLIPBOARD]]"
		}\
		else\
		{
			set a [read_file \
				"[selection get -type FILE_NAME \
				-selection CLIPBOARD]"]
			write_file "$new/[file tail [selection get \
				-type FILE_NAME -selection CLIPBOARD]]" "$a"
		}
	}
}

proc itemPaste {id new w}\
{
	global listwindows treestat dir
	pushd [pwd]
	if {$new == "p"}\
	{	
		if {$id > 0}\
		{	set new [keylget listwindows $id.cwd]
		}\
		else\
		{	set new [pwd]	
		}
		if {[regexp // $new]}\
		{	set new /
		}
	}
	set ct [catch {set sel [selection get -type FILE_NAME \
		-selection CLIPBOARD]}]
	if {$ct > 0}\
	{	popd
		return
	}
	if {[file isdirectory $sel]}\
	{	
		if {! [regexp $sel/ "$new/[file tail $sel]"]}\
		{
			pasteRecursive $id "$new" $w "$sel"
		}\
		else\
		{	tk_dialog .baddir {Error}\
				"Cannot paste $sel into $new" {} 0 OK
		}
	}\
	else\
	{
		set i ""
		set j 0
		set ct [catch {set a [read_file $sel]
			write_file "$new/[file tail $sel]$i" "$a"} errmsg]
		if {$ct > 0}\
		{	
			tk_dialog .baddir {Error}\
				$errmsg {} 0 OK
			popd
			return
		}
	}
	popd
	set dir [pwd]
	changeDir .center.icons.can
	foreach key [keylget listwindows]\
	{	_changeDirWindow $key
	}
}
	
proc DitemOpen {c}\
{
	if {[winfo exists .itemOpen]}\
	{	destroy .itemOpen
		destroy .openicon
	}
	global listicons0 s dir 
	toplevel .itemOpen -cursor top_left_arrow
	wm protocol .itemOpen WM_DELETE_WINDOW "catch {destroy .itemOpen
							destroy .openicon}"
	wm geometry .itemOpen 250x90+100+100
	wm title .itemOpen "Open file"
		toplevel .openicon
		label .openicon.img -image about
		pack .openicon.img
		wm iconwindow .itemOpen .openicon
	frame .itemOpen.ent
	pack .itemOpen.ent
	if {[file executable $dir]}\
	{	label .itemOpen.ent.txdir -text "\nArguments to \
			\"[keylget listicons0 [lindex $s 0]]\" : "
		pack .itemOpen.ent.txdir -side left
	}\
	else\
	{
		label .itemOpen.ent.txdir -text "\nOpen \
			\"[keylget listicons0 [lindex $s 0]]\" with :"
		pack .itemOpen.ent.txdir -side left
	}
	entry .itemOpen.ent.comm -width 25 -background #FFFFFF
	pack .itemOpen.ent.comm -side right -anchor s
	frame .itemOpen.but
	pack .itemOpen.but -side bottom
	button .itemOpen.but.ok -width 10 -text "OK" -command \
	{	
		set comm [.itemOpen.ent.comm get]
		catch {destroy .itemOpen
			destroy .openicon}
		if {[file executable $dir]}\
		{	set ct [catch {exec $dir $comm} errmsg]
		}\
		else\
		{
			set ct [catch {exec $comm $dir} errmsg]
		}
		if {$ct > 0}\
		{	tk_dialog .baddir {Error}\
				$errmsg {} 0 OK
			return
		}
		set dir [pwd]
		changeDir .center.icons.can
	}
	pack .itemOpen.but.ok -side left
	button .itemOpen.but.cancel -width 10 -text "Cancel" -command \
	{
		catch {destroy .itemOpen
			destroy	.openicon}
	}
	pack .itemOpen.but.cancel -side right
	bind .itemOpen <Return> \
	{	
		set comm [.itemOpen.ent.comm get]
		catch {destroy .itemOpen
			destroy .openicon}
		if {[file executable $dir]}\
		{	set ct [catch {exec $dir $comm &} errmsg]
		}\
		else\
		{
			set ct [catch {exec $comm $dir &} errmsg]
		}
		if {$ct > 0}\
		{	tk_dialog .baddir {Error}\
				$errmsg {} 0 OK
			return
		}
		set dir [pwd]
		changeDir .center.icons.can
	}
	focus .itemOpen.ent.comm
}

proc itemOpen {c}\
{
	global s dir listicons0
    	set s [$c gettags selected] 
	if {$s == ""}\
	{
		return
	}
    	if {[regexp ^icone $s]} \
	{
    		set f [keylget listicons0 [lindex $s 0]] 
		if {[file type $f] == "link"} {set f [file readlink $f]}
		switch [file type $f] \
		directory \
		{
			set dir $f
			changeDir .center.icons.can
		}\
		characterSpecial\
		{}\
		blockSpecial\
		{}\
		fifo\
		{}\
		link\
		{
		}\
		socket\
		{set ic "socket.bmp"}\
		file\
		{
			set dir $f
			DitemOpen $c
		}
	}
}

	set numwindow 0
proc newOpen {id c}\
{
	global listicons$id numwindow listwindows
	set t [$c gettags selected]
	if {$t == ""}\
	{	return
	}
	if {! [regexp ^icone $t]}\
	{	return
	}
	if {$id > 0}\
	{	set p [keylget listwindows $id.cwd]
		if {[regexp // $p]}\
		{	set p /
		}
	}\
	else\
	{	set p [pwd]
	}
	if {[file isdirectory $p/[keylget listicons$id [lindex $t 0]]]}\
	{
		_newWindow [_initWindow $p [keylget listicons$id [lindex $t 0]]]
	}
	$c dtag selected
	catch {itemLeave $c}
	catch {_itemLeaveWindow $c}
}

proc itemRename {id c}\
{
	global listicons$id treestat listwindows
	global dir
	set t [$c gettags selected]
	if {$id > 0}\
	{	set m [keylget listwindows $id.cwd]/
		if {[regexp // $m]}\
		{	set m /
		}
	}\
	else\
	{	set m ""
	}
	set l $m
	if {[file pathtype $dir] == "absolute"}\
	{	set l ""
	}
	set ct [catch {frename $m[keylget listicons$id [lindex $t 0]] $l$dir} errmsg]
	if {$ct > 0} \
	{
		tk_dialog .baddir {Error}\
			$errmsg {} 0 OK
		$c dtag selected
		return
	}\
	else\
	{
		catch {destroy .rename
			destroy .renameicon}
		$c dtag selected
		foreach key [keylget listwindows]\
		{
			if {[regexp ^$dir/$m[keylget listicons$id \
				[lindex $t 0]] [keylget listwindows $key.cwd]]}\
			{	_closeWindow $key
			}
		}
		foreach key [keylget listwindows]\
		{	_changeDirWindow $key
		}
		set dir [pwd]
		changeDir .center.icons.can
	}
}

proc DitemRename {id c}\
{
	global listicons$id gc i
	set i $id
	set gc $c
	if {[winfo exists .rename]}\
	{	destroy .rename
		destroy .renameicon
	}
	set t [$c gettags selected]
	if {![regexp ^icone $t]} \
	{	
		tk_dialog .baddir {Error}\
			"Please select an element to rename" {} 0 OK
		$c dtag selected
		return
	}
	toplevel .rename -cursor top_left_arrow
	wm protocol .rename WM_DELETE_WINDOW "catch {destroy .rename
							destroy .renameicon}"
	wm geometry .rename 250x150+100+100
	wm title .rename "Rename"
		toplevel .renameicon
		label .renameicon.img -image about
		pack .renameicon.img
		wm iconwindow .rename .renameicon
	frame .rename.ent
	pack .rename.ent
	label .rename.ent.txdir -wraplength 240 \
		-text "\nRename \"[keylget listicons$id [lindex $t 0]]\" to : "
	pack .rename.ent.txdir -side top -anchor w
	entry .rename.ent.dir -width 30  -background #FFFFFF
	pack .rename.ent.dir -side bottom -anchor w
	bind .rename.ent.dir <Return> \
	{	set dir [.rename.ent.dir get]
		itemRename $i $gc
	}
	frame .rename.but
	pack .rename.but -side bottom
	button .rename.but.ok -width 10 -text "OK" -command \
	{	
		set dir [.rename.ent.dir get]
		itemRename $i $gc
	}
	pack .rename.but.ok -side left
	button .rename.but.cancel -width 10 -text "Cancel" -command \
	{	catch {destroy .rename
			destroy .renameicon}
	}
	pack .rename.but.cancel -side right
	focus .rename.ent.dir
}

proc DitemLink {id c}\
{
	global listicons$id dir sym gc i
	set gc $c
	set i $id
	set sym 1
	if {[winfo exists .link]}\
	{	destroy .link
		destroy .linkicon
	}
	set t [$c gettags selected]
	if {$t == ""}\
	{	return
	}
	if {![regexp ^icone $t]} \
	{	
		tk_dialog .baddir {Error}\
			"Please select an element to make a link" {} 0 OK
		$c dtag selected
		return
	}
	set dir [keylget listicons$id [lindex $t 0]]
	toplevel .link -cursor top_left_arrow
	wm protocol .link WM_DELETE_WINDOW "catch {destroy .link
							destroy .linkicon}"
	wm geometry .link 250x150+100+100
	wm title .link "Link"
		toplevel .linkicon
		label .linkicon.img -image about
		pack .linkicon.img
		wm iconwindow .link .linkicon
	frame .link.ent
	pack .link.ent -expand yes 
	label .link.ent.txdir -wraplength 240 \
		-text "\nLink \"$dir\" to : "
	pack .link.ent.txdir -side top -anchor w
	entry .link.ent.dir -width 30  -background #FFFFFF
	pack .link.ent.dir -side bottom -anchor w
	bind .link.ent.dir <Return> \
	{	
		itemLink $i $gc [.link.ent.dir get] $sym
		catch {destroy .link
			destroy .linkicon}
	}
	frame .link.mid
	pack .link.mid  
	checkbutton .link.mid.sym -text "Symbolic link" -variable sym
	pack .link.mid.sym -side left -anchor w
	frame .link.but
	pack .link.but -side bottom
	button .link.but.ok -width 10 -text "OK" -command \
	{	
		itemLink $i $gc [.link.ent.dir get] $sym
		catch {destroy .link
			destroy .linkicon}
	}
	pack .link.but.ok -side left
	button .link.but.cancel -width 10 -text "Cancel" -command \
	{	
		catch {destroy .link
			destroy .linkicon}
	}
	pack .link.but.cancel -side right
	focus .link.ent.dir
}

proc itemLink {id c p sym}\
{
	global dir listwindows
	if {$id > 0}\
	{	set m [keylget listwindows $id.cwd]/
	}\
	else\
	{	set m [pwd]/
	}
	if {[regexp // $m]}\
	{	set m /
	}
	set l $m
	if {[file pathtype $p] == "absolute"}\
	{	set l ""
	}
	if {$sym == 1}\
	{
		set ct [catch {link -sym $m$dir $l$p} errmsg]
	}\
	else\
	{
		set ct [catch {link $m$dir $l$p} errmsg]
	}
	if {$ct > 0}\
	{	tk_dialog .baddir {Error}\
			$errmsg {} 0 OK
	}
	$c dtag selected
	catch {itemLeave $c}
	catch {_itemLeaveWindow $id}
	set dir [pwd]
	changeDir .center.icons.can
	foreach key [keylget listwindows]\
	{	_changeDirWindow $key
	}
}


proc createIcon {c id x y col tx}\
{
	global listcolor
	global icchar icblock icfifo iclink icsocket icdir icexec icfile icdef
	set ct [catch {switch [file type $tx]\
	{
		characterSpecial\
		{set ic $icchar}\
		blockSpecial\
		{set ic $icblock}
		fifo\
		{set ic $icfifo}\
		link\
		{set ic $iclink}\
		socket\
		{set ic $icsocket}\
		directory\
		{set ic $icdir}\
		file\
		{
			if {[file executable $tx]}\
			{set ic $icexec}\
			else\
			{set ic $icfile}\
		}
		default\
		{set ic $icdef}
	} } errmsg]
	if {$ct > 0}\
	{	set ic $icdef
	}
	set item [$c create bitmap $x $y \
		-bitmap @[file join $ic]]
	$c addtag icone$id withtag $item
	$c addtag icones withtag $item
	$c itemconfig $item -foreground [lindex $listcolor 2]
	set item [$c create text $x [expr $y+20] \
		-width 65 -anchor n  -text "$tx" -justify center]
	$c addtag $tx withtag $item
	$c addtag texte withtag $item
	regsub -all {\\} $tx {\\\\} tx2
	regsub -all "\}" $tx2 "\\\}" tx2
	regsub -all "\{" $tx2 "\\\{" tx2
	regsub -all {\]} $tx2 {\]} tx2
	regsub -all {\[} $tx2 {\[} tx2
	regsub -all {"} $tx2 {\"} tx2
	if {[clength $tx] > 7}\
	{
		$c bind "$tx" <Any-Enter> "$c itemconfigure \"$tx2\" -text \"$tx2\""
		$c bind "$tx" <Any-Leave> "if {[$c index $tx end] > 14}\
					{	
						$c dchars \"$tx2\" 12 end
						$c insert \"$tx2\" end >>
					}"
		$c bind icone$id <Any-Enter> "$c itemconfigure \"$tx2\" -text \"$tx2\""
		$c bind icone$id <Any-Leave> "if {[$c index $tx end] > 14}\
					{	
						$c dchars \"$tx2\" 12 end
						$c insert \"$tx2\" end >>
					}"
	}\
	else\
	{
		$c bind "$tx" <Any-Enter> ""
		$c bind "$tx" <Any-Leave> ""
		$c bind icone$id <Any-Enter> ""
		$c bind icone$id <Any-Leave> ""
	}
	global listicons0
	if {$c == ".center.icons.can"}\
	{
		keylset listicons0 icone$id $tx
	}
}

proc itemPermissions {id fol}\
{
	global owr oww owx grr grw grx otr otw otx suid sgid stk listwindows
	set own [expr ($owr * 4) + ($oww * 2) + $owx]
	set grp [expr ($grr * 4) + ($grw * 2) + $grx]
	set oth [expr ($otr * 4) + ($otw * 2) + $otx]
	set bit [expr ($suid * 4) + ($sgid * 2) + $stk]
	if {$id > 0}\
	{	set m [keylget listwindows $id.cwd]/
		if {[regexp // $m]}\
		{	set m /
		}
	}\
	else\
	{	set m ""
	}
	set ct [catch {exec chmod $bit$own$grp$oth "$m$fol"} errmsg]
	if {$ct > 0}\
	{
		tk_dialog .baddir {Error}\
			$errmsg {} 0 OK
	}
}	
	

proc DitemPermissions {id c}\
{
	global dir listicons$id listwindows gc i
	set gc $c
	set i $id
	global owr oww owx grr grw grx otr otw otx suid sgid stk
	global lowr loww lowx lgrr lgrw lgrx lotr lotw lotx lsuid lsgid lstk
	set lowr 0
	set loww 0
	set lowx 0
	set lgrr 0
	set lgrw 0
	set lgrx 0
	set lotr 0
	set lotw 0
	set lotx 0
	set lsuid 0
	set lsgid 0
	set lstk 0
	if {[winfo exists .permissions]}\
	{	destroy .permissions
		destroy .permicon
	}
	set t [$c gettags selected]
	if {$t == ""}\
	{	return
	}
	if {! [regexp ^icone $t]}\
	{	return
	}
	set t [keylget listicons$id [lindex $t 0]]
	set dir $t
	if {$id > 0}\
	{	set m [keylget listwindows $id.cwd]/
		if {[regexp // $m]}\
		{	set m /
		}
	}\
	else\
	{	set m ""
	}
	set a [exec ls -ld $m$t]
	set q [lindex $a 0]
	toplevel .permissions -cursor top_left_arrow
	wm protocol .permissions WM_DELETE_WINDOW "catch {destroy .permissions
							destroy .permicon}"
	wm geometry .permissions 350x150+100+100
	wm title .permissions "Permissions"
		toplevel .permicon
		label .permicon.img -image about
		pack .permicon.img
		wm iconwindow .permissions .permicon
	frame .permissions.ent 
	pack .permissions.ent
	label .permissions.ent.read -text "Read" -width 7
	grid configure .permissions.ent.read -column 2 -row 1
	label .permissions.ent.writ -text "Write" -width 7
	grid configure .permissions.ent.writ -column 3 -row 1
	label .permissions.ent.exec -text "Execute" -width 7
	grid configure .permissions.ent.exec -column 4 -row 1
	label .permissions.ent.own -text "Owner " -width 6
	grid configure .permissions.ent.own -column 1 -row 2
	checkbutton .permissions.ent.owr -variable owr
	grid configure .permissions.ent.owr -column 2 -row 2
	checkbutton .permissions.ent.oww -variable oww
	grid configure .permissions.ent.oww -column 3 -row 2
	checkbutton .permissions.ent.owx -variable owx
	grid configure .permissions.ent.owx -column 4 -row 2
	label .permissions.ent.grp -text "Group " -width 6
	grid configure .permissions.ent.grp -column 1 -row 3
	checkbutton .permissions.ent.grr -variable grr
	grid configure .permissions.ent.grr -column 2 -row 3
	checkbutton .permissions.ent.grw -variable grw
	grid configure .permissions.ent.grw -column 3 -row 3
	checkbutton .permissions.ent.grx -variable grx
	grid configure .permissions.ent.grx -column 4 -row 3
	label .permissions.ent.oth -text "Others" -width 6
	grid configure .permissions.ent.oth -column 1 -row 4
	checkbutton .permissions.ent.otr -variable otr
	grid configure .permissions.ent.otr -column 2 -row 4
	checkbutton .permissions.ent.otw -variable otw
	grid configure .permissions.ent.otw -column 3 -row 4
	checkbutton .permissions.ent.otx -variable otx
	grid configure .permissions.ent.otx -column 4 -row 4
	label .permissions.ent.txsuid -text "Suid bit  " -width 10
	grid configure .permissions.ent.txsuid -column 5 -row 2
	checkbutton .permissions.ent.suid -variable suid
	grid configure .permissions.ent.suid -column 6 -row 2
	label .permissions.ent.txsgid -text "Sgid bit  " -width 10
	grid configure .permissions.ent.txsgid -column 5 -row 3
	checkbutton .permissions.ent.sgid -variable sgid
	grid configure .permissions.ent.sgid -column 6 -row 3
	label .permissions.ent.txstk -text "Sticky bit" -width 10
	grid configure .permissions.ent.txstk -column 5 -row 4
	checkbutton .permissions.ent.stk -variable stk
	grid configure .permissions.ent.stk -column 6 -row 4
	frame .permissions.but
	pack .permissions.but -side bottom
	button .permissions.but.ok -width 10 -text "OK" -command \
	{	
		catch {destroy .permissions
			destroy .permicon}
		$gc dtag selected
		itemLeave .center.icons.can
		catch {_itemLeaveWindow $i}
		if {($owr != $lowr)||($oww != $loww)||($owx != $lowx)||\
			($grr != $lgrr)||($grw != $lgrw)||($grx != $lgrx)||\
			($otr != $lotr)||($otw != $lotw)||($otx != $lotx)||\
			($suid != $lsuid)||($sgid != $lsgid)||\
			($stk != $lstk)}\
		{

			itemPermissions $i $dir
		}
		set dir [pwd]
		changeDir .center.icons.can
		foreach key [keylget listwindows]\
		{	_changeDirWindow $key
		}
	}
	pack .permissions.but.ok -side left
	button .permissions.but.cancel -width 10 -text "Cancel" -command \
	{	catch {destroy .permissions
			destroy .permicon}
	}
	pack .permissions.but.cancel -side right
	bind .permissions <Return> \
	{	
		catch {destroy .permissions
			destroy .permicon}
		$gc dtag selected
		itemLeave .center.icons.can
		catch {_itemLeaveWindow $i}
		if {($owr != $lowr)||($oww != $loww)||($owx != $lowx)||\
			($grr != $lgrr)||($grw != $lgrw)||($grx != $lgrx)||\
			($otr != $lotr)||($otw != $lotw)||($otx != $lotx)||\
			($suid != $lsuid)||($sgid != $lsgid)||\
			($stk != $lstk)}\
		{

			itemPermissions $i $dir
		}
		set dir [pwd]
		changeDir .center.icons.can
		foreach key [keylget listwindows]\
		{	_changeDirWindow $key
		}
	}
	if {[regexp r [cindex $q 1]]}\
	{	.permissions.ent.owr select
		set lowr 1
	}\
	else\
	{	.permissions.ent.owr deselect
	}
	if {[regexp w [cindex $q 2]]}\
	{	.permissions.ent.oww select
		set loww 1
	}\
	else\
	{	.permissions.ent.oww deselect
	}
	if {[regexp x [cindex $q 3]]}\
	{	.permissions.ent.owx select
		set lowx 1
	}\
	else\
	{	.permissions.ent.owx deselect
	}
	if {[regexp S [cindex $q 3]]}\
	{	.permissions.ent.suid select
		set lsuid 1
	}\
	elseif {[regexp s [cindex $q 3]]}\
	{	.permissions.ent.suid select
		.permissions.ent.owx select
		set lsuid 1
		set lowx 1
	}\
	else\
	{	.permissions.ent.suid deselect
	}
	if {[regexp r [cindex $q 4]]}\
	{	.permissions.ent.grr select
		set lgrr 1
	}\
	else\
	{	.permissions.ent.grr deselect
	}
	if {[regexp w [cindex $q 5]]}\
	{	.permissions.ent.grw select
		set lgrw 1
	}\
	else\
	{	.permissions.ent.grw deselect
	}
	if {[regexp x [cindex $q 6]]}\
	{	.permissions.ent.grx select
		set lgrx 1
	}\
	else\
	{	.permissions.ent.grx deselect
	}
	if {[regexp S [cindex $q 6]]}\
	{	.permissions.ent.sgid select
		set lsgid 1
	}\
	elseif {[regexp s [cindex $q 6]]}\
	{	.permissions.ent.sgid select
		.permissions.ent.grx select
		set lsgid 1
		set lgrx 1
	}\
	else\
	{	.permissions.ent.sgid deselect
	}
	if {[regexp r [cindex $q 7]]}\
	{	.permissions.ent.otr select
		set lotr 1
	}\
	else\
	{	.permissions.ent.otr deselect
	}
	if {[regexp w [cindex $q 8]]}\
	{	.permissions.ent.otw select
		set lotw 1
	}\
	else\
	{	.permissions.ent.otw deselect
	}
	if {[regexp x [cindex $q 9]]}\
	{	.permissions.ent.otx select
		set lotx 1
	}\
	else\
	{	.permissions.ent.otx deselect
	}
	if {[regexp T [cindex $q 9]]}\
	{	.permissions.ent.stk select
		set lstk 1
	}\
	elseif {[regexp t [cindex $q 9]]}\
	{	.permissions.ent.stk select
		.permissions.ent.otx select
		set lstk 1
		set lotx 1
	}\
	else\
	{	.permissions.ent.stk deselect
	}
	update
	focus .permissions
}


proc DitemProperties {id c}\
{
	global listicons$id listwindows gc
	set gc $c
	if {[winfo exists .properties]}\
	{	destroy .properties
		destroy .propicon
	}
	set t [$c gettags selected]
	if {$t == ""}\
	{	return
	}
	if {! [regexp ^icone $t]}\
	{	return
	}
	set t [keylget listicons$id [lindex $t 0]]
	if {$id > 0}\
	{	set m [keylget listwindows $id.cwd]/
		if {[regexp // $m]}\
		{	set m /
		}
	}\
	else\
	{	set m ""
	}
	set a [exec ls -ld "$m$t"]
	toplevel .properties -cursor top_left_arrow
	wm protocol .properties WM_DELETE_WINDOW "catch {destroy .properties
							destroy .propicon}"
	wm geometry .properties 350x180+100+100
	wm title .properties "Properties"
		toplevel .propicon
		label .propicon.img -image about
		pack .propicon.img
		wm iconwindow .properties .propicon
	frame .properties.ent
	pack .properties.ent
	label .properties.ent.txdir -wraplength 340 -justify left \
		-text "\nName\t\t$m$t\
		\nType\t\t[file type $m$t]\
		\nPermissions\t[lindex $a 0]\
		\nHard Links\t[lindex $a 1]\
		\nOwner\t\t[lindex $a 2]\
		\nGroup\t\t[lindex $a 3]\
		\nSize\t\t[lindex $a 4] bytes\
		\nModification time\t[lindex $a 5] [lindex $a 6] [lindex $a 7]"
	pack .properties.ent.txdir
	frame .properties.but
	pack .properties.but -side bottom
	button .properties.but.ok -width 10 -text "OK" -command \
	{	catch {destroy .properties
			destroy .propicon}
		.center.icons.can dtag selected
		itemLeave .center.icons.can
		catch {_itemLeaveWindow $gc}
	}
	pack .properties.but.ok 
	bind .properties <Return> \
	{	catch {destroy .properties
			destroy .propicon}
		.center.icons.can dtag selected
		itemLeave .center.icons.can
		catch {_itemLeaveWindow $gc}
	}
	focus .properties
}
proc changeDir {c}\
{
	global dir flaghidden scrw
	set ct [catch {cd $dir} errmsg]
	if {$ct > 0} \
	{
		tk_dialog .baddir {Error} \
			$errmsg {} 0 OK
		return 	
	}\
	else\
	{
		catch {destroy .chdir
			destroy .chdiricon}
		if {$flaghidden}\
		{
			set n [lsort [readdir [pwd]]]
		}\
		else\
		{
			set n [exec ls]
			set n [split $n \n]
		}
		if {! [regexp \.trash $dir]}\
		{	
			set n [linsert $n 0 ..]
		}
		$c delete all
		for {set i 0} {$i<[llength $n]} {incr i}\
		{	
			set k [expr $scrw>=800?9:6]
			createIcon $c $i [expr 35+70*($i%$k)] \
				[expr 35+70*(floor($i/$k))]\
			 	blue [lindex $n $i]
		}
		foreach tg [$c find withtag texte]\
		{	
			if {[$c index [lindex [$c gettags $tg] 0] end] > 14}\
			{	
				$c dchars $tg 12 end
				$c insert $tg end >>
			}
		}
		$c configure \
			-scrollregion [$c bbox all]
		$c xview moveto 0
		if {[regexp \.trash [pwd]]}\
		{	.center.icons.buttons.lb configure -text TRASH
		}\
		else\
		{	.center.icons.buttons.lb configure -text [pwd]
		}
	}
}

proc DchangeDir {id c}\
{
	global dir listwindows i
	set i $id
	if {[winfo exists .chdir]}\
	{	destroy .chdir
		destroy .chdiricon
	}
	toplevel .chdir -cursor top_left_arrow
	wm protocol .chdir WM_DELETE_WINDOW "catch {destroy .chdir
							destroy .chdiricon}"
	wm geometry .chdir 250x90+100+100
	wm title .chdir "Change directory"
		toplevel .chdiricon
		label .chdiricon.img -image about
		pack .chdiricon.img
		wm iconwindow .chdir .chdiricon
	frame .chdir.ent
	pack .chdir.ent
	label .chdir.ent.txdir -text "\nChange to : "
	pack .chdir.ent.txdir -side left
	entry .chdir.ent.dir -width 30  -background #FFFFFF
	pack .chdir.ent.dir -side right -anchor s
	bind .chdir.ent.dir <Return> \
	{	
		if {$i == 0}\
		{
			set dir [.chdir.ent.dir get]
			changeDir .center.icons.can 
		}\
		else\
		{
			set v [.chdir.ent.dir get]
			if {[regexp ^/ $v]}\
			{	keylset listwindows $i.cwd $v
			}\
			else\
			{
				if {[regexp ^/$ [keylget listwindows $i.cwd]]}\
				{	
					keylset listwindows $i.cwd /$v
				}\
				else\
				{
					keylset listwindows $i.cwd [keylget \
					listwindows $i.cwd]/$v
				}
			}
			_changeDirWindow $i
		}
	}
	frame .chdir.but
	pack .chdir.but -side bottom
	button .chdir.but.ok -width 10 -text "OK" -command \
	{	
		if {$i == 0}\
		{
			set dir [.chdir.ent.dir get]
			changeDir .center.icons.can 
		}\
		else\
		{
			set v [.chdir.ent.dir get]
			if {[regexp ^/ $v]}\
			{	keylset listwindows $i.cwd $v
			}\
			else\
			{
				if {[regexp ^/$ [keylget listwindows $i.cwd]]}\
				{	
					keylset listwindows $i.cwd /$v
				}\
				else\
				{
					keylset listwindows $i.cwd [keylget \
					listwindows $i.cwd]/$v
				}
			}
			_changeDirWindow $i
		}
	}
	pack .chdir.but.ok -side left
	button .chdir.but.cancel -width 10 -text "Cancel" -command \
	{	catch {destroy .chdir
			destroy .chdiricon}
	}
	pack .chdir.but.cancel -side right
	focus .chdir.ent.dir
}

proc newDir {id c}\
{
	global dir listwindows
	global treestat
	if {$id > 0}\
	{	set m [keylget listwindows $id.cwd]/
		if {[regexp // $m]}\
		{	set m /
		}
	}\
	else\
	{	set m ""
	}
	set ct [catch {mkdir [list "$m$dir"]} errmsg]
	if {$ct > 0} \
	{
		tk_dialog .baddir {Error} \
			$errmsg {} 0 OK
		return
	}\
	else\
	{
		catch {destroy .newDir
			destroy .newdiricon}
		set dir [pwd]
		changeDir .center.icons.can
		foreach key [keylget listwindows]\
		{	_changeDirWindow $key
		}
		if {[keylget treestat $dir {}]}\
		{
			if {[keylget treestat $dir] == 1}\
			{
				treeClose .center.tree.can [pwd]
				treeOpen .center.tree.can [pwd]
			}
		}
	}
}
	

proc DnewDir {id c}\
{
	global gc i
	set gc $c
	set i $id
	if {[winfo exists .newDir]}\
	{	destroy .newDir
		destroy .newdiricon
	}
	toplevel .newDir -cursor top_left_arrow
	wm protocol .newDir WM_DELETE_WINDOW "catch {destroy .newDir
							destroy .newdiricon}"
	wm geometry .newDir 250x90+100+100
	wm title .newDir "New directory"
		toplevel .newdiricon
		label .newdiricon.img -image about
		pack .newdiricon.img
		wm iconwindow .newDir .newdiricon
	frame .newDir.ent
	pack .newDir.ent
	label .newDir.ent.txdir -text "\nName : "
	pack .newDir.ent.txdir -side left
	entry .newDir.ent.dir -width 30  -background #FFFFFF
	pack .newDir.ent.dir -side right -anchor s
	bind .newDir.ent.dir <Return> \
	{	set dir [.newDir.ent.dir get]
		newDir $i $gc
	}
	frame .newDir.but
	pack .newDir.but -side bottom
	button .newDir.but.ok -width 10 -text "OK" -command \
	{	
		set dir [.newDir.ent.dir get]
		newDir $i $gc
	}
	pack .newDir.but.ok -side left
	button .newDir.but.cancel -width 10 -text "Cancel" -command \
	{	catch {destroy .newDir
			destroy .newdiricon}
	}
	pack .newDir.but.cancel -side right
	focus .newDir.ent.dir
}
	
proc deleteDirect {id c}\
{
	global listicons$id listwindows dir
	set sel {}
	foreach ic [$c find all]\
	{
		if {[regexp selected [$c gettags $ic]]}\
		{
			lappend sel [keylget listicons$id \
				[lindex [$c gettags $ic] 0]]
		}
	}
	set i $id
	set dir [$c gettags selected]
    	if {! [regexp ^icone $dir]} \
	{	return
	}
	if {$i > 0}\
	{	set m [keylget listwindows $i.cwd]/
	}\
	else\
	{	set m [pwd]/
	}
	if {[regexp // $m]}\
	{	set m /
	}
	foreach ic $sel\
	{
		if {[file type "$m$ic"] == "link"}\
		{	
			unlink [list $m$ic]
		}\
		elseif {[file isdirectory "$m$ic"]}\
		{	rmRecursive "$m$ic"
			rmdir [list "$m$ic"]
		}\
		else\
		{	
			unlink [list $m$ic]
		}
		set dir [pwd]
		changeDir .center.icons.can
		foreach key [keylget listwindows]\
		{
			if {[regexp ^$m$ic [keylget listwindows $key.cwd]]}\
			{	_closeWindow $key
			}
		}
	}
	foreach key [keylget listwindows]\
	{
		_changeDirWindow $key
	}
}

proc sendTrash {id c}\
{
	global listicons$id treestat listwindows sel
	global dir i gc
	set sel {}
	foreach ic [$c find all]\
	{
		if {[regexp selected [$c gettags $ic]]}\
		{
			lappend sel [keylget listicons$id \
				[lindex [$c gettags $ic] 0]]
		}
	}
	set i $id
	set gc $c
	set dir [$c gettags selected]
    	if {! [regexp ^icone $dir]} \
	{	return
	}
	if {[winfo exists .sendTrash]}\
	{	destroy .sendTrash
		destroy .strashicon
	}
	toplevel .sendTrash -cursor top_left_arrow
	wm protocol .sendTrash WM_DELETE_WINDOW "catch {destroy .sendTrash
							destroy .strashicon}"
	wm geometry .sendTrash +100+100
	wm minsize .sendTrash 250 90
	wm title .sendTrash "Confirmation"
		toplevel .strashicon
		label .strashicon.img -image about
		pack .strashicon.img
		wm iconwindow .sendTrash .strashicon
	frame .sendTrash.ent
	pack .sendTrash.ent
	label .sendTrash.ent.txdir -justify left -wraplength 250 \
		-text "\nSend \"$sel\" to trash?" 
	pack .sendTrash.ent.txdir -side left
	frame .sendTrash.but
	pack .sendTrash.but -side bottom
	button .sendTrash.but.ok -width 10 -text "OK" -command \
	{	
		catch {destroy .sendTrash
			destroy .strashicon}
		set tmp ~/.sfm/.trash
		if {$i > 0}\
		{	set m [keylget listwindows $i.cwd]/
		}\
		else\
		{	set m [pwd]/
		}
		if {[regexp // $m]}\
		{	set m /
		}
		foreach ic $sel\
		{
			file stat "$m$ic" a
			file stat $tmp b
			if {[regexp [array get a dev] [array get b dev]]}\
			{
				set i ""
				set j 0
				while {[file exists ~/.sfm/.trash/$ic$i]}\
				{	incr j
					set i -$j
				}
				set ct [catch {frename $m$ic \
					~/.sfm/.trash/$ic$i} errmsg]
				if {$ct > 0}\
				{	tk_dialog .baddir {Error}\
						$errmsg {} 0 OK
					return
				}
				set dir [pwd]
				changeDir .center.icons.can
			}\
			else\
			{
				set f $ic
				itemCopy $i $gc $f
				itemPaste $i ~/.sfm/.trash $gc
				if {[file type "$m$f"] == "link"}\
				{	
					unlink [list $m$f]
				}\
				elseif {[file isdirectory "$m$f"]}\
				{	rmRecursive "$m$f"
					rmdir [list "$m$f"]
				}\
				else\
				{	
					unlink [list $m$f]
				}
				set dir [pwd]
				changeDir .center.icons.can
			}
			foreach key [keylget listwindows]\
			{
				if {[regexp ^$m$ic [keylget \
					listwindows $key.cwd]]}\
				{	_closeWindow $key
				}
			}
		}
		foreach key [keylget listwindows]\
		{
			_changeDirWindow $key
		}
	}
	pack .sendTrash.but.ok -side left
	button .sendTrash.but.cancel -width 10 -text "Cancel" -command \
	{	catch {destroy .sendTrash
			destroy .strashicon}
	}
	pack .sendTrash.but.cancel -side right
	bind .sendTrash <Return> \
	{	
		catch {destroy .sendTrash
			destroy .strashicon}
		set tmp ~/.sfm/.trash
		if {$i > 0}\
		{	set m [keylget listwindows $i.cwd]/
		}\
		else\
		{	set m [pwd]/
		}
		if {[regexp // $m]}\
		{	set m /
		}
		foreach ic $sel\
		{
			file stat $m$ic a
			file stat $tmp b
			if {[regexp [array get a dev] [array get b dev]]}\
			{
				set i ""
				set j 0
				while {[file exists ~/.sfm/.trash/$ic$i]}\
				{	incr j
					set i -$j
				}
				set ct [catch {frename $m$ic \
					~/.sfm/.trash/$ic$i} errmsg]
				if {$ct > 0}\
				{	tk_dialog .baddir {Error}\
						$errmsg {} 0 OK
					return
				}
				set dir [pwd]
				changeDir .center.icons.can
			}\
			else\
			{
				set f $ic
				itemCopy $i $gc $f
				itemPaste $i ~/.sfm/.trash $gc
				if {[file type "$m$f"] == "link"}\
				{	
					unlink [list $m$f]
				}\
				elseif {[file isdirectory "$m$f"]}\
				{	rmRecursive "$m$f"
					rmdir [list "$m$f"]
				}\
				else\
				{	
					unlink [list $m$f]
				}
				set dir [pwd]
				changeDir .center.icons.can
			}
			foreach key [keylget listwindows]\
			{
				if {[regexp "^$m$ic" [keylget \
					listwindows $key.cwd]]}\
				{	_closeWindow $key
				}
			}
		}
		foreach key [keylget listwindows]\
		{
			_changeDirWindow $key
		}
	}
	focus .sendTrash
}

proc rmRecursive {fol}\
{
	foreach f [readdir "$fol"]\
	{
		if {[file type "$fol/$f"] == "link"}\
		{
			unlink [list "$fol/$f"]
		}\
		elseif {[file isdirectory "$fol/$f"]}\
		{
			if {! [lempty [readdir "$fol/$f"]]}\
			{	
				rmRecursive "$fol/$f"
			}
			rmdir [list "$fol/$f"]
		}\
		else\
		{
			unlink [list "$fol/$f"]
		}
	}
}

proc emptyTrash {c}\
{
	global dir listwindows
	if {[winfo exists .emptyTrash]}\
	{	destroy .emptyTrash
		destroy .etrashicon
	}
	toplevel .emptyTrash -cursor top_left_arrow
	wm protocol .emptyTrash WM_DELETE_WINDOW "catch {destroy .emptyTrash
							destroy .etrashicon}"
	wm geometry .emptyTrash 250x90+100+100
	wm title .emptyTrash "Confirmation"
		toplevel .etrashicon
		label .etrashicon.img -image about
		pack .etrashicon.img
		wm iconwindow .emptyTrash .etrashicon
	frame .emptyTrash.ent
	pack .emptyTrash.ent
	label .emptyTrash.ent.txdir -text "\nConfirm empty trash ? "
	pack .emptyTrash.ent.txdir -side left
	frame .emptyTrash.but
	pack .emptyTrash.but -side bottom
	button .emptyTrash.but.ok -width 10 -text "OK" -command \
	{	
		catch {destroy .emptyTrash
			destroy .etrashicon}
		rmRecursive ~/.sfm/.trash
		set dir [pwd]
		changeDir .center.icons.can
		foreach key [keylget listwindows]\
		{
			_changeDirWindow $key
		}
	}
	pack .emptyTrash.but.ok -side left
	button .emptyTrash.but.cancel -width 10 -text "Cancel" -command \
	{	catch {destroy .emptyTrash
			destroy .etrashicon}
	}
	pack .emptyTrash.but.cancel -side right
	bind .emptyTrash <Return> \
	{	
		catch {destroy .emptyTrash
			destroy .etrashicon}
		rmRecursive ~/.sfm/.trash
		set dir [pwd]
		changeDir .center.icons.can
		foreach key [keylget listwindows]\
		{
			_changeDirWindow $key
		}
	}
	focus .emptyTrash
}

proc deleteDir {c}\
{
	global listicons0 treestat 
	global dir
	set fol [$c gettags selected]
    	if {[regexp ^icone $fol]} \
	{
		set fol [keylget listicons0 [lindex $fol 0]] 
		if {[file isdirectory "$fol"]}\
		{
			set ct [catch {rmdir [list "$fol"]} errmsg]
		}\
		else\
		{
			set ct [catch {unlink [list $fol]} errmsg]
		}
		if {$ct > 0} \
		{
			tk_dialog .baddir {Error} \
				$errmsg {} 0 OK
			return
		}\
		else\
		{
			set dir [pwd]
			changeDir .center.icons.can
			if {[keylget treestat "$fol" {}]}\
			{
				if {[keylget treestat "$fol"] == 1]}\
				{
					treeClose .center.tree.can [pwd]
					treeOpen .center.tree.can [pwd]
				}
			}
		}
		
	}
}

proc createMini {c fol}\
{
	global y
	global icminidir listmini listcolor
	incr y
	set x [expr [llength [split "$fol" /]]]
	set item [$c create bitmap [expr 22*$x] [expr 22*$y+3] \
		-bitmap @[file join $icminidir]\
		-foreground [lindex $listcolor [expr $x % 7]]] 
	$c addtag "$fol" withtag $item
	set item [$c create text [expr 15+22*$x] [expr 22*$y] \
		-text [file tail "$fol"] -anchor nw]
}
	

proc treeShow {c}\
{
	global dir
	set a [.center.tree.can gettags current]
	if {! [regexp ^/ $a]}\
	{
		return
	}
	set dir [lindex $a 0]
	changeDir .center.icons.can
}

proc treeDouble {c}\
{	
	global treestat
	set a [.center.tree.can gettags current] 
	if {! [regexp ^/ $a]}\
	{
		return
	}
	set fol [lindex $a 0]
	if {! [keylget treestat "$fol" {}]}\
	{
		treeOpen $c "$fol"
	}\
	elseif {[keylget treestat "$fol"] == 1}\
	{
		treeClose $c "$fol"
	}\
	else\
	{	
		treeOpen $c "$fol"
	}
}


proc listIndex {fol}\
{
	global listmini
	for {set i 0} {$i < [llength $listmini]} {incr i}\
	{
		if {[lindex $listmini $i] == "$fol"}\
		{	return $i
		}
	}
}

proc treeClose {c fol}\
{
	global listmini treestat
	set a [lsearch $listmini "$fol"]
	set b $a
	while {[regexp "$fol" [lindex $listmini $b]]}\
	{	
		keylset treestat "$fol" 0
		incr b
	}
	set listmini [concat [lrange $listmini 0 $a]\
		[lrange $listmini $b [llength $listmini]]]
	treeUpdate $c
}

proc treeUpdate {c}\
{
	global listmini
	set h [lindex [$c xview] 0]
	set v [lindex [$c yview] 0]
	$c delete all
	set y 0
	foreach fol $listmini \
	{	createMini $c "$fol"
	}
	$c configure -scrollregion [$c bbox all]
	$c xview moveto 0
	$c xview moveto $h
	$c yview moveto $v
}

proc treeOpen {c fol}\
{
	global listmini treestat
	set save "$fol"
	set ct [catch {set r [lsort [readdir $save]]} errmsg]
	if {$ct > 0}\
	{	tk_dialog .baddir {Error}\
			$errmsg {} 0 OK
		return
	}
	keylset treestat $save 1
	set s ""
	if {$save != "/"} {set s /}
	for {set i 0} {$i<[llength $r]} {incr i}\
	{
		if {[file isdirectory $save$s[lindex $r $i]]}\
		{
			lappend t [lindex $r $i]
		}
	}
	set ct [catch {lindex $t 0}]
	set pos [expr [listIndex $save]+1]
	if {$ct == 0}\
	{
		foreach fol $t\
		{
			set listmini [linsert $listmini $pos $save$s$fol]
			incr pos
		}
		set h [lindex [$c xview] 0]
		set v [lindex [$c yview] 0]
		$c delete all
		set y 0
		foreach fol $listmini \
		{	createMini $c "$fol"
		}
		$c configure -scrollregion [$c bbox all]
		$c xview moveto 0
		$c xview moveto $h
		$c yview moveto $v
	}
}

proc Dabout {}\
{
	global IMGPATH scrw scrh
	if {[winfo exists .about]}\
	{	destroy .about
	}
	toplevel .about -cursor top_left_arrow
	wm geometry .about 288x228+[expr ($scrw - 288)/2]+[expr ($scrh - 228)/2]
	wm resizable .about 0 0
	image create photo gabout -file $IMGPATH/sfm.gif
	label .about.img -image gabout
	pack .about.img
	bind .about <ButtonRelease> "catch {destroy .about}"
	bind .about <Return> "catch {destroy .about}"
	focus .about
}


####################################################################



##########################main#################################
set ct [catch {	if {! [file exists ~/.sfm]}\
	{	mkdir ~/.sfm
	}
	if {! [file exists ~/.sfm/.trash]}\
	{	mkdir ~/.sfm/.trash
	}
}]
if {$ct > 0}\
{	set rep [tk_dialog .baddir {Warning}\
		"Unabled to create ~/.sfm or ~/.sfm/.trash. Continue anyway ?" \
			{} 0 Yes No]
	if {$rep == 1}\
	{
		exit
	}
}
set dir ~
changeDir .center.icons.can
lappend listmini /
set x [split [pwd] /]
treeOpen .center.tree.can /
set g /
for {set i 1} {$i < [llength $x]} {incr i}\
{
	treeOpen .center.tree.can $g[lindex $x $i]
	set g $g[lindex $x $i]/
}
#################################################################

