
# This code creates the ICEM CFD Manager.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# Select domain files from the list of available ones.
# This routine returns a list of the pathnames for the selected domains.
# dir is the directory where the domain files are.
# type is a list with struct, unstruct, global, super, struct_super, or mixed.
# multi is 0 or 1.
# The static variable domain_history is used to record which ones were
# selected last.
# The variable domain_type says what the type is.

# This used only by the output interfaces in med

proc domain_select {dir message type {mult 0} {useboco 1} {usetopo 1}} {
    global domain_history env bigfont dselect_done icem_fluent
    global domain_selected_already place confname domain_type
    global ai_environment file_name

    mess "$message\n" blue
    if {$type == "unstruct"} {
	    return [tk_getOpenFile -initialdir $file_name(project_dir) -multiple $mult \
		    -filetypes {{{Mesh Files} {*.uns}} {{All files} *}} \
		    -initialfile [file tail $file_name(domain)] -multiple $mult]
    }
    
    if {$type == "struct"} {
        set fm [tk_getOpenFile -initialdir $file_name(project_dir) \
		    -filetypes {{{Mesh Files} {*Multiblock}} {{All files} *}} \
		    -initialfile [file tail $file_name(domain)] -multiple $mult]
        return [multiblock_domain_list $fm]
    }

    if {$type == "mixed"} {
        set fm [tk_getOpenFile -initialdir $file_name(project_dir) \
		    -filetypes {{{Mesh Files} {*.uns *Multiblock}} {{All files} *}} \
		    -initialfile [file tail $file_name(domain)] -multiple $mult]
 	    if [regexp Multiblock $fm] {
           return [multiblock_domain_list $fm]
	    } else {
	        return [list $fm]
	    }
    }
    if {$type == "mixed_super"} {
        set fm [tk_getOpenFile -initialdir $file_name(project_dir) \
		    -filetypes {{{Mesh Files} {*Multiblock super_domain.*}} {{All files} *}} \
		    -initialfile [file tail $file_name(domain)] -multiple $mult]
 	    if [regexp Multiblock $fm] {
            return [multiblock_domain_list $fm]
	    } else {
	        return [list $fm]
	    }
    }

#    if {[info exists ai_environment] && $ai_environment && ($type == "unstruct")} {
#	return $file_name(domain)
#    }
    
    # We override this since all the output interface tcl files know about the
    # old format only.
    set dir $file_name(domains_dir)
    
    catch {unset domain_type}
    
    foreach i {struct super1 unstruct} {
	    set $i {}
	    set number($i) 0
    }
    set totalnumber 0
    
    set pref ""
    if {[info exists ai_environment] && $ai_environment && ($type == "struct")} {
	    set pref [file rootname [file tail $file_name(settings)]]
    }
    
    set files [glob -nocomplain $dir/${pref}*]
    
    if {$files == ""} {
	    warningmsg "No domains exist"
	    return {}
    }
    set files [lsort -command domain_cmp $files]
    
    set results [eval domain_types $files]
    
    set num 0
    foreach file $files dtype $results {
	# These are capitalized...
	    case $dtype in {
	        struct {
		        lappend struct [file tail $file]
		        set domain_type($file) struct
		        incr number(struct)
		        incr totalnumber
	        }
	        super1 {
		        lappend super1 [file tail $file]
		        set domain_type($file) super1
		        incr number(super1)
		        incr totalnumber
	        }
	        unstruct {
		        lappend unstruct [file tail $file]
		        set domain_type($file) unstruct
		        incr number(unstruct)
		        incr totalnumber
	        }
	        default {
		        # mess "Ignored domain $file\n" red
	        }
	    }
    }

    if !$totalnumber {
	    mess "No domains are available\n" red
	    return {}
    }
    
    foreach t [array names number] {
	    set sb($t) 0
	    if {($totalnumber > 20) && ($number($t) > 5)} {
	        set number($t) 5
	        set sb($t) 1
	    }
    }
    
    set dselect_done 0
    catch {destroy .dselect}
    catch {unset domain_selected_already}
    top_window .dselect 0 "Domain selection"
    top_geometry .dselect +300+300
    catch {wm transient .dselect [winfo toplevel .v]}
    
    label .dselect.title -font $bigfont -text "Domain selection"
    message .dselect.message -aspect 300 -text "$message\n\nThe box \
	    titled \"Selected domains\" contains the domains selected. \
	    To add a domain to this list, click in any of the other\
	    boxes.  To remove a domain from the selected list,\
	    click on it in the \"Selected domains\" box.\n"
    frame .dselect.left
    frame .dselect.right
    frame .dselect.buttons
    pack append .dselect \
	    .dselect.title {top fill} \
	    .dselect.message {top fill} \
	    .dselect.buttons {bottom fill} \
	    .dselect.left {left fill expand} \
	    .dselect.right {right fill expand}
    
    set numgiven 0
    if {(($type == "struct") || ($type == "super") || ($type == "mixed")) && \
	    ($struct != "")} {
	domain_make_domain_box .dselect.struct "Structured" $struct \
		$number(struct) $sb(struct)
	pack append .dselect.left .dselect.struct {top expand fill}
	incr numgiven $number(struct)
    }
    if {(($type == "struct") || ($type == "super") || ($type == "mixed")) && \
	    ($super1 != "")} {
	domain_make_domain_box .dselect.super1 "Superdomain" $super1 \
	    $number(super1) $sb(super1)
	pack append .dselect.left .dselect.super1 {top expand fill}
	incr numgiven $number(super1)
    }
    if {(($type == "unstruct") || ($type == "mixed")) && ($unstruct != "")} {
	domain_make_domain_box .dselect.unstruct "Unstructured" $unstruct \
		$number(unstruct) $sb(unstruct)
	pack append .dselect.left .dselect.unstruct {top expand fill}
	incr numgiven $number(unstruct)
    }
    
    if !$numgiven {
	mess "No domains of the required types are available\n"
	destroy .dselect
	return {}
    }
    
    button .dselect.done -text Done -command "set dselect_done 1"
    button .dselect.cancel -text Cancel -command "set dselect_done 0"
    pack append .dselect.buttons \
	.dselect.done {left fillx pady 10 expand} \
	.dselect.cancel {left fillx pady 10 expand}
    
    scrolled_listbox .dselect.selected "Selected domains" domain_unput "" 0 0 1
    pack append .dselect.right .dselect.selected {top expand fill}
    
    if {[info exists ai_environment] && $ai_environment && ($type == "unstruct") && \
	    ($file_name(domain) != "")} {
	domain_put [file tail $file_name(domain)]
    } else {
	if [info exists domain_history($type)] {
	    foreach dom $domain_history($type) {
		if ![info exists domain_type($dir/$dom)] continue
		domain_put $dom
	    }
	}
    }
    
    top_deiconify .dselect
    
    tkwait variable dselect_done
    
    set domains {}
    
    set list .dselect.selected.list
    if {$dselect_done && [winfo exists $list]} {
	set domain_history($type) {}
	set num [$list size]
	for {set i 0} {$i < $num} {incr i} {
	    set dom [$list get $i]
	    lappend domain_history($type) $dom
	    lappend domains $dir/$dom
	}
	destroy .dselect
    } else {
	destroy .dselect
	return ""
    }

    if $mult {
	return $domains
    } else {
	return [lindex $domains 0]
    }
}

proc domain_reset_history {} {
    global domain_history
    catch {unset domain_history}
}

proc domain_make_domain_box {w title stuff height sb} {
    scrolled_listbox $w $title domain_put domain_put_and_finish 1 $height $sb
    foreach i $stuff {
	$w.list insert end $i
    }
}

proc domain_put_and_finish {domain} {
    global dselect_done
    domain_put $domain
    set dselect_done 1
}

proc domain_put {domain} {
    global domain_selected_already
    
    if {$domain == ""} return
    
    if [info exists domain_selected_already($domain)] return
    .dselect.selected.list insert end $domain
    set domain_selected_already($domain) 1
}

proc domain_unput {domain} {
    global domain_selected_already
    
    if {$domain == ""} return
   
    set list .dselect.selected.list
    set num [$list size]
    for {set i 0} {$i < $num} {incr i} {
	if {[$list get $i] == $domain} {
	    $list delete $i
	    unset domain_selected_already($domain)
	    return
	}
    }
}

proc domain_put_all {list in} {
    set num [$list size]
    for {set i 0} {$i < $num} {incr i} {
	if $in {
	    domain_put [$list get $i]
	} else {
	    domain_unput [$list get $i]
	}
    }
}

proc scrolled_listbox {w label singlecom doublecom allnone height sb} {
    frame $w -relief raised -borderwidth 2
    scrollbar $w.scroll -relief sunken -borderwidth 2 \
	-command "$w.list yview" -orient v
    if $height {
	listbox $w.list -yscroll "$w.scroll set" -borderwidth 2 -relief sunken \
	    -width 20 -height $height
    } else {
	listbox $w.list -yscroll "$w.scroll set" -borderwidth 2 -relief sunken
    }
    label $w.label -text $label
    frame $w.buttons
    pack append $w \
	$w.label {top fill} \
	$w.buttons {bottom fill} \
	$w.list {left expand fill}
    if $sb {
	pack append $w $w.scroll {right fill}
    }

    if $allnone {
	button $w.all -text All -command "domain_put_all $w.list 1"
	button $w.none -text None -command "domain_put_all $w.list 0"
	pack append $w.buttons \
	    $w.all {left expand pady 5 padx 5} \
	    $w.none {left expand pady 5 padx 5}	    
    }
    
    if {$singlecom != ""} {
	bind $w.list <Button-1> "$singlecom \[%W get \[%W nearest %y\]\]"
    }
    if {$doublecom != ""} {
	bind $w.list <Double-Button-1> "$doublecom \[%W get \[%W nearest %y\]\]"
    }
}

proc domain_cmp {dom1 dom2} {
    set n1 [file extension $dom1]
    set n2 [file extension $dom2]
    if {![regexp {\.([0-9]+)$} $n1 dummy num1] ||
	![regexp {\.([0-9]+)$} $n2 dummy num2] ||
	([file root $dom1] != [file root $dom2])} {
	    if {$dom1 == $dom2} { return 0 } \
		elseif {$dom1 > $dom2} { return 1 } \
		else { return -1 }
	}
    return [expr {$num1 - $num2}]
}

