[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 |
---|
[115] | 8 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
| 9 | # |
---|
| 10 | # See the file "license.terms" for information on usage and |
---|
| 11 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
[11] | 12 | # ====================================================================== |
---|
| 13 | package require Itk |
---|
| 14 | package require BLT |
---|
| 15 | |
---|
| 16 | option add *TextResult.width 4i widgetDefault |
---|
| 17 | option add *TextResult.height 4i widgetDefault |
---|
[46] | 18 | option add *TextResult.textBackground white widgetDefault |
---|
[11] | 19 | option add *TextResult.font \ |
---|
[676] | 20 | -*-helvetica-medium-r-normal-*-12-* widgetDefault |
---|
[46] | 21 | option add *TextResult.textFont \ |
---|
[676] | 22 | -*-courier-medium-r-normal-*-12-* widgetDefault |
---|
[11] | 23 | |
---|
[1710] | 24 | itk::usual Scrollset { |
---|
| 25 | } |
---|
| 26 | |
---|
[11] | 27 | itcl::class Rappture::TextResult { |
---|
| 28 | inherit itk::Widget |
---|
| 29 | |
---|
[1862] | 30 | constructor {args} { |
---|
[1923] | 31 | # defined below |
---|
[1862] | 32 | } |
---|
[11] | 33 | public method add {dataobj {settings ""}} |
---|
[13] | 34 | public method get {} |
---|
[11] | 35 | public method delete {args} |
---|
| 36 | public method scale {args} |
---|
[1862] | 37 | public method parameters {title args} { |
---|
[1923] | 38 | # do nothing |
---|
[1862] | 39 | } |
---|
[464] | 40 | public method download {option args} |
---|
[13] | 41 | |
---|
[46] | 42 | public method select {option args} |
---|
| 43 | public method find {option} |
---|
[1862] | 44 | public method popup {option args} |
---|
[46] | 45 | |
---|
[413] | 46 | private variable _dataobj "" ;# data object currently being displayed |
---|
| 47 | private variable _raised ;# maps all data objects => -raise param |
---|
[11] | 48 | } |
---|
[1923] | 49 | |
---|
[11] | 50 | itk::usual TextResult { |
---|
| 51 | keep -background -foreground -cursor -font |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | # ---------------------------------------------------------------------- |
---|
| 55 | # CONSTRUCTOR |
---|
| 56 | # ---------------------------------------------------------------------- |
---|
| 57 | itcl::body Rappture::TextResult::constructor {args} { |
---|
| 58 | option add hull.width hull.height |
---|
| 59 | pack propagate $itk_component(hull) no |
---|
| 60 | |
---|
[46] | 61 | # |
---|
| 62 | # CONTROL BAR with find/select functions |
---|
| 63 | # |
---|
| 64 | itk_component add controls { |
---|
[1923] | 65 | frame $itk_interior.cntls |
---|
[46] | 66 | } |
---|
| 67 | pack $itk_component(controls) -side bottom -fill x -pady {4 0} |
---|
| 68 | |
---|
| 69 | itk_component add selectall { |
---|
[1923] | 70 | button $itk_component(controls).selall -text "Select All" \ |
---|
| 71 | -command [itcl::code $this select all] |
---|
[46] | 72 | } |
---|
| 73 | pack $itk_component(selectall) -side right -fill y |
---|
| 74 | |
---|
| 75 | itk_component add findl { |
---|
[1923] | 76 | label $itk_component(controls).findl -text "Find:" |
---|
[46] | 77 | } |
---|
| 78 | pack $itk_component(findl) -side left |
---|
| 79 | |
---|
| 80 | itk_component add find { |
---|
[1923] | 81 | entry $itk_component(controls).find -width 20 |
---|
[46] | 82 | } |
---|
| 83 | pack $itk_component(find) -side left |
---|
| 84 | |
---|
| 85 | itk_component add finddown { |
---|
[1923] | 86 | button $itk_component(controls).finddown \ |
---|
| 87 | -image [Rappture::icon finddn] \ |
---|
| 88 | -relief flat -overrelief raised \ |
---|
| 89 | -command [itcl::code $this find down] |
---|
[46] | 90 | } { |
---|
[1923] | 91 | usual |
---|
| 92 | ignore -relief |
---|
[46] | 93 | } |
---|
| 94 | pack $itk_component(finddown) -side left |
---|
| 95 | |
---|
| 96 | itk_component add findup { |
---|
[1923] | 97 | button $itk_component(controls).findup \ |
---|
| 98 | -image [Rappture::icon findup] \ |
---|
| 99 | -relief flat -overrelief raised \ |
---|
| 100 | -command [itcl::code $this find up] |
---|
[46] | 101 | } { |
---|
[1923] | 102 | usual |
---|
| 103 | ignore -relief |
---|
[46] | 104 | } |
---|
| 105 | pack $itk_component(findup) -side left |
---|
| 106 | |
---|
| 107 | itk_component add findstatus { |
---|
[1923] | 108 | label $itk_component(controls).finds -width 10 -anchor w |
---|
[46] | 109 | } |
---|
| 110 | pack $itk_component(findstatus) -side left -expand yes -fill x |
---|
| 111 | |
---|
| 112 | # shortcut for Return in search field |
---|
| 113 | bind $itk_component(find) <KeyPress-Return> " |
---|
[1923] | 114 | $itk_component(finddown) configure -relief sunken |
---|
| 115 | update idletasks |
---|
| 116 | after 200 |
---|
| 117 | $itk_component(finddown) configure -relief flat |
---|
| 118 | $itk_component(finddown) invoke |
---|
[46] | 119 | " |
---|
| 120 | bind $itk_component(find) <KeyPress> \ |
---|
[1923] | 121 | [itcl::code $this find reset] |
---|
[46] | 122 | |
---|
| 123 | # |
---|
| 124 | # TEXT AREA |
---|
| 125 | # |
---|
[11] | 126 | itk_component add scroller { |
---|
[1923] | 127 | blt::scrollset $itk_interior.scroller \ |
---|
| 128 | -xscrollbar $itk_interior.scroller.xs \ |
---|
| 129 | -yscrollbar $itk_interior.scroller.ys \ |
---|
| 130 | -window $itk_interior.scroller.text |
---|
[11] | 131 | } |
---|
[1651] | 132 | blt::tk::scrollbar $itk_interior.scroller.xs |
---|
| 133 | blt::tk::scrollbar $itk_interior.scroller.ys |
---|
[11] | 134 | pack $itk_component(scroller) -expand yes -fill both |
---|
| 135 | |
---|
| 136 | itk_component add text { |
---|
[1923] | 137 | text $itk_component(scroller).text -width 1 -height 1 -wrap none |
---|
[46] | 138 | } { |
---|
[1923] | 139 | usual |
---|
| 140 | rename -background -textbackground textBackground Background |
---|
| 141 | rename -font -textfont textFont Font |
---|
[11] | 142 | } |
---|
| 143 | $itk_component(text) configure -state disabled |
---|
| 144 | |
---|
[23] | 145 | $itk_component(text) tag configure ERROR -foreground red |
---|
| 146 | |
---|
[1862] | 147 | itk_component add emenu { |
---|
[1923] | 148 | menu $itk_component(text).menu -tearoff 0 |
---|
[1862] | 149 | } { |
---|
[1923] | 150 | ignore -tearoff |
---|
[1862] | 151 | } |
---|
| 152 | $itk_component(emenu) add command \ |
---|
[1923] | 153 | -label "Select All" -accelerator "Ctrl+A" \ |
---|
| 154 | -command [itcl::code $this select all] |
---|
[1862] | 155 | $itk_component(emenu) add command \ |
---|
[1923] | 156 | -label "Select None" -accelerator "Esc" \ |
---|
| 157 | -command [itcl::code $this select none] |
---|
[1862] | 158 | bind $itk_component(text) <<PopupMenu>> \ |
---|
[1923] | 159 | [itcl::code $this popup menu emenu %X %Y] |
---|
[1863] | 160 | $itk_component(emenu) add command \ |
---|
[1923] | 161 | -label "Copy" -accelerator "Ctrl+C" \ |
---|
| 162 | -command [list event generate $itk_component(text) <<Copy>>] |
---|
[1862] | 163 | |
---|
[1863] | 164 | bind $itk_component(text) <Control-KeyPress-a> \ |
---|
[1923] | 165 | [list $itk_component(emenu) invoke "Select All"] |
---|
[1863] | 166 | bind $itk_component(text) <Control-KeyPress-c> \ |
---|
[1923] | 167 | [list $itk_component(emenu) invoke "Copy"] |
---|
[1863] | 168 | bind $itk_component(text) <Escape> \ |
---|
[1923] | 169 | [list $itk_component(emenu) invoke "Select None" ] |
---|
[1863] | 170 | bind $itk_component(text) <Enter> [list ::focus $itk_component(text)] |
---|
| 171 | |
---|
| 172 | eval itk_initialize $args |
---|
[11] | 173 | } |
---|
| 174 | |
---|
| 175 | # ---------------------------------------------------------------------- |
---|
| 176 | # USAGE: add <dataobj> ?<settings>? |
---|
| 177 | # |
---|
| 178 | # Clients use this to add a data object to the plot. If the optional |
---|
| 179 | # <settings> are specified, then the are applied to the data. Allowed |
---|
[13] | 180 | # settings are -color and -brightness, -width, -linestyle and -raise. |
---|
| 181 | # (Many of these are ignored.) |
---|
[11] | 182 | # ---------------------------------------------------------------------- |
---|
| 183 | itcl::body Rappture::TextResult::add {dataobj {settings ""}} { |
---|
| 184 | array set params { |
---|
[1923] | 185 | -color "" |
---|
| 186 | -brightness "" |
---|
| 187 | -width "" |
---|
| 188 | -linestyle "" |
---|
| 189 | -raise 0 |
---|
| 190 | -description "" |
---|
| 191 | -param "" |
---|
[11] | 192 | } |
---|
| 193 | foreach {opt val} $settings { |
---|
[1923] | 194 | if {![info exists params($opt)]} { |
---|
| 195 | error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]" |
---|
| 196 | } |
---|
| 197 | set params($opt) $val |
---|
[11] | 198 | } |
---|
| 199 | |
---|
[413] | 200 | set replace 0 |
---|
| 201 | if {"" != $dataobj} { |
---|
[1923] | 202 | set _raised($dataobj) $params(-raise) |
---|
| 203 | if {"" == $_dataobj} { |
---|
| 204 | set replace 1 |
---|
| 205 | } elseif {$_raised($_dataobj) == 0 && $params(-raise)} { |
---|
| 206 | set replace 1 |
---|
| 207 | } |
---|
[413] | 208 | } |
---|
[11] | 209 | |
---|
[413] | 210 | if {$replace} { |
---|
[1923] | 211 | $itk_component(text) configure -state normal |
---|
| 212 | $itk_component(text) delete 1.0 end |
---|
[413] | 213 | |
---|
[1923] | 214 | if {[$dataobj element -as type] == "log"} { |
---|
| 215 | # log output -- remove special =RAPPTURE-???=> messages |
---|
| 216 | set message [$dataobj get] |
---|
| 217 | while {[regexp -indices \ |
---|
| 218 | {=RAPPTURE-([a-zA-Z]+)=>([^\n]*)(\n|$)} $message \ |
---|
| 219 | match type mesg]} { |
---|
[23] | 220 | |
---|
[1923] | 221 | foreach {i0 i1} $match break |
---|
| 222 | set first [string range $message 0 [expr {$i0-1}]] |
---|
| 223 | if {[string length $first] > 0} { |
---|
| 224 | $itk_component(text) insert end $first |
---|
| 225 | } |
---|
[23] | 226 | |
---|
[1923] | 227 | foreach {t0 t1} $type break |
---|
| 228 | set type [string range $message $t0 $t1] |
---|
| 229 | foreach {m0 m1} $mesg break |
---|
| 230 | set mesg [string range $message $m0 $m1] |
---|
| 231 | if {[string length $mesg] > 0 |
---|
| 232 | && $type != "RUN" && $type != "PROGRESS"} { |
---|
| 233 | $itk_component(text) insert end $mesg $type |
---|
| 234 | $itk_component(text) insert end \n $type |
---|
| 235 | } |
---|
| 236 | set message [string range $message [expr {$i1+1}] end] |
---|
| 237 | } |
---|
[23] | 238 | |
---|
[1923] | 239 | if {[string length $message] > 0} { |
---|
| 240 | $itk_component(text) insert end $message |
---|
| 241 | if {[$itk_component(text) get end-2char] != "\n"} { |
---|
| 242 | $itk_component(text) insert end "\n" |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | } elseif {[$dataobj element -as type] == "string"} { |
---|
| 246 | # add string values |
---|
| 247 | set data [$dataobj get current] |
---|
| 248 | if {[Rappture::encoding::is binary $data]} { |
---|
| 249 | set data [Rappture::utils::hexdump -lines 1000 $data] |
---|
| 250 | } |
---|
| 251 | $itk_component(text) insert end $data |
---|
| 252 | } else { |
---|
| 253 | # any other string output -- add it directly |
---|
| 254 | $itk_component(text) insert end [$dataobj get] |
---|
| 255 | } |
---|
| 256 | $itk_component(text) configure -state disabled |
---|
[413] | 257 | |
---|
[1923] | 258 | set _dataobj $dataobj |
---|
[11] | 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | # ---------------------------------------------------------------------- |
---|
[13] | 263 | # USAGE: get |
---|
| 264 | # |
---|
| 265 | # Clients use this to query the list of objects being plotted, in |
---|
| 266 | # order from bottom to top of this result. |
---|
| 267 | # ---------------------------------------------------------------------- |
---|
| 268 | itcl::body Rappture::TextResult::get {} { |
---|
| 269 | return $_dataobj |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | # ---------------------------------------------------------------------- |
---|
[11] | 273 | # USAGE: delete ?<curve1> <curve2> ...? |
---|
| 274 | # |
---|
| 275 | # Clients use this to delete a curve from the plot. If no curves |
---|
| 276 | # are specified, then all curves are deleted. |
---|
| 277 | # ---------------------------------------------------------------------- |
---|
| 278 | itcl::body Rappture::TextResult::delete {args} { |
---|
[413] | 279 | if {[llength $args] == 0} { |
---|
[1923] | 280 | # delete everything |
---|
| 281 | catch {unset _raised} |
---|
| 282 | set _dataobj "" |
---|
| 283 | $itk_component(text) configure -state normal |
---|
| 284 | $itk_component(text) delete 1.0 end |
---|
| 285 | $itk_component(text) configure -state disabled |
---|
[413] | 286 | } else { |
---|
[1923] | 287 | # delete these specific objects |
---|
| 288 | foreach obj $args { |
---|
| 289 | catch {unset _raised($obj)} |
---|
| 290 | if {$obj == $_dataobj} { |
---|
| 291 | set _dataobj "" |
---|
| 292 | $itk_component(text) configure -state normal |
---|
| 293 | $itk_component(text) delete 1.0 end |
---|
| 294 | $itk_component(text) configure -state disabled |
---|
| 295 | } |
---|
| 296 | } |
---|
[413] | 297 | } |
---|
[11] | 298 | } |
---|
| 299 | |
---|
| 300 | # ---------------------------------------------------------------------- |
---|
| 301 | # USAGE: scale ?<curve1> <curve2> ...? |
---|
| 302 | # |
---|
| 303 | # Sets the default limits for the overall plot according to the |
---|
| 304 | # limits of the data for all of the given <curve> objects. This |
---|
| 305 | # accounts for all curves--even those not showing on the screen. |
---|
| 306 | # Because of this, the limits are appropriate for all curves as |
---|
| 307 | # the user scans through data in the ResultSet viewer. |
---|
| 308 | # ---------------------------------------------------------------------- |
---|
| 309 | itcl::body Rappture::TextResult::scale {args} { |
---|
| 310 | # nothing to do for text |
---|
| 311 | } |
---|
[46] | 312 | |
---|
| 313 | # ---------------------------------------------------------------------- |
---|
[193] | 314 | # USAGE: download coming |
---|
[464] | 315 | # USAGE: download controls <downloadCommand> |
---|
[193] | 316 | # USAGE: download now |
---|
[50] | 317 | # |
---|
| 318 | # Clients use this method to create a downloadable representation |
---|
| 319 | # of the plot. Returns a list of the form {ext string}, where |
---|
| 320 | # "ext" is the file extension (indicating the type of data) and |
---|
| 321 | # "string" is the data itself. |
---|
| 322 | # ---------------------------------------------------------------------- |
---|
[464] | 323 | itcl::body Rappture::TextResult::download {option args} { |
---|
[193] | 324 | switch $option { |
---|
[1923] | 325 | coming { |
---|
| 326 | # nothing to do |
---|
| 327 | } |
---|
| 328 | controls { |
---|
| 329 | # no controls for this download yet |
---|
| 330 | return "" |
---|
| 331 | } |
---|
| 332 | now { |
---|
| 333 | if {"" == $_dataobj} { |
---|
| 334 | return "" |
---|
| 335 | } |
---|
| 336 | if {[$_dataobj element -as type] == "log"} { |
---|
| 337 | set val [$itk_component(text) get 1.0 end] |
---|
| 338 | } elseif {[$_dataobj element -as type] == "string"} { |
---|
| 339 | set val [$_dataobj get current] |
---|
| 340 | } else { |
---|
| 341 | set val [$_dataobj get] |
---|
| 342 | } |
---|
[702] | 343 | |
---|
[1923] | 344 | set ext [$_dataobj get filetype] |
---|
| 345 | if {"" == $ext} { |
---|
| 346 | if {[Rappture::encoding::is binary $val]} { |
---|
| 347 | set ext ".dat" |
---|
| 348 | } else { |
---|
| 349 | set ext ".txt" |
---|
| 350 | } |
---|
| 351 | } |
---|
| 352 | return [list $ext $val] |
---|
| 353 | } |
---|
| 354 | default { |
---|
| 355 | error "bad option \"$option\": should be coming, controls, now" |
---|
| 356 | } |
---|
[193] | 357 | } |
---|
[50] | 358 | } |
---|
| 359 | |
---|
| 360 | # ---------------------------------------------------------------------- |
---|
[46] | 361 | # USAGE: select all |
---|
| 362 | # |
---|
| 363 | # Handles various selection operations within the text. |
---|
| 364 | # ---------------------------------------------------------------------- |
---|
| 365 | itcl::body Rappture::TextResult::select {option args} { |
---|
| 366 | switch -- $option { |
---|
[1923] | 367 | all { |
---|
| 368 | $itk_component(text) tag add sel 1.0 end |
---|
| 369 | } |
---|
| 370 | none { |
---|
| 371 | if { [$itk_component(text) tag ranges "sel"] != "" } { |
---|
| 372 | selection clear |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | default { |
---|
| 376 | error "bad option \"$option\": should be all or none" |
---|
| 377 | } |
---|
[46] | 378 | } |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | # ---------------------------------------------------------------------- |
---|
| 382 | # USAGE: find up |
---|
| 383 | # USAGE: find down |
---|
| 384 | # USAGE: find reset |
---|
| 385 | # |
---|
| 386 | # Handles various find operations within the text. These find a |
---|
| 387 | # bit of text and highlight it within the widget. The find operation |
---|
| 388 | # starts from the end of the currently highlighted text, or from the |
---|
| 389 | # beginning if there is no highlight. |
---|
| 390 | # ---------------------------------------------------------------------- |
---|
| 391 | itcl::body Rappture::TextResult::find {option} { |
---|
| 392 | # handle the reset case... |
---|
| 393 | $itk_component(find) configure \ |
---|
[1923] | 394 | -background $itk_option(-background) \ |
---|
| 395 | -foreground $itk_option(-foreground) |
---|
[46] | 396 | $itk_component(findstatus) configure -text "" |
---|
| 397 | |
---|
| 398 | if {$option == "reset"} { |
---|
[1923] | 399 | return |
---|
[46] | 400 | } |
---|
| 401 | |
---|
| 402 | # handle the up/down cases... |
---|
| 403 | set t $itk_component(text) |
---|
| 404 | set pattern [string trim [$itk_component(find) get]] |
---|
| 405 | |
---|
| 406 | if {"" == $pattern} { |
---|
[1923] | 407 | $itk_component(find) configure -background red -foreground white |
---|
| 408 | $itk_component(findstatus) configure -text "<< Enter a search string" |
---|
| 409 | return |
---|
[46] | 410 | } |
---|
| 411 | |
---|
| 412 | # find the starting point for the search |
---|
| 413 | set seln [$t tag nextrange sel 1.0] |
---|
| 414 | if {$seln == ""} { |
---|
[1923] | 415 | set t0 1.0 |
---|
| 416 | set t1 end |
---|
[46] | 417 | } else { |
---|
[1923] | 418 | foreach {t0 t1} $seln break |
---|
[46] | 419 | } |
---|
| 420 | $t tag remove sel 1.0 end |
---|
| 421 | |
---|
| 422 | # search up or down |
---|
| 423 | switch -- $option { |
---|
[1923] | 424 | up { |
---|
| 425 | set start [$t index $t0-1char] |
---|
| 426 | set next [$t search -backwards -nocase -- $pattern $start] |
---|
| 427 | } |
---|
| 428 | down { |
---|
| 429 | set start [$t index $t1+1char] |
---|
| 430 | set next [$t search -forwards -nocase -- $pattern $start] |
---|
| 431 | } |
---|
[46] | 432 | } |
---|
| 433 | |
---|
| 434 | if {"" != $next} { |
---|
[1923] | 435 | set len [string length $pattern] |
---|
| 436 | $t tag add sel $next $next+${len}chars |
---|
| 437 | $t see $next |
---|
| 438 | set lnum [lindex [split $next .] 0] |
---|
| 439 | set lines [lindex [split [$t index end] .] 0] |
---|
| 440 | set percent [expr {round(100.0*$lnum/$lines)}] |
---|
| 441 | set status "line $lnum --$percent%--" |
---|
[46] | 442 | } else { |
---|
[1923] | 443 | set status "Not found" |
---|
| 444 | $itk_component(find) configure -background red -foreground white |
---|
[46] | 445 | } |
---|
| 446 | $itk_component(findstatus) configure -text $status |
---|
| 447 | } |
---|
[1862] | 448 | |
---|
| 449 | # ---------------------------------------------------------------------- |
---|
| 450 | # USAGE: _popup menu <which> <X> <Y> |
---|
| 451 | # |
---|
| 452 | # Used internally to manage edit operations. |
---|
| 453 | # ---------------------------------------------------------------------- |
---|
| 454 | itcl::body Rappture::TextResult::popup {option args} { |
---|
| 455 | switch -- $option { |
---|
[1923] | 456 | menu { |
---|
| 457 | if {[llength $args] != 3} { |
---|
| 458 | error "wrong # args: should be \"_popup $option which x y\"" |
---|
| 459 | } |
---|
| 460 | set mname [lindex $args 0] |
---|
| 461 | set x [lindex $args 1] |
---|
| 462 | set y [lindex $args 2] |
---|
| 463 | tk_popup $itk_component($mname) $x $y |
---|
| 464 | } |
---|
| 465 | default { |
---|
| 466 | error "bad option \"$option\": should be menu" |
---|
| 467 | } |
---|
[1862] | 468 | } |
---|
| 469 | } |
---|