summaryrefslogtreecommitdiff
path: root/itcl/iwidgets/generic/buttonbox.itk
blob: dabada368ccd7d126532a70887400029acb44639 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#
# Buttonbox
# ----------------------------------------------------------------------
# Manages a framed area with Motif style buttons.  The button box can 
# be configured either horizontally or vertically.  
#
# ----------------------------------------------------------------------
#  AUTHOR: Mark L. Ulferts              EMAIL: mulferts@austin.dsccc.com
#          Bret A. Schuhmacher          EMAIL: bas@wn.com
#
#  @(#) $Id$
# ----------------------------------------------------------------------
#            Copyright (c) 1995 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.
# ======================================================================

#
# Usual options.
#
itk::usual Buttonbox {
    keep -background -cursor -foreground
}

# ------------------------------------------------------------------
#                            BUTTONBOX
# ------------------------------------------------------------------
itcl::class iwidgets::Buttonbox {
    inherit itk::Widget

    constructor {args} {}
    destructor {}

    itk_option define -pady padY Pad 5
    itk_option define -padx padX Pad 5
    itk_option define -orient orient Orient "horizontal"
    itk_option define -foreground foreground Foreground black
    
    public method index {args}
    public method add {args}
    public method insert {args}
    public method delete {args}
    public method default {args}
    public method hide {args}
    public method show {args}
    public method invoke {args}
    public method buttonconfigure {args}
    public method buttoncget {index option}

    private method _positionButtons {}
    private method _setBoxSize {{when later}}
    private method _getMaxWidth {}
    private method _getMaxHeight {}

    private variable _resizeFlag {}         ;# Flag for resize needed.
    private variable _buttonList {}         ;# List of all buttons in box.
    private variable _displayList {}        ;# List of displayed buttons.
    private variable _unique 0              ;# Counter for button widget ids.
}

namespace eval iwidgets::Buttonbox {
    #
    # Set up some class level bindings for map and configure events.
    #
    bind bbox-map <Map> [itcl::code %W _setBoxSize]
    bind bbox-config <Configure> [itcl::code %W _positionButtons]
}

#
# Provide a lowercased access method for the Buttonbox class.
# 
proc ::iwidgets::buttonbox {pathName args} {
    uplevel ::iwidgets::Buttonbox $pathName $args
}
    
# ------------------------------------------------------------------
#                        CONSTRUCTOR
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::constructor {args} {
    # 
    # Add Configure bindings for geometry management.  
    #
    bindtags $itk_component(hull) \
	    [linsert [bindtags $itk_component(hull)] 0 bbox-map]
    bindtags $itk_component(hull) \
	    [linsert [bindtags $itk_component(hull)] 1 bbox-config]
    
    pack propagate $itk_component(hull) no
    
    #
    # Initialize the widget based on the command line options.
    #
    eval itk_initialize $args
}

# ------------------------------------------------------------------
#                           DESTRUCTOR
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::destructor {} {
    if {$_resizeFlag != ""} {after cancel $_resizeFlag}
}

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

# ------------------------------------------------------------------
# OPTION: -pady
#
# Pad the y space between the button box frame and the hull.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Buttonbox::pady {
    _setBoxSize
}

# ------------------------------------------------------------------
# OPTION: -padx
#
# Pad the x space between the button box frame and the hull.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Buttonbox::padx {
    _setBoxSize
}

# ------------------------------------------------------------------
# OPTION: -orient
#
# Position buttons either horizontally or vertically.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Buttonbox::orient {
    switch $itk_option(-orient) {
	"horizontal" -
	"vertical" {
	    _setBoxSize
	}
	
	default {
	    error "bad orientation option \"$itk_option(-orient)\",\
		    should be either horizontal or vertical"
	}
    }
}

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

# ------------------------------------------------------------------
# METHOD: index index
#
# Searches the buttons in the box for the one with the requested tag,
# numerical index, keyword "end" or "default".  Returns the button's 
# tag if found, otherwise error.
# ------------------------------------------------------------------    
itcl::body iwidgets::Buttonbox::index {index} {
    if {[llength $_buttonList] > 0} {
	if {[regexp {(^[0-9]+$)} $index]} {
	    if {$index < [llength $_buttonList]} {
		return $index
	    } else {
		error "Buttonbox index \"$index\" is out of range"
	    }
	    
	} elseif {$index == "end"} {
	    return [expr {[llength $_buttonList] - 1}]
	    
	} elseif {$index == "default"} {
	    foreach knownButton $_buttonList {
		if {[$itk_component($knownButton) cget -defaultring]} {
		    return [lsearch -exact $_buttonList $knownButton]
		}
	    }
	    
	    error "Buttonbox \"$itk_component(hull)\" has no default"
	    
	} else {
	    if {[set idx [lsearch $_buttonList $index]] != -1} {
		return $idx
	    }
	    
	    error "bad Buttonbox index \"$index\": must be number, end,\
		    default, or pattern"
	}
	
    } else {
	error "Buttonbox \"$itk_component(hull)\" has no buttons"
    }
}

# ------------------------------------------------------------------
# METHOD: add tag ?option value option value ...?
#
# Add the specified button to the button box.  All PushButton options
# are allowed.  New buttons are added to the list of buttons and the 
# list of displayed buttons.  The PushButton path name is returned.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::add {tag args} {
    itk_component add $tag {
	iwidgets::Pushbutton $itk_component(hull).[incr _unique]
    } {
	usual
	rename -highlightbackground -background background Background
    }
    
    if {$args != ""} {
	uplevel $itk_component($tag) configure $args
    }
    
    lappend _buttonList $tag
    lappend _displayList $tag
    
    _setBoxSize
}

# ------------------------------------------------------------------
# METHOD: insert index tag ?option value option value ...?
#
# Insert the specified button in the button box just before the one 
# given by index.  All PushButton options are allowed.  New buttons 
# are added to the list of buttons and the list of displayed buttons.
# The PushButton path name is returned.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::insert {index tag args} {
    itk_component add $tag {
	iwidgets::Pushbutton $itk_component(hull).[incr _unique]
    } {
	usual
	rename -highlightbackground -background background Background
    }
    
    if {$args != ""} {
	uplevel $itk_component($tag) configure $args
    }
    
    set index [index $index]
    set _buttonList [linsert $_buttonList $index $tag]
    set _displayList [linsert $_displayList $index $tag]
    
    _setBoxSize
}

# ------------------------------------------------------------------
# METHOD: delete index
#
# Delete the specified button from the button box.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::delete {index} {
    set index [index $index]
    set tag [lindex $_buttonList $index]
    
    destroy $itk_component($tag)
    
    set _buttonList [lreplace $_buttonList $index $index]
    
    if {[set dind [lsearch $_displayList $tag]] != -1} {
	set _displayList [lreplace $_displayList $dind $dind]
    }
    
    _setBoxSize
    update idletasks
}

# ------------------------------------------------------------------
# METHOD: default index
#
# Sets the default to the push button given by index.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::default {index} {
    set index [index $index]
    
    set defbtn [lindex $_buttonList $index]
    
    foreach knownButton $_displayList {
	if {$knownButton == $defbtn} {
	    $itk_component($knownButton) configure -defaultring yes
	} else {
	    $itk_component($knownButton) configure -defaultring no
	}
    }
}

# ------------------------------------------------------------------
# METHOD: hide index
#
# Hide the push button given by index.  This doesn't remove the button 
# permanently from the display list, just inhibits its display.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::hide {index} {
    set index [index $index]
    set tag [lindex $_buttonList $index]
    
    if {[set dind [lsearch $_displayList $tag]] != -1} {
	place forget $itk_component($tag)
	set _displayList [lreplace $_displayList $dind $dind] 
	
	_setBoxSize
    }
}

# ------------------------------------------------------------------
# METHOD: show index
#
# Displays a previously hidden push button given by index.  Check if 
# the button is already in the display list.  If not then add it back 
# at it's original location and redisplay.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::show {index} {
    set index [index $index]
    set tag [lindex $_buttonList $index]
    
    if {[lsearch $_displayList $tag] == -1} {
	set _displayList [linsert $_displayList $index $tag]
	
	_setBoxSize
    }
}

# ------------------------------------------------------------------
# METHOD: invoke ?index?
#
# Invoke the command associated with a push button.  If no arguments
# are given then the default button is invoked, otherwise the argument
# is expected to be a button index.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::invoke {args} {
    if {[llength $args] == 0} {
	$itk_component([lindex $_buttonList [index default]]) invoke
	
    } else {
	$itk_component([lindex $_buttonList [index [lindex $args 0]]]) \
		invoke
    }
}

# ------------------------------------------------------------------
# METHOD: buttonconfigure index ?option? ?value option value ...?
#
# Configure a push button given by index.  This method allows 
# configuration of pushbuttons from the Buttonbox level.  The options
# may have any of the values accepted by the add method.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::buttonconfigure {index args} {
    set tag [lindex $_buttonList [index $index]]
    
    set retstr [uplevel $itk_component($tag) configure $args]
    
    _setBoxSize
    
    return $retstr
}

# ------------------------------------------------------------------
# METHOD: buttonccget index option
#
# Return value of option for push button given by index.  Option may
# have any of the values accepted by the add method.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::buttoncget {index option} {
    set tag [lindex $_buttonList [index $index]]
    
    set retstr [uplevel $itk_component($tag) cget [list $option]]
    
    return $retstr
}

# -----------------------------------------------------------------
# PRIVATE METHOD: _getMaxWidth
#
# Returns the required width of the largest button.
# -----------------------------------------------------------------
itcl::body iwidgets::Buttonbox::_getMaxWidth {} {
    set max 0
    
    foreach tag $_displayList {
	set w [winfo reqwidth $itk_component($tag)]
	
	if {$w > $max} {
	    set max $w
	}
    }
    
    return $max
}

# -----------------------------------------------------------------
# PRIVATE METHOD: _getMaxHeight
#
# Returns the required height of the largest button.
# -----------------------------------------------------------------
itcl::body iwidgets::Buttonbox::_getMaxHeight {} {
    set max 0
    
    foreach tag $_displayList {
	set h [winfo reqheight $itk_component($tag)]
	
	if {$h > $max} {
	    set max $h
	}
    }
    
    return $max
}

# ------------------------------------------------------------------
# METHOD: _setBoxSize ?when?
#
# Sets the proper size of the frame surrounding all the buttons.
# If "when" is "now", the change is applied immediately.  If it is 
# "later" or it is not specified, then the change is applied later, 
# when the application is idle.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::_setBoxSize {{when later}} {
    if {[winfo ismapped $itk_component(hull)]} {
	if {$when == "later"} {
	    if {$_resizeFlag == ""} {
		set _resizeFlag [after idle [itcl::code $this _setBoxSize now]]
	    }
	    return
	} elseif {$when != "now"} {
	    error "bad option \"$when\": should be now or later"
	}

	set _resizeFlag ""

	set numBtns [llength $_displayList]
	
	if {$itk_option(-orient) == "horizontal"} {
	    set minw [expr {$numBtns * [_getMaxWidth] \
		    + ($numBtns+1) * $itk_option(-padx)}]
	    set minh [expr {[_getMaxHeight] + 2 * $itk_option(-pady)}]
	    
	} else {
	    set minw [expr {[_getMaxWidth] + 2 * $itk_option(-padx)}]
	    set minh [expr {$numBtns * [_getMaxHeight] \
		    + ($numBtns+1) * $itk_option(-pady)}]
	}
	
	#
	# Remove the configure event bindings on the hull while we adjust the
	# width/height and re-position the buttons.  Once we're through, we'll
	# update and reinstall them.  This prevents double calls to position
	# the buttons.
	#
	set tags [bindtags $itk_component(hull)]
	if {[set i [lsearch $tags bbox-config]] != -1} {
	    set tags [lreplace $tags $i $i]
	    bindtags $itk_component(hull) $tags
	}
	
	component hull configure -width $minw -height $minh
	
	update idletasks
	    
	_positionButtons
	
	bindtags $itk_component(hull) [linsert $tags 0 bbox-config]
    }
}
    
# ------------------------------------------------------------------
# METHOD: _positionButtons
# 
# This method is responsible setting the width/height of all the 
# displayed buttons to the same value and for placing all the buttons
# in equidistant locations.
# ------------------------------------------------------------------
itcl::body iwidgets::Buttonbox::_positionButtons {} {
    set bf $itk_component(hull)
    set numBtns [llength $_displayList]
    
    # 
    # First, determine the common width and height for all the 
    # displayed buttons.
    #
    if {$numBtns > 0} {
	set bfWidth [winfo width $itk_component(hull)]
	set bfHeight [winfo height $itk_component(hull)]
	
	if {$bfWidth >= [winfo reqwidth $itk_component(hull)]} {
	    set _btnWidth [_getMaxWidth] 
	    
	} else {
	    if {$itk_option(-orient) == "horizontal"} {
		set _btnWidth [expr {$bfWidth / $numBtns}]
	    } else {
		set _btnWidth $bfWidth
	    }
	}	    
	
	if {$bfHeight >= [winfo reqheight $itk_component(hull)]} {
	    set _btnHeight [_getMaxHeight]
	    
	} else {
	    if {$itk_option(-orient) == "vertical"} {
		set _btnHeight [expr {$bfHeight / $numBtns}]
	    } else {
		set _btnHeight $bfHeight
	    }
	}	    
    }
    
    #
    # Place the buttons at the proper locations.
    #
    if {$numBtns > 0} {
	if {$itk_option(-orient) == "horizontal"} {
	    set leftover [expr {[winfo width $bf] \
		    - 2 * $itk_option(-padx) - $_btnWidth * $numBtns}]
	    
	    if {$numBtns > 0} {
		set offset [expr {$leftover / ($numBtns + 1)}]
	    } else {
		set offset 0
	    }
	    if {$offset < 0} {set offset 0}
	    
	    set xDist [expr {$itk_option(-padx) + $offset}]
	    set incrAmount [expr {$_btnWidth + $offset}]
	    
	    foreach button $_displayList {
		place $itk_component($button) -anchor w \
			-x $xDist -rely .5 -y 0 -relx 0 \
			-width $_btnWidth -height $_btnHeight
		
		set xDist [expr {$xDist + $incrAmount}]
	    }
	    
	} else {
	    set leftover [expr {[winfo height $bf] \
		    - 2 * $itk_option(-pady) - $_btnHeight * $numBtns}]
	    
	    if {$numBtns > 0} {
		set offset [expr {$leftover / ($numBtns + 1)}]
	    } else {
		set offset 0
	    }
	    if {$offset < 0} {set offset 0}
	    
	    set yDist [expr {$itk_option(-pady) + $offset}]
	    set incrAmount [expr {$_btnHeight + $offset}]
	    
	    foreach button $_displayList {
		place $itk_component($button) -anchor n \
			-y $yDist -relx .5 -x 0 -rely 0 \
			-width $_btnWidth -height $_btnHeight
		
		set yDist [expr {$yDist + $incrAmount}]
	    }
	}
    }
}