summaryrefslogtreecommitdiff
path: root/lib/python2.7/site-packages/wx-3.0-msw/wx/lib/dragscroller.py
blob: 7410da815077cde0104cce76690da2f3a668f165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#-----------------------------------------------------------------------------
# Name:        dragscroller.py
# Purpose:     Scrolls a wx.ScrollWindow by dragging
#
# Author:      Riaan Booysen
#
# Created:     2006/09/05
# Copyright:   (c) 2006
# Licence:     wxPython
#-----------------------------------------------------------------------------

import wx

class DragScroller:
    """ Scrolls a wx.ScrollWindow in the direction and speed of a mouse drag.
    
        Call Start with the position of the drag start.
        Call Stop on the drag release. """

    def __init__(self, scrollwin, rate=30, sensitivity=0.75):
        self.scrollwin = scrollwin
        self.rate = rate
        self.sensitivity = sensitivity

        self.pos = None
        self.timer = None
    
    def GetScrollWindow(self):
        return self.scrollwin
    def SetScrollWindow(self, scrollwin):
        self.scrollwin = scrollwin
    
    def GetUpdateRate(self):
        return self.rate
    def SetUpdateRate(self, rate):
        self.rate = rate

    def GetSensitivity(self):
        return self.sensitivity
    def SetSensitivity(self, sensitivity):
        self.sensitivity = sensitivity

    def Start(self, pos):
        """ Start a drag scroll operation """
        if not self.scrollwin:
            raise Exception, 'No ScrollWindow defined'
        
        self.pos = pos
        self.scrollwin.SetCursor(wx.StockCursor(wx.CURSOR_SIZING))
        if not self.scrollwin.HasCapture():
            self.scrollwin.CaptureMouse()

        self.timer = wx.Timer(self.scrollwin)
        self.scrollwin.Bind(wx.EVT_TIMER, self.OnTimerDoScroll, id=self.timer.GetId())
        self.timer.Start(self.rate)
    
    def Stop(self):
        """ Stops a drag scroll operation """
        if self.timer and self.scrollwin:
            self.timer.Stop()
            self.scrollwin.Disconnect(self.timer.GetId())
            self.timer.Destroy()
            self.timer = None

            self.scrollwin.SetCursor(wx.STANDARD_CURSOR)
            if self.scrollwin.HasCapture():
                self.scrollwin.ReleaseMouse()

    def OnTimerDoScroll(self, event):
        if self.pos is None or not self.timer or not self.scrollwin: 
            return
        
        new = self.scrollwin.ScreenToClient(wx.GetMousePosition())
        dx = int((new.x-self.pos.x)*self.sensitivity)
        dy = int((new.y-self.pos.y)*self.sensitivity)
        spx = self.scrollwin.GetScrollPos(wx.HORIZONTAL)
        spy = self.scrollwin.GetScrollPos(wx.VERTICAL)

        self.scrollwin.Scroll(spx+dx, spy+dy)