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
14
16 This command provides a Tcl interface to the X selection mechanism and
17 implements the full selection functionality described in the X Inter-
18 Client Communication Conventions Manual (ICCCM).
19
20 Note that for management of the CLIPBOARD selection (see below), the
21 clipboard command may also be used.
22
23 The first argument to selection determines the format of the rest of
24 the arguments and the behavior of the command. The following forms are
25 currently supported:
26
27 selection clear ?-displayof window? ?-selection selection?
28 If selection exists anywhere on window's display, clear it so
29 that no window owns the selection anymore. Selection specifies
30 the X selection that should be cleared, and should be an atom
31 name such as PRIMARY or CLIPBOARD; see the Inter-Client Communi‐
32 cation Conventions Manual for complete details. Selection
33 defaults to PRIMARY and window defaults to “.”. Returns an
34 empty string.
35
36 selection get ?-displayof window? ?-selection selection? ?-type type?
37 Retrieves the value of selection from window's display and
38 returns it as a result. Selection defaults to PRIMARY and win‐
39 dow defaults to “.”. Type specifies the form in which the
40 selection is to be returned (the desired “target” for conver‐
41 sion, in ICCCM terminology), and should be an atom name such as
42 STRING or FILE_NAME; see the Inter-Client Communication Conven‐
43 tions Manual for complete details. Type defaults to STRING.
44 The selection owner may choose to return the selection in any of
45 several different representation formats, such as STRING,
46 UTF8_STRING, ATOM, INTEGER, etc. (this format is different than
47 the selection type; see the ICCCM for all the confusing
48 details). If the selection is returned in a non-string format,
49 such as INTEGER or ATOM, the selection command converts it to
50 string format as a collection of fields separated by spaces:
51 atoms are converted to their textual names, and anything else is
52 converted to hexadecimal integers. Note that selection get does
53 not retrieve the selection in the UTF8_STRING format unless told
54 to.
55
56 selection handle ?-selection s? ?-type t? ?-format f? window command
57 Creates a handler for selection requests, such that command will
58 be executed whenever selection s is owned by window and someone
59 attempts to retrieve it in the form given by type t (e.g. t is
60 specified in the selection get command). S defaults to PRIMARY,
61 t defaults to STRING, and f defaults to STRING. If command is
62 an empty string then any existing handler for window, t, and s
63 is removed. Note that when the selection is handled as type
64 STRING it is also automatically handled as type UTF8_STRING as
65 well.
66
67 When selection is requested, window is the selection owner, and
68 type is the requested type, command will be executed as a Tcl
69 command with two additional numbers appended to it (with space
70 separators). The two additional numbers are offset and max‐
71 Chars: offset specifies a starting character position in the
72 selection and maxChars gives the maximum number of characters to
73 retrieve. The command should return a value consisting of at
74 most maxChars of the selection, starting at position offset.
75 For very large selections (larger than maxChars) the selection
76 will be retrieved using several invocations of command with
77 increasing offset values. If command returns a string whose
78 length is less than maxChars, the return value is assumed to
79 include all of the remainder of the selection; if the length of
80 command's result is equal to maxChars then command will be
81 invoked again, until it eventually returns a result shorter than
82 maxChars. The value of maxChars will always be relatively large
83 (thousands of characters).
84
85 If command returns an error then the selection retrieval is
86 rejected just as if the selection did not exist at all.
87
88 The format argument specifies the representation that should be
89 used to transmit the selection to the requester (the second col‐
90 umn of Table 2 of the ICCCM), and defaults to STRING. If format
91 is STRING, the selection is transmitted as 8-bit ASCII charac‐
92 ters (i.e. just in the form returned by command, in the system
93 encoding; the UTF8_STRING format always uses UTF-8 as its encod‐
94 ing). If format is ATOM, then the return value from command is
95 divided into fields separated by white space; each field is
96 converted to its atom value, and the 32-bit atom value is trans‐
97 mitted instead of the atom name. For any other format, the
98 return value from command is divided into fields separated by
99 white space and each field is converted to a 32-bit integer; an
100 array of integers is transmitted to the selection requester.
101
102 The format argument is needed only for compatibility with selec‐
103 tion requesters that do not use Tk. If Tk is being used to
104 retrieve the selection then the value is converted back to a
105 string at the requesting end, so format is irrelevant.
106
107 selection own ?-displayof window? ?-selection selection?
108
109 selection own ?-command command? ?-selection selection? window
110 The first form of selection own returns the path name of the
111 window in this application that owns selection on the display
112 containing window, or an empty string if no window in this
113 application owns the selection. Selection defaults to PRIMARY
114 and window defaults to “.”.
115
116 The second form of selection own causes window to become the new owner
117 of selection on window's display, returning an empty string as result.
118 The existing owner, if any, is notified that it has lost the selection.
119 If command is specified, it is a Tcl script to execute when some other
120 window claims ownership of the selection away from window. Selection
121 defaults to PRIMARY.
122
124 On X11 platforms, one of the standard selections available is the SEC‐
125 ONDARY selection. Hardly anything uses it, but here is how to read it
126 using Tk:
127 set selContents [selection get -selection SECONDARY]
128
129 Many different types of data may be available for a selection; the spe‐
130 cial type TARGETS allows you to get a list of available types:
131 foreach type [selection get -type TARGETS] {
132 puts "Selection PRIMARY supports type $type"
133 }
134
135 To claim the selection, you must first set up a handler to supply the
136 data for the selection. Then you have to claim the selection...
137 # Set up the data handler ready for incoming requests
138 set foo "This is a string with some data in it... blah blah"
139 selection handle -selection SECONDARY . getData
140 proc getData {offset maxChars} {
141 puts "Retrieving selection starting at $offset"
142 return [string range $::foo $offset [expr {$offset+$maxChars}]]
143 }
144
145 # Now we grab the selection itself
146 puts "Claiming selection"
147 selection own -command lost -selection SECONDARY .
148 proc lost {} {
149 puts "Lost selection"
150 }
151
153 clipboard(n)
154
156 clear, format, handler, ICCCM, own, selection, target, type
157
158
159
160Tk 8.1 selection(n)