3 #Under the GPL, etc, etc.
5 #Thanks to Robin Dunn for taking the time to show me how to do this
10 #This is a hacked version of DragAndDrop.py from the wxPython demo 2.5.2.8
12 import wx
, wx
.stc
, wx
.lib
.dialogs
13 from wx
.lib
.gestures
import MouseGestures
17 #Add a dialog to record gestures (Have it showcase the manual mode)
18 #Allow users to remove gestures
20 #----------------------------------------------------------------------
22 class TestPanel(wx
.Panel
):
23 def __init__(self
, parent
, log
):
24 wx
.Panel
.__init
__(self
, parent
, -1)
26 ID_GESTURE
= wx
.NewId()
28 ID_MODIFIER
= wx
.NewId()
29 ID_VISIBLE
= wx
.NewId()
35 self
.mg
= MouseGestures(self
, Auto
=True)
37 self
.mg
.SetGesturesVisible(True)
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
)
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)
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))
61 w
,h
= text
.GetTextExtent(msg
)
62 text
.SetSize(wx
.Size(w
,h
+1))
63 text
.SetForegroundColour(wx
.BLUE
)
65 self
.SetBackgroundColour(wx
.WHITE
)
68 outsideSizer
= wx
.BoxSizer(wx
.VERTICAL
)
70 btnSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
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
)
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
)
79 outsideSizer
.Add(wx
.StaticText(self
, -1, ' '), 0, wx
.EXPAND
)
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
)
86 self
.SetAutoLayout(True)
87 self
.SetSizer(outsideSizer
)
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
)
95 def LogSomethingClever(self
, somethingclever
):
96 self
.log
.WriteText(somethingclever
)
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()
104 d
= wx
.TextEntryDialog(self
, 'Print the following text on "%s":' % gesture
, "Gesture Action", "")
105 answer2
= d
.ShowModal()
109 if (answer1
== wx
.ID_OK
) and (answer2
== wx
.ID_OK
):
110 self
.mg
.AddGesture(gesture
.upper(), self
.LogSomethingClever
, text
)
112 def OnChangeModifiers(self
, event
):
113 choices
= [wx
.WXK_CONTROL
, wx
.WXK_SHIFT
, wx
.WXK_ALT
]
114 schoices
= ['Control', 'Shift', 'Alt']
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()
121 if (answer
== wx
.ID_OK
):
126 modifiers
.append(choices
[x
])
127 modstring
+= schoices
[x
] + ' '
128 self
.mg
.SetModifiers(modifiers
)
129 self
.log
.WriteText('Set Modifiers to: ' + modstring
)
131 self
.mg
.SetModifiers()
132 self
.log
.WriteText('UnSet All Modifiers')
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()
142 if (answer
== wx
.ID_OK
):
143 self
.mg
.SetMouseButton(choices
[i
])
144 self
.log
.WriteText('Set the Mouse Button to ' + schoices
[i
])
146 def OnDownThenRight(self
):
147 self
.log
.WriteText('You made an "L"!')
149 def OnToggleVisible(self
, event
):
150 visual
= self
.btnToggleVisible
.GetValue()
151 self
.mg
.SetGesturesVisible(visual
)
153 self
.log
.WriteText('Made Gestures Visible')
155 self
.log
.WriteText('Made Gestures Invisible')
158 self
.mg
.SetGesturePen(wx
.Colour(0, 144, 255), 5)
159 self
.log
.WriteText('Set Gesture Colour to Blue')
161 def SetToOrange(self
):
162 self
.mg
.SetGesturePen(wx
.Colour(255, 156, 0), 5)
163 self
.log
.WriteText('Set Gesture Colour to Orange')
165 def ShowSomethingClever(self
, somethingclever
):
166 d
= wx
.MessageDialog(self
, somethingclever
, 'Mouse Gesture Action', wx
.OK
)
170 #----------------------------------------------------------------------
172 def runTest(frame
, nb
, log
):
173 win
= TestPanel(nb
, log
)
176 #----------------------------------------------------------------------
182 This demo shows how to add MouseGestures
183 to your program, and showcases the MouseGestures
184 class in all it's mousey glory.
191 if __name__
== '__main__':
194 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])