#! ../mofe --f

XmMainWindow main topLevel
 XmMenuBar menu_bar main 
    XmPulldownMenu mpa menu_bar unmanaged
      XmPushButton Quit mpa activateCallback quit
    XmCascadeButton Actions menu_bar subMenuId mpa

    XmPulldownMenu mph menu_bar unmanaged
      XmPushButton Help mph activateCallback "manageChild helpbox"
    XmCascadeButton Help menu_bar subMenuId mph
    sV menu_bar menuHelpWidget Help

 XmMessageDialog helpbox Help unmanaged \
	dialogTitle "$argv0 Help" okLabelString "Close" messageString "
  This script shows various input techniques for Motif text widgets.
  The input (typed characters or pasted text) can be vetoed 
  by the application or autmatically mapped to other characters. 
 
   - In the first example, all input characters (or inserted selections)
     are mapped to uppercase characters
   - in the second example, only numeric input is allowed
   - in the third example, only characterwise input at the end
     of the text is allowed. The dashes of a telephone number are
     inserted automatically
 
  Gustaf Neumann                     Mohegan Lake,  Oct 30, 1994
  "
  unmanageChild \
	[XmMessageBoxGetChild helpbox DIALOG_CANCEL_BUTTON] \
	[XmMessageBoxGetChild helpbox DIALOG_HELP_BUTTON] 

#-------------------- here starts the real work -----------------

 XmRowColumn rc main \
    packing PACK_COLUMN numColumns 3 orientation HORIZONTAL \
    entryAlignment ALIGNMENT_END

  XmLabel     text      rc labelString "Enter Text:"
  XmTextField text_edit rc \
      modifyVerifyCallback {insertUpper "%s" } 

  XmLabel     number      rc labelString "Enter Number:"
  XmTextField number_edit rc \
      modifyVerifyCallback {insertNumber "%s" } 

  XmLabel phone      rc labelString "Enter Phone Number:"
  XmText  phone_edit rc \
      modifyVerifyCallback { insertPhone %W %i %f "%s" } 

realize

proc insertUpper {string} {
  XmModifyVerifyCBset true -1 -1 -1 [string toupper $string]
}

proc insertNumber {string} {
  XmModifyVerifyCBset [regexp {^[0-9]*$} $string match] -1 -1 -1 $string
}

proc insertPhone {widget insertPos startPos string} {
  set lenBefore [XmTextGetLastPosition $widget]
  set lenAfter  [expr $lenBefore+[string length $string]]
  if {$insertPos < $lenBefore \
      || ![regexp {^[0-9]?$} $string match] \
      || $lenAfter>12 } {
    XmModifyVerifyCBset false -1 -1 -1 *
    return
  }
  switch $startPos {
    3 -
    7 { if [string match "" $string]   { incr startPos -1} }
    2 -
    6 { if [string compare "" $string] { append string - } }
  }
  XmModifyVerifyCBset true -1 $startPos -1 $string
}
  
