<s> import pyjd <EOL> from pyjamas import DOM <EOL> from pyjamas import Window <EOL> from pyjamas import Factory <EOL> from __pyjamas__ import JS , doc <EOL> from pyjamas . ui . SimplePanel import SimplePanel <EOL> from pyjamas . ui . Widget import Widget <EOL> from pyjamas . ui . MouseListener import MouseHandler <EOL> from pyjamas . ui . RootPanel import RootPanel <EOL> mousecapturer = None <EOL> def getMouseCapturer ( ** kwargs ) : <EOL> global mousecapturer <EOL> if mousecapturer is None : <EOL> mousecapturer = GlassWidget ( ** kwargs ) <EOL> elif not isinstance ( mousecapturer , GlassWidget ) : <EOL> return None <EOL> return mousecapturer <EOL> def show ( mousetarget , ** kwargs ) : <EOL> global mousecapturer <EOL> mc = getMouseCapturer ( ** kwargs ) <EOL> mc . mousetarget = mousetarget <EOL> if isinstance ( mousetarget , MouseHandler ) : <EOL> mc . mousehandler = True <EOL> mc . show ( ) <EOL> def hide ( ) : <EOL> global mousecapturer <EOL> mousecapturer . hide ( ) <EOL> class GlassWidget ( Widget , MouseHandler ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . glassListeners = [ ] <EOL> self . showing = False <EOL> self . mousehandler = False <EOL> if not '<STR_LIT>' in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> if '<STR_LIT>' in kwargs : <EOL> element = kwargs . pop ( '<STR_LIT>' ) <EOL> else : <EOL> element = DOM . createDiv ( ) <EOL> self . setElement ( element ) <EOL> Widget . __init__ ( self , ** kwargs ) <EOL> MouseHandler . __init__ ( self ) <EOL> self . setzIndex ( <NUM_LIT> ) <EOL> self . addMouseListener ( self ) <EOL> def addGlassListener ( self , listener ) : <EOL> self . glassListeners . append ( listener ) <EOL> def hide ( self , autoClosed = False ) : <EOL> self . showing = False <EOL> self . hideGlass ( ) <EOL> DOM . removeEventPreview ( self ) <EOL> RootPanel ( ) . remove ( self ) <EOL> self . onHideImpl ( self . getElement ( ) ) <EOL> DOM . releaseCapture ( self . getElement ( ) ) <EOL> for listener in self . glassListeners : <EOL> if hasattr ( listener , '<STR_LIT>' ) : <EOL> listener . onGlassHide ( self , autoClosed ) <EOL> else : <EOL> listener ( self , autoClosed ) <EOL> def _event_targets_popup ( self , event ) : <EOL> target = DOM . eventGetTarget ( event ) <EOL> return target and DOM . isOrHasChild ( self . getElement ( ) , target ) <EOL> def onEventPreview ( self , event ) : <EOL> etype = DOM . eventGetType ( event ) <EOL> if etype == "<STR_LIT>" or etype == "<STR_LIT>" : <EOL> if DOM . getCaptureElement ( ) is not None : <EOL> return True <EOL> elif etype == "<STR_LIT>" or etype == "<STR_LIT>" or etype == "<STR_LIT>" or etype == "<STR_LIT>" : <EOL> if DOM . getCaptureElement ( ) is not None : <EOL> return True <EOL> return self . _event_targets_popup ( event ) <EOL> def onHideImpl ( self , popup ) : <EOL> pass <EOL> def onShowImpl ( self , popup ) : <EOL> pass <EOL> def removeGlassListener ( self , listener ) : <EOL> self . glassListeners . remove ( listener ) <EOL> def setGlassPosition ( self ) : <EOL> top = Window . getScrollTop ( ) <EOL> left = Window . getScrollLeft ( ) <EOL> height = Window . getClientHeight ( ) <EOL> width = Window . getClientWidth ( ) <EOL> el = self . getElement ( ) <EOL> DOM . setStyleAttribute ( el , "<STR_LIT>" , "<STR_LIT>" ) <EOL> DOM . setStyleAttribute ( el , "<STR_LIT:left>" , <EOL> "<STR_LIT:%s>" % left if left == <NUM_LIT:0> else "<STR_LIT>" % left ) <EOL> DOM . setStyleAttribute ( el , "<STR_LIT>" , <EOL> "<STR_LIT:%s>" % top if top == <NUM_LIT:0> else "<STR_LIT>" % top ) <EOL> DOM . setStyleAttribute ( el , "<STR_LIT>" , "<STR_LIT>" % ( top + height ) ) <EOL> DOM . setStyleAttribute ( el , "<STR_LIT:width>" , "<STR_LIT>" % ( left + width ) ) <EOL> if pyjd . is_desktop : <EOL> DOM . setStyleAttribute ( el , "<STR_LIT>" , "<STR_LIT>" ) <EOL> DOM . setStyleAttribute ( el , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def showGlass ( self ) : <EOL> self . setGlassPosition ( ) <EOL> doc ( ) . body . appendChild ( self . getElement ( ) ) <EOL> Window . addWindowResizeListener ( self ) <EOL> def hideGlass ( self ) : <EOL> Window . removeWindowResizeListener ( self ) <EOL> doc ( ) . body . removeChild ( self . getElement ( ) ) <EOL> def onWindowResized ( self , width , height ) : <EOL> self . setGlassPosition ( ) <EOL> def show ( self ) : <EOL> if self . showing : <EOL> return <EOL> self . showing = True <EOL> self . showGlass ( ) <EOL> DOM . addEventPreview ( self ) <EOL> RootPanel ( ) . add ( self ) <EOL> self . onShowImpl ( self . getElement ( ) ) <EOL> DOM . setCapture ( self . getElement ( ) ) <EOL> def adjustMousePos ( self , x , y ) : <EOL> x += self . getAbsoluteLeft ( ) - self . mousetarget . getAbsoluteLeft ( ) <EOL> y += self . getAbsoluteTop ( ) - self . mousetarget . getAbsoluteTop ( ) <EOL> return x , y <EOL> def onMouseDown ( self , sender , x , y ) : <EOL> x , y = self . adjustMousePos ( x , y ) <EOL> if self . mousehandler : <EOL> self . mousetarget . onBrowserEvent ( DOM . eventGetCurrentEvent ( ) ) <EOL> else : <EOL> self . mousetarget . onMouseDown ( sender , x , y ) <EOL> def onMouseEnter ( self , sender ) : <EOL> self . mousetarget . onMouseGlassEnter ( sender ) <EOL> def onMouseLeave ( self , sender ) : <EOL> self . mousetarget . onMouseGlassLeave ( sender ) <EOL> def onMouseMove ( self , sender , x , y ) : <EOL> x , y = self . adjustMousePos ( x , y ) <EOL> if self . mousehandler : <EOL> self . mousetarget . onBrowserEvent ( DOM . eventGetCurrentEvent ( ) ) <EOL> else : <EOL> self . mousetarget . onMouseMove ( sender , x , y ) <EOL> def onMouseUp ( self , sender , x , y ) : <EOL> x , y = self . adjustMousePos ( x , y ) <EOL> if self . mousehandler : <EOL> self . mousetarget . onBrowserEvent ( DOM . eventGetCurrentEvent ( ) ) <EOL> else : <EOL> self . mousetarget . onMouseUp ( sender , x , y ) <EOL> Factory . registerClass ( '<STR_LIT>' , '<STR_LIT>' , GlassWidget ) </s>
