]>
Commit | Line | Data |
---|---|---|
d14a1e28 | 1 | #---------------------------------------------------------------------------- |
d4b73b1b | 2 | # Name: scrolledpanel.py |
d14a1e28 RD |
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 | #---------------------------------------------------------------------------- | |
b881fc78 RD |
9 | # 12/11/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
10 | # | |
11 | # o 2.5 compatability update. | |
d14a1e28 | 12 | # |
d4b73b1b RD |
13 | # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
14 | # | |
15 | # o wxScrolledPanel -> ScrolledPanel | |
16 | # | |
1fded56b | 17 | |
b881fc78 | 18 | import wx |
1fded56b | 19 | |
d14a1e28 | 20 | |
d4b73b1b | 21 | class ScrolledPanel( wx.ScrolledWindow ): |
b881fc78 | 22 | """\ |
d4b73b1b | 23 | ScrolledPanel fills a "hole" in the implementation of wx.ScrolledWindow, |
d14a1e28 RD |
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, | |
b881fc78 RD |
30 | pos = wx.DefaultPosition, size = wx.DefaultSize, |
31 | style = wx.TAB_TRAVERSAL, name = "scrolledpanel"): | |
d14a1e28 | 32 | |
b881fc78 | 33 | wx.ScrolledWindow.__init__(self, parent, -1, |
d14a1e28 RD |
34 | pos=pos, size=size, |
35 | style=style, name=name) | |
36 | ||
b881fc78 | 37 | self.Bind(wx.EVT_CHILD_FOCUS, self.OnChildFocus) |
d14a1e28 RD |
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__ | |
d4b73b1b | 44 | function of any class that is derived from ScrolledPanel, |
d14a1e28 RD |
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) | |
b881fc78 | 66 | wx.CallAfter(self.Scroll, 0, 0) # scroll back to top after initial events |
d14a1e28 RD |
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 | ||
b881fc78 | 89 | clntsz = self.GetClientSize() |
d14a1e28 RD |
90 | |
91 | # is it past the right edge ? | |
b881fc78 RD |
92 | if cpos.x + csz.width > clntsz.width and sppu_x > 0: |
93 | diff = (cpos.x + csz.width - clntsz.width) / sppu_x | |
d14a1e28 RD |
94 | new_vs_x = vs_x + diff + 1 |
95 | ||
96 | # is it below the bottom ? | |
b881fc78 RD |
97 | if cpos.y + csz.height > clntsz.height and sppu_y > 0: |
98 | diff = (cpos.y + csz.height - clntsz.height) / sppu_y | |
d14a1e28 RD |
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) |