Package NVDAObjects :: Package window :: Module excel
[hide private]
[frames] | no frames]

Source Code for Module NVDAObjects.window.excel

  1  #NVDAObjects/excel.py 
  2  #A part of NonVisual Desktop Access (NVDA) 
  3  #Copyright (C) 2006-2007 NVDA Contributors <http://www.nvda-project.org/> 
  4  #This file is covered by the GNU General Public License. 
  5  #See the file COPYING for more details. 
  6   
  7  from comtypes import COMError 
  8  import comtypes.automation 
  9  import wx 
 10  import oleacc 
 11  import textInfos 
 12  import colors 
 13  import eventHandler 
 14  import gui 
 15  import winUser 
 16  import controlTypes 
 17  from . import Window 
 18  from .. import NVDAObjectTextInfo 
 19  import scriptHandler 
 20   
 21  xlA1 = 1 
22 23 -class ExcelBase(Window):
24 """A base that all Excel NVDAObjects inherit from, which contains some useful methods.""" 25 26 @staticmethod
27 - def excelWindowObjectFromWindow(windowHandle):
28 try: 29 pDispatch=oleacc.AccessibleObjectFromWindow(windowHandle,winUser.OBJID_NATIVEOM,interface=comtypes.automation.IDispatch) 30 except (COMError,WindowsError): 31 return None 32 return comtypes.client.dynamic.Dispatch(pDispatch)
33 34 @staticmethod
35 - def getCellAddress(cell, external=False):
36 return cell.Address(False, False, xlA1, external)
37
38 - def fireFocusOnSelection(self):
39 selection=self.excelWindowObject.Selection 40 if selection.Count>1: 41 obj=ExcelSelection(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelRangeObject=selection) 42 else: 43 obj=ExcelCell(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelCellObject=selection) 44 eventHandler.executeEvent("gainFocus",obj)
45
46 -class Excel7Window(ExcelBase):
47 """An overlay class for Window for the EXCEL7 window class, which simply bounces focus to the active excel cell.""" 48
49 - def _get_excelWindowObject(self):
50 return self.excelWindowObjectFromWindow(self.windowHandle)
51
52 - def event_gainFocus(self):
54
55 -class ExcelWorksheet(ExcelBase):
56 57 role=controlTypes.ROLE_TABLE 58
59 - def __init__(self,windowHandle=None,excelWindowObject=None,excelWorksheetObject=None):
60 self.excelWindowObject=excelWindowObject 61 self.excelWorksheetObject=excelWorksheetObject 62 super(ExcelWorksheet,self).__init__(windowHandle=windowHandle) 63 for gesture in self.__changeSelectionGestures: 64 self.bindGesture(gesture, "changeSelection")
65
66 - def _get_name(self):
67 return self.excelWorksheetObject.name
68
69 - def _isEqual(self, other):
70 if not super(ExcelWorksheet, self)._isEqual(other): 71 return False 72 return self.excelWorksheetObject.index == other.excelWorksheetObject.index
73
74 - def _get_firstChild(self):
75 cell=self.excelWorksheetObject.cells(1,1) 76 return ExcelCell(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelCellObject=cell)
77
78 - def script_changeSelection(self,gesture):
79 gesture.send() 80 if scriptHandler.isScriptWaiting(): 81 # Prevent lag if keys are pressed rapidly. 82 return 83 self.fireFocusOnSelection()
84 script_changeSelection.canPropagate=True 85 86 __changeSelectionGestures = ( 87 "kb:tab", 88 "kb:shift+tab", 89 "kb:upArrow", 90 "kb:downArrow", 91 "kb:leftArrow", 92 "kb:rightArrow", 93 "kb:control+upArrow", 94 "kb:control+downArrow", 95 "kb:control+leftArrow", 96 "kb:control+rightArrow", 97 "kb:home", 98 "kb:end", 99 "kb:control+home", 100 "kb:control+end", 101 "kb:shift+upArrow", 102 "kb:shift+downArrow", 103 "kb:shift+leftArrow", 104 "kb:shift+rightArrow", 105 "kb:shift+control+upArrow", 106 "kb:shift+control+downArrow", 107 "kb:shift+control+leftArrow", 108 "kb:shift+control+rightArrow", 109 "kb:shift+home", 110 "kb:shift+end", 111 "kb:shift+control+home", 112 "kb:shift+control+end", 113 "kb:shift+space", 114 "kb:control+space", 115 "kb:control+pageUp", 116 "kb:control+pageDown", 117 "kb:control+v", 118 )
119
120 -class ExcelCellTextInfo(NVDAObjectTextInfo):
121
122 - def _getFormatFieldAndOffsets(self,offset,formatConfig,calculateOffsets=True):
123 formatField=textInfos.FormatField() 124 fontObj=self.obj.excelCellObject.font 125 if formatConfig['reportFontName']: 126 formatField['font-name']=fontObj.name 127 if formatConfig['reportFontSize']: 128 formatField['font-size']=str(fontObj.size) 129 if formatConfig['reportFontAttributes']: 130 formatField['bold']=fontObj.bold 131 formatField['italic']=fontObj.italic 132 formatField['underline']=fontObj.underline 133 if formatConfig['reportColor']: 134 try: 135 formatField['color']=colors.RGB.fromCOLORREF(int(fontObj.color)) 136 except COMError: 137 pass 138 try: 139 formatField['background-color']=colors.RGB.fromCOLORREF(int(self.obj.excelCellObject.interior.color)) 140 except COMError: 141 pass 142 return formatField,(self._startOffset,self._endOffset)
143
144 -class ExcelCell(ExcelBase):
145 146 @classmethod
147 - def kwargsFromSuper(cls,kwargs,relation=None):
148 windowHandle=kwargs['windowHandle'] 149 excelWindowObject=cls.excelWindowObjectFromWindow(windowHandle) 150 if not excelWindowObject: 151 return False 152 if isinstance(relation,tuple): 153 excelCellObject=excelWindowObject.rangeFromPoint(relation[0],relation[1]) 154 else: 155 excelCellObject=excelWindowObject.ActiveCell 156 if not excelCellObject: 157 return False 158 kwargs['excelWindowObject']=excelWindowObject 159 kwargs['excelCellObject']=excelCellObject 160 return True
161
162 - def __init__(self,windowHandle=None,excelWindowObject=None,excelCellObject=None):
163 self.excelWindowObject=excelWindowObject 164 self.excelCellObject=excelCellObject 165 super(ExcelCell,self).__init__(windowHandle=windowHandle)
166 167 role=controlTypes.ROLE_TABLECELL 168 169 TextInfo=ExcelCellTextInfo 170
171 - def _isEqual(self,other):
172 if not super(ExcelCell,self)._isEqual(other): 173 return False 174 thisAddr=self.getCellAddress(self.excelCellObject,True) 175 try: 176 otherAddr=self.getCellAddress(other.excelCellObject,True) 177 except COMError: 178 #When cutting and pasting the old selection can become broken 179 return False 180 return thisAddr==otherAddr
181
182 - def _get_name(self):
183 return self.getCellAddress(self.excelCellObject)
184
185 - def _get_value(self):
186 return self.excelCellObject.Text
187
188 - def _get_description(self):
189 return _("has formula") if self.excelCellObject.HasFormula else ""
190
191 - def _get_parent(self):
192 worksheet=self.excelCellObject.Worksheet 193 return ExcelWorksheet(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelWorksheetObject=worksheet)
194
195 - def _get_next(self):
196 try: 197 next=self.excelCellObject.next 198 except COMError: 199 next=None 200 if next: 201 return ExcelCell(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelCellObject=next)
202
203 - def _get_previous(self):
204 try: 205 previous=self.excelCellObject.previous 206 except COMError: 207 previous=None 208 if previous: 209 return ExcelCell(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelCellObject=previous)
210
211 -class ExcelSelection(ExcelBase):
212 213 role=controlTypes.ROLE_TABLECELL 214
215 - def __init__(self,windowHandle=None,excelWindowObject=None,excelRangeObject=None):
216 self.excelWindowObject=excelWindowObject 217 self.excelRangeObject=excelRangeObject 218 super(ExcelSelection,self).__init__(windowHandle=windowHandle)
219
220 - def _get_states(self):
221 states=super(ExcelSelection,self).states 222 states.add(controlTypes.STATE_SELECTED) 223 return states
224
225 - def _get_name(self):
226 firstCell=self.excelRangeObject.Item(1) 227 lastCell=self.excelRangeObject.Item(self.excelRangeObject.Count) 228 return _("%s %s through %s %s")%(self.getCellAddress(firstCell),firstCell.Text,self.getCellAddress(lastCell),lastCell.Text)
229
230 - def _get_parent(self):
231 worksheet=self.excelRangeObject.Worksheet 232 return ExcelWorksheet(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelWorksheetObject=worksheet)
233 234 #Its useful for an excel selection to be announced with reportSelection script
235 - def makeTextInfo(self,position):
236 if position==textInfos.POSITION_SELECTION: 237 position=textInfos.POSITION_ALL 238 return super(ExcelSelection,self).makeTextInfo(position)
239