Merge branch 'master' of ssh://git.codethink.co.uk/git/atspi-dbus
[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 from base import Enum as _Enum
23
24 #------------------------------------------------------------------------------
25
26 class RelationType(_Enum):
27     _enum_lookup = {
28         0:'RELATION_NULL',
29         1:'RELATION_LABEL_FOR',
30         2:'RELATION_LABELLED_BY',
31         3:'RELATION_CONTROLLER_FOR',
32         4:'RELATION_CONTROLLED_BY',
33         5:'RELATION_MEMBER_OF',
34         6:'RELATION_TOOLTIP_FOR',
35         7:'RELATION_NODE_CHILD_OF',
36         8:'RELATION_EXTENDED',
37         9:'RELATION_FLOWS_TO',
38         10:'RELATION_FLOWS_FROM',
39         11:'RELATION_SUBWINDOW_OF',
40         12:'RELATION_EMBEDS',
41         13:'RELATION_EMBEDDED_BY',
42         14:'RELATION_POPUP_FOR',
43         15:'RELATION_PARENT_WINDOW_OF',
44         16:'RELATION_DESCRIPTION_FOR',
45         17:'RELATION_DESCRIBED_BY',
46         18:'RELATION_LAST_DEFINED',
47     }
48
49 #------------------------------------------------------------------------------
50
51 RELATION_CONTROLLED_BY = RelationType(4)
52
53 RELATION_CONTROLLER_FOR = RelationType(3)
54
55 RELATION_DESCRIBED_BY = RelationType(17)
56
57 RELATION_DESCRIPTION_FOR = RelationType(16)
58
59 RELATION_EMBEDDED_BY = RelationType(13)
60
61 RELATION_EMBEDS = RelationType(12)
62
63 RELATION_EXTENDED = RelationType(8)
64
65 RELATION_FLOWS_FROM = RelationType(10)
66
67 RELATION_FLOWS_TO = RelationType(9)
68
69 RELATION_LABELLED_BY = RelationType(2)
70
71 RELATION_LABEL_FOR = RelationType(1)
72
73 RELATION_LAST_DEFINED = RelationType(18)
74
75 RELATION_MEMBER_OF = RelationType(5)
76
77 RELATION_NODE_CHILD_OF = RelationType(7)
78
79 RELATION_NULL = RelationType(0)
80
81 RELATION_PARENT_WINDOW_OF = RelationType(15)
82
83 RELATION_POPUP_FOR = RelationType(14)
84
85 RELATION_SUBWINDOW_OF = RelationType(11)
86
87 RELATION_TOOLTIP_FOR = RelationType(6)
88
89 #------------------------------------------------------------------------------
90
91 # Build a dictionary mapping relation values to names based on the prefix of the enum constants.
92
93 RELATION_VALUE_TO_NAME = dict(((value, name[9:].lower().replace('_', ' ')) 
94                                for name, value 
95                                in globals().items()
96                                if name.startswith('RELATION_')))
97
98 #------------------------------------------------------------------------------
99
100 def _marshal_relation_set(bitfield):
101         """
102         The D-Bus protocol has a relation set passed as an array of
103         relation types and object arrays.
104
105         This function marshals the D-Bus message into a list of relation
106         objects.
107         """
108         (lower, upper) = bitfield
109
110         states = []
111
112         pos = 0
113         while (lower):
114                 if (1L)&lower:
115                         #TODO Return the state objects rather than integers.
116                         states.append(pos)
117                 pos+=1
118         while (upper):
119                 if (1L)&upper:
120                         #TODO return the state objects rather than integers.
121                         states.append(pos)
122
123         return StateSet(*states)
124
125 #------------------------------------------------------------------------------
126
127 class Relation(object):
128     """
129     An interface via which objects' non-hierarchical relationships
130     to one another are indicated. An instance of Relations represents
131     a "one-to-many" correspondance.
132     """
133     
134     def getNTargets(self, *args, **kwargs):
135         """
136         @return the number of objects to which this relationship applies.
137         """
138         func = self.get_dbus_method("getNTargets")
139         return func(*args, **kwargs)
140     
141     def getRelationType(self, *args, **kwargs):
142         """
143         @return the RelationType of this Relation.
144         """
145         func = self.get_dbus_method("getRelationType")
146         return func(*args, **kwargs)
147     
148     def getRelationTypeName(self, *args, **kwargs):
149         """
150         @return an unlocalized string representing the relation type.
151         """
152         func = self.get_dbus_method("getRelationTypeName")
153         return func(*args, **kwargs)
154     
155     def getTarget(self, *args, **kwargs):
156         """
157         @return an Object which is the 'nth'target of this Relation,
158         e.g. the Object at index i in the list of Objects having the
159         specified relationship to this Accessible.
160         """
161         func = self.get_dbus_method("getTarget")
162         return func(*args, **kwargs)
163
164 #END----------------------------------------------------------------------------