]>
Commit | Line | Data |
---|---|---|
f77c79d0 RD |
1 | #----------------------------------------------------------------------------- |
2 | # Name: dragscroller.py | |
3 | # Purpose: Scrolls a wx.ScrollWindow by dragging | |
4 | # | |
5 | # Author: Riaan Booysen | |
6 | # | |
7 | # Created: 2006/09/05 | |
8 | # Copyright: (c) 2006 | |
9 | # Licence: wxPython | |
10 | #----------------------------------------------------------------------------- | |
11 | ||
12 | import wx | |
13 | ||
14 | class DragScroller: | |
15 | """ Scrolls a wx.ScrollWindow in the direction and speed of a mouse drag. | |
16 | ||
17 | Call Start with the position of the drag start. | |
18 | Call Stop on the drag release. """ | |
19 | ||
20 | def __init__(self, scrollwin, rate=30, sensitivity=0.75): | |
21 | self.scrollwin = scrollwin | |
22 | self.rate = rate | |
23 | self.sensitivity = sensitivity | |
24 | ||
25 | self.pos = None | |
26 | self.timer = None | |
27 | ||
28 | def GetScrollWindow(self): | |
29 | return self.scrollwin | |
30 | def SetScrollWindow(self, scrollwin): | |
31 | self.scrollwin = scrollwin | |
32 | ||
33 | def GetUpdateRate(self): | |
34 | return self.rate | |
35 | def SetUpdateRate(self, rate): | |
36 | self.rate = rate | |
37 | ||
38 | def GetSensitivity(self): | |
39 | return self.sensitivity | |
40 | def SetSensitivity(self, sensitivity): | |
41 | self.sensitivity = sensitivity | |
42 | ||
43 | def Start(self, pos): | |
44 | """ Start a drag scroll operation """ | |
45 | if not self.scrollwin: | |
46 | raise Exception, 'No ScrollWindow defined' | |
47 | ||
48 | self.pos = pos | |
49 | self.scrollwin.SetCursor(wx.StockCursor(wx.CURSOR_SIZING)) | |
50 | self.scrollwin.CaptureMouse() | |
51 | ||
52 | self.timer = wx.Timer(self.scrollwin) | |
53 | self.scrollwin.Bind(wx.EVT_TIMER, self.OnTimerDoScroll, id=self.timer.GetId()) | |
54 | self.timer.Start(self.rate) | |
55 | ||
56 | def Stop(self): | |
57 | """ Stops a drag scroll operation """ | |
58 | if self.timer and self.scrollwin: | |
59 | self.timer.Stop() | |
60 | self.scrollwin.Disconnect(self.timer.GetId()) | |
61 | self.timer.Destroy() | |
62 | self.timer = None | |
63 | ||
64 | self.scrollwin.SetCursor(wx.STANDARD_CURSOR) | |
65 | self.scrollwin.ReleaseMouse() | |
66 | ||
67 | def OnTimerDoScroll(self, event): | |
68 | if self.pos is None or not self.timer or not self.scrollwin: | |
69 | return | |
70 | ||
71 | new = self.scrollwin.ScreenToClient(wx.GetMousePosition()) | |
72 | dx = int((new.x-self.pos.x)*self.sensitivity) | |
73 | dy = int((new.y-self.pos.y)*self.sensitivity) | |
74 | spx = self.scrollwin.GetScrollPos(wx.HORIZONTAL) | |
75 | spy = self.scrollwin.GetScrollPos(wx.VERTICAL) | |
76 | ||
77 | self.scrollwin.Scroll(spx+dx, spy+dy) |