GTK+ By Example/Tree View/DnD

Drag'n'Drop (DnD) **** needs revision ***
This section needs revision more than any other section. If you know anything about tree view drag'n'drop, you probably know more than the author of this text. Please give some feedback in that case.

If you want to dive into treeview drag'n'drop, you might want to check out Owen Taylor's mail on that topic. It might not be completely identical to what has actually been implemented, but it gives a great overview, and provides more information than the docs do.

In addition to the standard Gtk+ Drag and Drop mechanisms that work with any widget, there are special Drag and Drop mechanisms just for the tree view widget. You usually want to use the tree-view specific Drag-and-Drop framework.

Drag'n'Dropping Row-Unrelated Data to and from a Tree View from other Windows or Widgets
Drag'n'Dropping general information from or to a tree view widget works just like it works with any other widget and involves the standard Gtk+ Drag and Drop mechanisms. If you use this, you can receive drops to or initiate drags from anywhere in your tree view (including empty sections). This is not row- or column-specific and is most likely not want you want. Nevertheless, here is a small example of a tree view in which you can drag'n'drop URIs from other applications (browsers, for example), with the dropped URIs just being appended to the list (note that usually you would probably rather want to set up your whole window as a target then and not just the tree view widget):

If you are receiving drops into a tree view, you can connect to the view's "drag-motion" signal to track the mouse pointer while it is in a drag and drop operation over the tree view. This is useful for example if you want to expand a collapsed node in a tree when the mouse hovers above the node for a certain amount of time during a drag'n'drop operation. Here is an example of how to achieve this:

Connect to the view's "drag-drop" signal to be called when the drop happens. You can translate the coordinates provided into a tree path with gtk_tree_view_get_path_at_pos.

Dragging Rows Around Within a Tree
Both GtkListStore and GtkTreeStore implement the GtkTreeDragDest and GtkTreeDragSource interfaces, which means that they have in-built support for row reordering. You need to call gtk_tree_view_set_reorderable to activate this, and then connect to the tree model's signals to catch the reorderings that take place.

Example shown below

Compile:

Dragging Rows from One Tree to Another
This is actually much easier to do than setting up drag-and-drop functionality from arbitrary sources. Data is passed from one treeview to another by means of a GtkSelectionData object. Think of this like a post box. You put something in it when "drag-data-get" is fired and you take your stuff out when "drag-data-received" is emitted.

The example shown below was put together using many different sources but the fundamental design comes from a php-gtk2 cookbook article

Compile:

GTK support numerous data types through GtkSelectionData. But in this example we only send across the value of a pointer. A pointer to a row data structure containing all the data required to populate the destination treeview.