paulb@235 | 1 | #!/usr/bin/env python |
paulb@235 | 2 | |
paulb@235 | 3 | """ |
paulb@256 | 4 | DOM Level 3 Events support, with SVG Tiny 1.2 implementation additions. |
paulb@256 | 5 | See: http://www.w3.org/TR/DOM-Level-3-Events/events.html |
paulb@235 | 6 | |
paulb@235 | 7 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@235 | 8 | |
paulb@235 | 9 | This library is free software; you can redistribute it and/or |
paulb@235 | 10 | modify it under the terms of the GNU Lesser General Public |
paulb@235 | 11 | License as published by the Free Software Foundation; either |
paulb@235 | 12 | version 2.1 of the License, or (at your option) any later version. |
paulb@235 | 13 | |
paulb@235 | 14 | This library is distributed in the hope that it will be useful, |
paulb@235 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@235 | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@235 | 17 | Lesser General Public License for more details. |
paulb@235 | 18 | |
paulb@235 | 19 | You should have received a copy of the GNU Lesser General Public |
paulb@235 | 20 | License along with this library; if not, write to the Free Software |
paulb@235 | 21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@235 | 22 | """ |
paulb@235 | 23 | |
paulb@262 | 24 | import xml.dom |
paulb@256 | 25 | import time |
paulb@256 | 26 | |
paulb@256 | 27 | XML_EVENTS_NAMESPACE = "http://www.w3.org/2001/xml-events" |
paulb@256 | 28 | |
paulb@235 | 29 | class EventException(Exception): |
paulb@235 | 30 | |
paulb@235 | 31 | UNSPECIFIED_EVENT_TYPE_ERR = 0 |
paulb@235 | 32 | DISPATCH_REQUEST_ERR = 1 |
paulb@235 | 33 | |
paulb@256 | 34 | class DocumentEvent: |
paulb@256 | 35 | |
paulb@262 | 36 | """ |
paulb@262 | 37 | An event interface supportable by documents. |
paulb@262 | 38 | See: http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-DocumentEvent |
paulb@262 | 39 | """ |
paulb@256 | 40 | |
paulb@256 | 41 | def canDispatch(self, namespaceURI, type): |
paulb@262 | 42 | return namespaceURI is None and event_types.has_key(type) |
paulb@256 | 43 | |
paulb@256 | 44 | def createEvent(self, eventType): |
paulb@262 | 45 | try: |
paulb@262 | 46 | return event_types[eventType]() |
paulb@262 | 47 | except KeyError: |
paulb@262 | 48 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR) |
paulb@256 | 49 | |
paulb@235 | 50 | class Event: |
paulb@235 | 51 | |
paulb@256 | 52 | "An event class." |
paulb@256 | 53 | |
paulb@235 | 54 | CAPTURING_PHASE = 1 |
paulb@235 | 55 | AT_TARGET = 2 |
paulb@235 | 56 | BUBBLING_PHASE = 3 |
paulb@235 | 57 | |
paulb@262 | 58 | def __init__(self): |
paulb@256 | 59 | |
paulb@256 | 60 | "Initialise the event." |
paulb@256 | 61 | |
paulb@256 | 62 | # Initialised later: |
paulb@256 | 63 | |
paulb@262 | 64 | self.target = None |
paulb@262 | 65 | self.currentTarget = None |
paulb@262 | 66 | self.defaultPrevented = 0 |
paulb@256 | 67 | self.type = None |
paulb@256 | 68 | self.namespaceURI = None |
paulb@256 | 69 | |
paulb@256 | 70 | # DOM Level 3 Events: |
paulb@256 | 71 | |
paulb@256 | 72 | self.bubbles = None |
paulb@256 | 73 | self.eventPhase = self.CAPTURING_PHASE |
paulb@256 | 74 | self.timeStamp = time.time() |
paulb@235 | 75 | |
paulb@235 | 76 | def initEvent(self, eventTypeArg, canBubbleArg, cancelableArg): |
paulb@256 | 77 | self.initEventNS(None, eventTypeArg, canBubbleArg, cancelableArg) |
paulb@235 | 78 | |
paulb@235 | 79 | def initEventNS(self, namespaceURIArg, eventTypeArg, canBubbleArg, cancelableArg): |
paulb@256 | 80 | self.namespaceURI = namespaceURIArg |
paulb@256 | 81 | self.type = eventTypeArg |
paulb@256 | 82 | self.bubbles = canBubbleArg |
paulb@256 | 83 | self.cancelable = cancelableArg |
paulb@235 | 84 | |
paulb@235 | 85 | def preventDefault(self): |
paulb@256 | 86 | self.defaultPrevented = 1 |
paulb@235 | 87 | |
paulb@235 | 88 | def stopPropagation(self): |
paulb@235 | 89 | pass |
paulb@235 | 90 | |
paulb@235 | 91 | def stopImmediatePropagation(self): |
paulb@235 | 92 | pass |
paulb@235 | 93 | |
paulb@262 | 94 | class UIEvent(Event): |
paulb@262 | 95 | |
paulb@262 | 96 | "A user interface event." |
paulb@262 | 97 | |
paulb@262 | 98 | def __init__(self): |
paulb@262 | 99 | Event.__init__(self) |
paulb@262 | 100 | self.detail = None |
paulb@262 | 101 | |
paulb@262 | 102 | class MouseEvent(UIEvent): |
paulb@262 | 103 | |
paulb@262 | 104 | "A mouse-related event." |
paulb@262 | 105 | |
paulb@262 | 106 | def __init__(self): |
paulb@262 | 107 | Event.__init__(self) |
paulb@262 | 108 | self.screenX, self.screenY, self.clientX, self.clientY, self.button = None, None, None, None, None |
paulb@262 | 109 | |
paulb@262 | 110 | # Event types registry. |
paulb@262 | 111 | |
paulb@262 | 112 | event_types = { |
paulb@262 | 113 | "Event" : Event, |
paulb@262 | 114 | "UIEvent" : UIEvent, |
paulb@262 | 115 | "MouseEvent" : MouseEvent |
paulb@262 | 116 | } |
paulb@262 | 117 | |
paulb@235 | 118 | class EventTarget: |
paulb@235 | 119 | |
paulb@235 | 120 | "An event target class." |
paulb@235 | 121 | |
paulb@235 | 122 | def addEventListener(self, type, listener, useCapture): |
paulb@235 | 123 | self.addEventListenerNS(None, type, listener, useCapture) |
paulb@235 | 124 | |
paulb@262 | 125 | def addEventListenerNS(self, namespaceURI, type, listener, useCapture, group=None): # NOTE: group ignored |
paulb@262 | 126 | listeners = self.ownerDocument.global_.listeners |
paulb@262 | 127 | if not listeners.has_key(self): |
paulb@262 | 128 | listeners[self] = {} |
paulb@262 | 129 | if not listeners[self].has_key((namespaceURI, type)): |
paulb@262 | 130 | listeners[self][(namespaceURI, type)] = [] |
paulb@262 | 131 | listeners[self][(namespaceURI, type)].append((listener, useCapture)) |
paulb@235 | 132 | |
paulb@235 | 133 | def dispatchEvent(self, evt): |
paulb@262 | 134 | listeners = self.ownerDocument.global_.listeners |
paulb@235 | 135 | if not evt.type: |
paulb@235 | 136 | raise EventException(EventException.UNSPECIFIED_EVENT_TYPE_ERR) |
paulb@256 | 137 | # Dispatch on namespaceURI, type. |
paulb@262 | 138 | for listener, useCapture in listeners.get(self, {}).get((evt.namespaceURI, evt.type), []): |
paulb@256 | 139 | listener.handleEvent(evt) |
paulb@256 | 140 | return evt.defaultPrevented |
paulb@235 | 141 | |
paulb@235 | 142 | def removeEventListener(self, type, listener, useCapture): |
paulb@235 | 143 | self.removeEventListenerNS(None, type, listener, useCapture) |
paulb@235 | 144 | |
paulb@235 | 145 | def removeEventListenerNS(self, namespaceURI, type, listener, useCapture): |
paulb@262 | 146 | listeners = self.ownerDocument.global_.listeners |
paulb@262 | 147 | if listeners.has_key(self) and listeners[self].has_key((namespaceURI, type)): |
paulb@235 | 148 | try: |
paulb@262 | 149 | listeners[self][(namespaceURI, type)].remove((listener, useCapture)) |
paulb@235 | 150 | except ValueError: |
paulb@235 | 151 | pass |
paulb@235 | 152 | |
paulb@235 | 153 | # vim: tabstop=4 expandtab shiftwidth=4 |