1 # $XConsortium: DtFuncs.sh /main/3 1995/11/01 15:48:57 rswiston $
2 ###############################################################################
3 # (c) Copyright 1993, 1994 Hewlett-Packard Company
4 # (c) Copyright 1993, 1994 International Business Machines Corp.
5 # (c) Copyright 1993, 1994 Sun Microsystems, Inc.
6 # (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of
8 ###############################################################################
10 ###############################################################################
12 # DtkshAddButtons - Convenience function for adding 1 or more buttons of the
13 # same kind into a composite widget. Most frequently
14 # used to add a collection of buttons into a menupane.
18 # DtkshAddButtons parent widgetClass label1 callback1 [label2 callback2 ...]
20 # DtkshAddButtons [-w] parent widgetClass variable1 label1 callback1 \
21 # [variable2 label2 callback2 ...]
23 # The "-w" option indicates that the convenience function should return
24 # the widget handle for each of the created buttons. The widget handle
25 # is returned in the specified environment variable.
27 # The widgetClass can be one of the following, and will default to the
28 # XmPushButtonGadget class, if not specified:
33 # XmToggleButtonGadget
35 # XmCascadeButtonGadget
39 # DtkshAddButtons $MENU XmPushButtonGadget Open do_Open Save do_Save Quit exit
41 # DtkshAddButtons -w $MENU XmPushButtonGadget B1 Open do_Open B2 Save do_Save
46 typeset parent widgetClass callback returnWidget="false" TMP=""
47 typeset -i paramCount=2
49 if [ $# -ge 1 ] && [ x"$1" = "x-w" ]; then
62 widgetClass=${1:-XmPushButtonGadget}
65 XmPushButtonGadget) callback=activateCallback;;
66 XmPushButton) callback=activateCallback;;
67 XmToggleButtonGadget) callback=valueChangedCallback;;
68 XmToggleButton) callback=valueChangedCallback;;
69 XmCascadeButtonGadget) callback=activateCallback;;
70 XmCascadeButton) callback=activateCallback;;
74 while [ $# -ge $paramCount ]
76 if [ "$returnWidget" = true ]; then
77 if [ ! "$3" = "" ]; then
78 XtCreateManagedWidget "$1" "$1" $widgetClass "$parent" \
79 labelString:"$2" ${callback}:"$3"
81 XtCreateManagedWidget "$1" "$1" $widgetClass "$parent" \
86 if [ ! "$2" = "" ]; then
87 XtCreateManagedWidget Id "btn" $widgetClass "$parent" \
88 labelString:"$1" ${callback}:"$2"
90 XtCreateManagedWidget Id "btn" $widgetClass "$parent" \
101 ###############################################################################
103 # DtkshSetReturnKeyControls - Convenience function for configuring a text
104 # widget (within a form!) so that the Return key does not
105 # activate the default button within the form, but instead
106 # moves the focus to the next text widget within the form.
107 # This is useful if you have a window which contains a
108 # series of text fields, and the default button should not
109 # be activated until the user presses the Return key in the
114 # DtkshSetReturnKeyControls textWidgetId nextTextWidgetId formWidgetId \
117 # The textWidgetId parameter specifies the widget which is to be configured
118 # to catch the 'Return' key, and force the focus to move to the next text
119 # widget (as indicated by the nextTextWidgetId parameter). The formWidgetId
120 # parameter specifies the form which contains the default button, and should
121 # be the parent of the two text widgets. The defaultButtonId indicates which
122 # component is to be treated as the default button within the form.
126 # DtkshSetReturnKeyControls $TEXT1 $TEXT2 $FORM $OK
127 # DtkshSetReturnKeyControls $TEXT2 $TEXT3 $FORM $OK
130 DtkshSetReturnKeyControls()
132 if [ $# -ne 4 ]; then
136 XtAddCallback $1 focusCallback "XtSetValues $3 defaultButton:NULL"
137 XtAddCallback $1 losingFocusCallback "XtSetValues $3 defaultButton:$4"
139 XtOverrideTranslations $1 \
140 "Ctrl<Key>Return:ksh_eval(\"XmProcessTraversal $2 TRAVERSE_CURRENT\")
141 <Key>Return:ksh_eval(\"XmProcessTraversal $2 TRAVERSE_CURRENT\")"
147 ###############################################################################
152 # DtkshLeftOf - Convenience functions for specifying form constraints.
153 # This set of functions allow a component to be attached
154 # to one of the edges of another component.
158 # DtkshUnder widgetId [offset]
159 # DtkshOver widgetId [offset]
160 # DtkshRightOf widgetId [offset]
161 # DtkshLeftOf widgetId [offset]
163 # The widgetId parameter specifies the widget to which the current
164 # component is to be attached. The offset value is optional, and
165 # defaults to 0 if not specified.
169 # XtCreateManagedWidget BUTTON2 button2 XmPushButton $FORM \
170 # labelString:"Exit" \
171 # $(DtkshUnder $BUTTON1)
176 if [ $# -lt 1 ]; then
180 echo "topWidget:$1 topAttachment:ATTACH_WIDGET topOffset:${2:-0}"
185 if [ $# -lt 1 ]; then
189 echo "bottomWidget:$1 bottomAttachment:ATTACH_WIDGET bottomOffset:${2:-0}"
194 if [ $# -lt 1 ]; then
198 echo "leftWidget:$1 leftAttachment:ATTACH_WIDGET leftOffset:${2:-0}"
203 if [ $# -lt 1 ]; then
207 echo "rightWidget:$1 rightAttachment:ATTACH_WIDGET rightOffset:${2:-0}"
212 ###############################################################################
217 # DtkshFloatBottom - Convenience functions for specifying form constraints.
218 # This set of functions allow a component to be positioned
219 # independent of the other components within the form.
220 # As the form grows or shrinks, the component maintains
221 # its relative position within the form. The component
222 # may still grow or shrink, depending upon the other form
223 # constraints which have been specified for the component.
227 # DtkshFloatRight [position]
228 # DtkshFloatLeft [position]
229 # DtkshFloatTop [position]
230 # DtkshFloatBottom [position]
232 # The optional position parameter specifies the relative position
233 # to which the indicated edge of the component will be positioned.
234 # A default position is used, if not specified.
238 # XtCreateManagedWidget BUTTON1 button1 XmPushButton $FORM \
240 # $(DtkshUnder $SEPARATOR) \
241 # $(DtkshFloatLeft 10) \
242 # $(DtkshFloatRight 40)
247 echo "rightAttachment:ATTACH_POSITION rightPosition:${1:-0}"
252 echo "leftAttachment:ATTACH_POSITION leftPosition:${1:-0}"
257 echo "topAttachment:ATTACH_POSITION topPosition:${1:-0}"
262 echo "bottomAttachment:ATTACH_POSITION bottomPosition:${1:-0}"
267 ###############################################################################
272 # DtkshAnchorBottom - Convenience functions for specifying form constraints.
273 # This set of functions allow a component to be attached
274 # to one of the edges of the form in such a fashion that
275 # as the form grows or shrinks, the component's position
276 # does not change. However, depending upon the other
277 # form constaints set on this component, the component
278 # may still grow or shrink in size.
282 # DtkshAnchorRight [offset]
283 # DtkshAnchorLeft [offset]
284 # DtkshAnchorTop [offset]
285 # DtkshAnchorBottom [offset]
287 # The optional offset parameter specifies how far from the edge
288 # of the form the component should be positioned. If an offset
289 # is not specified, then 0 is user.
293 # XtCreateManagedWidget BUTTON1 button1 XmPushButton $FORM \
295 # $(DtkshUnder $SEPARATOR) \
296 # $(DtkshAnchorLeft 10) \
297 # $(DtkshAnchorBottom 10)
302 echo "rightAttachment:ATTACH_FORM rightOffset:${1:-0}"
307 echo "leftAttachment:ATTACH_FORM leftOffset:${1:-0}"
312 echo "topAttachment:ATTACH_FORM topOffset:${1:-0}"
317 echo "bottomAttachment:ATTACH_FORM bottomOffset:${1:-0}"
322 ###############################################################################
325 # DtkshSpanHeight - Convenience functions for specifying form constraints.
326 # This set of functions allow a component to be configured
327 # such that it spans either the full height or width of
328 # the form widget. This effect is accomplished by attaching
329 # two edges of the component (top & bottom for DtkshSpanHeight,
330 # and left and right for DtkshSpanWidth) to the form. The
331 # component will typically resize whenever the form is
336 # DtkshSpanWidth [offset]
337 # DtkshSpanHeight [offset]
339 # The optional offset parameter specifies how far from the edge
340 # of the form the component should be positioned. If an offset
341 # is not specified, then 0 is user.
345 # XtCreateManagedWidget SEPARATOR $FORM XmSeparator \
346 # $(DtkshSpanWidth 1 1)
351 echo "leftAttachment:ATTACH_FORM leftOffset:${1:-0} \
352 rightAttachment:ATTACH_FORM rightOffset:${2:-0}"
357 echo "topAttachment:ATTACH_FORM topOffset:${1:-0} \
358 bottomAttachment:ATTACH_FORM bottomOffset:${2:-0}"
363 ###############################################################################
365 # DtkshDisplayInformationDialog
366 # DtkshDisplayQuestionDialog
367 # DtkshDisplayWarningDialog
368 # DtkshDisplayWorkingDialog
369 # DtkshDisplayErrorDialog - Convenience functions for creating a single
370 # instance of each of the flavors of the Motif
371 # feedback dialog. If an instance of the requested
372 # type of dialog already exists, then it will be
373 # reused. The parent of the dialog is obtained
374 # from the environment variable $TOPLEVEL, which
375 # should be set by the calling shell script. The
376 # handle for the requested dialog is returned in
377 # one of the following environment variables:
379 # _DT_ERROR_DIALOG_HANDLE
380 # _DT_QUESTION_DIALOG_HANDLE
381 # _DT_WORKING_DIALOG_HANDLE
382 # _DT_WARNING_DIALOG_HANDLE
383 # _DT_INFORMATION_DIALOG_HANDLE
385 # WARNING: IF ATTACHING YOUR OWN CALLBACKS TO THE DIALOG
386 # BUTTONS, DO NOT DESTROY THE DIALOG WHEN YOU
387 # ARE DONE WITH IT; SIMPLY UNMANAGE THE DIALOG,
388 # SO THAT IT CAN BE USED AT A LATER TIME.
392 # DtkshDisplay*Dialog title message okCallback closeCallback helpCallback \
395 # The "Ok" button is always managed, and by default will simply unmanage
396 # the dialog. The "Cancel" and "Help" buttons are only managed when a
397 # callback is supplied for them.
399 # The "dialogStyle" parameter accepts any of the standard resource settings
400 # supported by the bulletin board widget.
404 # DtkshDisplayErrorDialog "Read Error" "Unable to read the file" "OkCallback" \
405 # "CancelCallback" "" DIALOG_PRIMARY_APPLICATION_MODAL
409 # Global feedback dialog handles
410 _DT_ERROR_DIALOG_HANDLE=""
411 _DT_QUESTION_DIALOG_HANDLE=""
412 _DT_WORKING_DIALOG_HANDLE=""
413 _DT_WARNING_DIALOG_HANDLE=""
414 _DT_INFORMATION_DIALOG_HANDLE=""
415 _DT_TMP_DIALOG_HANDLE=""
418 # This function was accidentally *not* renamed to DtkshDisplayErrorDialog
419 # when the sample implementation of dtksh was released; however, the
420 # documentation tells users to use DtkshDisplayErrorDialog, but it did not
421 # exist. Therefore, to be backwards compatible, both DtDisplayErrorDialog
422 # and DtkshDisplayErrorDialog are defined.
424 DtDisplayErrorDialog()
426 _DtkshDisplayFeedbackDialog "$_DT_ERROR_DIALOG_HANDLE" "Error" "${@:-}"
427 if [ "$_DT_ERROR_DIALOG_HANDLE" = "" ] ; then
428 _DT_ERROR_DIALOG_HANDLE=$_DT_TMP_DIALOG_HANDLE
433 DtkshDisplayErrorDialog()
435 _DtkshDisplayFeedbackDialog "$_DT_ERROR_DIALOG_HANDLE" "Error" "${@:-}"
436 if [ "$_DT_ERROR_DIALOG_HANDLE" = "" ] ; then
437 _DT_ERROR_DIALOG_HANDLE=$_DT_TMP_DIALOG_HANDLE
442 DtkshDisplayQuestionDialog()
444 _DtkshDisplayFeedbackDialog "$_DT_QUESTION_DIALOG_HANDLE" "Question" "${@:-}"
445 if [ "$_DT_QUESTION_DIALOG_HANDLE" = "" ] ; then
446 _DT_QUESTION_DIALOG_HANDLE=$_DT_TMP_DIALOG_HANDLE
451 DtkshDisplayWorkingDialog()
453 _DtkshDisplayFeedbackDialog "$_DT_WORKING_DIALOG_HANDLE" "Working" "${@:-}"
454 if [ "$_DT_WORKING_DIALOG_HANDLE" = "" ] ; then
455 _DT_WORKING_DIALOG_HANDLE=$_DT_TMP_DIALOG_HANDLE
460 DtkshDisplayWarningDialog()
462 _DtkshDisplayFeedbackDialog "$_DT_WARNING_DIALOG_HANDLE" "Warning" "${@:-}"
463 if [ "$_DT_WARNING_DIALOG_HANDLE" = "" ] ; then
464 _DT_WARNING_DIALOG_HANDLE=$_DT_TMP_DIALOG_HANDLE
469 DtkshDisplayInformationDialog()
471 _DtkshDisplayFeedbackDialog "$_DT_INFORMATION_DIALOG_HANDLE" "Information" \
473 if [ "$_DT_INFORMATION_DIALOG_HANDLE" = "" ] ; then
474 _DT_INFORMATION_DIALOG_HANDLE=$_DT_TMP_DIALOG_HANDLE
481 ###############################################################################
483 # DtkshDisplayQuickHelpDialog
484 # DtkshDisplayHelpDialog - Convenience functions for creating a single
485 # instance of a help dialog and a quick help
486 # dialog. If an instance of the requested type
487 # of help dialog already exists, then it will be
488 # reused. The parent of the dialog is obtained
489 # from the environment variable $TOPLEVEL, which
490 # should be set by the calling shell script. The
491 # handle for the requested dialog is returned in
492 # one of the following environment variables:
494 # _DT_HELP_DIALOG_HANDLE
495 # _DT_QUICK_HELP_DIALOG_HANDLE
497 # WARNING: DO NOT DESTROY THIS DIALOG, UNLESS YOU ALSO CLEAR THE
498 # CORRESPONDING ENVIRONMENT VARIABLE, SO THAT THIS CODE
499 # WILL NOT ATTEMPT TO REUSE THE DIALOG AGAIN.
503 # DtkshDisplay*HelpDialog title helpType helpInformation [locationId]
505 # The meaning of the parameters is dependent upon the value specified
506 # for the 'helpType' parameter. There meanings are explained below:
508 # helpType = HELP_TYPE_TOPIC
509 # helpInformation = help volume name
510 # locationId = help topic location id
512 # helpType = HELP_TYPE_STRING
513 # helpInformation = help string
514 # locationId = <not used>
516 # helpType = HELP_TYPE_DYNAMIC_STRING
517 # helpInformation = help string
518 # locationId = <not used>
520 # helpType = HELP_TYPE_MAN_PAGE
521 # helpInformation = man page name
522 # locationId = <not used>
524 # helpType = HELP_TYPE_FILE
525 # helpInformation = help file name
526 # locationId = <not used>
530 # DtkshDisplayHelpDialog "Help On Dtksh" HELP_TYPE_FILE "HelpFileName"
534 # Global help dialog handles
535 _DT_HELP_DIALOG_HANDLE=""
536 _DT_QUICK_HELP_DIALOG_HANDLE=""
539 DtkshDisplayQuickHelpDialog()
541 _DtkshDisplayHelpDialog "$_DT_QUICK_HELP_DIALOG_HANDLE" "Quick" "${@:-}"
542 if [ "$_DT_QUICK_HELP_DIALOG_HANDLE" = "" ] ; then
543 _DT_QUICK_HELP_DIALOG_HANDLE=$_DT_TMP_DIALOG_HANDLE
548 DtkshDisplayHelpDialog()
550 _DtkshDisplayHelpDialog "$_DT_HELP_DIALOG_HANDLE" "" "${@:-}"
551 if [ "$_DT_HELP_DIALOG_HANDLE" = "" ] ; then
552 _DT_HELP_DIALOG_HANDLE=$_DT_TMP_DIALOG_HANDLE
557 ##############################################################################
559 # This internal shell function performs most of the work required to
560 # create an instance of a feedback dialog (error, warning, information,
561 # working and question). It will reuse an existing instance of the
562 # requested type of feedback dialog, if one has already been created;
563 # otherwise, it will create a new one.
565 # The "Ok" button is always managed, and by default will simply unpost
566 # the dialog. The "Cancel" and "Help" buttons are only managed if the
567 # callers specifies a callback for the butttons. Both the "Ok" and
568 # "Cancel" buttons rely on the fact that the 'autoUnpost' resource for
569 # the dialog is 'True'.
571 # The implied parent of the dialog is identified by the environment
572 # variable '$TOPLEVEL'.
574 # The incoming parameters are defined as follows (note that $1 and $2 are
575 # defined by the convenience function which is calling us, while $3 - $8
576 # are the parameters which were passed by the caller to the convenience
579 # $1 = existing dialog handle, or "" if first time
580 # $2 = type of feedback dialog (Information, Question, Working, ... )
582 # $4 = message string
584 # $6 = cancelCallback
589 _DtkshDisplayFeedbackDialog()
591 if [ "$1" = "" ]; then
592 XmCreate${2}Dialog _DT_TMP_DIALOG_HANDLE $TOPLEVEL "$2"
594 _DT_TMP_DIALOG_HANDLE=$1
597 XtSetValues $_DT_TMP_DIALOG_HANDLE \
598 dialogTitle:"${3:-$2}" \
599 messageString:"${4:- }" \
600 dialogStyle:"${8:-DIALOG_MODELESS}"
602 if [ $# -ge 5 ] && [ "$5" != "" ]; then
603 XtSetValues $_DT_TMP_DIALOG_HANDLE okCallback:"$5"
606 if [ $# -lt 6 ] || [ "$6" = "" ]; then
607 XtUnmanageChild $(XmMessageBoxGetChild "-" $_DT_TMP_DIALOG_HANDLE \
608 DIALOG_CANCEL_BUTTON)
610 XtSetValues $_DT_TMP_DIALOG_HANDLE cancelCallback:"$6"
613 if [ $# -lt 7 ] || [ "$7" = "" ]; then
614 XtUnmanageChild $(XmMessageBoxGetChild "-" $_DT_TMP_DIALOG_HANDLE \
617 XtSetValues $_DT_TMP_DIALOG_HANDLE helpCallback:"$7"
620 _DtkshPositionDialog "$1"
621 XtManageChild $_DT_TMP_DIALOG_HANDLE
626 ##############################################################################
628 # This internal shell function performs most of the work required to
629 # create an instance of a help dialog (regular help or quick help)
630 # It will reuse an existing instance of the requested type of help
631 # dialog, if one has already been created; otherwise, it will create
634 # The implied parent of the dialog is identified by the environment
635 # variable '$TOPLEVEL'.
637 # The incoming parameters are defined as follows (note that $1 and $2 are
638 # defined by the convenience function which is calling us, while $3 - $6
639 # are the parameters which were passed by the caller to the convenience
642 # $1 = existing dialog handle, or "" if first time
643 # $2 = type of help dialog (Quick or "")
646 # $5 = help information:
647 # help volume (if help type = HELP_TYPE_TOPIC)
648 # help string (if help type = HELP_TYPE_STRING)
649 # help string (if help type = HELP_TYPE_DYNAMIC_STRING)
650 # man page name (if help type = HELP_TYPE_MAN_PAGE)
651 # help file name (if help type = HELP_TYPE_FILE)
652 # $6 = help location Id (if help type = HELP_TYPE_TOPIC)
655 _DtkshDisplayHelpDialog()
657 typeset helpType ARG1="" ARG2="" ARG3=""
658 typeset helpType VAL1="" VAL2="" VAL3=""
660 helpType="${4:-HELP_TYPE_TOPIC}"
665 HELP_TYPE_TOPIC) ARG2="helpVolume:"
668 VAL3="${6:-_HOMETOPIC}";;
669 HELP_TYPE_STRING) ARG2="stringData:"
671 HELP_TYPE_DYNAMIC_STRING) ARG2="stringData:"
673 HELP_TYPE_MAN_PAGE) ARG2="manPage:"
675 HELP_TYPE_FILE) ARG2="helpFile:"
680 if [ "$1" = "" ]; then
681 if [ "$ARG3" != "" ]; then
682 DtCreateHelp${2}Dialog _DT_TMP_DIALOG_HANDLE $TOPLEVEL "$2" \
683 "${ARG1}${VAL1}" "${ARG2}${VAL2}" "${ARG3}${VAL3}"
685 DtCreateHelp${2}Dialog _DT_TMP_DIALOG_HANDLE $TOPLEVEL "$2" \
686 "${ARG1}${VAL1}" "${ARG2}${VAL2}"
689 _DT_TMP_DIALOG_HANDLE=$1
690 if [ "$ARG3" != "" ]; then
691 XtSetValues $_DT_TMP_DIALOG_HANDLE \
692 "${ARG1}${VAL1}" "${ARG2}${VAL2}" "${ARG3}${VAL3}"
694 XtSetValues $_DT_TMP_DIALOG_HANDLE \
695 "${ARG1}${VAL1}" "${ARG2}${VAL2}"
699 if [ "$2" = "Quick" ]; then
700 XtSetSensitive $(DtHelpQuickDialogGetChild "-" $_DT_TMP_DIALOG_HANDLE \
701 HELP_QUICK_HELP_BUTTON) false
703 XtSetValues $(XtParent "-" $_DT_TMP_DIALOG_HANDLE) title:"${3:-Help}"
704 _DtkshPositionDialog "$1"
705 XtManageChild $_DT_TMP_DIALOG_HANDLE
710 ##############################################################################
712 # This internal shell function takes care of positioning the dialog so
713 # that it is centered over the window for which it is transient; if the
714 # window it is transient for is not currently managed, then the window
715 # will be positioned over in the center of the screen.
717 # Positioning does not occur that first time the dialog is posted; that
718 # is taken care of automatically by Motif and the window manager. It
719 # only needs to happen for subsequent postings.
722 _DtkshPositionDialog()
724 typeset -i WIDTH HEIGHT X_P Y_P WIDTH_P HEIGHT_P
725 typeset -i finalX finalY
727 if [ "$1" != "" ] && ! XtIsManaged $1 && XtIsShell $TOPLEVEL ; then
728 XtGetValues $1 width:WIDTH height:HEIGHT
729 if XtIsRealized $TOPLEVEL; then
730 XtGetValues $TOPLEVEL x:X_P y:Y_P width:WIDTH_P height:HEIGHT_P
731 (( finalX=$X_P+($WIDTH_P-$WIDTH)/2 ))
732 (( finalY=$Y_P+($HEIGHT_P-$HEIGHT)/2 ))
734 (( finalX=($(XWidthOfScreen "-" $(XtScreen "-" $1) )-$WIDTH)/2 ))
735 (( finalY=($(XHeightOfScreen "-" $(XtScreen "-" $1) )-$HEIGHT)/2 ))
737 XtSetValues $(XtParent "-" $1) x:$finalX y:$finalY