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