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

Source Code for Module 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   
18 -def fireMouseEvent(listeners, sender, event):
19 x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(sender.getElement()) 20 y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(sender.getElement()) 21 22 type = DOM.eventGetType(event) 23 if type == "mousedown": 24 for listener in listeners: 25 listener.onMouseDown(sender, x, y) 26 elif type == "mouseup": 27 for listener in listeners: 28 listener.onMouseUp(sender, x, y) 29 elif type == "mousemove": 30 for listener in listeners: 31 listener.onMouseMove(sender, x, y) 32 elif type == "mouseover": 33 from_element = DOM.eventGetFromElement(event) 34 if not DOM.isOrHasChild(sender.getElement(), from_element): 35 for listener in listeners: 36 listener.onMouseEnter(sender) 37 elif type == "mouseout": 38 to_element = DOM.eventGetToElement(event) 39 if not DOM.isOrHasChild(sender.getElement(), to_element): 40 for listener in listeners: 41 listener.onMouseLeave(sender)
42 43 MOUSE_EVENTS = [ "mousedown", "mouseup", "mousemove", "mouseover", "mouseout"] 44
45 -class MouseHandler(object):
46
47 - def __init__(self, preventDefault=False):
48 49 self._mouseListeners = [] 50 self._mousePreventDefault = preventDefault 51 self.sinkEvents( Event.MOUSEEVENTS )
52
53 - def onBrowserEvent(self, event):
54 type = DOM.eventGetType(event) 55 if type in MOUSE_EVENTS: 56 if self._mousePreventDefault: 57 DOM.eventPreventDefault(event) 58 fireMouseEvent(self._mouseListeners, self, event)
59
60 - def addMouseListener(self, listener):
61 self._mouseListeners.append(listener)
62
63 - def removeMouseListener(self, listener):
64 self._mouseListeners.remove(listener)
65
66 - def onMouseMove(self, sender, x, y):
67 pass
68
69 - def onMouseDown(self, sender, x, y):
70 pass
71
72 - def onMouseUp(self, sender, x, y):
73 pass
74
75 - def onMouseEnter(self, sender):
76 pass
77
78 - def onMouseLeave(self, sender):
79 pass
80