
# This code creates the user interface for the mesh editor.
#
# Copyright (c) 1997 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com


proc pcube_run {} {
    global env file_name
    global is_win

    if {[info exists file_name(project_dir)] && ($file_name(project_dir) == "")} {
      mess "Unable to run pcube - no project selected.\n" red
      return
    }
    
    set part [ddn_select_part project 1]
    if {$part == ""} return
    set selected [lindex $part 1]
    set filename [lindex $part 2]
    set db $file_name(parts_dir)
    if {[file pathtype $db] == "relative"} { set db [pwd]/$db }
    array set oenv [array get env]
    
    set boco_file $file_name(project_dir)/boco_info
    set topo_file $file_name(project_dir)/topo_info
    set mulcad_file $file_name(project_dir)/topo_mulcad_out
    set clust_file $file_name(project_dir)/clust
    set topo2_file $file_name(project_dir)/topo
    if [file exists $boco_file] {set before_boco [file mtime $boco_file]}
    if [file exists $topo_file] {set before_topo [file mtime $topo_file]}
    if [file exists $mulcad_file] {set before_mulcad [file mtime $mulcad_file]}
    if [file exists $clust_file] {set before_clust [file mtime $clust_file]}
    if [file exists $topo2_file] {set before_topo2 [file mtime $topo2_file]}
   
    if !$is_win {
	set my_workstation [exec /bin/uname]
	if {[string match "IRIX*" $my_workstation]} {
	    set tcl_level tcl7.0
	    set tk_level  tk3.3
	} else {
	    set tcl_level tcl7.0
	    set tk_level  tk3.3
	}
	
	if {![file exists $file_name(topology_dir)]} {
	    exec /bin/mkdir $file_name(topology_dir)
	}
	
	# provide vendor name for P-Cube startup - 8.5.94 - GWH
	set env(ICEM_CDC) 1 

        # Source an open.tcl initialization file if present.
        if {[file exists $file_name(project_dir)/open.tcl]} {
	    infomsg "Processing initialization commands in $file_name(project_dir)/open.tcl."
	    source $file_name(project_dir)/open.tcl
        } else {
	    if {[info exists env(PCUBE_USER_TOL)]} {
	        if {$env(PCUBE_USER_TOL) != 1.0} {
		    set env(PCUBE_USER_TOL) 1.0
		    infomsg "PCUBE_USER_TOL reset to $env(PCUBE_USER_TOL)."
	        }
	    }
	    if {[info exists env(PCUBE_CAD_TOL)]} {
	        if {$env(PCUBE_CAD_TOL) != 0.001} {
		    set env(PCUBE_CAD_TOL) 0.001
		    infomsg "PCUBE_CAD_TOL reset to $env(PCUBE_CAD_TOL)."
	        }
	    }
        }
	if [info exists env(LD_LIBRARY_PATH)] {
	    set env(LD_LIBRARY_PATH) $env(LD_LIBRARY_PATH):$env(ICEM_ACN)/ddn/shared_lib
	} else {
	    set env(LD_LIBRARY_PATH) $env(ICEM_ACN)/ddn/shared_lib
	}
	set env(TCL_LIBRARY) $env(ICEM_ACN)/ddn/fbd/tcl/tcl/$tcl_level/library
	set env(TK_LIBRARY) $env(ICEM_ACN)/ddn/fbd/tcl/tcl/$tk_level/library
	set command "db=$db [ddn_options]"
	set pn "pn=[ddn_partname $selected]"
	ic_run_application_command . bin pcube "$command \"$pn\""
    
    } else {
	
	set tcl_level tcl7.5
	set tk_level tk4.1
	set p1 [pwd]
	
	infomsg "TCL_LIBRARY $env(ICEM_ACN)/ddn/fbd/tcl/tcl/$tcl_level"
	infomsg "TK_LIBRARY $env(ICEM_ACN)/ddn/fbd/tcl/tcl/$tk_level"
	
	set env(TCL_LIBRARY) $env(ICEM_ACN)/ddn/fbd/tcl/tcl/$tcl_level
	set env(TK_LIBRARY) $env(ICEM_ACN)/ddn/fbd/tcl/tcl/$tk_level
	
	if {![file exists $file_name(topology_dir) ]} {
	    exec md $file_name(topology_dir)
	}
	
	set command "$env(ICEM_ACN)/bin/pcube.exe "
	set pwd [pwd]
	catch {exec del out.pcube}
	wm iconify .v
	set ww [ application_wait ]
	update
	
	if ![info exists env(WIN95)] {
	    # Windows NT
	    set old_path "$env(Path)"
	    set env(Path) " "
	    set env(Path) "$env(ICEM_ACN)/ddn/fbd/tcl/lib;/Netscape/Navigator/Program"
	    catch {eval exec $command db=$db pn=$selected [ddn_options] > out.pcube} res
	    set env(Path) "$old_path"
	} else {
	    # Windows 95
	    set old_path "$env(PATH)"
	    set env(PATH) " "
	    set env(PATH) "$env(ICEM_ACN)/ddn/fbd/tcl/lib;/Netscape/Navigator/Program"
	    catch {eval exec $command db=$db pn=$selected [ddn_options] > out.pcube} res
	    set env(PATH) "$old_path"
	}
	
	if [file exists out.pcube] {
	    set res [exec cat.exe out.pcube]
	    infomsg "$res" 
	}
	top_destroy $ww
	wm deiconify .v
	update
	infomsg "DONE"
    }
    if [file exists $boco_file] {set after_boco [file mtime $boco_file]}
    if [file exists $topo_file] {set after_topo [file mtime $topo_file]}
    if [file exists $mulcad_file] {set after_mulcad [file mtime $mulcad_file]}
    if [file exists $clust_file] {set after_clust [file mtime $clust_file]}
    if [file exists $topo2_file] {set after_topo2 [file mtime $topo2_file]}		
    if {([info exists before_boco] && [info exists after_boco] && \
	($after_boco > $before_boco)) || (![info exists before_boco] && \
	[info exists after_boco])} {
	ic_cp $boco_file $file_name(mesh_dir)
	rm_tmp_file $boco_file
    }
    if {([info exists before_topo] && [info exists after_topo] && \
	($after_topo > $before_topo)) || (![info exists before_topo] && \
	[info exists after_topo])} {
	ic_cp $topo_file $file_name(mesh_dir)
	rm_tmp_file $topo_file
    }
    if {([info exists before_mulcad] && [info exists after_mulcad] && \
	($after_mulcad > $before_mulcad)) || (![info exists before_mulcad] && \
	[info exists after_mulcad])} {
	ic_cp $mulcad_file $file_name(mesh_dir)
	rm_tmp_file $mulcad_file
    }
    if {([info exists before_clust] && [info exists after_clust] && \
        ($after_clust > $before_clust)) || (![info exists before_clust] && \
	[info exists after_clust])} {
	ic_cp $clust_file $file_name(mesh_dir)
	rm_tmp_file $clust_file
    }
    if {([info exists before_topo2] && [info exists after_topo2] && \
	($after_topo2 > $before_topo2)) || (![info exists before_topo2] && \
	[info exists after_topo2])} {
	ic_cp $topo2_file $file_name(topology_dir)
	rm_tmp_file $topo2_file
    }
       
    array set env [array get oenv]
    update_partlist - 1
}

proc pcube_set_user_tol {} {
    global env file_name
    
    if {[info exists file_name(project_dir)] && ($file_name(project_dir) == "")} {
      mess "Unable to set pcube tolerance - no project selected.\n" red
      return
    }
    if {[info exists env(PCUBE_USER_TOL)]} {
        set default $env(PCUBE_USER_TOL)
    } else {
        set default 1.0
    }
    
    set user_tol [get_text "P-Cube User Tolerance" $default]
    
    if {"$user_tol" != "*cancel*"} {
        # Create or replace open.tcl initialization file.
        if {[info exists env(PCUBE_CAD_TOL)]} {
	    set default $env(PCUBE_CAD_TOL)
	} else {
	    set default 0.001
	}
	set cad_tol [get_text "P-Cube CAD Tolerance" $default]
	if {"$cad_tol" != "*cancel*"} {
            if {![file exists $file_name(project_dir)/open.tcl]} {
                infomsg "Storing this setting in $file_name(project_dir)/open.tcl."
            } else {
                infomsg "Updating this setting in $file_name(project_dir)/open.tcl."
            }
            exec echo "set env(PCUBE_USER_TOL) $user_tol" > $file_name(project_dir)/open.tcl
            exec echo infomsg \"PCUBE_USER_TOL set to \$env(PCUBE_USER_TOL).\" \
	        >> $file_name(project_dir)/open.tcl
	    exec echo "set env(PCUBE_CAD_TOL) $cad_tol" >> $file_name(project_dir)/open.tcl
	    exec echo infomsg \"PCUBE_CAD_TOL set to \$env(PCUBE_CAD_TOL).\" \
	        >> $file_name(project_dir)/open.tcl
            source $file_name(project_dir)/open.tcl
        }
    }
}

# Add entry to Utility menu for deleting undo files.
proc pcube_delete_undos {} {
    global file_name

    if {[info exists file_name(project_dir)] && ($file_name(project_dir) == "")} {
      mess "Unable to delete pcube undo files - no project selected.\n" red
      return
    }

    set sel [select_files "$file_name(parts_dir)/cubdb/checkpoint/*.undo.*" "Select undo files to delete." files]
    if {$sel == {}} {
	errormsg "No undo files selected."
	return
    }
    foreach i $sel {
      rm_tmp_file $i
    }
}

proc get_text { prompt { def "" } } {
	global get_text_val
	set get_text_val $def
	catch { form_kill .get_text }
	set d [form_init .get_text "$prompt" mesh]
	if { $d != "" } {
		form_label $d.label $prompt { 1 1 } 
		form_entry $d.text get_text_val text { 1 2 } 
		form_finish +300+300
	}
	form_wait .get_text
	return $get_text_val
	
}

proc select_files {filespec message {type ""}} {
  global env fselect_done

  set specfiles [glob -nocomplain $filespec]
  if {$specfiles == ""} {
    errormsg "No files matching \"$filespec\"."
    return {}
  }
  set files {}
  foreach file $specfiles {
    if {($type == "") || \
        ($type == "directories" && [file isdirectory $file]) || \
        ($type == "files" && [file isfile $file])} {
      lappend files [file tail $file]
    }
  }
  set files [lsort $files]

  set fselect_done 0
  catch {top_destroy .fselect}
  top_window .fselect 0 "Select $type."
  top_geometry .fselect +300+300
  if {$type == ""} {
    set type "files"
  } else {
    set title $type
  }
  message .fselect.message -aspect 600 -text "$message"

  frame .fselect.frame
  frame .fselect.buttons
  pack append .fselect \
        .fselect.message {top fill} \
        .fselect.buttons {bottom fill} \
        .fselect.frame {left right fill expand}

  multiselect_listbox .fselect.files "$filespec" {} {} 1 10 1
  foreach i $files {
    .fselect.files.list insert end $i
  }
  pack append .fselect.frame .fselect.files {top expand fill}

  # Restore standard bindings from listbox.tcl.
  # (ICEMAN replaced them for the Listbox class.)
  bind .fselect.files.list <B1-Motion> {
      set tkPriv(x) %x
      set tkPriv(y) %y
      tkListboxMotion %W [%W index @%x,%y]
  }

  button .fselect.done -text Done -command "set fselect_done 1"
  button .fselect.cancel -text Cancel -command "set fselect_done 0"
  pack append .fselect.buttons \
        .fselect.done {left fillx pady 10 expand} \
        .fselect.cancel {left fillx pady 10 expand}

  top_deiconify .fselect

  tkwait variable fselect_done

  set files {}
  if $fselect_done {
      set filepath [file dirname $filespec]
      set selections [.fselect.files.list curselection]
      foreach i $selections {
          set file [.fselect.files.list get $i]
          lappend files $filepath/$file
      }
  }

  top_destroy .fselect
  return $files
}

proc multiselect_listbox {w label singlecom doublecom allnone height sb} {
  global scrollbar_inactivecolor scrollbar_activecolor

  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 -selectmode extended
  } else {
    listbox $w.list -yscroll "$w.scroll set" -borderwidth 2 -relief sunken \
            -selectmode extended
  }
  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 "tkListboxSelectAll $w.list"
    button $w.none -text None -command "listbox_select_none $w.list"
    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 listbox_select_all {list} {
    $list select from 0
    $list select to [expr [$list size] - 1]
}

proc listbox_select_none {list} {
    $list selection clear 0 end
}
