]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/scrolledpanel.py
f9640c03b2c9963273385402f249b8444bb4671f
   1 #---------------------------------------------------------------------------- 
   2 # Name:         scrolledpanel.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. 
  13 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
  15 # o wxScrolledPanel -> ScrolledPanel 
  21 class ScrolledPanel( wx
.ScrolledWindow 
): 
  23 ScrolledPanel fills a "hole" in the implementation of wx.ScrolledWindow, 
  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.) 
  29     def __init__(self
, parent
, id=-1, 
  30                  pos 
= wx
.DefaultPosition
, size 
= wx
.DefaultSize
, 
  31                  style 
= wx
.TAB_TRAVERSAL
, name 
= "scrolledpanel"): 
  33         wx
.ScrolledWindow
.__init
__(self
, parent
, -1, 
  35                                   style
=style
, name
=name
) 
  37         self
.Bind(wx
.EVT_CHILD_FOCUS
, self
.OnChildFocus
) 
  40     def SetupScrolling(self
, scroll_x
=True, scroll_y
=True, rate_x
=20, rate_y
=20): 
  42         This function sets up the event handling necessary to handle 
  43         scrolling properly. It should be called within the __init__ 
  44         function of any class that is derived from ScrolledPanel, 
  45         once the controls on the panel have been constructed and 
  46         thus the size of the scrolling area can be determined. 
  49         # The following is all that is needed to integrate the sizer and the 
  51         if not scroll_x
: rate_x 
= 0 
  52         if not scroll_y
: rate_y 
= 0 
  54         # Round up the virtual size to be a multiple of the scroll rate 
  55         sizer 
= self
.GetSizer() 
  57             w
, h 
= sizer
.GetMinSize() 
  59                 w 
+= rate_x 
- (w 
% rate_x
) 
  61                 h 
+= rate_y 
- (h 
% rate_y
) 
  62             self
.SetVirtualSize( (w
, h
) ) 
  63             self
.SetVirtualSizeHints( w
, h 
) 
  65         self
.SetScrollRate(rate_x
, rate_y
) 
  66         wx
.CallAfter(self
.Scroll
, 0, 0) # scroll back to top after initial events 
  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. 
  73         child 
= evt
.GetWindow() 
  75         sppu_x
, sppu_y 
= self
.GetScrollPixelsPerUnit() 
  76         vs_x
, vs_y   
= self
.GetViewStart() 
  77         cpos 
= child
.GetPosition() 
  79         new_vs_x
, new_vs_y 
= -1, -1 
  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
) 
  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
) 
  89         clntsz 
= self
.GetClientSize() 
  91         # is it past the right edge ? 
  92         if cpos
.x 
+ csz
.width 
> clntsz
.width 
and sppu_x 
> 0: 
  93             diff 
= (cpos
.x 
+ csz
.width 
- clntsz
.width
) / sppu_x
 
  94             new_vs_x 
= vs_x 
+ diff 
+ 1 
  96         # is it below the bottom ? 
  97         if cpos
.y 
+ csz
.height 
> clntsz
.height 
and sppu_y 
> 0: 
  98             diff 
= (cpos
.y 
+ csz
.height 
- clntsz
.height
) / sppu_y
 
  99             new_vs_y 
= vs_y 
+ diff 
+ 1 
 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
)