source: trunk/gui/scripts/note.tcl @ 1555

Last change on this file since 1555 was 1342, checked in by gah, 15 years ago

preliminary HQ output from molvisviewer; unexpand tabs; all jpeg generation at 100%

File size: 6.2 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}
41
42# ----------------------------------------------------------------------
43# CONSTRUCTOR
44# ----------------------------------------------------------------------
45itcl::body Rappture::Note::constructor {owner path args} {
46    if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} {
47        error "bad object \"$owner\": should be Rappture::ControlOwner"
48    }
49    set _owner $owner
50    set _path $path
51
52    #
53    # Create the widget and configure it properly based on other
54    # hints in the XML.
55    #
56    itk_component add scroller {
57        Rappture::Scroller $itk_interior.scroller \
58            -xscrollmode auto -yscrollmode auto
59    }
60    pack $itk_component(scroller) -expand yes -fill both
61
62    itk_component add html {
63        Rappture::HTMLviewer $itk_component(scroller).html
64    }
65    $itk_component(scroller) contents $itk_component(html)
66
67    eval itk_initialize $args
68
69    _setContents [$_owner xml get $_path.contents]
70}
71
72# ----------------------------------------------------------------------
73# USAGE: value ?-check? ?<newval>?
74#
75# Clients use this to query/set the value for this widget.  With
76# no args, it returns the current value for the widget.  If the
77# <newval> is specified, it sets the value of the widget and
78# sends a <<Value>> event.  If the -check flag is included, the
79# new value is not actually applied, but just checked for correctness.
80# ----------------------------------------------------------------------
81itcl::body Rappture::Note::value {args} {
82    set onlycheck 0
83    set i [lsearch -exact $args -check]
84    if {$i >= 0} {
85        set onlycheck 1
86        set args [lreplace $args $i $i]
87    }
88
89    if {[llength $args] == 1} {
90        if {$onlycheck} {
91            # someday we may add validation...
92            return
93        }
94        set newval [lindex $args 0]
95        _setContents $newval
96        return $newval
97
98    } elseif {[llength $args] != 0} {
99        error "wrong # args: should be \"value ?-check? ?newval?\""
100    }
101
102    #
103    # Query the value and return.
104    #
105    error "don't know how to check value of <note>"
106    return
107}
108
109# ----------------------------------------------------------------------
110# USAGE: label
111#
112# Clients use this to query the label associated with this widget.
113# Reaches into the XML and pulls out the appropriate label string.
114# ----------------------------------------------------------------------
115itcl::body Rappture::Note::label {} {
116    error "can't get label"
117}
118
119# ----------------------------------------------------------------------
120# USAGE: tooltip
121#
122# Clients use this to query the tooltip associated with this widget.
123# Reaches into the XML and pulls out the appropriate description
124# string.  Returns the string that should be used with the
125# Rappture::Tooltip facility.
126# ----------------------------------------------------------------------
127itcl::body Rappture::Note::tooltip {} {
128    error "can't get tooltip"
129}
130
131# ----------------------------------------------------------------------
132# USAGE: _setContents <info>
133#
134# Used internally to load HTML info into the display area for this
135# widget.
136# ----------------------------------------------------------------------
137itcl::body Rappture::Note::_setContents {info} {
138    switch -regexp -- $info {
139        ^https?:// {
140            if {[catch {http::geturl $info} token] == 0} {
141                set html [http::data $token]
142                http::cleanup $token
143            } else {
144                set html "<html><body><h1>Oops! Internal Error</h1><p>[_escapeChars $token]</p></body></html>"
145            }
146            $itk_component(html) load $html
147        }
148        ^file:// {
149            set file [string range $info 7 end]
150            if {[file pathtype $file] != "absolute"} {
151                # find the file on a search path
152                set dir [[$_owner tool] installdir]
153                set searchlist [list $dir [file join $dir docs]]
154                foreach dir $searchlist {
155                    if {[file readable [file join $dir $file]]} {
156                        set file [file join $dir $file]
157                        break
158                    }
159                }
160            }
161
162            # load the contents of the file
163            set cmds {
164                set fid [open $file r]
165                set html [read $fid]
166                close $fid
167            }
168            if {[catch $cmds result]} {
169                set html "<html><body><h1>Oops! File Not Found</h1><p>[_escapeChars $result]</p></body></html>"
170            }
171
172            # not HTML? then escape nasty characters and display it.
173            if {![regexp {<html>.*</html>} $html]} {
174                set html "<html><body><p>[_escapeChars $html]</p></body></html>"
175            }
176            $itk_component(html) load $html -in $file
177        }
178        default {
179            set html "<html><body><p>[_escapeChars $info]</p></body></html>"
180            $itk_component(html) load $html
181        }
182    }
183}
184
185# ----------------------------------------------------------------------
186# USAGE: _escapeChars <info>
187#
188# Used internally to escape HTML characters in ordinary text.  Used
189# when trying to display ordinary text, which may have things like
190# "<b>" or "x < 2" embedded within it.
191# ----------------------------------------------------------------------
192itcl::body Rappture::Note::_escapeChars {info} {
193    regsub -all & $info \001 info
194    regsub -all \" $info {\&quot;} info
195    regsub -all < $info {\&lt;} info
196    regsub -all > $info {\&gt;} info
197    regsub -all \001 $info {\&amp;} info
198    return $info
199}
Note: See TracBrowser for help on using the repository browser.