After you have decided which type of data you want your application
or widget to accept, you tell everyone about this decision by calling
the drag_dest_set() function on
the target widget. Since we want to accept files, we need the
text/uri-list target type:
Example 4.2. Accepting files
//at the end of buildDialog()
$this->targets = array( array( 'text/uri-list', 0, 0));
$this->tree->drag_dest_set( GTK_DEST_DEFAULT_ALL, $this->targets, GDK_ACTION_COPY | GDK_ACTION_MOVE);
Let's analyze the code:
We want to accept files, and files indicated by the
text/uri-list target type.
Drags shall be accepted from everywhere, so we give the second
parameter a 0 and the third the same, as we
don't have to distinguish between different target types.
The tree shall accept both copy and
move requests. The move is very important,
as all KDE applications (including Konqueror) seem to give their
files to targets only which accept the move
action.
The Gtk knows now that we accept files, but we didn't say what to
do with them. The plan is to add all the dropped files to the tree,
as children of the currently selected item.
Example 4.3. Handling the files
//at the end of buildDialog()
$this->tree->connect( 'drag-data-received', array( &$this, 'dragDataReceived'));
// a new function
function dragDataReceived($tree, $context , $x, $y, $data , $info, $time) {
if (count($tree->selection) != 1) {
$parent = $this->ndToplevel;
} else {
$parent = $tree->selection[0];
}
$strData = $data->data;
$arData = explode("\n", $strData);
foreach ($arData as $strLine) {
$strFile = trim($strLine);
if ($strFile == '') { continue; }
$tree->insert_node($parent, null, array(basename(urldecode($strFile)), $strFile, ''), 0, null, null, null, null, false, true);
}
$tree->columns_autosize();
}
If you drop some files from your favorite file manager, you will see
that they are added to the list.