~stomato463/+junk/nvdajp

« back to all changes in this revision

Viewing changes to source/NVDAObjects/window/excel.py

  • Committer: Masataka Shinke
  • Date: 2011-10-25 12:35:26 UTC
  • mfrom: (4185 jpmain)
  • mto: This revision was merged to the branch mainline in revision 4211.
  • Revision ID: mshinke@users.sourceforge.jp-20111025123526-ze527a2rl3z0g2ky
lp:~nishimotz/nvdajp/main : 4185 をマージ

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#This file is covered by the GNU General Public License.
5
5
#See the file COPYING for more details.
6
6
 
7
 
import time
8
 
import re
9
 
import ctypes
10
7
from comtypes import COMError
11
8
import comtypes.automation
12
9
import wx
13
10
import oleacc
14
 
import textInfos.offsets
 
11
import textInfos
 
12
import colors
15
13
import eventHandler
16
14
import gui
17
 
import gui.scriptUI
18
15
import winUser
19
16
import controlTypes
20
 
import speech
21
 
from keyUtils import sendKey, key
22
17
from . import Window
23
18
from .. import NVDAObjectTextInfo
24
 
import appModuleHandler
25
 
 
26
 
re_dollaredAddress=re.compile(r"^\$?([a-zA-Z]+)\$?([0-9]+)")
27
 
 
28
 
class CellEditDialog(gui.scriptUI.ModalDialog):
29
 
 
30
 
        def __init__(self,cell):
31
 
                super(CellEditDialog,self).__init__(None)
32
 
                self._cell=cell
33
 
 
34
 
        def onCellTextChar(self,evt):
35
 
                if evt.GetKeyCode() == wx.WXK_RETURN:
36
 
                        if evt.AltDown():
37
 
                                i=self._cellText.GetInsertionPoint()
38
 
                                self._cellText.Replace(i,i,"\n")
39
 
                        else:
40
 
                                self.onOk(None)
41
 
                        return
42
 
                evt.Skip(True)
43
 
 
44
 
        def onOk(self,evt):
45
 
                self._cell.formulaLocal=self._cellText.GetValue()
46
 
                self.dialog.EndModal(wx.ID_OK)
47
 
 
48
 
        def makeDialog(self):
49
 
                d=wx.Dialog(gui.mainFrame, wx.ID_ANY, title=_("NVDA Excel Cell Editor"))
50
 
                mainSizer = wx.BoxSizer(wx.VERTICAL)
51
 
                mainSizer.Add(wx.StaticText(d,wx.ID_ANY, label=_("Enter cell contents")))
52
 
                self._cellText=wx.TextCtrl(d, wx.ID_ANY, size=(300, 200), style=wx.TE_RICH|wx.TE_MULTILINE)
53
 
                self._cellText.Bind(wx.EVT_KEY_DOWN, self.onCellTextChar)
54
 
                self._cellText.SetValue(self._cell.formulaLocal)
55
 
                mainSizer.Add(self._cellText)
56
 
                mainSizer.Add(d.CreateButtonSizer(wx.OK|wx.CANCEL))
57
 
                d.Bind(wx.EVT_BUTTON,self.onOk,id=wx.ID_OK)
58
 
                d.SetSizer(mainSizer)
59
 
                self._cellText.SetFocus()
60
 
                return d
61
 
 
62
 
class ExcelWindow(Window):
63
 
        """A base that all Excel NVDAObjects inherit from, which contains some useful static methods."""
 
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."""
64
25
 
65
26
        @staticmethod
66
27
        def excelWindowObjectFromWindow(windowHandle):
71
32
                return comtypes.client.dynamic.Dispatch(pDispatch)
72
33
 
73
34
        @staticmethod
74
 
        def getCellAddress(cell):
75
 
                return re_dollaredAddress.sub(r"\1\2",cell.Address())
76
 
 
77
 
class Excel7Window(ExcelWindow):
 
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):
78
47
        """An overlay class for Window for the EXCEL7 window class, which simply bounces focus to the active excel cell."""
79
48
 
80
49
        def _get_excelWindowObject(self):
81
50
                return self.excelWindowObjectFromWindow(self.windowHandle)
82
51
 
83
52
        def event_gainFocus(self):
84
 
                activeCell=self.excelWindowObject.ActiveCell
85
 
                obj=ExcelCell(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelCellObject=activeCell)
86
 
                eventHandler.executeEvent("gainFocus",obj)
 
53
                self.fireFocusOnSelection()
87
54
 
88
 
class ExcelWorksheet(ExcelWindow):
 
55
class ExcelWorksheet(ExcelBase):
89
56
 
90
57
        role=controlTypes.ROLE_TABLE
91
58
 
93
60
                self.excelWindowObject=excelWindowObject
94
61
                self.excelWorksheetObject=excelWorksheetObject
95
62
                super(ExcelWorksheet,self).__init__(windowHandle=windowHandle)
 
63
                for gesture in self.__changeSelectionGestures:
 
64
                        self.bindGesture(gesture, "changeSelection")
96
65
 
97
66
        def _get_name(self):
98
67
                return self.excelWorksheetObject.name
99
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
 
100
74
        def _get_firstChild(self):
101
75
                cell=self.excelWorksheetObject.cells(1,1)
102
76
                return ExcelCell(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelCellObject=cell)
103
77
 
104
 
 
105
 
        def script_extendSelection(self,keyPress):
106
 
                sendKey(keyPress)
107
 
                selection=self.excelWindowObject.Selection
108
 
                if selection.Count>1:
109
 
                        obj=ExcelSelection(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelRangeObject=selection)
110
 
                else:
111
 
                        obj=ExcelCell(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelCellObject=selection)
112
 
                eventHandler.executeEvent("gainFocus",obj)
113
 
        script_extendSelection.__doc__=_("Extends the selection and speaks the last selected cell")
114
 
        script_extendSelection.canPropagate=True
115
 
 
116
 
        def script_moveByCell(self,keyPress):
117
 
                """Moves to a cell and speaks its coordinates and content"""
118
 
                sendKey(keyPress)
119
 
                activeCell=self.excelWindowObject.ActiveCell
120
 
                obj=ExcelCell(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelCellObject=activeCell)
121
 
                eventHandler.executeEvent("gainFocus",obj)
122
 
        script_moveByCell.__doc__=_("Moves to a cell and speaks its coordinates and content")
123
 
        script_moveByCell.canPropagate=True
124
 
 
125
 
[ExcelWorksheet.bindKey(keyName,scriptName) for keyName,scriptName in [
126
 
        ("Tab","moveByCell"),
127
 
        ("Shift+Tab","moveByCell"),
128
 
        ("ExtendedUp","moveByCell"),
129
 
        ("ExtendedDown","moveByCell"),
130
 
        ("ExtendedLeft","moveByCell"),
131
 
        ("ExtendedRight","moveByCell"),
132
 
        ("Control+ExtendedUp","moveByCell"),
133
 
        ("Control+ExtendedDown","moveByCell"),
134
 
        ("Control+ExtendedLeft","moveByCell"),
135
 
        ("Control+ExtendedRight","moveByCell"),
136
 
        ("ExtendedHome","moveByCell"),
137
 
        ("ExtendedEnd","moveByCell"),
138
 
        ("Control+ExtendedHome","moveByCell"),
139
 
        ("Control+ExtendedEnd","moveByCell"),
140
 
        ("Shift+ExtendedUp","extendSelection"),
141
 
        ("Shift+ExtendedDown","extendSelection"),
142
 
        ("Shift+ExtendedLeft","extendSelection"),
143
 
        ("Shift+ExtendedRight","extendSelection"),
144
 
        ("Shift+Control+ExtendedUp","extendSelection"),
145
 
        ("Shift+Control+ExtendedDown","extendSelection"),
146
 
        ("Shift+Control+ExtendedLeft","extendSelection"),
147
 
        ("Shift+Control+ExtendedRight","extendSelection"),
148
 
        ("Shift+ExtendedHome","extendSelection"),
149
 
        ("Shift+ExtendedEnd","extendSelection"),
150
 
        ("Shift+Control+ExtendedHome","extendSelection"),
151
 
        ("Shift+Control+ExtendedEnd","extendSelection"),
152
 
]]
153
 
 
154
 
class ExcelCellTextInfo(textInfos.offsets.OffsetsTextInfo):
 
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):
155
121
 
156
122
        def _getFormatFieldAndOffsets(self,offset,formatConfig,calculateOffsets=True):
157
123
                formatField=textInfos.FormatField()
164
130
                        formatField['bold']=fontObj.bold
165
131
                        formatField['italic']=fontObj.italic
166
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
167
142
                return formatField,(self._startOffset,self._endOffset)
168
143
 
169
 
        def _getTextRange(self,start,end):
170
 
                text=self.obj.excelCellObject.Text
171
 
                return text[start:end]
172
 
 
173
 
class ExcelCell(ExcelWindow):
 
144
class ExcelCell(ExcelBase):
174
145
 
175
146
        @classmethod
176
147
        def kwargsFromSuper(cls,kwargs,relation=None):
200
171
        def _isEqual(self,other):
201
172
                if not super(ExcelCell,self)._isEqual(other):
202
173
                        return False
203
 
                thisAddr=self.getCellAddress(self.excelCellObject)
204
 
                otherAddr=self.getCellAddress(other.excelCellObject)
 
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
205
180
                return thisAddr==otherAddr
206
181
 
207
182
        def _get_name(self):
233
208
                if previous:
234
209
                        return ExcelCell(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelCellObject=previous)
235
210
 
236
 
        def script_editCell(self,keyPress):
237
 
                cellEditDialog=CellEditDialog(self.excelWindowObject.ActiveCell)
238
 
                cellEditDialog.run()
239
 
 
240
 
ExcelCell.bindKey("f2","editCell")
241
 
 
242
 
class ExcelSelection(ExcelWindow):
243
 
 
244
 
        role=controlTypes.ROLE_GROUPING
 
211
class ExcelSelection(ExcelBase):
 
212
 
 
213
        role=controlTypes.ROLE_TABLECELL
245
214
 
246
215
        def __init__(self,windowHandle=None,excelWindowObject=None,excelRangeObject=None):
247
216
                self.excelWindowObject=excelWindowObject
248
217
                self.excelRangeObject=excelRangeObject
249
218
                super(ExcelSelection,self).__init__(windowHandle=windowHandle)
250
219
 
 
220
        def _get_states(self):
 
221
                states=super(ExcelSelection,self).states
 
222
                states.add(controlTypes.STATE_SELECTED)
 
223
                return states
 
224
 
251
225
        def _get_name(self):
252
 
                return _("selection")
253
 
 
254
 
        def _get_value(self):
255
226
                firstCell=self.excelRangeObject.Item(1)
256
227
                lastCell=self.excelRangeObject.Item(self.excelRangeObject.Count)
257
228
                return _("%s %s through %s %s")%(self.getCellAddress(firstCell),firstCell.Text,self.getCellAddress(lastCell),lastCell.Text)
260
231
                worksheet=self.excelRangeObject.Worksheet
261
232
                return ExcelWorksheet(windowHandle=self.windowHandle,excelWindowObject=self.excelWindowObject,excelWorksheetObject=worksheet)
262
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)