Re: new Target API [was: Re: introspection and broken API]



Ryan McDougall wrote:

I noticed the new API in devhelp, but not how to use it. I mostly went
by tests/testdnd.c.
The tests/ subdirectory is not the best place to look for examples. The files found there are often designed to stress test parts of GTK, or try and expose a particular bug. They shouldn't be considered "best practices" for using GTK -- demos/gtk-demo is a better place to look, but doesn't seem to have a good example for this.

How do you tell a widget to use a GtkTargetList? Create an empty
GtkTargetEntry, call gtk_drag_dest_set with empty entry array, then call
gtk_drag_dest_set_target_list?
A GtkTargetList is used to represent the list of formats that a widget can provide for a drag, or accept for a drop. If you are just using the simple APIs like gtk_drag_source_set() and gtk_drag_dest_set(), you just pass in an array of GtkTargetEntry's, which get converted into a target list.

The gtk_drag_{dest,source}_add_XXXX_targets() functions similarly let you ignore GtkTargetLists, so you may as well suggest their use in your tutorial. Just call them after the corresponding gtk_drag_{dest,source}_set() call. The "info" argument will be passed to your "drag_data_get" or "drag_data_received" handlers (it looks like you are calling this "target_type" in your tutorial).

In your "drag_data_get" or "drag_data_received" handlers, you can use the appropriate gtk_selection_data_* function to set or get the text/image/URI in the GtkSelectionData object. Any necessary conversions will be taken care of for you.

I'll add the new stuff to my tutorial ASAP.

Also, I tried passing an gint32 in my tutorial since that was the
simplest datatype not included in your new API. Is there a reason you
can't pass integers?
You can pass any kind of data via DnD -- you just need to make sure the sender and receiver agree on a name for that format. I don't know if there is a commonly used format name for a 32-bit integer.

Do you have a particular use case in mind? Possibly passing the integer as a string would be a good idea (you might want to advertise it as such whatever you do, so that you can drag to apps that can accept text drags).

Another note about your example: the way you pass an integer in the selection is not 64-bit clean. When using format==32, you are expected to pass in an array of long's. On 32-bit systems this is fine, since gint32 is the same size as long. But on most 64-bit systems they are different, which will cause troubles (yes, this API is a bit weird, but it is weird in the same way as Xlib). So your code should probably look like this:

const long integer_data = 42;
gtk_selection_data_set(selection_data, selection_data->target, 32, &integer_data, sizeof(integer_data));

Good luck,

James.



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