1 #Copyright (C) 2008 Codethink Ltd
2 #copyright: Copyright (c) 2005, 2007 IBM Corporation
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.
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.
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}
20 #authors: Peter Parente, Mark Doffman
22 from base import Enum as _Enum
24 #------------------------------------------------------------------------------
26 class StateType(_Enum):
41 13:'STATE_HAS_TOOLTIP',
42 14:'STATE_HORIZONTAL',
45 17:'STATE_MULTI_LINE',
46 18:'STATE_MULTISELECTABLE',
50 22:'STATE_SELECTABLE',
54 26:'STATE_SINGLE_LINE',
59 31:'STATE_MANAGES_DESCENDANTS',
60 32:'STATE_INDETERMINATE',
64 36:'STATE_INVALID_ENTRY',
65 37:'STATE_SUPPORTS_AUTOCOMPLETION',
66 38:'STATE_SELECTABLE_TEXT',
67 39:'STATE_IS_DEFAULT',
69 41:'STATE_LAST_DEFINED',
72 #------------------------------------------------------------------------------
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)
117 #------------------------------------------------------------------------------
119 # Build a dictionary mapping state values to names based on the prefix of the enum constants.
121 STATE_VALUE_TO_NAME = dict(((value, name[6:].lower().replace('_', ' '))
124 if name.startswith('STATE_')))
126 #------------------------------------------------------------------------------
128 def _marshal_state_set(bitfield):
130 The D-Bus protocol has a stateset object passed
131 as a 64bit bitfield. The Bits are passed as two 32bit
134 This function marshals the D-Bus message into a
135 StateSet object that corresponds to these states.
137 (lower, upper) = bitfield
144 #TODO Return the state objects rather than integers.
149 #TODO return the state objects rather than integers.
152 return StateSet(*states)
154 #------------------------------------------------------------------------------
156 class StateSet(object):
158 The StateSet object implements a wrapper around a
159 bitmap of Accessible states.
161 The StateSet object is the instantaneous state of
162 the Accessible object and is not updated with its
163 container Accessible. This behaviour is different
164 to the CORBA version of AT-SPI
166 def __init__(self, *states):
168 Initializes the state set with the given states.
170 @param states: States to add immediately
173 map(self.add, states)
176 def contains(self, state):
178 Checks if this StateSet contains the given state.
180 @param state: State to check
181 @type state: Accessibility.StateType
182 @return: True if the set contains the given state
185 return state in self.states
187 def add(self, *states):
189 Adds states to this set.
191 @param states: State(s) to add
192 @type states: Accessibility.StateType
194 self.states.add(state)
196 def remove(self, state):
198 Removes states from this set.
200 @param states: State(s) to remove
201 @type states: Accessibility.StateType
203 self.states.remove(state)
205 def equals(self, state_set):
207 Checks if this StateSet contains exactly the same members as the given
210 @param state_set: Another set
211 @type state_set: Accessibility.StateSet
212 @return: Are the sets equivalent in terms of their contents?
215 return set(state_set.getStates()) == self.states
217 def compare(self, state_set):
219 Finds the symmetric difference between this state set andthe one provided,
220 and returns it as a new StateSet.
222 @note: This does not use L{_StateSetImpl.compare} which cannot be
223 implemented at this time
224 @param state_set: Set to compare against
225 @type state_set: Accessibility.StateSet
226 @return: Proxy for the new set
229 a = set(self.getStates())
230 b = set(state_set.getStates())
231 diff = a.symmetric_difference(b)
232 return StateSet(*diff)
236 Checks if this StateSet is empty.
238 @return: Is it empty?
241 return len(self.states) == 0
245 Gets the sequence of all states in this set.
247 @return: List of states
250 return list(self.states)
252 #END----------------------------------------------------------------------------