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:
//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:
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.
Ejemplo 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.