[11] | 1 | # ---------------------------------------------------------------------- |
---|
| 2 | # COMPONENT: TextResult - Log output for ResultSet |
---|
| 3 | # |
---|
| 4 | # This widget is used to show text output in a ResultSet. The log |
---|
| 5 | # output from a tool, for example, is rendered as a TextResult. |
---|
| 6 | # ====================================================================== |
---|
| 7 | # AUTHOR: Michael McLennan, Purdue University |
---|
| 8 | # Copyright (c) 2004-2005 |
---|
| 9 | # Purdue Research Foundation, West Lafayette, IN |
---|
| 10 | # ====================================================================== |
---|
| 11 | package require Itk |
---|
| 12 | package require BLT |
---|
| 13 | |
---|
| 14 | option add *TextResult.width 4i widgetDefault |
---|
| 15 | option add *TextResult.height 4i widgetDefault |
---|
[46] | 16 | option add *TextResult.textBackground white widgetDefault |
---|
[11] | 17 | option add *TextResult.font \ |
---|
[46] | 18 | -*-helvetica-medium-r-normal-*-*-120-* widgetDefault |
---|
| 19 | option add *TextResult.textFont \ |
---|
[11] | 20 | -*-courier-medium-r-normal-*-*-120-* widgetDefault |
---|
| 21 | |
---|
| 22 | itcl::class Rappture::TextResult { |
---|
| 23 | inherit itk::Widget |
---|
| 24 | |
---|
| 25 | constructor {args} { # defined below } |
---|
| 26 | |
---|
| 27 | public method add {dataobj {settings ""}} |
---|
[13] | 28 | public method get {} |
---|
[11] | 29 | public method delete {args} |
---|
| 30 | public method scale {args} |
---|
[50] | 31 | public method download {} |
---|
[13] | 32 | |
---|
[46] | 33 | public method select {option args} |
---|
| 34 | public method find {option} |
---|
| 35 | |
---|
[13] | 36 | set _dataobj "" ;# data object currently being displayed |
---|
[46] | 37 | |
---|
| 38 | private common icons |
---|
| 39 | set dir [file dirname [info script]] |
---|
| 40 | set icons(up) [image create photo -file [file join $dir images findup.gif]] |
---|
| 41 | set icons(down) [image create photo -file [file join $dir images finddn.gif]] |
---|
[11] | 42 | } |
---|
| 43 | |
---|
| 44 | itk::usual TextResult { |
---|
| 45 | keep -background -foreground -cursor -font |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | # ---------------------------------------------------------------------- |
---|
| 49 | # CONSTRUCTOR |
---|
| 50 | # ---------------------------------------------------------------------- |
---|
| 51 | itcl::body Rappture::TextResult::constructor {args} { |
---|
| 52 | option add hull.width hull.height |
---|
| 53 | pack propagate $itk_component(hull) no |
---|
| 54 | |
---|
[46] | 55 | # |
---|
| 56 | # CONTROL BAR with find/select functions |
---|
| 57 | # |
---|
| 58 | itk_component add controls { |
---|
| 59 | frame $itk_interior.cntls |
---|
| 60 | } |
---|
| 61 | pack $itk_component(controls) -side bottom -fill x -pady {4 0} |
---|
| 62 | |
---|
| 63 | itk_component add selectall { |
---|
| 64 | button $itk_component(controls).selall -text "Select All" \ |
---|
| 65 | -command [itcl::code $this select all] |
---|
| 66 | } |
---|
| 67 | pack $itk_component(selectall) -side right -fill y |
---|
| 68 | |
---|
| 69 | itk_component add findl { |
---|
| 70 | label $itk_component(controls).findl -text "Find:" |
---|
| 71 | } |
---|
| 72 | pack $itk_component(findl) -side left |
---|
| 73 | |
---|
| 74 | itk_component add find { |
---|
| 75 | entry $itk_component(controls).find -width 20 |
---|
| 76 | } |
---|
| 77 | pack $itk_component(find) -side left |
---|
| 78 | |
---|
| 79 | itk_component add finddown { |
---|
| 80 | button $itk_component(controls).finddown -image $icons(down) \ |
---|
| 81 | -relief flat -overrelief raised \ |
---|
| 82 | -command [itcl::code $this find down] |
---|
| 83 | } { |
---|
| 84 | usual |
---|
| 85 | ignore -relief |
---|
| 86 | } |
---|
| 87 | pack $itk_component(finddown) -side left |
---|
| 88 | |
---|
| 89 | itk_component add findup { |
---|
| 90 | button $itk_component(controls).findup -image $icons(up) \ |
---|
| 91 | -relief flat -overrelief raised \ |
---|
| 92 | -command [itcl::code $this find up] |
---|
| 93 | } { |
---|
| 94 | usual |
---|
| 95 | ignore -relief |
---|
| 96 | } |
---|
| 97 | pack $itk_component(findup) -side left |
---|
| 98 | |
---|
| 99 | itk_component add findstatus { |
---|
| 100 | label $itk_component(controls).finds -width 10 -anchor w |
---|
| 101 | } |
---|
| 102 | pack $itk_component(findstatus) -side left -expand yes -fill x |
---|
| 103 | |
---|
| 104 | # shortcut for Return in search field |
---|
| 105 | bind $itk_component(find) <KeyPress-Return> " |
---|
| 106 | $itk_component(finddown) configure -relief sunken |
---|
| 107 | update idletasks |
---|
| 108 | after 200 |
---|
| 109 | $itk_component(finddown) configure -relief flat |
---|
| 110 | $itk_component(finddown) invoke |
---|
| 111 | " |
---|
| 112 | bind $itk_component(find) <KeyPress> \ |
---|
| 113 | [itcl::code $this find reset] |
---|
| 114 | |
---|
| 115 | # |
---|
| 116 | # TEXT AREA |
---|
| 117 | # |
---|
[11] | 118 | itk_component add scroller { |
---|
| 119 | Rappture::Scroller $itk_interior.scroller \ |
---|
| 120 | -xscrollmode auto -yscrollmode auto |
---|
| 121 | } |
---|
| 122 | pack $itk_component(scroller) -expand yes -fill both |
---|
| 123 | |
---|
| 124 | itk_component add text { |
---|
| 125 | text $itk_component(scroller).text -width 1 -height 1 -wrap none |
---|
[46] | 126 | } { |
---|
| 127 | usual |
---|
| 128 | rename -background -textbackground textBackground Background |
---|
| 129 | rename -font -textfont textFont Font |
---|
[11] | 130 | } |
---|
| 131 | $itk_component(scroller) contents $itk_component(text) |
---|
| 132 | $itk_component(text) configure -state disabled |
---|
| 133 | |
---|
[23] | 134 | $itk_component(text) tag configure ERROR -foreground red |
---|
| 135 | |
---|
[11] | 136 | eval itk_initialize $args |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | # ---------------------------------------------------------------------- |
---|
| 140 | # USAGE: add <dataobj> ?<settings>? |
---|
| 141 | # |
---|
| 142 | # Clients use this to add a data object to the plot. If the optional |
---|
| 143 | # <settings> are specified, then the are applied to the data. Allowed |
---|
[13] | 144 | # settings are -color and -brightness, -width, -linestyle and -raise. |
---|
| 145 | # (Many of these are ignored.) |
---|
[11] | 146 | # ---------------------------------------------------------------------- |
---|
| 147 | itcl::body Rappture::TextResult::add {dataobj {settings ""}} { |
---|
| 148 | array set params { |
---|
| 149 | -color "" |
---|
[13] | 150 | -brightness "" |
---|
[11] | 151 | -width "" |
---|
[13] | 152 | -linestyle "" |
---|
[11] | 153 | -raise "" |
---|
| 154 | } |
---|
| 155 | foreach {opt val} $settings { |
---|
| 156 | if {![info exists params($opt)]} { |
---|
| 157 | error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]" |
---|
| 158 | } |
---|
| 159 | set params($opt) $val |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | $itk_component(text) configure -state normal |
---|
| 163 | $itk_component(text) delete 1.0 end |
---|
| 164 | |
---|
| 165 | if {"" != $dataobj} { |
---|
[23] | 166 | if {[$dataobj element -as type] == "log"} { |
---|
| 167 | # log output -- remove special =RAPPTURE-???=> messages |
---|
| 168 | set message [$dataobj get] |
---|
| 169 | while {[regexp -indices \ |
---|
| 170 | {=RAPPTURE-([a-zA-Z]+)=>([^\n]*)(\n|$)} $message \ |
---|
| 171 | match type mesg]} { |
---|
| 172 | |
---|
| 173 | foreach {i0 i1} $match break |
---|
| 174 | set first [string range $message 0 [expr {$i0-1}]] |
---|
| 175 | if {[string length $first] > 0} { |
---|
| 176 | $itk_component(text) insert end $first |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | foreach {t0 t1} $type break |
---|
| 180 | set type [string range $message $t0 $t1] |
---|
| 181 | foreach {m0 m1} $mesg break |
---|
| 182 | set mesg [string range $message $m0 $m1] |
---|
| 183 | if {[string length $mesg] > 0 |
---|
| 184 | && $type != "RUN" && $type != "PROGRESS"} { |
---|
| 185 | $itk_component(text) insert end $mesg $type |
---|
| 186 | $itk_component(text) insert end \n $type |
---|
| 187 | } |
---|
| 188 | set message [string range $message [expr {$i1+1}] end] |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | if {[string length $message] > 0} { |
---|
| 192 | $itk_component(text) insert end $message |
---|
| 193 | if {[$itk_component(text) get end-2char] != "\n"} { |
---|
| 194 | $itk_component(text) insert end "\n" |
---|
| 195 | } |
---|
| 196 | } |
---|
[64] | 197 | } elseif {[$dataobj element -as type] == "string"} { |
---|
| 198 | # add string values |
---|
| 199 | $itk_component(text) insert end [$dataobj get current] |
---|
[11] | 200 | } else { |
---|
[23] | 201 | # any other string output -- add it directly |
---|
| 202 | $itk_component(text) insert end [$dataobj get] |
---|
[11] | 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | $itk_component(text) configure -state disabled |
---|
[13] | 207 | set _dataobj $dataobj |
---|
[11] | 208 | } |
---|
| 209 | |
---|
| 210 | # ---------------------------------------------------------------------- |
---|
[13] | 211 | # USAGE: get |
---|
| 212 | # |
---|
| 213 | # Clients use this to query the list of objects being plotted, in |
---|
| 214 | # order from bottom to top of this result. |
---|
| 215 | # ---------------------------------------------------------------------- |
---|
| 216 | itcl::body Rappture::TextResult::get {} { |
---|
| 217 | return $_dataobj |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | # ---------------------------------------------------------------------- |
---|
[11] | 221 | # USAGE: delete ?<curve1> <curve2> ...? |
---|
| 222 | # |
---|
| 223 | # Clients use this to delete a curve from the plot. If no curves |
---|
| 224 | # are specified, then all curves are deleted. |
---|
| 225 | # ---------------------------------------------------------------------- |
---|
| 226 | itcl::body Rappture::TextResult::delete {args} { |
---|
| 227 | $itk_component(text) configure -state normal |
---|
| 228 | $itk_component(text) delete 1.0 end |
---|
| 229 | $itk_component(text) configure -state disabled |
---|
[13] | 230 | set _dataobj "" |
---|
[11] | 231 | } |
---|
| 232 | |
---|
| 233 | # ---------------------------------------------------------------------- |
---|
| 234 | # USAGE: scale ?<curve1> <curve2> ...? |
---|
| 235 | # |
---|
| 236 | # Sets the default limits for the overall plot according to the |
---|
| 237 | # limits of the data for all of the given <curve> objects. This |
---|
| 238 | # accounts for all curves--even those not showing on the screen. |
---|
| 239 | # Because of this, the limits are appropriate for all curves as |
---|
| 240 | # the user scans through data in the ResultSet viewer. |
---|
| 241 | # ---------------------------------------------------------------------- |
---|
| 242 | itcl::body Rappture::TextResult::scale {args} { |
---|
| 243 | # nothing to do for text |
---|
| 244 | } |
---|
[46] | 245 | |
---|
| 246 | # ---------------------------------------------------------------------- |
---|
[50] | 247 | # USAGE: download |
---|
| 248 | # |
---|
| 249 | # Clients use this method to create a downloadable representation |
---|
| 250 | # of the plot. Returns a list of the form {ext string}, where |
---|
| 251 | # "ext" is the file extension (indicating the type of data) and |
---|
| 252 | # "string" is the data itself. |
---|
| 253 | # ---------------------------------------------------------------------- |
---|
| 254 | itcl::body Rappture::TextResult::download {} { |
---|
| 255 | return [list .txt [$itk_component(text) get 1.0 end]] |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | # ---------------------------------------------------------------------- |
---|
[46] | 259 | # USAGE: select all |
---|
| 260 | # |
---|
| 261 | # Handles various selection operations within the text. |
---|
| 262 | # ---------------------------------------------------------------------- |
---|
| 263 | itcl::body Rappture::TextResult::select {option args} { |
---|
| 264 | switch -- $option { |
---|
| 265 | all { |
---|
| 266 | $itk_component(text) tag add sel 1.0 end |
---|
| 267 | } |
---|
| 268 | default { |
---|
| 269 | error "bad option \"$option\": should be all" |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | # ---------------------------------------------------------------------- |
---|
| 275 | # USAGE: find up |
---|
| 276 | # USAGE: find down |
---|
| 277 | # USAGE: find reset |
---|
| 278 | # |
---|
| 279 | # Handles various find operations within the text. These find a |
---|
| 280 | # bit of text and highlight it within the widget. The find operation |
---|
| 281 | # starts from the end of the currently highlighted text, or from the |
---|
| 282 | # beginning if there is no highlight. |
---|
| 283 | # ---------------------------------------------------------------------- |
---|
| 284 | itcl::body Rappture::TextResult::find {option} { |
---|
| 285 | # handle the reset case... |
---|
| 286 | $itk_component(find) configure \ |
---|
| 287 | -background $itk_option(-background) \ |
---|
| 288 | -foreground $itk_option(-foreground) |
---|
| 289 | $itk_component(findstatus) configure -text "" |
---|
| 290 | |
---|
| 291 | if {$option == "reset"} { |
---|
| 292 | return |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | # handle the up/down cases... |
---|
| 296 | set t $itk_component(text) |
---|
| 297 | set pattern [string trim [$itk_component(find) get]] |
---|
| 298 | |
---|
| 299 | if {"" == $pattern} { |
---|
| 300 | $itk_component(find) configure -background red -foreground white |
---|
| 301 | $itk_component(findstatus) configure -text "<< Enter a search string" |
---|
| 302 | return |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | # find the starting point for the search |
---|
| 306 | set seln [$t tag nextrange sel 1.0] |
---|
| 307 | if {$seln == ""} { |
---|
| 308 | set t0 1.0 |
---|
| 309 | set t1 end |
---|
| 310 | } else { |
---|
| 311 | foreach {t0 t1} $seln break |
---|
| 312 | } |
---|
| 313 | $t tag remove sel 1.0 end |
---|
| 314 | |
---|
| 315 | # search up or down |
---|
| 316 | switch -- $option { |
---|
| 317 | up { |
---|
| 318 | set start [$t index $t0-1char] |
---|
| 319 | set next [$t search -backwards -nocase -- $pattern $start] |
---|
| 320 | } |
---|
| 321 | down { |
---|
| 322 | set start [$t index $t1+1char] |
---|
| 323 | set next [$t search -forwards -nocase -- $pattern $start] |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | if {"" != $next} { |
---|
| 328 | set len [string length $pattern] |
---|
| 329 | $t tag add sel $next $next+${len}chars |
---|
| 330 | $t see $next |
---|
| 331 | set lnum [lindex [split $next .] 0] |
---|
| 332 | set lines [lindex [split [$t index end] .] 0] |
---|
| 333 | set percent [expr {round(100.0*$lnum/$lines)}] |
---|
| 334 | set status "line $lnum --$percent%--" |
---|
| 335 | } else { |
---|
| 336 | set status "Not found" |
---|
| 337 | $itk_component(find) configure -background red -foreground white |
---|
| 338 | } |
---|
| 339 | $itk_component(findstatus) configure -text $status |
---|
| 340 | } |
---|