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