Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 35.  Selections and the Clipboard

The selection Command

There are two Tcl commands that deal with selections. The selection command is a general-purpose command that can set and get different selections. By default it manipulates the PRIMARY selection. The clipboard command stores data for later retrieval using the CLIPBOARD selection.

The selection command exposes the fully general selection model of different selections, types, and formats. You can define selection handlers that return selection values, and you can assert ownership of a selection and find out when you lose ownership to another application. Example 35-5 on page 512 shows a selection handler for a canvas.

A selection can have a type. The default is STRING. The type is different than the name of the selection (e.g., PRIMARY or CLIPBOARD). Each type can have a format, and the default format is also STRING. Ordinarily these defaults are fine. If you are dealing with non-Tk applications, however, you may need to ask for their selections by the right type (e.g., FILE_NAME). Formats include STRING, ATOM, and INTEGER. An ATOM is a name that is registered with the X server and identified by number. It is probably not a good idea to use non-STRING types and formats because it limits what other applications can use the information. The details about X selection types and formats are specified in the Inter-Client Communication Conventions Manual (David Rosenthal, Stuart Marks, X Consortium Standard). This is distributed with the X11 sources and can be found on the web at http://tronche.lri.fr:8000/gui/x/icccm/.

All of the selection operations take a -selection option that specifies the name of the selection being manipulated. This defaults to PRIMARY. Some of the operations take a -displayof option that specifies what display the selection is on. The value for this option is a Tk pathname of a window, and the selection on that window's display is manipulated. This is useful in X where applications can have their windows on remote displays. The default is to manipulate the selection on the display of the main window. Table 35-1 summarizes the selection command:

Table 35-1. The selection command.
selection clear ?-displayof win? ?-selection sel?Clears the specified selection.
selection get ?displayof win? ?-selection sel? ?-type type?Returns the specified selection. The type defaults to STRING.
selection handle ?-selection sel? ?-type type? ?-format format? window commandDefines command to be the handler for selection requests when window owns the selection.
selection own ?-displayof window? ?-selection sel?Returns the Tk pathname of the window that owns the selection, if it is in this application.
selection own ?-command command? ?-selection sel? windowAsserts that window owns the sel selection. The command is called when ownership of the selection is taken away from window.

      Previous section   Next section