X Window Selection - XDND

XDND

Drag-and-drop in the X Window System is regulated by the Xdnd convention. When the user drags the selected text into a window and releases the mouse button, the exchange of data is done as for the primary selection. Drag-and-drop is complicated by what happens during the drag. Namely, when the user drags the selection to different parts of the desktop or a window, the user expects to be able to tell whether text can be dropped or not. In particular, the target should display visual feedback on whether or not it will accept the drop, and the cursor should change to indicate the action that will be taken; e.g., copy or move.

In the Xdnd protocol, the window where the text is selected and the drag begins is called the source; the window over which the cursor hovers is called the target. The communication between the source and the target is driven by the source because the source "grabs" the cursor. An exchange between source and target is therefore necessary in order for the target to even know that drag-and-drop is happening. Since the source decides the shape of the cursor, the source must receive a response from the target in order to update the cursor. In addition, since the target may need to draw a bombsight to indicate where the drop will occur, and since acceptance of the drop may depend on the exact location of the cursor, this exchange must happen repeatedly as the cursor moves. In fact, even if the cursor does not move, messages must be exchanged to allow the target to scroll when the cursor is near an edge of the viewing area. Otherwise, the user will only be able to drop on the visible portion of the target.

A program can state that a window can be the target of a drop by creating a property named XdndAware which contains the highest version of the protocol that the program supports. This way, applications which support newer versions can fall back to older versions in order to interoperate correctly. In addition, all applications that are written without support for Xdnd will be ignored.

When the cursor enters the target window, the source checks the presence of the XdndAware property on that window. If this property is present, an exchange begins:

  • the source tells the target that the cursor has entered the target while dragging some data by sending an event XdndEnter
  • the target can find out which kind of data is dragged (text, image, etc.) by looking at this event and possibly by further interaction with the source

While the cursor is inside the target window:

  • the source sends XdndPosition events to tell the target where the cursor currently is
  • the target replies with XdndStatus events to tell the source whether the data can be dropped in the current position
  • the source sends a message XdndLeave or XdndDrop when the cursor has left the window or the button has been released, respectively

If the user drops, the target requests the selection from the source as usual. When the transfer of the selection is concluded, the target sends an XdndFinish event to tell the source that the transfer has been successful.

In summary, the protocol is driven by the source, which keeps the target informed of what is happening with the cursor. In reply, the target tells the source whether a drop would be accepted or not. The target must also be informed when the user releases the mouse button, as this event starts a regular request for a selection, which is a protocol driven by the target.

The above is the description of the Xdnd convention for drag-and-drop. Different conventions for drag-and-drop are used in Motif, OffiX, and Amulet.

Read more about this topic:  X Window Selection