Tk_CreateSelHandler(3tk)
β¨ Tk_CreateSelHandler β bestie, let's talk selections β¨
okay so real talk: this page is from the Tk Library Procedures crew, and it's the manual entry for Tk_CreateSelHandler(3tk) (and its ride-or-die partner Tk_DeleteSelHandler, who we'll get to). it's all about arranging for your app to handle requests for a selection. X11 clipboard drama, but make it cute. let's go π
πΈ NAME πΈ
Tk_CreateSelHandler, Tk_DeleteSelHandler β arrange to handle requests for a selection
β¨ SYNOPSIS β¨
#include <tk.h>
Tk_CreateSelHandler(tkwin, selection, target, proc, clientData, format)
Tk_DeleteSelHandler(tkwin, selection, target)
π ARGUMENTS π
Tk_Window tkwin(in) β the window thatprocis gonna be providing selection info for. it's giving "this is my window, this is my selection" πAtom selection(in) β the name of the selection thatprocwill be spilling the tea about.Atom target(in) β the form in whichproccan hand over the selection (likeSTRINGorFILE_NAME, for example). this is the one that corresponds to the type arguments in selection commands, iykyk.Tk_SelectionProc *proc(in) β the procedure to invoke whenever the selection is owned bytkwinand the selection contents get requested in the format given bytarget. the moment the vibe check hits,procgets called. π₯void *clientData(in) β an arbitrary one-word value to pass toproc. no questions asked, it just gets handed over. π«ΆAtom format(in) β if the selection requestor is not in this process, thenformatis the thing that determines the representation used to transmit the selection over to its requestor.
π¦ DESCRIPTION π¦
So. Tk_CreateSelHandler arranges for a particular procedure (proc) to be called whenever selection is owned by tkwin and the selection contents are requested in the form given by target. literally that's the whole job description, no cap.
Target should be one of the entries defined in the left column of Table 2 of the X Inter-Client Communication Conventions Manual (ICCCM), or honestly any other form in which an application is willing to present the selection. the most common form is STRING. periodt. π
Proc should have arguments and a result that match the type Tk_SelectionProc:
typedef Tcl_Size Tk_SelectionProc(
void *clientData,
Tcl_Size offset,
char *buffer,
Tcl_Size maxBytes);
The clientData parameter to proc is a copy of the clientData argument you gave to Tk_CreateSelHandler. typically, clientData points to a data structure containing application-specific information that's needed to retrieve the selection β like its little backpack of context. π
offsetspecifies an offset position into the selection.bufferspecifies a location at which to copy information about the selection.maxBytesspecifies the amount of space available atbuffer.
Proc should place a NULL-terminated string at buffer containing maxBytes or fewer characters (not including the terminating NULL), and it should return a count of the number of non-NULL characters stored at buffer. and if the selection no longer exists β like, it once existed but the user deleted the range of characters containing it β then proc should return -1. gone girl behaviour, but in a C function. π
π¦ when the selection is THICC π¦
When transferring large selections, Tk will break them up into smaller pieces (typically a few thousand bytes each) for more efficient transmission. it's not being extra, it's just optimizing. β¨
It does this by calling proc one or more times, using successively higher values of offset to retrieve successive portions of the selection. here's the tea on the return values:
- if
procreturns a count less thanmaxBytes, that means the entire remainder of the selection has been returned. we're done here. π - if
proc's return value ismaxBytes, that means there may be additional information in the selection, so Tk must make another call toprocto retrieve the next portion. cliffhanger energy. π
π€ formats, formats, formats π€
Proc always returns selection information in the form of a character string. BUT β and this is the plot twist β the ICCCM allows for information to be transmitted from the selection owner to the selection requestor in any of several formats, such as a string, an array of atoms, an array of integers, etc.
The format argument to Tk_CreateSelHandler indicates what format should be used to transmit the selection to its requestor (peep the middle column of Table 2 of the ICCCM for examples). so here's how it shakes out:
- if
formatis notSTRING, then Tk will take the value returned byprocand divided it into fields separated by white space. - if
formatisATOM, then Tk will return the selection as an array of atoms, with each field inproc's result treated as the name of one atom. - for any other value of
format, Tk will return the selection as an array of 32-bit values, where each field ofproc's result is treated as a number and translated to a 32-bit value.
in any event, the format atom is returned to the selection requestor along with the contents of the selection. both of them, together, always. π
π replacing and deleting π
If Tk_CreateSelHandler is called when there already exists a handler for selection and target on tkwin, then the existing handler is replaced with a new one. out with the old, in with the new, slay. π
Tk_DeleteSelHandler removes the handler given by tkwin, selection, and target, if such a handler exists. and if there is no such handler? it has no effect. it's unbothered, it's unshaken, it simply does nothing. π§
π KEYWORDS π
format, handler, selection, target
okay that's the whole page bestie, you're officially selection-handler literate π this was Tk, version 4.0, and yeah we are absolutely iconic for making it to the end together. mwah. π β¨
π¦ where this page comes from
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 1990-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms