dtksh Sample Application (font chooser)


# This function builds the XLFD string and echos it to its standard output.

function getXLFD {
# declare local variables
  typeset facename set
  typeset CURFONT

# Get the selected items from FN list box, and put them into variable facename.
  XtGetValues $FN selectedItems:facename

# concat the face name to the start of the XLFD
  XtGetValues $BOLD set:set
  if [[ $set == true ]]
  then CURFONT="${CURFONT}-bold"
  else CURFONT="${CURFONT}-medium"
  XtGetValues $ITALIC set:set
  if [[ $set == true ]] then
    if [[ facename = "Helvetica" || facename = "Courier" ]]
    then CURFONT="${CURFONT}-o-normal--"
    else CURFONT="${CURFONT}-i-normal--"
  else CURFONT="${CURFONT}-r-normal--"
  XtGetValues $FS value:size
  echo "$CURFONT"

# Set the text font used for the preview widget to the value build in
# the function getXLFD.
# This function must be added to to the appropriate callback list for
# each control, since there is no message passing hierarchy.
function RESET_CB {
  XtSetValues $PREVIEW fontList:"$(getXLFD)"

alias OK_CB='getXLFD;exit'

alias CANCEL_CB=exit

alias HELP_CB='DtDisplayQuickHelpDialog "HELP" HELP_TYPE_STRING \
	"This is a help string, I could have also specified a file."'

# Increment/decrement value in size text widget when the up arrow button is
# pressed.  First argument is either 1 or -1.
function ARROW_CB {
  typeset size
  XtGetValues $FS value:size

  if [[ $size = +([0-9]) ]]
  then (( size = size + $1 ))
  else size=1
  XtSetValues $FS value:$size

# set the default font.  Note that full XLFD names must be supplied:
XtInitialize TOPLEVEL font_chooser "Font Chooser" "$@"
XmCreateForm FORM $TOPLEVEL main_form width:346 height:334

# have to set the fonts for each type of objects separately
XtSetValues $FORM textFontList:"$DEFAULT_FONT" \
	buttonFontList:"$DEFAULT_FONT" labelFontList:"$DEFAULT_FONT"
XmCreateLabel L1 $FORM name_label labelString:"Name:" \
	x:8 y:4 width:64 height:32 alignment:ALIGNMENT_BEGINNING

XmCreateScrolledList FN $FORM font_name visibleItemCount:7
XtSetValues $(XtParent - $FN) x:16 y:38
XtAddCallback $FN browseSelectionCallback RESET_CB

XmListAddItems $FN 0 "Charter" "Clean" "Courier" "Helvetica" "Lucida" \
	"LucidaBright" "LucidaTypewriter" "New Century Schoolbook" \
	"Symbol" "Times" "fixed" "terminal"

XmCreateLabel L2 $FORM size_label labelString:"Size:" \
	x:224 y:40 width:48 height:32 alignment:ALIGNMENT_BEGINNING
XmCreateText FS $FORM font_size value:"14" \
	x:272 y:40 width:40 height:32
XtAddCallback $FS losingFocusCallback RESET_CB
XmCreateArrowButton UA $FORM ab1 \
	x:312 y:36 height:22
XtAddCallback $UA activateCallback 'ARROW_CB 1'
XmCreateArrowButton DA $FORM ab2 arrowDirection:ARROW_DOWN \
	x:312 y:58 height:24
XtAddCallback $DA activateCallback 'ARROW_CB -1'

XmCreateLabel L3 $FORM style_label labelString:"Style:" \
	x:224 y:104 width:52 height:32 alignment:ALIGNMENT_BEGINNING
XmCreateToggleButton BOLD $FORM bold labelString:"Bold" \
	x:276 y:104 width:60 height:28
XtAddCallback $BOLD valueChangedCallback RESET_CB
XmCreateToggleButton ITALIC $FORM italic labelString:"Italic" \
	x:276 y:132 width:60 height:28
XtAddCallback $ITALIC valueChangedCallback RESET_CB

XmCreateLabel L4 $FORM size_label labelString:"Size:" \
	x:12 y:200 width:76 height:32 alignment:ALIGNMENT_BEGINNING
XmCreateText PREVIEW $FORM preview value:"Sample text" \
	x:86 y:200 width:246 height:72

XmCreatePushButton OK $FORM ok_button labelString:"OK" showAsDefault:true \
	x:13 y:284 width:104 height:44
XtAddCallback $OK activateCallback OK_CB

XmCreatePushButton CANCEL $FORM ok_button labelString:"Cancel" \
	x:129 y:288 width:96 height:36
XtAddCallback $CANCEL activateCallback CANCEL_CB

XmCreatePushButton HELP $FORM ok_button labelString:"Help" \
	x:238 y:288 width:96 height:36
XtAddCallback $HELP activateCallback HELP_CB

# Manage each of the children created, since they aren't managed when created.

XtManageChildren $L1 $L2 $L3 $L4 $FS $UA $DA $BOLD $ITALIC $PREVIEW
XtManageChildren $FN
XtManageChildren $OK $CANCEL $HELP
XtManageChildren $FORM
XtRealizeWidget $TOPLEVEL