source: branches/blt4/gui/scripts/textentry.tcl @ 1691

Last change on this file since 1691 was 1651, checked in by gah, 15 years ago
File size: 19.1 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: textentry - general-purpose text entry widget
3#
4#  This widget is a cross between the Tk entry and text widgets.  For
5#  one-line messages, it acts like an entry widget.  For larger
6#  messages, it morphs into a text widget.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2005  Purdue Research Foundation
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15
16option add *TextEntry.size auto widgetDefault
17option add *TextEntry.width 0 widgetDefault
18option add *TextEntry.height 0 widgetDefault
19option add *TextEntry.editable yes widgetDefault
20option add *TextEntry.textBackground white widgetDefault
21option add *TextEntry*disabledForeground #a3a3a3 widgetDefault
22option add *TextEntry*disabledBackground white widgetDefault
23
24option add *TextEntry.hintForeground gray50 widgetDefault
25option add *TextEntry.hintFont \
26    -*-helvetica-medium-r-normal-*-10-* widgetDefault
27option add *TextEntry.codeFont \
28    -*-courier-medium-r-normal-*-12-* widgetDefault
29
30
31itcl::class Rappture::TextEntry {
32    inherit itk::Widget
33
34    itk_option define -editable editable Editable ""
35    itk_option define -state state State "normal"
36    itk_option define -width width Width 0
37    itk_option define -height height Height 0
38
39    constructor {owner path args} { # defined below }
40
41    public method value {args}
42
43    public method label {}
44    public method tooltip {}
45    public method size {} { return $_size }
46
47    protected method _layout {}
48    protected method _setValue {value}
49    protected method _newValue {}
50    protected method _edit {option args}
51    protected method _fixState {}
52    protected method _uploadValue {args}
53    protected method _downloadValue {}
54
55    private variable _dispatcher "" ;# dispatcher for !events
56    private variable _owner ""      ;# thing managing this control
57    private variable _path ""       ;# path in XML to this number
58
59    private variable _layout ""     ;# entry or full text size
60    private variable _mode "ascii"  ;# ascii text or binary data
61    private variable _value ""      ;# value inside the widget
62    private variable _size ""       ;# size hint from XML
63}
64                                                                               
65itk::usual TextEntry {
66}
67
68# ----------------------------------------------------------------------
69# CONSTRUCTOR
70# ----------------------------------------------------------------------
71itcl::body Rappture::TextEntry::constructor {owner path args} {
72    if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} {
73        error "bad object \"$owner\": should be Rappture::ControlOwner"
74    }
75    set _owner $owner
76    set _path $path
77
78    Rappture::dispatcher _dispatcher
79    $_dispatcher register !layout
80    $_dispatcher dispatch $this !layout "[itcl::code $this _layout]; list"
81
82    set _size [$_owner xml get $path.size]
83
84    set hints [$_owner xml get $path.about.hints]
85    if {[string length $hints] > 0} {
86        itk_component add hints {
87            ::label $itk_interior.hints -anchor w -text $hints
88        } {
89            usual
90            rename -foreground -hintforeground hintForeground Foreground
91            rename -font -hintfont hintFont Font
92        }
93        pack $itk_component(hints) -side bottom -fill x
94    }
95
96    eval itk_initialize $args
97
98    set str [$_owner xml get $path.default]
99    if {"" != $str} {
100        _layout  ;# must fix layout or value won't take
101        value $str
102    }
103}
104
105# ----------------------------------------------------------------------
106# USAGE: value ?-check? ?<newval>?
107#
108# Clients use this to query/set the value for this widget.  With
109# no args, it returns the current value for the widget.  If the
110# <newval> is specified, it sets the value of the widget and
111# sends a <<Value>> event.  If the -check flag is included, the
112# new value is not actually applied, but just checked for correctness.
113# ----------------------------------------------------------------------
114itcl::body Rappture::TextEntry::value {args} {
115    set onlycheck 0
116    set i [lsearch -exact $args -check]
117    if {$i >= 0} {
118        set onlycheck 1
119        set args [lreplace $args $i $i]
120    }
121
122    if {[llength $args] == 1} {
123        if {$onlycheck} {
124            # someday we may add validation...
125            return
126        }
127        set newval [lindex $args 0]
128        _setValue $newval
129
130        $_dispatcher event -idle !layout
131        event generate $itk_component(hull) <<Value>>
132        return $newval
133
134    } elseif {[llength $args] != 0} {
135        error "wrong # args: should be \"value ?-check? ?newval?\""
136    }
137
138    #
139    # Query the value and return.
140    #
141    if {$_mode == "ascii"} {
142        if {$_layout == "entry"} {
143            return [$itk_component(entry) get]
144        } elseif {$_layout == "text"} {
145            return [$itk_component(text) get 1.0 end-1char]
146        }
147    } else {
148        return $_value
149    }
150    return ""
151}
152
153# ----------------------------------------------------------------------
154# USAGE: label
155#
156# Clients use this to query the label associated with this widget.
157# Reaches into the XML and pulls out the appropriate label string.
158# ----------------------------------------------------------------------
159itcl::body Rappture::TextEntry::label {} {
160    set label [$_owner xml get $_path.about.label]
161    if {"" == $label} {
162        set label "String"
163    }
164    return $label
165}
166
167# ----------------------------------------------------------------------
168# USAGE: tooltip
169#
170# Clients use this to query the tooltip associated with this widget.
171# Reaches into the XML and pulls out the appropriate description
172# string.  Returns the string that should be used with the
173# Rappture::Tooltip facility.
174# ----------------------------------------------------------------------
175itcl::body Rappture::TextEntry::tooltip {} {
176    set str [$_owner xml get $_path.about.description]
177    return [string trim $str]
178}
179
180# ----------------------------------------------------------------------
181# USAGE: _layout
182#
183# Used internally to change the layout of this widget depending
184# on the .size hint and its contents.  Switches between an entry
185# and a text widget.
186# ----------------------------------------------------------------------
187itcl::body Rappture::TextEntry::_layout {} {
188    set size $_size
189    if {$size == "" || $size == "auto"} {
190        #
191        # If the size is "auto", then look at the current value
192        # and count its lines/characters.
193        #
194        set val ""
195        if {$_layout == "entry"} {
196            set val [$itk_component(entry) get]
197        } elseif {$_layout == "text"} {
198            set val [$itk_component(text) get 1.0 end-1char]
199        }
200
201        set chars 0
202        set lines 0
203        foreach line [split $val \n] {
204            incr lines
205            if {[string length $line] > $chars} {
206                set chars [string length $line]
207            }
208        }
209        incr chars
210
211        if {$lines > 1} {
212            set size "${chars}x${lines}"
213        } else {
214            set size $chars
215        }
216    }
217
218    if {[regexp {^[0-9]+$} $size]} {
219        #
220        # If the size is WW, then flip to entry mode, with
221        # a requested size of WW characters.
222        #
223        if {$_layout != "entry"} {
224            set val ""
225            if {$_layout == "text"} {
226                set val [$itk_component(text) get 1.0 end-1char]
227                destroy $itk_component(text)
228                destroy $itk_component(scrollbars)
229            }
230
231            itk_component add entry {
232                entry $itk_interior.entry
233            } {
234                usual
235                rename -background -textbackground textBackground Background
236                rename -foreground -textforeground textForeground Foreground
237            }
238            pack $itk_component(entry) -expand yes -fill both
239            $itk_component(entry) configure \
240                -background $itk_option(-textbackground) \
241                -foreground $itk_option(-textforeground)
242
243            bind $itk_component(entry) <KeyPress> [itcl::code $this _newValue]
244            bind $itk_component(entry) <Control-KeyPress-a> \
245                "[list $itk_component(entry) selection range 0 end]; break"
246
247            itk_component add emenu {
248                menu $itk_component(entry).menu -tearoff 0
249            }
250            $itk_component(emenu) add command -label "Cut" -accelerator "^X" \
251                -command [list event generate $itk_component(entry) <<Cut>>]
252            $itk_component(emenu) add command -label "Copy" -accelerator "^C" \
253                -command [list event generate $itk_component(entry) <<Copy>>]
254            $itk_component(emenu) add command -label "Paste" -accelerator "^V" \
255                -command [list event generate $itk_component(entry) <<Paste>>]
256            $itk_component(emenu) add command -label "Select All" -accelerator "^A" -command [list $itk_component(entry) selection range 0 end]
257            bind $itk_component(entry) <<PopupMenu>> \
258                [itcl::code $this _edit menu emenu %X %Y]
259
260            set _layout "entry"
261            _setValue $val
262        }
263        $itk_component(entry) configure -width $size
264
265    } elseif {[regexp {^([0-9]+)x([0-9]+)$} $size match w h]} {
266        #
267        # If the size is WWxHH, then flip to text mode, with
268        # a requested size of HH lines by WW characters.
269        #
270        if {$_layout != "text"} {
271            set val ""
272            if {$_layout == "entry"} {
273                set val [$itk_component(entry) get]
274                destroy $itk_component(entry)
275            }
276
277            itk_component add scrollbars {
278                blt::scrollset $itk_interior.scrl \
279                    -xscrollbar $itk_interior.scrl.xs \
280                    -yscrollbar $itk_interior.scrl.ys \
281                    -window $itk_interior.scrl.text
282            }
283            blt::tk::scrollbar $itk_interior.scrl.xs
284            blt::tk::scrollbar $itk_interior.scrl.ys
285            pack $itk_component(scrollbars) -expand yes -fill both
286
287            itk_component add text {
288                text $itk_component(scrollbars).text \
289                    -width 1 -height 1 -wrap char
290            } {
291                usual
292                rename -background -textbackground textBackground Background
293                rename -foreground -textforeground textForeground Foreground
294                rename -font -codefont codeFont CodeFont
295            }
296            $itk_component(text) configure \
297                -background $itk_option(-textbackground) \
298                -foreground $itk_option(-textforeground) \
299                -font $itk_option(-codefont)
300
301            bind $itk_component(text) <KeyPress> [itcl::code $this _newValue]
302            bind $itk_component(text) <Control-KeyPress-a> \
303                "[list $itk_component(text) tag add sel 1.0 end]; break"
304
305            itk_component add tmenu {
306                menu $itk_component(text).menu -tearoff 0
307            }
308            $itk_component(tmenu) add command -label "Cut" -accelerator "^X" \
309                -command [list event generate $itk_component(text) <<Cut>>]
310            $itk_component(tmenu) add command -label "Copy" -accelerator "^C" \
311                -command [list event generate $itk_component(text) <<Copy>>]
312            $itk_component(tmenu) add command -label "Paste" -accelerator "^V" \
313                -command [list event generate $itk_component(text) <<Paste>>]
314            $itk_component(tmenu) add command -label "Select All" -accelerator "^A" -command [list $itk_component(text) tag add sel 1.0 end]
315            $itk_component(tmenu) add separator
316
317            $itk_component(tmenu) add command \
318                -label [Rappture::filexfer::label upload] \
319                -command [itcl::code $this _uploadValue -start]
320            $itk_component(tmenu) add command \
321                -label [Rappture::filexfer::label download] \
322                -command [itcl::code $this _downloadValue]
323
324            bind $itk_component(text) <<PopupMenu>> \
325                [itcl::code $this _edit menu tmenu %X %Y]
326
327            set _layout "text"
328            _setValue $val
329        }
330        $itk_component(text) configure -width $w -height $h
331    }
332
333    #
334    # Fix the overall widget size according to -width / -height
335    #
336    if {$itk_option(-width) == 0 && $itk_option(-height) == 0} {
337        pack propagate $itk_component(hull) yes
338    } else {
339        pack propagate $itk_component(hull) no
340        component hull configure \
341            -width $itk_option(-width) -height $itk_option(-width)
342    }
343}
344
345# ----------------------------------------------------------------------
346# USAGE: _setValue <newValue>
347#
348# Used internally to set the value for this widget.  If the <newValue>
349# string is ASCII, then it is stored directly and the widget is enabled
350# for editing.  Otherwise, the value is cached and a representation of
351# the data is displayed.
352# ----------------------------------------------------------------------
353itcl::body Rappture::TextEntry::_setValue {newval} {
354    if {[Rappture::encoding::is binary $newval]} {
355        # looks like a binary file
356        set _mode "binary"
357        set _value $newval
358
359        if {$_layout == "entry" || [string match {*x[01]} $_size]} {
360            set newval [Rappture::utils::hexdump -lines 0 $_value]
361        } else {
362            set newval [Rappture::utils::hexdump -lines 1000 $_value]
363        }
364    } else {
365        # ascii file -- map carriage returns to line feeds
366        set _mode "ascii"
367        set _value ""
368        regsub -all "\r\n" $newval "\n" newval
369        regsub -all "\r" $newval "\n" newval
370    }
371
372    if {$_layout == "entry"} {
373        $itk_component(entry) configure -state normal
374        $itk_component(emenu) entryconfigure "Cut" -state normal
375        $itk_component(emenu) entryconfigure "Paste" -state normal
376        $itk_component(entry) delete 0 end
377        $itk_component(entry) insert 0 $newval
378        if {!$itk_option(-editable) || $_mode == "binary"} {
379            $itk_component(entry) configure -state disabled
380            $itk_component(emenu) entryconfigure "Cut" -state disabled
381            $itk_component(emenu) entryconfigure "Paste" -state disabled
382        }
383    } elseif {$_layout == "text"} {
384        $itk_component(text) configure -state normal
385        $itk_component(tmenu) entryconfigure "Cut" -state normal
386        $itk_component(tmenu) entryconfigure "Paste" -state normal
387        $itk_component(text) delete 1.0 end
388        $itk_component(text) insert end $newval
389        if {!$itk_option(-editable) || $_mode == "binary"} {
390            set hull $itk_component(hull)
391            set dfg [option get $hull disabledForeground Foreground]
392            set dbg [option get $hull disabledBackground Background]
393            $itk_component(text) configure -state disabled \
394                -background $dbg -foreground $dfg
395            $itk_component(tmenu) entryconfigure "Cut" -state disabled
396            $itk_component(tmenu) entryconfigure "Paste" -state disabled
397        } else {
398            $itk_component(text) configure \
399                -background $itk_option(-textbackground) \
400                -foreground $itk_option(-textforeground)
401        }
402    }
403}
404
405# ----------------------------------------------------------------------
406# USAGE: _newValue
407#
408# Invoked automatically whenever the value in the entry changes.
409# Sends a <<Value>> event to notify clients of the change.
410# ----------------------------------------------------------------------
411itcl::body Rappture::TextEntry::_newValue {} {
412    event generate $itk_component(hull) <<Value>>
413}
414
415# ----------------------------------------------------------------------
416# USAGE: _edit menu <which> <X> <Y>
417#
418# Used internally to manage edit operations.
419# ----------------------------------------------------------------------
420itcl::body Rappture::TextEntry::_edit {option args} {
421    if {$itk_option(-state) == "disabled"} {
422        return  ;# disabled? then bail out here!
423    }
424    switch -- $option {
425        menu {
426            if {[llength $args] != 3} {
427                error "wrong # args: should be \"_edit $option which x y\""
428            }
429            set mname [lindex $args 0]
430            set x [lindex $args 1]
431            set y [lindex $args 2]
432            tk_popup $itk_component($mname) $x $y
433        }
434        default {
435            error "bad option \"$option\": should be menu"
436        }
437    }
438}
439
440# ----------------------------------------------------------------------
441# USAGE: _fixState
442#
443# Used internally to update the internal widgets whenever the
444# -state/-editable options change.  Enables or disables various
445# widgets.
446# ----------------------------------------------------------------------
447itcl::body Rappture::TextEntry::_fixState {} {
448    if {$itk_option(-editable) && $itk_option(-state) == "normal"} {
449        set state normal
450    } else {
451        set state disabled
452    }
453    if {$_layout == "entry"} {
454        $itk_component(entry) configure -state $state
455        $itk_component(emenu) entryconfigure "Cut" -state $state
456        $itk_component(emenu) entryconfigure "Copy" -state $state
457        $itk_component(emenu) entryconfigure "Paste" -state $state
458    } elseif {$_layout == "text"} {
459        $itk_component(text) configure -state $state
460        $itk_component(tmenu) entryconfigure "Cut" -state $state
461        $itk_component(tmenu) entryconfigure "Copy" -state $state
462        $itk_component(tmenu) entryconfigure "Paste" -state $state
463    }
464}
465
466# ----------------------------------------------------------------------
467# USAGE: _uploadValue -start
468# USAGE: _uploadValue -assign <key> <value> <key> <value> ...
469#
470# Used internally to initiate an upload operation.  Prompts the
471# user to upload into the text area of this widget.
472# ----------------------------------------------------------------------
473itcl::body Rappture::TextEntry::_uploadValue {args} {
474    switch -- $_layout {
475        entry   { set widget $itk_component(entry) }
476        text    { set widget $itk_component(text) }
477        default { set widget $itk_component(hull) }
478    }
479
480    set opt [lindex $args 0]
481    switch -- $opt {
482        -start {
483            set tool [Rappture::Tool::resources -appname]
484            set cntls [list $_path [label] [tooltip]]
485            Rappture::filexfer::upload \
486                $tool $cntls [itcl::code $this _uploadValue -assign]
487        }
488        -assign {
489            array set data [lrange $args 1 end] ;# skip option
490            if {[info exists data(error)]} {
491                Rappture::Tooltip::cue $widget $data(error)
492            }
493            if {[info exists data(data)]} {
494                Rappture::Tooltip::cue hide  ;# take down note about the popup
495                _setValue $data(data)
496                _newValue
497            }
498        }
499        default {
500            error "bad option \"$opt\": should be -start or -assign"
501        }
502    }
503}
504
505# ----------------------------------------------------------------------
506# USAGE: _downloadValue
507#
508# Used internally to initiate a download operation.  Takes the current
509# value and downloads it to the user in a new browser window.
510# ----------------------------------------------------------------------
511itcl::body Rappture::TextEntry::_downloadValue {} {
512    set mesg [Rappture::filexfer::download [value] input.txt]
513
514    if {"" != $mesg} {
515        switch -- $_layout {
516            entry   { set widget $itk_component(entry) }
517            text    { set widget $itk_component(text) }
518            default { set widget $itk_component(hull) }
519        }
520        Rappture::Tooltip::cue $widget $mesg
521    }
522}
523
524# ----------------------------------------------------------------------
525# CONFIGURATION OPTION: -editable
526# ----------------------------------------------------------------------
527itcl::configbody Rappture::TextEntry::editable {
528    if {![string is boolean -strict $itk_option(-editable)]} {
529        error "bad value \"$itk_option(-editable)\": should be boolean"
530    }
531    _fixState
532}
533
534# ----------------------------------------------------------------------
535# CONFIGURATION OPTION: -state
536# ----------------------------------------------------------------------
537itcl::configbody Rappture::TextEntry::state {
538    set valid {normal disabled}
539    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
540        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
541    }
542    if {$_layout == "text"} {
543        if {$itk_option(-state) == "disabled"} {
544            set fg [option get $itk_component(text) disabledForeground Foreground]
545        } else {
546            set fg $itk_option(-foreground)
547        }
548        $itk_component(text) configure -foreground $fg
549    }
550    _fixState
551}
552
553# ----------------------------------------------------------------------
554# CONFIGURATION OPTION: -width
555# ----------------------------------------------------------------------
556itcl::configbody Rappture::TextEntry::width {
557    # check size to see if it has the proper form
558    winfo pixels $itk_component(hull) $itk_option(-width)
559    $_dispatcher event -idle !layout
560}
561
562# ----------------------------------------------------------------------
563# CONFIGURATION OPTION: -height
564# ----------------------------------------------------------------------
565itcl::configbody Rappture::TextEntry::height {
566    # check size to see if it has the proper form
567    winfo pixels $itk_component(hull) $itk_option(-height)
568    $_dispatcher event -idle !layout
569}
Note: See TracBrowser for help on using the repository browser.