]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/scrolledpanel.py
1 #----------------------------------------------------------------------------
2 # Name: wxScrolledPanel.py
5 # Copyright: (c) 2003 by Will Sadkin
7 # License: wxWindows license
8 #----------------------------------------------------------------------------
11 from wxPython
.wx
import *
14 class wxScrolledPanel( wxScrolledWindow
):
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.)
22 def __init__(self
, parent
, id=-1,
23 pos
= wxDefaultPosition
, size
= wxDefaultSize
,
24 style
= wxTAB_TRAVERSAL
, name
= "scrolledpanel"):
26 wxScrolledWindow
.__init
__(self
, parent
, -1,
28 style
=style
, name
=name
)
30 EVT_CHILD_FOCUS(self
, self
.OnChildFocus
)
33 def SetupScrolling(self
, scroll_x
=True, scroll_y
=True, rate_x
=20, rate_y
=20):
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.
42 # The following is all that is needed to integrate the sizer and the
44 if not scroll_x
: rate_x
= 0
45 if not scroll_y
: rate_y
= 0
47 # Round up the virtual size to be a multiple of the scroll rate
48 sizer
= self
.GetSizer()
50 w
, h
= sizer
.GetMinSize()
52 w
+= rate_x
- (w
% rate_x
)
54 h
+= rate_y
- (h
% rate_y
)
55 self
.SetVirtualSize( (w
, h
) )
56 self
.SetVirtualSizeHints( w
, h
)
58 self
.SetScrollRate(rate_x
, rate_y
)
59 wxCallAfter(self
.Scroll
, 0, 0) # scroll back to top after initial events
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.
66 child
= evt
.GetWindow()
68 sppu_x
, sppu_y
= self
.GetScrollPixelsPerUnit()
69 vs_x
, vs_y
= self
.GetViewStart()
70 cpos
= child
.GetPosition()
72 new_vs_x
, new_vs_y
= -1, -1
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
)
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
)
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
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
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
)