The simpleDND Package

The simpleDND package implements a lightweight mechanism for drag-and-drop operations within a single Tcl/Tk application. When used, it provides a visual icon and cursor change to indicate a dragged object, and can execute any Tcl command upon a successful drop operation. The package is designed to be easy-to-use, with just a few commands required to add drag-and-drop. Any widget can be registered as a drag source or drop target.

simpleDND is called via package require simpleDND. It is implemented with the following command:

simplednd::dragRegister must be called for each widget that is set up as a drag source. For instance, if you have a listbox .l and a listbox .b that are both drag sources, the command must be called for each widget. Also, if listbox .l and listbox .b have multiple drop targets, then the command must be called for each drop target.

The dragcmd should be used to configure the drag icon. The variables ::simplednd::dragtext and ::simplednd::dragimg are used for this purpose. At a minimum, you must define a single command setting default values for the dragtext and dragimg variables, or you can provide a different command for each drag operation. simpleDND does not provide default values for the text or image in the drag icon; without at least one dragcmd, you will be dragging around an empty label as the drag icon.

The dropcmd is called on a successful drop, i.e. when the button is released over the drop target registered with the drag source. If the button is released over a widget that is not registered as the drop target, the cursor changes to a standard arrow and the drag icon disappears.

A demonstration of simpleDND is included with the package. Call simplednd::demo after loading the package. You can also inspect the demo source code for ideas on how to implement simpleDND in your own applications.