6d1ede0d9afc31fa6aebcacc49aa380c838075d0
[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
25 #------------------------------------------------------------------------------
26
27 class RelationType(_Enum):
28         _enum_lookup = {
29                 0:'RELATION_NULL',
30                 1:'RELATION_LABEL_FOR',
31                 2:'RELATION_LABELLED_BY',
32                 3:'RELATION_CONTROLLER_FOR',
33                 4:'RELATION_CONTROLLED_BY',
34                 5:'RELATION_MEMBER_OF',
35                 6:'RELATION_TOOLTIP_FOR',
36                 7:'RELATION_NODE_CHILD_OF',
37                 8:'RELATION_EXTENDED',
38                 9:'RELATION_FLOWS_TO',
39                 10:'RELATION_FLOWS_FROM',
40                 11:'RELATION_SUBWINDOW_OF',
41                 12:'RELATION_EMBEDS',
42                 13:'RELATION_EMBEDDED_BY',
43                 14:'RELATION_POPUP_FOR',
44                 15:'RELATION_PARENT_WINDOW_OF',
45                 16:'RELATION_DESCRIPTION_FOR',
46                 17:'RELATION_DESCRIBED_BY',
47                 18:'RELATION_LAST_DEFINED',
48         }
49
50 #------------------------------------------------------------------------------
51
52 RELATION_CONTROLLED_BY = RelationType(4)
53 RELATION_CONTROLLER_FOR = RelationType(3)
54 RELATION_DESCRIBED_BY = RelationType(17)
55 RELATION_DESCRIPTION_FOR = RelationType(16)
56 RELATION_EMBEDDED_BY = RelationType(13)
57 RELATION_EMBEDS = RelationType(12)
58 RELATION_EXTENDED = RelationType(8)
59 RELATION_FLOWS_FROM = RelationType(10)
60 RELATION_FLOWS_TO = RelationType(9)
61 RELATION_LABELLED_BY = RelationType(2)
62 RELATION_LABEL_FOR = RelationType(1)
63 RELATION_LAST_DEFINED = RelationType(18)
64 RELATION_MEMBER_OF = RelationType(5)
65 RELATION_NODE_CHILD_OF = RelationType(7)
66 RELATION_NULL = RelationType(0)
67 RELATION_PARENT_WINDOW_OF = RelationType(15)
68 RELATION_POPUP_FOR = RelationType(14)
69 RELATION_SUBWINDOW_OF = RelationType(11)
70 RELATION_TOOLTIP_FOR = RelationType(6)
71
72 #------------------------------------------------------------------------------
73
74 # Build a dictionary mapping relation values to names based on the prefix of the enum constants.
75
76 RELATION_VALUE_TO_NAME = dict(((value, name[9:].lower().replace('_', ' ')) 
77                                for name, value 
78                                in globals().items()
79                                if name.startswith('RELATION_')))
80
81 #------------------------------------------------------------------------------
82
83 def _marshal_relation_set(cache, app_name, relation_set):
84         """
85         The D-Bus protocol has a relation set passed as an array of
86         relation types and object arrays.
87
88         This function marshals the D-Bus message into a list of relation
89         objects.
90         """
91         return [Relation(cache, app_name, *relation) for relation in relation_set]
92
93 #------------------------------------------------------------------------------
94
95 class Relation(object):
96         """
97         An interface via which objects' non-hierarchical relationships
98         to one another are indicated. An instance of Relations represents
99         a "one-to-many" correspondance.
100         """
101
102         def __init__(self, cache, app_name, type, objects):
103                 self._type = type
104                 self._objects = objects
105
106                 self._cache = cache
107                 self._app_name = app_name
108
109         def getNTargets(self):
110                 """
111                 @return the number of objects to which this relationship applies.
112                 """
113                 return len(self._objects)
114
115         def getRelationType(self):
116                 """
117                 @return the RelationType of this Relation.
118                 """
119                 # TODO Return a relation enum type
120                 return self._type
121
122         def getRelationTypeName(self):
123                 """
124                 @return an unlocalized string representing the relation type.
125                 """
126                 return RELATION_VALUE_TO_NAME[self._type]
127
128         def getTarget(self, index):
129                 """
130                 @return an Object which is the 'nth'target of this Relation,
131                 e.g. the Object at index i in the list of Objects having the
132                 specified relationship to this Accessible.
133                 """
134                 return self._cache.create_accessible(self._app_name,
135                                                      self._objects[index],
136                                                      interfaces.ATSPI_ACCESSIBLE)
137
138 #END----------------------------------------------------------------------------