fdfb2848e5198d4249602f29ea677c860304cde3
[platform/core/uifw/at-spi2-atk.git] / pyatspi / state.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 StateType(_Enum):
27         _enum_lookup = {
28                 0:'STATE_INVALID',
29                 1:'STATE_ACTIVE',
30                 2:'STATE_ARMED',
31                 3:'STATE_BUSY',
32                 4:'STATE_CHECKED',
33                 5:'STATE_COLLAPSED',
34                 6:'STATE_DEFUNCT',
35                 7:'STATE_EDITABLE',
36                 8:'STATE_ENABLED',
37                 9:'STATE_EXPANDABLE',
38                 10:'STATE_EXPANDED',
39                 11:'STATE_FOCUSABLE',
40                 12:'STATE_FOCUSED',
41                 13:'STATE_HAS_TOOLTIP',
42                 14:'STATE_HORIZONTAL',
43                 15:'STATE_ICONIFIED',
44                 16:'STATE_MODAL',
45                 17:'STATE_MULTI_LINE',
46                 18:'STATE_MULTISELECTABLE',
47                 19:'STATE_OPAQUE',
48                 20:'STATE_PRESSED',
49                 21:'STATE_RESIZABLE',
50                 22:'STATE_SELECTABLE',
51                 23:'STATE_SELECTED',
52                 24:'STATE_SENSITIVE',
53                 25:'STATE_SHOWING',
54                 26:'STATE_SINGLE_LINE',
55                 27:'STATE_STALE',
56                 28:'STATE_TRANSIENT',
57                 29:'STATE_VERTICAL',
58                 30:'STATE_VISIBLE',
59                 31:'STATE_MANAGES_DESCENDANTS',
60                 32:'STATE_INDETERMINATE',
61                 33:'STATE_REQUIRED',
62                 34:'STATE_TRUNCATED',
63                 35:'STATE_ANIMATED',
64                 36:'STATE_INVALID_ENTRY',
65                 37:'STATE_SUPPORTS_AUTOCOMPLETION',
66                 38:'STATE_SELECTABLE_TEXT',
67                 39:'STATE_IS_DEFAULT',
68                 40:'STATE_VISITED',
69                 41:'STATE_LAST_DEFINED',
70         }
71
72 #------------------------------------------------------------------------------
73
74 STATE_ACTIVE = StateType(1)
75 STATE_ANIMATED = StateType(35)
76 STATE_ARMED = StateType(2)
77 STATE_BUSY = StateType(3)
78 STATE_CHECKED = StateType(4)
79 STATE_COLLAPSED = StateType(5)
80 STATE_DEFUNCT = StateType(6)
81 STATE_EDITABLE = StateType(7)
82 STATE_ENABLED = StateType(8)
83 STATE_EXPANDABLE = StateType(9)
84 STATE_EXPANDED = StateType(10)
85 STATE_FOCUSABLE = StateType(11)
86 STATE_FOCUSED = StateType(12)
87 STATE_HAS_TOOLTIP = StateType(13)
88 STATE_HORIZONTAL = StateType(14)
89 STATE_ICONIFIED = StateType(15)
90 STATE_INDETERMINATE = StateType(32)
91 STATE_INVALID = StateType(0)
92 STATE_INVALID_ENTRY = StateType(36)
93 STATE_IS_DEFAULT = StateType(39)
94 STATE_LAST_DEFINED = StateType(41)
95 STATE_MANAGES_DESCENDANTS = StateType(31)
96 STATE_MODAL = StateType(16)
97 STATE_MULTISELECTABLE = StateType(18)
98 STATE_MULTI_LINE = StateType(17)
99 STATE_OPAQUE = StateType(19)
100 STATE_PRESSED = StateType(20)
101 STATE_REQUIRED = StateType(33)
102 STATE_RESIZABLE = StateType(21)
103 STATE_SELECTABLE = StateType(22)
104 STATE_SELECTABLE_TEXT = StateType(38)
105 STATE_SELECTED = StateType(23)
106 STATE_SENSITIVE = StateType(24)
107 STATE_SHOWING = StateType(25)
108 STATE_SINGLE_LINE = StateType(26)
109 STATE_STALE = StateType(27)
110 STATE_SUPPORTS_AUTOCOMPLETION = StateType(37)
111 STATE_TRANSIENT = StateType(28)
112 STATE_TRUNCATED = StateType(34)
113 STATE_VERTICAL = StateType(29)
114 STATE_VISIBLE = StateType(30)
115 STATE_VISITED = StateType(40)
116
117 #------------------------------------------------------------------------------
118
119 # Build a dictionary mapping state values to names based on the prefix of the enum constants.
120
121 STATE_VALUE_TO_NAME = dict(((value, name[6:].lower().replace('_', ' '))
122                             for name, value
123                             in globals().items() 
124                             if name.startswith('STATE_')))
125
126 #------------------------------------------------------------------------------
127
128 def _marshal_state_set(bitfield):
129         """
130         The D-Bus protocol has a stateset object passed
131         as a 64bit bitfield. The Bits are passed as two 32bit
132         integers.
133
134         This function marshals the D-Bus message into a 
135         StateSet object that corresponds to these states.
136         """
137         (lower, upper) = bitfield
138
139         states = []
140
141         pos = 0
142         while (lower):
143                 if (1L)&lower:
144                         states.append(StateType(pos))
145                 pos+=1
146                 lower >>= 1
147
148         pos = 32
149         while (upper):
150                 if (1L)&upper:
151                         states.append(StateType(pos))
152                 pos+=1
153                 upper >>= 1
154
155         return StateSet(*states)
156
157 #------------------------------------------------------------------------------
158
159 class StateSet(object):
160         """
161         The StateSet object implements a wrapper around a
162         bitmap of Accessible states.
163
164         The StateSet object is the instantaneous state of
165         the Accessible object and is not updated with its
166         container Accessible. This behaviour is different
167         to the CORBA version of AT-SPI
168         """
169         def __init__(self, *states):
170                 """
171                 Initializes the state set with the given states.
172
173                 @param states: States to add immediately
174                 @type states: list
175                 """
176                 self.states = set()
177                 map(self.add, states)
178
179         def contains(self, state):
180                 """
181                 Checks if this StateSet contains the given state.
182
183                 @param state: State to check
184                 @type state: Accessibility.StateType
185                 @return: True if the set contains the given state
186                 @rtype: boolean
187                 """
188                 return state in self.states
189
190         def add(self, *states):
191                 """
192                 Adds states to this set.
193
194                 @param states: State(s) to add
195                 @type states: Accessibility.StateType
196                 """
197                 for state in states:
198                         self.states.add(state)
199
200         def remove(self, state):
201                 """
202                 Removes states from this set.
203
204                 @param states: State(s) to remove
205                 @type states: Accessibility.StateType
206                 """
207                 self.states.remove(state)
208
209         def equals(self, state_set):
210                 """
211                 Checks if this StateSet contains exactly the same members as the given
212                 StateSet.
213
214                 @param state_set: Another set
215                 @type state_set: Accessibility.StateSet
216                 @return: Are the sets equivalent in terms of their contents?
217                 @rtype: boolean
218                 """
219                 return set(state_set.getStates()) == self.states
220
221         def compare(self, state_set):
222                 """
223                 Finds the symmetric difference between this state set andthe one provided,
224                 and returns it as a new StateSet.
225
226                 @note: This does not use L{_StateSetImpl.compare} which cannot be
227                 implemented at this time
228                 @param state_set: Set to compare against
229                 @type state_set: Accessibility.StateSet
230                 @return: Proxy for the new set
231                 @rtype: L{StateSet}
232                 """
233                 a = set(self.getStates())
234                 b = set(state_set.getStates())
235                 diff = a.symmetric_difference(b)
236                 return StateSet(*diff)
237
238         def isEmpty(self):
239                 """
240                 Checks if this StateSet is empty.
241
242                 @return: Is it empty?
243                 @rtype: boolean
244                 """
245                 return len(self.states) == 0
246
247         def getStates(self):
248                 """
249                 Gets the sequence of all states in this set.
250
251                 @return: List of states
252                 @rtype: list
253                 """
254                 return list(self.states)
255
256 #END----------------------------------------------------------------------------