dialog.tcl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. # Copyright (c) 1992-1993 The Regents of the University of California.
  7. # Copyright (c) 1994-1997 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. #
  13. # ::tk_dialog:
  14. #
  15. # This procedure displays a dialog box, waits for a button in the dialog
  16. # to be invoked, then returns the index of the selected button. If the
  17. # dialog somehow gets destroyed, -1 is returned.
  18. #
  19. # Arguments:
  20. # w - Window to use for dialog top-level.
  21. # title - Title to display in dialog's decorative frame.
  22. # text - Message to display in dialog.
  23. # bitmap - Bitmap to display in dialog (empty string means none).
  24. # default - Index of button that is to display the default ring
  25. # (-1 means none).
  26. # args - One or more strings to display in buttons across the
  27. # bottom of the dialog box.
  28. proc ::tk_dialog {w title text bitmap default args} {
  29. global tcl_platform
  30. variable ::tk::Priv
  31. # Check that $default was properly given
  32. if {[string is integer -strict $default]} {
  33. if {$default >= [llength $args]} {
  34. return -code error "default button index greater than number of\
  35. buttons specified for tk_dialog"
  36. }
  37. } elseif {"" eq $default} {
  38. set default -1
  39. } else {
  40. set default [lsearch -exact $args $default]
  41. }
  42. set windowingsystem [tk windowingsystem]
  43. if {$windowingsystem eq "aqua"} {
  44. option add *Dialog*background systemDialogBackgroundActive widgetDefault
  45. option add *Dialog*Button.highlightBackground \
  46. systemDialogBackgroundActive widgetDefault
  47. }
  48. # 1. Create the top-level window and divide it into top
  49. # and bottom parts.
  50. destroy $w
  51. toplevel $w -class Dialog
  52. wm title $w $title
  53. wm iconname $w Dialog
  54. wm protocol $w WM_DELETE_WINDOW { }
  55. # Dialog boxes should be transient with respect to their parent,
  56. # so that they will always stay on top of their parent window. However,
  57. # some window managers will create the window as withdrawn if the parent
  58. # window is withdrawn or iconified. Combined with the grab we put on the
  59. # window, this can hang the entire application. Therefore we only make
  60. # the dialog transient if the parent is viewable.
  61. #
  62. if {[winfo viewable [winfo toplevel [winfo parent $w]]] } {
  63. wm transient $w [winfo toplevel [winfo parent $w]]
  64. }
  65. if {$windowingsystem eq "aqua"} {
  66. ::tk::unsupported::MacWindowStyle style $w moveableModal {}
  67. } elseif {$windowingsystem eq "x11"} {
  68. wm attributes $w -type dialog
  69. }
  70. frame $w.bot
  71. frame $w.top
  72. if {$windowingsystem eq "x11"} {
  73. $w.bot configure -relief raised -bd 1
  74. $w.top configure -relief raised -bd 1
  75. }
  76. pack $w.bot -side bottom -fill both
  77. pack $w.top -side top -fill both -expand 1
  78. grid anchor $w.bot center
  79. # 2. Fill the top part with bitmap and message (use the option
  80. # database for -wraplength and -font so that they can be
  81. # overridden by the caller).
  82. option add *Dialog.msg.wrapLength 3i widgetDefault
  83. option add *Dialog.msg.font TkCaptionFont widgetDefault
  84. label $w.msg -justify left -text $text
  85. pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 3m -pady 3m
  86. if {$bitmap ne ""} {
  87. if {$windowingsystem eq "aqua" && $bitmap eq "error"} {
  88. set bitmap "stop"
  89. }
  90. label $w.bitmap -bitmap $bitmap
  91. pack $w.bitmap -in $w.top -side left -padx 3m -pady 3m
  92. }
  93. # 3. Create a row of buttons at the bottom of the dialog.
  94. set i 0
  95. foreach but $args {
  96. button $w.button$i -text $but -command [list set ::tk::Priv(button) $i]
  97. if {$i == $default} {
  98. $w.button$i configure -default active
  99. } else {
  100. $w.button$i configure -default normal
  101. }
  102. grid $w.button$i -in $w.bot -column $i -row 0 -sticky ew \
  103. -padx 10 -pady 4
  104. grid columnconfigure $w.bot $i
  105. # We boost the size of some Mac buttons for l&f
  106. if {$windowingsystem eq "aqua"} {
  107. set tmp [string tolower $but]
  108. if {$tmp eq "ok" || $tmp eq "cancel"} {
  109. grid columnconfigure $w.bot $i -minsize 90
  110. }
  111. grid configure $w.button$i -pady 7
  112. }
  113. incr i
  114. }
  115. # 4. Create a binding for <Return> on the dialog if there is a
  116. # default button.
  117. # Convention also dictates that if the keyboard focus moves among the
  118. # the buttons that the <Return> binding affects the button with the focus.
  119. if {$default >= 0} {
  120. bind $w <Return> [list $w.button$default invoke]
  121. }
  122. bind $w <<PrevWindow>> [list bind $w <Return> {[tk_focusPrev %W] invoke}]
  123. bind $w <Tab> [list bind $w <Return> {[tk_focusNext %W] invoke}]
  124. # 5. Create a <Destroy> binding for the window that sets the
  125. # button variable to -1; this is needed in case something happens
  126. # that destroys the window, such as its parent window being destroyed.
  127. bind $w <Destroy> {set ::tk::Priv(button) -1}
  128. # 6. Withdraw the window, then update all the geometry information
  129. # so we know how big it wants to be, then center the window in the
  130. # display (Motif style) and de-iconify it.
  131. ::tk::PlaceWindow $w
  132. tkwait visibility $w
  133. # 7. Set a grab and claim the focus too.
  134. if {$default >= 0} {
  135. set focus $w.button$default
  136. } else {
  137. set focus $w
  138. }
  139. tk::SetFocusGrab $w $focus
  140. # 8. Wait for the user to respond, then restore the focus and
  141. # return the index of the selected button. Restore the focus
  142. # before deleting the window, since otherwise the window manager
  143. # may take the focus away so we can't redirect it. Finally,
  144. # restore any grab that was in effect.
  145. vwait ::tk::Priv(button)
  146. catch {
  147. # It's possible that the window has already been destroyed,
  148. # hence this "catch". Delete the Destroy handler so that
  149. # Priv(button) doesn't get reset by it.
  150. bind $w <Destroy> {}
  151. }
  152. tk::RestoreFocusGrab $w $focus
  153. return $Priv(button)
  154. }