
# This code is shared by all the ICEM CFD GUI's.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc pathentry {what path {choices ""} {dir ""}} {
    global pathentry_choices pathentry_dir
    
    # don't use this any more because file names might have spaces in them
    return
    
    set pathentry_choices($path) $choices
    set pathentry_dir($path) $dir
    
    if {$what == "on"} {
	bind $path <Key-space> {pathentry_doit %W; break}
    } else {
	bind $path <Key-space> {}
	catch {top_destroy .pathentry_popup}
    }
}

proc pathentry_clear {} {catch {top_destroy .pathentry_popup}}

proc pathentry_doit {ent} {
    global pathentry_choices pathentry_dir
    
    pathentry_clear	
    
    set path [$ent get]

    
    set is_path [expr {$pathentry_choices($ent) == ""}]
    
    if $is_path {
#	if ![string match */* $path] { set path ./$path }
	if [string match */ $path] {
	    if {$pathentry_dir($ent) == ""} {
		set dir $path
	    } else {
		set dir $pathentry_dir($ent)/$path
	    }
	    set file ""
	} else {
	    if {$pathentry_dir($ent) == ""} {
		set dir [file dirname $path]
	    } else {
		set dir $pathentry_dir($ent)/[file dirname $path]
	    }
	    set file [file tail $path]
	}
	
	if {![file exists $dir]} {beep; return}
	if {$dir == "/"} {
	    set poss [glob -nocomplain /$file*]
	} else {
	    set poss [glob -nocomplain $dir/$file*]
	}
	if {$pathentry_dir($ent) != ""} {
	    set pp ""
	    foreach p $poss {
		lappend pp [ file tail $p ]
	    }
	    set poss $pp
	}
    } else {
	set poss ""
	foreach p $pathentry_choices($ent) {
	    if [string match $path* $p] { lappend poss $p }
	}
    }
    if {$poss == ""} {beep; return}
    if {[llength $poss] == 1} {
	if {($poss == $path) && $is_path && \
		[ file exists $poss ] && \
		([file type $poss] == "directory")} {
	    append poss /
	}
	$ent delete 0 end
	$ent insert 0 $poss
	return
    }
    
    # now extract the longest match.
    set poss [lsort $poss]
    set done 0
    set advanced 0
    while {!$done} {
	set pos [string length $path]
	set cc ""
	foreach x $poss {
	    if {[string length $x] <= $pos} {set done 1; break}
	    set y [string index $x $pos]
	    if {($cc != "") && ($cc != $y)} {set done 1; break}
	    set cc $y
	}
	if !$done {
	    set path [format "%s%s" $path $cc]
	    set advanced 1
	}
	#puts stdout "path is $path"
    }
    
    if $advanced {
	$ent delete 0 end
	$ent insert 0 $path
	return
    }
    
    set no_tail 0
    #set no_tail [string match */* [lindex $poss 0]]
    
    top_transient .pathentry_popup ""
    listbox .pathentry_popup.box -relief raised -borderwidth 2 \
	-background #aabbcc
    pack append .pathentry_popup \
	.pathentry_popup.box top
    set maxw 10
    set numins 0
    foreach q $poss {
	if !$no_tail { set q [file tail $q] }
	if {![regsub -all {\.} $q {_} r]} {set r $q}
	.pathentry_popup.box insert end "$q"
	if {[string length $q] > $maxw} { set maxw [string length $q] }
	incr numins
	if {$numins > 60} {
	    .pathentry_popup.box insert end " ... (truncated)"
	    incr numins
	    break
	}
    }
    if {$maxw > 100} { set maxw 100 }
    .pathentry_popup.box configure -width $maxw -height $numins
    bind .pathentry_popup.box <Button-1> "%W select set \[%W nearest %y\]; \
	    pathentry_clicked $ent \[lindex \[selection get\] 0\]; break"
    
    set x [expr {[winfo rootx $ent] + 100}]
    set y [expr {[winfo rooty $ent] + [winfo height $ent]}]
    top_geometry .pathentry_popup "+$x+$y"
}

proc pathentry_clicked {ent comp} {
    top_destroy .pathentry_popup
    if ![winfo exists $ent] return
    set what [$ent get]
    $ent delete 0 end
    set i [string last / $what]
    if {$i == -1} {
	$ent insert 0 $comp
    } else {
	$ent insert 0 [string range $what 0 $i]$comp
    }
}
