home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / lib / tk8.5 / dialog.tcl < prev    next >
Encoding:
Text File  |  2006-06-17  |  6.3 KB  |  209 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. # RCS: @(#) $Id: dialog.tcl,v 1.20 2006/01/25 18:22:04 dgp Exp $
  7. #
  8. # Copyright (c) 1992-1993 The Regents of the University of California.
  9. # Copyright (c) 1994-1997 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.  If the
  20. # dialog somehow gets destroyed, -1 is returned.
  21. #
  22. # Arguments:
  23. # w -        Window to use for dialog top-level.
  24. # title -    Title to display in dialog's decorative frame.
  25. # text -    Message to display in dialog.
  26. # bitmap -    Bitmap to display in dialog (empty string means none).
  27. # default -    Index of button that is to display the default ring
  28. #        (-1 means none).
  29. # args -    One or more strings to display in buttons across the
  30. #        bottom of the dialog box.
  31.  
  32. proc ::tk_dialog {w title text bitmap default args} {
  33.     global tcl_platform
  34.     variable ::tk::Priv
  35.  
  36.     # Check that $default was properly given
  37.     if {[string is integer -strict $default]} {
  38.     if {$default >= [llength $args]} {
  39.         return -code error "default button index greater than number of\
  40.             buttons specified for tk_dialog"
  41.     }
  42.     } elseif {"" eq $default} {
  43.     set default -1
  44.     } else {
  45.     set default [lsearch -exact $args $default]
  46.     }
  47.  
  48.     # 1. Create the top-level window and divide it into top
  49.     # and bottom parts.
  50.  
  51.     destroy $w
  52.     toplevel $w -class Dialog
  53.     wm title $w $title
  54.     wm iconname $w Dialog
  55.     wm protocol $w WM_DELETE_WINDOW { }
  56.  
  57.     # Dialog boxes should be transient with respect to their parent,
  58.     # so that they will always stay on top of their parent window.  However,
  59.     # some window managers will create the window as withdrawn if the parent
  60.     # window is withdrawn or iconified.  Combined with the grab we put on the
  61.     # window, this can hang the entire application.  Therefore we only make
  62.     # the dialog transient if the parent is viewable.
  63.     #
  64.     if {[winfo viewable [winfo toplevel [winfo parent $w]]] } {
  65.     wm transient $w [winfo toplevel [winfo parent $w]]
  66.     }
  67.  
  68.     set windowingsystem [tk windowingsystem]
  69.     if {$windowingsystem eq "aqua"} {
  70.     ::tk::unsupported::MacWindowStyle style $w dBoxProc
  71.     }
  72.  
  73.     frame $w.bot
  74.     frame $w.top
  75.     if {$windowingsystem eq "x11"} {
  76.     $w.bot configure -relief raised -bd 1
  77.     $w.top configure -relief raised -bd 1
  78.     }
  79.     pack $w.bot -side bottom -fill both
  80.     pack $w.top -side top -fill both -expand 1
  81.     grid anchor $w.bot center
  82.  
  83.     # 2. Fill the top part with bitmap and message (use the option
  84.     # database for -wraplength and -font so that they can be
  85.     # overridden by the caller).
  86.  
  87.     option add *Dialog.msg.wrapLength 3i widgetDefault
  88.     if {$windowingsystem eq "aqua"} {
  89.     option add *Dialog.msg.font system widgetDefault
  90.     } else {
  91.     option add *Dialog.msg.font {Times 12} widgetDefault
  92.     }
  93.  
  94.     label $w.msg -justify left -text $text
  95.     pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 3m -pady 3m
  96.     if {$bitmap ne ""} {
  97.     if {$windowingsystem eq "aqua" && $bitmap eq "error"} {
  98.         set bitmap "stop"
  99.     }
  100.     label $w.bitmap -bitmap $bitmap
  101.     pack $w.bitmap -in $w.top -side left -padx 3m -pady 3m
  102.     }
  103.  
  104.     # 3. Create a row of buttons at the bottom of the dialog.
  105.  
  106.     set i 0
  107.     foreach but $args {
  108.     button $w.button$i -text $but -command [list set ::tk::Priv(button) $i]
  109.     if {$i == $default} {
  110.         $w.button$i configure -default active
  111.     } else {
  112.         $w.button$i configure -default normal
  113.     }
  114.     grid $w.button$i -in $w.bot -column $i -row 0 -sticky ew \
  115.         -padx 10 -pady 4
  116.     grid columnconfigure $w.bot $i
  117.     # We boost the size of some Mac buttons for l&f
  118.     if {$windowingsystem eq "aqua"} {
  119.         set tmp [string tolower $but]
  120.         if {$tmp eq "ok" || $tmp eq "cancel"} {
  121.         grid columnconfigure $w.bot $i -minsize [expr {59 + 20}]
  122.         }
  123.     }
  124.     incr i
  125.     }
  126.  
  127.     # 4. Create a binding for <Return> on the dialog if there is a
  128.     # default button.
  129.  
  130.     if {$default >= 0} {
  131.     bind $w <Return> "
  132.     [list $w.button$default] configure -state active -relief sunken
  133.     update idletasks
  134.     after 100
  135.     set ::tk::Priv(button) $default
  136.     "
  137.     }
  138.  
  139.     # 5. Create a <Destroy> binding for the window that sets the
  140.     # button variable to -1;  this is needed in case something happens
  141.     # that destroys the window, such as its parent window being destroyed.
  142.  
  143.     bind $w <Destroy> {set ::tk::Priv(button) -1}
  144.  
  145.     # 6. Withdraw the window, then update all the geometry information
  146.     # so we know how big it wants to be, then center the window in the
  147.     # display and de-iconify it.
  148.  
  149.     wm withdraw $w
  150.     update idletasks
  151.     set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
  152.         - [winfo vrootx [winfo parent $w]]}]
  153.     set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
  154.         - [winfo vrooty [winfo parent $w]]}]
  155.     # Make sure that the window is on the screen and set the maximum
  156.     # size of the window is the size of the screen.  That'll let things
  157.     # fail fairly gracefully when very large messages are used. [Bug 827535]
  158.     if {$x < 0} {
  159.     set x 0
  160.     }
  161.     if {$y < 0} {
  162.     set y 0
  163.     }
  164.     wm maxsize $w [winfo screenwidth $w] [winfo screenheight $w]
  165.     wm geometry $w +$x+$y
  166.     wm deiconify $w
  167.  
  168.     tkwait visibility $w
  169.  
  170.     # 7. Set a grab and claim the focus too.
  171.  
  172.     set oldFocus [focus]
  173.     set oldGrab [grab current $w]
  174.     if {$oldGrab ne ""} {
  175.     set grabStatus [grab status $oldGrab]
  176.     }
  177.     grab $w
  178.     if {$default >= 0} {
  179.     focus $w.button$default
  180.     } else {
  181.     focus $w
  182.     }
  183.  
  184.     # 8. Wait for the user to respond, then restore the focus and
  185.     # return the index of the selected button.  Restore the focus
  186.     # before deleting the window, since otherwise the window manager
  187.     # may take the focus away so we can't redirect it.  Finally,
  188.     # restore any grab that was in effect.
  189.  
  190.     vwait ::tk::Priv(button)
  191.     catch {focus $oldFocus}
  192.     catch {
  193.     # It's possible that the window has already been destroyed,
  194.     # hence this "catch".  Delete the Destroy handler so that
  195.     # Priv(button) doesn't get reset by it.
  196.  
  197.     bind $w <Destroy> {}
  198.     destroy $w
  199.     }
  200.     if {$oldGrab ne ""} {
  201.     if {$grabStatus ne "global"} {
  202.         grab $oldGrab
  203.     } else {
  204.         grab -global $oldGrab
  205.     }
  206.     }
  207.     return $Priv(button)
  208. }
  209.