Chapter 20. Managing Selections

Table of Contents
20.1. Overview
20.2. Retrieving the selection
20.3. Supplying the selection

20.1. Overview

One type of interprocess communication supported by X and GTK is selections. A selection identifies a chunk of data, for instance, a portion of text, selected by the user in some fashion, for instance, by dragging with the mouse. Only one application on a display (the owner) can own a particular selection at one time, so when a selection is claimed by one application, the previous owner must indicate to the user that selection has been relinquished. Other applications can request the contents of a selection in different forms, called targets. There can be any number of selections, but most X applications only handle one, the primary selection.

In most cases, it isn't necessary for a PyGTK application to deal with selections itself. The standard widgets, such as the Entry widget, already have the capability to claim the selection when appropriate (e.g., when the user drags over text), and to retrieve the contents of the selection owned by another widget or another application (e.g., when the user clicks the second mouse button). However, there may be cases in which you want to give other widgets the ability to supply the selection, or you wish to retrieve targets not supported by default.

A fundamental concept needed to understand selection handling is that of the atom. An atom is an integer that uniquely identifies a string (on a certain display). Certain atoms are predefined by the X server, GTK and PyGTK (in GDK.py):
 
SELECTION_TYPE_ATOM
SELECTION_TYPE_BITMAP
SELECTION_TYPE_COLORMAP
SELECTION_TYPE_DRAWABLE
SELECTION_TYPE_INTEGER
SELECTION_TYPE_PIXMAP
SELECTION_TYPE_WINDOW
SELECTION_TYPE_STRING

You should use the functions atom_intern(), to get the atom corresponding to a string, and atom_name(), to get the name of an atom. Both selections and targets are identified by atoms.