source: trunk/lang/tcl/scripts/types/choices.tcl @ 2157

Last change on this file since 2157 was 2157, checked in by mmc, 13 years ago

Added support for -tooltip options on all attributes, and added descriptions
of attributes to all objects. This gives the user pop-up help inside the
builder.

File size: 8.6 KB
Line 
1# ----------------------------------------------------------------------
2#  EDITOR: choice option values
3#
4#  Used within the Instant Rappture builder to edit the options
5#  associated with a "choice" object.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004-2010  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# ======================================================================
13itcl::class AttrChoices {
14    constructor {win args} {
15        Rappture::getopts args params {
16            value -tooltip ""
17        }
18
19        frame $win.cntls
20        pack $win.cntls -fill x
21        button $win.cntls.add -text "Add" -command [itcl::code $this _add]
22        pack $win.cntls.add -side left
23        Rappture::Tooltip::for $win.cntls.add "Adds a new option to the list of choices."
24        button $win.cntls.del -text "Delete" -command [itcl::code $this _delete]
25        pack $win.cntls.del -side right
26        Rappture::Tooltip::for $win.cntls.del "Removes an option from the list of choices."
27
28        Rappture::Scroller $win.scrl -height 1.5i \
29            -xscrollmode auto -yscrollmode auto
30        pack $win.scrl -fill x
31        listbox $win.scrl.lbox -selectmode single -exportselection off
32        bind $win.scrl.lbox <<ListboxSelect>> [itcl::code $this _edit]
33        $win.scrl contents $win.scrl.lbox
34        Rappture::Tooltip::for $win.scrl.lbox $params(-tooltip)
35
36        frame $win.editelem
37        pack $win.editelem -fill x
38        grid columnconfigure $win.editelem 1 -weight 1
39
40        label $win.editelem.title -text "Edit selected entry:"
41        grid $win.editelem.title -row 0 -column 0 -columnspan 3 -sticky w
42        label $win.editelem.l -text "Label:"
43        grid $win.editelem.l -row 1 -column 0 -sticky e
44        entry $win.editelem.label
45        Rappture::Tooltip::for $win.editelem.label "Label for the option that appears in the drop-down list of choices."
46        grid $win.editelem.label -row 1 -column 1 -columnspan 2 -sticky ew
47        label $win.editelem.v -text "Value:"
48        grid $win.editelem.v -row 2 -column 0 -sticky e
49        entry $win.editelem.value
50        Rappture::Tooltip::for $win.editelem.value "Value for the option reported to your program when this choice is selected."
51        grid $win.editelem.value -row 2 -column 1 -sticky ew
52        label $win.editelem.vopt -text "(optional)"
53        grid $win.editelem.vopt -row 2 -column 2 -sticky e
54        label $win.editelem.d -text "Description:"
55        grid $win.editelem.d -row 3 -column 0 -sticky e
56        Rappture::Scroller $win.editelem.scrl -height 1i -yscrollmode auto
57        grid $win.editelem.scrl -row 3 -column 1 -columnspan 2 -sticky nsew
58        text $win.editelem.scrl.desc -wrap char
59        $win.editelem.scrl contents $win.editelem.scrl.desc
60        Rappture::Tooltip::for $win.editelem.scrl.desc "Description of the option that appears in the tooltip when you mouse over the choice when it appears in the combobox."
61
62        bind $win.editelem.label <KeyPress-Return> \
63            "[itcl::code $this _finalize label]; focus \[tk_focusNext %W\]"
64        bind $win.editelem.label <KeyPress-Tab> \
65            "[itcl::code $this _finalize label]; focus \[tk_focusNext %W\]"
66        bind $win.editelem.value <KeyPress-Return> "focus \[tk_focusNext %W\]"
67
68        set _win $win
69    }
70    destructor {
71    }
72
73    public method load {opts} {
74        set _options ""
75        foreach rec $opts {
76            set label [lindex $rec 0]
77            set value [lindex $rec 1]
78            set desc [lindex $rec 2]
79            lappend _options [list $label $value $desc]
80        }
81
82        # display all options and select the first one for editing
83        $_win.scrl.lbox delete 0 end
84        foreach rec $_options {
85            $_win.scrl.lbox insert end [lindex $rec 0]
86        }
87        $_win.scrl.lbox selection set 0
88        _edit
89    }
90
91    public method check {} {
92        # should be okay -- labels/values can be anything
93    }
94
95    public method save {var} {
96        _finalize  ;# apply any pending changes
97
98        upvar $var val
99        set val $_options
100
101        return 1
102    }
103
104    public method edit {} {
105        focus -force $_win.scrl.lbox
106    }
107
108    public proc import {xmlobj path} {
109        # instead of storing within the given path, we must store a
110        # series of <option> tags within the parent on the path
111        set path [join [lrange [split $path .] 0 end-1] .]
112
113        # load the info from various <option> tags
114        set rval ""
115        foreach cpath [$xmlobj children -type option -as path $path] {
116            set label [$xmlobj get $cpath.about.label]
117            set desc  [$xmlobj get $cpath.about.description]
118            set value [$xmlobj get $cpath.value]
119            lappend rval [list $label $value $desc]
120        }
121        return $rval
122    }
123
124    public proc export {xmlobj path value} {
125        # instead of storing within the given path, we must store a
126        # series of <option> tags within the parent on the path
127        set path [join [lrange [split $path .] 0 end-1] .]
128
129        # clear out all existing elements
130        foreach cpath [$xmlobj children -type option -as path $path] {
131            $xmlobj remove $cpath
132        }
133
134        # create new elements for all options
135        set id 0
136        foreach rec $value {
137            foreach {label value desc} $rec break
138            set elem "option([incr id])"
139            $xmlobj put $path.$elem.about.label $label
140            if {$desc ne ""} {
141                $xmlobj put $path.$elem.about.description $desc
142            }
143            if {$value ne ""} {
144                $xmlobj put $path.$elem.value $value
145            }
146        }
147    }
148
149    public method clear {} {
150        set _options ""
151    }
152
153    # add a new entry into the list and load it for editing
154    private method _add {} {
155        _finalize   ;# apply any pending changes
156        set name "Option #[incr _count]"
157        $_win.scrl.lbox insert end $name
158        lappend _options [list $name "" ""]
159        $_win.scrl.lbox selection clear 0 end
160        $_win.scrl.lbox selection set end
161        $_win.scrl.lbox see end
162        _edit  ;# edit the new entry
163    }
164
165    # delete the selected entry from the list and select the next one
166    private method _delete {} {
167        set i [$_win.scrl.lbox curselection]
168        if {$i ne ""} {
169            _finalize clear  ;# clear editing area
170            set _options [lreplace $_options $i $i]
171            $_win.scrl.lbox delete $i
172
173            # find the next best item to select
174            if {$i >= [llength $_options]} {
175                set i [expr {[llength $_options]-1}]
176            }
177            if {$i >= 0} {
178                $_win.scrl.lbox selection set $i
179                _edit
180            } else {
181                focus $_win.scrl.lbox
182            }
183        }
184    }
185
186    # edit the selected entry
187    private method _edit {} {
188        _finalize   ;# apply any pending changes
189
190        set i [lindex [$_win.scrl.lbox curselection] 0]
191        if {$i ne ""} {
192            set rec [lindex $_options $i]
193            $_win.editelem.label insert 0 [lindex $rec 0]
194            $_win.editelem.value insert 0 [lindex $rec 1]
195            $_win.editelem.scrl.desc insert 1.0 [lindex $rec 2]
196
197            $_win.editelem.label selection from 0
198            $_win.editelem.label selection to end
199            focus $_win.editelem.label
200
201            set _editing $i
202        }
203    }
204
205    # finalize any changes for the entry being edited
206    private method _finalize {{what clear}} {
207        if {$_editing ne "" && $_editing < [llength $_options]} {
208            set lval [string trim [$_win.editelem.label get]]
209            set vval [string trim [$_win.editelem.value get]]
210            set dval [string trim [$_win.editelem.scrl.desc get 1.0 end]]
211            set rec [list $lval $vval $dval]
212            set _options [lreplace $_options $_editing $_editing $rec]
213            $_win.scrl.lbox delete $_editing
214            $_win.scrl.lbox insert $_editing $lval
215            if {$what ne "clear"} {
216                $_win.scrl.lbox selection set $_editing
217            }
218        }
219
220        # no longer editing anything -- clear out
221        if {$what eq "clear"} {
222            set _editing ""
223            $_win.editelem.label delete 0 end
224            $_win.editelem.value delete 0 end
225            $_win.editelem.scrl.desc delete 1.0 end
226        }
227    }
228
229    private variable _win ""       ;# containing frame
230    private variable _count 0      ;# counter for unique numbers
231    private variable _options ""   ;# list of options: {label value desc}
232    private variable _editing ""   ;# index of entry being edited
233}
Note: See TracBrowser for help on using the repository browser.