#! ../mofe --f
# This is a sample script how a password entry field could be implemented
# in the OSF/Motif version of Wafe by defining
#
#   - PasswordEntryField: for creating
#   - getPassword: for retrieving the Password
#   - clearPassword: for clearing the contents of the Password 
#   - PasswordInsertCharacter: which is doing the actual work
#
# These procedures should be included/auto-loaded for other mofe scripts
# dealing with password entries.
#
# The PasswordEntryField could be implemented more sophisticated
# by using wafeWid.tcl and retrieving the password via resource.
#
# Gustaf Neumann                         Aug, 9, 1994, Mohegan Lake, NY 


# the procedure PasswordEntryField can be used like a widget creation command
# where ``args'' keeps the resource value pairs
proc PasswordEntryField {name parent args} {
  global _Password
  set _Password([eval "XmText $name $parent $args translations {#override \n\
        <Key>: exec(PasswordInsertCharacter %W {%s} {%a})                 \n\
        <BtnDown>: exec()                                                 \n\
      }" ]) ""
}

# the password can be retrieved via getPassword
proc getPassword {widgetReference} {
  global _Password
  return $_Password([widgetId $widgetReference])
}

# .. and cleared via clearPassword
proc clearPassword {widgetReference} {
  PasswordInsertCharacter [widgetId $widgetReference] Escape ""
}

# PasswordInsertCharacter is the action routine called for each keypress
proc PasswordInsertCharacter {textWidgetID sym ascii} {
  upvar #0 _Password($textWidgetID) pwd
  switch $sym {
    BackSpace { if [string compare "" $pwd] {
                  set pwd [string range $pwd 0 [expr [string length $pwd]-2]]
	          callActionProc $textWidgetID {} delete-previous-character }
	      }
    Escape    { set pwd {}
                callActionProc $textWidgetID {} delete-previous-word
              }
    Return    { callCallbacks $textWidgetID activateCallback
              }
    default   { if {[string length $sym] == 1} {
	          append pwd $ascii
	          callActionProc $textWidgetID {} insert-string * }
              }
  }
}

# now we can use the password entry field...
PasswordEntryField pwd topLevel \
    activateCallback {puts stderr "The password is <[getPassword %W]>"} 
realize

