
# 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 host_vendor
    
    if {$root_dir == ""} {
	if ![info exists env(ICEM_ACN)] return
	set root_dir $env(ICEM_ACN)
    }
    
    if {$base == ""} {
	if [info exists help_base] {
	    set base $help_base
	} else {
	    set base base
	}
    }
    
    set help_viewer Default
    if [file exists $env(HOME)/.iceman_modals] {
	set rc [open $env(HOME)/.iceman_modals r]
	while {![eof $rc]} {
	    set line [gets $rc]
	    if [string match "set help_viewer*" $line] {	
		switch -glob $line {
		    *efault*  { set help_viewer Default }
		    *etscape*  { set help_viewer Default }
		    *ippo*    { set help_viewer Hippo }
		}
		break
	    }
	}
	close $rc
    }

    if {[info exists host_vendor] && (($host_vendor == "nt") || \
				      ($host_vendor == "win") || \
				      ($host_vendor == "decwin"))} {    
    	set help_program(Hippo) $root_dir/bin/help_viewer.bat
    	set help_program(Default) start
    } else {
	if {[get_path netscape 1] == ""} { set help_viewer Hippo }   
		set npath $root_dir/bin/netscape.remote
    	if ![file exists $npath] {
		set npath [get_path netscape 1]
    	}
    	set has_netscape [expr {[get_path netscape 1] != ""}]
    
    	if ![info exists help_viewer] {
		if $has_netscape {
	  	  set help_program $npath
		} else {
	   	 set help_program $root_dir/bin/help_viewer
		}
    	} else {
		if {($help_viewer == "Hippo") || !$has_netscape} {
	   	 if {$help_viewer != "Hippo"} {
			mess "Warning: help viewer defined as Netscape but it could not be found\n" red
	    	}
	   	 set help_program $root_dir/bin/help_viewer
		} else {
	  	  set help_program $npath
		}
   	 }
    }
    set help_dir $root_dir/help
    set help_info(.) $base
    bind all <F1> {help_window %X %Y}
    bind all <Help> {help_window %X %Y}
	
    if {[info command help_info_define] != ""} {
	help_info_define ; # sets the global variable help_info
    }
}

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_window {x y} {
    global help_delay
    
    if [info exists help_delay] return
    
    set w [winfo containing $x $y]
    if {$w == ""} { set w . }
    
    help_window_path $w
}

proc help_window_path {w} {			; # Find help files
  global help_info help_delay			; # for Manager and Leo
  
  if [string match *.m $w ] {			; # Menu button section
    set title {}
    set label [ $w entrycget active -label]
    foreach word $label {
      if {$word != " "} {
        set title $title$word
      }
    }
    if [info exists help_info($title)] {
      if [string match .v.panel.surfs.menu.m $w] {	; # Leo surface menu
        set num [$w index active]
        if { $num == 1 || $num == 3 } {
          if {![info exists env(ICEM_FLUENT)] && \
              ![info exists env(ICEM_POWERMESH)]} {	; # only in HIGH
            set title $title.surfs
          }
        }
      }							; # Leo surface menu end
      set info $help_info($title)
    } else {
      set info $help_info(.)
    }
    help_topic [lindex $info 0] [lindex $info 1]     
    set help_delay 1
    after 5000 "unset help_delay"
    return
  }						; # Menu button section end
      
  set label ""					; # Various section
  if [string match .modals* $w ] {
    set label ".modals"
  } elseif [string match .select_one* $w ] {	; # select_one help button
    set res [winfo children $w]			; # 2 Versions for
    foreach r $res {				; # HIGH and POWERMESH/GEOMESH
      if { $r == ".select_one.message" } {
        set m [$r cget -text]
        if { [string match {* a project from the list*} $m ] || \
             [string match {* a configuration from the list*} $m ] } {
         set label ".select_one.open_conf"
        } elseif { [string match {* a new project*} $m ] || \
                   [string match {* a new configuration*} $m ] } {
         set label ".select_one.new_conf"
        } elseif { [string match {* project to copy*} $m ] || \
                   [string match {* configuration to copy*} $m ] } {
         set label ".select_one.copy_conf"
        } elseif { [string match {* project to rename*} $m ] || \
                   [string match {* configuration to rename*} $m ] } {
         set label ".select_one.rename_conf"
        } elseif { [string match {* project to delete*} $m ] || \
                   [string match {* configuration to delete*} $m ] } {
         set label ".select_one.delete_conf"
        } elseif [string match {* a subproject, or*} $m ] {
         set label ".select_one.open_subproject"
        } elseif [string match {* subproject to copy*} $m ] {
         set label ".select_one.copy_subproject"
        } elseif [string match {* subproject to rename*} $m ] {
         set label ".select_one.rename_subproject"
        } elseif [string match {* subproject to delete*} $m ] {
          set label ".select_one.delete_subproject"
        } elseif { [string match {* project to archive*} $m ] || \
                   [string match {* existing configuration*} $m ] } {
          set label ".select_one.archive_conf"
        } elseif [string match {Select a  solver to use*} $m ] {
          set label ".select_one.select_solver"
        } else {
          set label ".select_one"
        }
        break
      }
    }						; # select_one help button end
  } elseif [string match .deselect* $w ] {
    set label ".deselect"
  } elseif [string match .tearoff* $w ] {	; # tearoff Menus Manager
    set num [$w index active]
    set label ".tearoff"
    if { [$w type $num] != "separator" } {
      set label [ $w entrycget $num -label ]
      regsub -all { } $label {} label
    }						; # tearoff Menus Manager end
  } elseif [string match .pathnames* $w ] {
    set label ".pathnames"
  } elseif [string match .gplot* $w ] {
    set label ".glpot" 
  } elseif [string match .plot3d* $w ] {
    set label ".plot3d"
  } elseif [string match .patran* $w ] {
    set label ".patran"
  } elseif [string match .stl* $w ] {
    set label ".stl" 
  } elseif [string match .nastran* $w ] {
    set label ".nastran"
  } elseif [string match .rampant* $w ] {
    set label ".rampant"
  } elseif [string match .leo* $w ] {
    set label ".leo"
  } elseif [string match .nekton* $w ] {
    set label ".nekton"
  } elseif [string match .v_domains* $w ] {
    set label ".v_domains"
  } elseif [string match .v_subface* $w ] {
    set label ".v_subface"
  } elseif [string match .v_edge* $w ] {
    set label ".v_edge"
  } elseif [string match .v_vertex* $w ] {
    set label ".v_vertex"
  } elseif [string match .boco_domains* $w ] {
    set label ".boco_domains"
  } elseif [string match .boco_subface* $w ] {
    set label ".boco_subface"
  } elseif [string match .boco_edge* $w ] {
    set label ".boco_edge"
  } elseif [string match .boco_vertex* $w ] {
    set label ".boco_vertex"
  } elseif [string match .families* $w ] {
    set label ".families"
  } elseif [string match .secondary* $w ] {
    set label ".secondary"
  } elseif [string match .move_domains* $w ] {
    set label ".move_domains"
  } elseif [string match .verify_periodic* $w ] {
    set label ".verify_periodic"
  } elseif [string match .boco_sol* $w ] {
    set label ".boco_sol"
  } elseif [string match .methods.smbuttons* $w ] {
    set label ".methods.smbuttons"
  } elseif [string match .methods.smchoice* $w ] {
    set label ".methods.smchoice"
  } elseif [string match .methods.savebuttons* $w ] {
    set label ".methods.savebuttons"
  } elseif [string match .methods.param_but* $w ] {
    set label ".methods.param_but"
  } elseif [string match .methods.nrz3a* $w ] {
    set label ".methods.nrz3a"
  } elseif [string match .methods.nrz3* $w ] {
    set label ".methods.nrz3"
  } elseif [string match .methods.nrz4a* $w ] {
    set label ".methods.nrz4a"
  } elseif [string match .methods.nrz4b* $w ] {
    set label ".methods.nrz4b"
  } elseif [string match .methods.nrz4* $w ] {
    set label ".methods.nrz4"
  } elseif [string match .methods.nrz5a* $w ] {
    set label ".methods.nrz5a"
  } elseif [string match .methods.nrz5b* $w ] {
    set label ".methods.nrz5b"
  } elseif [string match .methods.nrz5* $w ] {
    set label ".methods.nrz5"
  } elseif [string match .methods.ex2choice* $w ] {
    set label ".methods.ex2choice"
  } elseif [string match .methods* $w ] {
    set label ".methods"
  } elseif [string match *tearoff* $w ] {	; # tearoff Menus Leo
    set num [$w index active]
    set label ".tearoff"
    if { [$w type $num] != "separator" } {
      set label [ $w entrycget $num -label ]
      regsub -all { } $label {} label
    }   
  }						; # tearoff Menus Leo end
    
  if {$label != ""} {
    if [info exists help_info($label)] {
      set info $help_info($label)
    } else {
      set info $help_info(.)
    }
    help_topic [lindex $info 0] [lindex $info 1]
    set help_delay 1
    after 5000 "unset help_delay"
    return
  }						; # Various section end

  if {[winfo class $w] == "Menu"} {		; # Menu section
    set item [$w index @$y]
    if [info exists help_info($w.$item)] {
      set info $help_info($w.$item)
    }
  }
  if ![info exists info] {
    while {![info exists help_info($w)]} {
      set w [winfo parent $w]
    }
    set info $help_info($w)
  }
  help_topic [lindex $info 0] [lindex $info 1]
    
  set help_delay 1
  after 5000 "unset help_delay"  		; # Menu section end
}

proc help_topic {file {tag ""}} {
    global help_file help_program help_dir env host_vendor once_hack help_viewer
    
    if {[info exists host_vendor] && (($host_vendor == "nt") || \
				      ($host_vendor == "win") || \
				      ($host_vendor == "decwin"))} {
	set url file:$help_dir/$file
	if {$tag != ""} {
	    set url $url.html#$tag
	} else {
	    set url $url.html
	}
	regsub -all / $url \\\\ url
        if {($help_viewer == "Hippo")} {
	    set prog $help_program(Hippo)
	} else {
	    set prog $help_program(Default)
	} 
        set url_file [ file tail $url ]
	mess "Starting help \"$url_file\"\n"
	exec $prog $url &
	return
    }
    set url file:$help_dir/$file
    if {$tag != ""} {
	set url $url.html#$tag
    } else {
	set url $url.html
    }
    mess "Starting help viewer using the command \"$help_program $url\"\n"
    exec $help_program $url &
}

if {[info commands help_application] == ""} {
    proc help_application {} {
	global help_info
	
	help_topic [lindex $help_info(.) 0] [lindex $help_info(.) 1]
    }
}
