
# This code creates the user interface for the mesh editor.
#
# Copyright (c) 1999 Pacific Marketing and Consulting, Inc,
#	and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
#          Jari Spaete, jari.spaete@ansys.com
#
# interrupt check numdone numtotal message color
# if numtotal is 0 then it won't update the percentage
# if only interrupt check is given then it will check but not update
# interrupt add_message "xxxx" : goes above the Done line

proc interrupt {what args} {
    global interrupt_pending interrupt_prev_pct interrupt_prev_mess
    global tk_version interrupt_disable tdv_default interrupt_totalval
    global interrupt_frame is_win interrupt_mess_given
    global interrupt_use_command env
    global plot_window ai_environment

    if {![info exists tk_version] || [info exists interrupt_disable] || ([interp exists batch_interp] && [batch_interp eval info exists interrupt_disable])} {
        return 0
    }

    set aienv 0
    if {!$is_win && [info exists interrupt_frame]} {
        set frame $interrupt_frame
        set topl 0
    } elseif {[info exists ai_environment] && $ai_environment} {
        set topl 0
        set aienv 1
        set frame $plot_window.progress
        ::AIEnv::GUIManagement::ConfigureConsoleZone 1
        busy reset
    } else {
        set frame .progress
        set topl 1
    }


    switch $what {
	set {
	    if {$args == ""} { set args 1 }
	    set interrupt_pending $args
	}
	unset {
	    catch { unset interrupt_pending }
		if $aienv {
	        if [ic_wb2_is_running] {
	            aie_unset_global wb2_icemcfd_running
	        }
	    }
	}
	init {
	    set interrupt_prev_pct 0
	    set interrupt_prev_mess ""
	    set interrupt_mess_given ""
	    set interrupt_totalval ""
	    catch { unset interrupt_pending }
	    if $aienv {
	        if [ic_wb2_is_running] {
	            aie_unset_global wb2_icemcfd_running
	        }
	        if {[info exists plot_window] && [winfo exists $plot_window.graph]} {
                aie_smooth_clear_histogram
            }
	        frame $frame -relief sunken -bd 2
		    pack $frame -side left -fill x -expand 1 -padx 10
		    if {[aie_get_global icem_cfd_idle] == "1"} {
		        label $frame.title -text " Idle"
		    } else {
		        label $frame.title -text "Operation in progress"
		    }
		    pack $frame.title -side top -anchor w
	    } elseif $topl {
		    catch {top_destroy $frame}
		    if {[aie_get_global icem_cfd_idle] == "1"} {
		        top_window $frame 1 "Idle" "" "" 0 1
		    } else {
		        top_window $frame 1 "Operation in progress" "" "" 0 1
		    }
	    } else {
		    foreach ch [winfo children $frame] { destroy $ch }
	    }
	    if $aienv {
	        if [ic_wb2_is_running] {
	            aie_set_global wb2_icemcfd_running 1
	        }
	    }
	    frame $frame.back -background yellow -relief flat -borderwidth 0
	    frame $frame.front -background green -relief raised -borderwidth 5
	    button $frame.stop -text " Interrupt " -padx 3 -pady 0 -background red
	    if [info exists interrupt_use_command] {
		    $frame.stop configure -command $interrupt_use_command
	    } else {
		    $frame.stop configure -command "interrupt set 1"
	    }
	    if $topl {
		    $frame.back configure -width 300 -height 25
		    $frame.front configure -width 0 -height 25
		    $frame.stop configure -pady 3
	    }
	    set stbuts $frame.stop
	    foreach {text color val} $args {
		    button $frame.stop$val -text " $text " -padx 3 -pady 3 -command "interrupt set $val" -background $color
		    lappend stbuts $frame.stop$val
	    }
	    if {[aie_get_global icem_cfd_idle] == "1"} {
	        label $frame.status -relief flat -borderwidth 0 -anchor w -text " Press Interrupt to reactivate ICEM CFD"
	    } else {		    
	        label $frame.status -relief flat -borderwidth 0 -anchor w -text " 0 % done"
	    }
	    if $topl {
		    $frame.status configure -relief ridge -borderwidth 2
	    }
	    if $aienv {
		    pack $frame.status -side bottom -fill both -expand 1
		    pack $frame.back -side left -fill both -expand 1
	    } else {
		    pack $frame.status -side bottom -fill both ;# -expand 1
		    pack $frame.back -side left -fill both ;# -expand 1
	    }
	    eval pack $stbuts -side left -fill both
	    place $frame.front -in $frame.back -x 0 -anchor nw -y 0 -relheight 1
	    if $topl {
		    set ww [winfo width .]
		    top_geometry $frame +[expr $ww - 380]+0
		    form_auto_raise $frame
	    }
	    catch {grab $frame}
	    update
	}
	begin {
	    # this is an alternate form :
	    # interrupt begin title message totalval [ stop_func ] [ no_grab ]
	    # totalval can be 0 in which case no bar is put up

	    set interrupt_prev_pct ""
	    set interrupt_prev_mess ""
	    set interrupt_mess_given [lindex $args 1]
	    set interrupt_totalval [lindex $args 2]

	    catch { unset interrupt_pending }
	    if $aienv {
	        if [ic_wb2_is_running] {
	            aie_unset_global wb2_icemcfd_running
	        }
	        if {[info exists plot_window] && [winfo exists $plot_window.graph]} {
                aie_smooth_clear_histogram
            }
	        frame $frame -relief sunken -bd 2
		    pack $frame -side left -fill x -expand 1 -padx 10
		    label $frame.title -text [lindex $args 0]
		    pack $frame.title -side top -anchor w
	    } elseif $topl {
		    top_destroy $frame
		    top_window $frame 1 [lindex $args 0] "" "" 0 1
	    } else {
		    foreach ch [winfo children $frame] { destroy $ch }
	    }
	    if $interrupt_totalval {
		    frame $frame.back -background yellow -relief flat -borderwidth 0
		    frame $frame.front -background green -relief raised -borderwidth 5
		    if {[aie_get_global icem_cfd_idle] == "1"} {
		        set stext " Press Interrupt to reactivate ICEM CFD"
		    } else {
		        if $topl {
		             set stext " 0 % done"
		        } else {
		            set stext "[lindex $args 1]: 0 % done"
		        }
		    }
		    label $frame.status -relief flat -borderwidth 0 -anchor w -text $stext
		    if $topl {
		        $frame.back configure -width 300 -height 25
		        $frame.front configure -width 0 -height 25
		        $frame.status configure -relief ridge -borderwidth 2
		    }
	    }
	    if [info exists interrupt_use_command] {
		    set cmd $interrupt_use_command
	    } else {
		    set cmd "interrupt set 1"
		    if {[lindex $args 3] != ""} {
		        append cmd "; [lindex $args 3]"
		    }
	    }
	    button $frame.stop -text " Interrupt " -padx 3 -command $cmd -background red
	    if $topl {
		    label $frame.mess -relief ridge -borderwidth 2 -anchor w -text "[lindex $args 1] ..."
		    pack $frame.mess -side top -fill both -expand 1
		    $frame.stop configure -pady 3
	    }
	    pack $frame.status -side bottom -fill both -expand 1
	    if $interrupt_totalval {
		    pack $frame.back -side left -fill both -expand 1
		    pack $frame.stop -side left -fill both
		    place $frame.front -in $frame.back -x 0 -anchor nw -y 0 -relheight 1
	    } else {
		    pack $frame.stop -side left
	    }
	    if $topl {
		    set ww [winfo width .]
		    top_geometry $frame +[expr $ww - 380]+0
		    update idle
		    raise $frame
		    form_auto_raise $frame
	    }
	    if {[lindex $args 4] == "no_grab"} {
            $frame.stop configure -background orange
        } else {
		    catch { grab $frame }
	    }
	    update
	}
	add_message {
	    if {[winfo exists $frame.stop] && !$topl} {
		    if ![winfo exists $frame.status] {
		        error "The interrupt window is gone"
		    }
		    set n 0
		    set col black
		    if {[lindex $args 3] != ""} { set col [lindex $args 3] }
		    while {[winfo exists $frame.mess$n]} { incr n }
		    label $frame.mess$n -relief ridge -borderwidth 2 -anchor w -text [lindex $args 2] -foreground $col
		    pack $frame.mess$n -side bottom -fill both -expand 1 -before $frame.status
		    update
	    }
	    return 0
	}
	finish {
		if $aienv {
	        if [ic_wb2_is_running] {
	            aie_unset_global wb2_icemcfd_running
	        }
	    }
	    if ![winfo exists $frame.stop] {
		    # Maybe we were called recursively
		    return 0
	    }
	    # Just to look nice
	    interrupt check 100 100
        if {[grab status $frame] != "none"} {busy 0}
	    if $topl {
		    catch { top_destroy $frame }
	    } else {
		    foreach ch [winfo children $frame] { destroy $ch }
	    }
	    if $aienv {
	        catch { destroy $frame }
	    }
	    grab release [grab current]
	    if {[info exists interrupt_pending] && $interrupt_pending} {
	        if {[aie_get_global icem_cfd_idle] != "1"} {
	            mess "Interrupted\n" red
	        }
	    }
	    catch { unset interrupt_pending }
        if $aienv {
	        if [ic_wb2_is_running] {
	            aie_unset_global wb2_icemcfd_running
	        }
            ::AIEnv::GUIManagement::ConfigureConsoleZone 0
        }
	    update
	}
	check {
	    if ![winfo exists $frame.stop] {
		    # Maybe we were called recursively
		    return 0
	    }
	    set pa [tdv_active 0]
	    update
	    if ![winfo exists $frame.status] {
		    # Maybe the status bar went away in the last update ...
		    tdv_active $pa
		    return 0
	    }
	    if {[lindex $args 0] >= 0} {
		    if {[lindex $args 1] > 0} {
		        set pct [expr {[lindex $args 0] / ([lindex $args 1] + 0.0)}]
		        set interrupt_prev_pct $pct
		    } else {
		        set pct $interrupt_prev_pct
		    }
		    if {[aie_get_global icem_cfd_idle] == "1"} {
		        set mess " Press Interrupt to reactivate ICEM CFD"
		    } else {
		        set mess " [expr int($pct * 100)] % done"
		    }
		    if {[lindex $args 2] != ""} {
		        append mess "; [lindex $args 2]"
		        set interrupt_prev_mess [lindex $args 2]
		    } elseif {$interrupt_prev_mess != ""} {
		        append mess "; $interrupt_prev_mess"
		    }
		    set col black
		    if {[lindex $args 3] != ""} {
		        set col [lindex $args 3]
		    }
		    $frame.status configure -text $mess -fg $col
		    update
		    set ww [expr [winfo width $frame.back] * $pct]
		    $frame.front configure -width $ww
	    }
	    update idle
	    tdv_active $pa
	    if [info exists interrupt_pending] {
		    return $interrupt_pending
	    } else {
		    return 0
	    }
	}
	update {
	    # This is an alternate form:
	    # interrupt update totamt message
	    if ![winfo exists $frame.stop] {
		    # Maybe we were called recursively
		    return 0
	    }

	    if ![winfo exists $frame.status] {
		    error "The interrupt window is gone"
	    }

	    set pct [expr {int(100 * [lindex $args 0] / ($interrupt_totalval + 0.0))}]
	    if {$pct == $interrupt_prev_pct} { return 0 }
	    set interrupt_prev_pct $pct

	    set pa [tdv_active 0]
	    update
	    if ![winfo exists $frame.status] return ;# update might have dismissed it
        if {[aie_get_global icem_cfd_idle] == "1"} {
	        if $topl {
		        $frame.status configure -text " Press Interrupt to reactivate ICEM CFD"
		        if {[lindex $args 1] != ""} {
		            $frame.mess configure -text "Press Interrupt to reactivate ICEM CFD ..."
		        }
	        } else {
		        set m [lindex $args 1]
		        if {$m == ""} { set m $interrupt_mess_given }
		        $frame.status configure -text "Press Interrupt to reactivate ICEM CFD"
	        }
        } else {
	        if $topl {
		        $frame.status configure -text " $pct % done"
		        if {[lindex $args 1] != ""} {
		            $frame.mess configure -text "[lindex $args 1] ..."
		        }
	        } else {
		        set m [lindex $args 1]
		        if {$m == ""} { set m $interrupt_mess_given }
		        $frame.status configure -text "$m: $pct % done"
	        }
	    }
	    update
	    if ![winfo exists $frame.status] return ;# update might have dismissed it

	    set ww [expr [winfo width $frame.back] * $pct * 0.01]
	    $frame.front configure -width $ww

	    update idle
	    tdv_active $pa
	    if {[info exists interrupt_pending] && $interrupt_pending} {
		    if $aienv {
	            if [ic_wb2_is_running] {
	                aie_unset_global wb2_icemcfd_running
	            }
	        }
		    mess "Interrupted\n" red
		    if {[grab status $frame] != "none"} {busy 0}
		    if $topl {
		        top_destroy $frame
		    } else {
		        foreach ch [winfo children $frame] { destroy $ch }
		    }
		    if $aienv { catch {destroy $frame} }
		    set i $interrupt_pending
		    unset interrupt_pending
		    grab release [grab current]
		    return $i
	    } else {
		    return 0
	    }
	}
    }
}
