44#See the file COPYING for more details.
55#Copyright (C) 2006-2017 NV Access Limited, Babbage B.V.
66
7+ import ctypes
78from ctypes import *
89from ctypes .wintypes import RECT
910from comtypes import BSTR
@@ -169,14 +170,22 @@ def initialize():
169170 _requestTextChangeNotificationsForWindow = NVDAHelper .localLib .displayModel_requestTextChangeNotificationsForWindow
170171
171172def getCaretRect (obj ):
172- left = c_long ()
173- top = c_long ()
174- right = c_long ()
175- bottom = c_long ()
176- res = watchdog .cancellableExecute (NVDAHelper .localLib .displayModel_getCaretRect , obj .appModule .helperLocalBindingHandle , obj .windowThreadID , byref (left ),byref (top ),byref (right ),byref (bottom ))
177- if res != 0 :
178- raise RuntimeError ("displayModel_getCaretRect failed with res %d" % res )
179- return RECT (left ,top ,right ,bottom )
173+ left = ctypes .c_long ()
174+ top = ctypes .c_long ()
175+ right = ctypes .c_long ()
176+ bottom = ctypes .c_long ()
177+ res = watchdog .cancellableExecute (
178+ NVDAHelper .localLib .displayModel_getCaretRect ,
179+ obj .appModule .helperLocalBindingHandle ,
180+ obj .windowThreadID ,
181+ ctypes .byref (left ),
182+ ctypes .byref (top ),
183+ ctypes .byref (right ),
184+ ctypes .byref (bottom )
185+ )
186+ if res != 0 :
187+ raise RuntimeError (f"displayModel_getCaretRect failed with res { res } " )
188+ return RectLTRB (left , top , right , bottom )
180189
181190def getWindowTextInRect (bindingHandle , windowHandle , left , top , right , bottom ,minHorizontalWhitespace ,minVerticalWhitespace ,stripOuterWhitespace = True ,includeDescendantWindows = True ):
182191 text , cpBuf = watchdog .cancellableExecute (_getWindowTextInRect , bindingHandle , windowHandle , includeDescendantWindows , left , top , right , bottom ,minHorizontalWhitespace ,minVerticalWhitespace ,stripOuterWhitespace )
@@ -555,7 +564,12 @@ class EditableTextDisplayModelTextInfo(DisplayModelTextInfo):
555564 minVerticalWhitespace = 4
556565 stripOuterWhitespace = False
557566
558- def _findCaretOffsetFromLocation (self ,caretRect ,validateBaseline = True ,validateDirection = True ):
567+ def _findCaretOffsetFromLocation (
568+ selff ,
569+ caretRect : RectLTRB ,
570+ validateBaseline : bool = True ,
571+ validateDirection : bool = True
572+ ):
559573 # Accepts logical coordinates.
560574 for charOffset , ((charLeft , charTop , charRight , charBottom ),charBaseline ,charDirection ) in enumerate (self ._getStoryOffsetLocations ()):
561575 # Skip any character that does not overlap the caret vertically
@@ -577,17 +591,12 @@ def _findCaretOffsetFromLocation(self,caretRect,validateBaseline=True,validateDi
577591 raise LookupError
578592
579593 def _getCaretOffset (self ):
580- caretRect = getCaretRect (self .obj )
581- objLocation = self .obj .location
582- objRect = RECT (objLocation [0 ],objLocation [1 ],objLocation [0 ]+ objLocation [2 ],objLocation [1 ]+ objLocation [3 ])
583- objRect .left ,objRect .top = windowUtils .physicalToLogicalPoint (
584- self .obj .windowHandle ,objRect .left ,objRect .top )
585- objRect .right ,objRect .bottom = windowUtils .physicalToLogicalPoint (
586- self .obj .windowHandle ,objRect .right ,objRect .bottom )
587- caretRect .left = max (objRect .left ,caretRect .left )
588- caretRect .top = max (objRect .top ,caretRect .top )
589- caretRect .right = min (objRect .right ,caretRect .right )
590- caretRect .bottom = min (objRect .bottom ,caretRect .bottom )
594+ caretRect = getCaretRect (self .obj )
595+ objLocation = self .obj .location
596+ objRect = objLocation .toLTRB ().toLogical (self .obj .windowHandle )
597+ caretRect = caretRect .intersection (objRect )
598+ if not any (caretRect ):
599+ raise RuntimeError ("The caret rectangle does not overlap with the window" )
591600 # Find a character offset where the caret overlaps vertically, overlaps horizontally, overlaps the baseline and is totally within or on the correct side for the reading order
592601 try :
593602 return self ._findCaretOffsetFromLocation (caretRect ,validateBaseline = True ,validateDirection = True )
0 commit comments