MetaCard Sample Application (font chooser)

Size field script:
# The closeField message is sent when the field is losing the focus and
# the value in the field *has* changed (otherwise exitField is sent).
on closeField
  resetFont # resetFont message goes up hierarchy to stack script
end closeField

Size scrollbar script:
# The scrollbarLineInc message is sent when the up arrow button is
# pressed. The validation operator "is a number" is used to verify
# that the field has a number in before subtracting one from it.
on scrollbarLineInc
  if field "Font Size" is a number
  then subtract 1 from field "Font Size"
  else put "1" into field "Font Size"
  resetFont
end scrollbarLineInc

# The scrollbarLineDec message is sent when the down arrow button is
# pressed. 
on scrollbarLineDec
  if field "Font Size" is a number  
  then add 1 to field "Font Size"
  else put "1" into field "Font Size"
  resetFont
end scrollbarLineDec

Stack script:
# All controls send the help message when the F1 or keyboard Help keys
# are pressed.  This one handler handles messages from all of them.
# card "Font chooser" of stack "Dialog Box Help" is one card in a
# MetaCard stack that would have help for each of the dialog boxes.
on help
  go to card "Font chooser" of stack "Dialog Box Help"
end help

# All button clicks end up here in the stack script if they aren't
# handled in the scripts for the various controls
on mouseUp
  resetFont
end mouseUp

# This function handler builds a string used to specify a font in the
# X Logical Font Description system.
function getXLFD
# The selectedText is the currently hilited list item when a field is
# used as a list box
  put the selectedText of field "Font Name" into facename
  put "*-" & facename into fstring
  if the hilite of button "Bold"
  then put "-bold" after fstring
  else put "-medium" after fstring
# Need to special case Helvetica and Courier (it may be logical, but
# XLFD certainly isn't consistent ;-).
  if the hilite of button "Italic" then
    if facename is "Helvetica" or facename is "Courier"
    then put "-o-normal--" after fstring
    else put "-i-normal--" after fstring
  else put "-r-normal--" after fstring
  put field "Font Size" & "-*" after fstring
  return fstring
end getXLFD

# This function resets the font used for the Preview field.  It's
# called from the mouseUp handler and from the closeField handler in
# the size field script.  Note the use of the user function getXLFD
on resetFont
  set the textFont of field "Preview" to getXLFD()
end resetFont

# returnKey is send when the return (enter) key is pressed when a
# control other than a field has the focus (returnInField is sent when
# a field has the focus).  This handler implements the default
# behavior by sending the mouseUp message to the OK button.
on returnKey
  send mouseUp to button "OK"
end returnKey

on returnInField
  send mouseUp to button "OK"
end returnInField

Cancel button script:
on mouseUp
  close this stack
end mouseUp

OK button script:
# Writing a string to stdout prints it out on the console.  The
# "return" constant, which is actually a linefeed on UNIX systems,
# needs to be written too.
on mouseUp
  write getXLFD() & return to stdout
  close this stack
end mouseUp