]>
Commit | Line | Data |
---|---|---|
51c5e1f2 RD |
1 | #******************* |
2 | #By Daniel Pozmanter | |
3 | #Under the GPL, etc, etc. | |
4 | ||
5 | #Thanks to Robin Dunn for taking the time to show me how to do this | |
6 | #via the mailing list. | |
7 | ||
7a0c9b39 | 8 | #Version 0.0.0 |
51c5e1f2 RD |
9 | |
10 | #This is a hacked version of DragAndDrop.py from the wxPython demo 2.5.2.8 | |
11 | ||
7a0c9b39 | 12 | import wx, wx.stc, wx.lib.dialogs |
51c5e1f2 RD |
13 | from wx.lib.gestures import MouseGestures |
14 | ||
15 | #ToDo: | |
16 | ||
17 | #Add a dialog to record gestures (Have it showcase the manual mode) | |
18 | #Allow users to remove gestures | |
19 | ||
20 | #---------------------------------------------------------------------- | |
21 | ||
22 | class TestPanel(wx.Panel): | |
23 | def __init__(self, parent, log): | |
24 | wx.Panel.__init__(self, parent, -1) | |
25 | ||
26 | ID_GESTURE = wx.NewId() | |
7a0c9b39 RD |
27 | ID_MOUSE = wx.NewId() |
28 | ID_MODIFIER = wx.NewId() | |
29 | ID_VISIBLE = wx.NewId() | |
51c5e1f2 RD |
30 | |
31 | self.log = log | |
32 | ||
33 | #Mouse Gestures: | |
34 | ||
35 | self.mg = MouseGestures(self, Auto=True) | |
36 | ||
37 | self.mg.SetGesturesVisible(True) | |
38 | ||
7a0c9b39 RD |
39 | self.mg.AddGesture('LR', self.ShowSomethingClever, 'Left then Right!') |
40 | self.mg.AddGesture('39', self.ShowSomethingClever, 'You made a V!') | |
51c5e1f2 RD |
41 | self.mg.AddGesture('L', self.LogSomethingClever, 'You moved left') |
42 | self.mg.AddGesture('9', self.LogSomethingClever, 'You moved right and up') | |
43 | self.mg.AddGesture('U', self.LogSomethingClever, 'You moved up') | |
7a0c9b39 RD |
44 | self.mg.AddGesture('DR', self.OnDownThenRight) |
45 | self.mg.AddGesture('LDRU', self.SetToBlue) | |
46 | self.mg.AddGesture('RDLU', self.SetToOrange) | |
51c5e1f2 RD |
47 | |
48 | #Widgets: | |
49 | ||
50 | self.btnAddGesture = wx.Button(self, ID_GESTURE, 'Add New Gesture') | |
7a0c9b39 RD |
51 | self.btnChangeMouseButton = wx.Button(self, ID_MOUSE, 'Change Mouse Button') |
52 | self.btnChangeModifier = wx.Button(self, ID_MODIFIER, 'Change Modifier') | |
53 | self.btnToggleVisible = wx.ToggleButton(self, ID_VISIBLE, 'Toggle Gestures Visible') | |
54 | self.btnToggleVisible.SetValue(True) | |
51c5e1f2 RD |
55 | |
56 | msg = "Mouse Gestures" | |
57 | text = wx.StaticText(self, -1, "", style=wx.ALIGN_CENTRE) | |
58 | text.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False)) | |
59 | text.SetLabel(msg) | |
60 | ||
61 | w,h = text.GetTextExtent(msg) | |
62 | text.SetSize(wx.Size(w,h+1)) | |
7a0c9b39 RD |
63 | text.SetForegroundColour(wx.BLUE) |
64 | ||
65 | self.SetBackgroundColour(wx.WHITE) | |
51c5e1f2 RD |
66 | |
67 | #Sizer: | |
68 | outsideSizer = wx.BoxSizer(wx.VERTICAL) | |
69 | ||
7a0c9b39 RD |
70 | btnSizer = wx.BoxSizer(wx.HORIZONTAL) |
71 | ||
51c5e1f2 RD |
72 | outsideSizer.Add(text, 0, wx.EXPAND|wx.ALL, 5) |
73 | outsideSizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND) | |
74 | outsideSizer.Add(wx.StaticText(self, -1, ' '), 0, wx.EXPAND) | |
75 | outsideSizer.Add(wx.StaticText(self, -1, 'Hold The Middle Mouse Button Down to Gesticulate'), 0, wx.EXPAND) | |
7a0c9b39 RD |
76 | outsideSizer.Add(wx.StaticText(self, -1, 'Left Then Right, Left, The Diagonal Up/Right, Down Then Right, Diagonal Down/Right Then Diagonal Up/Right, and Up are Preset'), 0, wx.EXPAND) |
77 | outsideSizer.Add(wx.StaticText(self, -1, 'Left,Down,Right,Up Sets the line colour to Blue.'), 0, wx.EXPAND) | |
78 | outsideSizer.Add(wx.StaticText(self, -1, 'Right,Down,Left,Up Sets the line colour to Orange.'), 0, wx.EXPAND) | |
51c5e1f2 | 79 | outsideSizer.Add(wx.StaticText(self, -1, ' '), 0, wx.EXPAND) |
7a0c9b39 RD |
80 | btnSizer.Add(self.btnAddGesture, 0, wx.SHAPED) |
81 | btnSizer.Add(self.btnChangeMouseButton, 0, wx.SHAPED) | |
82 | btnSizer.Add(self.btnChangeModifier, 0, wx.SHAPED) | |
83 | btnSizer.Add(self.btnToggleVisible, 0, wx.SHAPED) | |
84 | outsideSizer.Add(btnSizer, 0, wx.SHAPED) | |
51c5e1f2 RD |
85 | |
86 | self.SetAutoLayout(True) | |
87 | self.SetSizer(outsideSizer) | |
88 | ||
51c5e1f2 | 89 | #Events: |
7a0c9b39 RD |
90 | self.Bind(wx.EVT_BUTTON, self.OnAddGesture, id=ID_GESTURE) |
91 | self.Bind(wx.EVT_BUTTON, self.OnChangeMouseButton, id=ID_MOUSE) | |
92 | self.Bind(wx.EVT_BUTTON, self.OnChangeModifiers, id=ID_MODIFIER) | |
93 | self.Bind(wx.EVT_TOGGLEBUTTON, self.OnToggleVisible, id=ID_VISIBLE) | |
51c5e1f2 RD |
94 | |
95 | def LogSomethingClever(self, somethingclever): | |
96 | self.log.WriteText(somethingclever) | |
97 | ||
98 | def OnAddGesture(self, event): | |
99 | d = wx.TextEntryDialog(self, "Enter Gesture (LRUD1379) (EG Right Then Up Then DownLeft is RU1):", "Add New Gesture", "") | |
100 | answer1 = d.ShowModal() | |
101 | gesture = d.GetValue() | |
102 | d.Destroy() | |
7a0c9b39 | 103 | |
51c5e1f2 RD |
104 | d = wx.TextEntryDialog(self, 'Print the following text on "%s":' % gesture, "Gesture Action", "") |
105 | answer2 = d.ShowModal() | |
106 | text = d.GetValue() | |
107 | d.Destroy() | |
7a0c9b39 | 108 | |
51c5e1f2 RD |
109 | if (answer1 == wx.ID_OK) and (answer2 == wx.ID_OK): |
110 | self.mg.AddGesture(gesture.upper(), self.LogSomethingClever, text) | |
7a0c9b39 RD |
111 | |
112 | def OnChangeModifiers(self, event): | |
113 | choices = [wx.WXK_CONTROL, wx.WXK_SHIFT, wx.WXK_ALT] | |
114 | schoices = ['Control', 'Shift', 'Alt'] | |
115 | ||
116 | d = wx.lib.dialogs.MultipleChoiceDialog(self, 'Select Modifier Keys:\n(Select None if you do not want to use modifier keys\n\n', "Change Modifier Keys", schoices) | |
117 | answer = d.ShowModal() | |
118 | tuply = d.GetValue() | |
119 | d.Destroy() | |
120 | ||
121 | if (answer == wx.ID_OK): | |
122 | if len(tuply) > 0: | |
123 | modifiers = [] | |
124 | modstring = '' | |
125 | for x in tuply: | |
126 | modifiers.append(choices[x]) | |
127 | modstring += schoices[x] + ' ' | |
128 | self.mg.SetModifiers(modifiers) | |
129 | self.log.WriteText('Set Modifiers to: ' + modstring) | |
130 | else: | |
131 | self.mg.SetModifiers() | |
132 | self.log.WriteText('UnSet All Modifiers') | |
133 | ||
134 | def OnChangeMouseButton(self, event): | |
135 | choices = [wx.MOUSE_BTN_LEFT, wx.MOUSE_BTN_MIDDLE, wx.MOUSE_BTN_RIGHT] | |
136 | schoices = ['Left', 'Middle', 'Right'] | |
137 | d = wx.SingleChoiceDialog(self, "Set Mouse Button To", "Change Mouse Button", schoices, wx.OK|wx.CANCEL) | |
138 | d.SetSize(wx.Size(250, 200)) | |
139 | answer = d.ShowModal() | |
140 | i = d.GetSelection() | |
141 | d.Destroy() | |
142 | if (answer == wx.ID_OK): | |
143 | self.mg.SetMouseButton(choices[i]) | |
144 | self.log.WriteText('Set the Mouse Button to ' + schoices[i]) | |
51c5e1f2 RD |
145 | |
146 | def OnDownThenRight(self): | |
147 | self.log.WriteText('You made an "L"!') | |
148 | ||
7a0c9b39 RD |
149 | def OnToggleVisible(self, event): |
150 | visual = self.btnToggleVisible.GetValue() | |
151 | self.mg.SetGesturesVisible(visual) | |
152 | if visual: | |
153 | self.log.WriteText('Made Gestures Visible') | |
154 | else: | |
155 | self.log.WriteText('Made Gestures Invisible') | |
156 | ||
157 | def SetToBlue(self): | |
158 | self.mg.SetGesturePen(wx.Colour(0, 144, 255), 5) | |
159 | self.log.WriteText('Set Gesture Colour to Blue') | |
160 | ||
161 | def SetToOrange(self): | |
162 | self.mg.SetGesturePen(wx.Colour(255, 156, 0), 5) | |
163 | self.log.WriteText('Set Gesture Colour to Orange') | |
164 | ||
165 | def ShowSomethingClever(self, somethingclever): | |
166 | d = wx.MessageDialog(self, somethingclever, 'Mouse Gesture Action', wx.OK) | |
167 | d.ShowModal() | |
168 | d.Destroy() | |
169 | ||
51c5e1f2 RD |
170 | #---------------------------------------------------------------------- |
171 | ||
172 | def runTest(frame, nb, log): | |
173 | win = TestPanel(nb, log) | |
174 | return win | |
175 | ||
176 | #---------------------------------------------------------------------- | |
177 | ||
178 | ||
179 | overview = """\ | |
180 | <html> | |
181 | <body> | |
182 | This demo shows how to add MouseGestures | |
183 | to your program, and showcases the MouseGestures | |
184 | class in all it's mousey glory. | |
185 | <p><p> | |
186 | </body> | |
187 | </html> | |
188 | """ | |
189 | ||
190 | ||
191 | if __name__ == '__main__': | |
192 | import sys,os | |
193 | import run | |
194 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) | |
195 | ||
7a0c9b39 | 196 |