Tk Source Code

View Ticket
Login
Ticket UUID: a9cf210a4237ba1a4194129650c640f1733ab519
Title: Text select doesn't cover first character
Type: Bug Version: 8.6
Submitter: Moosems Created on: 2023-04-19 23:26:10
Subsystem: 18. [text] Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2023-04-30 17:51:24
Resolution: Fixed Closed By: fvogel
    Closed on: 2023-04-30 17:51:24
Description:

When moving cursor above the window and selecting, the first char is not selected. This is shown here.

set text [text .text -width 40 -height 10]
pack $text

for {set i 0} {$i < 100} {incr i} {
    $text insert end "This is line $i of text\n"
}

Tcl version: 8.6.13

User Comments: fvogel added on 2023-04-30 17:51:24:
Also merged in revised_text.

fvogel added on 2023-04-30 17:44:39:
Merged in core-8-6-branch and trunk.

fvogel added on 2023-04-23 08:35:07:

I have fixed the failure, see [30e6a5a7].


chrstphrchvz added on 2023-04-23 06:29:22:

I also observe the change causing text-27.15b to fail on my machine (macOS 12.6.5 Intel) but I have not investigated why.


fvogel added on 2023-04-22 17:19:49:
Hmmm, the fix seems (see Github Actions) to trigger failure of text-27.15b, only on macOs with aqua. Will investigate.

fvogel added on 2023-04-21 19:09:29:

Fixed in [898c06572b]. I think that's fine.


fvogel added on 2023-04-21 07:47:43:
Yes, that's exactly where I designed the same fix. Basically, return $pos (instead of $pos + 1) whenever the mouse cursor $x,$y is outside the text area of the widget. I have to think a bit more about whether there could be other cases, and also what's the impact on each caller of tk::TextClosestGap (did you already research this perhaps?)

chrstphrchvz added on 2023-04-21 02:57:39:

Does this seem like the correct fix?

--- library/text.tcl.orig
+++ library/text.tcl
@@ -539,7 +539,8 @@ proc ::tk::TextClosestGap {w x y} {
     if {$bbox eq ""} {
        return $pos
     }
-    if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
+    if {$y < [lindex [$w dlineinfo $pos] 1] ||
+       ($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
        return $pos
     }
     $w index "$pos + 1 char"

Checking $y < [lindex $bbox 1] would not be appropriate because if a line contains characters with different font sizes, the top of the bounding box for some characters can be below the top pixel of a line.


oehhar added on 2023-04-20 11:50:23:

Can also reproduce on Windows and Tk 8.6.13.


chrstphrchvz added on 2023-04-20 06:33:56:

I can reproduce this on both Aqua and X11 (I have not tried Windows); it is present for both 8.5.9 and core-8-6-branch. When the mouse cursor/pointer is above the text widget, the first character of the first line is only selected when the cursor is also to the left of it.