Module fixedscrolledpanel
[hide private]
[frames] | no frames]

Source Code for Module fixedscrolledpanel

  1  """ 
  2  wx.ScrolledPanel with corrected behaviour. 
  3   
  4  This file has been modified from the original and remains under the same 
  5  license as the original.  (The original is in the wx distribution 
  6  as lib/scrolledpanel.py.  The following changes were made: 
  7   
  8   - Doesn't automatically scroll back to the top when SetupScrolling() 
  9     is called 
 10   - OnChildFocus is broken down into one method to get child focus 
 11     window, and a second method to scroll to make it visible 
 12   - MakeChildVisible (second method from above) now works correctly 
 13     if the child is not a direct child of the panel 
 14   - Changed to use 'import wx.' import style 
 15  """ 
 16  ### 
 17  ###  $Id: fixedscrolledpanel.py 911 2004-02-09 09:05:11Z rogerb $ 
 18   
 19  #---------------------------------------------------------------------------- 
 20  # Name:         wx.ScrolledPanel.py 
 21  # Author:       Will Sadkin 
 22  # Created:      03/21/2003 
 23  # Copyright:    (c) 2003 by Will Sadkin 
 24  # RCS-ID:       Id: scrolledpanel.py,v 1.1.2.5 2003/09/24 00:22:50 RD Exp  
 25  # License:      wx.Windows license 
 26  #---------------------------------------------------------------------------- 
 27  # 
 28   
 29  import wx 
 30   
 31   
32 -class wxScrolledPanel( wx.ScrolledWindow ):
33 """ 34 wxScrolledPanel fills a "hole" in the implementation of wx.ScrolledWindow, 35 providing automatic scrollbar and scrolling behavior and the tab traversal 36 management that wx.ScrolledWindow lacks. This code was based on the original 37 demo code showing how to do this, but is now available for general use 38 as a proper class (and the demo is now converted to just use it.) 39 """
40 - def __init__(self, parent, id=-1, 41 pos = wx.DefaultPosition, size = wx.DefaultSize, 42 style = wx.TAB_TRAVERSAL, name = "scrolledpanel"):
43 44 # RB: id parameter wasn't being passed 45 wx.ScrolledWindow.__init__(self, parent, id, 46 pos=pos, size=size, 47 style=style, name=name) 48 49 wx.EVT_CHILD_FOCUS(self, self.OnChildFocus)
50 51
52 - def SetupScrolling(self, scroll_x=True, scroll_y=True, rate_x=20, rate_y=20):
53 """ 54 This function sets up the event handling necessary to handle 55 scrolling properly. It should be called within the __init__ 56 function of any class that is derived from wx.ScrolledPanel, 57 once the controls on the panel have been constructed and 58 thus the size of the scrolling area can be determined. 59 60 """ 61 # The following is all that is needed to integrate the sizer and the 62 # scrolled window. 63 if not scroll_x: rate_x = 0 64 if not scroll_y: rate_y = 0 65 66 # Round up the virtual size to be a multiple of the scroll rate 67 sizer = self.GetSizer() 68 if sizer: 69 w, h = sizer.GetMinSize() 70 71 if rate_x: 72 w += rate_x - (w % rate_x) 73 if rate_y: 74 h += rate_y - (h % rate_y) 75 self.SetVirtualSize( (w, h) ) 76 self.SetVirtualSizeHints( w, h ) 77 78 self.SetScrollRate(rate_x, rate_y)
79 # RB: line commented out 80 ##wx.CallAfter(self.Scroll, 0, 0) # scroll back to top after initial events 81 82
83 - def OnChildFocus(self, evt):
84 # If the child window that gets the focus is not visible, 85 # this handler will try to scroll enough to see it. 86 evt.Skip() 87 child = evt.GetWindow() 88 # RB: broke into two seperate methods 89 self.MakeChildVisible(child)
90 91
92 - def MakeChildVisible(self, child):
93 if child is self: # bizarrely this gets called 94 return 95 sppu_x, sppu_y = self.GetScrollPixelsPerUnit() 96 vs_x, vs_y = self.GetViewStart() 97 new_vs_x, new_vs_y = -1, -1 98 99 widget=child 100 while widget.GetParent() is not self: 101 widget=widget.GetParent() 102 child=widget 103 104 cpos = child.GetPosition() 105 csz = child.GetSize() 106 107 # is it before the left edge? 108 if cpos.x < 0 and sppu_x > 0: 109 new_vs_x = vs_x + (cpos.x / sppu_x) 110 111 # is it above the top? 112 if cpos.y < 0 and sppu_y > 0: 113 new_vs_y = vs_y + (cpos.y / sppu_y) 114 115 116 # is it past the right edge ? 117 if cpos.x + csz.width > self.GetClientSize().width and sppu_x > 0: 118 diff = (cpos.x + csz.width - self.GetClientSize().width) / sppu_x 119 new_vs_x = vs_x + diff + 1 120 121 # is it below the bottom ? 122 if cpos.y + csz.height > self.GetClientSize().height and sppu_y > 0: 123 diff = (cpos.y + csz.height - self.GetClientSize().height) / sppu_y 124 new_vs_y = vs_y + diff + 1 125 126 # if we need to adjust 127 if new_vs_x != -1 or new_vs_y != -1: 128 self.Scroll(new_vs_x, new_vs_y)
129