source: trunk/gui/scripts/editor.tcl @ 11

Last change on this file since 11 was 11, checked in by mmc, 19 years ago

Major reorganization of the entire package. The config.xml file
is now irrelevant. All the action is in the tool.xml file. The
main program now organizes all input into 1) side-by-side pages,
2) input/result (wizard-style) pages, or 3) a series of wizard-
style pages. The <input> can have <phase> parts representing
the various pages.

Added a new ContourResult? widget based on Swaroop's vtk plotting
code.

Also, added easymesh and showmesh to the "tools" directory.
We need these for Eric Polizzi's code.

File size: 9.4 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: editor - pop-up editor for little bits of text
3#
4#  This widget acts as a pop-up editor for small text fields.  It
5#  pops up on top of any text field, accepts edits, and then attempts
6#  to validate and apply changes back to the underlying widget.
7#
8#  This widget uses a number of callbacks to handle communication
9#  with the underlying widget:
10#
11#  -activatecommand .... Should return a key/value list with the
12#                        following elements:
13#                          x ...... root x coordinate for editor
14#                          y ...... root y coordinate for editor
15#                          w ...... width of text being edited
16#                          h ...... height of text being edited
17#                          text ... initial text for the editor
18#
19#  -validatecommand .... Invoked with the new value as an argument.
20#                        Should return 1 if the value is okay, and
21#                        0 otherwise.
22#
23#  -applycommand ....... Invoked with the new value as an argument.
24#                        Should apply the new value to the underlying
25#                        widget.
26#
27# ======================================================================
28#  AUTHOR:  Michael McLennan, Purdue University
29#  Copyright (c) 2004-2005
30#  Purdue Research Foundation, West Lafayette, IN
31# ======================================================================
32package require Itk
33
34option add *Editor.background white widgetDefault
35option add *Editor.outline black widgetDefault
36option add *Editor.borderwidth 1 widgetDefault
37option add *Editor.relief flat widgetDefault
38option add *Editor.selectBorderWidth 0 widgetDefault
39
40itcl::class Rappture::Editor {
41    inherit itk::Toplevel
42
43    itk_option define -outline outline Outline ""
44    itk_option define -activatecommand activateCommand ActivateCommand ""
45    itk_option define -validatecommand validateCommand ValidateCommand ""
46    itk_option define -applycommand applyCommand ApplyCommand ""
47
48    constructor {args} { # defined below }
49
50    public method activate {}
51    public method deactivate {args}
52    public method value {newval}
53
54    protected method _click {x y}
55    protected method _resize {}
56    protected variable _loc   ;# array of editor location parameters
57}
58                                                                               
59itk::usual Editor {
60    keep -cursor -font
61}
62
63# ----------------------------------------------------------------------
64# CONSTRUCTOR
65# ----------------------------------------------------------------------
66itcl::body Rappture::Editor::constructor {args} {
67    wm overrideredirect $itk_component(hull) yes
68    wm withdraw $itk_component(hull)
69
70    itk_option remove hull.background hull.borderwidth
71    component hull configure -borderwidth 1
72
73    itk_component add editor {
74        entry $itk_interior.editor -highlightthickness 0
75    } {
76        usual
77        keep -relief
78        ignore -highlightthickness
79        ignore -highlightcolor
80        ignore -highlightbackground
81    }
82    pack $itk_component(editor) -expand yes -fill both
83
84    bind $itk_component(editor) <KeyPress> \
85        [itcl::code $this _resize]
86    bind $itk_component(editor) <KeyPress-Return> \
87        [itcl::code $this deactivate]
88    bind $itk_component(editor) <KeyPress-Escape> \
89        [itcl::code $this deactivate -abort]
90    bind $itk_component(editor) <ButtonPress> \
91        [itcl::code $this _click %X %Y]
92
93    eval itk_initialize $args
94}
95
96# ----------------------------------------------------------------------
97# USAGE: activate
98#
99# Clients use this to start the editing process on the underlying
100# widget.  This pops up the editor with the current text from the
101# underlying widget and allows the user to edit the text.  The editor
102# remains up until it is deactivated.
103# ----------------------------------------------------------------------
104itcl::body Rappture::Editor::activate {} {
105    set e $itk_component(editor)
106
107    set info ""
108    if {[string length $itk_option(-activatecommand)] > 0} {
109        set status [catch {uplevel #0 $itk_option(-activatecommand)} info]
110        if {$status != 0} {
111            bgerror $info
112            return
113        }
114    }
115
116    #
117    # Pull out the location information from the values passed back
118    # from the activation command.  We must have at least an x,y
119    # coordinate.  If we get width and height too, then use it.
120    # If not, figure out the width and height based on the size
121    # of the string.
122    #
123    array set vals $info
124    if {![info exists vals(x)] || ![info exists vals(y)]} {
125        return
126    }
127    set _loc(x) $vals(x)
128    set _loc(y) $vals(y)
129    set _loc(w) [expr {([info exists vals(w)]) ? $vals(w) : 0}]
130    set _loc(h) [expr {([info exists vals(h)]) ? $vals(h) : 0}]
131
132    $itk_component(editor) delete 0 end
133    if {[info exists vals(text)]} {
134        $itk_component(editor) insert end $vals(text)
135    }
136    $itk_component(editor) select from 0
137    $itk_component(editor) select to end
138
139    _resize
140    wm deiconify $itk_component(hull)
141    raise $itk_component(hull)
142    focus $itk_component(editor)
143
144    # try to grab the pointer, and keep trying...
145    update
146    while {[catch {grab set -global $itk_component(editor)}]} {
147        after 100
148    }
149}
150
151# ----------------------------------------------------------------------
152# USAGE: deactivate ?-abort?
153#
154# This is invoked automatically whenever the user presses Enter or
155# Escape in the editor.  Clients can also use it explicitly to
156# deactivate the editor.
157#
158# If the -abort flag is specified, then the editor is taken down
159# without any validation or application of the result.  Otherwise,
160# we validate the contents of the editor and apply the change back
161# to the widget.
162# ----------------------------------------------------------------------
163itcl::body Rappture::Editor::deactivate {args} {
164    # take down any error cue that might be up
165    ::Rappture::Tooltip::cue hide
166
167    if {$args == "-abort"} {
168        grab release $itk_component(editor)
169        wm withdraw $itk_component(hull)
170        return
171    }
172
173    set str [$itk_component(editor) get]
174
175    #
176    # If there's a -validatecommand option, then invoke the code
177    # now to check the new value.
178    #
179    if {[string length $itk_option(-validatecommand)] > 0} {
180        set cmd "uplevel #0 [list $itk_option(-validatecommand) [list $str]]"
181        if {[catch $cmd result]} {
182            bgerror $result
183            set result 1
184        }
185        if {$result == 0} {
186            bell
187            $itk_component(editor) select from 0
188            $itk_component(editor) select to end
189            $itk_component(editor) icursor end
190            focus $itk_component(editor)
191            return
192        }
193    }
194
195    grab release $itk_component(editor)
196    wm withdraw $itk_component(hull)
197
198    #
199    # If there's an -applycommand option, then invoke the code
200    # now to apply the new value.
201    #
202    if {[string length $itk_option(-applycommand)] > 0} {
203        set cmd "uplevel #0 [list $itk_option(-applycommand) [list $str]]"
204        if {[catch $cmd result]} {
205            bgerror $result
206            return
207        }
208    }
209}
210
211# ----------------------------------------------------------------------
212# USAGE: value <newval>
213#
214# Clients use this to suggest a new value, particular when they've
215# caught an error in the editing process.  For example, if the user's
216# value is below the minimum allowed value, a client would call this
217# method to suggest the minimum value.
218# ----------------------------------------------------------------------
219itcl::body Rappture::Editor::value {newval} {
220    $itk_component(editor) delete 0 end
221    $itk_component(editor) insert end $newval
222}
223
224# ----------------------------------------------------------------------
225# USAGE: _click <X> <Y>
226#
227# This is invoked automatically whenever the user clicks somewhere
228# inside or outside of the editor.  If the <X>,<Y> coordinate is
229# outside the editor, then we assume the user is done and wants to
230# take the editor down.  Otherwise, we do nothing, and let the entry
231# bindings take over.
232# ----------------------------------------------------------------------
233itcl::body Rappture::Editor::_click {x y} {
234    if {[winfo containing $x $y] != $itk_component(editor)} {
235        deactivate
236    }
237}
238
239# ----------------------------------------------------------------------
240# USAGE: _resize
241#
242# Invoked automatically as each key is pressed in the editor.
243# Resizes the editor so that it is just big enough to show all
244# of the text within it.
245# ----------------------------------------------------------------------
246itcl::body Rappture::Editor::_resize {} {
247    set e $itk_component(editor)
248    set str [$e get]
249    set fnt [$e cget -font]
250
251    set w [expr {[font measure $fnt $str]+20}]
252    set w [expr {($w < $_loc(w)) ? $_loc(w) : $w}]
253    if {$w+$_loc(x) >= [winfo screenwidth $e]} {
254        set w [expr {[winfo screenwidth $e]-$_loc(x)}]
255    }
256
257    set h [expr {[font metrics $fnt -linespace]+4}]
258    set h [expr {($h < $_loc(h)) ? $_loc(h) : $h}]
259    if {$h+$_loc(y) >= [winfo screenwidth $e]} {
260        set h [expr {[winfo screenwidth $e]-$_loc(y)}]
261    }
262
263    wm geometry $itk_component(hull) "${w}x${h}+$_loc(x)+$_loc(y)"
264}
265
266# ----------------------------------------------------------------------
267# CONFIGURATION OPTION: -outline
268# ----------------------------------------------------------------------
269itcl::configbody Rappture::Editor::outline {
270    component hull configure -background $itk_option(-outline)
271}
Note: See TracBrowser for help on using the repository browser.