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

Source Code for Module pyjamas.ui.UIObject

  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  from pyjamas import Window 
 18  from pyjamas.ui import Applier 
 19   
20 -def setStyleName(element, style, add):
21 22 oldStyle = DOM.getAttribute(element, "className") 23 if oldStyle is None: 24 oldStyle = "" 25 idx = oldStyle.find(style) 26 27 # Calculate matching index 28 lastPos = len(oldStyle) 29 while idx != -1: 30 if idx == 0 or (oldStyle[idx - 1] == " "): 31 last = idx + len(style) 32 if (last == lastPos) or ((last < lastPos) and (oldStyle[last] == " ")): 33 break 34 idx = oldStyle.find(style, idx + 1) 35 36 if add: 37 if idx == -1: 38 DOM.setAttribute(element, "className", oldStyle + " " + style) 39 else: 40 if idx != -1: 41 if idx == 0: 42 begin = '' 43 else: 44 begin = oldStyle[:idx-1] 45 end = oldStyle[idx + len(style):] 46 DOM.setAttribute(element, "className", begin + end)
47
48 -class UIObject(Applier):
49
50 - def __init__(self, **kwargs):
51 # do not initialise element, here, to None, whatever you do. 52 # there are circumstances where UIObject.__init__ is the last 53 # thing that is done in derived classes, where self.setElement 54 # will _already_ have been called. 55 Applier.__init__(self, **kwargs)
56
57 - def getAbsoluteLeft(self):
58 return DOM.getAbsoluteLeft(self.getElement())
59
60 - def getAbsoluteTop(self):
61 return DOM.getAbsoluteTop(self.getElement())
62
63 - def getElement(self):
64 """Get the DOM element associated with the UIObject, if any""" 65 return self.element
66
67 - def getOffsetHeight(self):
68 return DOM.getIntAttribute(self.element, "offsetHeight")
69
70 - def getOffsetWidth(self):
71 return DOM.getIntAttribute(self.element, "offsetWidth")
72
73 - def getStyleName(self):
74 return DOM.getAttribute(self.element, "className")
75
76 - def getStylePrimaryName(self):
77 """Return with the first className if there are multiples""" 78 fullClassName = self.getStyleName() 79 if fullClassName: return fullClassName.split()[0]
80
81 - def getTitle(self):
82 return DOM.getAttribute(self.element, "title")
83
84 - def setElement(self, element):
85 """Set the DOM element associated with the UIObject.""" 86 self.element = element
87
88 - def setHeight(self, height):
89 """Set the height of the element associated with this UIObject. The 90 value should be given as a CSS value, such as 100px, 30%, or 50pi""" 91 DOM.setStyleAttribute(self.element, "height", str(height))
92
93 - def getHeight(self):
94 return DOM.getStyleAttribute(self.element, "height")
95
96 - def setPixelSize(self, width, height):
97 """Set the width and height of the element associated with this UIObject 98 in pixels. Width and height should be numbers.""" 99 if width >= 0: 100 self.setWidth("%dpx" % width) 101 if height >= 0: 102 self.setHeight("%dpx" % height)
103
104 - def setSize(self, width, height):
105 """Set the width and height of the element associated with this UIObject. The 106 values should be given as a CSS value, such as 100px, 30%, or 50pi""" 107 self.setWidth(width) 108 self.setHeight(height)
109
110 - def addStyleName(self, style):
111 """Append a style to the element associated with this UIObject. This is 112 a CSS class name. It will be added after any already-assigned CSS class for 113 the element.""" 114 self.setStyleName(self.element, style, True)
115
116 - def addStyleDependentName(self, styleSuffix):
117 """Adds a secondary or dependent style name to this element. 118 For example if the primary stylename is gwt-TextBox, 119 self.addStyleDependentName("readonly") will return gwt-TextBox-readonly.""" 120 self.addStyleName(self.getStylePrimaryName()+"-"+styleSuffix)
121
122 - def removeStyleName(self, style):
123 """Remove a style from the element associated with this UIObject. This is 124 a CSS class name.""" 125 self.setStyleName(self.element, style, False)
126
127 - def removeStyleDependentName(self, styleSuffix):
128 """Remove a dependent style name by specifying the style name's suffix.""" 129 self.removeStyleName(self.getStylePrimaryName()+"-"+styleSuffix)
130 131 # also callable as: setStyleName(self, style)
132 - def setStyleName(self, element, style=None, add=True):
133 """When called with a single argument, this replaces all the CSS classes 134 associated with this UIObject's element with the given parameter. Otherwise, 135 this is assumed to be a worker function for addStyleName and removeStyleName.""" 136 # emulate setStyleName(self, style) 137 if style is None: 138 style = element 139 DOM.setAttribute(self.element, "className", style) 140 return 141 setStyleName(element, style, add)
142
143 - def setTitle(self, title):
144 DOM.setAttribute(self.element, "title", title)
145
146 - def setWidth(self, width):
147 """Set the width of the element associated with this UIObject. The 148 value should be given as a CSS value, such as 100px, 30%, or 50pi""" 149 DOM.setStyleAttribute(self.element, "width", str(width))
150
151 - def getWidth(self):
152 return DOM.getStyleAttribute(self.element, "width")
153
154 - def sinkEvents(self, eventBitsToAdd):
155 """Request that the given events be delivered to the event handler for this 156 element. The event bits passed are added (using inclusive OR) to the events 157 already "sunk" for the element associated with the UIObject. The event bits 158 are a combination of values from class L{Event}.""" 159 if self.element: 160 DOM.sinkEvents(self.getElement(), eventBitsToAdd | DOM.getEventsSunk(self.getElement()))
161
162 - def setzIndex(self, index):
163 DOM.setIntStyleAttribute(self.element, "zIndex", index)
164
165 - def isVisible(self, element=None):
166 """Determine whether this element is currently visible, by checking 167 the CSS property 'display' 168 """ 169 if not element: 170 element = self.element 171 try: # yuk! 172 return element.style.display != "none" 173 except AttributeError: # not been set (yet?) 174 return True
175 176 # also callable as: setVisible(visible)
177 - def setVisible(self, element, visible=None):
178 """Set whether this element is visible or not. If a single parameter is 179 given, the self.element is used. This modifies the CSS property 'display', 180 which means that an invisible element not only is not drawn, but doesn't 181 occupy any space on the page.""" 182 if visible is None: 183 visible = element 184 element = self.element 185 186 if visible: 187 DOM.setStyleAttribute(element, 'display', "") 188 else: 189 DOM.setStyleAttribute(element, 'display', "none")
190
191 - def unsinkEvents(self, eventBitsToRemove):
192 """Reverse the operation of sinkEvents. See L{UIObject.sinkevents}.""" 193 DOM.sinkEvents(self.getElement(), ~eventBitsToRemove & DOM.getEventsSunk(self.getElement()))
194 195 Factory.registerClass('pyjamas.ui.UIObject', UIObject) 196