# This code is shared by all the ICEM CFD GUI's.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
#  moved to this file from mainwindow.tcl by Jason

if {[info command blt::busy] == ""} { set dont_use_blt_busy 1 }

if [info exists dont_use_blt_busy] {
    proc busy {on args} {
	global busy_saved_cursors busy_level busy_cur still_busy
	
	set debug 0
	if ![info exists busy_level] {
	    set busy_level 0
	}
	
	set prev_level $busy_level
	
	if {$on == "reset"} {
	    if $busy_level {
		set busy_level 0
	    }
	} else {
	    if $on { incr busy_level $on } else { incr busy_level -1 }
	}
	if {$busy_level > 1} {
	    if {$debug} {
	        mess "busy_level = $busy_level, prev_level = return = $prev_level\n" red
	    }
	    return $prev_level
	}
	
	if {$busy_level < 0} {
	    set busy_level 0
	    if {$debug} {
	        mess "busy_level = $busy_level, prev_level = $prev_level, return = 0\n" red
	    }
	    return 0
	}
	
	if $busy_level {
	    busy_set_cursors .
	} else {
	    foreach i [array names busy_saved_cursors] {
		if [winfo exists $i] {
		    catch {set_cursor $i $busy_saved_cursors($i)}
		}
	    }
	    catch {unset busy_saved_cursors}
	}
# We need "update" instead of "update idle", so it can finish destroying any current popup windows
# so that the pointer can enter the main window before any processing occurs, or else the 
# cursor will not change
	if {![info exists still_busy] || !$still_busy} {
		set still_busy 1
	update
		after idle {set still_busy 0}
    }
	if {$debug} {
	    mess "busy_level = $busy_level, prev_level = return = $prev_level\n" red
	}
	return $prev_level
    }
} else {
    
    proc busy {on args} {
	global busy_level busy_cur still_busy
	
	if ![info exists busy_level] {
	    set busy_level 0
	}
	
	set prev_level $busy_level
	
	if {$on == "reset"} {
	    if $busy_level {
		set busy_level 0
	    }
	} else {
	    if $on { incr busy_level $on } else { incr busy_level -1 }
	}
	if {$busy_level > 1} {
	    return $prev_level
	}
	
	if {$busy_level < 0} {
	    set busy_level 0
	    return 0
	}
	
	if $busy_level {
	    blt::busy hold .
	    #-cursor $busy_cur
	} else {
	    catch {blt::busy release .}
	}
	if {![info exists still_busy] || !$still_busy} {
		set still_busy 1
	update
		after idle {set still_busy 0}
	}
	return $prev_level
    }
}

proc busy_set_cursors {w} {
    global busy_cur busy_saved_cursors toplevel
    
    foreach cw [winfo children $w] {
        if ![winfo ismapped $cw] continue
        if ![catch {$cw cget -cursor} cc] {
            # For some strange reason configuring the cursor for .e
            # makes the window jump.
            set is_top [expr {![string compare $cw [winfo toplevel $cw]]} && \
                            !$toplevel(single)]
            if {($is_top || ($cc != "") || ([winfo toplevel $cw] == ".e")) && \
                    ![info exists busy_saved_cursors($cw)]} {
                if {($cw != ".e") && ![string match .e.unstruct* $cw]} {
                    set busy_saved_cursors($cw) $cc
                    catch {set_cursor $cw $busy_cur}
                }
            }
        }
        busy_set_cursors $cw
    }
}
    
if {[info commands wm] == ""} {
    # We're running in batch mode so don't do anything.
    proc busy {args} {}
}

