File: broadcaster.py

package info (click to toggle)
python-chaco 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,144 kB
  • sloc: python: 35,936; ansic: 1,211; cpp: 241; makefile: 124; sh: 5
file content (56 lines) | stat: -rw-r--r-- 1,899 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
""" Defines the BroadcasterTool class.
"""
from enable.api import BaseTool
from traits.api import Dict, List

class BroadcasterTool(BaseTool):
    """ A simple tool that keeps a list of other tools, and broadcasts events it
    receives to all of the tools.
    """

    # The tools to which this tool broadcasts events.
    tools = List

    # Mapping from tools to transforms, for tools that can be mouse owners.
    # (See enable.AbstractWindow.)
    mouse_owners = Dict

    def dispatch(self, event, suffix):
        """ Dispatches a mouse event based on the current event state.

        Overrides BaseTool.
        """
        handled = False   # keeps track of whether any tool handled this event

        if event.window.mouse_owner == self:
            tools = self.mouse_owners.keys()
            mouse_owned = True
        else:
            tools = self.tools
            mouse_owned = False

        for tool in tools:
            if mouse_owned:
                event.window.set_mouse_owner(tool, self.mouse_owners[tool])

            tool.dispatch(event, suffix)
            if event.handled:
                handled = True
                event.handled = False

            if mouse_owned and event.window.mouse_owner is None:
                # The tool owned the mouse before handling the previous event,
                # and now doesn't, so remove it from the list of mouse_owners
                del self.mouse_owners[tool]

            elif not mouse_owned and event.window.mouse_owner == tool:
                # The tool is a new mouse owner
                self.mouse_owners[tool] = event.window.mouse_owner_transform

        if len(self.mouse_owners) == 0:
            if event.window.mouse_owner == self:
                event.window.set_mouse_owner(None)
        else:
            event.window.set_mouse_owner(self, event.net_transform())

        event.handled = handled