wxPython / demo /

Full commit
#By Daniel Pozmanter

#Thanks to Robin Dunn for taking the time to show me how to do this
#via the mailing list.

#Version 0.0.0

#This is a hacked version of from the wxPython demo

import wx, wx.lib.dialogs
from wx.lib.gestures import MouseGestures


#Add a dialog to record gestures  (Have it showcase the manual mode)
#Allow users to remove gestures


class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        wx.Panel.__init__(self, parent, -1)

        ID_GESTURE = wx.NewId()
        ID_MOUSE = wx.NewId()
        ID_MODIFIER = wx.NewId()
        ID_VISIBLE = wx.NewId()

        self.log = log

        #Mouse Gestures:
    = MouseGestures(self, Auto=True,
       'LR', self.ShowSomethingClever, 'Left then Right!')'39', self.ShowSomethingClever, 'You made a V!')'L', self.LogSomethingClever, 'You moved left')'9', self.LogSomethingClever, 'You moved right and up')'U', self.LogSomethingClever, 'You moved up')'DR', self.OnDownThenRight)'LDRU', self.SetToBlue)'RDLU', self.SetToOrange)
        self.btnAddGesture = wx.Button(self, ID_GESTURE, 'Add New Gesture')
        self.btnChangeMouseButton = wx.Button(self, ID_MOUSE, 'Change Mouse Button')
        self.btnChangeModifier = wx.Button(self, ID_MODIFIER, 'Change Modifier')
        self.btnToggleVisible = wx.ToggleButton(self, ID_VISIBLE, 'Toggle Gestures Visible')
        msg = "Mouse Gestures"
        text = wx.StaticText(self, -1, "", style=wx.ALIGN_CENTRE)
        text.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False))

        w,h = text.GetTextExtent(msg)
        outsideSizer = wx.BoxSizer(wx.VERTICAL)
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        outsideSizer.Add(text, 0, wx.EXPAND|wx.ALL, 5)
        outsideSizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND)
        outsideSizer.Add(wx.StaticText(self, -1, '   '), 0, wx.EXPAND)
        outsideSizer.Add(wx.StaticText(self, -1, 'Hold The Middle Mouse Button Down to Gesticulate'), 0, wx.EXPAND)
        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)
        outsideSizer.Add(wx.StaticText(self, -1, 'Left,Down,Right,Up Sets the line colour to Blue.'), 0, wx.EXPAND)
        outsideSizer.Add(wx.StaticText(self, -1, 'Right,Down,Left,Up Sets the line colour to Orange.'), 0, wx.EXPAND)
        outsideSizer.Add(wx.StaticText(self, -1, '   '), 0, wx.EXPAND)
        btnSizer.Add(self.btnAddGesture, 0, wx.SHAPED)
        btnSizer.Add(self.btnChangeMouseButton, 0, wx.SHAPED)
        btnSizer.Add(self.btnChangeModifier, 0, wx.SHAPED)
        btnSizer.Add(self.btnToggleVisible, 0, wx.SHAPED)
        outsideSizer.Add(btnSizer, 0, wx.SHAPED)

        self.Bind(wx.EVT_BUTTON, self.OnAddGesture, id=ID_GESTURE)
        self.Bind(wx.EVT_BUTTON, self.OnChangeMouseButton, id=ID_MOUSE)
        self.Bind(wx.EVT_BUTTON, self.OnChangeModifiers, id=ID_MODIFIER)
        self.Bind(wx.EVT_TOGGLEBUTTON, self.OnToggleVisible, id=ID_VISIBLE)

    def LogSomethingClever(self, somethingclever):
    def OnAddGesture(self, event):
        d = wx.TextEntryDialog(self, "Enter Gesture (LRUD1379) (EG Right Then Up Then DownLeft is RU1):", "Add New Gesture", "")
        answer1 = d.ShowModal()
        gesture = d.GetValue()
        d = wx.TextEntryDialog(self, 'Print the following text on "%s":' % gesture, "Gesture Action", "")
        answer2 = d.ShowModal()
        text = d.GetValue()
        if (answer1 == wx.ID_OK) and (answer2 == wx.ID_OK):
  , self.LogSomethingClever, text)
    def OnChangeModifiers(self, event):
        choices = [wx.WXK_CONTROL, wx.WXK_SHIFT, wx.WXK_ALT]
        schoices = ['Control', 'Shift', 'Alt']
        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)
        answer = d.ShowModal()
        tuply = d.GetValue()
        if (answer == wx.ID_OK):
            if len(tuply) > 0:
                modifiers = []
                modstring = ''
                for x in tuply:
                    modstring += schoices[x] + ' '
                self.log.WriteText('Set Modifiers to: ' + modstring)
                self.log.WriteText('UnSet All Modifiers')
    def OnChangeMouseButton(self, event):
        choices = [wx.MOUSE_BTN_LEFT, wx.MOUSE_BTN_MIDDLE, wx.MOUSE_BTN_RIGHT]
        schoices = ['Left', 'Middle', 'Right']
        d = wx.SingleChoiceDialog(self, "Set Mouse Button To", "Change Mouse Button", schoices,
        d.SetSize(wx.Size(250, 200))
        answer = d.ShowModal()
        i = d.GetSelection()
        if (answer == wx.ID_OK):
            self.log.WriteText('Set the Mouse Button to ' + schoices[i])
    def OnDownThenRight(self):
        self.log.WriteText('You made an "L"!')

    def OnToggleVisible(self, event):
        visual = self.btnToggleVisible.GetValue()
        if visual:
            self.log.WriteText('Made Gestures Visible')
            self.log.WriteText('Made Gestures Invisible')

    def SetToBlue(self):, 144, 255), 5)
        self.log.WriteText('Set Gesture Colour to Blue')
    def SetToOrange(self):, 156, 0), 5)
        self.log.WriteText('Set Gesture Colour to Orange')

    def ShowSomethingClever(self, somethingclever):
        d = wx.MessageDialog(self, somethingclever, 'Mouse Gesture Action', wx.OK)


def runTest(frame, nb, log):
    win = TestPanel(nb, log)
    return win


overview = """\
This demo shows how to add MouseGestures
to your program, and showcases the MouseGestures
class in all it's mousey glory.

if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])