### Copyright (C) 1995, 1996, 1997 Jeppe Buk (buk@imada.sdu.dk)
### 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.

proc functions {} {
  # Menus and Bindings / Function Definitions
  Desc "Definitions of functions used in menus and bindings"
  ShortDesc "Function definitions"

  Header functionsH -text "Function Definitions" -frame:relief flat \
      -help "On this page you define your own functions to use in bindings."

  Header description -text "Two special functions can be created.\nThey"\
      "are called 'InitFunction' and 'RestartFunction'"\
      "respectively.\nSee the help page on the Dotfile Generator menu"\
      "for details."

  Entry functionName -text "Name of the function" \
      -help "To define a new function enter the name of the function here."

  global Builtins
  ComboBox builtinCall -noedit 1 -text "Builtin" -default Beep \
      -entryhelp $Builtins \
      -help "Select a built-in function to call from this menu"
  
  Menu events -text "Event" -entryhelp {
    Immediate "Execute the command right away" \
        Motion "Execute the command on motion of the mouse" \
        Click "Execute the command on mouse click" \
        DoubleClick "Execute the command on mouse double-click" } \
      -help "Select an event to cause execution of the command"
  Entry arguments -text "Arguments" \
      -help "Any additional arguments are entered here (the syntax is your" \
      "responsibility) - see help for the individual commands."
  
  ExtEntry functionEntry -count 10 -entries {builtinCall events arguments}
  ExtEntry function -entries {functionName functionEntry} -count 1 \
      -orient top -index functionName

  Label funcArgs -justify left -wraplength 600

  Change {
    if {$changeElm == "builtinCall"} {
      set bcindex -1
      set found "no"
      foreach bc $Builtins {
	incr bcindex
	if {$bc == $builtinCall} {
	  set found "yes"
	  break
	}
      }
      if {$found == "yes"} {
	set help [lindex $Builtins [expr $bcindex + 1]]
      } else {
	set help "\[Help not found\]"
      }
      set funcArgs "Help for function \"$builtinCall\":\n$help"
      eval "[pick [funcWithArgs $builtinCall] Enable Disable] arguments"
    }
  }
  ShowPage {
    set funcArgs ""
  }
  
  Save {
    forevery function {
      if {$functionName != {}} {
        print "AddToFunc \"$functionName\""
        forevery functionEntry {
          if {[funcWithArgs $builtinCall]} {
            print "+\t\"[string index $events(name) 0]\"\t$builtinCall $arguments"
          } else {
           print "+\t\"[string index $events(name) 0]\"\t$builtinCall"
          }
        }
      }
    }
  }
}
