]>
Commit | Line | Data |
---|---|---|
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 | EVT_CHILD_FOCUS(self, self.OnChildFocus) | |
31 | ||
32 | ||
33 | def SetupScrolling(self, scroll_x=True, scroll_y=True, rate_x=20, rate_y=20): | |
34 | """ | |
35 | This function sets up the event handling necessary to handle | |
36 | scrolling properly. It should be called within the __init__ | |
37 | function of any class that is derived from wxScrolledPanel, | |
38 | once the controls on the panel have been constructed and | |
39 | thus the size of the scrolling area can be determined. | |
40 | ||
41 | """ | |
42 | # The following is all that is needed to integrate the sizer and the | |
43 | # scrolled window. | |
44 | if not scroll_x: rate_x = 0 | |
45 | if not scroll_y: rate_y = 0 | |
46 | ||
47 | # Round up the virtual size to be a multiple of the scroll rate | |
48 | sizer = self.GetSizer() | |
49 | if sizer: | |
50 | w, h = sizer.GetMinSize() | |
51 | if rate_x: | |
52 | w += rate_x - (w % rate_x) | |
53 | if rate_y: | |
54 | h += rate_y - (h % rate_y) | |
55 | self.SetVirtualSize( (w, h) ) | |
56 | self.SetVirtualSizeHints( w, h ) | |
57 | ||
58 | self.SetScrollRate(rate_x, rate_y) | |
59 | wxCallAfter(self.Scroll, 0, 0) # scroll back to top after initial events | |
60 | ||
61 | ||
62 | def OnChildFocus(self, evt): | |
63 | # If the child window that gets the focus is not visible, | |
64 | # this handler will try to scroll enough to see it. | |
65 | evt.Skip() | |
66 | child = evt.GetWindow() | |
67 | ||
68 | sppu_x, sppu_y = self.GetScrollPixelsPerUnit() | |
69 | vs_x, vs_y = self.GetViewStart() | |
70 | cpos = child.GetPosition() | |
71 | csz = child.GetSize() | |
72 | new_vs_x, new_vs_y = -1, -1 | |
73 | ||
74 | # is it before the left edge? | |
75 | if cpos.x < 0 and sppu_x > 0: | |
76 | new_vs_x = vs_x + (cpos.x / sppu_x) | |
77 | ||
78 | # is it above the top? | |
79 | if cpos.y < 0 and sppu_y > 0: | |
80 | new_vs_y = vs_y + (cpos.y / sppu_y) | |
81 | ||
82 | ||
83 | # is it past the right edge ? | |
84 | if cpos.x + csz.width > self.GetClientSize().width and sppu_x > 0: | |
85 | diff = (cpos.x + csz.width - self.GetClientSize().width) / sppu_x | |
86 | new_vs_x = vs_x + diff + 1 | |
87 | ||
88 | # is it below the bottom ? | |
89 | if cpos.y + csz.height > self.GetClientSize().height and sppu_y > 0: | |
90 | diff = (cpos.y + csz.height - self.GetClientSize().height) / sppu_y | |
91 | new_vs_y = vs_y + diff + 1 | |
92 | ||
93 | # if we need to adjust | |
94 | if new_vs_x != -1 or new_vs_y != -1: | |
95 | self.Scroll(new_vs_x, new_vs_y) |