WJG (06/06/06) Ah, 666, the number of the beast or the beastly BWidget ButtonBox? This stalwart member of the BWidget set is useful for making clusters of Buttons but what if you want other items such as everyone's favourite -ComboBox? Mmm, not really. So, here'a an alternative.
RH (07/06/06) There is already a nice Toolbar from
George Petasis. It doesn't rely on any widget extension.
WJG (09/06/06) Here's revised version of the earlier code (now deleted). The proc arguments remain the same but now toolbars can be detached into floating palettes. Close the palette and the toolbar returns to its orginal ordering in its container. I did look at GP's ToolBar, nice work. GP's package allows for the side and bottom packing of toolbars which I haven't allowed for. It's not particularly a feature that I would personally use, but seeing as it's
what's expected perhaps I'll have a go at it next.
package require BWidget
namespace eval ToolBar {
Widget::define ToolBar ToolBar
Widget::tkinclude ToolBar frame .f
Widget::declare ToolBar { }
}
proc ToolBar::create { path args } {
array set maps [list ToolBar {} :cmd {} .f {}]
array set maps [Widget::parseArgs ToolBar $args]
Widget::initFromODB ToolBar "$path" $maps(ToolBar)
set frame [eval frame $path [Widget::subcget $path :cmd] \
-class ToolBar -relief groove -bd 1 -highlightthickness 0]
set fr [eval frame $frame.f $maps(.f)]
pack $fr -fill both -expand true
set p .pal_[string trimleft $path .]
toplevel $p
wm withdraw $p
wm attributes $p -tool 1
wm title $p {}
wm transient $p .
wm resizable $p 0 0
pack [frame $p.fr -relief groove -borderwidth 0] -side left -anchor nw
pack [frame $fr.rule -bg #9999ff -relief raised -borderwidth 1 -width 3] -side left -anchor nw -fill y
bind $fr.rule <Button-1> [ list ToolBar::rule_cmd $path $p %X %Y ]
return [Widget::create ToolBar $path]
}
proc ToolBar::palette_cmd {a b c} {
set opts "-side left -anchor nw"
set slave_list [pack slaves $c] ;
set slave_list [lsort [lappend slave_list $a]]
eval pack $slave_list -in $c $opts
wm withdraw $b
}
proc ToolBar::rule_cmd {b p x y} {
wm geometry $p =+$x+$y
wm deiconify $p
set c [lindex [pack info $b] 1]
wm protocol $p WM_DELETE_WINDOW [list ToolBar::palette_cmd $b $p $c]
pack forget $b
}
proc ToolBar::configure { path args } {
set res [Widget::configure $path $args]
return $res
}
proc ToolBar::cget { path option } {
return [Widget::cget $path $option]
}
proc ToolBar::add { path class indx args} {
set p .pal_[string trimleft $path .]
if {[string tolower $class] == "rule"} {
frame $path.f.r$indx -width 2 -borderwidth 1 -relief groove
pack $path.f.r$indx -side left -anchor nw -fill y -padx 2
frame $p.r$indx -width 2 -borderwidth 1 -relief groove
pack $p.r$indx -side left -anchor nw -fill y -padx 2
return
}
set args1 {}
set args2 {}
set argsp1 {}
set argsp2 {}
foreach {arg val} $args {
switch -- $arg {
-help {
append args1 "DynamicHelp::add $path.f.$indx -help ballon -text \{$val\}"
append argsp1 "DynamicHelp::add $p.$indx -help ballon -text \{$val\}"
}
default {
append args2 " $arg \{$val\} "
append argsp2 " $arg \{$val\} "
}
}
}
eval "$class $path.f.$indx $args2"
eval "$class $p.$indx $argsp2"
eval $args1
pack $path.f.$indx -side left -padx 2
eval $argsp1
pack $p.$indx -side left -padx 2
return $path.f.$indx
}
proc ToolBar::demo {} {
catch {console show}
BWidget::place . 500 500 center
set base [frame .fr -relief groove -bd 2]
pack $base -side top -anchor nw -fill x
pack [text .txt] -side top -anchor nw -expand 1 -fill both
pack [ToolBar .tb1 ] -in $base -side left -padx 0 -anchor nw
foreach {i img hlp} {
1 New "New Document"
2 Open "Open New File"
3 Save "Save Work"
} {
.tb1 add button b$i \
-image [Bitmap::get $img] \
-relief flat -overrelief raised \
-help abc \
-command cmd$img
}
pack [ToolBar .tb2 ] -in $base -side left -padx 0 -anchor nw
foreach {i img hlp} {
1 Cut "Cut selection to clipboard"
2 Copy "Copy selection to clipboard"
3 Paste "Paste clipboard into selection"
} {
.tb2 add button b$i \
-image [Bitmap::get $img] \
-relief flat -overrelief raised \
-help abc \
-command cmd$img
}
pack [ToolBar .tb3 ] -in $base -side left -padx 0 -anchor nw
foreach {i img hlp} {
1 Bold "Embolden Selection"
2 Overstrike "Overstrike Selection"
3 Underline "Underline Selection"
} {
.tb3 add Button b$i \
-image [Bitmap::get $img] \
-relief link \
-help abc \
-command cmd$img
}
.tb3 add Rule 2
.tb3 add ComboBox cmb1 -values {How Now Brown Cow}
.tb3 add Rule 3
.tb3 add checkbutton cb1 -text test
}
ToolBar::demo