#!/bin/sh
#the next line restarts using wish \
exec wish "$0" "$@"

#    usehelp, a Tcl/Tk script for the use of helpfiles in Tk windows.
#    Copyright (C) 1996  H. D. Baecker
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#    H. D. Baecker, Box 474, Lake Cowichan, BC, V0R 2G0, Canada
#    hbaecker@duncan.island.net


proc cmOpen {} {

uplevel #0 {
wm title . $filename
gets $f tmp
seek $f $tmp
gets $f plonk;#throw away title
gets $f plonk
set W(LP) $tmp
set W(C) $plonk
set W(CN) 1
for {set j 1} {$j <= $plonk} {incr j} {
   gets $f W($j,T)
   gets $f W($j,BP)
   gets $f W($j,S)
   }
} 
}

proc cmExit {} {
global f
   
   close $f
   exit
   }

proc lister {name listing} {
 
      global item phrase
       
   toplevel .l -class Lister
   wm iconname .l Listing
   frame .l.topest -relief raise -bd 1
   pack .l.topest -side top -fill both
   label .l.topest.lab1 -text $name
   label .l.topest.lab2 -text "To cancel press <Return>"
   label .l.topest.lab3 -text "To Select Double-Click Button 1"
   pack .l.topest.lab1 .l.topest.lab2 .l.topest.lab3 -side top
   frame .l.top -relief raised -bd 1
   pack .l.top -side top -fill both
   frame .l.bot -relief raised -bd 1
   pack .l.bot -side bottom -fill both
   
   listbox .l.top.things -relief raised -borderwidth 2 \
      -height 0 -width 0 -selectmode single \
      -yscrollcommand ".l.top.scroll set" \
       -xscrollcommand ".l.bot.scrollx set"
   pack .l.top.things -side left -fill both -expand yes
   scrollbar .l.top.scroll -command ".l.top.things yview"
   pack .l.top.scroll -side right -fill y
   scrollbar .l.bot.scrollx -orient horizontal \
      -command ".l.top.things xview"
   pack .l.bot.scrollx -side bottom -fill x
   foreach thing $listing {
      .l.top.things insert end $thing
   }           
     
   bind .l.top.things <Double-Button-1> \
     {set phrase [.l.top.things get active]
      set item [.l.top.things curselection]}
   bind .l.top.things <Return> {set item -1}
          
   set oldFocus [focus]
   grab set .l
   focus .l.top.things
   tkwait variable item
   
   destroy .l
   focus $oldFocus
   return $item
}
   
proc notelist {} {

uplevel #0 {
   set dino {}
   set num {}
   for {set k 1} {$k <= $W(C)} {incr k} {
               lappend dino $W($k,T)
               lappend num $k
         }
        
   set L [lister $h $dino]
   if {$L == -1} {return -1}
   return [lindex $num $L]
}
}
proc SelectTopic {} {

uplevel #0 {
   .text delete 1.0 end
   set h "Select Help Topic"             
   set L [notelist]
   if {$L == -1} {
      return
      }
   set W(CN) $L      
   seek $f $W($L,BP)
      .text insert end [read $f $W($L,S)]    
      .label configure -text $W($L,T) 
}    
}


set nbook "UseHelp"
    
#make menu

frame .mbar -relief raised -bd 2 
pack .mbar -side top -fill both
wm iconname . $nbook
wm title . $nbook

menubutton .mbar.select -text Select -underline 0 -menu .mbar.select.menu
menu .mbar.select.menu
.mbar.select.menu add command -label "Topic" -underline 0 \
	-command SelectTopic
.mbar.select.menu add command -label "Exit" -underline 0 \
      -command cmExit
pack .mbar.select -side left 
tk_menuBar .mbar .mbar.select
label .label -relief groove -width 72 -height 1 -bg Grey
  
text .text -relief raised -bd 2 -width 72 -height 20 -wrap word \
   -yscrollcommand ".scroll set"
scrollbar .scroll -command ".text yview"
pack .scroll -side right -fill y
pack .text -side left
pack .mbar .label -side top
pack .text -side bottom       
update
    .text insert 1.0 "Usehelp version .60, Copyright (C) 1996 H. D. Baecker \
    Usehelp comes with ABSOLUTELY NO WARRANTY; for details see the \
    enclosed file COPYING. \
    This is free software, and you are welcome to redistribute it
    under certain conditions, which are also defined in the \
    enclosed file COPYING."
   
focus .text
update
after 5000
   set n .help   
   if {$argc} {
      set filename $argv
      } else {
      set filename [exec FileSelect -patt "*.help"]
      }

   if {[file extension $filename] != $n} {append filename $n}
   set f [open $filename]
   cmOpen         
   SelectTopic   
while {1 == 1} {
   update
   after 69
   }


































