Drag and Drop

Drag and Drop — Functions for controlling drag and drop handling

Functions

Types and Values

Includes

#include <gtk/gtk.h>

Description

GTK+ has a rich set of functions for doing inter-process communication via the drag-and-drop metaphor.

As well as the functions listed here, applications may need to use some facilities provided for Selections. Also, the Drag and Drop API makes use of signals in the GtkWidget class.

Functions

gtk_drag_dest_set ()

void
gtk_drag_dest_set (GtkWidget *widget,
                   GtkDestDefaults flags,
                   GdkContentFormats *targets,
                   GdkDragAction actions);

Sets a widget as a potential drop destination, and adds default behaviors.

The default behaviors listed in flags have an effect similar to installing default handlers for the widget’s drag-and-drop signals (“drag-motion”, “drag-drop”, ...). They all exist for convenience. When passing GTK_DEST_DEFAULT_ALL for instance it is sufficient to connect to the widget’s “drag-data-received” signal to get primitive, but consistent drag-and-drop support.

Things become more complicated when you try to preview the dragged data, as described in the documentation for “drag-motion”. The default behaviors described by flags make some assumptions, that can conflict with your own signal handlers. For instance GTK_DEST_DEFAULT_DROP causes invokations of gdk_drag_status() in the context of “drag-motion”, and invokations of gdk_drag_finish() in “drag-data-received”. Especially the later is dramatic, when your own “drag-motion” handler calls gtk_drag_get_data() to inspect the dragged data.

There’s no way to set a default action here, you can use the “drag-motion” callback for that. Here’s an example which selects the action to use depending on whether the control key is pressed or not:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
static void
drag_motion (GtkWidget *widget,
             GdkDrag *drag,
             gint x,
             gint y,
             guint time)
{
  GdkModifierType mask;

  gdk_surface_get_pointer (gtk_widget_get_surface (widget),
                          NULL, NULL, &mask);
  if (mask & GDK_CONTROL_MASK)
    gdk_drag_status (context, GDK_ACTION_COPY, time);
  else
    gdk_drag_status (context, GDK_ACTION_MOVE, time);
}

[method]

Parameters

widget

a GtkWidget

 

flags

which types of default drag behavior to use

 

targets

the drop types that this widget will accept, or NULL. Later you can access the list with gtk_drag_dest_get_target_list() and gtk_drag_dest_find_target().

[allow-none]

actions

a bitmask of possible actions for a drop onto this widget .

 

gtk_drag_dest_unset ()

void
gtk_drag_dest_unset (GtkWidget *widget);

Clears information about a drop destination set with gtk_drag_dest_set(). The widget will no longer receive notification of drags.

[method]

Parameters

widget

a GtkWidget

 

gtk_drag_dest_find_target ()

const char *
gtk_drag_dest_find_target (GtkWidget *widget,
                           GdkDrop *drop,
                           GdkContentFormats *target_list);

Looks for a match between the supported targets of drop and the dest_target_list , returning the first matching target, otherwise returning NULL. dest_target_list should usually be the return value from gtk_drag_dest_get_target_list(), but some widgets may have different valid targets for different parts of the widget; in that case, they will have to implement a drag_motion handler that passes the correct target list to this function.

[method]

Parameters

widget

drag destination widget

 

drop

GdkDrop

 

target_list

list of droppable targets, or NULL to use gtk_drag_dest_get_target_list (widget ).

[allow-none]

Returns

first target that the source offers and the dest can accept, or NULL.

[transfer none][nullable]

gtk_drag_dest_get_target_list ()

GdkContentFormats *
gtk_drag_dest_get_target_list (GtkWidget *widget);

Returns the list of targets this widget can accept from drag-and-drop.

[method]

Parameters

widget

a GtkWidget

 

Returns

the GdkContentFormats, or NULL if none.

[nullable][transfer none]

gtk_drag_dest_set_target_list ()

void
gtk_drag_dest_set_target_list (GtkWidget *widget,
                               GdkContentFormats *target_list);

Sets the target types that this widget can accept from drag-and-drop. The widget must first be made into a drag destination with gtk_drag_dest_set().

[method]

Parameters

widget

a GtkWidget that’s a drag destination

 

target_list

list of droppable targets, or NULL for none.

[allow-none]

gtk_drag_dest_add_text_targets ()

void
gtk_drag_dest_add_text_targets (GtkWidget *widget);

Add the text targets supported by GtkSelectionData to the target list of the drag destination. The targets are added with info = 0. If you need another value, use gtk_target_list_add_text_targets() and gtk_drag_dest_set_target_list().

[method]

Parameters

widget

a GtkWidget that’s a drag destination

 

gtk_drag_dest_add_image_targets ()

void
gtk_drag_dest_add_image_targets (GtkWidget *widget);

Add the image targets supported by GtkSelectionData to the target list of the drag destination. The targets are added with info = 0. If you need another value, use gtk_target_list_add_image_targets() and gtk_drag_dest_set_target_list().

[method]

Parameters

widget

a GtkWidget that’s a drag destination

 

gtk_drag_dest_add_uri_targets ()

void
gtk_drag_dest_add_uri_targets (GtkWidget *widget);

Add the URI targets supported by GtkSelectionData to the target list of the drag destination. The targets are added with info = 0. If you need another value, use gtk_target_list_add_uri_targets() and gtk_drag_dest_set_target_list().

[method]

Parameters

widget

a GtkWidget that’s a drag destination

 

gtk_drag_dest_set_track_motion ()

void
gtk_drag_dest_set_track_motion (GtkWidget *widget,
                                gboolean track_motion);

Tells the widget to emit “drag-motion” and “drag-leave” events regardless of the targets and the GTK_DEST_DEFAULT_MOTION flag.

This may be used when a widget wants to do generic actions regardless of the targets that the source offers.

[method]

Parameters

widget

a GtkWidget that’s a drag destination

 

track_motion

whether to accept all targets

 

gtk_drag_dest_get_track_motion ()

gboolean
gtk_drag_dest_get_track_motion (GtkWidget *widget);

Returns whether the widget has been configured to always emit “drag-motion” signals.

[method]

Parameters

widget

a GtkWidget that’s a drag destination

 

Returns

TRUE if the widget always emits “drag-motion” events

gtk_drag_get_data ()

void
gtk_drag_get_data (GtkWidget *widget,
                   GdkDrop *drop,
                   GdkAtom target);

Gets the data associated with a drag. When the data is received or the retrieval fails, GTK+ will emit a “drag-data-received” signal. Failure of the retrieval is indicated by the length field of the selection_data signal parameter being negative. However, when gtk_drag_get_data() is called implicitely because the GTK_DEST_DEFAULT_DROP was set, then the widget will not receive notification of failed drops.

[method]

Parameters

widget

the widget that will receive the “drag-data-received” signal

 

drop

the GdkDrop

 

target

the target (form of the data) to retrieve

 

gtk_drag_get_source_widget ()

GtkWidget *
gtk_drag_get_source_widget (GdkDrag *drag);

Determines the source widget for a drag.

Parameters

drag

a drag context

 

Returns

if the drag is occurring within a single application, a pointer to the source widget. Otherwise, NULL.

[nullable][transfer none]

gtk_drag_highlight ()

void
gtk_drag_highlight (GtkWidget *widget);

Highlights a widget as a currently hovered drop target. To end the highlight, call gtk_drag_unhighlight(). GTK+ calls this automatically if GTK_DEST_DEFAULT_HIGHLIGHT is set.

[method]

Parameters

widget

a widget to highlight

 

gtk_drag_unhighlight ()

void
gtk_drag_unhighlight (GtkWidget *widget);

Removes a highlight set by gtk_drag_highlight() from a widget.

[method]

Parameters

widget

a widget to remove the highlight from

 

gtk_drag_begin ()

GdkDrag *
gtk_drag_begin (GtkWidget *widget,
                GdkDevice *device,
                GdkContentFormats *targets,
                GdkDragAction actions,
                gint x,
                gint y);

Initiates a drag on the source side. The function only needs to be used when the application is starting drags itself, and is not needed when gtk_drag_source_set() is used.

[method]

Parameters

widget

the source widget

 

device

the device that starts the drag or NULL to use the default pointer.

[nullable]

targets

The targets (data formats) in which the source can provide the data

 

actions

A bitmask of the allowed drag actions for this drag

 

x

The initial x coordinate to start dragging from, in the coordinate space of widget .

 

y

The initial y coordinate to start dragging from, in the coordinate space of widget .

 

Returns

the context for this drag.

[transfer none]

gtk_drag_cancel ()

void
gtk_drag_cancel (GdkDrag *drag);

Cancels an ongoing drag operation on the source side.

If you want to be able to cancel a drag operation in this way, you need to keep a pointer to the drag context, either from an explicit call to gtk_drag_begin(), or by connecting to “drag-begin”.

If context does not refer to an ongoing drag operation, this function does nothing.

If a drag is cancelled in this way, the result argument of “drag-failed” is set to GTK_DRAG_RESULT_ERROR .

Parameters

drag

a drag context, as e.g. returned by gtk_drag_begin()

 

gtk_drag_set_icon_widget ()

void
gtk_drag_set_icon_widget (GdkDrag *drag,
                          GtkWidget *widget,
                          gint hot_x,
                          gint hot_y);

Changes the icon for drag operation to a given widget. GTK+ will not destroy the widget, so if you don’t want it to persist, you should connect to the “drag-end” signal and destroy it yourself.

Parameters

drag

the context for a drag

 

widget

a widget to use as an icon

 

hot_x

the X offset within widget of the hotspot

 

hot_y

the Y offset within widget of the hotspot

 

gtk_drag_set_icon_paintable ()

void
gtk_drag_set_icon_paintable (GdkDrag *drag,
                             GdkPaintable *paintable,
                             int hot_x,
                             int hot_y);

Sets paintable as the icon for a given drag. GTK+ retains references for the arguments, and will release them when they are no longer needed.

To position the paintable relative to the mouse, its top left will be positioned hot_x , hot_y pixels from the mouse cursor.

Parameters

drag

the context for a drag

 

paintable

the GdkPaintable to use as icon

 

hot_x

the X offset of the hotspot within the icon

 

hot_y

the Y offset of the hotspot within the icon

 

gtk_drag_set_icon_name ()

void
gtk_drag_set_icon_name (GdkDrag *drag,
                        const gchar *icon_name,
                        gint hot_x,
                        gint hot_y);

Sets the icon for a given drag from a named themed icon. See the docs for GtkIconTheme for more details. Note that the size of the icon depends on the icon theme (the icon is loaded at the symbolic size GTK_ICON_SIZE_DND), thus hot_x and hot_y have to be used with care.

Parameters

drag

the context for a drag

 

icon_name

name of icon to use

 

hot_x

the X offset of the hotspot within the icon

 

hot_y

the Y offset of the hotspot within the icon

 

gtk_drag_set_icon_gicon ()

void
gtk_drag_set_icon_gicon (GdkDrag *drag,
                         GIcon *icon,
                         gint hot_x,
                         gint hot_y);

Sets the icon for a given drag from the given icon . See the documentation for gtk_drag_set_icon_name() for more details about using icons in drag and drop.

Parameters

drag

the context for a drag

 

icon

a GIcon

 

hot_x

the X offset of the hotspot within the icon

 

hot_y

the Y offset of the hotspot within the icon

 

gtk_drag_set_icon_default ()

void
gtk_drag_set_icon_default (GdkDrag *drag);

Sets the icon for a particular drag to the default icon.

Parameters

drag

the context for a drag

 

gtk_drag_check_threshold ()

gboolean
gtk_drag_check_threshold (GtkWidget *widget,
                          gint start_x,
                          gint start_y,
                          gint current_x,
                          gint current_y);

Checks to see if a mouse drag starting at (start_x , start_y ) and ending at (current_x , current_y ) has passed the GTK+ drag threshold, and thus should trigger the beginning of a drag-and-drop operation.

[method]

Parameters

widget

a GtkWidget

 

start_x

X coordinate of start of drag

 

start_y

Y coordinate of start of drag

 

current_x

current X coordinate

 

current_y

current Y coordinate

 

Returns

TRUE if the drag threshold has been passed.

gtk_drag_source_set ()

void
gtk_drag_source_set (GtkWidget *widget,
                     GdkModifierType start_button_mask,
                     GdkContentFormats *targets,
                     GdkDragAction actions);

Sets up a widget so that GTK+ will start a drag operation when the user clicks and drags on the widget. The widget must have a window.

[method]

Parameters

widget

a GtkWidget

 

start_button_mask

the bitmask of buttons that can start the drag

 

targets

the targets that the drag will support, may be NULL.

[allow-none]

actions

the bitmask of possible actions for a drag from this widget

 

gtk_drag_source_set_icon_name ()

void
gtk_drag_source_set_icon_name (GtkWidget *widget,
                               const gchar *icon_name);

Sets the icon that will be used for drags from a particular source to a themed icon. See the docs for GtkIconTheme for more details.

[method]

Parameters

widget

a GtkWidget

 

icon_name

name of icon to use

 

gtk_drag_source_set_icon_gicon ()

void
gtk_drag_source_set_icon_gicon (GtkWidget *widget,
                                GIcon *icon);

Sets the icon that will be used for drags from a particular source to icon . See the docs for GtkIconTheme for more details.

[method]

Parameters

widget

a GtkWidget

 

icon

A GIcon

 

gtk_drag_source_set_icon_paintable ()

void
gtk_drag_source_set_icon_paintable (GtkWidget *widget,
                                    GdkPaintable *paintable);

Sets the icon that will be used for drags from a particular source to paintable .

[method]

Parameters

widget

a GtkWidget

 

paintable

A GdkPaintable

 

gtk_drag_source_unset ()

void
gtk_drag_source_unset (GtkWidget *widget);

Undoes the effects of gtk_drag_source_set().

[method]

Parameters

widget

a GtkWidget

 

gtk_drag_source_set_target_list ()

void
gtk_drag_source_set_target_list (GtkWidget *widget,
                                 GdkContentFormats *target_list);

Changes the target types that this widget offers for drag-and-drop. The widget must first be made into a drag source with gtk_drag_source_set().

[method]

Parameters

widget

a GtkWidget that’s a drag source

 

target_list

list of draggable targets, or NULL for none.

[allow-none]

gtk_drag_source_get_target_list ()

GdkContentFormats *
gtk_drag_source_get_target_list (GtkWidget *widget);

Gets the list of targets this widget can provide for drag-and-drop.

[method]

Parameters

widget

a GtkWidget

 

Returns

the GdkContentFormats, or NULL if none.

[nullable][transfer none]

gtk_drag_source_add_text_targets ()

void
gtk_drag_source_add_text_targets (GtkWidget *widget);

Add the text targets supported by GtkSelectionData to the target list of the drag source. The targets are added with info = 0. If you need another value, use gtk_content_formats_add_text_targets() and gtk_drag_source_set_target_list().

[method]

Parameters

widget

a GtkWidget that’s is a drag source

 

gtk_drag_source_add_image_targets ()

void
gtk_drag_source_add_image_targets (GtkWidget *widget);

Add the writable image targets supported by GtkSelectionData to the target list of the drag source. The targets are added with info = 0. If you need another value, use gtk_target_list_add_image_targets() and gtk_drag_source_set_target_list().

[method]

Parameters

widget

a GtkWidget that’s is a drag source

 

gtk_drag_source_add_uri_targets ()

void
gtk_drag_source_add_uri_targets (GtkWidget *widget);

Add the URI targets supported by GtkSelectionData to the target list of the drag source. The targets are added with info = 0. If you need another value, use gtk_content_formats_add_uri_targets() and gtk_drag_source_set_target_list().

[method]

Parameters

widget

a GtkWidget that’s is a drag source

 

Types and Values

enum GtkDestDefaults

The GtkDestDefaults enumeration specifies the various types of action that will be taken on behalf of the user for a drag destination site.

Members

GTK_DEST_DEFAULT_MOTION

If set for a widget, GTK+, during a drag over this widget will check if the drag matches this widget’s list of possible formats and actions. GTK+ will then call gdk_drag_status() as appropriate.

 

GTK_DEST_DEFAULT_HIGHLIGHT

If set for a widget, GTK+ will draw a highlight on this widget as long as a drag is over this widget and the widget drag format and action are acceptable.

 

GTK_DEST_DEFAULT_DROP

If set for a widget, when a drop occurs, GTK+ will will check if the drag matches this widget’s list of possible formats and actions. If so, GTK+ will call gtk_drag_get_data() on behalf of the widget. Whether or not the drop is successful, GTK+ will call gdk_drag_finish(). If the action was a move, then if the drag was successful, then TRUE will be passed for the delete parameter to gdk_drag_finish().

 

GTK_DEST_DEFAULT_ALL

If set, specifies that all default actions should be taken.

 

enum GtkDragResult

Gives an indication why a drag operation failed. The value can by obtained by connecting to the “drag-failed” signal.

Members

GTK_DRAG_RESULT_SUCCESS

The drag operation was successful.

 

GTK_DRAG_RESULT_NO_TARGET

No suitable drag target.

 

GTK_DRAG_RESULT_USER_CANCELLED

The user cancelled the drag operation.

 

GTK_DRAG_RESULT_TIMEOUT_EXPIRED

The drag operation timed out.

 

GTK_DRAG_RESULT_GRAB_BROKEN

The pointer or keyboard grab used for the drag operation was broken.

 

GTK_DRAG_RESULT_ERROR

The drag operation failed due to some unspecified error.