source: trunk/gui/scripts/imageentry.tcl @ 1599

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

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

File size: 7.3 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: ImageEntry - widget for displaying images
3#
4#  This widget represents an <image> entry on a control panel.
5#  It displays images which help explain the input.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004-2005  Purdue Research Foundation
9#
10#  See the file "license.terms" for information on usage and
11#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12# ======================================================================
13package require Itk
14package require Img
15
16itcl::class Rappture::ImageEntry {
17    inherit itk::Widget
18
19    itk_option define -state state State "normal"
20
21    constructor {owner path args} { # defined below }
22    destructor { # defined below }
23
24    public method value {args}
25
26    public method label {}
27    public method tooltip {}
28
29    private method _redraw {}
30
31    private variable _owner ""    ;# thing managing this control
32    private variable _path ""     ;# path in XML to this image
33    private variable _imh ""      ;# image handle for current value
34    private variable _resize ""   ;# image for resize operations
35}
36
37itk::usual ImageEntry {
38    keep -cursor -font
39    keep -foreground -background
40    keep -textbackground
41    keep -selectbackground -selectforeground -selectborderwidth
42}
43
44# ----------------------------------------------------------------------
45# CONSTRUCTOR
46# ----------------------------------------------------------------------
47itcl::body Rappture::ImageEntry::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 image {
59        ::label $itk_interior.image -borderwidth 0
60    }
61    pack $itk_component(image) -expand yes -fill both
62    bind $itk_component(image) <Configure> [itcl::code $this _redraw]
63
64    set str [$_owner xml get $path.current]
65    if {[string length $str] == 0} {
66        set str [$_owner xml get $path.default]
67    }
68    if {[string length $str] > 0} {
69        value $str
70    }
71
72    eval itk_initialize $args
73}
74
75# ----------------------------------------------------------------------
76# DESTRUCTOR
77# ----------------------------------------------------------------------
78itcl::body Rappture::ImageEntry::destructor {} {
79    if {"" != $_imh} { image delete $_imh }
80    if {"" != $_resize} { image delete $_resize }
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# ----------------------------------------------------------------------
92itcl::body Rappture::ImageEntry::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        if {[string length $newval] > 0} {
107            set imh [image create photo -data $newval]
108        } else {
109            set imh ""
110        }
111
112        if {$_imh != ""} {
113            image delete $_imh
114        }
115        set _imh $imh
116        _redraw
117        return $newval
118
119    } elseif {[llength $args] != 0} {
120        error "wrong # args: should be \"value ?-check? ?newval?\""
121    }
122
123    #
124    # Query the value and return.
125    #
126    set data ""
127    if {"" != $_imh} { set data [$_imh cget -data] }
128    return $data
129}
130
131# ----------------------------------------------------------------------
132# USAGE: label
133#
134# Clients use this to query the label associated with this widget.
135# Reaches into the XML and pulls out the appropriate label string.
136# ----------------------------------------------------------------------
137itcl::body Rappture::ImageEntry::label {} {
138    set label [$_owner xml get $_path.about.label]
139    return [string trim $label]
140}
141
142# ----------------------------------------------------------------------
143# USAGE: tooltip
144#
145# Clients use this to query the tooltip associated with this widget.
146# Reaches into the XML and pulls out the appropriate description
147# string.  Returns the string that should be used with the
148# Rappture::Tooltip facility.
149# ----------------------------------------------------------------------
150itcl::body Rappture::ImageEntry::tooltip {} {
151    set str [$_owner xml get $_path.about.description]
152    return [string trim $str]
153}
154
155# ----------------------------------------------------------------------
156# USAGE: _redraw
157#
158# Used internally to update the image displayed in this entry.
159# If the <resize> parameter is set, then the image is resized.
160# The "auto" value resizes to the current area.  The "width=XX" or
161# "height=xx" form resizes to a particular size.  The "none" value
162# leaves the image as-is; this is the default.
163# ----------------------------------------------------------------------
164itcl::body Rappture::ImageEntry::_redraw {} {
165    if {"" == $_imh} {
166        $itk_component(image) configure -image ""
167        return
168    }
169
170    set iw [image width $_imh]
171    set ih [image height $_imh]
172    $itk_component(image) configure -image "" -width $iw -height $ih
173
174    set str [string trim [$_owner xml get $_path.resize]]
175    if {"" == $str} {
176        set str "none"
177    }
178    switch -glob -- $str {
179        auto {
180            if {$_resize == ""} {
181                set _resize [image create photo]
182            }
183            set w [winfo width $itk_component(image)]
184            set h [winfo height $itk_component(image)]
185            if {$w/double($iw) < $h/double($ih)} {
186                set h [expr {round($w/double($iw)*$ih)}]
187            } else {
188                set w [expr {round($h/double($ih)*$iw)}]
189            }
190            $_resize configure -width $w -height $h
191            blt::winop resample $_imh $_resize
192            $itk_component(image) configure -image $_resize
193        }
194        width=* - height=* {
195            if {$_resize == ""} {
196                set _resize [image create photo]
197            }
198            if {[regexp {^width=([0-9]+)$} $str match size]} {
199                set w $size
200                set h [expr {round($w*$ih/double($iw))}]
201                $_resize configure -width $w -height $h
202                blt::winop resample $_imh $_resize
203                $itk_component(image) configure -image $_resize \
204                    -width $w -height $h
205            } elseif {[regexp {^height=([0-9]+)$} $str match size]} {
206                set h $size
207                set w [expr {round($h*$iw/double($ih))}]
208                $_resize configure -width $w -height $h
209                blt::winop resample $_imh $_resize
210                $itk_component(image) configure -image $_resize \
211                    -width $w -height $h
212            } else {
213                $itk_component(image) configure -image $_imh
214            }
215        }
216        default {
217            $itk_component(image) configure -image $_imh
218        }
219    }
220}
221
222# ----------------------------------------------------------------------
223# CONFIGURATION OPTION: -state
224# ----------------------------------------------------------------------
225itcl::configbody Rappture::ImageEntry::state {
226    set valid {normal disabled}
227    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
228        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
229    }
230    $itk_component(image) configure -state $itk_option(-state)
231}
Note: See TracBrowser for help on using the repository browser.