
# Help system code.
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc help_setup {{base ""} {root_dir ""}} {
    global help_program help_dir help_info help_base env help_viewer
    global is_win
    
    if {$base == ""} {
	if [info exists help_base] {
	    set base $help_base
	} else {
	    set base index
	}
    }
    
    if $is_win {
	set help_program start
    } else {
	set help_program [get_path netscape 1]
	if {$help_program == ""} { set help_program [get_path mozilla 1] }
    }
    
    if ![info exists help_dir] {
	if {$root_dir != ""} {
	    set help_dir $root_dir/help
	} elseif [info exists env(ICEM_ACN)] {
	    set help_dir $env(ICEM_ACN)/help
	} else {
	    user_error "no help directory can be found!"
	    return
	}
    }
    
    set help_info(.) $base
    bind all <F1> {help_window %X %Y}
    bind all <Help> {help_window %X %Y}
}

proc help_define {w item page tag} {
    global help_info
    
    if {$item != ""} {
	set help_info($w.$item) [list $page $tag]
    } else {
	set help_info($w) [list $page $tag]
    }
}

proc help_clear {prefix} {
    global help_info
    
    foreach w [array names help_info] {
	if [string match $prefix* $w] {
	    unset help_info($w)
	}
    }
}

proc help_window {x y} {
    global help_delay help_window_override

    if {[info command help_window_override] != ""} {
	help_window_override $x $y
	return
    }
    if [info exists help_delay] return
    set w [winfo containing $x $y]
    if {$w == ""} { set w . }
    set rely [expr $y - [winfo rooty $w]]
    help_window_path $w $rely
}

proc help_window_path {w {y 0}} {
    global help_info help_delay tearoff_menus help_toplevel_info_written
    
    # There must be a better way to do this but .....
    foreach ww [array names help_info] {
	set unset_ww 0
	if ![string match .* $ww] {
	    # This must be a title for a toplevel window
	    foreach tw [winfo children .] {
		# checks if there is help info available for each window $tw
		# with the title $ww
		if {![info exists help_info($tw)] && ([top_title $tw] == $ww)} {
		    set help_info($tw) $help_info($ww)
		    # it seems there can be more than one window $tw 
		    # with the title $ww, so don't unset till have checked
		    # all of them
		    set unset_ww 1
		}
	    }
	    if $unset_ww { unset help_info($ww) }
	}
    }
    if {[winfo class $w] == "Menu"} {
	set item [$w entrycget [$w index @$y] -label]
	if [info exists tearoff_menus($w)] {
	    set w $tearoff_menus($w)
	}
	if [info exists help_info($w.$item)] {
	    set info $help_info($w.$item)
	}

    }
    if ![info exists info] {
	while {($w != "") && ![info exists help_info($w)]} {
	    set w [winfo parent $w]
	}
	if {$w == ""} {
	    mess "Sorry, no help available for this window\n" red
	    return
	}
	set info $help_info($w)
    }
    help_topic [lindex $info 0] [lindex $info 1]
    
    # I don't remember why this is needed
    set help_delay 1
    after 500 "catch {unset help_delay}"
}

proc help_main_topic {} {
    global help_main_topic
    global env
    
    if [info exists env(ICEM_AI_ENVIRONMENT)] {
	  namespace eval ::AIEnv::GUIManagement {
	    global env
	    source $env(ICEM_ACN)/lib/ai_env/scripts/aie_contexHelp.tcl
	    source $env(ICEM_ACN)/lib/ai_env/scripts/aie_javahelp.tcl
             chmhlp_intialize
	  }
          ::AIEnv::GUIManagement::help_book
    } else {
      if [info exists help_main_topic] {
 	help_topic $help_main_topic
      } else {
	help_window_path .
      }
    }
}

proc help_topic {file {tag ""}} {
    global help_file help_program help_dir env host_vendor is_win help_base
    
    set is_chm 0
    if {$is_win && ([file extension $help_base] == ".chm")} {
	set is_chm 1
	
	if {[file extension $file] == ".chm"} {
	    set subfile ""
	} elseif [file exists $help_dir/$file] {
	    set subfile ::__SLASH__[file tail $file]
	} elseif [file exists $help_dir/$file.html] {
	    set subfile ::__SLASH__[file tail $file.html]
	} elseif [file exists $help_dir/$file.htm] {
	    set subfile ::__SLASH__[file tail $file.htm]
	} else {
	    set subfile ""
	}
	
	set url $help_dir/${help_base}$subfile
	if {($subfile != "") && ($tag != "")} { append url \#$tag }
	
    } else {
	if {[file pathtype $file] == "relative"} {
	    set file $help_dir/$file
	}
	
	if [file exists $file] {
	    
	} elseif [file exists $file.html] {
	    set file $file.html
	} elseif [file exists $file.htm] {
	    set file $file.htm
	} else {
	    mess "Warning: no file $file or $file.html or $file.htm found\n" red
	    set url $help_dir
	    set tag ""
	}
	set url file:$file
	if {$tag != ""} { append url \#$tag }
    }
    if $is_win {
	regsub -all / $url \\\\ url
	regsub -all __SLASH__ $url / url
    }
    
    help_url $url $is_chm
}

proc help_url {url {is_chm 0}} {
    global is_win env help_program
    
    if $is_win {
	if $is_chm {
	    set prog $env(SystemRoot)\\hh
	} else {
	    set prog start
	}
	
	mess "Starting html viewer: \"$prog $url\"\n"
	exec $prog $url &
    } else {
	if {![info exists help_program] || $help_program == ""} {
	    user_error "No html viewer program could be found!"
	    return
	}
	mess "Starting html viewer: \"$help_program $url\"\n"
	if [catch {exec $help_program -remote openurl($url)} res] {
	    if ![string match {*warning*} $res] {
		puts $res
		exec $help_program $url &
	    }
	}
    }
}



