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

Source Code for Module pyjamas.ui.splitpanel

  1  """ 
  2  /* 
  3   * Copyright 2008 Google Inc. 
  4   *  
  5   * Licensed under the Apache License, Version 2.0 (the "License") you may not 
  6   * use this file except in compliance with the License. You may obtain a copy of 
  7   * the License at 
  8   *  
  9   * http:#www.apache.org/licenses/LICENSE-2.0 
 10   *  
 11   * Unless required by applicable law or agreed to in writing, software 
 12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 14   * License for the specific language governing permissions and limitations under 
 15   * the License. 
 16   */ 
 17  """ 
 18  from pyjamas.ui.Panel import Panel 
 19  from pyjamas.ui import Event 
 20   
 21  from pyjamas import DOM 
 22   
23 -class SplitPanel(Panel):
24 """ Abstract base class for {@link HorizontalSplitPanel} and 25 {@link VerticalSplitPanel}. 26 """ 27
28 - def __init__(self, mainElem, splitElem, headElem, tailElem, **kwargs):
29 """ Initializes the split panel. 30 @param mainElem the root element for the split panel 31 @param splitElem the element that acts as the splitter 32 @param headElem the element to contain the top or left most widget 33 @param tailElem the element to contain the bottom or right most widget 34 """ 35 36 self.widgets = [None, None] 37 self.elements = [headElem, tailElem] 38 self.isResizing = False 39 40 self.setElement(mainElem) 41 self.splitElem = splitElem 42 43 Panel.__init__(self, **kwargs) 44 45 self.sinkEvents(Event.MOUSEEVENTS)
46
47 - def addAbsolutePositoning(self, elem):
48 """ Sets an elements positioning to absolute. 49 """ 50 DOM.setStyleAttribute(elem, "position", "absolute")
51
52 - def addClipping(self, elem):
53 """ Adds clipping to an element. 54 """ 55 DOM.setStyleAttribute(elem, "overflow", "hidden")
56
57 - def addScrolling(self, elem):
58 """ Adds as-needed scrolling to an element. 59 """ 60 DOM.setStyleAttribute(elem, "overflow", "auto")
61
62 - def expandToFitParentUsingCssOffsets(self, elem):
63 """ Sizes and element to consume the full area of its parent 64 using the CSS properties left, right, top, and 65 bottom. This method is used for all browsers except IE6/7. 66 """ 67 zeroSize = "0px" 68 69 self.addAbsolutePositoning(elem) 70 self.setLeft(elem, zeroSize) 71 self.setRight(elem, zeroSize) 72 self.setTop(elem, zeroSize) 73 self.setBottom(elem, zeroSize)
74
75 - def expandToFitParentUsingPercentages(self, elem):
76 """ Sizes an element to consume the full areas of its parent 77 using 100% width and height. This method is used on IE6/7 78 where CSS offsets don't work reliably. 79 """ 80 zeroSize = "0px" 81 fullSize = "100%" 82 83 self.addAbsolutePositoning(elem) 84 self.setTop(elem, zeroSize) 85 self.setLeft(elem, zeroSize) 86 self.setElemWidth(elem, fullSize) 87 self.setElemHeight(elem, fullSize)
88
89 - def getOffsetHeight(self, elem):
90 """ Returns the offsetHeight element property. 91 """ 92 return DOM.getIntAttribute(elem, "offsetHeight")
93
94 - def getOffsetWidth(self, elem):
95 """ Returns the offsetWidth element property. 96 """ 97 return DOM.getIntAttribute(elem, "offsetWidth")
98
99 - def preventBoxStyles(self, elem):
100 """ Adds zero or none CSS values for padding, margin and 101 border to prevent stylesheet overrides. Returns the 102 element for convenience to support builder pattern. 103 """ 104 DOM.setIntStyleAttribute(elem, "padding", 0) 105 DOM.setIntStyleAttribute(elem, "margin", 0) 106 DOM.setStyleAttribute(elem, "border", "none") 107 return elem
108
109 - def setBottom(self, elem, size):
110 """ Convenience method to set bottom offset of an element. 111 """ 112 DOM.setStyleAttribute(elem, "bottom", size)
113
114 - def setElemHeight(self, elem, height):
115 """ Convenience method to set the height of an element. 116 """ 117 DOM.setStyleAttribute(elem, "height", height)
118
119 - def setLeft(self, elem, left):
120 """ Convenience method to set the left offset of an element. 121 """ 122 DOM.setStyleAttribute(elem, "left", left)
123
124 - def setRight(self, elem, right):
125 """ Convenience method to set the right offset of an element. 126 """ 127 DOM.setStyleAttribute(elem, "right", right)
128
129 - def setTop(self, elem, top):
130 """ Convenience method to set the top offset of an element. 131 """ 132 DOM.setStyleAttribute(elem, "top", top)
133
134 - def setElemWidth(self, elem, width):
135 """ Convenience method to set the width of an element. 136 """ 137 DOM.setStyleAttribute(elem, "width", width)
138
139 - def add(self, w):
140 if self.getWidget(0) is None: 141 self.setWidget(0, w) 142 elif self.getWidget(1) is None: 143 self.setWidget(1, w)
144 #else: 145 # raise IllegalStateException("A Splitter can only contain two Widgets.") 146
147 - def isResizing(self):
148 """ Indicates whether the split panel is being resized. 149 150 @return <code>True</code> if the user is dragging the splitter, 151 <code>False</code> otherwise 152 """ 153 return self.isResizing
154
155 - def __iter__(self):
156 return self.widgets.__iter__()
157
158 - def onBrowserEvent(self, event):
159 typ = DOM.eventGetType(event) 160 161 if typ == "mousedown": 162 target = DOM.eventGetTarget(event) 163 if DOM.isOrHasChild(self.splitElem, target): 164 self.startResizingFrom(DOM.eventGetClientX(event) - 165 self.getAbsoluteLeft(), 166 DOM.eventGetClientY(event) - self.getAbsoluteTop()) 167 DOM.setCapture(self.getElement()) 168 DOM.eventPreventDefault(event) 169 170 elif typ == "mouseup": 171 DOM.releaseCapture(self.getElement()) 172 self.stopResizing() 173 174 elif typ == 'mousemove': 175 if self.isResizing: 176 #assert DOM.getCaptureElement() is not None 177 self.onSplitterResize(DOM.eventGetClientX(event) - 178 self.getAbsoluteLeft(), 179 DOM.eventGetClientY(event) - self.getAbsoluteTop()) 180 DOM.eventPreventDefault(event)
181
182 - def remove(self, widget):
183 if widgets[0] == widget: 184 setWidget(0, None) 185 return True 186 elif widgets[1] == widget: 187 setWidget(1, None) 188 return True 189 return False
190
191 - def setSplitPosition(self, size):
192 """ Moves the position of the splitter. 193 @param size the new size of the left region in CSS units 194 (e.g. "10px", "1em") 195 """ 196 pass
197
198 - def getWidgetElement(self, index):
199 """ Gets the content element for the given index. 200 @param index the index of the element, only 0 and 1 are valid. 201 @return the element 202 """ 203 return self.elements[index]
204
205 - def getSplitElement(self):
206 """ Gets the element that is acting as the splitter. 207 @return the element 208 """ 209 return self.splitElem
210
211 - def getWidget(self, index):
212 """ Gets one of the contained widgets. 213 @param index the index of the widget, only 0 and 1 are valid. 214 @return the widget 215 """ 216 return self.widgets[index]
217
218 - def setWidget(self, index, w):
219 """ Sets one of the contained widgets. 220 @param index the index, only 0 and 1 are valid 221 @param w the widget 222 """ 223 oldWidget = self.widgets[index] 224 225 if oldWidget == w: 226 return 227 228 if w is not None: 229 w.removeFromParent() 230 231 # Remove the old child. 232 if oldWidget is not None: 233 # Orphan old. 234 self.disown(oldWidget) 235 # Physical detach old. 236 #DOM.removeChild(self.elements[index], oldWidget.getElement()) 237 238 # Logical detach old / attach new. 239 self.widgets[index] = w 240 241 if w is not None: 242 # Physical attach new. 243 DOM.appendChild(self.elements[index], w.getElement()) 244 245 # Adopt new. 246 self.adopt(w, None)
247
248 - def onSplitterResize(self, x, y):
249 """ Called on each mouse drag event as the user is dragging 250 the splitter. 251 @param x the x coord of the mouse relative to the panel's extent 252 @param y the y coord of the mosue relative to the panel's extent 253 """ 254 pass
255
256 - def onSplitterResizeStarted(self, x, y):
257 """ Called when the user starts dragging the splitter. 258 @param x the x coord of the mouse relative to the panel's extent 259 @param y the y coord of the mouse relative to the panel's extent 260 """
261
262 - def startResizingFrom(self, x, y):
263 self.isResizing = True 264 self.onSplitterResizeStarted(x, y)
265
266 - def stopResizing(self):
267 self.isResizing = False
268