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

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