### 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 keys {} {
  # Menus and Bindings / Key Bindings
  Desc "Definitions of key bindings"
  ShortDesc "Key bindings"

  Header h -text "Key Bindings" -frame:relief flat \
      -help "On this page you configure key bindings."
  Line l
  Label index -text "Desc:" -label:relief groove -packLabel:ipadx 5

  ComboBox keysym -text "Keysym" -entries { Left Right Up Down
    F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 L1 L2 L3 L4 L5 L6 L7 L8 L9 L10
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  } \
      -packFrame:anchor nw \
      -help "Select a keysym from the list, if the desired keysym isn't" \
      "present you can enter it yourself."

  ListBox keyContext -text "Context" -entries {
    Any Root Window "Title bar" Sides Frame Icon "0 button" "1 button"\
	"2 button" "3 button" "4 button" "5 button" "6 button" "7 button" \
	"8 button" "9 button"} -width 8 \
      -selectmode multiple \
      -help "Select the context(s) in which to bind the key"
  ListBox keyModifier -text "Modifier" -entries {
    Any Control Shift Meta 1mod 2mod 3mod 4mod 5mod} -width 8 \
      -selectmode multiple \
      -help "Select the modifier(s) to press in conjunction with the key"
  Label funcLabel -text "Function to call" \
      -help "This is a list of the built-in functions you can bind the key to"
  global Builtins
  ComboBox builtinCall -noedit 1 -default Beep -entryhelp $Builtins \
      -help "Select a built-in function to call from this menu"

  Label argLabel -text "Arguments" \
      -help "Any additional arguments are entered here (the syntax is your" \
      "responsibility) - see help for the individual commands."
  Entry keyArgs \
      -help "Any additional arguments are entered here (the syntax is your" \
      "responsibility) - see help for the individual commands."
  Frame called -entries {funcLabel builtinCall argLabel keyArgs} -orient top

  Frame keyFrame -entries {keyContext keyModifier called}
  ExtEntry keys -lines 1 -count 1 -entries {index keysym keyFrame} \
      -index index -orient top

  Label funcArgs -justify left -wraplength 400

  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] argLabel keyArgs"
    }
  }
  ShowPage {
    set funcArgs ""
  }
  
  PageEnd {
    forevery keys {
      if {$keysym == ""} {
	error "You must specify keysym in key binding."
      }
      if { [llength $keyContext(name)] == 0 } {
	error "You must specify context(s) in key binding."
      }
    }
  }

  Change {
    if {$changeElm == "keyModifier" || $changeElm == "keysym" ||
	$changeElm == "keyContext"} {
      set index "Key: [makeContextOrModifier $keyModifier(name)] $keysym, Context: [makeContextOrModifier $keyContext(name)]"
    }
  }

  Save {
    forevery keys {
      set context [makeContextOrModifier $keyContext(name)]
      set modifier [makeContextOrModifier $keyModifier(name)]
      print "Key $keysym $context $modifier $builtinCall $keyArgs"
    }
  }
}
