Index: combobox.tcl ================================================================== --- combobox.tcl +++ combobox.tcl @@ -1,9 +1,9 @@ # ------------------------------------------------------------------------------ # combobox.tcl # This file is part of Unifix BWidget Toolkit -# $Id: combobox.tcl,v 1.1.1.1 1999/08/03 20:20:23 ericm Exp $ +# $Id: combobox.tcl,v 1.2 1999/10/21 17:41:37 sven Exp $ # ------------------------------------------------------------------------------ # Index of commands: # - ComboBox::create # - ComboBox::configure # - ComboBox::cget @@ -55,10 +55,12 @@ # ------------------------------------------------------------------------------ # Command ComboBox::create # ------------------------------------------------------------------------------ proc ComboBox::create { path args } { + global tcl_platform + Widget::init ComboBox $path $args frame $path -background [Widget::getoption $path -background] \ -highlightthickness 0 -bd 0 -relief flat -takefocus 0 @@ -65,39 +67,53 @@ bindtags $path [list $path BwComboBox [winfo toplevel $path] all] set labf [eval LabelFrame::create $path.labf [Widget::subcget $path .labf] \ -focus $path.e] set entry [eval Entry::create $path.e [Widget::subcget $path .e] \ - -relief flat -borderwidth 0] - - set width 11 + -relief flat -borderwidth 0 -takefocus 1] + ::bind $path.e "$path _focus_in" + ::bind $path.e "$path _focus_out" + + if {![string compare $tcl_platform(platform) "unix"]} { + set ipadx 0 + set width 11 + } else { + set ipadx 2 + set width 15 + } set height [winfo reqheight $entry] set arrow [eval ArrowButton::create $path.a [Widget::subcget $path .a] \ -width $width -height $height \ -highlightthickness 0 -borderwidth 1 -takefocus 0 \ -dir bottom \ -type button \ + -ipadx $ipadx \ -command [list "ComboBox::_mapliste $path"]] - + + set frame [LabelFrame::getframe $labf] pack $arrow -in $frame -side right -fill y pack $entry -in $frame -side left -fill both -expand yes pack $labf -fill x -expand yes - if { [Widget::getoption $path -editable] == 0 } { - ::bind $entry "ArrowButton::invoke $path.a" + if { [Widget::getoption $path -editable] != 0 } { + ::bind $entry "ComboBox::_unmapliste $path" + $path.e config -state normal } else { - ::bind $entry "ComboBox::_unmapliste $path" + ::bind $entry "ArrowButton::invoke $path.a" + $path.e config -state disabled } ::bind $path "ComboBox::_unmapliste $path" - ::bind $entry "ComboBox::_modify_value $path previous" - ::bind $entry "ComboBox::_modify_value $path next" - ::bind $entry "ComboBox::_modify_value $path first" - ::bind $entry "ComboBox::_modify_value $path last" - + ::bind $entry "ComboBox::_unmapliste $path" + ::bind $entry "ComboBox::_mapliste $path" + ::bind $entry "ComboBox::_modify_value $path previous" + ::bind $entry "ComboBox::_modify_value $path next" + ::bind $entry "ComboBox::_modify_value $path first" + ::bind $entry "ComboBox::_modify_value $path last" + rename $path ::$path:cmd proc ::$path { cmd args } "return \[eval ComboBox::\$cmd $path \$args\]" return $path } @@ -116,12 +132,14 @@ } if { [Widget::hasChanged $path -editable ed] } { if { $ed } { ::bind $path.e "ComboBox::_unmapliste $path" + $path.e config -state normal } else { ::bind $path.e "ArrowButton::invoke $path.a" + $path.e config -state disabled } } return $res } @@ -277,39 +295,46 @@ if { ![llength [Widget::getoption $path -values]] } { return } _create_popup $path - ArrowButton::configure $path.a -dir top + ArrowButton::configure $path.a -relief sunken + update + $listb selection clear 0 end set values [$listb get 0 end] set curval [Entry::cget $path.e -text] if { [set idx [lsearch $values $curval]] != -1 || [set idx [lsearch $values "$curval*"]] != -1 } { $listb selection set $idx $listb activate $idx $listb see $idx } else { + $listb selection set 0 $listb activate 0 $listb see 0 } + ::bind $listb "ComboBox::_unmapliste $path; break" + set frame [LabelFrame::getframe $path.labf] BWidget::place $path.shell [winfo width $frame] 0 below $frame + focus -force $listb wm deiconify $path.shell raise $path.shell BWidget::grab global $path + ArrowButton::configure $path.a -relief raised } # ------------------------------------------------------------------------------ # Command ComboBox::_unmapliste # ------------------------------------------------------------------------------ proc ComboBox::_unmapliste { path } { BWidget::grab release $path + focus -force $path.e destroy $path.shell - ArrowButton::configure $path.a -dir bottom } # ------------------------------------------------------------------------------ # Command ComboBox::_select @@ -336,5 +361,52 @@ if { [set cmd [Widget::getoption $path -modifycmd]] != "" } { uplevel \#0 $cmd } } } + + +# ------------------------------------------------------------------------------ +# Command ComboBox::_focus_in +# ------------------------------------------------------------------------------ +proc ComboBox::_focus_in { path } { + variable background + variable foreground + + if { [Widget::getoption $path -editable] == 0 } { + set value [Entry::cget $path.e -text] + if {[string equal $value ""]} { + # If the entry is empty, we need to do some magic to + # make it "selected" + if {[$path.e cget -bg] != [$path.e cget -selectbackground]} { + # Copy only if we know that this is not the selection + # background color (by accident... focus out without + # focus in etc. + set background [$path.e cget -bg] + set foreground [$path.e cget -fg] + } + $path.e configure -bg [$path.e cget -selectbackground] + $path.e configure -fg [$path.e cget -selectforeground] + } + } + $path.e selection clear + $path.e selection range 0 end +} + + +# ------------------------------------------------------------------------------ +# Command ComboBox::_focus_out +# ------------------------------------------------------------------------------ +proc ComboBox::_focus_out { path } { + variable background + variable foreground + + if { [Widget::getoption $path -editable] == 0 } { + if {[info exists background]} { + $path.e configure -bg $background + $path.e configure -fg $foreground + unset background + unset foreground + } + } + $path.e selection clear +}