source: trunk/gui/scripts/textresult.tcl @ 46

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