#!/usr/X386/bin/wish -f
# Program: faqbro
# Tcl version: 7.3 (Tcl/Tk/XF)
# Tk version: 3.6
# XF version: 2.2
#

#################################################################
# faqbro: SGML$@7A<0(J FAQ $@%V%i%&%6(J Ver0.2 1994-06-05
# $@:n<T!'9bED(J  $@?.0l(J  (Nifty: NBA01741)
# Change Log:
# 1. Item $@$K$h$kA*Br(J
# 2. Chapter, Subject $@$N$J$$7A<0$X$NBP1~(J($@$r$7$?$D$b$j(J)
# 3. FAQ $@$N@Z$j=P$7$K(J sed $@$r;H$o$J$$$h$&$K$7$?!#(J
# 4. $@%j%=!<%9$K$h$k%+%9%?%^%$%:(J
# 5. $@%U%!%$%k%;%l%/%?(J
#    ($@%U%!%$%k%;%l%/%?$O(J xf $@$N(J template $@$r;H$C$F$$$^$9!#(J)
# 6. SGML$@%?%0$N1#JC!"?'$D$-I=<((J
#
# faqbro: SGML$@7A<0(J FAQ $@%V%i%&%6(J Ver0.1 1994-05-04
#################################################################

# procedure to show window .
proc ShowWindow. {args} {# xf ignore me 7

  # Window manager configurations
  global tkVersion
  wm positionfrom . user
  wm sizefrom . user
  wm grid . 80 24 7 14
  wm maxsize . 1112 840
  wm minsize . 0 0
  wm title . {faqbro}


  # build widget .frame
  frame .frame

  # build widget .frame.scrollbar2
  scrollbar .frame.scrollbar2 \
    -command {.frame.listbox1 yview} \
    -relief {raised}

  # build widget .frame.scrollbar3
  scrollbar .frame.scrollbar3 \
    -command {.frame.listbox1 xview} \
    -orient {horizontal} \
    -relief {raised}

  # build widget .frame.label0
  label .frame.label0 \
    -font {-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1} \
    -relief {raised} \
    -text {Item}

  # build widget .frame.listbox1
  listbox .frame.listbox1 \
    -exportselection {0} \
    -geometry {14x10} \
    -relief {raised} \
    -xscrollcommand {.frame.scrollbar3 set} \
    -yscrollcommand {.frame.scrollbar2 set}
  # bindings
  bind .frame.listbox1 <B1-Motion> {%W select from [%W nearest %y]}
  bind .frame.listbox1 <Button-1> {%W select from [%W nearest %y]}
  bind .frame.listbox1 <ButtonRelease-1> {SelectItem [%W nearest %y]}
  bind .frame.listbox1 <Shift-B1-Motion> {%W select from [%W nearest %y]}
  bind .frame.listbox1 <Shift-Button-1> {%W select from [%W nearest %y]}

  # pack widget .frame
  pack append .frame \
    .frame.label0 {top frame center fillx} \
    .frame.scrollbar2 {left frame center filly} \
    .frame.listbox1 {top frame center expand fill} \
    .frame.scrollbar3 {bottom frame center fillx}

  # build widget .frame0
  frame .frame0 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame0.frame
  frame .frame0.frame

  # build widget .frame0.frame.scrollbar2
  scrollbar .frame0.frame.scrollbar2 \
    -command {.frame0.frame.listbox1 yview} \
    -relief {raised}

  # build widget .frame0.frame.scrollbar3
  scrollbar .frame0.frame.scrollbar3 \
    -command {.frame0.frame.listbox1 xview} \
    -orient {horizontal} \
    -relief {raised}

  # build widget .frame0.frame.label1
  label .frame0.frame.label1 \
    -font {-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1} \
    -relief {raised} \
    -text {Chapter}

  # build widget .frame0.frame.listbox1
  listbox .frame0.frame.listbox1 \
    -exportselection {0} \
    -geometry {3x3} \
    -relief {raised} \
    -xscrollcommand {.frame0.frame.scrollbar3 set} \
    -yscrollcommand {.frame0.frame.scrollbar2 set}
  # bindings
  bind .frame0.frame.listbox1 <B1-Motion> {%W select from [%W nearest %y]}
  bind .frame0.frame.listbox1 <Button-1> {%W select from [%W nearest %y]}
  bind .frame0.frame.listbox1 <ButtonRelease-1> {SelectChapter [%W nearest %y]}
  bind .frame0.frame.listbox1 <Shift-B1-Motion> {%W select from [%W nearest %y]}
  bind .frame0.frame.listbox1 <Shift-Button-1> {%W select from [%W nearest %y]}

  # pack widget .frame0.frame
  pack append .frame0.frame \
    .frame0.frame.label1 {top frame center fillx} \
    .frame0.frame.scrollbar2 {left frame center filly} \
    .frame0.frame.listbox1 {top frame center fillx} \
    .frame0.frame.scrollbar3 {bottom frame center fillx}

  # build widget .frame0.frame2
  frame .frame0.frame2

  # build widget .frame0.frame2.scrollbar2
  scrollbar .frame0.frame2.scrollbar2 \
    -command {.frame0.frame2.listbox1 yview} \
    -relief {raised}

  # build widget .frame0.frame2.scrollbar3
  scrollbar .frame0.frame2.scrollbar3 \
    -command {.frame0.frame2.listbox1 xview} \
    -orient {horizontal} \
    -relief {raised}

  # build widget .frame0.frame2.label0
  label .frame0.frame2.label0 \
    -font {-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1} \
    -relief {raised} \
    -text {Section}

  # build widget .frame0.frame2.listbox1
  listbox .frame0.frame2.listbox1 \
    -exportselection {0} \
    -geometry {3x3} \
    -relief {raised} \
    -xscrollcommand {.frame0.frame2.scrollbar3 set} \
    -yscrollcommand {.frame0.frame2.scrollbar2 set}
  # bindings
  bind .frame0.frame2.listbox1 <B1-Motion> {%W select from [%W nearest %y]}
  bind .frame0.frame2.listbox1 <Button-1> {%W select from [%W nearest %y]}
  bind .frame0.frame2.listbox1 <ButtonRelease-1> {SelectSection [%W nearest %y]}
  bind .frame0.frame2.listbox1 <Shift-B1-Motion> {%W select from [%W nearest %y]}
  bind .frame0.frame2.listbox1 <Shift-Button-1> {%W select from [%W nearest %y]}

  # pack widget .frame0.frame2
  pack append .frame0.frame2 \
    .frame0.frame2.label0 {top frame center fillx} \
    .frame0.frame2.scrollbar2 {left frame center filly} \
    .frame0.frame2.listbox1 {top frame center fillx} \
    .frame0.frame2.scrollbar3 {bottom frame center fillx}

  # pack widget .frame0
  pack append .frame0 \
    .frame0.frame {left frame center expand fillx} \
    .frame0.frame2 {left frame center expand fillx}

  # build widget .frame1
  frame .frame1 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame1.scrollbar1
  scrollbar .frame1.scrollbar1 \
    -command {.frame1.text2 yview} \
    -relief {raised}

  # build widget .frame1.label2
  label .frame1.label2 \
    -font {-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1} \
    -relief {raised} \
    -text {FAQ}

  # build widget .frame1.text2
  text .frame1.text2 \
    -borderwidth {2} \
    -exportselection {0} \
    -relief {raised} \
    -wrap {word} \
    -yscrollcommand {.frame1.scrollbar1 set}
  # bindings
  bind .frame1.text2 <Any-Key> {NoFunction}

  # pack widget .frame1
  pack append .frame1 \
    .frame1.label2 {top frame center fillx} \
    .frame1.scrollbar1 {left frame center filly} \
    .frame1.text2 {top frame center expand fill}

  # build widget .frame2
  frame .frame2 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame2.menubutton0
  menubutton .frame2.menubutton0 \
    -background {#fdf3adf4adf4} \
    -font {-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1} \
    -foreground {#000000000000} \
    -menu {.frame2.menubutton0.m} \
    -relief {raised} \
    -text {File} \
    -underline {0}

  # build widget .frame2.menubutton0.m
  menu .frame2.menubutton0.m \
    -activeborderwidth {2} \
    -font {-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1}
  .frame2.menubutton0.m add command \
    -command {FSBox {Select File:} {} {SourceFile $fsBox(path)/$fsBox(name)} {}} \
    -label {Load File} \
    -underline {0}
  .frame2.menubutton0.m add separator
  .frame2.menubutton0.m add command \
    -command {destroy .} \
    -label {Quit program} \
    -underline {0}

  # build widget .frame2.menubutton1
  menubutton .frame2.menubutton1 \
    -background {#fdf3adf4adf4} \
    -font {-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1} \
    -foreground {#000000000000} \
    -menu {.frame2.menubutton1.m} \
    -relief {raised} \
    -text {Select} \
    -underline {0}

  # build widget .frame2.menubutton1.m
  menu .frame2.menubutton1.m \
    -activeborderwidth {2} \
    -font {-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1}
  .frame2.menubutton1.m add radiobutton \
    -command {RepackChapSect} \
    -label {by Chapter&Section} \
    -value {ChapSect} \
    -variable {SelectStat} \
    -underline {3}
  .frame2.menubutton1.m add radiobutton \
    -command {RepackItem} \
    -label {by Item} \
    -value {Item} \
    -variable {SelectStat} \
    -underline {3}

  # pack widget .frame2
  pack append .frame2 \
    .frame2.menubutton0 {left frame center} \
    .frame2.menubutton1 {left frame center padx 32}

  # build widget .frame3
  frame .frame3

  # build widget .frame3.scrollbar2
  scrollbar .frame3.scrollbar2 \
    -command {.frame3.listbox1 yview} \
    -relief {raised}

  # build widget .frame3.scrollbar3
  scrollbar .frame3.scrollbar3 \
    -command {.frame3.listbox1 xview} \
    -orient {horizontal} \
    -relief {raised}

  # build widget .frame3.label1
  label .frame3.label1 \
    -font {-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1} \
    -relief {raised} \
    -text {Subject}

  # build widget .frame3.listbox1
  listbox .frame3.listbox1 \
    -exportselection {0} \
    -geometry {14x6} \
    -relief {raised} \
    -xscrollcommand {.frame3.scrollbar3 set} \
    -yscrollcommand {.frame3.scrollbar2 set}
  # bindings
  bind .frame3.listbox1 <B1-Motion> {%W select from [%W nearest %y]}
  bind .frame3.listbox1 <Button-1> {%W select from [%W nearest %y]}
  bind .frame3.listbox1 <ButtonRelease-1> {SelectSubject [%W nearest %y]}
  bind .frame3.listbox1 <Shift-B1-Motion> {%W select from [%W nearest %y]}
  bind .frame3.listbox1 <Shift-Button-1> {%W select from [%W nearest %y]}

  # pack widget .frame3
  pack append .frame3 \
    .frame3.label1 {top frame center fillx} \
    .frame3.scrollbar2 {left frame center filly} \
    .frame3.listbox1 {top frame center expand fill} \
    .frame3.scrollbar3 {bottom frame center fillx}

  # pack widget .
  pack append . \
    .frame2 {top frame center fillx} \
    .frame0 {top frame center fillx} \
    .frame3 {top frame center fillx} \
    .frame1 {top frame center expand fill}

  global tkVersion
  if {$tkVersion >= 3.0} {
    tk_menuBar .frame2 .frame2.menubutton0 .frame2.menubutton1
  } {
    tk_menus . .frame2.menubutton0 .frame2.menubutton1
  }


  if {"[info procs XFEdit]" != ""} {
    catch "XFMiscBindWidgetTree .xfFSBox"
    after 2 "catch {XFEditSetShowWindows}"
  }
}



# Procedure: DispFaq
proc DispFaq { asubject} {
    global sgmf sgmfd
    global substart subend
    global curchapnum cursectnum cursubnum curitemnum cursubnum2
    global SelectStat
    global item itemlist

    if {"$SelectStat" == "ChapSect"} {
	if {![info exists substart($curchapnum:$cursectnum:$cursubnum)]} {
	    [SN faq] delete 0.0 end
	    return
	}
	set st $substart($curchapnum:$cursectnum:$cursubnum)
	set ed $subend($curchapnum:$cursectnum:$cursubnum)
    } else {
	set idx [lindex $item([lindex $itemlist $curitemnum]) $asubject]
	set st $substart($idx)
	set ed $subend($idx)
    }

    set t [SN faq]
    $t delete 0.0 end

    set inQ 0
    set inA 0
    set inC 0

    seek $sgmfd $st
    set text [split [read $sgmfd [expr $ed-$st]] \n]

    foreach tt "$text" {
	if {[regexp {^<([/]?[A-Za-z]*)>(.*)} "$tt" dumy tag ii]} {
	    case $tag in {
		Q {
		    set inQ 1
		    InsertTextTag $t "Question.\n" font1
		}
		A {
		    set inA 1
		    InsertTextTag $t "Answer.\n" font1
		}
		C {
		    set inC 1
		    InsertTextTag $t "Comment.\n" font1
		}
		FIX {
		    InsertTextTag $t "\n"
		}
		/Q {
		    set inQ 0
		    InsertTextTag $t "\n"
		    continue
		}
		/A {
		    set inA 0
		    InsertTextTag $t "\n"
		    continue
		}
		/C {
		    set inC 0
		    InsertTextTag $t "\n"
		    continue
		}
		/FIX {
		    InsertTextTag $t "\n"
		}
		Poster {InsertTextTag $t "Poster: " font1}
		Date {InsertTextTag $t "Date: " font1}
		Item {InsertTextTag $t "Item: " font1}
		Subject {continue}
		default {
		    puts stderr "unexpected sgml tag in $curchapnum:$cursectnum:$cursubnum"
		    puts stderr "tag: $tag"
		}
	    }
	    set ii [string trim "$ii"]
	    if {$ii != ""} {
		if {$inQ} {
		    InsertTextTag $t "$ii\n" colorQ
		} else {
		if {$inA} {
		    InsertTextTag $t "$ii\n" colorA
		} else {
		if {$inC} {
		    InsertTextTag $t "$ii\n" colorC
		} else {
		    InsertTextTag $t "$ii\n"
		}}}
	    }
	    continue
	}
	if {$inQ} {
	    InsertTextTag $t "$tt\n" colorQ
	    continue
	}
	if {$inA} {
	    InsertTextTag $t "$tt\n" colorA
	    continue
	}
	if {$inC} {
	    InsertTextTag $t "$tt\n" colorC
	    continue
	}
	if {[string trim $tt] != ""} {
	    puts stderr	 "unexpected format in $curchapnum:$cursectnum:$cursubnum"
	    puts stderr "text: $tt"
	}
    }
}


# Procedure: DispSection
proc DispSection { achapter} {
    global section
    global curchapnum cursectnum

    set l [SN section]
    $l delete 0 end
    for {set i 0} {[info exists section($curchapnum:$i)]} {incr i} {
	$l insert end $section($curchapnum:$i)
    }
}


# Procedure: DispSubject
proc DispSubject { asection} {
    global subject
    global curchapnum cursectnum
    global item itemlist
    global SelectStat

    if {"$SelectStat" == "ChapSect"} {
	set l [SN subject]
	$l delete 0 end
	for {set i 0} {[info exists subject($curchapnum:$cursectnum:$i)]} {incr i} {
	    $l insert end $subject($curchapnum:$cursectnum:$i)
	}
    } else {
	set aitem $asection
	set s [SN subject]
	$s delete 0 end
	foreach i $item([lindex $itemlist $aitem]) {
	    $s insert end $subject($i)
	}
    }
}

# Procedure: FSBox
proc FSBox { {fsBoxMessage "Select file:"} {fsBoxFileName ""} {fsBoxActionOk ""} {fsBoxActionCancel ""}} {
# xf ignore me 5
##########
# Procedure: FSBox
# Description: show file selector box
# Arguments: fsBoxMessage - the text to display
#            fsBoxFileName - a file name that should be selected
#            fsBoxActionOk - the action that should be performed on ok
#            fsBoxActionCancel - the action that should be performed on cancel
# Returns: the filename that was selected, or nothing
# Sideeffects: none
##########
# 
# global fsBox(activeBackground) - active background color
# global fsBox(activeForeground) - active foreground color
# global fsBox(background) - background color
# global fsBox(font) - text font
# global fsBox(foreground) - foreground color
# global fsBox(extensions) - scan directory for extensions
# global fsBox(scrollActiveForeground) - scrollbar active background color
# global fsBox(scrollBackground) - scrollbar background color
# global fsBox(scrollForeground) - scrollbar foreground color
# global fsBox(scrollSide) - side where scrollbar is located

  global fsBox

  set tmpButtonOpt ""
  set tmpFrameOpt ""
  set tmpMessageOpt ""
  set tmpScaleOpt ""
  set tmpScrollOpt ""
  if {"$fsBox(activeBackground)" != ""} {
    append tmpButtonOpt "-activebackground \"$fsBox(activeBackground)\" "
  }
  if {"$fsBox(activeForeground)" != ""} {
    append tmpButtonOpt "-activeforeground \"$fsBox(activeForeground)\" "
  }
  if {"$fsBox(background)" != ""} {
    append tmpButtonOpt "-background \"$fsBox(background)\" "
    append tmpFrameOpt "-background \"$fsBox(background)\" "
    append tmpMessageOpt "-background \"$fsBox(background)\" "
  }
  if {"$fsBox(font)" != ""} {
    append tmpButtonOpt "-font \"$fsBox(font)\" "
    append tmpMessageOpt "-font \"$fsBox(font)\" "
  }
  if {"$fsBox(foreground)" != ""} {
    append tmpButtonOpt "-foreground \"$fsBox(foreground)\" "
    append tmpMessageOpt "-foreground \"$fsBox(foreground)\" "
  }
  if {"$fsBox(scrollActiveForeground)" != ""} {
    append tmpScrollOpt "-activeforeground \"$fsBox(scrollActiveForeground)\" "
  }
  if {"$fsBox(scrollBackground)" != ""} {
    append tmpScrollOpt "-background \"$fsBox(scrollBackground)\" "
  }
  if {"$fsBox(scrollForeground)" != ""} {
    append tmpScrollOpt "-foreground \"$fsBox(scrollForeground)\" "
  }

  if {[file exists [file tail $fsBoxFileName]] &&
      [IsAFile [file tail $fsBoxFileName]]} {
    set fsBox(name) [file tail $fsBoxFileName]
  } {
    set fsBox(name) ""
  }
  if {[file exists $fsBoxFileName] && [IsADir $fsBoxFileName]} {
    set fsBox(path) $fsBoxFileName
  } {
    if {"[file rootname $fsBoxFileName]" != "."} {
      set fsBox(path) [file rootname $fsBoxFileName]
    }
  }
  if {$fsBox(showPixmap)} {
    set fsBox(path) [string trimleft $fsBox(path) @]
  }
  if {"$fsBox(path)" != "" && [file exists $fsBox(path)] &&
      [IsADir $fsBox(path)]} {
    set fsBox(internalPath) $fsBox(path)
  } {
    if {"$fsBox(internalPath)" == "" ||
        ![file exists $fsBox(internalPath)]} {
      set fsBox(internalPath) [pwd]
    }
  }
  # build widget structure

  # start build of toplevel
  if {"[info commands XFDestroy]" != ""} {
    catch {XFDestroy .fsBox}
  } {
    catch {destroy .fsBox}
  }
  toplevel .fsBox  -borderwidth 0
  catch ".fsBox config $tmpFrameOpt"
  wm geometry .fsBox 350x300 
  wm title .fsBox {File select box}
  wm maxsize .fsBox 1000 1000
  wm minsize .fsBox 100 100
  # end build of toplevel

  label .fsBox.message1  -anchor c  -relief raised  -text "$fsBoxMessage"
  catch ".fsBox.message1 config $tmpMessageOpt"

  frame .fsBox.frame1  -borderwidth 0  -relief raised
  catch ".fsBox.frame1 config $tmpFrameOpt"

  button .fsBox.frame1.ok  -text "OK"  -command "
      global fsBox
      set fsBox(name) \[.fsBox.file.file get\]
      if {$fsBox(showPixmap)} {
        set fsBox(path) @\[.fsBox.path.path get\]
      } {
        set fsBox(path) \[.fsBox.path.path get\]
      }
      set fsBox(internalPath) \[.fsBox.path.path get\]
      $fsBoxActionOk
      if {\"\[info commands XFDestroy\]\" != \"\"} {
        catch {XFDestroy .fsBox}
      } {
        catch {destroy .fsBox}
      }"
  catch ".fsBox.frame1.ok config $tmpButtonOpt"

  button .fsBox.frame1.rescan  -text "Rescan"  -command {
      global fsBox
      FSBoxFSShow [.fsBox.path.path get]  [.fsBox.pattern.pattern get] $fsBox(all)}
  catch ".fsBox.frame1.rescan config $tmpButtonOpt"

  button .fsBox.frame1.cancel  -text "Cancel"  -command "
      global fsBox
      set fsBox(name) {}
      set fsBox(path) {}
      $fsBoxActionCancel
      if {\"\[info commands XFDestroy\]\" != \"\"} {
        catch {XFDestroy .fsBox}
      } {
        catch {destroy .fsBox}
      }"
  catch ".fsBox.frame1.cancel config $tmpButtonOpt"

  if {$fsBox(showPixmap)} {
    frame .fsBox.frame2  -borderwidth 0  -relief raised
    catch ".fsBox.frame2 config $tmpFrameOpt"

    scrollbar .fsBox.frame2.scrollbar3  -command {.fsBox.frame2.canvas2 xview}  -orient {horizontal}  -relief {raised}
    catch ".fsBox.frame2.scrollbar3 config $tmpScrollOpt"

    scrollbar .fsBox.frame2.scrollbar1  -command {.fsBox.frame2.canvas2 yview}  -relief {raised}
    catch ".fsBox.frame2.scrollbar1 config $tmpScrollOpt"

    canvas .fsBox.frame2.canvas2  -confine {true}  -relief {raised}  -scrollregion {0c 0c 20c 20c}  -width {100}  -xscrollcommand {.fsBox.frame2.scrollbar3 set}  -yscrollcommand {.fsBox.frame2.scrollbar1 set}
    catch ".fsBox.frame2.canvas2 config $tmpFrameOpt"

    .fsBox.frame2.canvas2 addtag currentBitmap withtag [.fsBox.frame2.canvas2 create bitmap 5 5 -anchor nw]
  }

  frame .fsBox.path  -borderwidth 0  -relief raised
  catch ".fsBox.path config $tmpFrameOpt"

  frame .fsBox.path.paths  -borderwidth 2  -relief raised
  catch ".fsBox.path.paths config $tmpFrameOpt"

  menubutton .fsBox.path.paths.paths  -borderwidth 0  -menu ".fsBox.path.paths.paths.menu"  -relief flat  -text "Pathname:"
  catch ".fsBox.path.paths.paths config $tmpButtonOpt"

  menu .fsBox.path.paths.paths.menu
  catch ".fsBox.path.paths.paths.menu config $tmpButtonOpt"

  .fsBox.path.paths.paths.menu add command  -label "[string trimright $fsBox(internalPath) {/@}]"  -command "
       global fsBox
       FSBoxFSShow \[.fsBox.path.path get\]  \[.fsBox.pattern.pattern get\] \$fsBox(all)
       .fsBox.path.path delete 0 end
       .fsBox.path.path insert 0 [string trimright $fsBox(internalPath) {/@}]"

  entry .fsBox.path.path  -relief raised
  catch ".fsBox.path.path config $tmpMessageOpt"

  if {![IsADir $fsBox(internalPath)]} {
    set $fsBox(internalPath) [pwd]
  }
  .fsBox.path.path insert 0 $fsBox(internalPath)

  frame .fsBox.pattern  -borderwidth 0  -relief raised
  catch ".fsBox.pattern config $tmpFrameOpt"

  frame .fsBox.pattern.patterns  -borderwidth 2  -relief raised
  catch ".fsBox.pattern.patterns config $tmpFrameOpt"

  menubutton .fsBox.pattern.patterns.patterns  -borderwidth 0  -menu ".fsBox.pattern.patterns.patterns.menu"  -relief flat  -text "Selection pattern:"
  catch ".fsBox.pattern.patterns.patterns config $tmpButtonOpt"

  menu .fsBox.pattern.patterns.patterns.menu
  catch ".fsBox.pattern.patterns.patterns.menu config $tmpButtonOpt"

  .fsBox.pattern.patterns.patterns.menu add checkbutton  -label "Scan extensions"  -variable fsBoxExtensions  -command {
      global fsBox
      FSBoxFSShow [.fsBox.path.path get]  [.fsBox.pattern.pattern get] $fsBox(all)}

  entry .fsBox.pattern.pattern  -relief raised
  catch ".fsBox.pattern.pattern config $tmpMessageOpt"

  .fsBox.pattern.pattern insert 0 $fsBox(pattern)
  
  frame .fsBox.files  -borderwidth 0  -relief raised
  catch ".fsBox.files config $tmpFrameOpt"

  scrollbar .fsBox.files.vscroll  -relief raised  -command ".fsBox.files.files yview"
  catch ".fsBox.files.vscroll config $tmpScrollOpt"

  scrollbar .fsBox.files.hscroll  -orient horiz  -relief raised  -command ".fsBox.files.files xview"
  catch ".fsBox.files.hscroll config $tmpScrollOpt"

  listbox .fsBox.files.files  -exportselection false  -relief raised  -xscrollcommand ".fsBox.files.hscroll set"  -yscrollcommand ".fsBox.files.vscroll set"
  catch ".fsBox.files.files config $tmpMessageOpt"

  frame .fsBox.file  -borderwidth 0  -relief raised
  catch ".fsBox.file config $tmpFrameOpt"

  label .fsBox.file.labelfile  -relief raised  -text "Filename:"
  catch ".fsBox.file.labelfile config $tmpMessageOpt"

  entry .fsBox.file.file  -relief raised
  catch ".fsBox.file.file config $tmpMessageOpt"

  .fsBox.file.file delete 0 end
  .fsBox.file.file insert 0 $fsBox(name)
  
  checkbutton .fsBox.pattern.all  -offvalue 0  -onvalue 1  -text "Show all files"  -variable fsBox(all)  -command {
      global fsBox
      FSBoxFSShow [.fsBox.path.path get]  [.fsBox.pattern.pattern get] $fsBox(all)}
  catch ".fsBox.pattern.all config $tmpButtonOpt"

  FSBoxFSShow $fsBox(internalPath) $fsBox(pattern) $fsBox(all)

  # bindings
  bind .fsBox.files.files <Double-Button-1> "
    FSBoxFSFileSelectDouble %W $fsBox(showPixmap) \{$fsBoxActionOk\} %y"
  bind .fsBox.files.files <ButtonPress-1> "
    FSBoxFSFileSelect %W $fsBox(showPixmap) %y"
  bind .fsBox.files.files <Button1-Motion> "
    FSBoxFSFileSelect %W $fsBox(showPixmap) %y"
  bind .fsBox.files.files <Shift-Button1-Motion> "
    FSBoxFSFileSelect %W $fsBox(showPixmap) %y"
  bind .fsBox.files.files <Shift-ButtonPress-1> "
    FSBoxFSFileSelect %W $fsBox(showPixmap) %y"

  bind .fsBox.path.path <Tab> {
    FSBoxFSNameComplete path}
  bind .fsBox.path.path <Return> {
    global tkVersion
    global fsBox
    FSBoxFSShow [.fsBox.path.path get]  [.fsBox.pattern.pattern get] $fsBox(all)
    FSBoxFSInsertPath
    if {$tkVersion >= 3.0} {
      .fsBox.file.file icursor end
    } {
      .fsBox.file.file cursor end
    }
    focus .fsBox.file.file}
  catch "bind .fsBox.path.path <Up> {}"
  bind .fsBox.path.path <Down> {
    global tkVersion
    if {$tkVersion >= 3.0} {
      .fsBox.file.file icursor end
    } {
      .fsBox.file.file cursor end
    }
    focus .fsBox.file.file}

  bind .fsBox.file.file <Tab> {
    FSBoxFSNameComplete file}
  bind .fsBox.file.file <Return> "
    global fsBox
    set fsBox(name) \[.fsBox.file.file get\]
    if {$fsBox(showPixmap)} {
      set fsBox(path) @\[.fsBox.path.path get\]
    } {
      set fsBox(path) \[.fsBox.path.path get\]
    }
    set fsBox(internalPath) \[.fsBox.path.path get\]
    $fsBoxActionOk
    if {\"\[info commands XFDestroy\]\" != \"\"} {
      catch {XFDestroy .fsBox}
    } {
      catch {destroy .fsBox}
    }"
  bind .fsBox.file.file <Up> {
    global tkVersion
    if {$tkVersion >= 3.0} {
      .fsBox.path.path icursor end
    } {
      .fsBox.path.path cursor end
    }
    focus .fsBox.path.path}
  bind .fsBox.file.file <Down> {
    global tkVersion
    if {$tkVersion >= 3.0} {
      .fsBox.pattern.pattern icursor end
    } {
      .fsBox.pattern.pattern cursor end
    }
    focus .fsBox.pattern.pattern}

  bind .fsBox.pattern.pattern <Return> {
    global fsBox
    FSBoxFSShow [.fsBox.path.path get]  [.fsBox.pattern.pattern get] $fsBox(all)}
  bind .fsBox.pattern.pattern <Up> {
    global tkVersion
    if {$tkVersion >= 3.0} {
      .fsBox.file.file icursor end
    } {
      .fsBox.file.file cursor end
    }
    focus .fsBox.file.file}
  catch "bind .fsBox.pattern.pattern <Down> {}"

  # packing
  pack append .fsBox.files  .fsBox.files.vscroll "$fsBox(scrollSide) filly"  .fsBox.files.hscroll {bottom fillx}  .fsBox.files.files {left fill expand}
  pack append .fsBox.file  .fsBox.file.labelfile {left}  .fsBox.file.file {left fill expand}
  pack append .fsBox.frame1  .fsBox.frame1.ok {left fill expand}  .fsBox.frame1.rescan {left fill expand}  .fsBox.frame1.cancel {left fill expand}
  pack append .fsBox.path.paths  .fsBox.path.paths.paths {left}
  pack append .fsBox.pattern.patterns  .fsBox.pattern.patterns.patterns {left}
  pack append .fsBox.path  .fsBox.path.paths {left}  .fsBox.path.path {left fill expand}
  pack append .fsBox.pattern  .fsBox.pattern.patterns {left}  .fsBox.pattern.all {right fill}  .fsBox.pattern.pattern {left fill expand}
  if {$fsBox(showPixmap)} {
    pack append .fsBox.frame2  .fsBox.frame2.scrollbar1 {left filly}  .fsBox.frame2.canvas2 {top expand fill}  .fsBox.frame2.scrollbar3 {top fillx} 

    pack append .fsBox  .fsBox.message1 {top fill}  .fsBox.frame1 {bottom fill}  .fsBox.pattern {bottom fill}  .fsBox.file {bottom fill}  .fsBox.path {bottom fill}  .fsBox.frame2 {right fill}  .fsBox.files {left fill expand}
  } {
    pack append .fsBox  .fsBox.message1 {top fill}  .fsBox.frame1 {bottom fill}  .fsBox.pattern {bottom fill}  .fsBox.file {bottom fill}  .fsBox.path {bottom fill}  .fsBox.files {left fill expand}
  }

  if {"$fsBoxActionOk" == "" && "$fsBoxActionCancel" == ""} {
    # wait for the box to be destroyed
    update idletask
    grab .fsBox
    tkwait window .fsBox

    if {"[string trim $fsBox(path)]" != "" ||
        "[string trim $fsBox(name)]" != ""} {
      if {"[string trimleft [string trim $fsBox(name)] /]" == ""} {
        return [string trimright [string trim $fsBox(path)] /]
      } {
        return [string trimright [string trim $fsBox(path)] /]/[string trimleft [string trim $fsBox(name)] /]
      }
    }
  }
}


# Procedure: FSBoxBindSelectOne
proc FSBoxBindSelectOne { fsBoxW fsBoxY} {
# xf ignore me 6

  set fsBoxNearest [$fsBoxW nearest $fsBoxY]
  if {$fsBoxNearest >= 0} {
    $fsBoxW select from $fsBoxNearest
    $fsBoxW select to $fsBoxNearest
  }
}


# Procedure: FSBoxFSFileSelect
proc FSBoxFSFileSelect { fsBoxW fsBoxShowPixmap fsBoxY} {
# xf ignore me 6
  global fsBox

  FSBoxBindSelectOne $fsBoxW $fsBoxY
  set fsBoxNearest [$fsBoxW nearest $fsBoxY]
  if {$fsBoxNearest >= 0} {
    set fsBoxTmpEntry [$fsBoxW get $fsBoxNearest]
    if {"[string index $fsBoxTmpEntry  [expr [string length $fsBoxTmpEntry]-1]]" == "/" ||
        "[string index $fsBoxTmpEntry  [expr [string length $fsBoxTmpEntry]-1]]" == "@"} {
      set fsBoxFileName [string range $fsBoxTmpEntry 0  [expr [string length $fsBoxTmpEntry]-2]]
      if {![IsADir [string trimright $fsBox(internalPath)/$fsBoxFileName @]] &&
          ![IsASymlink [string trimright $fsBox(internalPath)/$fsBoxFileName @]]} {
        set fsBoxFileName $fsBoxTmpEntry
      }
    } {
      if {"[string index $fsBoxTmpEntry  [expr [string length $fsBoxTmpEntry]-1]]" == "*"} {
        set fsBoxFileName [string range $fsBoxTmpEntry 0  [expr [string length $fsBoxTmpEntry]-2]]
        if {![file executable $fsBox(internalPath)/$fsBoxFileName]} {
          set fsBoxFileName $fsBoxTmpEntry
        }
      } {
        set fsBoxFileName $fsBoxTmpEntry
      }
    }
    if {![IsADir [string trimright $fsBox(internalPath)/$fsBoxFileName @]]} {
      set fsBox(name) $fsBoxFileName
      .fsBox.file.file delete 0 end
      .fsBox.file.file insert 0 $fsBox(name)
      if {$fsBoxShowPixmap} {
        catch ".fsBox.frame2.canvas2 itemconfigure currentBitmap -bitmap \"@$fsBox(internalPath)/$fsBox(name)\""
      }
    }
  }
}


# Procedure: FSBoxFSFileSelectDouble
proc FSBoxFSFileSelectDouble { fsBoxW fsBoxShowPixmap fsBoxAction fsBoxY} {
# xf ignore me 6
  global fsBox

  FSBoxBindSelectOne $fsBoxW $fsBoxY
  set fsBoxNearest [$fsBoxW nearest $fsBoxY]
  if {$fsBoxNearest >= 0} {
    set fsBoxTmpEntry [$fsBoxW get $fsBoxNearest]
    if {"$fsBoxTmpEntry" == "../"} {
      set fsBoxTmpEntry [string trimright [string trim $fsBox(internalPath)] "@/"]
      if {"$fsBoxTmpEntry" == ""} {
        return
      }
      FSBoxFSShow [file dirname $fsBoxTmpEntry]  [.fsBox.pattern.pattern get] $fsBox(all)
      .fsBox.path.path delete 0 end
      .fsBox.path.path insert 0 $fsBox(internalPath)
    } {
      if {"[string index $fsBoxTmpEntry  [expr [string length $fsBoxTmpEntry]-1]]" == "/" ||
          "[string index $fsBoxTmpEntry  [expr [string length $fsBoxTmpEntry]-1]]" == "@"} {
        set fsBoxFileName [string range $fsBoxTmpEntry 0  [expr [string length $fsBoxTmpEntry]-2]]
        if {![IsADir [string trimright $fsBox(internalPath)/$fsBoxFileName @]] &&
            ![IsASymlink [string trimright $fsBox(internalPath)/$fsBoxFileName @]]} {
          set fsBoxFileName $fsBoxTmpEntry
        }
      } {
        if {"[string index $fsBoxTmpEntry  [expr [string length $fsBoxTmpEntry]-1]]" == "*"} {
          set fsBoxFileName [string range $fsBoxTmpEntry 0  [expr [string length $fsBoxTmpEntry]-2]]
          if {![file executable $fsBox(internalPath)/$fsBoxFileName]} {
            set fsBoxFileName $fsBoxTmpEntry
          }
        } {
          set fsBoxFileName $fsBoxTmpEntry
        }
      }
      if {[IsADir [string trimright $fsBox(internalPath)/$fsBoxFileName @]]} {
        set fsBox(internalPath) "[string trimright $fsBox(internalPath) {/@}]/$fsBoxFileName"
        FSBoxFSShow $fsBox(internalPath)  [.fsBox.pattern.pattern get] $fsBox(all)
        .fsBox.path.path delete 0 end
        .fsBox.path.path insert 0 $fsBox(internalPath)
      } {
        set fsBox(name) $fsBoxFileName
        if {$fsBoxShowPixmap} {
          set fsBox(path) @$fsBox(internalPath)
        } {
          set fsBox(path) $fsBox(internalPath)
        }
        if {"$fsBoxAction" != ""} {
          eval "global fsBox; $fsBoxAction"
        }
        if {"[info commands XFDestroy]" != ""} {
          catch {XFDestroy .fsBox}
        } {
          catch {destroy .fsBox}
        }
      }
    }
  }
}


# Procedure: FSBoxFSInsertPath
proc FSBoxFSInsertPath {} {
# xf ignore me 6
  global fsBox

  set fsBoxLast [.fsBox.path.paths.paths.menu index last]
  set fsBoxNewEntry [string trimright [.fsBox.path.path get] "/@"]
  for {set fsBoxCounter 0} {$fsBoxCounter <= $fsBoxLast} {incr fsBoxCounter 1} {
    if {"$fsBoxNewEntry" ==  "[lindex [.fsBox.path.paths.paths.menu entryconfigure  $fsBoxCounter -label] 4]"} {
      return
    }
  }
  if {$fsBoxLast < 9} {
    .fsBox.path.paths.paths.menu add command  -label "$fsBoxNewEntry"  -command "
        global fsBox
        FSBoxFSShow $fsBoxNewEntry  \[.fsBox.pattern.pattern get\] \$fsBox(all)
        .fsBox.path.path delete 0 end
        .fsBox.path.path insert 0 $fsBoxNewEntry"
  } {
    for {set fsBoxCounter 0} {$fsBoxCounter < $fsBoxLast} {incr fsBoxCounter 1} {
      .fsBox.path.paths.paths.menu entryconfigure  $fsBoxCounter -label  [lindex [.fsBox.path.paths.paths.menu entryconfigure  [expr $fsBoxCounter+1] -label] 4]
      .fsBox.path.paths.paths.menu entryconfigure $fsBoxCounter  -command "
          global fsBox
          FSBoxFSShow [lindex [.fsBox.path.paths.paths.menu entryconfigure  [expr $fsBoxCounter+1] -label] 4]  \[.fsBox.pattern.pattern get\] \$fsBox(all)
          .fsBox.path.path delete 0 end
          .fsBox.path.path insert 0 [lindex  [.fsBox.path.paths.paths.menu entryconfigure  [expr $fsBoxCounter+1] -label] 4]"
    }
    .fsBox.path.paths.paths.menu entryconfigure $fsBoxLast  -label "$fsBoxNewEntry"
    .fsBox.path.paths.paths.menu entryconfigure $fsBoxCounter  -command "
        global fsBox
        FSBoxFSShow \[.fsBox.path.path get\]  \[.fsBox.pattern.pattern get\] \$fsBox(all)
        .fsBox.path.path delete 0 end
        .fsBox.path.path insert 0 $fsBoxNewEntry"
  }
}


# Procedure: FSBoxFSNameComplete
proc FSBoxFSNameComplete { fsBoxType} {
# xf ignore me 6
  global tkVersion
  global fsBox

  set fsBoxNewFile ""
  if {"$fsBoxType" == "path"} {
    set fsBoxDirName [file dirname [.fsBox.path.path get]]
    set fsBoxFileName [file tail [.fsBox.path.path get]]
  } {
    set fsBoxDirName [file dirname [.fsBox.path.path get]/]
    set fsBoxFileName [file tail [.fsBox.file.file get]]
  }

  set fsBoxNewFile ""
  if {[IsADir [string trimright $fsBoxDirName @]]} {
    catch "glob -nocomplain $fsBoxDirName/${fsBoxFileName}*" fsBoxResult
    foreach fsBoxCounter $fsBoxResult {
      if {"$fsBoxNewFile" == ""} {
        set fsBoxNewFile [file tail $fsBoxCounter]
      } {
        if {"[string index [file tail $fsBoxCounter] 0]" !=
            "[string index $fsBoxNewFile 0]"} {
          set fsBoxNewFile ""
          break
        }
        set fsBoxCounter1 0
        set fsBoxTmpFile1 $fsBoxNewFile
        set fsBoxTmpFile2 [file tail $fsBoxCounter]
        set fsBoxLength1 [string length $fsBoxTmpFile1]
        set fsBoxLength2 [string length $fsBoxTmpFile2]
        set fsBoxNewFile ""
        if {$fsBoxLength1 > $fsBoxLength2} {
          set fsBoxLength1 $fsBoxLength2
        }
        while {$fsBoxCounter1 < $fsBoxLength1} {
          if {"[string index $fsBoxTmpFile1 $fsBoxCounter1]" ==  "[string index $fsBoxTmpFile2 $fsBoxCounter1]"} {
            append fsBoxNewFile [string index $fsBoxTmpFile1 $fsBoxCounter1]
          } {
            break
          }
          incr fsBoxCounter1 1
        }
      }
    }
  }
  if {"$fsBoxNewFile" != ""} {
    if {[IsADir [string trimright $fsBoxDirName/$fsBoxNewFile @]] ||
        ![IsAFile [string trimright $fsBoxDirName/$fsBoxNewFile @]]} {
      if {[IsADir [string trimright $fsBoxDirName/$fsBoxNewFile @]]} {
        if {"$fsBoxDirName" == "/"} {
          .fsBox.path.path delete 0 end
          .fsBox.path.path insert 0 "/[string trimright [string trim $fsBoxNewFile /] @]/"
        } {
          .fsBox.path.path delete 0 end
          .fsBox.path.path insert 0 "[string trimright $fsBoxDirName /]/[string trimright [string trim $fsBoxNewFile /] @]/"
        }
        FSBoxFSShow [.fsBox.path.path get]  [.fsBox.pattern.pattern get] $fsBox(all)
        FSBoxFSInsertPath
      } {
        .fsBox.path.path delete 0 end
        .fsBox.path.path insert 0 "[string trimright $fsBoxDirName /]/[string trimright [string trim $fsBoxNewFile /] @]"
      }
    } {
      .fsBox.path.path delete 0 end
      .fsBox.path.path insert 0 "[string trimright $fsBoxDirName {@/}]/"
      .fsBox.file.file delete 0 end
      .fsBox.file.file insert 0 $fsBoxNewFile
      if {$tkVersion >= 3.0} {
        .fsBox.file.file icursor end
      } {
        .fsBox.file.file cursor end
      }
      focus .fsBox.file.file
    }
  }
}


# Procedure: FSBoxFSShow
proc FSBoxFSShow { fsBoxPath fsBoxPattern fsBoxAll} {
# xf ignore me 6
  global fsBox

  set tmpButtonOpt ""
  if {"$fsBox(activeBackground)" != ""} {
    append tmpButtonOpt "-activebackground \"$fsBox(activeBackground)\" "
  }
  if {"$fsBox(activeForeground)" != ""} {
    append tmpButtonOpt "-activeforeground \"$fsBox(activeForeground)\" "
  }
  if {"$fsBox(background)" != ""} {
    append tmpButtonOpt "-background \"$fsBox(background)\" "
  }
  if {"$fsBox(font)" != ""} {
    append tmpButtonOpt "-font \"$fsBox(font)\" "
  }
  if {"$fsBox(foreground)" != ""} {
    append tmpButtonOpt "-foreground \"$fsBox(foreground)\" "
  }

  set fsBox(pattern) $fsBoxPattern
  if {[file exists $fsBoxPath] && [file readable $fsBoxPath] &&
      [IsADir $fsBoxPath]} {
    set fsBox(internalPath) $fsBoxPath
  } {
    if {[file exists $fsBoxPath] && [file readable $fsBoxPath] &&
        [IsAFile $fsBoxPath]} {
      set fsBox(internalPath) [file dirname $fsBoxPath]
      .fsBox.file.file delete 0 end
      .fsBox.file.file insert 0 [file tail $fsBoxPath]
      set fsBoxPath $fsBox(internalPath)
    } {
      while {"$fsBoxPath" != "" && "$fsBoxPath" != "/" &&
             ![file isdirectory $fsBoxPath]} {
        set fsBox(internalPath) [file dirname $fsBoxPath]
         set fsBoxPath $fsBox(internalPath)
      }
    }
  }
  if {"$fsBoxPath" == ""} {
    set fsBoxPath "/"
    set fsBox(internalPath) "/"
  }
  .fsBox.path.path delete 0 end
  .fsBox.path.path insert 0 $fsBox(internalPath)

  if {[.fsBox.files.files size] > 0} {
    .fsBox.files.files delete 0 end
  }
  if {$fsBoxAll} {
    if {[catch "exec ls -F -a $fsBoxPath" fsBoxResult]} {
      puts stderr "$fsBoxResult"
    }
  } {
    if {[catch "exec ls -F $fsBoxPath" fsBoxResult]} {
      puts stderr "$fsBoxResult"
    }
  }
  set fsBoxElementList [lsort $fsBoxResult]

  foreach fsBoxCounter [winfo children .fsBox.pattern.patterns.patterns] {
    if {[string length [info commands XFDestroy]] > 0} {
      catch {XFDestroy $fsBoxCounter}
    } {
      catch {destroy $fsBoxCounter}
    }
  }
  menu .fsBox.pattern.patterns.patterns.menu
  catch ".fsBox.pattern.patterns.patterns.menu config $tmpButtonOpt"

  if {$fsBox(extensions)} {
    .fsBox.pattern.patterns.patterns.menu add command  -label "*"  -command {
        global fsBox
        set fsBox(pattern) "*"
        .fsBox.pattern.pattern delete 0 end
        .fsBox.pattern.pattern insert 0 $fsBox(pattern)
        FSBoxFSShow [.fsBox.path.path get] $fsBox(pattern)  $fsBox(all)}
  }

  if {"$fsBoxPath" != "/"} {
    .fsBox.files.files insert end "../"
  }
  foreach fsBoxCounter $fsBoxElementList {
    if {[string match $fsBoxPattern $fsBoxCounter] ||
        [IsADir [string trimright $fsBoxPath/$fsBoxCounter "/@"]]} {
      if {"$fsBoxCounter" != "../" &&
          "$fsBoxCounter" != "./"} {
        .fsBox.files.files insert end $fsBoxCounter
      }
    }

    if {$fsBox(extensions)} {
      catch "file rootname $fsBoxCounter" fsBoxRootName
      catch "file extension $fsBoxCounter" fsBoxExtension
      set fsBoxExtension [string trimright $fsBoxExtension "/*@"]
      if {"$fsBoxExtension" != "" && "$fsBoxRootName" != ""} {
        set fsBoxInsert 1
        set fsBoxLast [.fsBox.pattern.patterns.patterns.menu index last]
        for {set fsBoxCounter1 0} {$fsBoxCounter1 <= $fsBoxLast} {incr fsBoxCounter1 1} {
          if {"*$fsBoxExtension" ==  "[lindex [.fsBox.pattern.patterns.patterns.menu entryconfigure  $fsBoxCounter1 -label] 4]"} {
            set fsBoxInsert 0
          }
        }
	if {$fsBoxInsert} {
          .fsBox.pattern.patterns.patterns.menu add command  -label "*$fsBoxExtension"  -command "
              global fsBox
              set fsBox(pattern) \"*$fsBoxExtension\"
              .fsBox.pattern.pattern delete 0 end
              .fsBox.pattern.pattern insert 0 \$fsBox(pattern)
              FSBoxFSShow \[.fsBox.path.path get\] \$fsBox(pattern)  \$fsBox(all)"
        }
      }
    }
  }
  if {$fsBox(extensions)} {
    .fsBox.pattern.patterns.patterns.menu add separator
  }
  if {$fsBox(extensions) || 
      "[.fsBox.pattern.patterns.patterns.menu index last]" == "none"} {
    .fsBox.pattern.patterns.patterns.menu add checkbutton  -label "Scan extensions"  -variable "fsBox(extensions)"  -command {
        global fsBox
        FSBoxFSShow [.fsBox.path.path get]  [.fsBox.pattern.pattern get] $fsBox(all)}
  }
}



# Procedure: InitItem
proc InitItem {} {
    global item itemlist cursubnum2 curitemnum

    set itemlist [lsort [array names item]]
    set l [SN item]
    $l delete 0 end
    foreach i $itemlist {
	$l insert end $i
    }
    set curitemnum 0
    set cursubnum2 0
    $l select from 0
}


# Procedure: InsertTextTag
proc InsertTextTag { w text {tags ""}} {
    set start [$w index insert]
    $w insert insert "$text"
    foreach i [$w tag names $start] {$w tag remove $i $start insert}
    foreach i $tags {$w tag add $i $start insert}
}


# Procedure: IsADir
proc IsADir { pathName} {
# xf ignore me 5
##########
# Procedure: IsADir
# Description: check if name is a directory (including symbolic links)
# Arguments: pathName - the path to check
# Returns: 1 if its a directory, otherwise 0
# Sideeffects: none
##########

  if {[file isdirectory $pathName]} {
    return 1
  } {
    catch "file type $pathName" fileType
    if {"$fileType" == "link"} {
      if {[catch "file readlink $pathName" linkName]} {
        return 0
      }
      catch "file type $linkName" fileType
      while {"$fileType" == "link"} {
        if {[catch "file readlink $linkName" linkName]} {
          return 0
        }
        catch "file type $linkName" fileType
      }
      return [file isdirectory $linkName]
    }
  }
  return 0
}


# Procedure: IsAFile
proc IsAFile { fileName} {
# xf ignore me 5
##########
# Procedure: IsAFile
# Description: check if filename is a file (including symbolic links)
# Arguments: fileName - the filename to check
# Returns: 1 if its a file, otherwise 0
# Sideeffects: none
##########

  if {[file isfile $fileName]} {
    return 1
  } {
    catch "file type $fileName" fileType
    if {"$fileType" == "link"} {
      if {[catch "file readlink $fileName" linkName]} {
        return 0
      }
      catch "file type $linkName" fileType
      while {"$fileType" == "link"} {
        if {[catch "file readlink $linkName" linkName]} {
          return 0
        }
        catch "file type $linkName" fileType
      }
      return [file isfile $linkName]
    }
  }
  return 0
}


# Procedure: IsASymlink
proc IsASymlink { fileName} {
# xf ignore me 5
##########
# Procedure: IsASymlink
# Description: check if filename is a symbolic link
# Arguments: fileName - the path/filename to check
# Returns: none
# Sideeffects: none
##########

  catch "file type $fileName" fileType
  if {"$fileType" == "link"} {
    return 1
  }
  return 0
}



# Procedure: NoFunction
if {"[info procs NoFunction]" == ""} {
proc NoFunction { args} {
# xf ignore me 7
##########
# Procedure: NoFunction
# Description: do nothing (especially with scales and scrollbars)
# Arguments: args - a number of ignored parameters
# Returns: none
# Sideeffects: none
##########
}
}


# Procedure: RepackChapSect
proc RepackChapSect {} {
    global cursubnum cursectnum

    pack unpack [SN itemFrame]
    pack after [SN menuFrame] [SN selectFrame] {top frame center fillx}
    DispSubject $cursectnum
    [SN subject] select from $cursubnum
    SelectSubject $cursubnum
}


# Procedure: RepackItem
proc RepackItem {} {
    global cursubnum2 curitemnum

    pack unpack [SN selectFrame]
    pack after [SN menuFrame] [SN itemFrame] {right frame center filly}
    DispSubject $curitemnum
    [SN subject] select from $cursubnum2
    SelectSubject $cursubnum2
}


# Procedure: SN
if {"[info procs SN]" == ""} {
proc SN { {xfName ""}} {
# xf ignore me 7
##########
# Procedure: SN
# Description: map a symbolic name to the widget path
# Arguments: xfName
# Returns: the symbolic name
# Sideeffects: none
##########

  SymbolicName $xfName
}
}


# Procedure: SelectChapter
proc SelectChapter { chapnum} {
    global curchapnum

    if {$curchapnum == -1} {return}
    set curchapnum $chapnum
    set w [SN chapter]
    $w select from $chapnum
    DispSection $chapnum
    SelectSection 0
    SelectSubject 0
}


# Procedure: SelectItem
proc SelectItem { aitem} {
    global item itemlist curitemnum cursubnum2
    global subject

    set curitemnum $aitem
    set cursubnum2 0
    DispSubject $aitem
    [SN subject] select from 0
    DispFaq 0
}


# Procedure: SelectSection
proc SelectSection { sectnum} {
    global cursectnum

    set cursectnum $sectnum
    set w [SN section]
    $w select from $sectnum
    DispSubject $sectnum
    SelectSubject 0
}


# Procedure: SelectSubject
proc SelectSubject { subnum} {
    global cursubnum cursubnum2 SelectStat

    if {"$SelectStat" == "ChapSect"} {
	set cursubnum $subnum
    } else {
	set cursubnum2 $subnum
    }
    set w [SN subject]
    $w select from $subnum
    DispFaq $subnum
}


# Procedure: SourceFile
proc SourceFile { file} {
    global argv0
    global sgmf sgmfd
    global chapter chapnum
    global section
    global subject substart subend
    global curchapnum
    global cursectnum
    global cursubnum
    global item
    global SelectStat

    set linenum 0
    set chapnum -1
    set sectnum -1
    set subnum -1

    if {"$sgmfd" != ""} {close $sgmfd}
    if {[catch {set sgmfd [open $file r]}]} {return NG}

    catch {unset chapter section subject substart subend item itemlist}
    [SN chapter] delete 0 end

    while {![eof $sgmfd]} {
	gets $sgmfd line
	incr linenum
	if {[regexp {^<([/a-zA-Z]*)> *(.*)} "$line" dumy tag rest]} {
	    case $tag in {
		chap {
		    regexp "<ct>(.*)</ct>" "$rest" dumy chapname
		    incr chapnum
		    set sectnum -1
		    set chapter($chapnum) $chapname
		    [SN chapter] insert end "$chapname"
		}
		sect {
		    regexp "<st>(.*)</st>" "$rest" dumy sectname
		    incr sectnum
		    set subnum -1
		    set section($chapnum:$sectnum) "$sectname"
		}
		FAQ {
		    incr subnum
		    set substart($chapnum:$sectnum:$subnum) [tell $sgmfd]
		}
		Subject {
		    set subject($chapnum:$sectnum:$subnum) "$rest"
		}
		/FAQ {
		    set subend($chapnum:$sectnum:$subnum) \
			[expr [tell $sgmfd]-[string length $line]-1]
		}
		Item {
		    set items [split "$rest" ,]
		    foreach ii "$items" {
			set ii "[string trim $ii]"
			if {$ii != ""} {
			    lappend item($ii) $chapnum:$sectnum:$subnum
			}
		    }
		}
	    }
	}
    }

    set sgmf $file

    if {$chapnum > -1} {
	set curchapnum 0
	set cursectnum 0
    } else {
	set curchapnum $chapnum
	set cursectnum -1
    }
    set cursubnum 0

    InitItem

    DispSection 0
    DispSubject 0
    DispFaq 0
    [SN chapter] select from 0
    [SN section] select from 0
    [SN subject] select from 0

    return OK
}


# Procedure: SymbolicName
if {"[info procs SymbolicName]" == ""} {
proc SymbolicName { {xfName ""}} {
# xf ignore me 7
##########
# Procedure: SymbolicName
# Description: map a symbolic name to the widget path
# Arguments: xfName
# Returns: the symbolic name
# Sideeffects: none
##########

  global symbolicName

  if {"$xfName" != ""} {
    set xfArrayName ""
    append xfArrayName symbolicName ( $xfName )
    if {![catch "set \"$xfArrayName\"" xfValue]} {
      return $xfValue
    } {
      if {"[info commands XFProcError]" != ""} {
        XFProcError "Unknown symbolic name:\n$xfName"
      } {
        puts stderr "XF error: unknown symbolic name:\n$xfName"
      }
    }
  }
  return ""
}
}




# application parsing procedure
proc XFLocalParseAppDefs {xfAppDefFile} {
  global xfAppDefaults

  # basically from: Michael Moore
  if {[file exists $xfAppDefFile] &&
      [file readable $xfAppDefFile] &&
      "[file type $xfAppDefFile]" == "link"} {
    catch "file type $xfAppDefFile" xfType
    while {"$xfType" == "link"} {
      if {[catch "file readlink $xfAppDefFile" xfAppDefFile]} {
        return
      }
      catch "file type $xfAppDefFile" xfType
    }
  }
  if {!("$xfAppDefFile" != "" &&
        [file exists $xfAppDefFile] &&
        [file readable $xfAppDefFile] &&
        "[file type $xfAppDefFile]" == "file")} {
    return
  }
  if {![catch "open $xfAppDefFile r" xfResult]} {
    set xfAppFileContents [read $xfResult]
    close $xfResult
    foreach line [split $xfAppFileContents "\n"] {
      # backup indicates how far to backup.  It applies to the
      # situation where a resource name ends in . and when it
      # ends in *.  In the second case you want to keep the *
      # in the widget name for pattern matching, but you want
      # to get rid of the . if it is the end of the name. 
      set backup -2  
      set line [string trim $line]
      if {[string index $line 0] == "#" || "$line" == ""} {
        # skip comments and empty lines
        continue
      }
      set list [split $line ":"]
      set resource [string trim [lindex $list 0]]
      set i [string last "." $resource]
      set j [string last "*" $resource]
      if {$j > $i} { 
        set i $j
        set backup -1
      }
      incr i
      set name [string range $resource $i end]
      incr i $backup
      set widname [string range $resource 0 $i]
      set value [string trim [lindex $list 1]]
      if {"$widname" != "" && "$widname" != "*"} {
        # insert the widget and resourcename to the application
        # defaults list.
        set xfAppDefaults($widname:[string tolower $name]) $value
      }
    }
  }
}

# application loading procedure
proc XFLocalLoadAppDefs {{xfClasses ""} {xfPriority "startupFile"} {xfAppDefFile ""}} {
  global env

  if {"$xfAppDefFile" == ""} {
    set xfFileList ""
    if {[info exists env(XUSERFILESEARCHPATH)]} {
      append xfFileList [split $env(XUSERFILESEARCHPATH) :]
    }
    if {[info exists env(XAPPLRESDIR)]} {
      append xfFileList [split $env(XAPPLRESDIR) :]
    }
    if {[info exists env(XFILESEARCHPATH)]} {
      append xfFileList [split $env(XFILESEARCHPATH) :]
    }
    append xfFileList " /usr/lib/X11/app-defaults"
    append xfFileList " /usr/X11/lib/X11/app-defaults"

    foreach xfCounter1 $xfClasses {
      foreach xfCounter2 $xfFileList {
        set xfPathName $xfCounter2
        if {[regsub -all "%N" "$xfPathName" "$xfCounter1" xfResult]} {
          set xfPathName $xfResult
        }
        if {[regsub -all "%T" "$xfPathName" "app-defaults" xfResult]} {
          set xfPathName $xfResult
        }
        if {[regsub -all "%S" "$xfPathName" "" xfResult]} {
          set xfPathName $xfResult
        }
        if {[regsub -all "%C" "$xfPathName" "" xfResult]} {
          set xfPathName $xfResult
        }
        if {[file exists $xfPathName] &&
            [file readable $xfPathName] &&
            ("[file type $xfPathName]" == "file" ||
             "[file type $xfPathName]" == "link")} {
          catch "option readfile $xfPathName $xfPriority"
          if {"[info commands XFParseAppDefs]" != ""} {
            XFParseAppDefs $xfPathName
          } {
            if {"[info commands XFLocalParseAppDefs]" != ""} {
              XFLocalParseAppDefs $xfPathName
            }
          }
        } {
          if {[file exists $xfCounter2/$xfCounter1] &&
              [file readable $xfCounter2/$xfCounter1] &&
              ("[file type $xfCounter2/$xfCounter1]" == "file" ||
               "[file type $xfCounter2/$xfCounter1]" == "link")} {
            catch "option readfile $xfCounter2/$xfCounter1 $xfPriority"
            if {"[info commands XFParseAppDefs]" != ""} {
              XFParseAppDefs $xfCounter2/$xfCounter1
            } {
              if {"[info commands XFLocalParseAppDefs]" != ""} {
                XFLocalParseAppDefs $xfCounter2/$xfCounter1
              }
            }
          }
        }
      }
    }
  } {
    # load a specific application defaults file
    if {[file exists $xfAppDefFile] &&
        [file readable $xfAppDefFile] &&
        ("[file type $xfAppDefFile]" == "file" ||
         "[file type $xfAppDefFile]" == "link")} {
      catch "option readfile $xfAppDefFile $xfPriority"
      if {"[info commands XFParseAppDefs]" != ""} {
        XFParseAppDefs $xfAppDefFile
      } {
        if {"[info commands XFLocalParseAppDefs]" != ""} {
          XFLocalParseAppDefs $xfAppDefFile
        }
      }
    }
  }
}

# application setting procedure
proc XFLocalSetAppDefs {{xfWidgetPath "."}} {
  global xfAppDefaults

  if {![info exists xfAppDefaults]} {
    return
  }
  foreach xfCounter [array names xfAppDefaults] {
    if {[string match "${xfWidgetPath}*" $xfCounter]} {
      set widname [string range $xfCounter 0 [expr [string first : $xfCounter]-1]]
      set name [string range $xfCounter [expr [string first : $xfCounter]+1] end]
      # Now lets see how many tcl commands match the name
      # pattern specified.
      set widlist [info command $widname]
      if {"$widlist" != ""} {
        foreach widget $widlist {
          # make sure this command is a widget.
          if {![catch "winfo id $widget"]} {
            catch "$widget configure -[string tolower $name] $xfAppDefaults($xfCounter)" 
          }
        }
      }
    }
  }
}



# end source
proc EndSrc {} {
    global default_filename filename
    global argc argv argv0
    global sgmf sgmfd
    global chapter chapnum
    global section
    global subject substart subend
    global curchapnum
    global cursectnum
    global cursubnum
    global item itemlist
    global SelectStat

    set SelectStat ChapSect
    set sgmfd ""
    set sgmf ""

    catch {unset chapter section subject substart subend item itemlist}
    [SN chapter] delete 0 end
    [SN section] delete 0 end
    [SN subject] delete 0 end

# set default
    set default_filename "[option get . defaultFile ""]"
    set color_answer "[option get . answerColor Foreground]"
    if {"$color_answer" == ""} {set color_answer green4}
    set color_question "[option get . questionColor Foreground]"
    if {"$color_question" == ""} {set color_question red4}
    set color_comment "[option get . commentColor Foreground]"
    if {"$color_comment" == ""} {set color_comment blue4}

    [SN faq] tag configure sunken  -background #eed5b7 -relief sunken
    [SN faq] tag configure font1  -font -Adobe-Times-Bold-R-Normal-*-140-*
    [SN faq] tag configure colorA -foreground $color_answer
    [SN faq] tag configure colorQ -foreground $color_question
    [SN faq] tag configure colorC -foreground $color_comment

    if {$argc > 1} { puts stderr "usage: $argv0 \[file\]"; exit 2 }

    if {$argc == 1} {
	set filename [lindex $argv 0]
    } else {
	if {"$default_filename" != ""} {
	    set filename $default_filename
	} else {
	    puts "$argv0: needs one argument(filename).";
	    exit 1;
	}
    }
    if {[SourceFile $filename] != "OK"} {
	puts stderr "$argv0: cannot open $filename"
	exit 2
    }
}

# initialize global variables
global {SelectStat}
global {chapnum}
global {chapter}
global {curchapnum}
global {curitem}
global {curitemnum}
global {cursectnum}
global {cursubnum}
global {cursubnum2}
global {default_filename}
global {filename}
global {fsBox}
global {fsBoxExtensions}
set {fsBox(activeBackground)} {}
set {fsBox(activeForeground)} {}
set {fsBox(all)} {0}
set {fsBox(background)} {}
set {fsBox(button)} {0}
set {fsBox(extensions)} {0}
set {fsBox(font)} {}
set {fsBox(foreground)} {}
set {fsBox(internalPath)} {}
set {fsBox(name)} {}
set {fsBox(path)} {}
set {fsBox(pattern)} {*.sgm}
set {fsBox(scrollActiveForeground)} {}
set {fsBox(scrollBackground)} {}
set {fsBox(scrollForeground)} {}
set {fsBox(scrollSide)} {left}
set {fsBox(showPixmap)} {0}
global {fsBoxExtensions}
set {fsBoxExtensions} {0}
global {item}
global {itemlist}
global {section}
global {sgmf}
global {subend}
global {subject}
global {substart}

# please don't modify the following
# variables. They are needed by xf.
global {autoLoadList}
set {autoLoadList(faqbro)} {0}
set {autoLoadList(main.tcl)} {0}
global {internalAliasList}
set {internalAliasList} {}
global {moduleList}
set {moduleList(faqbro)} {}
global {preloadList}
set {preloadList(xfInternal)} {}
global {symbolicName}
set {symbolicName(chapFrame)} {.frame0.frame}
set {symbolicName(chapter)} {.frame0.frame.listbox1}
set {symbolicName(contentsList)} {.frame2.frame6.listbox1}
set {symbolicName(dirList)} {.frame2.frame.listbox1}
set {symbolicName(dirName)} {.frame1.label4}
set {symbolicName(faq)} {.frame1.text2}
set {symbolicName(fileName)} {.frame1.label5}
set {symbolicName(fileSelect)} {.top0}
set {symbolicName(item)} {.frame.listbox1}
set {symbolicName(itemFrame)} {.frame}
set {symbolicName(menuFrame)} {.frame2}
set {symbolicName(root)} {.}
set {symbolicName(sectFrame)} {.frame0.frame2}
set {symbolicName(section)} {.frame0.frame2.listbox1}
set {symbolicName(selectFrame)} {.frame0}
set {symbolicName(subject)} {.frame3.listbox1}
global {xfWmSetPosition}
set {xfWmSetPosition} {}
global {xfWmSetSize}
set {xfWmSetSize} {}
global {xfAppDefToplevels}
set {xfAppDefToplevels} {}

# load default bindings.
if {[info exists env(XF_BIND_FILE)] &&
    "[info procs XFShowHelp]" == ""} {
  source $env(XF_BIND_FILE)
}

# parse and apply application defaults.
XFLocalLoadAppDefs Faqbro
XFLocalSetAppDefs

# display/remove toplevel windows.
ShowWindow.

# end source
EndSrc

# eof
#

