86e3a76dedf2d485cf799b32ed71ec5ba29a3088
[platform/core/uifw/at-spi2-atk.git] / pyatspi / relation.py
1 #Copyright (C) 2008 Codethink Ltd
2 #copyright: Copyright (c) 2005, 2007 IBM Corporation
3
4 #This library is free software; you can redistribute it and/or
5 #modify it under the terms of the GNU Lesser General Public
6 #License version 2 as published by the Free Software Foundation.
7
8 #This program is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #GNU General Public License for more details.
12 #You should have received a copy of the GNU Lesser General Public License
13 #along with this program; if not, write to the Free Software
14 #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15
16 #Portions of this code originally licensed and copyright (c) 2005, 2007
17 #IBM Corporation under the BSD license, available at
18 #U{http://www.opensource.org/licenses/bsd-license.php}
19
20 #authors: Peter Parente, Mark Doffman
21
22 import interfaces
23 from base import Enum as _Enum
24 from factory import create_accessible
25
26 #------------------------------------------------------------------------------
27
28 class RelationType(_Enum):
29     _enum_lookup = {
30         0:'RELATION_NULL',
31         1:'RELATION_LABEL_FOR',
32         2:'RELATION_LABELLED_BY',
33         3:'RELATION_CONTROLLER_FOR',
34         4:'RELATION_CONTROLLED_BY',
35         5:'RELATION_MEMBER_OF',
36         6:'RELATION_TOOLTIP_FOR',
37         7:'RELATION_NODE_CHILD_OF',
38         8:'RELATION_EXTENDED',
39         9:'RELATION_FLOWS_TO',
40         10:'RELATION_FLOWS_FROM',
41         11:'RELATION_SUBWINDOW_OF',
42         12:'RELATION_EMBEDS',
43         13:'RELATION_EMBEDDED_BY',
44         14:'RELATION_POPUP_FOR',
45         15:'RELATION_PARENT_WINDOW_OF',
46         16:'RELATION_DESCRIPTION_FOR',
47         17:'RELATION_DESCRIBED_BY',
48         18:'RELATION_LAST_DEFINED',
49     }
50
51 #------------------------------------------------------------------------------
52
53 RELATION_CONTROLLED_BY = RelationType(4)
54
55 RELATION_CONTROLLER_FOR = RelationType(3)
56
57 RELATION_DESCRIBED_BY = RelationType(17)
58
59 RELATION_DESCRIPTION_FOR = RelationType(16)
60
61 RELATION_EMBEDDED_BY = RelationType(13)
62
63 RELATION_EMBEDS = RelationType(12)
64
65 RELATION_EXTENDED = RelationType(8)
66
67 RELATION_FLOWS_FROM = RelationType(10)
68
69 RELATION_FLOWS_TO = RelationType(9)
70
71 RELATION_LABELLED_BY = RelationType(2)
72
73 RELATION_LABEL_FOR = RelationType(1)
74
75 RELATION_LAST_DEFINED = RelationType(18)
76
77 RELATION_MEMBER_OF = RelationType(5)
78
79 RELATION_NODE_CHILD_OF = RelationType(7)
80
81 RELATION_NULL = RelationType(0)
82
83 RELATION_PARENT_WINDOW_OF = RelationType(15)
84
85 RELATION_POPUP_FOR = RelationType(14)
86
87 RELATION_SUBWINDOW_OF = RelationType(11)
88
89 RELATION_TOOLTIP_FOR = RelationType(6)
90
91 #------------------------------------------------------------------------------
92
93 # Build a dictionary mapping relation values to names based on the prefix of the enum constants.
94
95 RELATION_VALUE_TO_NAME = dict(((value, name[9:].lower().replace('_', ' ')) 
96                                for name, value 
97                                in globals().items()
98                                if name.startswith('RELATION_')))
99
100 #------------------------------------------------------------------------------
101
102 def _marshal_relation_set(cache, dbus_object, app_name, relation_set):
103         """
104         The D-Bus protocol has a relation set passed as an array of
105         relation types and object arrays.
106
107         This function marshals the D-Bus message into a list of relation
108         objects.
109         """
110         return [Relation(cache, dbus_object, app_name, *relation) for relation in relation_set]
111
112 #------------------------------------------------------------------------------
113
114 class Relation(object):
115     """
116     An interface via which objects' non-hierarchical relationships
117     to one another are indicated. An instance of Relations represents
118     a "one-to-many" correspondance.
119     """
120
121     def __init__(self, cache, dbus_object, app_name, type, objects):
122         self._type = type
123         self._objects = objects
124
125         self._dbus_object = dbus_object
126         self._cache = cache
127         self._app_name = app_name
128     
129     def getNTargets(self):
130         """
131         @return the number of objects to which this relationship applies.
132         """
133         return len(self._objects)
134     
135     def getRelationType(self):
136         """
137         @return the RelationType of this Relation.
138         """
139         return self._type
140     
141     def getRelationTypeName(self):
142         """
143         @return an unlocalized string representing the relation type.
144         """
145         return RELATION_VALUE_TO_NAME[self._type]
146     
147     def getTarget(self, index):
148         """
149         @return an Object which is the 'nth'target of this Relation,
150         e.g. the Object at index i in the list of Objects having the
151         specified relationship to this Accessible.
152         """
153         return create_accessible(self._cache,
154                                  self._app_name,
155                                  self._objects[index],
156                                  interfaces.ATSPI_ACCESSIBLE,
157                                  dbus_object=self._dbus_object)
158
159 #END----------------------------------------------------------------------------