]>
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 #----------------------------------------------------------------------------
9 # 12/11/2003 - Jeff Grimmett (grimmtooth@softhome.net)
11 # o 2.5 compatability update.
17 class wxScrolledPanel( wx
.ScrolledWindow
):
19 wxScrolledPanel fills a "hole" in the implementation of wxScrolledWindow,
20 providing automatic scrollbar and scrolling behavior and the tab traversal
21 management that wxScrolledWindow lacks. This code was based on the original
22 demo code showing how to do this, but is now available for general use
23 as a proper class (and the demo is now converted to just use it.)
25 def __init__(self
, parent
, id=-1,
26 pos
= wx
.DefaultPosition
, size
= wx
.DefaultSize
,
27 style
= wx
.TAB_TRAVERSAL
, name
= "scrolledpanel"):
29 wx
.ScrolledWindow
.__init
__(self
, parent
, -1,
31 style
=style
, name
=name
)
33 self
.Bind(wx
.EVT_CHILD_FOCUS
, self
.OnChildFocus
)
36 def SetupScrolling(self
, scroll_x
=True, scroll_y
=True, rate_x
=20, rate_y
=20):
38 This function sets up the event handling necessary to handle
39 scrolling properly. It should be called within the __init__
40 function of any class that is derived from wxScrolledPanel,
41 once the controls on the panel have been constructed and
42 thus the size of the scrolling area can be determined.
45 # The following is all that is needed to integrate the sizer and the
47 if not scroll_x
: rate_x
= 0
48 if not scroll_y
: rate_y
= 0
50 # Round up the virtual size to be a multiple of the scroll rate
51 sizer
= self
.GetSizer()
53 w
, h
= sizer
.GetMinSize()
55 w
+= rate_x
- (w
% rate_x
)
57 h
+= rate_y
- (h
% rate_y
)
58 self
.SetVirtualSize( (w
, h
) )
59 self
.SetVirtualSizeHints( w
, h
)
61 self
.SetScrollRate(rate_x
, rate_y
)
62 wx
.CallAfter(self
.Scroll
, 0, 0) # scroll back to top after initial events
65 def OnChildFocus(self
, evt
):
66 # If the child window that gets the focus is not visible,
67 # this handler will try to scroll enough to see it.
69 child
= evt
.GetWindow()
71 sppu_x
, sppu_y
= self
.GetScrollPixelsPerUnit()
72 vs_x
, vs_y
= self
.GetViewStart()
73 cpos
= child
.GetPosition()
75 new_vs_x
, new_vs_y
= -1, -1
77 # is it before the left edge?
78 if cpos
.x
< 0 and sppu_x
> 0:
79 new_vs_x
= vs_x
+ (cpos
.x
/ sppu_x
)
81 # is it above the top?
82 if cpos
.y
< 0 and sppu_y
> 0:
83 new_vs_y
= vs_y
+ (cpos
.y
/ sppu_y
)
85 clntsz
= self
.GetClientSize()
87 # is it past the right edge ?
88 if cpos
.x
+ csz
.width
> clntsz
.width
and sppu_x
> 0:
89 diff
= (cpos
.x
+ csz
.width
- clntsz
.width
) / sppu_x
90 new_vs_x
= vs_x
+ diff
+ 1
92 # is it below the bottom ?
93 if cpos
.y
+ csz
.height
> clntsz
.height
and sppu_y
> 0:
94 diff
= (cpos
.y
+ csz
.height
- clntsz
.height
) / sppu_y
95 new_vs_y
= vs_y
+ diff
+ 1
97 # if we need to adjust
98 if new_vs_x
!= -1 or new_vs_y
!= -1:
99 self
.Scroll(new_vs_x
, new_vs_y
)