1
2
3
4
5
6
7 """General functions for NVDA"""
8
9 import config
10 import textInfos
11 import globalVars
12 from logHandler import log
13 import ui
14 import treeInterceptorHandler
15 import NVDAObjects
16 import NVDAObjects.IAccessible
17 import winUser
18 import controlTypes
19 import win32clipboard
20 import win32con
21 import eventHandler
22 import braille
23 import watchdog
24
25
26
28 """
29 Gets the current object with focus.
30 @returns: the object with focus
31 @rtype: L{NVDAObjects.NVDAObject}
32 """
33 return globalVars.focusObject
34
36 """Gets the current foreground object.
37 @returns: the current foreground object
38 @rtype: L{NVDAObjects.NVDAObject}
39 """
40 return globalVars.foregroundObject
41
43 """Stores the given object as the current foreground object. (Note: it does not physically change the operating system foreground window, but only allows NVDA to keep track of what it is).
44 @param obj: the object that will be stored as the current foreground object
45 @type obj: NVDAObjects.NVDAObject
46 """
47 if not isinstance(obj,NVDAObjects.NVDAObject):
48 return False
49 globalVars.foregroundObject=obj
50 return True
51
53 """Stores an object as the current focus object. (Note: this does not physically change the window with focus in the operating system, but allows NVDA to keep track of the correct object).
54 Before overriding the last object, this function calls event_loseFocus on the object to notify it that it is loosing focus.
55 @param obj: the object that will be stored as the focus object
56 @type obj: NVDAObjects.NVDAObject
57 """
58 if not isinstance(obj,NVDAObjects.NVDAObject):
59 return False
60 if globalVars.focusObject:
61 eventHandler.executeEvent("loseFocus",globalVars.focusObject)
62 oldFocusLine=globalVars.focusAncestors
63
64 if globalVars.focusObject:
65 oldFocusLine.append(globalVars.focusObject)
66 oldAppModuleSet=set(o.appModule for o in oldFocusLine if o and o.appModule)
67 ancestors=[]
68 tempObj=obj
69 matchedOld=False
70 focusDifferenceLevel=0
71 oldFocusLineLength=len(oldFocusLine)
72
73 safetyCount=0
74 while tempObj:
75 if safetyCount<100:
76 safetyCount+=1
77 else:
78 try:
79 log.error("Never ending focus ancestry: last object: %s, %s, window class %s, application name %s"%(tempObj.name,controlTypes.speechRoleLabels[tempObj.role],tempObj.windowClassName,tempObj.appModule.appName))
80 except:
81 pass
82 tempObj=getDesktopObject()
83
84 for index in xrange(oldFocusLineLength-1,-1,-1):
85 watchdog.alive()
86 if tempObj==oldFocusLine[index]:
87
88
89 origAncestors=oldFocusLine[0:index+1]
90
91 if ancestors and origAncestors:
92 ancestors[0].parent=origAncestors[-1]
93 origAncestors.extend(ancestors)
94 ancestors=origAncestors
95 focusDifferenceLevel=index+1
96
97 matchedOld=True
98 break
99 if matchedOld:
100 break
101
102 ancestors.insert(0,tempObj)
103 container=tempObj.container
104 tempObj.container=container
105 tempObj=container
106
107 del ancestors[-1]
108 newAppModuleSet=set(o.appModule for o in ancestors+[obj] if o and o.appModule)
109 for removedMod in oldAppModuleSet-newAppModuleSet:
110 if not removedMod.sleepMode and hasattr(removedMod,'event_appModule_loseFocus'):
111 try:
112 removedMod.event_appModule_loseFocus()
113 except watchdog.CallCancelled:
114 pass
115 for addedMod in newAppModuleSet-oldAppModuleSet:
116 if not addedMod.sleepMode and hasattr(addedMod,'event_appModule_gainFocus'):
117 addedMod.event_appModule_gainFocus()
118 try:
119 treeInterceptorHandler.cleanup()
120 except watchdog.CallCancelled:
121 pass
122 treeInterceptorObject=None
123 o=None
124 watchdog.alive()
125 for o in ancestors[focusDifferenceLevel:]+[obj]:
126 treeInterceptorObject=treeInterceptorHandler.update(o)
127
128
129 if obj is o or obj in treeInterceptorObject:
130 obj.treeInterceptor=treeInterceptorObject
131 else:
132 obj.treeInterceptor=None
133
134 globalVars.focusDifferenceLevel=focusDifferenceLevel
135 globalVars.focusObject=obj
136 globalVars.focusAncestors=ancestors
137 braille.invalidateCachedFocusAncestors(focusDifferenceLevel)
138 if config.conf["reviewCursor"]["followFocus"]:
139 setNavigatorObject(obj if not obj.treeInterceptor or obj.treeInterceptor.passThrough or not obj.treeInterceptor.isReady else obj.treeInterceptor.rootNVDAObject)
140 return True
141
144
147
151
153 """Tells NVDA to remember the given object as the object that is directly under the mouse"""
154 globalVars.mouseObject=obj
155
159
163
180
189
201
203 """Sets an object to be the current navigator object. Navigator objects can be used to navigate around the operating system (with the number pad) with out moving the focus. It also sets the current review position to None so that next time the review position is asked for, it is created from the navigator object.
204 @param obj: the object that will be set as the current navigator object
205 @type obj: NVDAObjects.NVDAObject
206 """
207 if not isinstance(obj,NVDAObjects.NVDAObject):
208 return False
209 globalVars.navigatorObject=obj
210 globalVars.reviewPosition=None
211 globalVars.reviewPositionObj=None
212 eventHandler.executeEvent("becomeNavigatorObject",obj)
213
224
226 """Breaks down the given integer in to a list of numbers that are 2 to the power of their position."""
227 return [x for x in [1<<y for y in xrange(32)] if x&states]
228
229
231 """Moves the mouse to the given NVDA object's position"""
232 location=obj.location
233 if location and (len(location)==4):
234 (left,top,width,height)=location
235 x=(left+left+width)/2
236 y=(top+top+height)/2
237 winUser.setCursorPos(x,y)
238
253
255 """Copies the given text to the windows clipboard.
256 @returns: True if it succeeds, False otherwise.
257 @rtype: boolean
258 @param text: the text which will be copied to the clipboard
259 @type text: string
260 """
261 if isinstance(text,basestring) and len(text)>0 and not text.isspace():
262 try:
263 win32clipboard.OpenClipboard()
264 except win32clipboard.error:
265 return False
266 try:
267 win32clipboard.EmptyClipboard()
268 win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, text)
269 finally:
270 win32clipboard.CloseClipboard()
271 win32clipboard.OpenClipboard()
272 try:
273 got = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
274 finally:
275 win32clipboard.CloseClipboard()
276 if got == text:
277 return True
278 return False
279
281 """Receives text from the windows clipboard.
282 @returns: Clipboard text
283 @rtype: string
284 """
285 text = ""
286 win32clipboard.OpenClipboard()
287 try:
288 text = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
289 finally:
290 win32clipboard.CloseClipboard()
291 return text
292
294 """Obtain the status bar for the current foreground object.
295 @return: The status bar object or C{None} if no status bar was found.
296 @rtype: L{NVDAObjects.NVDAObject}
297 """
298
299
300 foreground = getForegroundObject()
301 location=foreground.location
302 if not location:
303 return None
304 left, top, width, height = location
305 bottom = top + height - 1
306 obj = NVDAObjects.IAccessible.getNVDAObjectFromPoint(left, bottom)
307
308
309 while obj and not obj.role == controlTypes.ROLE_STATUSBAR:
310 obj = obj.parent
311
312 return obj
313
315 """Get the text from a status bar.
316 This includes the name of the status bar and the names and values of all of its children.
317 @param obj: The status bar.
318 @type obj: L{NVDAObjects.NVDAObject}
319 @return: The status bar text.
320 @rtype: str
321 """
322 text = obj.name
323 if text is None:
324 text = ""
325 return text + " ".join(chunk for child in obj.children for chunk in (child.name, child.value) if chunk and isinstance(chunk, basestring) and not chunk.isspace())
326
328 """Replaces invalid characters in a given string to make a windows compatible file name.
329 @param name: The file name to filter.
330 @type name: str
331 @returns: The filtered file name.
332 @rtype: str
333 """
334 invalidChars=':?*\|<>/"'
335 for c in invalidChars:
336 name=name.replace(c,'_')
337 return name
338