home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tkisrc04.zip / lib / tk / dialog.tcl < prev    next >
Text File  |  1998-09-09  |  4KB  |  141 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. # SCCS: @(#) dialog.tcl 1.25 96/04/10 15:43:33
  7. #
  8. # Copyright (c) 1992-1993 The Regents of the University of California.
  9. # Copyright (c) 1994-1996 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.  
  43.     # The following command has been removed because if it is present
  44.     # the dialog won't be posted if [winfo parent $w] is iconified.
  45.  
  46. #    wm transient $w [winfo toplevel [winfo parent $w]]
  47.     frame $w.bot -relief raised -bd 1
  48.     pack $w.bot -side bottom -fill both
  49.     frame $w.top -relief raised -bd 1
  50.     pack $w.top -side top -fill both -expand 1
  51.  
  52.     # 2. Fill the top part with bitmap and message (use the option
  53.     # database for -wraplength so that it can be overridden by
  54.     # the caller).
  55.  
  56.     option add *Dialog.msg.wrapLength 3i widgetDefault
  57.     label $w.msg -justify left -text $text
  58.     catch {$w.msg configure -font \
  59.         -Adobe-Times-Medium-R-Normal--*-180-*-*-*-*-*-*
  60.     }
  61.     pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 3m -pady 3m
  62.     if {$bitmap != ""} {
  63.     label $w.bitmap -bitmap $bitmap
  64.     pack $w.bitmap -in $w.top -side left -padx 3m -pady 3m
  65.     }
  66.  
  67.     # 3. Create a row of buttons at the bottom of the dialog.
  68.  
  69.     set i 0
  70.     foreach but $args {
  71.     button $w.button$i -text $but -command "set tkPriv(button) $i"
  72.     if {$i == $default} {
  73.         frame $w.default -relief sunken -bd 1
  74.         raise $w.button$i $w.default
  75.         pack $w.default -in $w.bot -side left -expand 1 -padx 3m -pady 2m
  76.         pack $w.button$i -in $w.default -padx 2m -pady 2m
  77.     } else {
  78.         pack $w.button$i -in $w.bot -side left -expand 1 \
  79.             -padx 3m -pady 2m
  80.     }
  81.     incr i
  82.     }
  83.  
  84.     # 4. Create a binding for <Return> on the dialog if there is a
  85.     # default button.
  86.  
  87.     if {$default >= 0} {
  88.     bind $w <Return> "
  89.         $w.button$default configure -state active -relief sunken
  90.         update idletasks
  91.         after 100
  92.         set tkPriv(button) $default
  93.     "
  94.     }
  95.  
  96.     # 5. Withdraw the window, then update all the geometry information
  97.     # so we know how big it wants to be, then center the window in the
  98.     # display and de-iconify it.
  99.  
  100.     wm withdraw $w
  101.     update idletasks
  102.     set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
  103.         - [winfo vrootx [winfo parent $w]]]
  104.     set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
  105.         - [winfo vrooty [winfo parent $w]]]
  106.     wm geom $w +$x+$y
  107.     wm deiconify $w
  108.  
  109.     # 6. Set a grab and claim the focus too.
  110.  
  111.     set oldFocus [focus]
  112.     set oldGrab [grab current $w]
  113.     if {$oldGrab != ""} {
  114.     set grabStatus [grab status $oldGrab]
  115.     }
  116.     grab $w
  117.     if {$default >= 0} {
  118.     focus $w.button$default
  119.     } else {
  120.     focus $w
  121.     }
  122.  
  123.     # 7. Wait for the user to respond, then restore the focus and
  124.     # return the index of the selected button.  Restore the focus
  125.     # before deleting the window, since otherwise the window manager
  126.     # may take the focus away so we can't redirect it.  Finally,
  127.     # restore any grab that was in effect.
  128.  
  129.     tkwait variable tkPriv(button)
  130.     catch {focus $oldFocus}
  131.     destroy $w
  132.     if {$oldGrab != ""} {
  133.     if {$grabStatus == "global"} {
  134.         grab -global $oldGrab
  135.     } else {
  136.         grab $oldGrab
  137.     }
  138.     }
  139.     return $tkPriv(button)
  140. }
  141.