GTK+ 3 Reference Manual: Drag and Drop

## Drag and Drop Drag and Drop — Functions for controlling drag and drop handling |

## Functions

void | gtk_drag_dest_set ()
void | gtk_drag_dest_set_proxy ()
void | gtk_drag_dest_unset ()
GdkAtom | gtk_drag_dest_find_target ()
GtkTargetList * | gtk_drag_dest_get_target_list ()
void | gtk_drag_dest_set_target_list ()
void | gtk_drag_dest_add_text_targets ()
void | gtk_drag_dest_add_image_targets ()
void | gtk_drag_dest_add_uri_targets ()
void | gtk_drag_dest_set_track_motion ()
gboolean | gtk_drag_dest_get_track_motion ()
void | gtk_drag_finish ()
void | gtk_drag_get_data ()
GtkWidget * | gtk_drag_get_source_widget ()
void | gtk_drag_highlight ()
void | gtk_drag_unhighlight ()
GdkDragContext * | gtk_drag_begin ()
GdkDragContext * | gtk_drag_begin_with_coordinates ()
void | gtk_drag_set_icon_widget ()
void | gtk_drag_set_icon_pixbuf ()
void | gtk_drag_set_icon_stock ()
void | gtk_drag_set_icon_surface ()
void | gtk_drag_set_icon_name ()
void | gtk_drag_set_icon_gicon ()
void | gtk_drag_set_icon_default ()
gboolean | gtk_drag_check_threshold ()
void | gtk_drag_source_set ()
void | gtk_drag_source_set_icon_pixbuf ()
void | gtk_drag_source_set_icon_stock ()
void | gtk_drag_source_set_icon_name ()
void | gtk_drag_source_set_icon_gicon ()
void | gtk_drag_source_unset ()
void | gtk_drag_source_set_target_list ()
GtkTargetList * | gtk_drag_source_get_target_list ()
void | gtk_drag_source_add_text_targets ()
void | gtk_drag_source_add_image_targets ()
void | gtk_drag_source_add_uri_targets ()

## Types and Values

enum | GtkDestDefaults
enum | GtkTargetFlags
enum | GtkDragResult

## 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,
                       const GtkTargetEntry *targets,
                       gint n_targets,
                       GdkDragAction actions);

#### Parameters

widget | a GtkWidget |
flags | which types of default drag behavior to use |
targets | a pointer to an array of GtkTargetEntrys indicating 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][array length=n_targets]
n_targets | the number of entries in `targets` |
actions | a bitmask of possible actions for a drop onto this `widget` . |

### gtk_drag_dest_set_proxy ()

    void
    gtk_drag_dest_set_proxy (GtkWidget *widget,
                             GdkWindow *proxy_window,
                             GdkDragProtocol protocol,
                             gboolean use_coordinates);

#### Parameters

widget | a GtkWidget |
proxy_window | the window to which to forward drag events |
protocol | the drag protocol which the `proxy_window` accepts (You can use `gdk_drag_get_protocol()` to determine this) |
use_coordinates | If `TRUE`, send the same coordinates to the destination, because it is an embedded subwindow. |

### gtk_drag_dest_unset ()

    void
    gtk_drag_dest_unset (GtkWidget *widget);

#### Parameters

widget | a GtkWidget |

### gtk_drag_dest_find_target ()

    GdkAtom
    gtk_drag_dest_find_target (GtkWidget *widget,
                               GdkDragContext *context,
                               GtkTargetList *target_list);

#### Parameters

widget | drag destination widget |
context | drag context |
target_list | list of droppable targets, or `NULL` to use gtk_drag_dest_get_target_list (`widget` ). | [allow-none]

### gtk_drag_dest_get_target_list ()

    GtkTargetList *
    gtk_drag_dest_get_target_list (GtkWidget *widget);

#### Parameters

widget | a GtkWidget |

### gtk_drag_dest_set_target_list ()

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

#### 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);

#### Parameters

widget | a GtkWidget that’s a drag destination |

Since 2.6

### gtk_drag_dest_add_image_targets ()

    void
    gtk_drag_dest_add_image_targets (GtkWidget *widget);

#### Parameters

widget | a GtkWidget that’s a drag destination |

Since 2.6

### gtk_drag_dest_add_uri_targets ()

    void
    gtk_drag_dest_add_uri_targets (GtkWidget *widget);

#### Parameters

widget | a GtkWidget that’s a drag destination |

Since 2.6

### gtk_drag_dest_set_track_motion ()

    void
    gtk_drag_dest_set_track_motion (GtkWidget *widget,
                                    gboolean track_motion);

#### Parameters

widget | a GtkWidget that’s a drag destination |
track_motion | whether to accept all targets |

Since 2.10

### gtk_drag_dest_get_track_motion ()

    gboolean
    gtk_drag_dest_get_track_motion (GtkWidget *widget);

#### Parameters

widget | a GtkWidget that’s a drag destination |

Since 2.10

### gtk_drag_finish ()

    void
    gtk_drag_finish (GdkDragContext *context,
                     gboolean success,
                     gboolean del,
                     guint32 time_);

#### Parameters

context | the drag context. |
success | a flag indicating whether the drop was successful |
del | a flag indicating whether the source should delete the original data. (This should be `TRUE` for a move) |
time_ | the timestamp from the “drag-drop” signal. |

### gtk_drag_get_data ()

    void
    gtk_drag_get_data (GtkWidget *widget,
                       GdkDragContext *context,
                       GdkAtom target,
                       guint32 time_);

#### Parameters

widget | the widget that will receive the “drag-data-received” signal. |
context | the drag context |
target | the target (form of the data) to retrieve. |
time_ | a timestamp for retrieving the data. This will generally be the time received in a “drag-motion”" or “drag-drop”" signal. |

### gtk_drag_get_source_widget ()

    GtkWidget *
    gtk_drag_get_source_widget (GdkDragContext *context);

#### Parameters

context | a (destination side) drag context |

### gtk_drag_highlight ()

    void
    gtk_drag_highlight (GtkWidget *widget);

#### Parameters

widget | a widget to highlight |

### gtk_drag_unhighlight ()

    void
    gtk_drag_unhighlight (GtkWidget *widget);

#### Parameters

widget | a widget to remove the highlight from. |

### gtk_drag_begin ()

    GdkDragContext *
    gtk_drag_begin (GtkWidget *widget,
                    GtkTargetList *targets,
                    GdkDragAction actions,
                    gint button,
                    GdkEvent *event);

`gtk_drag_begin` has been deprecated since version 3.10 and should not be used in newly-written code.

Use `gtk_drag_begin_with_coordinates()` instead

#### Parameters

widget | the source widget. |
targets | The targets (data formats) in which the source can provide the data. |
actions | A bitmask of the allowed drag actions for this drag. |
button | The button the user clicked to start the drag. |
event | The event that triggered the start of the drag. |

### gtk_drag_begin_with_coordinates ()

    GdkDragContext *
    gtk_drag_begin_with_coordinates (GtkWidget *widget,
                                     GtkTargetList *targets,
                                     GdkDragAction actions,
                                     gint button,
                                     GdkEvent *event,
                                     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.

The `event` is used to retrieve the timestamp that will be used internally to grab the pointer. If `event` is `NULL`, then `GDK_CURRENT_TIME` will be used. However, you should try to pass a real event in all cases, since that can be used to get information about the drag.

Generally there are three cases when you want to start a drag by hand by calling this function:

1. During a “button-press-event” handler, if you want to start a drag immediately when the user presses the mouse button. Pass the `event` that you have in your “button-press-event” handler.

2. During a “motion-notify-event” handler, if you want to start a drag when the mouse moves past a certain threshold distance after a button-press. Pass the `event` that you have in your “motion-notify-event” handler.

3. During a timeout handler, if you want to start a drag after the mouse button is held down for some time. Try to save the last event that you got from the mouse, using `gdk_event_copy()`, and pass it to this function (remember to free the event with `gdk_event_free()` when you are done). If you can really not pass a real event, pass NULL instead.

#### Parameters

widget | the source widget |
targets | The targets (data formats) in which the source can provide the data |
actions | A bitmask of the allowed drag actions for this drag |
button | The button the user clicked to start the drag |
event | The event that triggered the start of the drag |
x | The initial x coordinate to start dragging from, in the coordinate space of `widget` . If -1 is passed, the coordinates are retrieved from `event` or the current pointer position |
y | The initial y coordinate to start dragging from, in the coordinate space of `widget` . If -1 is passed, the coordinates are retrieved from `event` or the current pointer position |

#### Returns

the context for this drag.

[transfer none]

Since 3.10

### gtk_drag_set_icon_widget ()

    void
    gtk_drag_set_icon_widget (GdkDragContext *context,
                              GtkWidget *widget,
                              gint hot_x,
                              gint hot_y);

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

#### Parameters

context | the context for a drag. (This must be called with a context for the source side of a drag) |
widget | a toplevel window 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_pixbuf ()

    void
    gtk_drag_set_icon_pixbuf (GdkDragContext *context,
                              GdkPixbuf *pixbuf,
                              gint hot_x,
                              gint hot_y);

Sets `pixbuf` as the icon for a given drag.

#### Parameters

context | the context for a drag. (This must be called with a context for the source side of a drag) |
pixbuf | the GdkPixbuf to use as the drag 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_stock ()

    void
    gtk_drag_set_icon_stock (GdkDragContext *context,
                             const gchar *stock_id,
                             gint hot_x,
                             gint hot_y);

`gtk_drag_set_icon_stock` has been deprecated since version 3.10 and should not be used in newly-written code.

Use `gtk_drag_set_icon_name()` instead.

Sets the icon for a given drag from a stock ID.

#### Parameters

context | the context for a drag. (This must be called with a context for the source side of a drag) |
stock_id | the ID of the stock icon to use for the drag. |
hot_x | the X offset within the icon of the hotspot. |
hot_y | the Y offset within the icon of the hotspot. |

### gtk_drag_set_icon_surface ()

    void
    gtk_drag_set_icon_surface (GdkDragContext *context,
                               cairo_surface_t *surface);

Sets `surface` 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 surface relative to the mouse, use `cairo_surface_set_device_offset()` on `surface` . The mouse cursor will be positioned at the (0,0) coordinate of the surface.

#### Parameters

context | the context for a drag. (This must be called with a context for the source side of a drag) |
surface | the surface to use as icon |

### gtk_drag_set_icon_name ()

    void
    gtk_drag_set_icon_name (GdkDragContext *context,
                            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

context | the context for a drag. (This must be called with a context for the source side of 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 |

Since 2.8

### gtk_drag_set_icon_gicon ()

    void
    gtk_drag_set_icon_gicon (GdkDragContext *context,
                             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

context | the context for a drag (This must be called with a context for the source side of 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 |

Since 3.2

### gtk_drag_set_icon_default ()

    void
    gtk_drag_set_icon_default (GdkDragContext *context);

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

#### Parameters

context | the context for a drag. (This must be called with a context for the source side of 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);

#### 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 |

### gtk_drag_source_set ()

    void
    gtk_drag_source_set (GtkWidget *widget,
                         GdkModifierType start_button_mask,
                         const GtkTargetEntry *targets,
                         gint n_targets,
                         GdkDragAction actions);

#### Parameters

widget | a GtkWidget |
start_button_mask | the bitmask of buttons that can start the drag |
targets | the table of targets that the drag will support, may be `NULL`. | [allow-none][array length=n_targets]
n_targets | the number of items in `targets` |
actions | the bitmask of possible actions for a drag from this widget |

### gtk_drag_source_set_icon_pixbuf ()

    void
    gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
                                     GdkPixbuf *pixbuf);

#### Parameters

widget | a GtkWidget |
pixbuf | the GdkPixbuf for the drag icon |

### gtk_drag_source_set_icon_stock ()

    void
    gtk_drag_source_set_icon_stock (GtkWidget *widget,
                                    const gchar *stock_id);

`gtk_drag_source_set_icon_stock` has been deprecated since version 3.10 and should not be used in newly-written code.

Use `gtk_drag_source_set_icon_name()` instead.

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

#### Parameters

widget | a GtkWidget |
stock_id | the ID of the stock icon to use |

### 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.

#### Parameters

widget | a GtkWidget |
icon_name | name of icon to use |

Since 2.8

### 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.

#### Parameters

widget | a GtkWidget |
icon | A GIcon |

Since 3.2

### gtk_drag_source_unset ()

    void
    gtk_drag_source_unset (GtkWidget *widget);

#### Parameters

widget | a GtkWidget |

### gtk_drag_source_set_target_list ()

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

#### Parameters

widget | a GtkWidget that’s a drag source |
target_list | list of draggable targets, or `NULL` for none. | [allow-none]

Since 2.4

### gtk_drag_source_get_target_list ()

    GtkTargetList *
    gtk_drag_source_get_target_list (GtkWidget *widget);

#### Parameters

widget | a GtkWidget |

Since 2.4

### gtk_drag_source_add_text_targets ()

    void
    gtk_drag_source_add_text_targets (GtkWidget *widget);

Add the text targets supported by GtkSelection 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_text_targets()` and `gtk_drag_source_set_target_list()`.

#### Parameters

widget | a GtkWidget that’s is a drag source |

Since 2.6

### gtk_drag_source_add_image_targets ()

    void
    gtk_drag_source_add_image_targets (GtkWidget *widget);

#### Parameters

widget | a GtkWidget that’s is a drag source |

Since 2.6

### gtk_drag_source_add_uri_targets ()

    void
    gtk_drag_source_add_uri_targets (GtkWidget *widget);

#### Parameters

widget | a GtkWidget that’s is a drag source |

Since 2.6

## 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 targets 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 targets 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 `gtk_drag_finish()`. If the action was a move, then if the drag was successful, then `TRUE` will be passed for the `delete` parameter to `gtk_drag_finish()`. |
GTK_DEST_DEFAULT_ALL | If set, specifies that all default actions should be taken. |

### enum GtkTargetFlags

The GtkTargetFlags enumeration is used to specify constraints on a GtkTargetEntry.

#### Members

GTK_TARGET_SAME_APP | If this is set, the target will only be selected for drags within a single application. |
GTK_TARGET_SAME_WIDGET | If this is set, the target will only be selected for drags within a single widget. |
GTK_TARGET_OTHER_APP | If this is set, the target will not be selected for drags within a single application. |
GTK_TARGET_OTHER_WIDGET | If this is set, the target will not be selected for drags withing a single widget. |

### 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. |