1selection(n) Tk Built-In Commands selection(n)
2
3
4
5______________________________________________________________________________
6
8 selection - Manipulate the X selection
9
11 selection option ?arg arg ...?
12______________________________________________________________________________
13
15 This command provides a Tcl interface to the X selection mechanism and
16 implements the full selection functionality described in the X Inter-
17 Client Communication Conventions Manual (ICCCM).
18
19 Note that for management of the CLIPBOARD selection (see below), the
20 clipboard command may also be used.
21
22 The first argument to selection determines the format of the rest of
23 the arguments and the behavior of the command. The following forms are
24 currently supported:
25
26 selection clear ?-displayof window? ?-selection selection?
27 If selection exists anywhere on window's display, clear it so
28 that no window owns the selection anymore. Selection specifies
29 the X selection that should be cleared, and should be an atom
30 name such as PRIMARY or CLIPBOARD; see the Inter-Client Communi‐
31 cation Conventions Manual for complete details. Selection de‐
32 faults to PRIMARY and window defaults to “.”. Returns an empty
33 string.
34
35 selection get ?-displayof window? ?-selection selection? ?-type type?
36 Retrieves the value of selection from window's display and re‐
37 turns it as a result. Selection defaults to PRIMARY and window
38 defaults to “.”. Type specifies the form in which the selection
39 is to be returned (the desired “target” for conversion, in ICCCM
40 terminology), and should be an atom name such as STRING or
41 FILE_NAME; see the Inter-Client Communication Conventions Manual
42 for complete details. Type defaults to STRING. The selection
43 owner may choose to return the selection in any of several dif‐
44 ferent representation formats, such as STRING, UTF8_STRING,
45 ATOM, INTEGER, etc. (this format is different than the selection
46 type; see the ICCCM for all the confusing details). If the se‐
47 lection is returned in a non-string format, such as INTEGER or
48 ATOM, the selection command converts it to string format as a
49 collection of fields separated by spaces: atoms are converted to
50 their textual names, and anything else is converted to hexadeci‐
51 mal integers. Note that selection get does not retrieve the se‐
52 lection in the UTF8_STRING format unless told to.
53
54 selection handle ?-selection s? ?-type t? ?-format f? window command
55 Creates a handler for selection requests, such that command will
56 be executed whenever selection s is owned by window and someone
57 attempts to retrieve it in the form given by type t (e.g. t is
58 specified in the selection get command). S defaults to PRIMARY,
59 t defaults to STRING, and f defaults to STRING. If command is
60 an empty string then any existing handler for window, t, and s
61 is removed. Note that when the selection is handled as type
62 STRING it is also automatically handled as type UTF8_STRING as
63 well.
64
65 When selection is requested, window is the selection owner, and
66 type is the requested type, command will be executed as a Tcl
67 command with two additional numbers appended to it (with space
68 separators). The two additional numbers are offset and max‐
69 Chars: offset specifies a starting character position in the
70 selection and maxChars gives the maximum number of characters to
71 retrieve. The command should return a value consisting of at
72 most maxChars of the selection, starting at position offset.
73 For very large selections (larger than maxChars) the selection
74 will be retrieved using several invocations of command with in‐
75 creasing offset values. If command returns a string whose
76 length is less than maxChars, the return value is assumed to in‐
77 clude all of the remainder of the selection; if the length of
78 command's result is equal to maxChars then command will be in‐
79 voked again, until it eventually returns a result shorter than
80 maxChars. The value of maxChars will always be relatively large
81 (thousands of characters).
82
83 If command returns an error then the selection retrieval is re‐
84 jected just as if the selection did not exist at all.
85
86 The format argument specifies the representation that should be
87 used to transmit the selection to the requester (the second col‐
88 umn of Table 2 of the ICCCM), and defaults to STRING. If format
89 is STRING, the selection is transmitted as 8-bit ASCII charac‐
90 ters (i.e. just in the form returned by command, in the system
91 encoding; the UTF8_STRING format always uses UTF-8 as its encod‐
92 ing). If format is ATOM, then the return value from command is
93 divided into fields separated by white space; each field is
94 converted to its atom value, and the 32-bit atom value is trans‐
95 mitted instead of the atom name. For any other format, the re‐
96 turn value from command is divided into fields separated by
97 white space and each field is converted to a 32-bit integer; an
98 array of integers is transmitted to the selection requester.
99
100 The format argument is needed only for compatibility with selec‐
101 tion requesters that do not use Tk. If Tk is being used to re‐
102 trieve the selection then the value is converted back to a
103 string at the requesting end, so format is irrelevant.
104
105 selection own ?-displayof window? ?-selection selection?
106
107 selection own ?-command command? ?-selection selection? window
108 The first form of selection own returns the path name of the
109 window in this application that owns selection on the display
110 containing window, or an empty string if no window in this ap‐
111 plication owns the selection. Selection defaults to PRIMARY and
112 window defaults to “.”.
113
114 The second form of selection own causes window to become the new
115 owner of selection on window's display, returning an empty
116 string as result. The existing owner, if any, is notified that
117 it has lost the selection. If command is specified, it is a Tcl
118 script to execute when some other window claims ownership of the
119 selection away from window. Selection defaults to PRIMARY.
120
122 The text, entry, ttk::entry, listbox, spinbox and ttk::spinbox widgets
123 have the option -exportselection. If a widget has this option set to
124 boolean true, then (in an unsafe interpreter) a selection made in the
125 widget is automatically written to the PRIMARY selection.
126
127 A GUI event, for example <<PasteSelection>>, can copy the PRIMARY se‐
128 lection to certain widgets. This copy is implemented by a widget bind‐
129 ing to the event. The binding script makes appropriate calls to the
130 selection command.
131
133 On X11, the PRIMARY selection is a system-wide feature of the X server,
134 allowing communication between different processes that are X11
135 clients.
136
137 On Windows, the PRIMARY selection is not provided by the system, but
138 only by Tk, and so it is shared only between windows of a parent inter‐
139 preter and its child interpreters. It is not shared between inter‐
140 preters in different processes or different threads. Each parent in‐
141 terpreter has a separate PRIMARY selection that is shared only with its
142 child interpreters which are not safe interpreters.
143
145 A safe interpreter cannot read from the PRIMARY selection because its
146 selection command is hidden. For this reason the PRIMARY selection
147 cannot be written to the Tk widgets of a safe interpreter.
148
149 A Tk widget can have its option -exportselection set to boolean true,
150 but in a safe interpreter this option has no effect: writing from the
151 widget to the PRIMARY selection is disabled.
152
153 These are security features. A safe interpreter may run untrusted
154 code, and it is a security risk if this untrusted code can read or
155 write the PRIMARY selection used by other interpreters.
156
158 On X11 platforms, one of the standard selections available is the SEC‐
159 ONDARY selection. Hardly anything uses it, but here is how to read it
160 using Tk:
161
162 set selContents [selection get -selection SECONDARY]
163
164 Many different types of data may be available for a selection; the spe‐
165 cial type TARGETS allows you to get a list of available types:
166
167 foreach type [selection get -type TARGETS] {
168 puts "Selection PRIMARY supports type $type"
169 }
170
171 To claim the selection, you must first set up a handler to supply the
172 data for the selection. Then you have to claim the selection...
173 # Set up the data handler ready for incoming requests
174 set foo "This is a string with some data in it... blah blah"
175 selection handle -selection SECONDARY . getData
176 proc getData {offset maxChars} {
177 puts "Retrieving selection starting at $offset"
178 return [string range $::foo $offset [expr {$offset+$maxChars-1}]]
179 }
180
181 # Now we grab the selection itself
182 puts "Claiming selection"
183 selection own -command lost -selection SECONDARY .
184 proc lost {} {
185 puts "Lost selection"
186 }
187
189 clipboard(n)
190
192 clear, format, handler, ICCCM, own, selection, target, type
193
194
195
196Tk 8.1 selection(n)