Tk Source Code

View Ticket
Login
Ticket UUID: 1f1273f739c5e4744049bf9725292ac38e8f93e5
Title: Checkin #3ccd19e0a7 breaks expected operation of text window on linux
Type: Bug Version: 8.6.11
Submitter: sbron Created on: 2021-04-14 14:25:41
Subsystem: 18. [text] Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2021-04-17 19:52:13
Resolution: None Closed By: nobody
    Closed on:
Description:

I was wondering why I recently started having problems using one of my programs. Checking the 8.6.11 changelog I found that a change has been made to a disabled text window via checkin [3ccd19e0a7].

I believe this was a bad change. On linux text is copied by simply selecting it. There is no need to use Ctrl-C. And selecting text using the mouse was already possible. For people who want to use the keyboard, the text widget would need to have configuration option -takefocus 1. In that case the widget can be given focus via tab-traversal, and then selection via the keyboard (and even Ctrl-C) already worked before this change.

To make the text widget on linux behave like Windows to accomodate users who want to use some weird combination of the mouse and keyboard to copy text, and breaking the normal expected behavior in the process is just silly.

Furthermore, for people having this strange requirement, the new behavior could very easily be accomplished by adding a simple binding:

    bind .txt <1> {focus %W}
The reverse is quite a bit harder to accomplish.

The new behavior also differs from an entry widget (at the risk that a code change will now be submitted to also break that).

I would prefer the change to be completely undone. But as a compromise, the binding could possibly check the -takefocus option. If the window can't be reached using the keyboard only, then it makes no sense to expect a superfluous key-combination to "work" (for some interpretation of that term).

User Comments: sbron added on 2021-04-17 19:52:13:

I really don't appreciate the derogatory tone of your responses. If you are trying to stop me from filing bug reports, you've nearly succeeded.

I have no clue why you claim that my patch prevents a selection being made in a disabled text widget. Obviously I tested it and it works fine on linux. It allows copying to both clipboard buffers. I have no possibility to test on Mac.

Sure, redefining a standard virtual binding is not optimal. But if that's what it takes to make something work correctly, it shouldn't be dismissed just because of that. I also never claimed that this should be the final solution. That's why I called it a first attempt.


bll added on 2021-04-17 17:01:41:
> I am well aware that there are programs that exhibit abnormal clipboard 
> behavior. I put that down to Windows developers converting a program for 
> use on linux without knowing (or caring about?) the normal look and feel 
> on linux. So they make it work like they are used to on Windows and 
> implement the CLIPBOARD selection rather than the PRIMARY selection. 
> Unfortunately, the more of those programs there are, the more people will 
> believe that this is normal. But that's 
> no reason for Tk to jump on the bandwagon. 

You persist in ignoring reality and defining your own version of "normal". 
Linux has always supported both primary and clipboard buffers. 
Both exist, both work, both have uses, and Control-C/V have been on Unix for
a very long time.

Your "patch" prevents any selection from being made in a disabled text widget
on Linux and Mac OS.  And redefining a standard virtual binding
is a really bad idea.

sbron added on 2021-04-15 21:36:39:

How's this for a first attempt at a solution that will allow Ctrl-C to work on a disabled text widget without it taking focus?

# Undo #3ccd19e0a7
proc tk::TextButton1 {w x y} {

    variable ::tk::Priv

    set Priv(selectMode) char
    set Priv(mouseMoved) 0
    set Priv(pressX) $x
    set anchorname [tk::TextAnchor $w]
    $w mark set insert [TextClosestGap $w $x $y]
    $w mark set $anchorname insert
    # Set the anchor mark's gravity depending on the click position
    # relative to the gap
    set bbox [$w bbox [$w index $anchorname]]
    if {$x > [lindex $bbox 0]} {
        $w mark gravity $anchorname right
    } else {
        $w mark gravity $anchorname left
    }
    # Allow focus in any case on Windows, because that will let the
    # selection be displayed even for state disabled text widgets.
    if {[tk windowingsystem] eq "win32"  || [$w cget -state] eq "normal"} {
        focus $w
    }
    if {[$w cget -autoseparators]} {
        $w edit separator
    }
}

# Move Control-C to a new virtual event
event delete <<Copy>> <Control-Key-c> <Control-Lock-Key-C>
event add <<CopyEx>> <Control-Key-c> <Control-Lock-Key-C>

# Create the new binding
bind all <<CopyEx>> {tk::CopyEx {%W}}

proc tk::CopyEx {w} {
    # Find the window that has the selection
    set owner [selection own -displayof $w]
    if {$owner eq ""} return
    # Only perform any action when the widgets are in the same toplevel
    if {[winfo toplevel $w] eq [winfo toplevel $owner]} {
        set data [selection get -displayof $w]
        clipboard clear -displayof $w
        clipboard append -displayof $w $data
    }
}
This basically checks if any widget in the current toplevel has any text selected when Ctrl-C is pressed. If so, that text is placed on the clipboard.

I have no idea if this will work on Windows. If not, the binding would have to be made dependent on the platform.

Also, more extensive testing of edge cases still needs to be done.


sbron added on 2021-04-15 20:04:07:

I am well aware that there are programs that exhibit abnormal clipboard behavior. I put that down to Windows developers converting a program for use on linux without knowing (or caring about?) the normal look and feel on linux. So they make it work like they are used to on Windows and implement the CLIPBOARD selection rather than the PRIMARY selection. Unfortunately, the more of those programs there are, the more people will believe that this is normal. But that's no reason for Tk to jump on the bandwagon.

You say: "Placing a burden on the developer to force the text widget to act as it should is not reasonable." Yet the recent change places a burden on me, as a developer, to make the text widget behave as it should and not take focus when it is disabled.

Anyway, it seems we're largely in agreement. The behavior of the text widget is not correct. The only difference is that you feel the current behavior is an improvement, while in my opinion it has deteriorated.

But I'll go ahead and add a few more lines to the code I already need to make Tk behave properly on linux. The frustrating part is that things that used to work fine get broken by so-called improvements that require me to add more and more code to undo the adverse effects of those "improvements".


bll added on 2021-04-15 15:29:15:
> ... A browser is not a good representative for standard behavior of applications. At least firefox ...

Very true.  The browsers have their own UI.
Nevertheless, the copy/paste of "label" text does work as normal.
This can be tested in other applications.

> Ctrl-C and Ctrl-V are Windows key combinations. Expecting those to work out 
> of the box on linux is unreasonable. Furthermore, as I've mentioned before,
> they are completely unnecessary. Copying on linux is done by selecting the 
> text. Clicking the middle mouse button performs the paste action. There is no 
> need for any keyboard interaction.

This is false.

Please read: https://wiki.archlinux.org/index.php/clipboard
Apparently you do not have a full understanding of how copy/paste works on 
Linux.  And assumptions that all users work the keyboard and mouse the way you do.

My VirtualBox VMs generally know nothing about the primary buffer, and I must use the
clipboard buffer to copy data across.

> ...

What you are describing are design flaws.
The text widget's selection and copy/paste actions should work when it
is disabled.

Placing a burden on the developer to force the text widget to act as it should
is not reasonable.  Text should be selectable and the copy/paste keys should 
work.

Reverting the change would be inappropriate.  A fix to make the text selectable
and where copy/paste work while the text widget is disabled and without changing
the focus selection would be ideal.

The current fix replaces poor behavior with better behavior which is perhaps not quite 
perfect.

sbron added on 2021-04-15 09:23:37:

The unexpected behavior is that a disabled widget takes focus when clicked. This does not happen in other applications. A browser is not a good representative for standard behavior of applications. At least firefox is lately trampling all standard platform behavior and enforces its own rules across platforms. But even in firefox a disabled text field does not take the focus when clicked.

Also the entry widget doesn't behave like this. Consider the following script:

pack [text .txt]
pack [entry .e1] -fill x
pack [entry .e2] -fill x
.txt insert end "Some text"
.e1 insert end "Some text"
.txt configure -state disabled
.e1 configure -state disabled
focus .e2

Clicking the disabled entry widget doesn't change the focus. The normal entry keeps the focus. However clicking the disabled text widget does move the focus when clicked.

I have none of the expectations that you mention. Copy/pasting text from a disabled text widget using the mouse the linux way always worked fine before this change. Ctrl-C and Ctrl-V are Windows key combinations. Expecting those to work out of the box on linux is unreasonable. Furthermore, as I've mentioned before, they are completely unnecessary. Copying on linux is done by selecting the text. Clicking the middle mouse button performs the paste action. There is no need for any keyboard interaction. So there is also no need for the text widget to get the focus.

The fact that the label widget may have some shortcommings that you try to resolve by using a disabled text widget as a label doesn't justify introducing quircky behavior when using the text widget for its intended purpose.

Making the old text widget behave as a label with copy/paste possibilties, even including the Windows key combinations, only required a few lines of code. But now the burden to add code is placed on developers who want the text widget to behave like a text widget. I just don't think that's right.


bll added on 2021-04-14 15:41:11:
References: https://wiki.archlinux.org/index.php/clipboard
https://core.tcl-lang.org/tk/info/d91e05bf20

> To make the text widget on linux behave like Windows to accomodate users who 
> want to use some weird combination of the mouse and keyboard to copy text, and 
> breaking the normal expected behavior in the process is just silly.

Saying something is silly does not make it so.

You will also need to explain in explicit detail how the disabled text
widget is now breaking normal expected behavior.   Works as expected 
on Linux.  

You will also need to explain how the entry widget differs.
The entry widget works as expected.  Primary selection and the 
clipboard both work.  Ctrl-C and Ctrl-V work.

Having an expectation that (a) the programmer must explicitly allow a disabled
widget to take focus (which doesn't make much sense) and (b) somehow the user 
must know to tab their focus to a disabled widget (how can they even tell
it is something that they can tab to?  Not to mention very few users even
know how to tab from field to field.) in order to be able to copy/paste
a piece of text on-screen seems unreasonable.

Having expectations that the programmer must make Tk behave as it should
already be behaving on various systems is unreasonable.

Tk is already very unfriendly in the copy/paste arena as labels cannot be selected or copied.  I have to use a disabled text widget in order to make
the text selectable, and I discovered that even so, the text could not be
copied.  Labels should also be modified to work in the same manner.  This
has been requested many times over the years.

The disabled text widget in 8.6.11 now behaves like most all applications.
Compare to selecting text displayed on the browser screen.