PEAR Manual
Prev |  | Next

# Example

Example -- How to use Gtk2_FileDrop

## Using Gtk2_FileDrop

Example 40-1. General setup
    <?php
    $accepted_types = array(
        'text/html',
        'text/plain',
        '.dat'
    );
    Gtk_FileDrop::attach($widget, $accepted_types);
    ?>
Here we set up the array of accepted file types: The widget shall accept files with MIME-Type `text/html`, `text/plain` and files ending with `.dat`. After this we set up the `$widget` to accept this files.

Example 40-2. Accepting directories
    $accepted_types = array(
        'inode/directory',
    );
The MIME type `inode/directory` has to be used if the widget shall accept directories.

> Note: When a file is dropped over a widget which accepts directories only, the directory of the filename is used.

Example 40-3. Setup with callback
    <?php
    function filesDropped($widget, $arFiles) {
        foreach($arFiles as $strFile) {
            echo "The file \"$strFile\" has been dropped\r\n";
        }
    }

    $accepted_types = array(
        'text/html',
        'text/plain',
        '.dat'
    );
    Gtk_FileDrop::attach($widget, $accepted_types, 'filesDropped', false);
    ?>
In this example we use a callback function to print out all dropped files to the console. Further we determine the fourth parameter as `false` which means that the widget's value is not changed automatically. To use the callback with objects, you pass an array with the reference of the object:
Example 40-4. Using a callback
    <?php
    Gtk_FileDrop::attach($widget, $accepted_types, array( &$this, 'filesDropped'));
    ?>

Example 40-5. Working example
    <?php
    require_once 'Gtk2/FileDrop.php';

    $window = new GtkWindow();
    $window->set_default_size(300, 30);
    $window->connect_simple('destroy', array('gtk', 'main_quit'));
    $window->show();

    $entry = new GtkEntry();
    $window->add($entry);
    $entry->set_text('drop text files on me');
    Gtk_FileDrop::attach($entry, array('text/plain'));

    $window->show_all();
    Gtk::main();
    ?>

Prev | Home | Next
Widget support | Up | Gtk2_VarDump