forked from nvaccess/nvda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
2016 lines (1817 loc) · 76.5 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#A part of NonVisual Desktop Access (NVDA)
#Copyright (C) 2006-2018 NV Access Limited, Babbage B.V.
#This file is covered by the GNU General Public License.
#See the file COPYING for more details.
from comtypes.automation import IEnumVARIANT, VARIANT
from comtypes import COMError, IServiceProvider, GUID
from comtypes.hresult import S_OK, S_FALSE
import ctypes
import os
import re
import itertools
from comInterfaces.tom import ITextDocument
import tones
import languageHandler
import textInfos.offsets
from textInfos.offsets import HIGH_SURROGATE_FIRST, HIGH_SURROGATE_LAST, LOW_SURROGATE_FIRST, LOW_SURROGATE_LAST
import colors
import time
import displayModel
import IAccessibleHandler
import oleacc
import JABHandler
import winUser
import globalVars
from logHandler import log
import speech
import braille
import api
import config
import controlTypes
from NVDAObjects.window import Window
from NVDAObjects import NVDAObject, NVDAObjectTextInfo, InvalidNVDAObject
import NVDAObjects.JAB
import eventHandler
from NVDAObjects.behaviors import ProgressBar, Dialog, EditableTextWithAutoSelectDetection, FocusableUnfocusableContainer, ToolTip, Notification
from locationHelper import RectLTWH
def getNVDAObjectFromEvent(hwnd,objectID,childID):
try:
accHandle=IAccessibleHandler.accessibleObjectFromEvent(hwnd,objectID,childID)
except WindowsError:
accHandle=None
if not accHandle:
return None
(pacc,accChildID)=accHandle
obj=IAccessible(IAccessibleObject=pacc,IAccessibleChildID=accChildID,event_windowHandle=hwnd,event_objectID=objectID,event_childID=childID)
return obj
def getNVDAObjectFromPoint(x,y):
accHandle=IAccessibleHandler.accessibleObjectFromPoint(x,y)
if not accHandle:
return None
(pacc,child)=accHandle
obj=IAccessible(IAccessibleObject=pacc,IAccessibleChildID=child)
return obj
FORMAT_OBJECT_ATTRIBS = frozenset({"text-align"})
def normalizeIA2TextFormatField(formatField):
try:
textAlign=formatField.pop("text-align")
except KeyError:
textAlign=None
if textAlign:
formatField["text-align"]=textAlign
try:
fontWeight=formatField.pop("font-weight")
except KeyError:
fontWeight=None
if fontWeight is not None and (fontWeight.lower()=="bold" or (fontWeight.isdigit() and int(fontWeight)>=700)):
formatField["bold"]=True
else:
formatField["bold"]=False
try:
fontStyle=formatField.pop("font-style")
except KeyError:
fontStyle=None
if fontStyle is not None and fontStyle.lower()=="italic":
formatField["italic"]=True
else:
formatField["italic"]=False
try:
invalid=formatField.pop("invalid")
except KeyError:
invalid=None
if invalid:
invalid=invalid.lower()
if invalid=="spelling":
formatField["invalid-spelling"]=True
elif invalid=="grammar":
formatField["invalid-grammar"]=True
color=formatField.get('color')
if color:
try:
formatField['color']=colors.RGB.fromString(color)
except ValueError:
pass
backgroundColor=formatField.get('background-color')
if backgroundColor:
try:
formatField['background-color']=colors.RGB.fromString(backgroundColor)
except ValueError:
pass
lineStyle=formatField.get("text-underline-style")
lineType=formatField.get("text-underline-type")
if lineStyle or lineType:
formatField["underline"]=lineStyle!="none" and lineType!="none"
lineStyle=formatField.get("text-line-through-style")
lineType=formatField.get("text-line-through-type")
if lineStyle or lineType:
formatField["strikethrough"]=lineStyle!="none" and lineType!="none"
language=formatField.get('language')
if language:
formatField['language']=languageHandler.normalizeLanguage(language)
class IA2TextTextInfo(textInfos.offsets.OffsetsTextInfo):
detectFormattingAfterCursorMaybeSlow=False
def _getOffsetFromPoint(self,x,y):
if self.obj.IAccessibleTextObject.nCharacters>0:
offset = self.obj.IAccessibleTextObject.OffsetAtPoint(x,y,IAccessibleHandler.IA2_COORDTYPE_SCREEN_RELATIVE)
# IA2 specifies that a result of -1 indicates that
# the point is invalid or there is no character under the point.
# Note that Chromium does not follow the spec and returns 0 for invalid or no character points.
# As 0 is a valid offset, there's nothing we could do other than just returning it.
if offset == -1:
raise LookupError("Invalid point or no character under point")
return offset
else:
raise LookupError
@classmethod
def _getBoundingRectFromOffsetInObject(cls,obj,offset):
try:
res=RectLTWH(*obj.IAccessibleTextObject.characterExtents(offset,IAccessibleHandler.IA2_COORDTYPE_SCREEN_RELATIVE))
except COMError:
raise NotImplementedError
if not any(res[2:]):
# Gecko tends to return (0,0,0,0) rectangles sometimes, for example in empty text fields.
# Chromium could return rectangles that are positioned at the upper left corner of the object,
# and they have a width and height of 0.
# Other IA2 implementations, such as the one in LibreOffice,
# tend to return the caret rectangle in this case, which is ok.
raise LookupError
return res
def _getBoundingRectFromOffset(self,offset):
return self._getBoundingRectFromOffsetInObject(self.obj, offset)
def _get_unit_mouseChunk(self):
return "mouseChunk"
def expand(self,unit):
if unit==self.unit_mouseChunk:
isMouseChunkUnit=True
oldStart=self._startOffset
oldEnd=self._endOffset
unit=super(IA2TextTextInfo,self).unit_mouseChunk
else:
isMouseChunkUnit=False
super(IA2TextTextInfo,self).expand(unit)
if isMouseChunkUnit:
text=self._getTextRange(self._startOffset,self._endOffset)
if not text:
return
try:
self._startOffset=text.rindex(u'\ufffc',0,oldStart-self._startOffset)
except ValueError:
pass
try:
self._endOffset=text.index(u'\ufffc',oldEnd-self._startOffset)
except ValueError:
pass
def _getCaretOffset(self):
try:
offset=self.obj.IAccessibleTextObject.caretOffset
except COMError:
log.debugWarning("IAccessibleText::caretOffset failed", exc_info=True)
raise RuntimeError("Retrieving caret offset failed")
if offset<0:
raise RuntimeError("no active caret in this object")
return offset
def _setCaretOffset(self,offset):
self.obj.IAccessibleTextObject.SetCaretOffset(offset)
def _getSelectionOffsets(self):
try:
nSelections=self.obj.IAccessibleTextObject.nSelections
except COMError:
nSelections=0
if nSelections:
(start,end)=self.obj.IAccessibleTextObject.Selection[0]
else:
start=self._getCaretOffset()
end=start
return [min(start,end),max(start,end)]
def _setSelectionOffsets(self,start,end):
for selIndex in xrange(self.obj.IAccessibleTextObject.NSelections):
self.obj.IAccessibleTextObject.RemoveSelection(selIndex)
if start!=end:
self.obj.IAccessibleTextObject.AddSelection(start,end)
else:
# A collapsed selection is the caret.
# Specifically handling it here as a setCaretOffset gets around some strange bugs in Chrome where setting a collapsed selection selects an entire table cell.
self._setCaretOffset(start)
def _getStoryLength(self):
try:
return self.obj.IAccessibleTextObject.NCharacters
except COMError:
log.debugWarning("IAccessibleText::nCharacters failed",exc_info=True)
return 0
def _getLineCount(self):
return -1
def _getTextRange(self,start,end):
try:
return self.obj.IAccessibleTextObject.text(start,end) or u""
except COMError:
return u""
def _getFormatFieldAndOffsets(self,offset,formatConfig,calculateOffsets=True):
try:
startOffset,endOffset,attribsString=self.obj.IAccessibleTextObject.attributes(offset)
except COMError:
log.debugWarning("could not get attributes",exc_info=True)
return textInfos.FormatField(),(self._startOffset,self._endOffset)
formatField=textInfos.FormatField()
if attribsString is None and offset>0:
try:
attribsString=self.obj.IAccessibleTextObject.attributes(offset-1)[2]
except COMError:
pass
if attribsString:
formatField.update(IAccessibleHandler.splitIA2Attribs(attribsString))
objAttribs = self.obj.IA2Attributes
for attr in FORMAT_OBJECT_ATTRIBS:
try:
formatField[attr] = objAttribs[attr]
except KeyError:
pass
normalizeIA2TextFormatField(formatField)
return formatField,(startOffset,endOffset)
def _getCharacterOffsets(self,offset):
try:
if offset>=self.obj.IAccessibleTextObject.nCharacters:
return offset,offset+1
except COMError:
pass
try:
start,end,text = self.obj.IAccessibleTextObject.TextAtOffset(offset,IAccessibleHandler.IA2_TEXT_BOUNDARY_CHAR)
except COMError:
return super(IA2TextTextInfo,self)._getCharacterOffsets(offset)
if HIGH_SURROGATE_FIRST <= text <= HIGH_SURROGATE_LAST or LOW_SURROGATE_FIRST <= text <= LOW_SURROGATE_LAST:
# #8953: Some IA2 implementations, including Gecko and Chromium,
# erroneously report one offset for surrogates.
return super(IA2TextTextInfo,self)._getCharacterOffsets(offset)
return start, end
def _getWordOffsets(self,offset):
try:
if offset>=self.obj.IAccessibleTextObject.nCharacters:
return offset,offset+1
except COMError:
pass
try:
start,end,text=self.obj.IAccessibleTextObject.TextAtOffset(offset,IAccessibleHandler.IA2_TEXT_BOUNDARY_WORD)
except COMError:
return super(IA2TextTextInfo,self)._getWordOffsets(offset)
if start>offset or offset>end:
# HACK: Work around buggy implementations which return a range that does not include offset.
return offset,offset+1
return start,end
def _getLineOffsets(self,offset):
try:
start,end,text=self.obj.IAccessibleTextObject.TextAtOffset(offset,IAccessibleHandler.IA2_TEXT_BOUNDARY_LINE)
return start,end
except COMError:
log.debugWarning("IAccessibleText::textAtOffset failed",exc_info=True)
return offset,offset+1
def _getSentenceOffsets(self,offset):
try:
if offset>=self.obj.IAccessibleTextObject.nCharacters:
return offset,offset+1
except COMError:
pass
try:
start,end,text=self.obj.IAccessibleTextObject.TextAtOffset(offset,IAccessibleHandler.IA2_TEXT_BOUNDARY_SENTENCE)
if start==end:
raise NotImplementedError
return start,end
except COMError:
return super(IA2TextTextInfo,self)._getSentenceOffsets(offset)
def _getParagraphOffsets(self,offset):
try:
if offset>self.obj.IAccessibleTextObject.nCharacters:
return offset,offset+1
except COMError:
pass
try:
start,end,text=self.obj.IAccessibleTextObject.TextAtOffset(offset,IAccessibleHandler.IA2_TEXT_BOUNDARY_PARAGRAPH)
if start>=end:
raise RuntimeError("did not expand to paragraph correctly")
return start,end
except (RuntimeError,COMError):
return super(IA2TextTextInfo,self)._getParagraphOffsets(offset)
def _lineNumFromOffset(self,offset):
return -1
def _iterTextWithEmbeddedObjects(self, withFields, formatConfig=None):
"""Iterate through the text, splitting at embedded object characters.
Where an embedded object character occurs, its offset is provided.
@param withFields: Whether to output control/format fields.
@type withFields: bool
@param formatConfig: Document formatting configuration.
@return: A generator of fields, text strings and numeric offsets of embedded object characters.
"""
if withFields:
items = self.getTextWithFields(formatConfig=formatConfig)
else:
items = [self.text]
offset = self._startOffset
for item in items:
if not isinstance(item, basestring):
# This is a field.
yield item
continue
itemLen = len(item)
# The text consists of smaller chunks of text interspersed with embedded object characters.
chunkStart = 0
while chunkStart < itemLen:
# Find the next embedded object character.
try:
chunkEnd = item.index(u"\uFFFC", chunkStart)
except ValueError:
# This is the last chunk of text.
yield item[chunkStart:]
break
if chunkStart != chunkEnd:
yield item[chunkStart:chunkEnd]
# We've hit an embedded object character, so yield its offset.
yield offset + chunkEnd
chunkStart = chunkEnd + 1
offset += itemLen
class IAccessible(Window):
"""
the NVDAObject for IAccessible
@ivar IAccessibleChildID: the IAccessible object's child ID
@type IAccessibleChildID: int
"""
IAccessibleTableUsesTableCellIndexAttrib=False #: Should the table-cell-index IAccessible2 object attribute be used rather than indexInParent?
IA2UniqueID=None #: The cached IAccessible2::uniqueID if its implemented
@classmethod
def getPossibleAPIClasses(cls,kwargs,relation=None):
from . import MSHTML
yield MSHTML.MSHTML
@classmethod
def kwargsFromSuper(cls,kwargs,relation=None):
acc=None
objID=None
windowHandle=kwargs['windowHandle']
if isinstance(relation,tuple):
acc=IAccessibleHandler.accessibleObjectFromPoint(relation[0],relation[1])
elif relation=="focus":
objID=winUser.OBJID_CLIENT
acc=IAccessibleHandler.accessibleObjectFromEvent(windowHandle,objID,0)
if not acc:
return False
testAccFocus=acc
usedAccFocus=False
# Keep doing accFocus until we can't anymore or until accFocus keeps returning the same object.
while True:
testAccFocus=IAccessibleHandler.accFocus(testAccFocus[0])
# Only set the acc variable if we get something useful using accFocus.
if testAccFocus and testAccFocus!=acc:
acc=testAccFocus
usedAccFocus=True
else:
# We can't go any further.
break
if usedAccFocus:
# We don't know the event parameters for this object.
objID=None
# This object may also be in a different window, so we need to recalculate the window handle.
kwargs['windowHandle']=None
elif relation in ("parent","foreground"):
objID=winUser.OBJID_CLIENT
else:
objID=winUser.OBJID_WINDOW
if not acc and objID is not None:
acc=IAccessibleHandler.accessibleObjectFromEvent(windowHandle,objID,0)
if not acc:
return False
kwargs['IAccessibleObject']=acc[0]
kwargs['IAccessibleChildID']=acc[1]
if objID:
# We know the event parameters, so pass them to the NVDAObject.
kwargs['event_windowHandle']=windowHandle
kwargs['event_objectID']=objID
kwargs['event_childID']=0
return True
def findOverlayClasses(self,clsList):
if self.event_objectID==winUser.OBJID_CLIENT and JABHandler.isJavaWindow(self.windowHandle):
clsList.append(JavaVMRoot)
windowClassName=self.windowClassName
role=self.IAccessibleRole
if self.role in (controlTypes.ROLE_APPLICATION, controlTypes.ROLE_DIALOG) and not self.isFocusable:
# Make unfocusable applications focusable.
# This is particularly useful for ARIA applications.
# We use the NVDAObject role instead of IAccessible role here
# because of higher API classes; e.g. MSHTML.
clsList.insert(0, FocusableUnfocusableContainer)
if hasattr(self, "IAccessibleTextObject"):
if role==oleacc.ROLE_SYSTEM_TEXT or controlTypes.STATE_EDITABLE in self.states:
clsList.append(EditableTextWithAutoSelectDetection)
# Use window class name and role to search for a class match in our static map.
keys=[(windowClassName,role),(None,role),(windowClassName,None)]
normalizedWindowClassName=Window.normalizeWindowClassName(windowClassName)
if normalizedWindowClassName!=windowClassName:
keys.insert(1,(normalizedWindowClassName,role))
keys.append((normalizedWindowClassName,None))
for key in keys:
newCls=None
classString=_staticMap.get(key,None)
if classString and classString.find('.')>0:
modString,classString=os.path.splitext(classString)
classString=classString[1:]
mod=__import__(modString,globals(),locals(),[])
newCls=getattr(mod,classString)
elif classString:
newCls=globals()[classString]
if newCls:
clsList.append(newCls)
# Some special cases.
if windowClassName=="Frame Notification Bar" and role==oleacc.ROLE_SYSTEM_CLIENT:
clsList.append(IEFrameNotificationBar)
elif self.event_objectID==winUser.OBJID_CLIENT and self.event_childID==0 and windowClassName=="_WwG":
from winword import WordDocument
clsList.append(WordDocument)
elif self.event_objectID==winUser.OBJID_CLIENT and self.event_childID==0 and windowClassName in ("_WwN","_WwO"):
if self.windowControlID==18:
from winword import SpellCheckErrorField
clsList.append(SpellCheckErrorField)
else:
from winword import WordDocument_WwN
clsList.append(WordDocument_WwN)
elif windowClassName=="DirectUIHWND" and role==oleacc.ROLE_SYSTEM_TOOLBAR:
parentWindow=winUser.getAncestor(self.windowHandle,winUser.GA_PARENT)
if parentWindow and winUser.getClassName(parentWindow)=="Frame Notification Bar":
clsList.append(IENotificationBar)
if windowClassName.lower().startswith('mscandui'):
import mscandui
mscandui.findExtraOverlayClasses(self,clsList)
elif windowClassName=="GeckoPluginWindow" and self.event_objectID==0 and self.IAccessibleChildID==0:
from mozilla import GeckoPluginWindowRoot
clsList.append(GeckoPluginWindowRoot)
maybeFlash = False
if ((windowClassName in ("MozillaWindowClass", "GeckoPluginWindow") and not isinstance(self.IAccessibleObject, IAccessibleHandler.IAccessible2))
or windowClassName in ("MacromediaFlashPlayerActiveX", "ApolloRuntimeContentWindow", "ShockwaveFlash", "ShockwaveFlashLibrary", "ShockwaveFlashFullScreen", "GeckoFPSandboxChildWindow")):
maybeFlash = True
elif windowClassName == "Internet Explorer_Server" and self.event_objectID > 0:
# #2454: In Windows 8 IE, Flash is exposed in the same HWND as web content.
from .MSHTML import MSHTML
# This is only possibly Flash if it isn't MSHTML.
if not isinstance(self, MSHTML):
maybeFlash = True
if maybeFlash:
# This is possibly a Flash object.
from . import adobeFlash
adobeFlash.findExtraOverlayClasses(self, clsList)
elif windowClassName.startswith('Mozilla'):
from . import mozilla
mozilla.findExtraOverlayClasses(self, clsList)
elif self.event_objectID in (None,winUser.OBJID_CLIENT) and windowClassName.startswith('bosa_sdm'):
if role==oleacc.ROLE_SYSTEM_GRAPHIC and controlTypes.STATE_FOCUSED in self.states:
from .msOffice import SDMSymbols
clsList.append(SDMSymbols)
else:
from .msOffice import SDM
clsList.append(SDM)
elif windowClassName == "DirectUIHWND" and role == oleacc.ROLE_SYSTEM_TEXT:
from NVDAObjects.window import DisplayModelEditableText
clsList.append(DisplayModelEditableText)
elif windowClassName in ("ListBox","ComboLBox") and role == oleacc.ROLE_SYSTEM_LISTITEM:
windowStyle = self.windowStyle
if (windowStyle & winUser.LBS_OWNERDRAWFIXED or windowStyle & winUser.LBS_OWNERDRAWVARIABLE) and not windowStyle & winUser.LBS_HASSTRINGS:
# This is an owner drawn ListBox and text has not been set for the items.
# See http://msdn.microsoft.com/en-us/library/ms971352.aspx#msaa_sa_listbxcntrls
clsList.append(InaccessibleListBoxItem)
elif windowClassName == "ComboBox" and role == oleacc.ROLE_SYSTEM_COMBOBOX:
windowStyle = self.windowStyle
if (windowStyle & winUser.CBS_OWNERDRAWFIXED or windowStyle & winUser.CBS_OWNERDRAWVARIABLE) and not windowStyle & winUser.CBS_HASSTRINGS:
# This is an owner drawn ComboBox and text has not been set for the items.
# See http://msdn.microsoft.com/en-us/library/ms971352.aspx#msaa_sa_listbxcntrls
clsList.append(InaccessibleComboBox)
elif windowClassName == "MsoCommandBar":
from .msOffice import BrokenMsoCommandBar
if BrokenMsoCommandBar.appliesTo(self):
clsList.append(BrokenMsoCommandBar)
if role==oleacc.ROLE_SYSTEM_TOOLBAR:
from .msOffice import MsoCommandBarToolBar
clsList.append(MsoCommandBarToolBar)
if windowClassName.startswith("Internet Explorer_"):
from . import MSHTML
MSHTML.findExtraIAccessibleOverlayClasses(self, clsList)
elif windowClassName in ("AVL_AVView", "FoxitDocWnd"):
from . import adobeAcrobat
adobeAcrobat.findExtraOverlayClasses(self, clsList)
elif windowClassName == "WebViewWindowClass":
from . import webKit
webKit.findExtraOverlayClasses(self, clsList)
elif windowClassName.startswith("Chrome_"):
from . import chromium
chromium.findExtraOverlayClasses(self, clsList)
#Support for Windowless richEdit
if not hasattr(IAccessible,"IID_ITextServices"):
try:
IAccessible.IID_ITextServices=ctypes.cast(ctypes.windll.msftedit.IID_ITextServices,ctypes.POINTER(GUID)).contents
except WindowsError:
log.debugWarning("msftedit not available, couldn't retrieve IID_ITextServices")
IAccessible.IID_ITextServices=None
if IAccessible.IID_ITextServices:
try:
pDoc=self.IAccessibleObject.QueryInterface(IServiceProvider).QueryService(IAccessible.IID_ITextServices,ITextDocument)
except COMError:
pDoc=None
if pDoc:
self._ITextDocumentObject=pDoc
self.editAPIVersion=2
from NVDAObjects.window.edit import Edit
clsList.append(Edit)
#Window root IAccessibles
if self.event_childID==0 and self.IAccessibleRole==oleacc.ROLE_SYSTEM_WINDOW:
clsList.append(WindowRoot if self.event_objectID==winUser.OBJID_WINDOW else GenericWindow)
if self.event_objectID==winUser.OBJID_TITLEBAR and self.event_childID==0:
clsList.append(Titlebar)
clsList.append(IAccessible)
if self.event_objectID==winUser.OBJID_CLIENT and self.event_childID==0 and not isinstance(self.IAccessibleObject,IAccessibleHandler.IAccessible2):
# This is the main (client) area of the window, so we can use other classes at the window level.
# #3872: However, don't do this for IAccessible2 because
# IA2 supersedes window level APIs and might conflict with them.
super(IAccessible,self).findOverlayClasses(clsList)
#Generic client IAccessibles with no children should be classed as content and should use displayModel
if clsList[0]==IAccessible and len(clsList)==3 and self.IAccessibleRole==oleacc.ROLE_SYSTEM_CLIENT and self.childCount==0:
clsList.insert(0,ContentGenericClient)
def __init__(self,windowHandle=None,IAccessibleObject=None,IAccessibleChildID=None,event_windowHandle=None,event_objectID=None,event_childID=None):
"""
@param pacc: a pointer to an IAccessible object
@type pacc: ctypes.POINTER(IAccessible)
@param child: A child ID that will be used on all methods of the IAccessible pointer
@type child: int
@param hwnd: the window handle, if known
@type hwnd: int
@param objectID: the objectID for the IAccessible Object, if known
@type objectID: int
"""
self.IAccessibleObject=IAccessibleObject
self.IAccessibleChildID=IAccessibleChildID
# Try every trick in the book to get the window handle if we don't have it.
if not windowHandle and isinstance(IAccessibleObject,IAccessibleHandler.IAccessible2):
windowHandle=self.IA2WindowHandle
#Mozilla Gecko: we can never use a MozillaWindowClass window for Gecko 1.9
tempWindow=windowHandle
while tempWindow and winUser.getClassName(tempWindow)=="MozillaWindowClass":
tempWindow=winUser.getAncestor(tempWindow,winUser.GA_PARENT)
if tempWindow and winUser.getClassName(tempWindow).startswith('Mozilla'):
windowHandle=tempWindow
try:
Identity=IAccessibleHandler.getIAccIdentity(IAccessibleObject,IAccessibleChildID)
except COMError:
Identity=None
if event_windowHandle is None and Identity and 'windowHandle' in Identity:
event_windowHandle=Identity['windowHandle']
if event_objectID is None and Identity and 'objectID' in Identity:
event_objectID=Identity['objectID']
if event_childID is None and Identity and 'childID' in Identity:
event_childID=Identity['childID']
if not windowHandle and event_windowHandle:
windowHandle=event_windowHandle
if not windowHandle:
windowHandle=IAccessibleHandler.windowFromAccessibleObject(IAccessibleObject)
if not windowHandle:
log.debugWarning("Resorting to WindowFromPoint on accLocation")
try:
left,top,width,height = IAccessibleObject.accLocation(0)
windowHandle=winUser.user32.WindowFromPoint(winUser.POINT(left,top))
except COMError, e:
log.debugWarning("accLocation failed: %s" % e)
if not windowHandle:
raise InvalidNVDAObject("Can't get a window handle from IAccessible")
if isinstance(IAccessibleObject,IAccessibleHandler.IAccessible2):
try:
self.IA2UniqueID=IAccessibleObject.uniqueID
except COMError:
log.debugWarning("could not get IAccessible2::uniqueID to use as IA2UniqueID",exc_info=True)
# Set the event params based on our calculated/construction info if we must.
if event_windowHandle is None:
event_windowHandle=windowHandle
if event_objectID is None and isinstance(IAccessibleObject,IAccessibleHandler.IAccessible2):
event_objectID=winUser.OBJID_CLIENT
if event_childID is None:
if self.IA2UniqueID is not None:
event_childID=self.IA2UniqueID
else:
event_childID=IAccessibleChildID
self.event_windowHandle=event_windowHandle
self.event_objectID=event_objectID
self.event_childID=event_childID
super(IAccessible,self).__init__(windowHandle=windowHandle)
try:
self.IAccessibleActionObject=IAccessibleObject.QueryInterface(IAccessibleHandler.IAccessibleAction)
except COMError:
pass
try:
self.IAccessibleTable2Object=self.IAccessibleObject.QueryInterface(IAccessibleHandler.IAccessibleTable2)
except COMError:
try:
self.IAccessibleTableObject=self.IAccessibleObject.QueryInterface(IAccessibleHandler.IAccessibleTable)
except COMError:
pass
try:
self.IAccessibleTextObject=IAccessibleObject.QueryInterface(IAccessibleHandler.IAccessibleText)
except COMError:
pass
if None not in (event_windowHandle,event_objectID,event_childID):
IAccessibleHandler.liveNVDAObjectTable[(event_windowHandle,event_objectID,event_childID)]=self
def isDuplicateIAccessibleEvent(self,obj):
"""Compaires the object of an event to self to see if the event should be treeted as duplicate."""
#MSAA child elements do not have unique winEvent params as a childID could be reused if an element was deleted etc
if self.IAccessibleChildID>0:
return False
return obj.event_windowHandle==self.event_windowHandle and obj.event_objectID==self.event_objectID and obj.event_childID==self.event_childID
def _get_shouldAllowIAccessibleFocusEvent(self):
"""Determine whether a focus event should be allowed for this object.
Normally, this checks for the focused state to help eliminate redundant or invalid focus events.
However, some implementations do not correctly set the focused state, so this must be overridden.
@return: C{True} if the focus event should be allowed.
@rtype: bool
"""
#this object or one of its ancestors must have state_focused.
testObj = self
while testObj:
if controlTypes.STATE_FOCUSED in testObj.states:
break
parent = testObj.parent
# Cache the parent.
testObj.parent = parent
testObj = parent
else:
return False
return True
def _get_TextInfo(self):
if hasattr(self,'IAccessibleTextObject'):
return IA2TextTextInfo
return super(IAccessible,self).TextInfo
def _isEqual(self,other):
if self.IAccessibleChildID!=other.IAccessibleChildID:
return False
if self.IAccessibleObject==other.IAccessibleObject:
return True
if isinstance(self.IAccessibleObject,IAccessibleHandler.IAccessible2) and isinstance(other.IAccessibleObject,IAccessibleHandler.IAccessible2):
# These are both IAccessible2 objects, so we can test unique ID.
# Unique ID is only guaranteed to be unique within a given window, so we must check window handle as well.
selfIA2Window=self.IA2WindowHandle
selfIA2ID=self.IA2UniqueID
otherIA2Window=other.IA2WindowHandle
otherIA2ID=other.IA2UniqueID
if selfIA2Window!=otherIA2Window:
# The window handles are different, so these are definitely different windows.
return False
# At this point, we know that the window handles are equal.
if selfIA2Window and (selfIA2ID or otherIA2ID):
# The window handles are valid and one of the objects has a valid unique ID.
# Therefore, we can safely determine equality or inequality based on unique ID.
return selfIA2ID==otherIA2ID
if self.event_windowHandle is not None and other.event_windowHandle is not None and self.event_windowHandle!=other.event_windowHandle:
return False
if self.event_objectID is not None and other.event_objectID is not None and self.event_objectID!=other.event_objectID:
return False
if self.event_childID is not None and other.event_childID is not None and self.event_childID!=other.event_childID:
return False
if not super(IAccessible,self)._isEqual(other):
return False
selfIden=self.IAccessibleIdentity
otherIden=other.IAccessibleIdentity
if selfIden!=otherIden:
return False
if self.location!=other.location:
return False
if self.IAccessibleRole!=other.IAccessibleRole:
return False
if self.name!=other.name:
return False
return True
def _get_name(self):
#The edit field in a combo box should not have a label
if self.role==controlTypes.ROLE_EDITABLETEXT:
# Make sure to cache the parents.
parent=self.parent=self.parent
if parent and parent.role==controlTypes.ROLE_WINDOW:
# The parent of the edit field is a window, so try the next ancestor.
parent=self.parent.parent=self.parent.parent
# Only scrap the label on the edit field if the parent combo box has a label.
if parent and parent.role==controlTypes.ROLE_COMBOBOX and parent.name:
return ""
try:
res=self.IAccessibleObject.accName(self.IAccessibleChildID)
except COMError:
res=None
return res if isinstance(res,basestring) and not res.isspace() else None
def _get_value(self):
try:
res=self.IAccessibleObject.accValue(self.IAccessibleChildID)
except COMError:
res=None
return res if isinstance(res,basestring) and not res.isspace() else None
def _get_actionCount(self):
if hasattr(self,'IAccessibleActionObject'):
try:
return self.IAccessibleActionObject.nActions()
except COMError:
return 0
return 1
def getActionName(self,index=None):
if not index:
index=self.defaultActionIndex
if hasattr(self,'IAccessibleActionObject'):
try:
return self.IAccessibleActionObject.name(index)
except COMError:
raise NotImplementedError
elif index==0:
try:
action=self.IAccessibleObject.accDefaultAction(self.IAccessibleChildID)
except COMError:
action=None
if action:
return action
raise NotImplementedError
def doAction(self,index=None):
if not index:
index=self.defaultActionIndex
if hasattr(self,'IAccessibleActionObject'):
try:
self.IAccessibleActionObject.doAction(index)
return
except COMError:
raise NotImplementedError
elif index==0:
try:
if self.IAccessibleObject.accDoDefaultAction(self.IAccessibleChildID)!=0:
raise NotImplementedError
return
except COMError:
raise NotImplementedError
raise NotImplementedError
def _get_IAccessibleIdentity(self):
if not hasattr(self,'_IAccessibleIdentity'):
try:
self._IAccessibleIdentity=IAccessibleHandler.getIAccIdentity(self.IAccessibleObject,self.IAccessibleChildID)
except COMError:
self._IAccessibleIdentity=None
return self._IAccessibleIdentity
def _get_IAccessibleRole(self):
if isinstance(self.IAccessibleObject,IAccessibleHandler.IAccessible2):
try:
role=self.IAccessibleObject.role()
except COMError:
role=0
else:
role=0
if role==0:
try:
role=self.IAccessibleObject.accRole(self.IAccessibleChildID)
except COMError as e:
log.debugWarning("accRole failed: %s" % e)
role=0
return role
def _get_role(self):
IARole=self.IAccessibleRole
if IARole==oleacc.ROLE_SYSTEM_CLIENT:
superRole=super(IAccessible,self).role
if superRole!=controlTypes.ROLE_WINDOW:
return superRole
if isinstance(IARole,basestring):
IARole=IARole.split(',')[0].lower()
log.debug("IARole: %s"%IARole)
return IAccessibleHandler.IAccessibleRolesToNVDARoles.get(IARole,controlTypes.ROLE_UNKNOWN)
# #2569: Don't cache role,
# as it relies on other properties which might change when overlay classes are applied.
_cache_role = False
def _get_IAccessibleStates(self):
try:
res=self.IAccessibleObject.accState(self.IAccessibleChildID)
except COMError:
return 0
return res if isinstance(res,int) else 0
def _get_states(self):
states=set()
if self.event_objectID in (winUser.OBJID_CLIENT, winUser.OBJID_WINDOW) and self.event_childID == 0:
states.update(super(IAccessible, self).states)
try:
IAccessibleStates=self.IAccessibleStates
except COMError:
log.debugWarning("could not get IAccessible states",exc_info=True)
else:
states.update(IAccessibleHandler.IAccessibleStatesToNVDAStates[x] for x in (y for y in (1<<z for z in xrange(32)) if y&IAccessibleStates) if IAccessibleHandler.IAccessibleStatesToNVDAStates.has_key(x))
if not hasattr(self.IAccessibleObject,'states'):
# Not an IA2 object.
return states
IAccessible2States=self.IA2States
states=states|set(IAccessibleHandler.IAccessible2StatesToNVDAStates[x] for x in (y for y in (1<<z for z in xrange(32)) if y&IAccessible2States) if IAccessibleHandler.IAccessible2StatesToNVDAStates.has_key(x))
# Readonly should override editable
if controlTypes.STATE_READONLY in states:
states.discard(controlTypes.STATE_EDITABLE)
try:
IA2Attribs=self.IA2Attributes
except COMError:
log.debugWarning("could not get IAccessible2 attributes",exc_info=True)
IA2Attribs=None
if IA2Attribs:
grabbed = IA2Attribs.get("grabbed")
if grabbed == "false":
states.add(controlTypes.STATE_DRAGGABLE)
elif grabbed == "true":
states.add(controlTypes.STATE_DRAGGING)
if IA2Attribs.get("dropeffect", "none") != "none":
states.add(controlTypes.STATE_DROPTARGET)
sorted = IA2Attribs.get("sort")
if sorted=="ascending":
states.add(controlTypes.STATE_SORTED_ASCENDING)
elif sorted=="descending":
states.add(controlTypes.STATE_SORTED_DESCENDING)
elif sorted=="other":
states.add(controlTypes.STATE_SORTED)
if controlTypes.STATE_HASPOPUP in states and controlTypes.STATE_AUTOCOMPLETE in states:
states.remove(controlTypes.STATE_HASPOPUP)
if controlTypes.STATE_HALFCHECKED in states:
states.discard(controlTypes.STATE_CHECKED)
return states
re_positionInfoEncodedAccDescription=re.compile(r"L(?P<level>\d+)(?:, (?P<indexInGroup>\d+) of (?P<similarItemsInGroup>\d+))?")
def _get_decodedAccDescription(self):
try:
description=self.IAccessibleObject.accDescription(self.IAccessibleChildID)
except COMError:
return None
if not description:
return None
if description.lower().startswith('description:'):
return description[12:].strip()
m=self.re_positionInfoEncodedAccDescription.match(description)
if m:
return m
return description
hasEncodedAccDescription=False #:If true, accDescription contains info such as level, and number of items etc.
def _get_description(self):
if self.hasEncodedAccDescription:
d=self.decodedAccDescription
if isinstance(d,basestring):
return d
else:
return ""
try:
res=self.IAccessibleObject.accDescription(self.IAccessibleChildID)
except COMError:
res=None
return res if isinstance(res,basestring) and not res.isspace() else None
def _get_keyboardShortcut(self):
try:
res=self.IAccessibleObject.accKeyboardShortcut(self.IAccessibleChildID)
except COMError:
res=None
return res if isinstance(res,basestring) and not res.isspace() else None
def _get_childCount(self):
if self.IAccessibleChildID!=0:
return 0
try:
return max(self.IAccessibleObject.accChildCount,0)
except COMError:
return 0
def _get_location(self):
try:
return RectLTWH(*self.IAccessibleObject.accLocation(self.IAccessibleChildID))
except COMError:
return None
def isPointInObject(self,x,y):
if self.windowHandle and not super(IAccessible,self).isPointInObject(x,y):
return False
res=IAccessibleHandler.accHitTest(self.IAccessibleObject,self.IAccessibleChildID,x,y)
if not res or res[0]!=self.IAccessibleObject or res[1]!=self.IAccessibleChildID:
return False
return True
def _get_labeledBy(self):
try:
(pacc,accChild)=IAccessibleHandler.accNavigate(self.IAccessibleObject,self.IAccessibleChildID,IAccessibleHandler.NAVRELATION_LABELLED_BY)
obj=IAccessible(IAccessibleObject=pacc,IAccessibleChildID=accChild)
return obj
except COMError:
return None
def _get_parent(self):
if self.IAccessibleChildID!=0:
return IAccessible(windowHandle=self.windowHandle,IAccessibleObject=self.IAccessibleObject,IAccessibleChildID=0,event_windowHandle=self.event_windowHandle,event_objectID=self.event_objectID,event_childID=0) or super(IAccessible,self).parent
res=IAccessibleHandler.accParent(self.IAccessibleObject,self.IAccessibleChildID)
if res:
parentObj=IAccessible(IAccessibleObject=res[0],IAccessibleChildID=res[1])
if parentObj:
#Hack around bad MSAA implementations that deliberately skip the window root IAccessible in the ancestry (Skype, iTunes)
if parentObj.windowHandle!=self.windowHandle and self.IAccessibleRole!=oleacc.ROLE_SYSTEM_WINDOW and winUser.getAncestor(self.windowHandle,winUser.GA_PARENT)==parentObj.windowHandle:
windowObj=Window(windowHandle=self.windowHandle)
if windowObj and isinstance(windowObj,IAccessible) and windowObj.IAccessibleRole==oleacc.ROLE_SYSTEM_WINDOW and windowObj.parent==parentObj:
return windowObj
return self.correctAPIForRelation(parentObj,relation="parent") or super(IAccessible,self).parent
return super(IAccessible,self).parent
def _get_next(self):
res=IAccessibleHandler.accNavigate(self.IAccessibleObject,self.IAccessibleChildID,oleacc.NAVDIR_NEXT)
if not res:
return None
if res[0]==self.IAccessibleObject:
#A sanity check for childIDs.
if self.IAccessibleChildID>0 and res[1]>self.IAccessibleChildID:
return IAccessible(windowHandle=self.windowHandle,IAccessibleObject=self.IAccessibleObject,IAccessibleChildID=res[1],event_windowHandle=self.event_windowHandle,event_objectID=self.event_objectID,event_childID=res[1])
else:
return self.correctAPIForRelation(IAccessible(IAccessibleObject=res[0],IAccessibleChildID=res[1]))
def _get_previous(self):
res=IAccessibleHandler.accNavigate(self.IAccessibleObject,self.IAccessibleChildID,oleacc.NAVDIR_PREVIOUS)
if not res:
return None
if res[0]==self.IAccessibleObject:
#A sanity check for childIDs.
if self.IAccessibleChildID>1 and res[1]<self.IAccessibleChildID:
return IAccessible(windowHandle=self.windowHandle,IAccessibleObject=self.IAccessibleObject,IAccessibleChildID=res[1],event_windowHandle=self.event_windowHandle,event_objectID=self.event_objectID,event_childID=res[1])
else:
return self.correctAPIForRelation(IAccessible(IAccessibleObject=res[0],IAccessibleChildID=res[1]))
def _get_firstChild(self):
child=IAccessibleHandler.accNavigate(self.IAccessibleObject,self.IAccessibleChildID,oleacc.NAVDIR_FIRSTCHILD)
if not child and self.IAccessibleChildID==0:
children=IAccessibleHandler.accessibleChildren(self.IAccessibleObject,0,1)
if len(children)>0:
child=children[0]