# Program: xf
# Description: handle hypertext widgets
#
# $Header: Hypertext[2.3] Wed Mar 10 11:57:22 1993 garfield@garfield frozen $

global xfDefaultConf
set xfDefaultConf(hypertext) 5

##########
# Procedure: XFAdd.Hypertext
# Description: add a hypertext
# Arguments: xfW - the widget
#            xfName - a optional name
#            xfType - add or config
# Returns: none
# Sideeffects: none
##########
proc XFAdd.Hypertext {xfW xfName xfType} {
  global xfConf
  global xfStatus

  XFEditSetStatus "Inserting Hypertext..."
  set xfName [XFMiscGetUniqueName $xfName hypertext]
  if {"$xfStatus(path)" == "."} {
    if {"$xfType" == "add"} {
      if {[catch "htext .$xfName" xfResult]} {
        XFProcError "$xfResult"
        XFEditSetStatus "Inserting Hypertext...aborted"
        return
      }
    } {
      if {[catch "htext .$xfName" xfResult]} {
        XFProcError "$xfResult"
        XFEditSetStatus "Inserting Hypertext...aborted"
        return
      }
    }

    XFMiscPositionWidget .$xfName
    XFMiscBindWidgetTree .$xfName
  } {
    if {"$xfType" == "add"} {
      if {[catch "htext $xfStatus(path).$xfName" xfResult]} {
        XFProcError "$xfResult"
        XFEditSetStatus "Inserting Hypertext...aborted"
        return
      }
    } {
      if {[catch "htext $xfStatus(path).$xfName" xfResult]} {
        XFProcError "$xfResult"
        XFEditSetStatus "Inserting Hypertext...aborted"
        return
      }
    }

    XFMiscPositionWidget $xfStatus(path).$xfName
    XFMiscBindWidgetTree $xfStatus(path).$xfName
  }

  incr xfStatus(elementCounter)
  XFEditSetPath $xfStatus(path)
  XFEditSetStatus "Inserting Hypertext...done"
}

##########
# Procedure: XFAddTmp.Hypertext
# Description: add a tmp hypertext
# Arguments: none
# Returns: none
# Sideeffects: none
##########
proc XFAddTmp.Hypertext {} {

  catch "destroy .xfEdit.tmplt"
  if {[catch "htext .xfEdit.tmplt"]} {
    XFProcConfConfigure undefined "" "" Hypertext add
  } {
    XFProcConfConfigure .xfEdit.tmplt "" "" Hypertext add
  }
}

##########
# Procedure: XFBind.Hypertext
# Description: default bindings for hypertext
# Arguments: xfW - the widget
# Returns: none
# Sideeffects: none
##########
proc XFBind.Hypertext {xfW} {

  if {"[info commands $xfW]" != ""} {
    bind $xfW <2> {%W scan mark %x %y}
    bind $xfW <B2-Motion> {%W scan dragto %x %y}
    bind $xfW <Control-p> {%W gotoline [expr [%W gotoline]-1]}
    bind $xfW <Control-n> {%W gotoline [expr [%W gotoline]+1]}
  }
}

##########
# Procedure: XFConfig.Hypertext4
# Description: configure a hypertext
# Arguments: xfW - the widget
#            xfType - config type (add config)
#            xfClass - the class we configure
#            xfLeader - the leading window
# Returns: none
# Sideeffects: none
##########
proc XFConfig.Hypertext4 {xfW xfType xfClass {xfLeader ""}} {
  global xfMisc
  global xfStatus

  if {"$xfType" == "add"} {
    set xfName hypertext$xfStatus(elementCounter)
  } {
    set xfName [XFMiscPathName $xfW]
  }
  XFEditSetStatus "Calling parameter setting for Hypertext..."

  # build widget structure
  XFTmpltToplevel .xf${xfClass}Config4 400x510 \
    "Hypertext parameters:[XFMiscPathTail $xfW]" $xfLeader

  XFElementInit $xfW .xf${xfClass}Config4 $xfType $xfClass \
    XFHypertextSetHypertext4 parameters $xfName 4
  XFElementColor $xfW .xf${xfClass}Config4 $xfType $xfClass bg \
    background Background "Background" XFHypertextSetHypertext4
  XFElementFont $xfW .xf${xfClass}Config4 $xfType $xfClass \
    font font Font "Font" XFHypertextSetHypertext4
  XFElementColor $xfW .xf${xfClass}Config4 $xfType $xfClass fg \
    foreground Foreground "Foreground" XFHypertextSetHypertext4
  XFElementGeometry $xfW .xf${xfClass}Config4 $xfType $xfClass \
    XFHypertextSetHypertext4
  XFElementScale $xfW .xf${xfClass}Config4 $xfType $xfClass linespacing \
    linespacing LineSpacing "Line spacing" "pixels" 40 \
    XFHypertextSetHypertext4
  XFElementText $xfW .xf${xfClass}Config4 $xfType $xfClass specialchar \
    specialchar SpecialChar "Special character" XFHypertextSetHypertext4
  XFElementText $xfW .xf${xfClass}Config4 $xfType $xfClass xscrollcommand \
    xscrollcommand XScrollCommand "X scroll command" XFHypertextSetHypertext4
  XFElementScale $xfW .xf${xfClass}Config4 $xfType $xfClass xscrollunits \
    xScrollUnits ScrollUnits "X scroll units" "pixels" 400 \
    XFHypertextSetHypertext4
  XFElementText $xfW .xf${xfClass}Config4 $xfType $xfClass yscrollcommand \
    yscrollcommand YScrollCommand "Y scroll command" XFHypertextSetHypertext4
  XFElementScale $xfW .xf${xfClass}Config4 $xfType $xfClass yscrollunits \
    xScrollUnits ScrollUnits "Y scroll units" "pixels" 400 \
    XFHypertextSetHypertext4

  # save current parameter
  XFElementSave $xfW $xfClass {background font foreground geometry linespacing specialchar xscrollcommand xscrollunits xscrollcommand xscrollunits}

  # packing
  pack append .xf${xfClass}Config4.params1 \
              .xf${xfClass}Config4.params1.params2 {left fill expand}
  pack append .xf${xfClass}Config4 \
              .xf${xfClass}Config4.pathname {top fill frame center} \
              .xf${xfClass}Config4.leave {bottom fill} \
              .xf${xfClass}Config4.additional {bottom fill} \
              .xf${xfClass}Config4.params1 {top fill expand}

  XFBindFormConnect .xf${xfClass}Config4.params1.params2 \
    "XFHypertextSetHypertext4 $xfW 0 $xfClass"

  XFEditSetStatus "Calling parameter setting for Hypertext...done"
}

##########
# Procedure: XFConfig.Hypertext5
# Description: configure a hypertext
# Arguments: xfW - the widget
#            xfType - config type (add config)
#            xfClass - the class we configure
#            xfLeader - the leading window
# Returns: none
# Sideeffects: none
##########
proc XFConfig.Hypertext5 {xfW xfType xfClass {xfLeader ""}} {
  global xfBind
  global xfMisc
  global xfStatus

  set xfName [XFMiscPathName $xfW]
  XFEditSetStatus "Calling parameter setting for Hypertext..."

  # build widget structure
  XFTmpltToplevel .xf${xfClass}Config5 400x540 \
    "Hypertext parameters:[XFMiscPathTail $xfW]" $xfLeader

  XFElementInit $xfW .xf${xfClass}Config5 $xfType $xfClass \
    XFHypertextSetHypertext5 parameters $xfName 5
  XFElementText $xfW .xf${xfClass}Config5 $xfType $xfClass filename \
    filename FileName "File name" XFHypertextSetHypertext5

  label .xf${xfClass}Config5.params1.params2.childMess \
    -anchor c \
    -text "Hypertext children:"

  XFTmpltListbox .xf${xfClass}Config5.params1.params2 childs
  .xf${xfClass}Config5.params1.params2.childs.childs configure \
    -geometry 20x10
  foreach xfCounter [winfo children $xfW] {
    .xf${xfClass}Config5.params1.params2.childs.childs insert end $xfCounter
  }

  label .xf${xfClass}Config5.params1.params2.textMess \
    -anchor c \
    -text "Text (write to the file to keep contents!):"

  if {"$xfType" == "add"} {
    XFTmpltText .xf${xfClass}Config5.params1.params2 text 0
  } {
    XFTmpltText .xf${xfClass}Config5.params1.params2 text 0 \
      [lindex [$xfW configure -text] 4]
  }
  set xfFileName \
    [.xf${xfClass}Config5.params1.params2.filename.filename get]
  if {"$xfFileName" != ""} {
    if {[file exists $xfFileName]} {
      if {![catch "open $xfFileName r" xfInFile]} {
        set xfText ""
        while {1} {
          if {[gets $xfInFile xfLine] == -1} {
            break
          }
          append xfText "$xfLine\n"
        }
        close $xfInFile
        XFMiscSetText .xf${xfClass}Config5.params1.params2.text.text "$xfText"
      }
    }
  }

  XFTmpltFrame .xf${xfClass}Config5.params1.params2.actions 0

  button .xf${xfClass}Config5.params1.params2.actions.insert \
    -text {Insert widget} \
    -command "XFHypertextInsert $xfW $xfClass"

  button .xf${xfClass}Config5.params1.params2.actions.rescan \
    -text {Rescan} \
    -command "
      XFMiscClearList .xf${xfClass}Config5.params1.params2.childs.childs
      foreach xfCounter \[winfo children $xfW\] {
        .xf${xfClass}Config5.params1.params2.childs.childs insert end \$xfCounter
      }"

  button .xf${xfClass}Config5.params1.params2.actions.save \
    -text {Save} \
    -command "
      set xfFileName \
        \[.xf${xfClass}Config5.params1.params2.filename.filename get\]
      if {\"\$xfFileName\" != \"\"} {
        if {\[catch \"open \$xfFileName w\" xfOutFile\]} {
          XFProcError \"Cannot open \$xfFileName\"
        } {
          set xfTmpContents \
            \[XFMiscGetText .xf${xfClass}Config5.params1.params2.text.text\]
          puts \$xfOutFile \"\$xfTmpContents\"
          close \$xfOutFile
        }
      } {
        XFProcError \"No file name specified\"
      }"

  .xf${xfClass}Config5.leave.ok configure \
    -command "
      set xfFileName \
        \[.xf${xfClass}Config5.params1.params2.filename.filename get\]
      XFMiscSetSymbolicName $xfW \
        \[.xf${xfClass}Config5.params1.params2.symname.symname get\]
      if {\"\$xfFileName\]\" != \"\"} {
        if {\[catch \"open \$xfFileName w\" xfOutFile\]} {
          XFProcError \"Cannot open \$xfFileName\"
        } {
          set xfTmpContents \
            \[XFMiscGetText .xf${xfClass}Config5.params1.params2.text.text\]
          puts \$xfOutFile \"\$xfTmpContents\"
          close \$xfOutFile
          XFHypertextSetHypertext5 $xfW 1 $xfClass
          destroy .xf${xfClass}Config5
        }
      } {
        XFProcError \"No file name specified (could not save)\"
      }"

  .xf${xfClass}Config5.leave.cancel configure \
    -command "destroy .xf${xfClass}Config5"

  # bindings
  bind .xf${xfClass}Config5.params1.params2.childs.childs $xfBind(configure) {
    XFBindSelectOne %W %y
    XFMiscFlash [%W get [%W curselect]]
    XFProcConfConfigure [%W get [%W curselect]]}
  bind .xf${xfClass}Config5.params1.params2.childs.childs $xfBind(select1) "
    XFBindSelectOne %W %y
    XFMiscFlash \[%W get \[%W curselect\]\]
    XFHypertextInsert $xfW $xfClass"
  bind .xf${xfClass}Config5.params1.params2.childs.childs <ButtonPress-1> {
    XFBindSelectOne %W %y
    XFMiscFlash [%W get [%W curselect]]}
  bind .xf${xfClass}Config5.params1.params2.childs.childs <Button1-Motion> {
    XFBindSelectOne %W %y}
  bind .xf${xfClass}Config5.params1.params2.childs.childs <Shift-ButtonPress-1> {
    XFBindSelectOne %W %y}
  bind .xf${xfClass}Config5.params1.params2.childs.childs <Shift-Button1-Motion> {
    XFBindSelectOne %W %y}

  bind .xf${xfClass}Config5.params1.params2.filename.filename $xfBind(configure) \
    "XFProcFSBoxFile {.xf${xfClass}Config5.params1.params2.filename.filename}"

  # packing
  pack append .xf${xfClass}Config5.params1.params2.actions \
              .xf${xfClass}Config5.params1.params2.actions.insert {left fill expand} \
              .xf${xfClass}Config5.params1.params2.actions.save {left fill expand} \
              .xf${xfClass}Config5.params1.params2.actions.rescan {left fill expand}
  pack append .xf${xfClass}Config5.params1.params2 \
              .xf${xfClass}Config5.params1.params2.actions {bottom fill} \
              .xf${xfClass}Config5.params1.params2.childMess {top fillx padx 6} \
              .xf${xfClass}Config5.params1.params2.childs {top fill} \
              .xf${xfClass}Config5.params1.params2.textMess {top fillx padx 6} \
              .xf${xfClass}Config5.params1.params2.text {top fill expand}
  pack append .xf${xfClass}Config5.params1 \
              .xf${xfClass}Config5.params1.params2 {left fill expand}
  pack append .xf${xfClass}Config5 \
              .xf${xfClass}Config5.pathname {top fill frame center} \
              .xf${xfClass}Config5.leave {bottom fill} \
              .xf${xfClass}Config5.additional {bottom fill} \
              .xf${xfClass}Config5.params1 {top fill expand}

  XFBindFormConnect .xf${xfClass}Config5.params1.params2 \
    "XFHypertextSetHypertext5 $xfW 0 $xfClass"

  XFEditSetStatus "Calling parameter setting for Hypertext...done"
}

##########
# Procedure: XFHypertextInsert
# Description: insert item into hypertext
# Arguments: xfW - the widget we configure
#            xfClass - the class we edit
# Returns: none
# Sideeffects: none
##########
proc XFHypertextInsert {xfW xfClass} {

  set xfCurSelect \
    [.xf${xfClass}Config5.params1.params2.childs.childs curselect]
  if {$xfCurSelect >= 0} {
    XFMiscInsertText .xf${xfClass}Config5.params1.params2.text.text \
      "%%$xfW append [.xf${xfClass}Config5.params1.params2.childs.childs get $xfCurSelect]%%"
  }
}

##########
# Procedure: XFSaveWidget.Hypertext
# Description: save hypertext widget
# Arguments: xfOutFile - the output file
#            xfW - the widget
# Returns: none
# Sideeffects: none
##########
proc XFSaveWidget.Hypertext {xfOutFile xfW} {
  global xfConf

  set xfType [string tolower [winfo class $xfW]]

  # what are we doing here ?
  puts $xfOutFile "\n  # build widget $xfW"

  # write widget type and path name
  puts $xfOutFile "  if {\[catch \"htext $xfW\"\]} {"
  puts $xfOutFile "    if {\"\[info procs XFEdit\]\" != \"\"} {"
  puts $xfOutFile "      XFProcError \"Unknown widget type: htext\""
  puts $xfOutFile "      return"
  puts $xfOutFile "    } {"
  puts $xfOutFile "      puts stderr \"Unknown widget type: htext\""
  puts $xfOutFile "      catch \"destroy .\""
  puts $xfOutFile "      catch \"exit 0\""
  puts $xfOutFile "    }"
  puts $xfOutFile "  }"

  puts $xfOutFile "  $xfW config " nonewline
  XFSaveWidgetResource $xfOutFile $xfW
  puts $xfOutFile ""
}

##########
# Procedure: XFHypertextSetHypertext4
# Description: set hypertext parameters
# Arguments: xfW - the widget
#            xfType - the type of setting (1 = set always, 0 = set
#                     only if permanent apply is on)
#            xfClass - the class we configure
#            xfParam1 - ignored parameter
# Returns: none
# Sideeffects: none
##########
proc XFHypertextSetHypertext4 {xfW xfType xfClass {xfParam1 ""}} {
  global xfConf
  global xfMisc

  if {$xfType == 0 && !$xfConf(applyParameters)} {
    return
  }
  if {"[info commands $xfW]" == ""} {
    return
  }
  XFMiscSetSymbolicName $xfW \
    [.xf${xfClass}Config4.params1.params2.symname.symname get]

  XFMiscSetResource $xfW background \
    [.xf${xfClass}Config4.params1.params2.bg.bg get]
  XFMiscSetResource $xfW font \
    [.xf${xfClass}Config4.params1.params2.font.font get]
  XFMiscSetResource $xfW foreground \
    [.xf${xfClass}Config4.params1.params2.fg.fg get]
  if {[.xf${xfClass}Config4.params1.params2.geo.geo1.geo1 get] > 0 &&
      [.xf${xfClass}Config4.params1.params2.geo.geo2.geo2 get] > 0} {
    XFMiscSetResource $xfW geometry \
      "[.xf${xfClass}Config4.params1.params2.geo.geo1.geo1 get]x[.xf${xfClass}Config4.params1.params2.geo.geo2.geo2 get]"
  }
  XFMiscSetResource $xfW linespacing \
    [.xf${xfClass}Config4.params1.params2.linespacing.linespacing get]
  if {"[.xf${xfClass}Config4.params1.params2.specialchar.specialchar get]" != ""} {
    XFMiscSetResource $xfW specialchar \
      [.xf${xfClass}Config4.params1.params2.specialchar.specialchar get]
  }
  XFMiscSetResource $xfW xscrollcommand \
    [.xf${xfClass}Config4.params1.params2.xscrollcommand.xscrollcommand get]
  XFMiscSetResource $xfW xscrollunits \
    [.xf${xfClass}Config4.params1.params2.xscrollunits.xscrollunits get]
  XFMiscSetResource $xfW yscrollcommand \
    [.xf${xfClass}Config4.params1.params2.yscrollcommand.yscrollcommand get]
  XFMiscSetResource $xfW yscrollunits \
    [.xf${xfClass}Config4.params1.params2.yscrollunits.yscrollunits get]
}

##########
# Procedure: XFHypertextSetHypertext5
# Description: set hypertext parameters
# Arguments: xfW - the widget
#            xfType - the type of setting (1 = set always, 0 = set
#                     only if permanent apply is on)
#            xfClass - the class we configure
#            xfParam1 - ignored parameter
# Returns: none
# Sideeffects: none
##########
proc XFHypertextSetHypertext5 {xfW xfType xfClass {xfParam1 ""}} {
  global xfConf

  if {$xfType == 0 && !$xfConf(applyParameters)} {
    return
  }
  if {"[info commands $xfW]" == ""} {
    return
  }
  XFMiscSetSymbolicName $xfW \
    [.xf${xfClass}Config5.params1.params2.symname.symname get]

  if {"[.xf${xfClass}Config5.params1.params2.filename.filename get]" != ""} {
    XFMiscSetResource $xfW filename \
      [.xf${xfClass}Config5.params1.params2.filename.filename get]
    XFMiscClearText .xf${xfClass}Config5.params1.params2.text.text
    XFMiscPutFileInText .xf${xfClass}Config5.params1.params2.text.text \
      [.xf${xfClass}Config5.params1.params2.filename.filename get]
    XFMiscClearList .xf${xfClass}Config5.params1.params2.childs.childs
    foreach xfCounter [winfo children $xfW] {
      .xf${xfClass}Config5.params1.params2.childs.childs insert end $xfCounter
    }
  } {
    XFMiscSetResource $xfW text \
      [XFMiscGetText .xf${xfClass}Config5.params1.params2.text.text]
  }
}

# eof

