summaryrefslogtreecommitdiff
path: root/itcl/iwidgets/generic/checkbox.itk
blob: 49e88887f76eaad0d663ee227a3f9575fc6acecd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#
# Checkbox
# ----------------------------------------------------------------------
# Implements a checkbuttonbox.  Supports adding, inserting, deleting,
# selecting, and deselecting of checkbuttons by tag and index.
#
# ----------------------------------------------------------------------
#  AUTHOR: John A. Tucker                EMAIL: jatucker@spd.dsccc.com
#
# ----------------------------------------------------------------------
#            Copyright (c) 1997 DSC Technologies Corporation
# ======================================================================
# Permission to use, copy, modify, distribute and license this software 
# and its documentation for any purpose, and without fee or written 
# agreement with DSC, is hereby granted, provided that the above copyright 
# notice appears in all copies and that both the copyright notice and 
# warranty disclaimer below appear in supporting documentation, and that 
# the names of DSC Technologies Corporation or DSC Communications 
# Corporation not be used in advertising or publicity pertaining to the 
# software without specific, written prior permission.
# 
# DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
# INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
# AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, 
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL 
# DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
# SOFTWARE.
# ======================================================================


#
# Use option database to override default resources of base classes.
#
option add *Checkbox.labelMargin	10	widgetDefault
option add *Checkbox.labelFont     \
      "-Adobe-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*"  widgetDefault
option add *Checkbox.labelPos		nw	widgetDefault
option add *Checkbox.borderWidth	2	widgetDefault
option add *Checkbox.relief		groove	widgetDefault

#
# Usual options.
#
itk::usual Checkbox {
    keep -background -borderwidth -cursor -foreground -labelfont
}

# ------------------------------------------------------------------
#                            CHECKBOX
# ------------------------------------------------------------------
itcl::class iwidgets::Checkbox {
    inherit iwidgets::Labeledframe

    constructor {args} {}

    itk_option define -orient orient Orient vertical

    public {
      method add {tag args}
      method insert {index tag args}
      method delete {index}
      method get {{index ""}}
      method index {index}
      method select {index}
      method deselect {index}
      method flash {index}
      method toggle {index}
      method buttonconfigure {index args}
  }

  private {

      method gettag {index}      ;# Get the tag of the checkbutton associated
                                 ;# with a numeric index

      variable _unique 0         ;# Unique id for choice creation.
      variable _buttons {}       ;# List of checkbutton tags.
      common buttonVar           ;# Array of checkbutton "-variables"
  }
}

#
# Provide a lowercased access method for the Checkbox class.
#
proc ::iwidgets::checkbox {pathName args} {
    uplevel ::iwidgets::Checkbox $pathName $args
}

# ------------------------------------------------------------------
#                        CONSTRUCTOR
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::constructor {args} {

    eval itk_initialize $args
}

# ------------------------------------------------------------------
#                            OPTIONS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# OPTION: -orient
#
# Allows the user to orient the checkbuttons either horizontally
# or vertically.  Added by Chad Smith (csmith@adc.com) 3/10/00.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Checkbox::orient {
  if {$itk_option(-orient) == "horizontal"} {
    foreach tag $_buttons {
      pack $itk_component($tag) -side left -anchor nw -padx 4 -expand 1
    }
  } elseif {$itk_option(-orient) == "vertical"} {
    foreach tag $_buttons {
      pack $itk_component($tag) -side top -anchor w -padx 4 -expand 0
    }
  } else {
    error "Bad orientation: $itk_option(-orient).  Should be\
      \"horizontal\" or \"vertical\"."
  }
}


# ------------------------------------------------------------------
#                            METHODS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# METHOD: index index
#
# Searches the checkbutton tags in the checkbox for the one with the
# requested tag, numerical index, or keyword "end".  Returns the 
# choices's numerical index if found, otherwise error.
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::index {index} {
    if {[llength $_buttons] > 0} {
        if {[regexp {(^[0-9]+$)} $index]} {
            if {$index < [llength $_buttons]} {
                return $index
            } else {
                error "Checkbox index \"$index\" is out of range"
            }

        } elseif {$index == "end"} {
            return [expr {[llength $_buttons] - 1}]

        } else {
            if {[set idx [lsearch $_buttons $index]] != -1} {
                return $idx
            }

            error "bad Checkbox index \"$index\": must be number, end,\
                    or pattern"
        }

    } else {
        error "Checkbox \"$itk_component(hull)\" has no checkbuttons"
    }
}

# ------------------------------------------------------------------
# METHOD: add tag ?option value option value ...?
#
# Add a new tagged checkbutton to the checkbox at the end.  The method 
# takes additional options which are passed on to the checkbutton
# constructor.  These include most of the typical checkbutton 
# options.  The tag is returned.
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::add {tag args} {
    itk_component add $tag {
        eval checkbutton $itk_component(childsite).cb[incr _unique] \
            -variable [list [itcl::scope buttonVar($this,$tag)]] \
            -anchor w \
            -justify left \
            -highlightthickness 0 \
            $args
    } { 
      usual
      keep -command -disabledforeground -selectcolor -state
      ignore -highlightthickness -highlightcolor
      rename -font -labelfont labelFont Font
    }

    # Redraw the buttons with the proper orientation.
    if {$itk_option(-orient) == "vertical"} {
      pack $itk_component($tag) -side top -anchor w -padx 4 -expand 0
    } else {
      pack $itk_component($tag) -side left -anchor nw -expand 1
    }

    lappend _buttons $tag

    return $tag
}

# ------------------------------------------------------------------
# METHOD: insert index tag ?option value option value ...?
#
# Insert the tagged checkbutton in the checkbox just before the 
# one given by index.  Any additional options are passed on to the
# checkbutton constructor.  These include the typical checkbutton
# options.  The tag is returned.
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::insert {index tag args} {
    itk_component add $tag {
        eval checkbutton $itk_component(childsite).cb[incr _unique] \
            -variable [list [itcl::scope buttonVar($this,$tag)]] \
            -anchor w \
            -justify left \
            -highlightthickness 0 \
            $args
    }  { 
      usual
      ignore -highlightthickness -highlightcolor
      rename -font -labelfont labelFont Font
    }

    set index [index $index]
    set before [lindex $_buttons $index]
    set _buttons [linsert $_buttons $index $tag]

    pack $itk_component($tag) -anchor w -padx 4 -before $itk_component($before)

    return $tag
}

# ------------------------------------------------------------------
# METHOD: delete index
#
# Delete the specified checkbutton.
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::delete {index} {

    set tag [gettag $index]
    set index [index $index]
    destroy $itk_component($tag)
    set _buttons [lreplace $_buttons $index $index]

    if { [info exists buttonVar($this,$tag)] == 1 } {
	unset buttonVar($this,$tag)
    }
}

# ------------------------------------------------------------------
# METHOD: select index
#
# Select the specified checkbutton.
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::select {index} {
    set tag [gettag $index]
    #-----------------------------------------------------------
    # BUG FIX: csmith (Chad Smith: csmith@adc.com), 3/30/99
    #-----------------------------------------------------------
    # This method should only invoke the checkbutton if it's not
    # already selected.  Check its associated variable, and if
    # it's set, then just ignore and return.
    #-----------------------------------------------------------
    if {[set [itcl::scope buttonVar($this,$tag)]] == 
	[[component $tag] cget -onvalue]} {
      return
    }
    $itk_component($tag) invoke
}

# ------------------------------------------------------------------
# METHOD: toggle index
#
# Toggle a specified checkbutton between selected and unselected
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::toggle {index} {
    set tag [gettag $index]
    $itk_component($tag) toggle
}

# ------------------------------------------------------------------
# METHOD: get
#
# Return the value of the checkbutton with the given index, or a
# list of all checkbutton values in increasing order by index.
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::get {{index ""}} {
    set result {}

    if {$index == ""} {
	foreach tag $_buttons {
	    if {$buttonVar($this,$tag)} {
		lappend result $tag
	    }
	}
    } else {
        set tag [gettag $index]
	set result $buttonVar($this,$tag)
    }

    return $result
}

# ------------------------------------------------------------------
# METHOD: deselect index
#
# Deselect the specified checkbutton.
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::deselect {index} {
    set tag [gettag $index]
    $itk_component($tag) deselect
}

# ------------------------------------------------------------------
# METHOD: flash index
#
# Flash the specified checkbutton.
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::flash {index} {
    set tag [gettag $index]
    $itk_component($tag) flash  
}

# ------------------------------------------------------------------
# METHOD: buttonconfigure index ?option? ?value option value ...?
#
# Configure a specified checkbutton.  This method allows configuration 
# of checkbuttons from the Checkbox level.  The options may have any 
# of the values accepted by the add method.
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::buttonconfigure {index args} { 
    set tag [gettag $index]
    eval $itk_component($tag) configure $args
}

# ------------------------------------------------------------------
# METHOD: gettag index
#
# Return the tag of the checkbutton associated with a specified
# numeric index
# ------------------------------------------------------------------
itcl::body iwidgets::Checkbox::gettag {index} {
    return [lindex $_buttons [index $index]]
}