Tk Source Code

Artifact [381a74f7]
Login

Artifact 381a74f70d06a1c1c1301ae76310c5e071130665:

Attachment "auto-accel-r1.patch" to ticket [563140ff] added by a_kovalenko 2002-06-07 05:25:29.
diff -Nau tk-ia/library/accel.tcl tk/library/accel.tcl
--- tk-ia/library/accel.tcl	Thu Jan  1 03:00:00 1970
+++ tk/library/accel.tcl	Thu Jun  6 17:24:43 2002
@@ -0,0 +1,133 @@
+# accel.tcl --
+#
+# This file provides unified way to select appropriate key accelerators
+# for widgets with underlined labels. This is nessesary, because some
+# problems were introduced in tk message boxes & standard dialogs
+# with labels translated via msgcat.
+# 
+# RCS: @(#) $Id$
+#
+# I hope this functionality will eventually be exported to application
+# programmers. 
+#----------------------------------------------------------------------
+# Usage:
+#
+# tk::accel::create {name} -- starts definition of new named 
+# group of key accelerators. It's convenient to use toplevel's name
+# for the group of accelerators that it uses.
+#
+# tk::accel::underline {name} {widget} ?action? -- underline one character in 
+# widget's label. In some cases, you can specify the action which
+# will be taken if appropriate accelerator key is pressed.
+# If the widget is a button, the action is "$widget invoke" by default.
+# If the widget is a menubutton, the action is "tk::MbPost $widget".
+#
+# tk::accel::delete {name} -- destroys accelerators group. Meant to be
+# called when associated toplevel is destroyed.
+#
+# tk::accel::select {name} {string} ?action? -- selects "best" character
+# to underline in $string, and associates action with appropriate 
+# accelerator key. Returns underline position in string.
+# If action is empty, only searching for previously defined underline
+# position is performed.
+#
+# tk::accel::dispatch {name} {key} -- event handler for <Alt-Key>,
+# which executes appropriate action for the key. The second argument
+# is supposed to be %A
+#
+# tk::accel::bind {name} ?widget? -- bind accel::dispatch to <Alt-Key>
+# for widget (or bindtag). if widget is uspecified, it's assumed to
+# be $name, too.
+
+namespace eval ::tk::accel {
+    proc create {name} {
+	variable groups
+	if {![info exists groups($name)]} {
+	    set groups($name) {}
+	}
+    }
+
+    proc select {name str {action {}}} {
+	variable groups
+	if {[info exists groups($name,for:$str)]} {
+	    return $groups($name,for:$str)
+	} else {
+	    if {$action == ""} {
+		return -1
+	    }
+	}
+	set ulinepos -1
+	set foundchar "-"
+	set founduline -1
+	foreach char [split $str {}] {
+	    incr ulinepos
+	    if {-1!=[string first [string toupper $char] $groups($name)]} {
+		# This binding is not unique -- skipping
+		continue
+	    }
+	    if {![string is alnum $char]} {
+		# Consider only alphanumeric accelerators
+		continue
+	    }
+	    if {[string is ascii $char] && [string is upper $char ]} {
+		# Upper & ascii char is always the BEST candidate.
+		set foundchar $char
+		set founduline $ulinepos
+		break
+	    }
+	    if {[string is upper $char]} {
+		if {![string is upper $foundchar]} {
+		    # Uppercase is better than lowercase
+		    set foundchar $char
+		    set founduline $ulinepos
+		}
+	    } else {
+		if {$founduline==-1} {
+		    # lowercase is better than nothing
+		    set foundchar $char
+		    set founduline $ulinepos
+		}
+	    }
+	    
+	}
+	set groups($name,for:$str) $founduline
+	if {$founduline != -1} {
+	    set groups($name,[string toupper $foundchar]) $action
+	    append groups($name) [string toupper $foundchar]
+	}
+	return $founduline
+    }
+
+    proc dispatch {name key} {
+	set char [string toupper $key]
+	variable groups
+	if {[info exists groups($name,$char)]} {
+	    uplevel \#0 $groups($name,$char)
+	}
+    }
+    
+    proc underline {name widget {action {}}} {
+	if {$action == ""} {
+	    switch [winfo class $widget] {
+		Button {set action [list $widget invoke] }
+		Menubutton {set action [list ::tk::MbPost $widget] }
+	    }
+	}
+	$widget configure -underline \
+	    [ select $name [$widget cget -text] $action]
+    }
+    proc delete {name} {
+	variable groups
+	array unset groups $name,* 
+	array unset groups $name 
+    }
+    proc bind {name {widget {}}} {
+	if {$widget == ""} {
+	    set widget $name
+	}
+	::bind $widget <Alt-Key> [list tk::accel::dispatch $name %A]
+    }
+}
+
+
+
diff -Nau tk-ia/library/clrpick.tcl tk/library/clrpick.tcl
--- tk-ia/library/clrpick.tcl	Tue Jun  4 05:46:00 2002
+++ tk/library/clrpick.tcl	Thu Jun  6 18:03:24 2002
@@ -197,6 +197,7 @@
 #
 proc ::tk::dialog::color::BuildDialog {w} {
     upvar ::tk::dialog::color::[winfo name $w] data
+    ::tk::accel::create $w
 
     # TopFrame contains the color strips and the color selection
     #
@@ -219,7 +220,8 @@
 	# The box frame contains the label and entry widget for an [R|G|B]
 	set box [frame $f.box]
 
-	label $box.label -text $l: -width $maxWidth -under 0 -anchor ne
+	label $box.label -text $l: -width $maxWidth -anchor ne
+	::tk::accel::underline $w $box.label [list focus $box.entry]
 	entry $box.entry -textvariable \
 		::tk::dialog::color::[winfo name $w]($color,intensity) \
 		-width 4
@@ -266,10 +268,11 @@
     #
     set selFrame [frame $topFrame.sel]
     set lab [label $selFrame.lab -text [mc "Selection:"] \
-	    -under 0 -anchor sw]
+	    -anchor sw]
     set ent [entry $selFrame.ent \
 	-textvariable ::tk::dialog::color::[winfo name $w](selection) \
 	-width 16]
+    ::tk::accel::underline $w $lab [list focus $ent]
     set f1  [frame $selFrame.f1 -relief sunken -bd 2]
     set data(finalCanvas) [frame $f1.demo -bd 0 -width 100 -height 70]
 
@@ -288,12 +291,14 @@
     set maxWidth [mcmax OK Cancel]
     set maxWidth [expr {$maxWidth<8?8:$maxWidth}]
     button $botFrame.ok     -text [mc "OK"]		\
-	    -width $maxWidth -under 0				\
+	    -width $maxWidth \
 	    -command [list tk::dialog::color::OkCmd $w]
     button $botFrame.cancel -text [mc "Cancel"]	\
-	    -width $maxWidth -under 0				\
+	    -width $maxWidth \
 	    -command [list tk::dialog::color::CancelCmd $w]
-
+    ::tk::accel::underline $w $botFrame.ok
+    ::tk::accel::underline $w $botFrame.cancel
+    
     set data(okBtn)      $botFrame.ok
     set data(cancelBtn)  $botFrame.cancel
  
@@ -303,14 +308,8 @@
 
 
     # Accelerator bindings
-
-    bind $w <Alt-r> [list focus $data(red,entry)]
-    bind $w <Alt-g> [list focus $data(green,entry)]
-    bind $w <Alt-b> [list focus $data(blue,entry)]
-    bind $w <Alt-s> [list focus $ent]
+    ::tk::accel::bind $w
     bind $w <KeyPress-Escape> [list tk::ButtonInvoke $data(cancelBtn)]
-    bind $w <Alt-c> [list tk::ButtonInvoke $data(cancelBtn)]
-    bind $w <Alt-o> [list tk::ButtonInvoke $data(okBtn)]
 
     wm protocol $w WM_DELETE_WINDOW [list tk::dialog::color::CancelCmd $w]
 }
Common subdirectories: tk-ia/library/demos and tk/library/demos
Common subdirectories: tk-ia/library/images and tk/library/images
diff -Nau tk-ia/library/msgbox.tcl tk/library/msgbox.tcl
--- tk-ia/library/msgbox.tcl	Tue Jun  4 05:46:01 2002
+++ tk/library/msgbox.tcl	Thu Jun  6 16:40:09 2002
@@ -3,7 +3,7 @@
 #	Implements messageboxes for platforms that do not have native
 #	messagebox support.
 #
-# RCS: @(#) $Id: msgbox.tcl,v 1.16 2002/04/29 13:17:44 bagnonm Exp $
+# RCS: @(#) $Id: msgbox.tcl,v 1.17 2002/06/06 01:09:41 a_kovalenko Exp $
 #
 # Copyright (c) 1994-1997 Sun Microsystems, Inc.
 #
@@ -268,7 +268,10 @@
     # 3. Create the top-level window and divide it into top
     # and bottom parts.
 
-    catch {destroy $w}
+    catch {
+	destroy $w
+	::tk::accel::delete $w
+    }
     toplevel $w -class Dialog
     wm title $w $data(-title)
     wm iconname $w Dialog
@@ -353,8 +356,11 @@
     grid columnconfigure $w.top 1 -weight 1
     grid rowconfigure $w.top 0 -weight 1
 
-    # 5. Create a row of buttons at the bottom of the dialog.
+    # Define a group of accelerator keys. See accel.tcl for 
+    # brief documentation about ::tk::accel::* commands.
+    ::tk::accel::create $w
 
+    # 5. Create a row of buttons at the bottom of the dialog.
     set i 0
     foreach but $buttons {
 	set name [lindex $but 0]
@@ -369,19 +375,17 @@
 
 	if {[string equal $name $data(-default)]} {
 	    $w.$name configure -default active
+	} else {
+	    $w.$name configure -default normal
 	}
 	pack $w.$name -in $w.bot -side left -expand 1 -padx 3m -pady 2m
 
-	# create the binding for the key accelerator, based on the underline
-	#
-	set underIdx [$w.$name cget -under]
-	if {$underIdx >= 0} {
-	    set key [string index [$w.$name cget -text] $underIdx]
-	    bind $w <Alt-[string tolower $key]>  [list $w.$name invoke]
-	    bind $w <Alt-[string toupper $key]>  [list $w.$name invoke]
-	}
+	# create the binding for the key accelerator
+	::tk::accel::underline $w $w.$name
 	incr i
     }
+    # Let $w handle key accelerators.
+    ::tk::accel::bind $w
 
     if {[string compare {} $data(-default)]} {
 	bind $w <FocusIn> {
Common subdirectories: tk-ia/library/msgs and tk/library/msgs
diff -Nau tk-ia/library/tclIndex tk/library/tclIndex
--- tk-ia/library/tclIndex	Tue Jun  4 05:46:01 2002
+++ tk/library/tclIndex	Thu Jun  6 16:36:43 2002
@@ -6,6 +6,12 @@
 # element name is the name of a command and the value is
 # a script that loads the command.
 
+set auto_index(::tk::accel::create) [list source [file join $dir accel.tcl]]
+set auto_index(::tk::accel::delete) [list source [file join $dir accel.tcl]]
+set auto_index(::tk::accel::underline) [list source [file join $dir accel.tcl]]
+set auto_index(::tk::accel::dispatch) [list source [file join $dir accel.tcl]]
+set auto_index(::tk::accel::select) [list source [file join $dir accel.tcl]]
+set auto_index(::tk::accel::bind) [list source [file join $dir accel.tcl]]
 set auto_index(::tk::dialog::error::Return) [list source [file join $dir bgerror.tcl]]
 set auto_index(::tk::dialog::error::Details) [list source [file join $dir bgerror.tcl]]
 set auto_index(::tk::dialog::error::SaveToLog) [list source [file join $dir bgerror.tcl]]
diff -Nau tk-ia/library/tkfbox.tcl tk/library/tkfbox.tcl
--- tk-ia/library/tkfbox.tcl	Tue Jun  4 05:46:00 2002
+++ tk/library/tkfbox.tcl	Thu Jun  6 17:31:00 2002
@@ -975,6 +975,7 @@
     global tk_library
 
     toplevel $w -class $class
+    tk::accel::create $w
 
     # f1: the frame with the directory option menu
     #
@@ -1032,9 +1033,9 @@
     #
     set f2 [frame $w.f2 -bd 0]
     label $f2.lab -text $fNameCaption -anchor e -width $fCaptionWidth \
-	    -under $fNameUnder -pady 0
+	    -pady 0
     set data(ent) [entry $f2.ent]
-
+   
     # The font to use for the icons. The default Canvas font on Unix
     # is just deviant.
     set ::tk::$w.icons(font) [$data(ent) cget -font]
@@ -1052,7 +1053,7 @@
 	# bindtags)
 	
 	set data(typeMenuLab) [button $f3.lab -text $fTypeCaption \
-		-anchor e -width $fCaptionWidth -under 9 \
+		-anchor e -width $fCaptionWidth \
 		-bd [$f2.lab cget -bd] \
 		-highlightthickness [$f2.lab cget -highlightthickness] \
 		-relief [$f2.lab cget -relief] \
@@ -1060,9 +1061,9 @@
 		-pady [$f2.lab cget -pady]]
 	bindtags $data(typeMenuLab) [list $data(typeMenuLab) Label \
 		[winfo toplevel $data(typeMenuLab)] all]
-	
 	set data(typeMenuBtn) [menubutton $f3.menu -indicatoron 1 \
 		-menu $f3.menu.m]
+	::tk::accel::underline $w $data(typeMenuLab) [list focus $data(typeMenuBtn)]
 	set data(typeMenu) [menu $data(typeMenuBtn).m -tearoff 0]
 	$data(typeMenuBtn) config -takefocus 1 -highlightthickness 2 \
 		-relief raised -bd 2 -anchor w
@@ -1103,8 +1104,9 @@
     $data(upBtn)     config -command [list ::tk::dialog::file::UpDirCmd $w]
     $data(cancelBtn) config -command [list ::tk::dialog::file::CancelCmd $w]
     bind $w <KeyPress-Escape> [list tk::ButtonInvoke $data(cancelBtn)]
-    bind $w <Alt-c> [list tk::ButtonInvoke $data(cancelBtn)]
-    bind $w <Alt-d> [list focus $data(dirMenuBtn)]
+    ::tk::accel::underline $w $data(cancelBtn) [list tk::ButtonInvoke $data(cancelBtn)] 
+    ::tk::accel::underline $w $f1.lab [list focus $data(dirMenuBtn)] 
+    ::tk::accel::underline $w $f2.lab [list focus $data(ent)]
 
     # Set up event handlers specific to File or Directory Dialogs
     #
@@ -1112,20 +1114,19 @@
     if { [string equal $class TkFDialog] } {
 	bind $data(ent) <Return> [list ::tk::dialog::file::ActivateEnt $w]
 	$data(okBtn)     config -command [list ::tk::dialog::file::OkCmd $w]
-	bind $w <Alt-t> [format {
+
+	::tk::accel::underline $w $data(typeMenuBtn) [format {
 	    if {[string equal [%s cget -state] "normal"]} {
 		focus %s
 	    }
 	} $data(typeMenuBtn) $data(typeMenuBtn)]
-	bind $w <Alt-n> [list focus $data(ent)]
-	bind $w <Alt-o> [list ::tk::dialog::file::InvokeBtn $w Open]
-	bind $w <Alt-s> [list ::tk::dialog::file::InvokeBtn $w Save]
+	::tk::accel::select $w [mc Open] [list ::tk::dialog::file::InvokeBtn $w Open]
+	::tk::accel::select $w [mc Save] [list ::tk::dialog::file::InvokeBtn $w Save]
     } else {
 	set okCmd [list ::tk::dialog::file::chooseDir::OkCmd $w]
 	bind $data(ent) <Return> $okCmd
 	$data(okBtn) config -command $okCmd
-	bind $w <Alt-s> [list focus $data(ent)]
-	bind $w <Alt-o> [list tk::ButtonInvoke $data(okBtn)]
+	::tk::accel::underline $w $data(okBtn) [list tk::ButtonInvoke $data(okBtn)]
     }
 
     # Build the focus group for all the entries
@@ -1133,6 +1134,7 @@
     ::tk::FocusGroup_Create $w
     ::tk::FocusGroup_BindIn $w  $data(ent) [list ::tk::dialog::file::EntFocusIn $w]
     ::tk::FocusGroup_BindOut $w $data(ent) [list ::tk::dialog::file::EntFocusOut $w]
+    ::tk::accel::bind $w
 }
 
 # ::tk::dialog::file::SetSelectMode --
@@ -1156,7 +1158,7 @@
     }
     set fNameUnder 5
     set iconListCommand [list ::tk::dialog::file::OkCmd $w]
-    $w.f2.lab configure -text $fNameCaption -under $fNameUnder
+    $w.f2.lab configure -text $fNameCaption 
     ::tk::IconList_Config $data(icons) \
 	    [list -multiple $multi -command $iconListCommand]
     return
@@ -1307,15 +1309,17 @@
 	# Restore the Open/Save Button if this is a File Dialog
 	#
 	if {[string equal $data(type) open]} {
-	    $data(okBtn) config -text "[mc "Open"]"
+	    $data(okBtn) config -text "[mc "Open"]" -underline \
+	    [::tk::accel::select $w [mc Open] {}]
 		set maxWidth [string length [mc "Open"]]
 		if {$maxWidth>[$data(okBtn) cget -width]} {
 			$data(okBtn) config -width $maxWidth
 			$data(cancelBtn) config -width $maxWidth
 		}
 	} else {
-	    $data(okBtn) config -text "[mc "Save"]"
-		set maxWidth [string length [mc "Save"]]
+	    $data(okBtn) config -text "[mc "Save"]" -underline \
+	    [::tk::accel::select $w [mc Save] {}]
+	    set maxWidth [string length [mc "Save"]]
 		if {$maxWidth>[$data(okBtn) cget -width]} {
 			$data(okBtn) config -width $maxWidth
 			$data(cancelBtn) config -width $maxWidth
@@ -1508,9 +1512,11 @@
     if { [string equal [winfo class $w] TkFDialog] } {
 	# If this is a File Dialog, make sure the buttons are labeled right.
 	if {[string equal $data(type) open]} {
-	    $data(okBtn) config -text "[mc "Open"]"
+	    $data(okBtn) config -text "[mc "Open"]" -underline \
+		[tk::accel::select $w [mc Open] {}]
 	} else {
-	    $data(okBtn) config -text "[mc "Save"]"
+	    $data(okBtn) config -text "[mc "Save"]" -underline \
+		[tk::accel::select $w [mc Save] {}]
 	}
     }
 }
@@ -1618,7 +1624,7 @@
 proc ::tk::dialog::file::InvokeBtn {w key} {
     upvar ::tk::dialog::file::[winfo name $w] data
 
-    if {[string equal [$data(okBtn) cget -text] $key]} {
+    if {[string equal [$data(okBtn) cget -text] [mc $key]]} {
 	::tk::ButtonInvoke $data(okBtn)
     }
 }
@@ -1710,14 +1716,18 @@
 
 	if { [string equal [winfo class $w] TkFDialog] } {
 	    if {[string equal $data(type) open]} {
-		$data(okBtn) config -text "[mc "Open"]"
+		$data(okBtn) config -text "[mc "Open"]" -underline \
+		    [::tk::accel::select $w [mc Open] {}]
+		
 	    } else {
-		$data(okBtn) config -text "[mc "Save"]"
+		$data(okBtn) config -text "[mc "Save"]" -underline \
+		    [::tk::accel::select $w [mc Save] {}]
 	    }
 	}
     } else {
 	if { [string equal [winfo class $w] TkFDialog] } {
-	    $data(okBtn) config -text "[mc "Open"]"
+	    $data(okBtn) config -text "[mc "Open"]" -underline \
+		    [::tk::accel::select $w [mc Open] {}]
 	}
     }
 }
diff -Nau tk-ia/library/xmfbox.tcl tk/library/xmfbox.tcl
--- tk-ia/library/xmfbox.tcl	Tue Jun  4 05:46:00 2002
+++ tk/library/xmfbox.tcl	Thu Jun  6 17:59:19 2002
@@ -95,6 +95,7 @@
 	MotifFDialog_BuildUI $w
     } elseif {[string compare [winfo class $w] TkMotifFDialog]} {
 	destroy $w
+	::tk::accel::delete $w
 	MotifFDialog_BuildUI $w
     } else {
 	set data(fEnt) $w.top.f1.ent
@@ -292,6 +293,7 @@
 proc ::tk::MotifFDialog_BuildUI {w} {
     set dataName [lindex [split $w .] end]
     upvar ::tk::dialog::file::$dataName data
+    ::tk::accel::create $w
 
     # Create the dialog toplevel and internal frames.
     #
@@ -323,7 +325,8 @@
 
     # The Filter box
     #
-    label $f1.lab -text [mc "Filter:"] -under 3 -anchor w
+    label $f1.lab -text [mc "Filter:"] -anchor w
+    ::tk::accel::underline $w $f1.lab [list focus $f1.ent]
     entry $f1.ent
     pack $f1.lab -side top -fill x -padx 6 -pady 4
     pack $f1.ent -side top -fill x -padx 4 -pady 0
@@ -338,7 +341,8 @@
 
     # The Selection box
     #
-    label $f3.lab -text [mc "Selection:"] -under 0 -anchor w
+    label $f3.lab -text [mc "Selection:"] -anchor w
+    ::tk::accel::underline $w $f3.lab [list focus $f3.ent]
     entry $f3.ent
     pack $f3.lab -side top -fill x -padx 6 -pady 0
     pack $f3.ent -side top -fill x -padx 4 -pady 4
@@ -349,28 +353,24 @@
     set maxWidth [mcmax OK Filter Cancel]
     set maxWidth [expr {$maxWidth<6?6:$maxWidth}]
     set data(okBtn) [button $bot.ok -text [mc "OK"] \
-	    -width $maxWidth -under 0 \
+	    -width $maxWidth \
 	    -command [list tk::MotifFDialog_OkCmd $w]]
     set data(filterBtn) [button $bot.filter -text [mc "Filter"] \
-	    -width $maxWidth -under 0 \
+	    -width $maxWidth \
 	    -command [list tk::MotifFDialog_FilterCmd $w]]
     set data(cancelBtn) [button $bot.cancel -text [mc "Cancel"] \
-	    -width $maxWidth -under 0 \
+	    -width $maxWidth \
 	    -command [list tk::MotifFDialog_CancelCmd $w]]
-
+    ::tk::accel::underline $w $data(okBtn)
+    ::tk::accel::underline $w $data(filterBtn)
+    ::tk::accel::underline $w $data(cancelBtn)
+    
     pack $bot.ok $bot.filter $bot.cancel -padx 10 -pady 10 -expand yes \
 	-side left
 
     # Create the bindings:
     #
-    bind $w <Alt-t> [list focus $data(fEnt)]
-    bind $w <Alt-d> [list focus $data(dList)]
-    bind $w <Alt-l> [list focus $data(fList)]
-    bind $w <Alt-s> [list focus $data(sEnt)]
-
-    bind $w <Alt-o> [list tk::ButtonInvoke $bot.ok]
-    bind $w <Alt-f> [list tk::ButtonInvoke $bot.filter]
-    bind $w <Alt-c> [list tk::ButtonInvoke $bot.cancel]
+    ::tk::accel::bind $w
 
     bind $data(fEnt) <Return> [list tk::MotifFDialog_ActivateFEnt $w]
     bind $data(sEnt) <Return> [list tk::MotifFDialog_ActivateSEnt $w]
@@ -406,7 +406,8 @@
 #			browsed or activated.
 
 proc ::tk::MotifFDialog_MakeSList {w f label under cmdPrefix} {
-    label $f.lab -text $label -under $under -anchor w
+    label $f.lab -text $label -anchor w
+    ::tk::accel::underline $w $f.lab [list focus $f.l]
     listbox $f.l -width 12 -height 5 -exportselection 0\
 	-xscrollcommand [list $f.h set]	-yscrollcommand [list $f.v set]
     scrollbar $f.v -orient vertical   -takefocus 0 -command [list $f.l yview]