Re: need some selection and dnd code



On Tue, May 27, 2003 at 03:16:20PM -0400, muppet wrote:

i'm looking at writing bindings for the selection and drag and drop functions
for gtk2-perl.

unfortunately, i have not use these APIs before, and so have no sense of
what's a normal usage and how the stuff should be wrapped.

so, if anybody could give me a primer on X selections and drag and drop in
Gtk, preferrably with some working C code, i'd be greatly appreciative.

I have only used a part of the dnd api, but from what I have seen there
aren't many changes from gtk 1.2.

The changes document at
http://developer.gnome.org/doc/API/2.0/gtk/gtk-changes-2-0.html
doesn't list any changes at all regarding X selections and DnD handling.

There are some information at
http://developer.gnome.org/doc/tutorials/gnome-libs/drag-and-drop.html (gtk1)
and
http://www.gtk.org/tutorial/ (gtk2)

There is something at 
http://wolfpack.twu.net/docs/gtkdnd/index.html
but I don't now good it is.

The old gtk-perl contained an example program called testdnd.pl which I
believe was converted from a C code example, testdnd.c.




Anyway, here is what I would normally do in a gtk-perl program to allow
drags and drops on widgets.  It is the basic functionality needed to use
dnd in an application.  I hope it can give you an idea of how it works,
even though it is only using the most basic part of it.

Common data for drag types:

our @image_drag_targets = ( { target=>"STRING", flags=>0, info=>0},
                            { target=>"text/plain", flags=>0, info=>1},
                            { target=>"text/uri", flags=>0, info=>2 },
                            { target=>"text/uri-list", flags=>0, info=>3 }
                          );


To allow drags from a widget:

    $widget->drag_source_set('button1_mask', 'copy', @image_drag_targets);
    widget->signal_connect('drag-data-get' => \&on_drag_data_get);

First arg is an event mask, second is 'copy', 'move or ['copy', 'move'].

The handler to send data on a successful drop somewhere else:

sub on_drag_data_get {
    my ($widget, $context, $data, $info, $time) = @_;
    $data->set($data->target, 8, $the_data_you_want_to_send);
}

To receive drops:

    $widget->drag_dest_set('all', 'copy', @image_drag_targets);
    $widget->signal_connect('drag-data-received' => \&on_drag_data_received);

The handler to receive drops:

sub on_drag_data_received {
    my ($widget, $context, $x, $y, $data, $info, $time) = @_;

    return unless ($data->length >= 0 and $data->format == 8);

    # Drag data is $data->data;

    printf("Recived type %s format %s length %d target %s selection %s\n",
           $data->type, $data->format, $data->length, $data->target, $data->selection);
    printf("Received $x $y dragged \"%s\"\n", $data->data);

    ...

    $context->finish(1, 0, $time);
}


-- 
René Seindal (rene seindal dk)                  http://sights.seindal.dk/




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]