source: branches/1.7/gui/scripts/note.tcl @ 6561

Last change on this file since 6561 was 5679, checked in by ldelgass, 9 years ago

Full merge 1.3 branch to uq branch to sync. Fixed partial subdirectory merge
by removing mergeinfo from lang/python/Rappture directory.

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