[pygtk] Treeview drag'drop chap14

Doug Quale quale1 at charter.net
Wed Oct 5 04:33:38 WST 2005

mc collilieux <mccnews at free.fr> writes:

>   - in exemple treeviewdnd.py, the row contains only a single string
>     and the description of targets is:
>          ('MY_TREE_MODEL_ROW', gtk.TARGET_SAME_WIDGET, 0),
>          ('text/plain', 0, 1),
>          ('TEXT', 0, 2),
>          ('STRING', 0, 3),
>          ]
> Targets is a (or a list of) 3-tuple, OK, but why four tuples in this
> list for only one string in the model ?
> And first string in each tuple represent the drag type, is the "drag
> type" whatever I want to write ? as ('submarine', 0, 0) ?
> (with a little more relation with the content, of course)

TARGETS is a list of TargetEntry.  Each TargetEntry is a 3-tuple, as
you noted. The 3-tuple is (drag_type, flags, type_id).  The flags are
the easiest part to describe, as they simply restrict the possible
drag sources that the drop target will accept.  Using flags=0 means
accept any drag source, and this is perhaps most commonly used.  You
can also use gtk.TARGET_SAME_APP or gtk.TARGET_SAME_WIDGET to restrict
possible drag sources to within the same application or within the
same widget.

The drag_type is a data format specifier.  The drop target uses it to
advertize which data formats it understands.  Some formats are
standard and used by many applications, like 'text/plain', 'TEXT', and
'STRING'.  You are allowed to use your own names here, but unless the
drag source is prepared to offer the data using the same drag type
name, it won't be used.  When allowing drag and drop of complex data
types unique to a single application or set of related applications
that you control, it makes sense to use your own drag types.

The type_id 3-tuple member is just an integer id that is passed to
some callbacks so that you don't have to do a string comparison on the
drag type.  In Python I'm not sure that this field is particularly
helpful.  You can use 1, 2, ..., or simply 0 for all of them if you
aren't going to use it.

The reason that TARGETS is a list of TargetEntry is so that the drag
source and the drop target can negotiate a data format that they both
understand.  For instance, you might prefer to receive text in UTF-8,
but some applications don't support it, so you might want to also
accept simple ascii.  The drop target types are listed in order of
preference, most prefered first in the list.  The drag source also has
a list of types that it can supply the data in, in preference order,
and GTK+ will try to use the type that is the best match for the
preferences of both the source and target.  If there are no matches,
the GTK+ won't perform the drag.

> 2nd problem, i have to transform the data of my row (contains integer)
> on a single string in the callback drag_data_get_data
> and retransform piece of string in integer in drag_data_received_data.
> Is it the good way to do ?
> I see that in SelectionData.set(type, format, data), data is only a
> string, perhaps my english don't help me to understand really
> and 3rd, when I add a gtk.gdk.Pixbuf to the models, I did not make the
> drag'drop because i dont know how transform an image in string. Here
> is why, i think i have not understand very well this section of
> tutorial

Transforming the data from string to integer is the right way to do
it.  Drag data is always a string, or just a sequence of bytes.  The
drag type tells the target how to interpret the string data, so you
would use different drag types to distinguish UTF-8 and UTF-16, for

You should be able to drag and drop pixbufs too.  Probably someone
here will supply an example you can study.  Often MIME types are used
as the drag type strings, so 'image/pixbuf' might be the appropriate
drag type to use here.

> If someone can explain more to me, thanks very much,
> and if someone can explain to me in french, if my problem is bad
> translation of tutorial and pygtk reference, thanks very, very, very
> much.

You seem to understand the tutorial quite well.  Unfortunately drag
and drop is rather complex, and I'm not sure that any of the tutorials
(pygtk or GTK+) cover it in an easy to understand way, even for native
English speakers.

More information about the pygtk mailing list