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@256 | 24 | import time |
paulb@256 | 25 | |
paulb@256 | 26 | XML_EVENTS_NAMESPACE = "http://www.w3.org/2001/xml-events" |
paulb@256 | 27 | |
paulb@235 | 28 | class EventException(Exception): |
paulb@235 | 29 | |
paulb@235 | 30 | UNSPECIFIED_EVENT_TYPE_ERR = 0 |
paulb@235 | 31 | DISPATCH_REQUEST_ERR = 1 |
paulb@235 | 32 | |
paulb@256 | 33 | class DocumentEvent: |
paulb@256 | 34 | |
paulb@256 | 35 | "An event interface supportable by documents." |
paulb@256 | 36 | |
paulb@256 | 37 | def canDispatch(self, namespaceURI, type): |
paulb@256 | 38 | raise NotImplementedError, "canDispatch" |
paulb@256 | 39 | |
paulb@256 | 40 | def createEvent(self, eventType): |
paulb@256 | 41 | raise NotImplementedError, "createEvent" |
paulb@256 | 42 | |
paulb@235 | 43 | class Event: |
paulb@235 | 44 | |
paulb@256 | 45 | "An event class." |
paulb@256 | 46 | |
paulb@235 | 47 | CAPTURING_PHASE = 1 |
paulb@235 | 48 | AT_TARGET = 2 |
paulb@235 | 49 | BUBBLING_PHASE = 3 |
paulb@235 | 50 | |
paulb@256 | 51 | def __init__(self, target, currentTarget): |
paulb@256 | 52 | |
paulb@256 | 53 | "Initialise the event." |
paulb@256 | 54 | |
paulb@256 | 55 | self.target = target |
paulb@256 | 56 | self.currentTarget = currentTarget |
paulb@256 | 57 | self.defaultPrevented = 0 |
paulb@256 | 58 | |
paulb@256 | 59 | # Initialised later: |
paulb@256 | 60 | |
paulb@256 | 61 | self.type = None |
paulb@256 | 62 | self.namespaceURI = None |
paulb@256 | 63 | |
paulb@256 | 64 | # DOM Level 3 Events: |
paulb@256 | 65 | |
paulb@256 | 66 | self.bubbles = None |
paulb@256 | 67 | self.eventPhase = self.CAPTURING_PHASE |
paulb@256 | 68 | self.timeStamp = time.time() |
paulb@235 | 69 | |
paulb@235 | 70 | def initEvent(self, eventTypeArg, canBubbleArg, cancelableArg): |
paulb@256 | 71 | self.initEventNS(None, eventTypeArg, canBubbleArg, cancelableArg) |
paulb@235 | 72 | |
paulb@235 | 73 | def initEventNS(self, namespaceURIArg, eventTypeArg, canBubbleArg, cancelableArg): |
paulb@256 | 74 | self.namespaceURI = namespaceURIArg |
paulb@256 | 75 | self.type = eventTypeArg |
paulb@256 | 76 | self.bubbles = canBubbleArg |
paulb@256 | 77 | self.cancelable = cancelableArg |
paulb@235 | 78 | |
paulb@235 | 79 | def preventDefault(self): |
paulb@256 | 80 | self.defaultPrevented = 1 |
paulb@235 | 81 | |
paulb@235 | 82 | def stopPropagation(self): |
paulb@235 | 83 | pass |
paulb@235 | 84 | |
paulb@235 | 85 | def stopImmediatePropagation(self): |
paulb@235 | 86 | pass |
paulb@235 | 87 | |
paulb@235 | 88 | class EventTarget: |
paulb@235 | 89 | |
paulb@235 | 90 | "An event target class." |
paulb@235 | 91 | |
paulb@235 | 92 | def __init__(self): |
paulb@235 | 93 | self.listeners = {} |
paulb@235 | 94 | |
paulb@235 | 95 | def addEventListener(self, type, listener, useCapture): |
paulb@235 | 96 | self.addEventListenerNS(None, type, listener, useCapture) |
paulb@235 | 97 | |
paulb@235 | 98 | def addEventListenerNS(self, namespaceURI, type, listener, useCapture): |
paulb@235 | 99 | if not self.listeners.has_key((namespaceURI, type)): |
paulb@235 | 100 | self.listeners[(namespaceURI, type)] = [] |
paulb@235 | 101 | self.listeners[(namespaceURI, type)].append((listener, useCapture)) |
paulb@235 | 102 | |
paulb@235 | 103 | def dispatchEvent(self, evt): |
paulb@235 | 104 | if not evt.type: |
paulb@235 | 105 | raise EventException(EventException.UNSPECIFIED_EVENT_TYPE_ERR) |
paulb@256 | 106 | # Dispatch on namespaceURI, type. |
paulb@256 | 107 | for listener in self.listeners.get((evt.namespaceURI, evt.type), []): |
paulb@256 | 108 | listener.handleEvent(evt) |
paulb@256 | 109 | return evt.defaultPrevented |
paulb@235 | 110 | |
paulb@235 | 111 | def removeEventListener(self, type, listener, useCapture): |
paulb@235 | 112 | self.removeEventListenerNS(None, type, listener, useCapture) |
paulb@235 | 113 | |
paulb@235 | 114 | def removeEventListenerNS(self, namespaceURI, type, listener, useCapture): |
paulb@235 | 115 | if self.listeners.has_key((namespaceURI, type)): |
paulb@235 | 116 | listeners = self.listeners[(namespaceURI, type)] |
paulb@235 | 117 | try: |
paulb@235 | 118 | listeners.remove((listener, useCapture)) |
paulb@235 | 119 | except ValueError: |
paulb@235 | 120 | pass |
paulb@235 | 121 | |
paulb@235 | 122 | # vim: tabstop=4 expandtab shiftwidth=4 |