bgerror.tcl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # bgerror.tcl --
  2. #
  3. # Implementation of the bgerror procedure. It posts a dialog box with
  4. # the error message and gives the user a chance to see a more detailed
  5. # stack trace, and possible do something more interesting with that
  6. # trace (like save it to a log). This is adapted from work done by
  7. # Donal K. Fellows.
  8. #
  9. # Copyright (c) 1998-2000 by Ajuba Solutions.
  10. # Copyright (c) 2007 by ActiveState Software Inc.
  11. # Copyright (c) 2007 Daniel A. Steffen <das@users.sourceforge.net>
  12. namespace eval ::tk::dialog::error {
  13. namespace import -force ::tk::msgcat::*
  14. namespace export bgerror
  15. option add *ErrorDialog.function.text [mc "Save To Log"] \
  16. widgetDefault
  17. option add *ErrorDialog.function.command [namespace code SaveToLog]
  18. option add *ErrorDialog*Label.font TkCaptionFont widgetDefault
  19. if {[tk windowingsystem] eq "aqua"} {
  20. option add *ErrorDialog*background systemAlertBackgroundActive \
  21. widgetDefault
  22. option add *ErrorDialog*info.text.background white widgetDefault
  23. option add *ErrorDialog*Button.highlightBackground \
  24. systemAlertBackgroundActive widgetDefault
  25. }
  26. }
  27. proc ::tk::dialog::error::Return {} {
  28. variable button
  29. .bgerrorDialog.ok configure -state active -relief sunken
  30. update idletasks
  31. after 100
  32. set button 0
  33. }
  34. proc ::tk::dialog::error::Details {} {
  35. set w .bgerrorDialog
  36. set caption [option get $w.function text {}]
  37. set command [option get $w.function command {}]
  38. if { ($caption eq "") || ($command eq "") } {
  39. grid forget $w.function
  40. }
  41. lappend command [$w.top.info.text get 1.0 end-1c]
  42. $w.function configure -text $caption -command $command
  43. grid $w.top.info - -sticky nsew -padx 3m -pady 3m
  44. }
  45. proc ::tk::dialog::error::SaveToLog {text} {
  46. if { $::tcl_platform(platform) eq "windows" } {
  47. set allFiles *.*
  48. } else {
  49. set allFiles *
  50. }
  51. set types [list \
  52. [list [mc "Log Files"] .log] \
  53. [list [mc "Text Files"] .txt] \
  54. [list [mc "All Files"] $allFiles] \
  55. ]
  56. set filename [tk_getSaveFile -title [mc "Select Log File"] \
  57. -filetypes $types -defaultextension .log -parent .bgerrorDialog]
  58. if {![string length $filename]} {
  59. return
  60. }
  61. set f [open $filename w]
  62. puts -nonewline $f $text
  63. close $f
  64. }
  65. proc ::tk::dialog::error::Destroy {w} {
  66. if {$w eq ".bgerrorDialog"} {
  67. variable button
  68. set button -1
  69. }
  70. }
  71. # ::tk::dialog::error::bgerror --
  72. # This is the default version of bgerror.
  73. # It tries to execute tkerror, if that fails it posts a dialog box containing
  74. # the error message and gives the user a chance to ask to see a stack
  75. # trace.
  76. # Arguments:
  77. # err - The error message.
  78. proc ::tk::dialog::error::bgerror err {
  79. global errorInfo tcl_platform
  80. variable button
  81. set info $errorInfo
  82. set ret [catch {::tkerror $err} msg];
  83. if {$ret != 1} {return -code $ret $msg}
  84. # Ok the application's tkerror either failed or was not found
  85. # we use the default dialog then :
  86. set windowingsystem [tk windowingsystem]
  87. if {$windowingsystem eq "aqua"} {
  88. set ok [mc Ok]
  89. } else {
  90. set ok [mc OK]
  91. }
  92. # Truncate the message if it is too wide (>maxLine characters) or
  93. # too tall (>4 lines). Truncation occurs at the first point at
  94. # which one of those conditions is met.
  95. set displayedErr ""
  96. set lines 0
  97. set maxLine 45
  98. foreach line [split $err \n] {
  99. if { [string length $line] > $maxLine } {
  100. append displayedErr "[string range $line 0 [expr {$maxLine-3}]]..."
  101. break
  102. }
  103. if { $lines > 4 } {
  104. append displayedErr "..."
  105. break
  106. } else {
  107. append displayedErr "${line}\n"
  108. }
  109. incr lines
  110. }
  111. set title [mc "Application Error"]
  112. set text [mc "Error: %1\$s" $displayedErr]
  113. set buttons [list ok $ok dismiss [mc "Skip Messages"] \
  114. function [mc "Details >>"]]
  115. # 1. Create the top-level window and divide it into top
  116. # and bottom parts.
  117. set dlg .bgerrorDialog
  118. destroy $dlg
  119. toplevel $dlg -class ErrorDialog
  120. wm withdraw $dlg
  121. wm title $dlg $title
  122. wm iconname $dlg ErrorDialog
  123. wm protocol $dlg WM_DELETE_WINDOW { }
  124. if {$windowingsystem eq "aqua"} {
  125. ::tk::unsupported::MacWindowStyle style $dlg moveableAlert {}
  126. } elseif {$windowingsystem eq "x11"} {
  127. wm attributes $dlg -type dialog
  128. }
  129. frame $dlg.bot
  130. frame $dlg.top
  131. if {$windowingsystem eq "x11"} {
  132. $dlg.bot configure -relief raised -bd 1
  133. $dlg.top configure -relief raised -bd 1
  134. }
  135. pack $dlg.bot -side bottom -fill both
  136. pack $dlg.top -side top -fill both -expand 1
  137. set W [frame $dlg.top.info]
  138. text $W.text -setgrid true -height 10 -wrap char \
  139. -yscrollcommand [list $W.scroll set]
  140. if {$windowingsystem ne "aqua"} {
  141. $W.text configure -width 40
  142. }
  143. scrollbar $W.scroll -command [list $W.text yview]
  144. pack $W.scroll -side right -fill y
  145. pack $W.text -side left -expand yes -fill both
  146. $W.text insert 0.0 "$err\n$info"
  147. $W.text mark set insert 0.0
  148. bind $W.text <ButtonPress-1> { focus %W }
  149. $W.text configure -state disabled
  150. # 2. Fill the top part with bitmap and message
  151. # Max-width of message is the width of the screen...
  152. set wrapwidth [winfo screenwidth $dlg]
  153. # ...minus the width of the icon, padding and a fudge factor for
  154. # the window manager decorations and aesthetics.
  155. set wrapwidth [expr {$wrapwidth-60-[winfo pixels $dlg 9m]}]
  156. label $dlg.msg -justify left -text $text -wraplength $wrapwidth
  157. if {$windowingsystem eq "aqua"} {
  158. # On the Macintosh, use the stop bitmap
  159. label $dlg.bitmap -bitmap stop
  160. } else {
  161. # On other platforms, make the error icon
  162. canvas $dlg.bitmap -width 32 -height 32 -highlightthickness 0
  163. $dlg.bitmap create oval 0 0 31 31 -fill red -outline black
  164. $dlg.bitmap create line 9 9 23 23 -fill white -width 4
  165. $dlg.bitmap create line 9 23 23 9 -fill white -width 4
  166. }
  167. grid $dlg.bitmap $dlg.msg -in $dlg.top -row 0 -padx 3m -pady 3m
  168. grid configure $dlg.msg -sticky nsw -padx {0 3m}
  169. grid rowconfigure $dlg.top 1 -weight 1
  170. grid columnconfigure $dlg.top 1 -weight 1
  171. # 3. Create a row of buttons at the bottom of the dialog.
  172. set i 0
  173. foreach {name caption} $buttons {
  174. button $dlg.$name -text $caption -default normal \
  175. -command [namespace code [list set button $i]]
  176. grid $dlg.$name -in $dlg.bot -column $i -row 0 -sticky ew -padx 10
  177. grid columnconfigure $dlg.bot $i -weight 1
  178. # We boost the size of some Mac buttons for l&f
  179. if {$windowingsystem eq "aqua"} {
  180. if {($name eq "ok") || ($name eq "dismiss")} {
  181. grid columnconfigure $dlg.bot $i -minsize 90
  182. }
  183. grid configure $dlg.$name -pady 7
  184. }
  185. incr i
  186. }
  187. # The "OK" button is the default for this dialog.
  188. $dlg.ok configure -default active
  189. bind $dlg <Return> [namespace code Return]
  190. bind $dlg <Destroy> [namespace code [list Destroy %W]]
  191. $dlg.function configure -command [namespace code Details]
  192. # 6. Withdraw the window, then update all the geometry information
  193. # so we know how big it wants to be, then center the window in the
  194. # display (Motif style) and de-iconify it.
  195. ::tk::PlaceWindow $dlg
  196. # 7. Ensure that we are topmost.
  197. raise $dlg
  198. if {[tk windowingsystem] eq "win32"} {
  199. # Place it topmost if we aren't at the top of the stacking
  200. # order to ensure that it's seen
  201. if {[lindex [wm stackorder .] end] ne "$dlg"} {
  202. wm attributes $dlg -topmost 1
  203. }
  204. }
  205. # 8. Set a grab and claim the focus too.
  206. ::tk::SetFocusGrab $dlg $dlg.ok
  207. # 9. Wait for the user to respond, then restore the focus and
  208. # return the index of the selected button. Restore the focus
  209. # before deleting the window, since otherwise the window manager
  210. # may take the focus away so we can't redirect it. Finally,
  211. # restore any grab that was in effect.
  212. vwait [namespace which -variable button]
  213. set copy $button; # Save a copy...
  214. ::tk::RestoreFocusGrab $dlg $dlg.ok destroy
  215. if {$copy == 1} {
  216. return -code break
  217. }
  218. }
  219. namespace eval :: {
  220. # Fool the indexer
  221. proc bgerror err {}
  222. rename bgerror {}
  223. namespace import ::tk::dialog::error::bgerror
  224. }