
# 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:  Jari Spaete jari.spaete@ansys.com
# $Revision: 749132 $ on $Date: 2014-07-02 09:51:09 -0400 (Wed, 02 Jul 2014) $

# This file contains special comments of the form ## html ... which are
# used to automatically generate documentation

## <h3>Utility functions</h3>

## Print the given message to the message window or the standard output if
## med is being run in batch mode.  There may be 1 or 2 arguments.  The first
## is the string and the second if given is the color to print the message
## in.

proc ic_mess {args} {
    # Need to translate from embedded \n to real newline.  Why this needs 4 \ I don't know
    regsub -all \\\\n $args \n args
    return [eval mess $args]
}

proc ic_mess_skipped {args} {
    
    return [ic_mess "Skipped: $args\n" darkgreen]
}

proc status_message {args} {
    # debug version; uses gui_interp proc status_message
  		vic_eval status_message $args 
  		vic_eval update idletasks 
}

## Print the error log <b>res</b> to the screen in red. If there are more
## than 64 lines, they will all be saved to a temporary file such as the
## default /tmp/ERROR_LOG0.tmp

proc ic_print_error_log {res} {
    set np 0
    foreach r $res {
        ic_mess "\t$r"
        incr np
        if {$np > 64} {
            set file [cmd_tempfile ERROR_LOG .tmp]
            set fp [open $file w]
            puts $fp $res
            close $fp
            ic_mess "[expr [llength $res] - 64] more messages - not printed\n" red
            ic_mess "All messages saved to file $file\n" red
            break
        }
    }
}

## Create a file with some stuff in it

proc ic_write_file {name text} {
    # there might be \n's in the text (not a control-R, but 2 characters)
    regsub -all \\\\n $text \n text
    set fp [open $name w]
    puts -nonewline $fp $text
    close $fp
}

## Quit med and save the current project settings.

proc ic_quit {} {
    global batch_mode

    if $batch_mode {
	ic_undo_stop
	exit 0
    }
    file_quit 0
}

## Call the <b>tk_dialog</b> utility function.

proc ic_dialog {args} {
    return [eval tk_dialog $args]
}

## Query the user to input a string value.

proc ic_question {args} {
    return [eval question_box $args]
}

## Present a dialog with a message and one button that says Ok that just
## dismisses the dialog.

proc ic_confirm {mess} {
    global batch_mode

    if !$batch_mode {
        tk_dialog .lpopup_message Message $mess "" -1 Ok
    }
}

## Present a dialog with a message and 2 buttons, which say Yes or No.
## Returns 1 or 0 depending on which is pressed.

proc ic_yes_or_no {mess} {
    return [expr ![tk_dialog .lpopup_message Query $mess "" -1 Yes No]]
}

## Present a dialog with a message and multiple buttons with the given
## labels.  Returns the ordinal of the one that the user presses.

proc ic_multiple_choice {mess args} {
    return [eval tk_dialog .lpopup_message Query \{$mess\} \{\} -1 $args]
}

## Same as <em>ic_multiple_choice</em> but <em>default</em> gives the index of the
## button that is to be the default button for the dialog. If less than zero then
## there won't be any default button.

proc ic_multiple_choice_default {default mess args} {
    return [eval tk_dialog .lpopup_message Query \{$mess\} \{\} $default $args]
}

## Wait for <em>ms</em> milliseconds, processing regular Tk events, and then
## return.

proc ic_pause {ms} {
    global ic_pause_variable
    set ic_pause_variable 0
    after [expr int($ms)] set ic_pause_variable 1
    vwait ic_pause_variable
}

## Check for batch mode.

proc ic_batch_mode {} {
    global batch_mode

    if $batch_mode {return 1}
    return 0
}
set dont_record_command(ic_batch_mode) 1

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

## Run some external application.

proc ic_run_application_command {dir progdir progname arguments} {
    set olddir [pwd]
    if [catch {cd $dir} res] {
	mess "Can't change directory: $res\n"
    }
    if {$progdir == ""} {
	set app $progname
    } else {
	set app [get_program $progdir $progname]
    }
    catch {
	run_application_command "\"$app\" $arguments"
    }
    cd $olddir
}

## Run some external application in batch mode

proc ic_run_application_batch {dir progdir progname envname arguments \
		{ logfile "" } } {
    set olddir [pwd]
    if [catch {cd $dir} res] {
	    mess "Can't change directory: $res\n"
    }
    if [catch {
	    run_application_batch $progdir/$progname $envname $arguments $logfile
    } res] {
	    user_error $res
    }
    cd $olddir
    return $res
}

## Run some external application, given the full path

proc ic_run_application_exec {dir progdir progname arguments } {
    set olddir [pwd]
    if [catch {cd $dir} res] {
        mess "Can't change directory: $res\n"
    }
    if {$progdir == ""} {
        set app $progname
    } else {
        set app [get_program $progdir $progname]
    }
    if [catch {
        run_application_exec "\"$app\" $arguments"
    } res] {
        user_error $res
    }
    cd $olddir
    return $res
}

## A simplified version of this.

proc ic_exec {args} {
    global env batch_mode

    if [catch {eval exec $args} res] {
	    user_error $res
    } else {
        if {!$batch_mode && [ic_wb2_is_running] && [string match "*output-interfaces*" "$args"]} {
            if ![ic_wb2_input_parameter_mode] {
                ic_socket_send_to_wb2 "wb_status 7"
            }
        }
    }
    return $res
}

## Run an application ...

proc ic_run_application_direct {dir progdir progname envname arguments} {
    global env black_and_white use_xwindows is_win
    
    if $black_and_white { set bw -bw } else { set bw "" }
    switch $use_xwindows {
	0 { set x "" }
	1 { set x -x }
	2 { set x -ogl }
    }
    
    #set env($rootenv) $env(ICEM_ACN) JKA: in favor of:
    set env($envname) $env(ICEM_ACN)
    
    set command "$bw $x $dir $arguments"
    ic_run_application_command . $progdir $progname $command
}

set dont_record_command(ic_unique_id) 1

proc ic_unique_id { } {
	global ic_fp_unique_num
	if { ! [ info exists ic_fp_unique_num ] } { 
		set ic_fp_unique_num 1
	}
	set fp_ext ic_file$ic_fp_unique_num
	incr ic_fp_unique_num
	return $fp_ext
}

proc ic_open { filename read_write fp_ext } {
	global ic_file_descriptors 
	
	mess "ic_open $filename $read_write $fp_ext\n"
	set fp [ open $filename $read_write ]
	if [ info exists ic_file_descriptors($fp_ext) ] {
		error "that file descriptor is already in use"
	}
	set ic_file_descriptors($fp_ext) $fp
	return $fp_ext
}


proc ic_puts { fp_ext string } {
	global ic_file_descriptors


	mess "ic_puts $fp_ext $string\n"

	if { ! [ info exists ic_file_descriptors($fp_ext) ] } {
		error "no such file open $fp_ext"
	}
	set fp $ic_file_descriptors($fp_ext)

	return [ puts $fp $string ]
}

proc ic_close { fp_ext } {
	global ic_file_descriptors

	if { ! [ info exists ic_file_descriptors($fp_ext) ] } {
                error "no such file open $fp_ext"
        }

	set fp $ic_file_descriptors($fp_ext)
	unset ic_file_descriptors($fp_ext)

	return [ close $fp ]
}
proc ic_rm { args } {
	if {$args != ""} {
		catch {eval file delete -force -- \
			[ glob -nocomplain $args] } res]
    	}
}
proc ic_rm_r { file } {
	return [ cmd_rm_r $file ]
}
proc ic_mv { from to } {
	return [ cmd_mv $from $to ]
}
proc ic_gzip { args } {
	return [ eval cmd_gzip $args ]
}
proc ic_rename { old new } {
	return [ cmd_rename $old $new ]
}
proc ic_cp { old new } {
	return [ cmd_cp $old $new ]
}
proc ic_cp_r { old new } {
	return [ cmd_cp_r $old $new ]
}
proc ic_mkdir { dir } {
	return [ cmd_mkdir $dir ]
}

## Removes duplicates from list of <i>names</i>.

proc ic_remove_duplicate {names} {
    catch {unset count}
    foreach c $names {
        if [info exists count($c)] {
            incr count($c)
        } else {
            set count($c) 1
        }
    }
    set new_names ""
    foreach cn [array names count] {
        if {$count($cn) == 1} {
            lappend new_names $cn
        }
    }
    return $new_names
}

proc undo_debug {s} {
    return
    mess "UNDO_DEBUG: $s" red
}

## Undo the previous action

proc ic_undo {} {
    global undo_redo uns_cur str_cur
	set nothing_done 0
    if [info exists undo_redo] {
	busy 1
	if [ic_uns_is_loaded] {
	    start_new_elems
	}
	set result [$undo_redo apply_undo]
	if {[string first "Nothing to be" $result] > 0} {
		set nothing_done 1
	}		
	set msg ""
	foreach {t n s p v} $result {
	    set res [undo_tmp_delete_graphics $t $n $s $p $v]
	    if {$res != ""} {
		append msg $res \n
	    }
	}
	update_bsp_consistency
	foreach {t n s p v} $result {
	    set res [undo_geom_update_graphics $t $n $s $p $v]
	    if {$res != ""} {
		append msg $res \n
	    }
	}
	if ![get_etm_undo_option 0] {
	    mess "$msg" blue
	} elseif {[lindex $msg 0] != "Nothing"} {
	    mess "$msg" blue
	}
	if [ic_uns_is_loaded] {
	    finish_new_elems
	    $uns_cur update_map_deletes
	    $uns_cur reset_extrude_hash
	}
	if [ic_str_is_loaded] {
	   if [get_etm_undo_option 1] {
	       # for undo operations after smoothing
	       $str_cur restore_previous_nodes
	       $str_cur glb_sm_init
	       mess "Structured operation undone.\n" blue
	   }
	}
	busy 0
    }
    if [ic_uns_is_loaded] {
    	update_uns_available
    }
    return $nothing_done
}

## Redo the previous undone action

proc ic_redo {} {
    global undo_redo uns_cur str_cur
	set nothing_done 0
    if [info exists undo_redo] {
	busy 1
	if [ic_uns_is_loaded] {
	    start_new_elems
	}
	set result [$undo_redo apply_redo]
	if {[string first "Nothing to be" $result] > 0} {
		set nothing_done 1
	}		
	set msg ""
	foreach {t n s p v} $result {
	    set res [undo_tmp_delete_graphics $t $n $s $p $v]
	    if {$res != ""} {
		append msg $res \n
	    }
	}
	update_bsp_consistency
	foreach {t n s p v} $result {
	    set res [undo_geom_update_graphics $t $n $s $p $v]
	    if {$res != ""} {
		append msg $res \n
	    }
	}	
	mess $msg blue	

	if [ic_uns_is_loaded] {
	    finish_new_elems
	    $uns_cur update_map_deletes
	    $uns_cur reset_extrude_hash
	}
	busy 0
    }
    update_uns_available
    return $nothing_done
}

## Set Undo group begin 

proc ic_undo_group_begin { {text "undo_group"} } {
    global undo_redo

    if [info exists undo_redo] {
        $undo_redo undo_group_begin $text
        undo_debug "ic_undo_group_begin \"$text\"\n"
    }
}

## Set Undo group end

proc ic_undo_group_end { {text "undo_group"} } {
    global undo_redo

    if [info exists undo_redo] {
        $undo_redo undo_group_end $text
        undo_debug "ic_undo_group_end \"$text\"\n"
    }
}

## Suspend undo logging

proc ic_undo_suspend {} {
    global undo_redo

    if [info exists undo_redo] {
        $undo_redo suspend_undo
        undo_debug "ic_undo_suspend\n"
    }
}
set dont_record_command(ic_undo_suspend) 1

## state of undo manager
proc ic_undo_suspended {} {
    global undo_redo
 
    set state 1
    if [info exists undo_redo] {
        set state [$undo_redo suspended]
    }
    return $state
}
set dont_record_command(ic_undo_suspended) 1

## resume undo logging
proc ic_undo_resume {} {
    global undo_redo
    
    if [info exists undo_redo] {
        $undo_redo resume_undo
        undo_debug "ic_undo_resume\n"
    }
}
set dont_record_command(ic_undo_resume) 1

proc ic_undo_enable_largeop {val} {
    global undo_redo
 
    if [info exists undo_redo] {
        $undo_redo enable_largeop $val
        undo_debug "ic_undo_enable_largeop $val\n"
    }
}
set dont_record_command(ic_undo_enable_largeop) 1

proc ic_undo_logging {on} {
    if $on {
        ic_undo_start
        undo_debug "ic_undo_logging on\n"
    } else {
        ic_undo_sto