source: trunk/gui/scripts/combobox.tcl @ 16

Last change on this file since 16 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.1 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: combobox - entry widget with a drop-down list of values
3#
4#  This widget is a typical combobox, an entry widget with a drop-down
5#  list of values.  If the -editable option is turned off, then the
6#  value can be set only from the drop-down list.  Otherwise, the
7#  drop-down is treated as a list of preset choices, but the user can
8#  type anything in the entry area.
9# ======================================================================
10#  AUTHOR:  Michael McLennan, Purdue University
11#  Copyright (c) 2004-2005
12#  Purdue Research Foundation, West Lafayette, IN
13# ======================================================================
14package require Itk
15package require BLT
16
17option add *Combobox.borderWidth 2 widgetDefault
18option add *Combobox.relief sunken widgetDefault
19option add *Combobox.width 10 widgetDefault
20option add *Combobox.editable yes widgetDefault
21option add *Combobox.textBackground white widgetDefault
22option add *Combobox.textForeground black widgetDefault
23option add *Combobox.font -*-helvetica-medium-r-normal-*-*-120-* widgetDefault
24
25itcl::class Rappture::Combobox {
26    inherit itk::Widget
27
28    itk_option define -editable editable Editable ""
29    itk_option define -width width Width 0
30
31    constructor {args} { # defined below }
32
33    public method value {args}
34    public method translate {value}
35    public method choices {option args}
36
37    protected method _entry {option}
38    protected method _dropdown {option}
39
40    blt::bitmap define ComboboxArrow {
41        #define arrow_width 8
42        #define arrow_height 4
43        static unsigned char arrow_bits[] = {
44           0xfe, 0x7c, 0x38, 0x10};
45    }
46}
47                                                                               
48itk::usual Combobox {
49    keep -cursor -font
50    keep -foreground -background
51    keep -textforeground -textbackground
52    keep -selectbackground -selectforeground -selectborderwidth
53}
54
55# ----------------------------------------------------------------------
56# CONSTRUCTOR
57# ----------------------------------------------------------------------
58itcl::body Rappture::Combobox::constructor {args} {
59    itk_option add hull.borderwidth hull.relief
60
61    itk_component add button {
62        button $itk_interior.btn -bitmap ComboboxArrow -padx 0 \
63            -borderwidth 1 -relief raised -highlightthickness 0
64    } {
65        usual
66        ignore -highlightthickness -highlightbackground -highlightcolor
67        ignore -borderwidth -relief
68    }
69    pack $itk_component(button) -side right -fill y
70
71    itk_component add entry {
72        entry $itk_interior.entry -borderwidth 0 -relief flat
73    } {
74        usual
75        keep -width
76        rename -highlightbackground -textbackground textBackground Background
77        rename -background -textbackground textBackground Background
78        rename -foreground -textforeground textForeground Foreground
79        rename -disabledbackground -textbackground textBackground Background
80        rename -disabledforeground -textforeground textForeground Foreground
81        ignore -borderwidth -relief
82    }
83    pack $itk_component(entry) -side left -expand yes -fill both
84
85    bind $itk_component(entry) <KeyPress-Return> \
86        [itcl::code $this _entry apply]
87    bind $itk_component(entry) <ButtonPress> \
88        [itcl::code $this _entry click]
89
90    itk_component add ddlist {
91        Rappture::Dropdownlist $itk_component(button).ddlist \
92            -postcommand [itcl::code $this _dropdown post] \
93            -unpostcommand [itcl::code $this _dropdown unpost] \
94    }
95
96    bind $itk_component(ddlist) <<DropdownlistSelect>> \
97        [itcl::code $this _dropdown select]
98
99    $itk_component(button) configure -command \
100        [list $itk_component(ddlist) post $itk_component(hull) left]
101
102    eval itk_initialize $args
103}
104
105# ----------------------------------------------------------------------
106# USAGE: value ?<newval>?
107#
108# Clients use this to query/set the value for this widget.  With
109# no args, it returns the current value for the widget.  If the
110# <newval> is specified, it sets the value of the widget and
111# sends a <<Value>> event.
112# ----------------------------------------------------------------------
113itcl::body Rappture::Combobox::value {args} {
114    if {[llength $args] == 1} {
115        set newval [lindex $args 0]
116
117        $itk_component(entry) configure -state normal
118        $itk_component(entry) delete 0 end
119        $itk_component(entry) insert 0 $newval
120        if {!$itk_option(-editable)} {
121            $itk_component(entry) configure -state disabled
122        }
123
124        after 10 [list event generate $itk_component(hull) <<Value>>]
125    } elseif {[llength $args] != 0} {
126        error "wrong # args: should be \"value ?newval?\""
127    }
128    return [$itk_component(entry) get]
129}
130
131# ----------------------------------------------------------------------
132# USAGE: translate <value>
133#
134# Clients use this to translate a value from the entry part of the
135# combobox to one of the underlying values in the combobox.  If the
136# <value> string matches one of the labels for the choices, this
137# method returns the corresponding value.  Otherwise, it returns "".
138# ----------------------------------------------------------------------
139itcl::body Rappture::Combobox::translate {value} {
140    foreach {val label} [choices get -both] {
141        if {$label == $value} {
142            return $val
143        }
144    }
145    return ""
146}
147
148# ----------------------------------------------------------------------
149# USAGE: choices insert <pos> ?<value1> <label1> ...?
150# USAGE: choices delete <first> ?<last>?
151# USAGE: choices get ?-value|-label|-both? ?<index>?
152# USAGE: choices index <value>
153#
154# Clients use this to manipulate the list of choices in the drop-down
155# list.  Each choice is represented by a (computer-friendly) value
156# and its corresponding (human-friendly) label.  The "get" option
157# returns information about options on the list, including the value,
158# the label, or both.
159# ----------------------------------------------------------------------
160itcl::body Rappture::Combobox::choices {option args} {
161    eval $itk_component(ddlist) $option $args
162}
163
164# ----------------------------------------------------------------------
165# USAGE: _entry apply
166# USAGE: _entry click
167#
168# Used internally to handle the dropdown list for this combobox.  The
169# post/unpost options are invoked when the list is posted or unposted
170# to manage the relief of the controlling button.  The select option
171# is invoked whenever there is a selection from the list, to assign
172# the value back to the gauge.
173# ----------------------------------------------------------------------
174itcl::body Rappture::Combobox::_entry {option} {
175    switch -- $option {
176        apply {
177            if {$itk_option(-editable)} {
178                event generate $itk_component(hull) <<Value>>
179            }
180        }
181        click {
182            if {!$itk_option(-editable)} {
183                $itk_component(button) configure -relief sunken
184                update idletasks; after 100
185                $itk_component(button) configure -relief raised
186
187                $itk_component(ddlist) post $itk_component(hull) left
188            }
189        }
190        default {
191            error "bad option \"$option\": should be apply, click"
192        }
193    }
194}
195
196# ----------------------------------------------------------------------
197# USAGE: _dropdown post
198# USAGE: _dropdown unpost
199# USAGE: _dropdown select
200#
201# Used internally to handle the dropdown list for this combobox.  The
202# post/unpost options are invoked when the list is posted or unposted
203# to manage the relief of the controlling button.  The select option
204# is invoked whenever there is a selection from the list, to assign
205# the value back to the gauge.
206# ----------------------------------------------------------------------
207itcl::body Rappture::Combobox::_dropdown {option} {
208    switch -- $option {
209        post {
210            set value [$itk_component(entry) get]
211            set i [$itk_component(ddlist) index -label $value]
212            if {$i >= 0} {
213                $itk_component(ddlist) select clear 0 end
214                $itk_component(ddlist) select set $i
215            }
216        }
217        unpost {
218            if {$itk_option(-editable)} {
219                focus $itk_component(entry)
220            }
221        }
222        select {
223            set val [$itk_component(ddlist) current -label]
224            if {"" != $val} {
225                value $val
226            }
227        }
228        default {
229            error "bad option \"$option\": should be post, unpost, select"
230        }
231    }
232}
233
234# ----------------------------------------------------------------------
235# CONFIGURATION OPTION: -editable
236# ----------------------------------------------------------------------
237itcl::configbody Rappture::Combobox::editable {
238    if {![string is boolean -strict $itk_option(-editable)]} {
239        error "bad value \"$itk_option(-editable)\": should be boolean"
240    }
241    if {$itk_option(-editable)} {
242        $itk_component(entry) configure -state normal
243    } else {
244        $itk_component(entry) configure -state disabled
245        if {[focus] == $itk_component(entry)} {
246            focus [tk_focusNext [focus]]
247        }
248    }
249}
Note: See TracBrowser for help on using the repository browser.