Tk's copy and paste support

Purpose: discuss unique features and special concerns relating to copy and paste functionality within Tk.

Q1. How do I get copy and paste within a Tk application?

A1. Copy and paste should, in general, work out of the box.

Q2. What about copy and paste between major versions like 7.6 or 8.0 and

        8.1 and newer?  Does copy and paste work between non-[Unicode] Tk and 
        Unicode Tk?

A2. Yes. It uses UTF8_STRING on unix and the standard APIs on Windows.

Q3. Anything special that needs to be done about copy and paste between Tk 8.1 and newer and non-Tk applications?

A3. No.

Q4. ... between Java applications and Tk 8.1 and newer applications?

A4. Same as above.

Q5. Does this work on Windows, MacOS, etc?

A5. OS X has some unicode display issues as of 8.4.9, but everywhere else is OK.

Q6. On X, there are several kinds of selection - clipboard, cutbuffers, etc. What does Tk's copy and paste use, and are there ways to access the other buffers?

A6. Using the clipboard and selection commands, it uses the PRIMARY selection, but you can access others via selection.

Q7. Has anyone done any interesting development relating to Tk's copy and paste functionality?

A7. These have potential interest

Q8. Are other people annoyed that they can't select (copy) text from a label widget? I often misuse an entry widget (disabled) just to get copy capability. (EKB - no, I don't expect to be able to copy from a label. If I use a disabled entry widget to allow a copy, I make sure it's sunken or otherwise distinguished from a label.) MG There are some comments/pieces of code on the label page about how to achieve this. Setok: I believe one should be able to copy all text from the screen, whatever the widget. Yes, it is frustrating that labels act like that.

A8 wdb I happen to have a solution for copying label entries to clipboard:

 % bind Label <3>
 {::Lambda::Function::62 %W %X %Y}
 % sourceCode ::Lambda::Function::62
 proc ::Lambda::Function::62 {w x y} {
     focus -force $w
     tk_popup .contextMenuLabelCopy $x $y
 }
 % .contextMenuLabelCopy entrycget 0 -command
  clipboard clear
  clipboard append [[focus] cget -text]
 % .contextMenuLabelCopy entrycget 0 -label
 Kopieren

From now on, all labels can copy. Instead of cryptic ::Lambda::Function::62, choose a name which tells more about its function. Simple.

Q9. When I copy from a Tcl/Tk app on OS X, then quit that app, the text I copied doesn't appear in the system-wide paste buffer. I need to switch focus to something else while the Tk app is still running for cross-app copy/paste to work as expected. I'm using Tcl/Tk 8.10 on Leopard, though I had the same problem with 8.7 on Tiger. Any way around this?

A9. SLB: I cannot answer specifically for OS X but a similar problem occurs on Windows. I did investigate that and found that Windows provides two methods for storing information in the clipboard: you can copy the textinto the clipboard or you can store in the clipboard a reference to the application holding the text. The latter is more efficient when the amount of text is very large but means the information is lost when the application terminates. This is the only form of copy offered by Tk. Resolving it would require a TIP to extend clipboard support in Tk.

Q10. A developer demonstrated a peculiar problem with Tk 8.5 (and 8.6) copy and paste. The Tk application runs on a Unix X system, displaying back to a Windows desktop. The copy and paste works fine between X applications. The copy and paste works relatively fine between other X applications and Windows native applications (such as Word, Internet Explorer, Wordpad, etc.) However, the copy and paste works extremely inconsistently between Tk applications and the native Windows applications. Drag highlighting within a Tk entry box and then copying, followed by pasting into MS Word results in only one character of the text being pasted. Double clicking on one word of a multi-word string in an entry box, copying, then pasting into Word works as it should - however, returning the mouse to the same entry box, double clicking another word, copying, and attempting to paste into Word results in the first, not the second, word being pasted.


[fill in more questions as needed]