PyXR

c:\projects\bitpim\src \ fixedscrolledpanel.py



0001 """
0002 wx.ScrolledPanel with corrected behaviour.
0003 
0004 This file has been modified from the original and remains under the same
0005 license as the original.  (The original is in the wx distribution
0006 as lib/scrolledpanel.py.  The following changes were made:
0007 
0008  - Doesn't automatically scroll back to the top when SetupScrolling()
0009    is called
0010  - OnChildFocus is broken down into one method to get child focus
0011    window, and a second method to scroll to make it visible
0012  - MakeChildVisible (second method from above) now works correctly
0013    if the child is not a direct child of the panel
0014  - Changed to use 'import wx.' import style
0015 """
0016 ###
0017 ###  $Id: fixedscrolledpanel.py 911 2004-02-09 09:05:11Z rogerb $
0018 
0019 #----------------------------------------------------------------------------
0020 # Name:         wx.ScrolledPanel.py
0021 # Author:       Will Sadkin
0022 # Created:      03/21/2003
0023 # Copyright:    (c) 2003 by Will Sadkin
0024 # RCS-ID:       Id: scrolledpanel.py,v 1.1.2.5 2003/09/24 00:22:50 RD Exp 
0025 # License:      wx.Windows license
0026 #----------------------------------------------------------------------------
0027 #
0028 
0029 import wx
0030 
0031 
0032 class wxScrolledPanel( wx.ScrolledWindow ):
0033     """
0034 wxScrolledPanel fills a "hole" in the implementation of wx.ScrolledWindow,
0035 providing automatic scrollbar and scrolling behavior and the tab traversal
0036 management that wx.ScrolledWindow lacks.  This code was based on the original
0037 demo code showing how to do this, but is now available for general use
0038 as a proper class (and the demo is now converted to just use it.)
0039 """
0040     def __init__(self, parent, id=-1,
0041                  pos = wx.DefaultPosition, size = wx.DefaultSize,
0042                  style = wx.TAB_TRAVERSAL, name = "scrolledpanel"):
0043 
0044         # RB: id parameter wasn't being passed
0045         wx.ScrolledWindow.__init__(self, parent, id,
0046                                   pos=pos, size=size,
0047                                   style=style, name=name)
0048 
0049         wx.EVT_CHILD_FOCUS(self, self.OnChildFocus)
0050 
0051 
0052     def SetupScrolling(self, scroll_x=True, scroll_y=True, rate_x=20, rate_y=20):
0053         """
0054         This function sets up the event handling necessary to handle
0055         scrolling properly. It should be called within the __init__
0056         function of any class that is derived from wx.ScrolledPanel,
0057         once the controls on the panel have been constructed and
0058         thus the size of the scrolling area can be determined.
0059 
0060         """
0061         # The following is all that is needed to integrate the sizer and the
0062         # scrolled window.
0063         if not scroll_x: rate_x = 0
0064         if not scroll_y: rate_y = 0
0065 
0066         # Round up the virtual size to be a multiple of the scroll rate
0067         sizer = self.GetSizer()
0068         if sizer:
0069             w, h = sizer.GetMinSize()
0070 
0071             if rate_x:
0072                 w += rate_x - (w % rate_x)
0073             if rate_y:
0074                 h += rate_y - (h % rate_y)
0075             self.SetVirtualSize( (w, h) )
0076             self.SetVirtualSizeHints( w, h )
0077 
0078         self.SetScrollRate(rate_x, rate_y)
0079         # RB: line commented out
0080         ##wx.CallAfter(self.Scroll, 0, 0) # scroll back to top after initial events
0081 
0082 
0083     def OnChildFocus(self, evt):
0084         # If the child window that gets the focus is not visible,
0085         # this handler will try to scroll enough to see it.
0086         evt.Skip()
0087         child = evt.GetWindow()
0088         # RB: broke into two seperate methods
0089         self.MakeChildVisible(child)
0090 
0091 
0092     def MakeChildVisible(self, child):
0093         if child is self: # bizarrely this gets called
0094             return
0095         sppu_x, sppu_y = self.GetScrollPixelsPerUnit()
0096         vs_x, vs_y   = self.GetViewStart()
0097         new_vs_x, new_vs_y = -1, -1
0098 
0099         widget=child
0100         while widget.GetParent() is not self:
0101             widget=widget.GetParent()
0102         child=widget
0103 
0104         cpos = child.GetPosition()
0105         csz  = child.GetSize()
0106         
0107         # is it before the left edge?
0108         if cpos.x < 0 and sppu_x > 0:
0109             new_vs_x = vs_x + (cpos.x / sppu_x)
0110 
0111         # is it above the top?
0112         if cpos.y < 0 and sppu_y > 0:
0113             new_vs_y = vs_y + (cpos.y / sppu_y)
0114 
0115 
0116         # is it past the right edge ?
0117         if cpos.x + csz.width > self.GetClientSize().width and sppu_x > 0:
0118             diff = (cpos.x + csz.width - self.GetClientSize().width) / sppu_x
0119             new_vs_x = vs_x + diff + 1
0120 
0121         # is it below the bottom ?
0122         if cpos.y + csz.height > self.GetClientSize().height and sppu_y > 0:
0123             diff = (cpos.y + csz.height - self.GetClientSize().height) / sppu_y
0124             new_vs_y = vs_y + diff + 1
0125 
0126         # if we need to adjust
0127         if new_vs_x != -1 or new_vs_y != -1:
0128             self.Scroll(new_vs_x, new_vs_y)
0129 

Generated by PyXR 0.9.4