source: branches/blt4/gui/scripts/note.tcl @ 1897

Last change on this file since 1897 was 1651, checked in by gah, 14 years ago
File size: 6.8 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: Note - widget for displaying HTML notes
3#
4#  This widget represents an entry on a control panel that displays
5#  information to the user.  It is not an input; it helps to describe
6#  the interface.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2007  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
15package require http
16
17itcl::class Rappture::Note {
18    inherit itk::Widget
19
20    # need this only to be like other controls
21    itk_option define -state state State ""
22
23    constructor {owner path args} { # defined below }
24
25    public method value {args}
26
27    public method label {}
28    public method tooltip {}
29
30    protected method _setContents {info}
31    protected method _escapeChars {text}
32
33    private variable _owner ""    ;# thing managing this control
34    private variable _path ""     ;# path in XML to this note
35}
36
37itk::usual Note {
38    keep -cursor
39    keep -foreground -background
40}
41itk::usual Scrollset {
42}
43
44# ----------------------------------------------------------------------
45# CONSTRUCTOR
46# ----------------------------------------------------------------------
47itcl::body Rappture::Note::constructor {owner path args} {
48    if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} {
49        error "bad object \"$owner\": should be Rappture::ControlOwner"
50    }
51    set _owner $owner
52    set _path $path
53
54    #
55    # Create the widget and configure it properly based on other
56    # hints in the XML.
57    #
58    itk_component add scroller {
59        blt::scrollset $itk_interior.scroller \
60            -xscrollbar $itk_interior.scroller.xs \
61            -yscrollbar $itk_interior.scroller.ys \
62            -window $itk_interior.scroller.html
63    }
64    blt::tk::scrollbar $itk_interior.scroller.xs
65    blt::tk::scrollbar $itk_interior.scroller.ys
66    pack $itk_component(scroller) -expand yes -fill both
67    itk_component add html {
68        Rappture::HTMLviewer $itk_component(scroller).html
69    }
70    eval itk_initialize $args
71
72    _setContents [$_owner xml get $_path.contents]
73}
74
75# ----------------------------------------------------------------------
76# USAGE: value ?-check? ?<newval>?
77#
78# Clients use this to query/set the value for this widget.  With
79# no args, it returns the current value for the widget.  If the
80# <newval> is specified, it sets the value of the widget and
81# sends a <<Value>> event.  If the -check flag is included, the
82# new value is not actually applied, but just checked for correctness.
83# ----------------------------------------------------------------------
84itcl::body Rappture::Note::value {args} {
85    set onlycheck 0
86    set i [lsearch -exact $args -check]
87    if {$i >= 0} {
88        set onlycheck 1
89        set args [lreplace $args $i $i]
90    }
91
92    if {[llength $args] == 1} {
93        if {$onlycheck} {
94            # someday we may add validation...
95            return
96        }
97        set newval [lindex $args 0]
98        _setContents $newval
99        return $newval
100
101    } elseif {[llength $args] != 0} {
102        error "wrong # args: should be \"value ?-check? ?newval?\""
103    }
104
105    #
106    # Query the value and return.
107    #
108    error "don't know how to check value of <note>"
109    return
110}
111
112# ----------------------------------------------------------------------
113# USAGE: label
114#
115# Clients use this to query the label associated with this widget.
116# Reaches into the XML and pulls out the appropriate label string.
117# ----------------------------------------------------------------------
118itcl::body Rappture::Note::label {} {
119    error "can't get label"
120}
121
122# ----------------------------------------------------------------------
123# USAGE: tooltip
124#
125# Clients use this to query the tooltip associated with this widget.
126# Reaches into the XML and pulls out the appropriate description
127# string.  Returns the string that should be used with the
128# Rappture::Tooltip facility.
129# ----------------------------------------------------------------------
130itcl::body Rappture::Note::tooltip {} {
131    error "can't get tooltip"
132}
133
134# ----------------------------------------------------------------------
135# USAGE: _setContents <info>
136#
137# Used internally to load HTML info into the display area for this
138# widget.
139# ----------------------------------------------------------------------
140itcl::body Rappture::Note::_setContents {info} {
141    switch -regexp -- $info {
142        ^https?:// {
143            if {[catch {http::geturl $info} token] == 0} {
144                set html [http::data $token]
145                http::cleanup $token
146            } else {
147                set html "<html><body><h1>Oops! Internal Error</h1><p>[_escapeChars $token]</p></body></html>"
148            }
149            $itk_component(html) load $html
150        }
151        ^file:// {
152            set file [string range $info 7 end]
153            if {[file pathtype $file] != "absolute"} {
154                # find the file on a search path
155                set dir [[$_owner tool] installdir]
156                set searchlist [list $dir [file join $dir docs]]
157                foreach dir $searchlist {
158                    if {[file readable [file join $dir $file]]} {
159                        set file [file join $dir $file]
160                        break
161                    }
162                }
163            }
164
165            # load the contents of the file
166            set cmds {
167                set fid [open $file r]
168                set html [read $fid]
169                close $fid
170            }
171            if {[catch $cmds result]} {
172                set html "<html><body><h1>Oops! File Not Found</h1><p>[_escapeChars $result]</p></body></html>"
173            }
174
175            # not HTML? then escape nasty characters and display it.
176            if {![regexp {<html>.*</html>} $html]} {
177                set html "<html><body><p>[_escapeChars $html]</p></body></html>"
178            }
179            $itk_component(html) load $html -in $file
180        }
181        default {
182            set html "<html><body><p>[_escapeChars $info]</p></body></html>"
183            $itk_component(html) load $html
184        }
185    }
186}
187
188# ----------------------------------------------------------------------
189# USAGE: _escapeChars <info>
190#
191# Used internally to escape HTML characters in ordinary text.  Used
192# when trying to display ordinary text, which may have things like
193# "<b>" or "x < 2" embedded within it.
194# ----------------------------------------------------------------------
195itcl::body Rappture::Note::_escapeChars {info} {
196    regsub -all & $info \001 info
197    regsub -all \" $info {\&quot;} info
198    regsub -all < $info {\&lt;} info
199    regsub -all > $info {\&gt;} info
200    regsub -all \001 $info {\&amp;} info
201    return $info
202}
Note: See TracBrowser for help on using the repository browser.