Note that for management of the CLIPBOARD selection (see below), the clipboard command may also be used.
The first argument to selection determines the format of the rest of the arguments and the behavior of the command. The following forms are currently supported:
When selection is requested, window is the selection owner, and type is the requested type, command will be executed as a Tcl command with two additional numbers appended to it (with space separators). The two additional numbers are offset and maxChars: offset specifies a starting character position in the selection and maxChars gives the maximum number of characters to retrieve. The command should return a value consisting of at most maxChars of the selection, starting at position offset. For very large selections (larger than maxChars) the selection will be retrieved using several invocations of command with increasing offset values. If command returns a string whose length is less than maxChars, the return value is assumed to include all of the remainder of the selection; if the length of command's result is equal to maxChars then command will be invoked again, until it eventually returns a result shorter than maxChars. The value of maxChars will always be relatively large (thousands of characters).
If command returns an error then the selection retrieval is rejected just as if the selection did not exist at all.
The format argument specifies the representation that should be used to transmit the selection to the requester (the second column of Table 2 of the ICCCM), and defaults to STRING. If format is STRING, the selection is transmitted as 8-bit ASCII characters (i.e. just in the form returned by command, in the system encoding; the UTF8_STRING format always uses UTF-8 as its encoding). If format is ATOM, then the return value from command is divided into fields separated by white space; each field is converted to its atom value, and the 32-bit atom value is transmitted instead of the atom name. For any other format, the return value from command is divided into fields separated by white space and each field is converted to a 32-bit integer; an array of integers is transmitted to the selection requester.
The format argument is needed only for compatibility with selection requesters that do not use Tk. If Tk is being used to retrieve the selection then the value is converted back to a string at the requesting end, so format is irrelevant.
The second form of selection own causes window to become the new owner of selection on window's display, returning an empty string as result. The existing owner, if any, is notified that it has lost the selection. If command is specified, it is a Tcl script to execute when some other window claims ownership of the selection away from window. Selection defaults to PRIMARY.
A GUI event, for example <<PasteSelection>>, can copy the PRIMARY selection to certain widgets. This copy is implemented by a widget binding to the event. The binding script makes appropriate calls to the selection command.
On Windows, the PRIMARY selection is not provided by the system, but only by Tk, and so it is shared only between windows of a parent interpreter and its child interpreters. It is not shared between interpreters in different processes or different threads. Each parent interpreter has a separate PRIMARY selection that is shared only with its child interpreters which are not safe interpreters.
A Tk widget can have its option -exportselection set to boolean true, but in a safe interpreter this option has no effect: writing from the widget to the PRIMARY selection is disabled.
These are security features. A safe interpreter may run untrusted code, and it is a security risk if this untrusted code can read or write the PRIMARY selection used by other interpreters.
set selContents [selection get -selection SECONDARY]
Many different types of data may be available for a selection; the special type TARGETS allows you to get a list of available types:
foreach type [selection get -type TARGETS] { puts "Selection PRIMARY supports type $type" }
To claim the selection, you must first set up a handler to supply the data for the selection. Then you have to claim the selection...
# Set up the data handler ready for incoming requests set foo "This is a string with some data in it... blah blah" selection handle -selection SECONDARY . getData proc getData {offset maxChars} { puts "Retrieving selection starting at $offset" return [string range $::foo $offset [expr {$offset+$maxChars-1}]] } # Now we grab the selection itself puts "Claiming selection" selection own -command lost -selection SECONDARY . proc lost {} { puts "Lost selection" }