Package pyjamas :: Package ui :: Module ScrollPanel
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.ScrollPanel

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  from pyjamas import DOM 
 16  from pyjamas import Factory 
 17   
 18  from pyjamas.ui.SimplePanel import SimplePanel 
 19  from pyjamas.ui import Event 
20 21 -class ScrollPanel(SimplePanel):
22 23 _props = [("always", "Always show scroll bars", 24 "AlwaysShowScrollBars", None), 25 ("position", "Vertical Scroll Position", 26 "ScrollPosition", None), 27 ("horizPos", "Horizontal Scroll Position", 28 "HorizontalScrollPosition", None), 29 ] 30
31 - def __init__(self, child=None, **kwargs):
32 self.scrollListeners = [] 33 34 if child is not None: 35 kwargs['Widget'] = child 36 if not kwargs.has_key('AlwaysShowScrollBars'): 37 kwargs['AlwaysShowScrollBars'] = False 38 39 SimplePanel.__init__(self, **kwargs) 40 self.sinkEvents(Event.ONSCROLL)
41 42 @classmethod
43 - def _getProps(self):
44 return SimplePanel._getProps() + self._props
45 46 # next three functions are part of the standard Builder API for panels
47 - def addIndexedItem(self, index, item):
48 self.setWidget(item)
49
50 - def getWidgetIndex(self, index):
51 return 0
52
53 - def getIndexedChild(self, index):
54 return self.getWidget()
55
56 - def addScrollListener(self, listener):
57 self.scrollListeners.append(listener)
58
59 - def ensureVisible(self, item):
60 scroll = self.getElement() 61 element = item.getElement() 62 self.ensureVisibleImpl(scroll, element)
63
64 - def getScrollPosition(self):
65 return DOM.getIntAttribute(self.getElement(), "scrollTop")
66
68 return DOM.getIntAttribute(self.getElement(), "scrollLeft")
69
70 - def onBrowserEvent(self, event):
71 type = DOM.eventGetType(event) 72 if type == "scroll": 73 for listener in self.scrollListeners: 74 listener.onScroll(self, self.getHorizontalScrollPosition(), 75 self.getScrollPosition())
76
77 - def removeScrollListener(self, listener):
78 self.scrollListeners.remove(listener)
79
80 - def setAlwaysShowScrollBars(self, alwaysShow):
81 if alwaysShow: 82 style = "scroll" 83 else: 84 style = "auto" 85 DOM.setStyleAttribute(self.getElement(), "overflow", style)
86
87 - def setScrollPosition(self, position):
88 DOM.setIntAttribute(self.getElement(), "scrollTop", position)
89
90 - def setHorizontalScrollPosition(self, position):
91 DOM.setIntAttribute(self.getElement(), "scrollLeft", position)
92
93 - def ensureVisibleImpl(self, scroll, e):
94 if not e: 95 return 96 item = e 97 realOffset = 0 98 while item and (item != scroll): 99 realOffset += item.offsetTop 100 item = item.offsetParent 101 scroll.scrollTop = realOffset - scroll.offsetHeight / 2
102 103 Factory.registerClass('pyjamas.ui.ScrollPanel', 'ScrollPanel', ScrollPanel) 104