annotate Lib/idlelib/keybindingDialog.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents dc3d24824b60
children eab361617f6a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
1 """
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
2 Dialog for building Tkinter accelerator key bindings
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
3 """
47183
b53604fad54b Tkinter rename reversal: remove tkinter package, adapt imports and docs.
Georg Brandl <georg@python.org>
parents: 47101
diff changeset
4 from Tkinter import *
b53604fad54b Tkinter rename reversal: remove tkinter package, adapt imports and docs.
Georg Brandl <georg@python.org>
parents: 47101
diff changeset
5 import tkMessageBox
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 39113
diff changeset
6 import string
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
7
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
8 class GetKeysDialog(Toplevel):
21763
652dc51b5f83 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21762
diff changeset
9 def __init__(self,parent,title,action,currentKeySequences):
652dc51b5f83 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21762
diff changeset
10 """
652dc51b5f83 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21762
diff changeset
11 action - string, the name of the virtual event these keys will be
652dc51b5f83 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21762
diff changeset
12 mapped to
652dc51b5f83 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21762
diff changeset
13 currentKeys - list, a list of all key sequence lists currently mapped
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
14 to virtual events, for overlap checking
21763
652dc51b5f83 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21762
diff changeset
15 """
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
16 Toplevel.__init__(self, parent)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
17 self.configure(borderwidth=5)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
18 self.resizable(height=FALSE,width=FALSE)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
19 self.title(title)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
20 self.transient(parent)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
21 self.grab_set()
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
22 self.protocol("WM_DELETE_WINDOW", self.Cancel)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
23 self.parent = parent
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
24 self.action=action
21763
652dc51b5f83 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21762
diff changeset
25 self.currentKeySequences=currentKeySequences
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
26 self.result=''
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
27 self.keyString=StringVar(self)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
28 self.keyString.set('')
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
29 self.SetModifiersForPlatform() # set self.modifiers, self.modifier_label
25478
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
30 self.modifier_vars = []
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
31 for modifier in self.modifiers:
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
32 variable = StringVar(self)
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
33 variable.set('')
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
34 self.modifier_vars.append(variable)
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
35 self.advanced = False
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
36 self.CreateWidgets()
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
37 self.LoadFinalKeyList()
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
38 self.withdraw() #hide while setting geometry
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
39 self.update_idletasks()
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
40 self.geometry("+%d+%d" %
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
41 ((parent.winfo_rootx()+((parent.winfo_width()/2)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
42 -(self.winfo_reqwidth()/2)),
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
43 parent.winfo_rooty()+((parent.winfo_height()/2)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
44 -(self.winfo_reqheight()/2)) )) ) #centre dialog over parent
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
45 self.deiconify() #geometry set, unhide
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
46 self.wait_window()
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
47
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
48 def CreateWidgets(self):
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
49 frameMain = Frame(self,borderwidth=2,relief=SUNKEN)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
50 frameMain.pack(side=TOP,expand=TRUE,fill=BOTH)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
51 frameButtons=Frame(self)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
52 frameButtons.pack(side=BOTTOM,fill=X)
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
53 self.buttonOK = Button(frameButtons,text='OK',
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
54 width=8,command=self.OK)
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
55 self.buttonOK.grid(row=0,column=0,padx=5,pady=5)
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
56 self.buttonCancel = Button(frameButtons,text='Cancel',
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
57 width=8,command=self.Cancel)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
58 self.buttonCancel.grid(row=0,column=1,padx=5,pady=5)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
59 self.frameKeySeqBasic = Frame(frameMain)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
60 self.frameKeySeqAdvanced = Frame(frameMain)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
61 self.frameControlsBasic = Frame(frameMain)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
62 self.frameHelpAdvanced = Frame(frameMain)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
63 self.frameKeySeqAdvanced.grid(row=0,column=0,sticky=NSEW,padx=5,pady=5)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
64 self.frameKeySeqBasic.grid(row=0,column=0,sticky=NSEW,padx=5,pady=5)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
65 self.frameKeySeqBasic.lift()
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
66 self.frameHelpAdvanced.grid(row=1,column=0,sticky=NSEW,padx=5)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
67 self.frameControlsBasic.grid(row=1,column=0,sticky=NSEW,padx=5)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
68 self.frameControlsBasic.lift()
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
69 self.buttonLevel = Button(frameMain,command=self.ToggleLevel,
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
70 text='Advanced Key Binding Entry >>')
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
71 self.buttonLevel.grid(row=2,column=0,stick=EW,padx=5,pady=5)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
72 labelTitleBasic = Label(self.frameKeySeqBasic,
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
73 text="New keys for '"+self.action+"' :")
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
74 labelTitleBasic.pack(anchor=W)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
75 labelKeysBasic = Label(self.frameKeySeqBasic,justify=LEFT,
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
76 textvariable=self.keyString,relief=GROOVE,borderwidth=2)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
77 labelKeysBasic.pack(ipadx=5,ipady=5,fill=X)
25478
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
78 self.modifier_checkbuttons = {}
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
79 column = 0
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
80 for modifier, variable in zip(self.modifiers, self.modifier_vars):
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
81 label = self.modifier_label.get(modifier, modifier)
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
82 check=Checkbutton(self.frameControlsBasic,
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
83 command=self.BuildKeyString,
25478
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
84 text=label,variable=variable,onvalue=modifier,offvalue='')
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
85 check.grid(row=0,column=column,padx=2,sticky=W)
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
86 self.modifier_checkbuttons[modifier] = check
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
87 column += 1
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
88 labelFnAdvice=Label(self.frameControlsBasic,justify=LEFT,
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
89 text=\
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
90 "Select the desired modifier keys\n"+
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
91 "above, and the final key from the\n"+
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
92 "list on the right.\n\n" +
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
93 "Use upper case Symbols when using\n" +
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
94 "the Shift modifier. (Letters will be\n" +
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
95 "converted automatically.)")
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
96 labelFnAdvice.grid(row=1,column=0,columnspan=4,padx=2,sticky=W)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
97 self.listKeysFinal=Listbox(self.frameControlsBasic,width=15,height=10,
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
98 selectmode=SINGLE)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
99 self.listKeysFinal.bind('<ButtonRelease-1>',self.FinalKeySelected)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
100 self.listKeysFinal.grid(row=0,column=4,rowspan=4,sticky=NS)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
101 scrollKeysFinal=Scrollbar(self.frameControlsBasic,orient=VERTICAL,
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
102 command=self.listKeysFinal.yview)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
103 self.listKeysFinal.config(yscrollcommand=scrollKeysFinal.set)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
104 scrollKeysFinal.grid(row=0,column=5,rowspan=4,sticky=NS)
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
105 self.buttonClear=Button(self.frameControlsBasic,
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
106 text='Clear Keys',command=self.ClearKeySeq)
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
107 self.buttonClear.grid(row=2,column=0,columnspan=4)
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
108 labelTitleAdvanced = Label(self.frameKeySeqAdvanced,justify=LEFT,
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
109 text="Enter new binding(s) for '"+self.action+"' :\n"+
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
110 "(These bindings will not be checked for validity!)")
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
111 labelTitleAdvanced.pack(anchor=W)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
112 self.entryKeysAdvanced=Entry(self.frameKeySeqAdvanced,
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
113 textvariable=self.keyString)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
114 self.entryKeysAdvanced.pack(fill=X)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
115 labelHelpAdvanced=Label(self.frameHelpAdvanced,justify=LEFT,
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
116 text="Key bindings are specified using Tkinter keysyms as\n"+
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
117 "in these samples: <Control-f>, <Shift-F2>, <F12>,\n"
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
118 "<Control-space>, <Meta-less>, <Control-Alt-Shift-X>.\n"
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
119 "Upper case is used when the Shift modifier is present!\n\n" +
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
120 "'Emacs style' multi-keystroke bindings are specified as\n" +
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
121 "follows: <Control-x><Control-y>, where the first key\n" +
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
122 "is the 'do-nothing' keybinding.\n\n" +
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
123 "Multiple separate bindings for one action should be\n"+
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
124 "separated by a space, eg., <Alt-v> <Meta-v>." )
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
125 labelHelpAdvanced.grid(row=0,column=0,sticky=NSEW)
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
126
25478
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
127 def SetModifiersForPlatform(self):
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
128 """Determine list of names of key modifiers for this platform.
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
129
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
130 The names are used to build Tk bindings -- it doesn't matter if the
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
131 keyboard has these keys, it matters if Tk understands them. The
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
132 order is also important: key binding equality depends on it, so
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
133 config-keys.def must use the same ordering.
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
134 """
52014
dc3d24824b60 Merged revisions 70176 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47183
diff changeset
135 import macosxSupport
dc3d24824b60 Merged revisions 70176 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47183
diff changeset
136 if macosxSupport.runningAsOSXApp():
25478
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
137 self.modifiers = ['Shift', 'Control', 'Option', 'Command']
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
138 else:
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
139 self.modifiers = ['Control', 'Alt', 'Shift']
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
140 self.modifier_label = {'Control': 'Ctrl'} # short name
25478
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
141
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
142 def ToggleLevel(self):
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
143 if self.buttonLevel.cget('text')[:8]=='Advanced':
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
144 self.ClearKeySeq()
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
145 self.buttonLevel.config(text='<< Basic Key Binding Entry')
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
146 self.frameKeySeqAdvanced.lift()
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
147 self.frameHelpAdvanced.lift()
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
148 self.entryKeysAdvanced.focus_set()
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
149 self.advanced = True
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
150 else:
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
151 self.ClearKeySeq()
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
152 self.buttonLevel.config(text='Advanced Key Binding Entry >>')
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
153 self.frameKeySeqBasic.lift()
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
154 self.frameControlsBasic.lift()
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
155 self.advanced = False
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
156
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
157 def FinalKeySelected(self,event):
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
158 self.BuildKeyString()
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
159
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
160 def BuildKeyString(self):
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
161 keyList = modifiers = self.GetModifiers()
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
162 finalKey = self.listKeysFinal.get(ANCHOR)
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
163 if finalKey:
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
164 finalKey = self.TranslateKey(finalKey, modifiers)
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
165 keyList.append(finalKey)
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
166 self.keyString.set('<' + string.join(keyList,'-') + '>')
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
167
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
168 def GetModifiers(self):
25478
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
169 modList = [variable.get() for variable in self.modifier_vars]
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
170 return filter(None, modList)
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
171
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
172 def ClearKeySeq(self):
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
173 self.listKeysFinal.select_clear(0,END)
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
174 self.listKeysFinal.yview(MOVETO, '0.0')
25478
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
175 for variable in self.modifier_vars:
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
176 variable.set('')
8c95486301b3 Mac users now see correct modifiers in the Key Binding Entry window.
Tony Lownds <tony@lownds.com>
parents: 22127
diff changeset
177 self.keyString.set('')
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
178
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
179 def LoadFinalKeyList(self):
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
180 #these tuples are also available for use in validity checks
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
181 self.functionKeys=('F1','F2','F2','F4','F5','F6','F7','F8','F9',
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
182 'F10','F11','F12')
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
183 self.alphanumKeys=tuple(string.ascii_lowercase+string.digits)
21764
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
184 self.punctuationKeys=tuple('~!@#%^&*()_-+={}[]|;:,.<>/?')
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
185 self.whitespaceKeys=('Tab','Space','Return')
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
186 self.editKeys=('BackSpace','Delete','Insert')
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
187 self.moveKeys=('Home','End','Page Up','Page Down','Left Arrow',
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
188 'Right Arrow','Up Arrow','Down Arrow')
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
189 #make a tuple of most of the useful common 'final' keys
21764
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
190 keys=(self.alphanumKeys+self.punctuationKeys+self.functionKeys+
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
191 self.whitespaceKeys+self.editKeys+self.moveKeys)
29794
94e5d1b8f696 SF patch #768187: replace apply(f, args, kwds) with f(*args, **kwds)
Raymond Hettinger <python@rcn.com>
parents: 26959
diff changeset
192 self.listKeysFinal.insert(END, *keys)
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
193
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
194 def TranslateKey(self, key, modifiers):
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
195 "Translate from keycap symbol to the Tkinter keysym"
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
196 translateDict = {'Space':'space',
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
197 '~':'asciitilde','!':'exclam','@':'at','#':'numbersign',
21764
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
198 '%':'percent','^':'asciicircum','&':'ampersand','*':'asterisk',
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
199 '(':'parenleft',')':'parenright','_':'underscore','-':'minus',
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
200 '+':'plus','=':'equal','{':'braceleft','}':'braceright',
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
201 '[':'bracketleft',']':'bracketright','|':'bar',';':'semicolon',
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
202 ':':'colon',',':'comma','.':'period','<':'less','>':'greater',
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
203 '/':'slash','?':'question','Page Up':'Prior','Page Down':'Next',
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
204 'Left Arrow':'Left','Right Arrow':'Right','Up Arrow':'Up',
39113
244fb524ddd4 Rebinding Tab key was inserting 'tab' instead of 'Tab'. Bug 1179168.
Kurt B. Kaiser <kbk@shore.net>
parents: 38744
diff changeset
205 'Down Arrow': 'Down', 'Tab':'Tab'}
21764
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
206 if key in translateDict.keys():
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
207 key = translateDict[key]
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
208 if 'Shift' in modifiers and key in string.ascii_lowercase:
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
209 key = key.upper()
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
210 key = 'Key-' + key
21764
0110a0983e61 further work on keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21763
diff changeset
211 return key
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
212
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
213 def OK(self, event=None):
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
214 if self.advanced or self.KeysOK(): # doesn't check advanced string yet
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
215 self.result=self.keyString.get()
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
216 self.destroy()
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
217
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
218 def Cancel(self, event=None):
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
219 self.result=''
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
220 self.destroy()
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
221
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
222 def KeysOK(self):
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
223 '''Validity check on user's 'basic' keybinding selection.
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
224
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
225 Doesn't check the string produced by the advanced dialog because
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
226 'modifiers' isn't set.
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
227
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
228 '''
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
229 keys = self.keyString.get()
21763
652dc51b5f83 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21762
diff changeset
230 keys.strip()
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
231 finalKey = self.listKeysFinal.get(ANCHOR)
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
232 modifiers = self.GetModifiers()
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
233 # create a key sequence list for overlap check:
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
234 keySequence = keys.split()
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
235 keysOK = False
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
236 title = 'Key Sequence Error'
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
237 if not keys:
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
238 tkMessageBox.showerror(title=title, parent=self,
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
239 message='No keys specified.')
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
240 elif not keys.endswith('>'):
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
241 tkMessageBox.showerror(title=title, parent=self,
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
242 message='Missing the final Key')
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
243 elif (not modifiers
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
244 and finalKey not in self.functionKeys + self.moveKeys):
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
245 tkMessageBox.showerror(title=title, parent=self,
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
246 message='No modifier key(s) specified.')
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
247 elif (modifiers == ['Shift']) \
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
248 and (finalKey not in
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
249 self.functionKeys + self.moveKeys + ('Tab', 'Space')):
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
250 msg = 'The shift modifier by itself may not be used with'\
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
251 ' this key symbol.'
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
252 tkMessageBox.showerror(title=title, parent=self, message=msg)
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
253 elif keySequence in self.currentKeySequences:
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
254 msg = 'This key combination is already in use.'
35931
cf2b4c8ad1ad 1. Made advanced keybinding dialog functional.
Kurt B. Kaiser <kbk@shore.net>
parents: 31013
diff changeset
255 tkMessageBox.showerror(title=title, parent=self, message=msg)
31013
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
256 else:
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
257 keysOK = True
270d59e346e6 Keybindings with the Shift modifier now work correctly. So do bindings
Kurt B. Kaiser <kbk@shore.net>
parents: 29794
diff changeset
258 return keysOK
26959
b8c72e86a12d Whitespace Normalization
Kurt B. Kaiser <kbk@shore.net>
parents: 25478
diff changeset
259
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
260 if __name__ == '__main__':
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
261 #test the dialog
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
262 root=Tk()
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
263 def run():
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
264 keySeq=''
21763
652dc51b5f83 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21762
diff changeset
265 dlg=GetKeysDialog(root,'Get Keys','find-again',[])
21762
a1b0658293a7 keybinding configuration
Steven M. Gava <elguavas@python.net>
parents: 21711
diff changeset
266 print dlg.result
21711
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
267 Button(root,text='Dialog',command=run).pack()
437393e780cb key binding handling dialog
Steven M. Gava <elguavas@python.net>
parents:
diff changeset
268 root.mainloop()