selection(3tk)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ selection(3tk) β€” the Tk built-in that lets you touch the X selection ✨

okay bestie so this is selection(3tk), one of the Tk Built-In Commands, and it's literally the whole vibe of "let me grab what somebody highlighted" πŸ’…


🌸 NAME 🌸

selection β€” manipulate the X selection. that's it, that's the job, and she does it flawlessly πŸ‘‘


πŸŽ€ SYNOPSIS πŸŽ€

selection option ?arg ...?

πŸ’– DESCRIPTION πŸ’–

so the tea is: this command gives you a Tcl interface to the X selection mechanism, and it implements the full selection functionality described in the X Inter-Client Communication Conventions Manual (ICCCM). iconic behavior honestly 🫢

heads up bestie: for management of the CLIPBOARD selection (more on her below), you can also use the clipboard command. two queens, same job, no drama πŸ’…

the first argument you pass to selection decides the format of everything after it and how the command behaves. these are the forms currently supported, and yes we're going through all of them, no skipping πŸ“

πŸ¦‹ selection clear ?-displayof window? ?-selection selection?

if the selection exists anywhere on window's display, this clears it so that no window owns the selection anymore. selection says which X selection should be cleared, and it should be an atom name like PRIMARY or CLIPBOARD β€” peep the Inter-Client Communication Conventions Manual for the complete details, she's thorough. selection defaults to PRIMARY and window defaults to ".". returns an empty string. clean slate, periodt ✨

πŸ¦‹ selection get ?-displayof window? ?-selection selection? ?-type type?

grabs the value of selection from window's display and returns it as the result. selection defaults to PRIMARY and window defaults to ".". type says what form you want the selection back in (that's the desired "target" for conversion, in ICCCM speak), and it should be an atom name like STRING or FILE_NAME β€” again, the ICCCM has all the lore. type defaults to STRING.

now listen, this next bit is lowkey the plot twist: the selection owner can choose to return the selection in any of several different representation formats, like STRING, UTF8_STRING, ATOM, INTEGER, etc. (and yes, that format is different from the selection type β€” go read the ICCCM for all the confusing details, I'm not making this up 😭). if the selection comes back in a non-string format, like INTEGER or ATOM, the selection command converts it to string format as a collection of fields separated by spaces: atoms get converted to their textual names, and anything else gets converted to hexadecimal integers. also note that selection get does not retrieve the selection in the UTF8_STRING format unless you literally tell it to. no cap πŸ’€

πŸ¦‹ selection handle ?-selection s? ?-type t? ?-format f? window command

this one sets up a handler for selection requests, so that command gets executed whenever selection s is owned by window and somebody tries to retrieve it in the form given by type t (like, t is what you'd specify in the selection get command). s defaults to PRIMARY, t defaults to STRING, and f defaults to STRING. if command is an empty string, then any existing handler for window, t, and s gets removed. and note: when the selection is handled as type STRING, it's also automatically handled as type UTF8_STRING as well. two for one, slay πŸ’…

when the selection is requested, window is the selection owner, and type is the requested type, command gets executed as a Tcl command with two additional numbers appended to it (separated by spaces). those two numbers are offset and maxChars: offset gives a starting character position in the selection, and maxChars gives the maximum number of characters to retrieve. your command should return a value consisting of at most maxChars of the selection, starting at position offset.

for very large selections (bigger than maxChars), the selection gets 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. but if the length of command's result is equal to maxChars, then command gets invoked again, and again, until it eventually returns a result shorter than maxChars. the value of maxChars will always be relatively large (thousands of characters), so don't stress 🫢

if command returns an error, then the selection retrieval is rejected, just as if the selection did not exist at all. rip 😭

the format argument specifies the representation that should be used to transmit the selection to the requester (that's the second column of Table 2 of the ICCCM), and it defaults to STRING. here's the breakdown:

  • 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 don't use Tk. if Tk is the one retrieving the selection, then the value gets converted back to a string at the requesting end, so format is irrelevant. iykyk 🌈

πŸ¦‹ selection own ?-displayof window? ?-selection selection?

πŸ¦‹ selection own ?-command command? ?-selection selection? window

two forms, two moods πŸ’œ

the first form of selection own returns the path name of the window in this application that owns selection on the display containing window, or an empty string if no window in this application owns the selection. selection defaults to PRIMARY and window defaults to ".".

the second form of selection own causes window to become the new owner of selection on window's display, and returns an empty string as the result. the existing owner, if there is one, is notified that it has lost the selection (aw, she's fine πŸ’€). if command is specified, it's a Tcl script to execute when some other window claims ownership of the selection away from window. selection defaults to PRIMARY.


🌈 WIDGET FACILITIES 🌈

the text, entry, ttk::entry, listbox, spinbox and ttk::spinbox widgets all have the option -exportselection. if a widget has this option set to boolean true, then (in an unsafe interpreter) a selection made in the widget is automatically written to the PRIMARY selection. automatic. no manual labor. we love efficiency ✨

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. teamwork makes the dream work 🫢


🧚 PORTABILITY ISSUES 🧚

On X11, the PRIMARY selection is a system-wide feature of the X server, which lets different processes that are X11 clients communicate with each other. very social of her πŸ’…

On Windows, the PRIMARY selection is not provided by the system β€” 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. exclusive, lowkey πŸŽ€


πŸ‘‘ SECURITY πŸ‘‘

a safe interpreter cannot read from the PRIMARY selection because its selection command is hidden. for this reason, the PRIMARY selection cannot be written to the Tk widgets of a safe interpreter. blocked, bestie πŸ™ˆ

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. do not mess with this one, fr fr πŸ”₯

πŸ’… EXAMPLES πŸ’…

on X11 platforms, one of the standard selections available is the SECONDARY selection. hardly anything uses it, but here's how to read it using Tk:

set selContents [selection get -selection SECONDARY]

many different types of data may be available for a selection; the special type TARGETS lets you 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"
}

main character energy, truly πŸ‘‘


🌟 SEE ALSO 🌟


⭐ KEYWORDS ⭐

clear, format, handler, ICCCM, own, selection, target, type


and that's the whole page bestie 🫢 this was Tk 8.1, from the selection(3tk) page, and we are so done here. go forth and own that PRIMARY selection, you absolute icon πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…

πŸ“¦ where this page comes from