]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/scrolledpanel.py
b8da67e4983561ea56b5bfdafc5c52e6a4e19e9d
[wxWidgets.git] / wxPython / wxPython / lib / scrolledpanel.py
1 #----------------------------------------------------------------------------
2 # Name: wxScrolledPanel.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 #
10
11 from wxPython.wx import *
12
13
14 class wxScrolledPanel( wxScrolledWindow ):
15 """
16 wxScrolledPanel fills a "hole" in the implementation of wxScrolledWindow,
17 providing automatic scrollbar and scrolling behavior and the tab traversal
18 management that wxScrolledWindow lacks. This code was based on the original
19 demo code showing how to do this, but is now available for general use
20 as a proper class (and the demo is now converted to just use it.)
21 """
22 def __init__(self, parent, id=-1,
23 pos = wxDefaultPosition, size = wxDefaultSize,
24 style = wxTAB_TRAVERSAL, name = "scrolledpanel"):
25
26 wxScrolledWindow.__init__(self, parent, -1,
27 pos=pos, size=size,
28 style=style, name=name)
29
30
31 def SetupScrolling(self, scroll_x=True, scroll_y=True, rate_x=1, rate_y=1):
32 """
33 This function sets up the event handling necessary to handle
34 scrolling properly. It should be called within the __init__
35 function of any class that is derived from wxScrolledPanel,
36 once the controls on the panel have been constructed and
37 thus the size of the scrolling area can be determined.
38
39 """
40 # The following is all that is needed to integrate the sizer and the
41 # scrolled window.
42 if not scroll_x: rate_x = 0
43 if not scroll_y: rate_y = 0
44
45 self.SetScrollRate(rate_x, rate_y)
46
47 self.GetSizer().SetVirtualSizeHints(self)
48 EVT_CHILD_FOCUS(self, self.OnChildFocus)
49 wxCallAfter(self.Scroll, 0, 0) # scroll back to top after initial events
50
51
52 def OnChildFocus(self, evt):
53 # If the child window that gets the focus is not visible,
54 # this handler will try to scroll enough to see it.
55 evt.Skip()
56 child = evt.GetWindow()
57
58 sppu_x, sppu_y = self.GetScrollPixelsPerUnit()
59 vs_x, vs_y = self.GetViewStart()
60 cpos = child.GetPosition()
61 csz = child.GetSize()
62 new_vs_x, new_vs_y = -1, -1
63
64 # is it before the left edge?
65 if cpos.x < 0 and sppu_x > 0:
66 new_vs_x = cpos.x / sppu_x
67
68 # is it above the top?
69 if cpos.y < 0 and sppu_y > 0:
70 new_vs_y = cpos.y / sppu_y
71
72
73 # is it past the right edge ?
74 if cpos.x + csz.width > self.GetClientSize().width and sppu_x > 0:
75 diff = (cpos.x + csz.width - self.GetClientSize().width) / sppu_x
76 new_vs_x = vs_x + diff + 1
77
78 # is it below the bottom ?
79 if cpos.y + csz.height > self.GetClientSize().height and sppu_y > 0:
80 diff = (cpos.y + csz.height - self.GetClientSize().height) / sppu_y
81 new_vs_y = vs_y + diff + 1
82
83 # if we need to adjust
84 if new_vs_x != -1 or new_vs_y != -1:
85 self.Scroll(new_vs_x, new_vs_y)