Package pallavi :: Package plugins :: Module SimpleKeybindings
[hide private]
[frames] | no frames]

Source Code for Module pallavi.plugins.SimpleKeybindings

 1  # Copyright (c) 2006 Dusty Phillips 
 2   
 3  # Permission is hereby granted, free of charge, to any person obtaining a 
 4  # copy of this software and associated documentation files (the "Software"), 
 5  # to deal in the Software without restriction, including without limitation 
 6  # the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 7  # and/or sell copies of the Software, and to permit persons to whom the 
 8  # Software is furnished to do so, subject to the following conditions: 
 9   
10  # The above copyright notice and this permission notice shall be included in 
11  # all copies or substantial portions of the Software. 
12   
13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
16  # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
18  # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
19  # DEALINGS IN THE SOFTWARE. 
20   
21  '''This plugin listens for certain keystrokes and binds them to actions. Other 
22  keystrokes are silently passed onto the StyledTextControl for handling. This 
23  is the simplest way to implement keybindings, as the StyledTextControl 
24  takes care of a lot of the work. However, it is not the most extensible, as 
25  modifying keybindings requires editing the plugin, and we end up with a mix 
26  of StyledTextControl keybindings and Pallavi handled keybindings. A more 
27  extensible solution was used in Pallavi 0.1 with the keystroke_conversion plugin, 
28  but because it was incomplete, this restricted a lot of the functionality. 
29   
30  Luckily, any number of keybinding plugins can be written (though perhaps they 
31  should not all be used at once!) So in the future, a more extensible system may 
32  be devised or some enterprising plugin developer. Ideally, I would like to see 
33  a modal plugin (like vim interaction) and a fully customizable plugin with a keybinding 
34  interface that lists all known user actions and allows them to be bound. And also 
35  a simple one for people that want sleek and elegant. 
36   
37  This Plugin queues the following event: 
38  KeyStroke 
39          When a key has been pressed 
40           
41  This plugins listens for the following events: 
42  TextViewAdded 
43          Sets up a keystroke listener on a textview when it has been added to the view. 
44  ''' 
45  from pallavi import EventActionManager 
46  import wx 
47   
48 -def setup():
49 for view in pallavi.views: 50 for textview in view.textViews: 51 TextViewAdded(None, view.textViews[textview]) 52 53 eventBus.AddListener("TextViewAdded", TextViewAdded) 54 view.Bind(wx.EVT_KEY_DOWN, KeyDown)
55
56 -def TextViewAdded(eventName, textview):
57 textview.Bind(wx.EVT_KEY_DOWN, KeyDown)
58
59 -def KeyDown(event):
60 key = "" 61 if event.AltDown(): 62 key = key + "ALT+" 63 if event.ControlDown(): 64 key = key + "CTRL+" 65 if event.ShiftDown(): 66 key = key + "SHIFT+" 67 if event.GetKeyCode() < 255: 68 key = key + chr(event.GetKeyCode()) 69 else: 70 # Ok, this is a rather "clever" hack. Clever code isn't always smart. 71 # There are certain keycodes in the wx keycode table outlined here: 72 # http://www.wxpython.org/docs/api/wx.KeyEvent-class.html 73 # Rather than trying to parse for each one, this one gets the list from the wx module 74 # in the filter statement (an optimization would statically build that list, but then it has 75 # to be kept up to date). It then uses an exec statement to assign the keycode a variable. 76 # It then uses the variable/constant's NAME to identify the key character as a keyboard 77 # shortcut that the user can define, removing the WXK_ prefix in the process. 78 for special_keyvariable in filter(lambda x: x.startswith("WXK_"), dir(wx)): 79 exec("special_keycode = wx." + special_keyvariable) 80 if event.GetKeyCode() == special_keycode: 81 key = key + special_keyvariable[4:] 82 #print key 83 eventBus.QueueEvent("KeyStroke", event.GetKeyCode()) 84 85 if config.simplekeybindings.has_key(key): 86 action = config.simplekeybindings[key] 87 if type(action) == tuple: 88 actions.Invoke(*action) 89 else: 90 actions.Invoke(action) 91 else: 92 event.Skip()
93