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

Source Code for Module library.pyjamas.ui.GlassWidget

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # Copyright (C) 2010 Serge Tarkovski <serge.tarkovski@gmail.com> 
  4  # 
  5  # Licensed under the Apache License, Version 2.0 (the "License"); 
  6  # you may not use this file except in compliance with the License. 
  7  # You may obtain a copy of 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, 
 13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 14  # See the License for the specific language governing permissions and 
 15  # limitations under the License. 
 16  import pyjd 
 17  from pyjamas import DOM 
 18  from pyjamas import Window 
 19  from pyjamas import Factory 
 20  from __pyjamas__ import JS, doc 
 21  from pyjamas.ui.SimplePanel import SimplePanel 
 22  from pyjamas.ui.Widget import Widget 
 23  from pyjamas.ui.MouseListener import MouseHandler 
 24  from pyjamas.ui.RootPanel import RootPanel 
 25   
 26  mousecapturer = None 
 27   
 28   
29 -def getMouseCapturer(**kwargs):
30 global mousecapturer 31 if mousecapturer is None: 32 mousecapturer = GlassWidget(**kwargs) 33 # if mousecapturer has been overloaded with something 34 # other than a GlassWidget (as in IE override) 35 # just return None 36 elif not isinstance(mousecapturer, GlassWidget): 37 return None 38 return mousecapturer
39 40
41 -def show(mousetarget, **kwargs):
42 global mousecapturer 43 mc = getMouseCapturer(**kwargs) 44 mc.mousetarget = mousetarget 45 if isinstance(mousetarget, MouseHandler): 46 mc.mousehandler = True 47 mc.show()
48 49
50 -def hide():
51 global mousecapturer 52 mousecapturer.hide()
53 54
55 -class GlassWidget(Widget, MouseHandler):
56 - def __init__(self, **kwargs):
57 58 self.glassListeners = [] 59 self.showing = False 60 self.mousehandler = False 61 62 if not 'StyleName' in kwargs: 63 kwargs['StyleName'] = "gwt-GlassWidget" 64 65 if 'Element' in kwargs: 66 element = kwargs.pop('Element') 67 else: 68 element = DOM.createDiv() 69 70 self.setElement(element) 71 Widget.__init__(self, **kwargs) 72 MouseHandler.__init__(self) 73 self.setzIndex(1000000) 74 self.addMouseListener(self)
75
76 - def addGlassListener(self, listener):
77 self.glassListeners.append(listener)
78
79 - def hide(self, autoClosed=False):
80 self.showing = False 81 82 self.hideGlass() 83 84 DOM.removeEventPreview(self) 85 86 RootPanel().remove(self) 87 self.onHideImpl(self.getElement()) 88 DOM.releaseCapture(self.getElement()) 89 for listener in self.glassListeners: 90 if hasattr(listener, 'onGlassHide'): 91 listener.onGlassHide(self, autoClosed) 92 else: 93 listener(self, autoClosed)
94
95 - def _event_targets_popup(self, event):
96 target = DOM.eventGetTarget(event) 97 return target and DOM.isOrHasChild(self.getElement(), target)
98
99 - def onEventPreview(self, event):
100 etype = DOM.eventGetType(event) 101 if etype == "mousedown" or etype == "blur": 102 if DOM.getCaptureElement() is not None: 103 return True 104 elif etype == "mouseup" or etype == "click" or \ 105 etype == "mousemove" or etype == "dblclick": 106 if DOM.getCaptureElement() is not None: 107 return True 108 return self._event_targets_popup(event)
109
110 - def onHideImpl(self, popup):
111 pass
112
113 - def onShowImpl(self, popup):
114 pass
115
116 - def removeGlassListener(self, listener):
117 self.glassListeners.remove(listener)
118
119 - def setGlassPosition(self):
120 top = Window.getScrollTop() 121 left = Window.getScrollLeft() 122 height = Window.getClientHeight() 123 width = Window.getClientWidth() 124 el = self.getElement() 125 DOM.setStyleAttribute(el, "position", "absolute") 126 DOM.setStyleAttribute(el, "left", 127 "%s" % left if left == 0 else "%spx" % left) 128 DOM.setStyleAttribute(el, "top", 129 "%s" % top if top == 0 else "%spx" % top) 130 DOM.setStyleAttribute(el, "height", "%spx" % (top + height)) 131 DOM.setStyleAttribute(el, "width", "%spx" % (left + width)) 132 # under pyjd glasswidget cannot be transparent, 133 # otherwise it drops the mousecapture, so we have 134 # to give it a 1% opaque background color 135 if pyjd.is_desktop: 136 # pyjd uses IE style opacity 137 DOM.setStyleAttribute(el, "filter", "alpha(opacity=1)") 138 # this is the Moz form of transparency 139 DOM.setStyleAttribute(el, "background", "rgba(255,255,255,0.1)")
140
141 - def showGlass(self):
142 self.setGlassPosition() 143 doc().body.appendChild(self.getElement()) 144 Window.addWindowResizeListener(self)
145
146 - def hideGlass(self):
147 Window.removeWindowResizeListener(self) 148 doc().body.removeChild(self.getElement())
149
150 - def onWindowResized(self, width, height):
151 self.setGlassPosition()
152
153 - def show(self):
154 if self.showing: 155 return 156 157 self.showing = True 158 159 self.showGlass() 160 161 DOM.addEventPreview(self) 162 163 RootPanel().add(self) 164 self.onShowImpl(self.getElement()) 165 DOM.setCapture(self.getElement())
166
167 - def adjustMousePos(self, x, y):
168 x += self.getAbsoluteLeft() - self.mousetarget.getAbsoluteLeft() 169 y += self.getAbsoluteTop() - self.mousetarget.getAbsoluteTop() 170 return x, y
171
172 - def onMouseDown(self, sender, x, y):
173 x, y = self.adjustMousePos(x, y) 174 if self.mousehandler: 175 self.mousetarget.onBrowserEvent(DOM.eventGetCurrentEvent()) 176 else: 177 self.mousetarget.onMouseDown(sender, x, y)
178
179 - def onMouseEnter(self, sender):
180 self.mousetarget.onMouseGlassEnter(sender)
181
182 - def onMouseLeave(self, sender):
183 self.mousetarget.onMouseGlassLeave(sender)
184
185 - def onMouseMove(self, sender, x, y):
186 x, y = self.adjustMousePos(x, y) 187 if self.mousehandler: 188 self.mousetarget.onBrowserEvent(DOM.eventGetCurrentEvent()) 189 else: 190 self.mousetarget.onMouseMove(sender, x, y)
191
192 - def onMouseUp(self, sender, x, y):
193 x, y = self.adjustMousePos(x, y) 194 if self.mousehandler: 195 self.mousetarget.onBrowserEvent(DOM.eventGetCurrentEvent()) 196 else: 197 self.mousetarget.onMouseUp(sender, x, y)
198 199 200 Factory.registerClass('pyjamas.ui.GlassWidget', 'GlassWidget', GlassWidget) 201