
# Minimal help system.
#
# Copyright (c) 1993 - 1995 ICEM Technologies GmbH.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
#          ICEM CFD Engineering, Berkeley, California

# This code is adapted from Ousterhout's rolodex demo.

# This routine sets the help info for a subtree that starts at w to be the string.

proc help_define {w string} {
    global helpTopics helpCmds
    
    set helpTopics($w) $string
    bind $w <Any-F1> {help_provide [winfo containing %X %Y] %X %Y}
    
    foreach ww [list_descendants $w] {
	set helpCmds($ww) "set topic $w"
	bind $w <Any-F1> {help_provide [winfo containing %X %Y] %X %Y}
    }    
}

proc list_descendants {w} {
    set res [winfo children $w]
    foreach r $res {
	set desc [list_descendants $r]
	if {$desc != {}} {lappend res $desc}
    }
    return $res
}

proc help_provide {topic {x 0} {y 0}} {
    global helpTopics helpCmds

    if {$topic == ""} return
    while {[info exists helpCmds($topic)]} {
	set topic [eval $helpCmds($topic)]
    }
    if [info exists helpTopics($topic)] {
	set msg $helpTopics($topic)
    } else {
	set msg "Sorry, but no help is available for the topic \"$topic\""
    }
    
    set yuck [format_block $msg 0 60]
    set numlines 1
    set maxlen 0
    set thislen 1
    for {set i 0} {$i < [string length $yuck]} {incr i} {
	set c [string index $yuck $i]
	case $c {
	    {"\t"} {incr thislen 8}
	    {"\n"} {
		incr numlines
		if {$thislen > $maxlen} {set maxlen $thislen}
		set thislen 1
	    }
	    default {incr thislen}
	}
    }
    if {$thislen > $maxlen} {set maxlen $thislen}
    
    set scroll 1
    set height 25
    if {$numlines < $height} {set height $numlines; set scroll 0}
    set width [expr {$maxlen + 6}]
    
    catch {destroy .help}
    toplevel .help
    
    text .help.text -relief raised -border 1 -background "#eeffcc" \
	-wrap word -width $width -height $height \
	-exportselection 0 -padx 4 -pady 4
    .help.text insert end $msg
    .help.text configure -state disabled
    if $scroll {.help.text configure -yscroll ".help.yscroll set"}
    
    button .help.button -text "Done" -command "destroy .help"
    
    if $scroll {
	scrollbar .help.yscroll -relief sunken \
	    -command ".help.text yview" -orient v
	pack append .help \
	    .help.button {bottom fill} \
	    .help.yscroll {right fill} \
	    .help.text {top fill expand}
    } else {
	pack append .help \
	    .help.button {bottom fill} \
	    .help.text {top fill expand}
    }

    wm geometry .help +200+200
    #putcenter .help
}

bind . <Any-F1> {help_provide [winfo containing %X %Y] %X %Y}
foreach menu [winfo children .menu] {
    catch {
	set m $menu.m
	bind $m <Any-F1> {help_provide [winfo containing %X %Y] %X %Y}
    }
}

# Here is something to take the help info and make a nice printout from it.

proc help_write_reference {} {
    global helpTopics whoami menu_disabled modal_data icem_fluent icem_powermesh
    
    set topics [lsort [array names helpTopics]]
    set file [open HelpSummary w]
    
    set form 1
    if {[query_question .query "Write formatted guide?"] == "*cancel*"} \
	{set form 0}
    
    if $form {set len 68} {set len 1000000}
    
    infomsg "Writing reference to file \"HelpSummary\" ..."
    
    puts $file ""
    puts $file "\t$whoami"
    puts $file "\t\tReference Guide"
    puts $file ""
    puts $file "\t---------------------------------"
    puts $file ""

    set text [format_block $helpTopics(general) 0 $len]
    puts $file $text
    puts $file ""
    set text [format_block $helpTopics(options) 0 $len]
    puts $file $text
    puts $file ""
    
    puts $file "User Interface"
    puts $file "---- ---------"
    set text [format_block $helpTopics(ui) 0 $len]
    puts $file $text
    puts $file ""
    set text [format_block $helpTopics(parts) 0 $len]
    puts $file $text
    puts $file ""
    set text [format_block $helpTopics(tcl) 0 $len]
    puts $file $text
    puts $file ""
    
    puts $file "Commands Available"
    puts $file "-------- ---------"
    puts $file "The commands available via the menus are listed below. Any commands with"
    puts $file "keyboard sequences have a \"Key:\" annotation to the right of the command name."
    puts $file "Commands that have been disabled and appear gray in the menu are not listed."
    
    # First assemble all the menu names.
    set menus {}
    set info [pack info .menu]
    set opt 0
    foreach win $info {
	if $opt {set opt 0; continue} {set opt 1}
	set mtext [llast [$win configure -text]]
	set mhtext $helpTopics($win)
	set huh [catch "$win configure -menu" res]
	if $huh continue
	set mwin [llast $res]
	set numents [$mwin index last]
	set mhtext [format_block $mhtext 8 $len]
	
	puts $file ""
	puts $file "    Menu: $mtext"
	puts $file "$mhtext"
	puts $file ""
	
	for {set n 0} {$n <= $numents} {incr n} {
	    if [info exists menu_disabled($mwin.$n)] continue
	    set huh [catch "$mwin entryconfigure $n -label" res]
	    if $huh continue
	    set etext [llast $res]
	    set eaccel [llast [$mwin entryconfigure $n -accelerator]]
	    eval "set eaccel $eaccel"
	    set metext $helpTopics($mwin.$n)
	    set metext [format_block $metext 16 $len]
	    
	    if $form {
		if {$eaccel == ""} then {
		    puts $file "\t$etext"
		} else {
		    set space [expr {40 - [string length $etext]}]
		    puts $file [format "\t$etext%*sKey: $eaccel" \
				$space " "]
		}
	    } else {
		puts $file "$etext"
		if {$eaccel != ""} {puts $file "Key: $eaccel"}
	    }
	    puts $file "$metext"
	}
    }
    puts $file ""
    
    if !$icem_fluent {
	puts $file "ICEM CFD/CAE Modals"
	puts $file "---- ------- ------"
    } else {
	puts $file "GeoMesh Modals"
	puts $file "------- ------"
    }
    set text [format_block $helpTopics(modals) 0 $len]
    puts $file $text
    puts $file "The available modals are as follows.\n"
    
    foreach mod $modal_data {
	set name [lindex $mod 0]
	set desc [lindex $mod 1]
	set text [format_block $helpTopics(modals.$name) 8 $len]
	puts $file "    $desc"
	puts $file $text
    }
    puts $file ""
    
    puts $file "Customization"
    puts $file "-------------"
    set text [format_block $helpTopics(custom) 0 $len]
    puts $file $text
    
    close $file
    infomsg "Done."
}

proc format_block {text indent length} {
    set formatted {}
    set what 0
    set len [string length $text]
    while {$what < $len} {
	set pos 1
	for {set i 0} {$i < $indent} {incr i} {
	    append formatted " "
	    incr pos
	}
	set sol 1
	set br 0
	while {$what < $len} {
	    set c [string index $text $what]
	    case $c {
		{" "} {
		    if {$pos > $length} {
			if {$pos < $len - 1} {
			    append formatted "\n"
			}
			set br 1
		    } else {
			if {!$sol} {
			    append formatted " "
			}
		    }
		}
		{"\n"} {
		    append formatted "\n"
		    set br 1
		}
		
		{"/" "."} {
		    set n [expr {$what + 1}]
		    if {($pos > $length) && \
			($pos < $len - 2) && \
			    ([string index $text $n] != "\n")} {
				append formatted "$c\n"
				set br 1
			    } else {
				append formatted $c
			    }
		}
		default {
		    append formatted $c
		    set sol 0
		}
	    }
	    incr pos
	    incr what
	    if $br break
	}
    }
    return $formatted
}

proc help_show_manual { manual_name } {
    global env icem_fluent icem_powermesh
    
    if $icem_fluent {set manual $env(ICEM_ACN)/info/GeoMesh.ps}
    if $icem_powermesh {set manual $env(ICEM_ACN)/info/Powermesh.ps}
    
    if [file exists $env(ICEM_ACN)/bin/ghostview] {
	
	infomsg "Please wait ....."
	
	set command "/bin/echo \" \" ;/bin/echo \" Preparing $manual_name Users Guide \";/bin/echo \" Please wait .... \" ;/bin/cp $env(ICEM_ACN)/info/Fontmap ./. ;/bin/cp $env(ICEM_ACN)/info/gs_init.ps ./. ;/bin/cp $env(ICEM_ACN)/info/gs_statd.ps ./. ;/bin/cp $env(ICEM_ACN)/info/gs_fonts.ps ./. ;/bin/cp $env(ICEM_ACN)/info/gs_dps1.ps ./. ;/bin/cp $env(ICEM_ACN)/info/gs_lev2.ps ./. ;/bin/cp $env(ICEM_ACN)/info/gs_type0.ps ./. ;/bin/cp $env(ICEM_ACN)/info/ptmr.gsf ./. ;/bin/cp $env(ICEM_ACN)/info/uglyr.gsf ./. ;/bin/cp $env(ICEM_ACN)/info/ncrr.gsf ./. ;/bin/cp $env(ICEM_ACN)/info/phvr.gsf ./. ;/bin/cp $env(ICEM_ACN)/info/phvb.gsf ./. ;/bin/cp $env(ICEM_ACN)/info/gs_sym_e.ps ./. ;/bin/cp $env(ICEM_ACN)/info/psyr.gsf ./. ;set path=.:/bin/:$env(ICEM_ACN)/bin:$env(ICEM_ACN)/info  ;$env(ICEM_ACN)/bin/ghostview -quiet -letter -nolabels -magstep -1 -page 5 $manual ;/bin/rm -f Fontmap gs_init.ps gs_statd.ps gs_fonts.ps gs_dps1.ps gs_lev2.ps gs_type0.ps ncrr.gsf phvr.gsf phvb.gsf gs_sym_e.ps psyr.gsf ptmr.gsf uglyr.gsf"
	
	
	runxcom "$manual_name" $command "" 1 0
	
	#     set previewer [exec /usr/sbin/xpsview -sc 0.87 -skipc $manual &]
	
    } else {
	infomsg "The previewer could not be located"
    }
    
}
