
# 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

# Stuff to control the layout of the manager window.

set active 1

proc disable_interface {} {global active; set active 0}
proc enable_interface {} {global active; set active 1; focus .}

foreach i [winfo child .] {catch {destroy $i}}

proc unimp {} {warningmsg "This command is not yet implemented."}

proc set_title {} {
    global icem_fluent icem_powermesh
    if $icem_fluent {
	wm title . "GeoMesh @ [cmd_uname_n]: [pwd]"
    } elseif $icem_powermesh {
        wm title . "ICEM CFD Powermesh @ [cmd_uname_n]: [pwd]"
    } else {
	wm title . "ICEM CFD @ [cmd_uname_n]: [pwd]"
    }
}
set_title

frame .menu -relief raised -borderwidth 1

frame .query -relief raised -borderwidth 1
query .query

frame .diag -relief raised -borderwidth 0
scrollbar .diag.yscroll -relief sunken -command ".diag.text yview" \
    -orient v
text .diag.text -relief raised -yscroll ".diag.yscroll set" \
    -width 80 -height 10 -exportselection 1 -wrap word -padx 4 -pady 4 \
    -borderwidth 1
pack append .diag .diag.yscroll {right fill} \
    .diag.text {top expand fill}

# Let's fix listboxes to have only single selections.
bind Listbox <B1-Motion> {}
bind Listbox <Shift-1> {}
bind Listbox <Shift-B1-Motion> {}

frame .parts -relief raised -borderwidth 0
frame .parts.b -relief raised -borderwidth 1
frame .parts.g -relief raised -borderwidth 0
frame .parts.l -relief raised -borderwidth 0
frame .parts.i -relief raised -borderwidth 0
frame .parts.v -relief raised -borderwidth 0
frame .parts.p -relief raised -borderwidth 0
frame .parts.d -relief raised -borderwidth 0
frame .parts.s -relief raised -borderwidth 0
frame .parts.k -relief raised -borderwidth 0

# -------------

label .parts.g.title -font $bigfont -relief raised -borderwidth 1 \
    -text $gui_name(geopart) -pady 5 -anchor nw
scrollbar .parts.g.scroll -relief sunken -command ".parts.g.list yview"
listbox .parts.g.list  -yscroll ".parts.g.scroll set" \
    -exportselection 0 -width 20 -height 8 \
    -borderwidth 1 -relief raised
pack append .parts.g .parts.g.title {top fill} \
    .parts.g.scroll {right filly frame w} \
    .parts.g.list {left expand fill frame e}

# -------------

# (MULCAD, Global)
label .parts.l.title -font $bigfont -relief raised -borderwidth 1 \
    -text $gui_name(meshpart) -pady 5 -anchor nw
scrollbar .parts.l.scroll -relief sunken -command ".parts.l.list yview"
listbox .parts.l.list  -yscroll ".parts.l.scroll set" \
    -exportselection 0 -width 20 -height 8 \
    -borderwidth 1 -relief raised
pack append .parts.l .parts.l.title {top fill} \
    .parts.l.scroll {right filly frame w} \
    .parts.l.list {left expand fill frame e}

# -------------

label .parts.i.title -font $bigfont -relief raised -borderwidth 1 \
    -text "IGES Parts" -pady 5 -anchor nw
scrollbar .parts.i.scroll -relief sunken -command ".parts.i.list yview"
listbox .parts.i.list  -yscroll ".parts.i.scroll set" \
    -exportselection 0 -width 20 -height 8 \
    -borderwidth 1 -relief raised
pack append .parts.i .parts.i.title {top fill} \
    .parts.i.scroll {right filly frame w} \
    .parts.i.list {left expand fill frame e}

# -------------

label .parts.d.title -font $bigfont -relief raised -borderwidth 1 \
    -text "DXF Parts" -pady 5 -anchor nw
scrollbar .parts.d.scroll -relief sunken -command ".parts.d.list yview"
listbox .parts.d.list  -yscroll ".parts.d.scroll set" \
    -exportselection 0 -width 20 -height 8 \
    -borderwidth 1 -relief raised
pack append .parts.d .parts.d.title {top fill} \
    .parts.d.scroll {right filly frame w} \
    .parts.d.list {left expand fill frame e}

# -------------

label .parts.v.title -font $bigfont -relief raised -borderwidth 1 \
    -text "TVDA Parts" -pady 5 -anchor nw
scrollbar .parts.v.scroll -relief sunken -command ".parts.v.list yview"
listbox .parts.v.list  -yscroll ".parts.v.scroll set" \
    -exportselection 0 -width 20 -height 8 \
    -borderwidth 1 -relief raised
pack append .parts.v .parts.v.title {top fill} \
    .parts.v.scroll {right filly frame w} \
    .parts.v.list {left expand fill frame e}

# -------------

label .parts.s.title -font $bigfont -relief raised -borderwidth 1 \
    -text "SET Parts" -pady 5 -anchor nw
scrollbar .parts.s.scroll -relief sunken -command ".parts.s.list yview"
listbox .parts.s.list  -yscroll ".parts.s.scroll set" \
    -exportselection 0 -width 20 -height 8 \
    -borderwidth 1 -relief raised
pack append .parts.s .parts.s.title {top fill} \
    .parts.s.scroll {right filly frame w} \
    .parts.s.list {left expand fill frame e}

# -------------

label .parts.p.title -font $bigfont -relief raised -borderwidth 1 \
    -text "PreBFC Parts" -pady 5 -anchor nw
scrollbar .parts.p.scroll -relief sunken -command ".parts.p.list yview"
listbox .parts.p.list  -yscroll ".parts.p.scroll set" \
    -exportselection 0 -width 20 -height 8 \
    -borderwidth 1 -relief raised
pack append .parts.p .parts.p.title {top fill} \
    .parts.p.scroll {right filly frame w} \
    .parts.p.list {left expand fill frame e}

# -------------

label .parts.k.title -font $bigfont -relief raised -borderwidth 1 \
    -text "Comak Parts" -pady 5 -anchor nw
scrollbar .parts.k.scroll -relief sunken -command ".parts.k.list yview"
listbox .parts.k.list  -yscroll ".parts.k.scroll set" \
    -exportselection 0 -width 20 -height 8 \
    -borderwidth 1 -relief raised
pack append .parts.k .parts.k.title {top fill} \
    .parts.k.scroll {right filly frame w} \
    .parts.k.list {left expand fill frame e}

# -------------

pack append .parts .parts.b {top fill} .parts.g {left fill expand} \
    .parts.l {right fill expand}

proc put_iges {} {
    pack unpack .parts.l
    pack append .parts .parts.i {right fill expand}
    .parts.i configure -width 20 -height 8
}

proc put_dxf {} {
    pack unpack .parts.l
    pack append .parts .parts.d {right fill expand}
    .parts.d configure -width 20 -height 8
}

proc put_tvda {} {
    pack unpack .parts.l
    pack append .parts .parts.v {right fill expand} 
    .parts.v configure -width 20 -height 8
}

proc put_set {} {
    pack unpack .parts.l
    pack append .parts .parts.s {right fill expand} 
    .parts.s configure -width 20 -height 8
}

proc put_prebfc {} {
    pack unpack .parts.l
    pack append .parts .parts.p {right fill expand}
    .parts.p configure -width 20 -height 8
}

proc put_comak {} {
    pack unpack .parts.l
    pack append .parts .parts.k {right fill expand}
    .parts.k configure -width 20 -height 8
}

proc unput_iges {} {
    pack unpack .parts.i
    pack append .parts .parts.l {right fill expand} 
    .parts.i configure -width 20 -height 8
}

proc unput_dxf {} {
    pack unpack .parts.d
    pack append .parts .parts.l {right fill expand} 
    .parts.d configure -width 20 -height 8
}

proc unput_tvda {} {
    pack unpack .parts.v
    pack append .parts .parts.l {right fill expand} 
    .parts.l configure -width 20 -height 8
}

proc unput_set {} {
    pack unpack .parts.s
    pack append .parts .parts.l {right fill expand} 
    .parts.l configure -width 20 -height 8
}

proc unput_prebfc {} {
    pack unpack .parts.p
    pack append .parts .parts.l {right fill expand}
    .parts.l configure -width 20 -height 8
}

proc unput_comak {} {
    pack unpack .parts.k
    pack append .parts .parts.l {right fill expand}
    .parts.l configure -width 20 -height 8
}

frame .labels -relief raised -borderwidth 1
frame .labels.l -relief flat -borderwidth 0
frame .labels.r -relief flat -borderwidth 0
pack append .labels \
    .labels.l {left expand fill} \
    .labels.r {right expand fill}

frame .labels.l.names -relief raised -borderwidth 0
label .labels.l.names.conf  -relief flat -borderwidth 0 -anchor nw \
    -text "$gui_name(Conf): "
label .labels.l.names.geo  -relief flat -borderwidth 0 -anchor nw \
    -text "$gui_name(Geo): "
label .labels.l.names.part  -relief flat -borderwidth 0 -anchor nw \
    -text "Part: "
label .labels.l.names.sheet  -relief flat -borderwidth 0 -anchor nw\
    -text "Sheet: "
pack append .labels.l.names \
    .labels.l.names.conf {top fill} \
    .labels.l.names.geo {top fill} \
    .labels.l.names.part {top fill} \
    .labels.l.names.sheet {top fill}

frame .labels.l.vals -relief raised -borderwidth 0
label .labels.l.vals.conf  -relief flat -borderwidth 0 -anchor nw \
    -text "- none -"
label .labels.l.vals.geo  -relief flat -borderwidth 0 -anchor nw \
    -text "- none -"
label .labels.l.vals.part  -relief flat -borderwidth 0 -anchor nw \
    -text "- none -"
label .labels.l.vals.sheet  -relief flat -borderwidth 0 -anchor nw\
    -text "- none -"
pack append .labels.l.vals \
    .labels.l.vals.conf {top fill} \
    .labels.l.vals.geo {top fill} \
    .labels.l.vals.part {top fill} \
    .labels.l.vals.sheet {top fill}

pack append .labels.l \
    .labels.l.names {left fill} \
    .labels.l.vals {right expand fill}

frame .labels.r.names -relief raised -borderwidth 0
label .labels.r.names.solver  -relief flat -borderwidth 0 \
    -anchor nw -text "Translator: "

frame .labels.r.vals -relief raised -borderwidth 0
label .labels.r.vals.solver  -relief flat -borderwidth 0 \
    -anchor nw -text "- none -"

if { !$icem_fluent } {
  if { !$icem_powermesh } {
    pack append .labels.r.names \
	.labels.r.names.solver {top fill}
    pack append .labels.r.vals \
	.labels.r.vals.solver {top fill}
  } else {
    pack append .labels.r.names \
        .labels.r.names.solver {top fill} 
    pack append .labels.r.vals \
        .labels.r.vals.solver {top fill} 
  }
}

pack append .labels.r \
    .labels.r.names {left fill} \
    .labels.r.vals {right expand fill}

pack append . .menu {top fill} \
    .labels {top fill} \
    .query {top fill} \
    .parts {top fill expand} \
    .diag {top fill}


proc make_menu {name label} {
    menubutton .menu.$name -text $label -menu .menu.$name.m
    menu .menu.$name.m
    
    pack append .menu .menu.$name {left fill padx 4}
}

proc make_entry {name label accel progname func {feats ""}} {
    global features_available last_was_line
    global icem_fluent icem_powermesh icem_stamp

    if {$feats != ""} {
	set ok 0; set nok 0
	foreach f $feats {
	    switch $f {
		high {
		    if {$icem_fluent || $icem_powermesh} return
		}
		low {
		    if {!$icem_fluent && !$icem_powermesh} return
		}
		fluent {
		    if !$icem_fluent return
		}
		nfluent {
		    if $icem_fluent return
		}
		power {
		    if !$icem_powermesh return
		}
		npower {
		    if !$icem_powermesh return
		}
		stamp {
		    if !$icem_stamp return
		}
		default {
		    if {![info exists features_available] || \
			    [info exists features_available($f)]} {
			set ok 1
			#puts "feature $f is available for $name"
		    } else {
			set nok 1
			#puts "feature $f is NOT available for $name"
		    }
		}
	    }
	}
	if {!$ok && $nok} return
    }
    
    .menu.$name.m add command -label $label -command "if \$active \{$func\}"
    set num [.menu.$name.m index last]
    
    if {$accel != ""} {
	.menu.$name.m entryconfigure $num -accel "    $accel"
	bind . $accel "if \$active \{$func\}"
    }
    set last_was_line 0
}

proc make_line {name} {
    global last_was_line
    
    if !$last_was_line {
	.menu.$name.m add separator
        set last_was_line 1
    }
}

# Don't put this stuff in yet.
# make_menu custom "Custom" {}
# if [file exists icemcfd-custom.tcl] {source icemcfd-custom.tcl}
# source $scriptlib/sample-custom.tcl

if !$icem_fluent {
    label .menu.cdlogo -borderwidth 5
    label .menu.cdlogotext -borderwidth 5 -text "ICEM CFD"
    foreach item { .menu.cdlogo .menu.cdlogotext } {
	bind $item <ButtonRelease> {
	    infomsg "This is $whoami."
	    infomsg "Version of $install_date"
	    infomsg "Copyright (c) 1993 - 1999 ICEM CFD Engineering"
	    infomsg "Have a nice day."
	}
    }

    #.menu.cdlogo configure -bitmap @$scriptlib/icemlogo.xbm
    .menu.cdlogo configure -text ICEM
    if !$black_and_white {

	    set logo "$scriptlib/med-vsmlogo-bits.xbm \#0000ff \
	    $scriptlib/med-vsmlogo-mask.xbm \#547eff"
            set im [image create bitmap -file [lindex $logo 0] \
                        -foreground [lindex $logo 1] \
                        -maskfile [lindex $logo 2] \
                        -background [lindex $logo 3]]
	    .menu.cdlogo configure -image $im

    }
    
} else {
    button .menu.cdlogo -text GeoMesh -relief raised \
	    -borderwidth 5 -command {
	infomsg "This is the $whoami for Fluent Inc."
	infomsg "Copyright (c) 1993 - 1999 ICEM CFD Engineering"
	infomsg "GeoMesh is a registered trademark of Fluent Incorporated."
	infomsg "Have a nice day."
    }
}

if !$icem_fluent {
    pack append .menu \
    	.menu.cdlogotext {right fill padx 4} \
    	.menu.cdlogo {right fill padx 4}
} else {
    pack append .menu \
    	.menu.cdlogo {right fill padx 4}
}

button .parts.b.copy  -text "Copy part" \
    -command "if {\$active} {part_copy}"
button .parts.b.move  -text "Move part" \
    -command "if {\$active} {part_move}"
button .parts.b.rm  -text "Delete part" \
    -command "if {\$active} {part_delete}"
button .parts.b.new -text "Unselect current part" \
    -command "if {\$active} {unselect_part}"

if {!$icem_fluent && !$icem_powermesh} {
    checkbutton .parts.b.objs -text "Comak parts" \
	    -variable comak_objects \
	    -command {
	if $comak_objects {
	    update_partlist - 1; put_comak
	} else {
	    unput_comak
	}
    }
}

pack append .parts.b \
    .parts.b.copy {left fill padx 4} \
    .parts.b.move {left fill padx 4} \
    .parts.b.rm {left fill padx 4} \
    .parts.b.new {left fill padx 4}

if {!$icem_fluent && !$icem_powermesh} {
    pack append .parts.b .parts.b.objs {left fill padx 4}
}

#bind . c {if $active {part_copy}}
#bind . m {if $active {part_move}}
#bind . d {if $active {part_delete}}
#bind . u {if $active {unselect_part}}

set waiting_for_part 0

bind .parts.g.list <Button-1> {
    global global_parts active waiting_for_part
    if $active {
	select_part %W %y $global_parts
    } else {if $waiting_for_part {
	select_part %W %y $global_parts
	set waiting_for_part 0
	query_waitvar_done .query *okay*
    }}
}

bind .parts.l.list <Button-1> {
    global local_parts active waiting_for_part
    if $active {
	select_part %W %y $local_parts
    } {if $waiting_for_part {
	select_part %W %y $local_parts
	set waiting_for_part 0
	query_waitvar_done .query *okay*
    }}
}

bind .parts.k.list <Button-1> {
    global comak_parts active waiting_for_part
    if $active {
	select_part %W %y $comak_parts
    } {if $waiting_for_part {
	select_part %W %y $comak_parts
	set waiting_for_part 0
	query_waitvar_done .query *okay*
    }}
}

bind .parts.g.list <Double-Button-1> {
    global global_parts active
    if $active {
	select_part %W %y $global_parts
	app_ddn
    }
}

bind .parts.i.list <Button-1> {
    set name [%W get  [%W nearest %y]]
    query_waitvar_done .query $name
}

bind .parts.d.list <Button-1> {
    set name [%W get  [%W nearest %y]]
    query_waitvar_done .query $name
}

bind .parts.v.list <Button-1> {
    set name [%W get [%W nearest %y]]
    query_waitvar_done .query $name
}

bind .parts.s.list <Button-1> {
    set name [%W get [%W nearest %y]]
    query_waitvar_done .query $name
}

bind .parts.p.list <Button-1> {
    set name [%W get [%W nearest %y]]
    query_waitvar_done .query $name
}



bind . <Lock-Key> {warningmsg "The shift-lock key is on -- please release it."}
foreach i {1 2 3 4 5} {
    bind . <Mod$i-Key> {warningmsg "An unrecognised modifier key was used -- \
perhaps the the \"Num Lock\" key is down."}
}

proc update_label {value which lorr} {
    if {$value == ""} \
	{.labels.$lorr.vals.$which configure -text "- none -"} \
	{.labels.$lorr.vals.$which configure -text $value}
}

proc nothing {args} {}

trace variable confname w {update_label $confname conf l; nothing}
trace variable geoname w {update_label $geoname geo l; nothing}
trace variable partname w {update_label $partname part l; nothing}
trace variable sheetname w {update_label $sheetname sheet l; nothing}
trace variable solver w {update_label $solver solver r; nothing}
