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

Source Code for Module library.pyjamas.ui.MouseListener

  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.ui import Event 
 17  from pyjamas.EventController import Handler 
 18   
19 -def fireMouseEvent(listeners, sender, event):
20 x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(sender.getElement()) 21 y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(sender.getElement()) 22 23 etype = DOM.eventGetType(event) 24 if etype == "mousedown": 25 for listener in listeners: 26 listener.onMouseDown(sender, x, y) 27 return True 28 elif etype == "mouseup": 29 for listener in listeners: 30 listener.onMouseUp(sender, x, y) 31 return True 32 elif etype == "mousemove": 33 for listener in listeners: 34 listener.onMouseMove(sender, x, y) 35 return True 36 elif etype == "mouseover": 37 to_element = DOM.eventGetToElement(event) 38 if to_element and not DOM.isOrHasChild(sender.getElement(), to_element): 39 for listener in listeners: 40 listener.onMouseEnter(sender) 41 return True 42 elif etype == "mouseout": 43 to_element = DOM.eventGetToElement(event) 44 if to_element and not DOM.isOrHasChild(sender.getElement(), to_element): 45 for listener in listeners: 46 listener.onMouseLeave(sender) 47 return True 48 return False
49 50 MOUSE_EVENTS = [ "mousedown", "mouseup", "mousemove", "mouseover", "mouseout"] 51
52 -class MouseHandler(object):
53
54 - def __init__(self, preventDefault=False):
55 56 self._mouseListeners = [] 57 self._mousePreventDefault = preventDefault 58 self.sinkEvents( Event.MOUSEEVENTS )
59
60 - def onBrowserEvent(self, event):
61 etype = DOM.eventGetType(event) 62 if etype in MOUSE_EVENTS: 63 if self._mousePreventDefault: 64 DOM.eventPreventDefault(event) 65 return fireMouseEvent(self._mouseListeners, self, event) 66 return False
67
68 - def addMouseListener(self, listener):
69 self._mouseListeners.append(listener)
70
71 - def removeMouseListener(self, listener):
72 self._mouseListeners.remove(listener)
73
74 - def onMouseMove(self, sender, x, y):
75 pass
76
77 - def onMouseDown(self, sender, x, y):
78 pass
79
80 - def onMouseUp(self, sender, x, y):
81 pass
82
83 - def onMouseEnter(self, sender):
84 pass
85
86 - def onMouseLeave(self, sender):
87 pass
88
89 - def onMouseGlassEnter(self, sender):
90 pass
91
92 - def onMouseGlassLeave(self, sender):
93 pass
94
95 -class MouseWheelHandler(object):
96
97 - def __init__(self, preventDefault=False):
98 99 self._mouseWheelListeners = [] 100 self._mouseWheelPreventDefault = preventDefault 101 self.sinkEvents( Event.ONMOUSEWHEEL )
102
103 - def onBrowserEvent(self, event):
104 etype = DOM.eventGetType(event) 105 if etype == "mousewheel": 106 if self._mouseWheelPreventDefault: 107 DOM.eventPreventDefault(event) 108 velocity = DOM.eventGetMouseWheelVelocityY(event) 109 for listener in self._mouseWheelListeners: 110 listener.onMouseWheel(self, velocity) 111 return True
112
113 - def addMouseWheelListener(self, listener):
114 self._mouseWheelListeners.append(listener)
115
116 - def removeMouseWheelListener(self, listener):
117 self._mouseWheelListeners.remove(listener)
118
119 - def onMouseWheel(self, sender, velocity):
120 pass
121