GTK+ Reference Manual
<<< Previous Page | Home | Up | Next Page >>>

# Drag and Drop

## Name

Drag and Drop -- Functions for controlling drag and drop handling.

## Synopsis

    #include <gtk/gtk.h>

    enum        GtkDestDefaults;
    enum        GtkTargetFlags;

    void        gtk_drag_dest_set               (GtkWidget *widget,
                                                 GtkDestDefaults flags,
                                                 const GtkTargetEntry *targets,
                                                 gint n_targets,
                                                 GdkDragAction actions);
    void        gtk_drag_dest_set_proxy         (GtkWidget *widget,
                                                 GdkWindow *proxy_window,
                                                 GdkDragProtocol protocol,
                                                 gboolean use_coordinates);
    void        gtk_drag_dest_unset             (GtkWidget *widget);
    void        gtk_drag_finish                 (GdkDragContext *context,
                                                 gboolean success,
                                                 gboolean del,
                                                 guint32 time);
    void        gtk_drag_get_data               (GtkWidget *widget,
                                                 GdkDragContext *context,
                                                 GdkAtom target,
                                                 guint32 time);
    GtkWidget*  gtk_drag_get_source_widget      (GdkDragContext *context);
    void        gtk_drag_highlight              (GtkWidget *widget);
    void        gtk_drag_unhighlight            (GtkWidget *widget);

    GdkDragContext* gtk_drag_begin              (GtkWidget *widget,
                                                 GtkTargetList *targets,
                                                 GdkDragAction actions,
                                                 gint button,
                                                 GdkEvent *event);
    void        gtk_drag_set_icon_widget        (GdkDragContext *context,
                                                 GtkWidget *widget,
                                                 gint hot_x,
                                                 gint hot_y);
    void        gtk_drag_set_icon_pixmap        (GdkDragContext *context,
                                                 GdkColormap *colormap,
                                                 GdkPixmap *pixmap,
                                                 GdkBitmap *mask,
                                                 gint hot_x,
                                                 gint hot_y);
    void        gtk_drag_set_icon_default       (GdkDragContext *context);
    void        gtk_drag_set_default_icon       (GdkColormap *colormap,
                                                 GdkPixmap *pixmap,
                                                 GdkBitmap *mask,
                                                 gint hot_x,
                                                 gint hot_y);
    void        gtk_drag_source_set             (GtkWidget *widget,
                                                 GdkModifierType start_button_mask,
                                                 const GtkTargetEntry *targets,
                                                 gint n_targets,
                                                 GdkDragAction actions);
    void        gtk_drag_source_set_icon        (GtkWidget *widget,
                                                 GdkColormap *colormap,
                                                 GdkPixmap *pixmap,
                                                 GdkBitmap *mask);
    void        gtk_drag_source_unset           (GtkWidget *widget);

    void        gtk_drag_source_handle_event    (GtkWidget *widget,
                                                 GdkEvent *event);
    void        gtk_drag_dest_handle_event      (GtkWidget *toplevel,
                                                 GdkEvent *event);

## Description

GTK+ has a rich set of functions for doing inter-process communication via the drag-and-drop metaphore. GTK+ can do drag and drop (DND) via multiple protocols. The currently supported protocols are the Xdnd and Motif protocols. 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.

## Details

### enum GtkDestDefaults

    typedef enum {
      GTK_DEST_DEFAULT_MOTION     = 1 << 0, /* respond to "drag_motion" */
      GTK_DEST_DEFAULT_HIGHLIGHT  = 1 << 1, /* auto-highlight */
      GTK_DEST_DEFAULT_DROP       = 1 << 2, /* respond to "drag_drop" */
      GTK_DEST_DEFAULT_ALL        = 0x07
    } GtkDestDefaults;

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

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 gtk_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 wiget drag format and action is accetable.
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_data_get() on behalf of the widget. Whether or not the drop is succesful, GTK+ will call gtk_drag_finish(). If the action was a move, then if the drag was succesful, 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

    typedef enum {
      GTK_TARGET_SAME_APP = 1 << 0,    /*< nick=same-app >*/
      GTK_TARGET_SAME_WIDGET = 1 << 1  /*< nick=same-widget >*/
    } GtkTargetFlags;

The GtkTargetFlags enumeration is used to specifies constraints on an entry in a GtkTargetTable.

`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_drag_dest_set ()

    void        gtk_drag_dest_set               (GtkWidget *widget,
                                                 GtkDestDefaults flags,
                                                 const GtkTargetEntry *targets,
                                                 gint n_targets,
                                                 GdkDragAction actions);

Set a widget as a potential drop destination.

`widget` : | a widget
`flags` : | the flags that specify what actions GTK+ should take on behalf of a widget for drops onto that widget. The `targets` and `actions` fields only are used if `GTK_DEST_DEFAULT_MOTION` or `GTK_DEST_DEFAULT_DROP` are given.
`targets` : | a pointer to an array of GtkTargetEntry indicating the drop types that this widget will accept.
`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);

Set this widget as a proxy for drops to another window.

`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 a embedded subwindow.

### gtk_drag_dest_unset ()

    void        gtk_drag_dest_unset             (GtkWidget *widget);

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

`widget` : | a GtkWidget

### gtk_drag_finish ()

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

Inform the drag source that the drop is finished, and that the data of the drag will no longer be required.

`context` : | the drag context.
`success` : | a flag indicating whether the drop was succesful
`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_data_drop" signal.

### gtk_drag_get_data ()

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

Get 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_DRAG_DEFAULT_DROP` was set, then the widget will not receive notification of failed drops.

`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_data_motion" or "drag_data_drop" signal.

### gtk_drag_get_source_widget ()

    GtkWidget*  gtk_drag_get_source_widget      (GdkDragContext *context);

Determine the source widget for a drag.

`context` : | a (destination side) drag context.
Returns : | if the drag is occurring within a single application, a pointer to the source widget. Otherwise, NULL.

### gtk_drag_highlight ()

    void        gtk_drag_highlight              (GtkWidget *widget);

Draw a highlight around a widget. This will attach handlers to "expose_event" and "draw", so the highlight will continue to be displayed until gtk_drag_unhighlight is called.

`widget` : | a widget to highlight

### gtk_drag_unhighlight ()

    void        gtk_drag_unhighlight            (GtkWidget *widget);

Remove a highlight set by gtk_drag_highlight() from a widget. is called.

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

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

`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. Usually
Returns : | The context for this drag.

### gtk_drag_set_icon_widget ()

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

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

`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_pixmap ()

    void        gtk_drag_set_icon_pixmap        (GdkDragContext *context,
                                                 GdkColormap *colormap,
                                                 GdkPixmap *pixmap,
                                                 GdkBitmap *mask,
                                                 gint hot_x,
                                                 gint hot_y);

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

`context` : | the context for a drag. (This must be called with a context for the source side of a drag)
`colormap` : | the colormap of the icon
`pixmap` : | the image data for the icon
`mask` : | the transparency mask for an image.
`hot_x` : | The X offset within `widget` of the hotspot.
`hot_y` : | The Y offset within `widget` of the hotspot.

### gtk_drag_set_icon_default ()

    void        gtk_drag_set_icon_default       (GdkDragContext *context);

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

`context` : | the context for a drag. (This must be called with a context for the source side of a drag)

### gtk_drag_set_default_icon ()

    void        gtk_drag_set_default_icon       (GdkColormap *colormap,
                                                 GdkPixmap *pixmap,
                                                 GdkBitmap *mask,
                                                 gint hot_x,
                                                 gint hot_y);

Change the default drag icon. GTK+ retains a reference count for the arguments, and will release them when they are no longer needed.

`colormap` : | the colormap of the icon
`pixmap` : | the image data for the icon
`mask` : | the transparency mask for an image.
`hot_x` : | The X offset within `widget` of the hotspot.
`hot_y` : | The Y offset within `widget` of the hotspot.

### gtk_drag_source_set ()

    void        gtk_drag_source_set             (GtkWidget *widget,
                                                 GdkModifierType start_button_mask,
                                                 const GtkTargetEntry *targets,
                                                 gint n_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.

`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
`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 ()

    void        gtk_drag_source_set_icon        (GtkWidget *widget,
                                                 GdkColormap *colormap,
                                                 GdkPixmap *pixmap,
                                                 GdkBitmap *mask);

Sets the icon that will be used for drags from a particular widget. GTK+ retains a reference count for the arguments, and will release them when they are no longer needed.

`widget` : | a GtkWidget
`colormap` : | the colormap of the icon
`pixmap` : | the image data for the icon
`mask` : | the transparency mask for an image.

### gtk_drag_source_unset ()

    void        gtk_drag_source_unset           (GtkWidget *widget);

Undo the effects of gtk_drag_source_set().

`widget` : | a GtkWidget

### gtk_drag_source_handle_event ()

    void        gtk_drag_source_handle_event    (GtkWidget *widget,
                                                 GdkEvent *event);

Internal function.

`widget` : |
`event` : |

### gtk_drag_dest_handle_event ()

    void        gtk_drag_dest_handle_event      (GtkWidget *toplevel,
                                                 GdkEvent *event);

Internal function.

`toplevel` : |
`event` : |

<<< Previous Page | Home | Up | Next Page >>>
Selections | Signals