Dragging files to other applications

The good news: Unlike the drop problems, all tested programs (Konqueror, Kate, Firefox, Opera, Nautilus) seem to be able to handle correct URIs dragged to them in the right way. Even Firefox accepts text/uri-list data.

The first step is again to tell the widget that it has the ability to act as a source for drag data. Furthermore, we need to provide the data we want to drag, which can be done by connecting the widget to the "drag-data-get" signal.

We told the widget the following:

Next is to provide the data for the drop target in the dragDataGet function:

The data are passed by overwriting the selection data via the set() method of the selection_data. As we want to follow the standards, the file name has to be converted to a full-flagged url-encoded URI, with the terminating \r\n at the end of the line. When nothing is selected in the tree, we don't change the selection data.

When trying to drag a file to your favorite application, you will note that nothing happens. Why? The URI passed to the program looks like that: file%3A%2F%2Flocalhost%2Fdata%2Fphp-gtk. Yes, urlencode doesn't take care of the character's meaning in the URI. Now it would cost some effort to make a function which does a correct encoding while taking care of special characters, as http URIs can contain slashes in a parameter which would have to be encoded, but slashes in the filename not. For that reason, only a simple back-translation of some chars is done after urlencoding the string:

With that, we can change our dragDataGet function:
$selection_data->set($selection_data->target, 8, $this->pathurlencode($this->getUriFromFile($file)) . "\r\n");

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.