home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09962.iso / vrml / cp2b2x.exe / DATA.Z / dialog.tcl < prev    next >
Text File  |  1996-04-23  |  4KB  |  124 lines

  1. # dialog.tcl --
  2. #
  3. # This file defines the procedure tk_dialog, which creates a dialog
  4. # box containing a bitmap, a message, and one or more buttons.
  5. #
  6. # @(#) dialog.tcl 1.19 95/09/27 09:51:36
  7. #
  8. # Copyright (c) 1992-1993 The Regents of the University of California.
  9. # Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10. #
  11. # See the file "license.terms" for information on usage and redistribution
  12. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13. #
  14.  
  15. #
  16. # tk_dialog:
  17. #
  18. # This procedure displays a dialog box, waits for a button in the dialog
  19. # to be invoked, then returns the index of the selected button.
  20. #
  21. # Arguments:
  22. # w -        Window to use for dialog top-level.
  23. # title -    Title to display in dialog's decorative frame.
  24. # text -    Message to display in dialog.
  25. # bitmap -    Bitmap to display in dialog (empty string means none).
  26. # default -    Index of button that is to display the default ring
  27. #        (-1 means none).
  28. # args -    One or more strings to display in buttons across the
  29. #        bottom of the dialog box.
  30.  
  31. proc tk_dialog {w title text bitmap default args} {
  32.     global tkPriv
  33.  
  34.     # 1. Create the top-level window and divide it into top
  35.     # and bottom parts.
  36.  
  37.     catch {destroy $w}
  38.     toplevel $w -class Dialog
  39.     wm title $w $title
  40.     wm iconname $w Dialog
  41.     wm protocol $w WM_DELETE_WINDOW { }
  42.     wm transient $w [winfo toplevel [winfo parent $w]]
  43.     frame $w.top -relief raised -bd 1
  44.     pack $w.top -side top -fill both
  45.     frame $w.bot -relief raised -bd 1
  46.     pack $w.bot -side bottom -fill both
  47.  
  48.     # 2. Fill the top part with bitmap and message (use the option
  49.     # database for -wraplength so that it can be overridden by
  50.     # the caller).
  51.  
  52.     option add *Dialog.msg.wrapLength 3i widgetDefault
  53.     label $w.msg -justify left -text $text \
  54.         -font -Adobe-Times-Medium-R-Normal--*-180-*-*-*-*-*-*
  55.     pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 3m -pady 3m
  56.     if {$bitmap != ""} {
  57.     label $w.bitmap -bitmap $bitmap
  58.     pack $w.bitmap -in $w.top -side left -padx 3m -pady 3m
  59.     }
  60.  
  61.     # 3. Create a row of buttons at the bottom of the dialog.
  62.  
  63.     set i 0
  64.     foreach but $args {
  65.     button $w.button$i -text $but -command "set tkPriv(button) $i"
  66.     if {$i == $default} {
  67.         frame $w.default -relief sunken -bd 1
  68.         raise $w.button$i $w.default
  69.         pack $w.default -in $w.bot -side left -expand 1 -padx 3m -pady 2m
  70.         pack $w.button$i -in $w.default -padx 2m -pady 2m
  71.         bind $w <Return> "$w.button$i flash; set tkPriv(button) $i"
  72.     } else {
  73.         pack $w.button$i -in $w.bot -side left -expand 1 \
  74.             -padx 3m -pady 2m
  75.     }
  76.     incr i
  77.     }
  78.  
  79.     # 4. Withdraw the window, then update all the geometry information
  80.     # so we know how big it wants to be, then center the window in the
  81.     # display and de-iconify it.
  82.  
  83.     wm withdraw $w
  84.     update idletasks
  85.     set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
  86.         - [winfo vrootx [winfo parent $w]]]
  87.     set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
  88.         - [winfo vrooty [winfo parent $w]]]
  89.     wm geom $w +$x+$y
  90.     wm deiconify $w
  91.  
  92.     # 5. Set a grab and claim the focus too.
  93.  
  94.     set oldFocus [focus]
  95.     set oldGrab [grab current $w]
  96.     if {$oldGrab != ""} {
  97.     set grabStatus [grab status $oldGrab]
  98.     }
  99.     grab $w
  100.     if {$default >= 0} {
  101.     focus $w.button$default
  102.     } else {
  103.     focus $w
  104.     }
  105.  
  106.     # 6. Wait for the user to respond, then restore the focus and
  107.     # return the index of the selected button.  Restore the focus
  108.     # before deleting the window, since otherwise the window manager
  109.     # may take the focus away so we can't redirect it.  Finally,
  110.     # restore any grab that was in effect.
  111.  
  112.     tkwait variable tkPriv(button)
  113.     catch {focus $oldFocus}
  114.     destroy $w
  115.     if {$oldGrab != ""} {
  116.     if {$grabStatus == "global"} {
  117.         grab -global $oldGrab
  118.     } else {
  119.         grab $oldGrab
  120.     }
  121.     }
  122.     return $tkPriv(button)
  123. }
  124.