]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/scrolledpanel.py
use Bind
[wxWidgets.git] / wxPython / wx / lib / scrolledpanel.py
1 #----------------------------------------------------------------------------
2 # Name: scrolledpanel.py
3 # Author: Will Sadkin
4 # Created: 03/21/2003
5 # Copyright: (c) 2003 by Will Sadkin
6 # RCS-ID: $Id$
7 # License: wxWindows license
8 #----------------------------------------------------------------------------
9 # 12/11/2003 - Jeff Grimmett (grimmtooth@softhome.net)
10 #
11 # o 2.5 compatability update.
12 #
13 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
14 #
15 # o wxScrolledPanel -> ScrolledPanel
16 #
17
18 import wx
19
20
21 class ScrolledPanel( wx.ScrolledWindow ):
22 """\
23 ScrolledPanel fills a "hole" in the implementation of wx.ScrolledWindow,
24 providing automatic scrollbar and scrolling behavior and the tab traversal
25 management that wxScrolledWindow lacks. This code was based on the original
26 demo code showing how to do this, but is now available for general use
27 as a proper class (and the demo is now converted to just use it.)
28 """
29 def __init__(self, parent, id=-1,
30 pos = wx.DefaultPosition, size = wx.DefaultSize,
31 style = wx.TAB_TRAVERSAL, name = "scrolledpanel"):
32
33 wx.ScrolledWindow.__init__(self, parent, -1,
34 pos=pos, size=size,
35 style=style, name=name)
36
37 self.Bind(wx.EVT_CHILD_FOCUS, self.OnChildFocus)
38
39
40 def SetupScrolling(self, scroll_x=True, scroll_y=True, rate_x=20, rate_y=20):
41 """
42 This function sets up the event handling necessary to handle
43 scrolling properly. It should be called within the __init__
44 function of any class that is derived from ScrolledPanel,
45 once the controls on the panel have been constructed and
46 thus the size of the scrolling area can be determined.
47
48 """
49 # The following is all that is needed to integrate the sizer and the
50 # scrolled window.
51 if not scroll_x: rate_x = 0
52 if not scroll_y: rate_y = 0
53
54 # Round up the virtual size to be a multiple of the scroll rate
55 sizer = self.GetSizer()
56 if sizer:
57 w, h = sizer.GetMinSize()
58 if rate_x:
59 w += rate_x - (w % rate_x)
60 if rate_y:
61 h += rate_y - (h % rate_y)
62 self.SetVirtualSize( (w, h) )
63 self.SetVirtualSizeHints( w, h )
64
65 self.SetScrollRate(rate_x, rate_y)
66 wx.CallAfter(self.Scroll, 0, 0) # scroll back to top after initial events
67
68
69 def OnChildFocus(self, evt):
70 # If the child window that gets the focus is not visible,
71 # this handler will try to scroll enough to see it.
72 evt.Skip()
73 child = evt.GetWindow()
74
75 sppu_x, sppu_y = self.GetScrollPixelsPerUnit()
76 vs_x, vs_y = self.GetViewStart()
77 cpos = child.GetPosition()
78 csz = child.GetSize()
79 new_vs_x, new_vs_y = -1, -1
80
81 # is it before the left edge?
82 if cpos.x < 0 and sppu_x > 0:
83 new_vs_x = vs_x + (cpos.x / sppu_x)
84
85 # is it above the top?
86 if cpos.y < 0 and sppu_y > 0:
87 new_vs_y = vs_y + (cpos.y / sppu_y)
88
89 clntsz = self.GetClientSize()
90
91 # is it past the right edge ?
92 if cpos.x + csz.width > clntsz.width and sppu_x > 0:
93 diff = (cpos.x + csz.width - clntsz.width) / sppu_x
94 new_vs_x = vs_x + diff + 1
95
96 # is it below the bottom ?
97 if cpos.y + csz.height > clntsz.height and sppu_y > 0:
98 diff = (cpos.y + csz.height - clntsz.height) / sppu_y
99 new_vs_y = vs_y + diff + 1
100
101 # if we need to adjust
102 if new_vs_x != -1 or new_vs_y != -1:
103 self.Scroll(new_vs_x, new_vs_y)