Commit e8b830a3 authored by Matthias Clasen's avatar Matthias Clasen
Browse files

dragsource: Reshuffle api a bit

Remove arguments from the constructor.

For actions, we now default to COPY, which is the most common one
that we should enable by default (MOVE requires handling deletion
on the the source side, and ASK only makes sense if we have
multiple actions).

For the content provider, we add a new ::prepare signal where
it should be provided just-in-time.
parent 38974d7d
......@@ -143,6 +143,19 @@ get_texture (GValue *value,
g_value_set_object (value, paintable);
}
static void
prepare_drag (GtkDragSource *source,
double x,
double y,
GtkWidget *image)
{
GdkContentProvider *content;
content = gdk_content_provider_new_with_callback (GDK_TYPE_TEXTURE, get_texture, image);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
}
static void
got_texture (GObject *source,
GAsyncResult *result,
......@@ -265,7 +278,6 @@ do_clipboard (GtkWidget *do_widget)
{ "paste", paste_image, NULL, NULL, NULL },
};
GActionGroup *actions;
GdkContentProvider *content = NULL;
GtkDragSource *source;
GtkDropTarget *dest;
GdkContentFormats *formats;
......@@ -331,10 +343,9 @@ do_clipboard (GtkWidget *do_widget)
gtk_container_add (GTK_CONTAINER (hbox), image);
/* make image a drag source */
content = gdk_content_provider_new_with_callback (GDK_TYPE_TEXTURE, get_texture, image);
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
g_object_unref (content);
source = gtk_drag_source_new ();
gtk_drag_source_attach (source, image, GDK_BUTTON1_MASK);
g_signal_connect (source, "prepare", G_CALLBACK (prepare_drag), NULL);
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
/* accept drops on image */
......@@ -364,9 +375,8 @@ do_clipboard (GtkWidget *do_widget)
gtk_container_add (GTK_CONTAINER (hbox), image);
/* make image a drag source */
content = gdk_content_provider_new_with_callback (GDK_TYPE_TEXTURE, get_texture, image);
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
g_object_unref (content);
source = gtk_drag_source_new ();
g_signal_connect (source, "prepare", G_CALLBACK (prepare_drag), NULL);
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
gtk_drag_source_attach (source, image, GDK_BUTTON1_MASK);
......
......@@ -423,8 +423,9 @@ setup_image_dnd (GtkWidget *image)
GdkContentProvider *content;
GtkDragSource *source;
source = gtk_drag_source_new ();
content = gdk_content_provider_new_with_callback (GDK_TYPE_TEXTURE, get_texture, image);
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
gtk_drag_source_attach (source, image, GDK_BUTTON1_MASK);
......@@ -436,8 +437,9 @@ setup_scalable_image_dnd (GtkWidget *image)
GdkContentProvider *content;
GtkDragSource *source;
source = gtk_drag_source_new ();
content = gdk_content_provider_new_with_callback (G_TYPE_FILE, get_file, image);
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
......
......@@ -2709,8 +2709,9 @@ gtk_calendar_drag_update (GtkGestureDrag *gesture,
gtk_gesture_drag_get_start_point (gesture, &start_x, &start_y);
source = gtk_drag_source_new ();
content = get_calendar_content (calendar);
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
device = gtk_gesture_get_device (GTK_GESTURE (gesture));
gtk_drag_source_drag_begin (source, widget, device, start_x, start_y);
......
......@@ -333,8 +333,9 @@ gtk_color_button_init (GtkColorButton *button)
gtk_widget_add_controller (GTK_WIDGET (button), GTK_EVENT_CONTROLLER (dest));
gdk_content_formats_unref (targets);
source = gtk_drag_source_new ();
content = gdk_content_provider_new_with_callback (GDK_TYPE_RGBA, get_rgba_value, button);
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
g_signal_connect (source, "drag-begin", G_CALLBACK (gtk_color_button_drag_begin), button);
......
......@@ -601,8 +601,9 @@ gtk_color_swatch_set_rgba (GtkColorSwatch *swatch,
GdkContentProvider *content;
GtkDragSource *source;
source = gtk_drag_source_new ();
content = gdk_content_provider_new_with_callback (GDK_TYPE_RGBA, get_rgba_value, swatch);
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
g_signal_connect (source, "drag-begin", G_CALLBACK (gtk_color_swatch_drag_begin), swatch);
......
......@@ -107,6 +107,7 @@ enum {
static GParamSpec *properties[NUM_PROPERTIES];
enum {
PREPARE,
DRAG_BEGIN,
DRAG_END,
DRAG_FAILED,
......@@ -115,11 +116,21 @@ enum {
static guint signals[NUM_SIGNALS];
static void gtk_drag_source_dnd_finished_cb (GdkDrag *drag,
GtkDragSource *source);
static void gtk_drag_source_drop_performed_cb (GdkDrag *drag,
GtkDragSource *source);
static void gtk_drag_source_cancel_cb (GdkDrag *drag,
GdkDragCancelReason reason,
GtkDragSource *source);
G_DEFINE_TYPE (GtkDragSource, gtk_drag_source, G_TYPE_OBJECT);
static void
gtk_drag_source_init (GtkDragSource *source)
{
source->actions = GDK_ACTION_COPY;
}
static void
......@@ -216,11 +227,21 @@ gtk_drag_source_class_init (GtkDragSourceClass *class)
g_param_spec_flags ("actions",
P_("Actions"),
P_("Supported actions"),
GDK_TYPE_DRAG_ACTION, 0,
GDK_TYPE_DRAG_ACTION, GDK_ACTION_COPY,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
signals[PREPARE] =
g_signal_new (I_("prepare"),
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
NULL,
G_TYPE_NONE, 2,
G_TYPE_DOUBLE, G_TYPE_DOUBLE);
/**
* GtkDragSource::drag-begin:
* @source: the #GtkDragSource
......@@ -287,14 +308,6 @@ gtk_drag_source_class_init (GtkDragSourceClass *class)
GDK_TYPE_DRAG_CANCEL_REASON);
}
static void gtk_drag_source_dnd_finished_cb (GdkDrag *drag,
GtkDragSource *source);
static void gtk_drag_source_drop_performed_cb (GdkDrag *drag,
GtkDragSource *source);
static void gtk_drag_source_cancel_cb (GdkDrag *drag,
GdkDragCancelReason reason,
GtkDragSource *source);
static void
drag_end (GtkDragSource *source,
gboolean success)
......@@ -390,7 +403,13 @@ gtk_drag_source_drag_begin (GtkDragSource *source,
dx = round (px) - x;
dy = round (py) - y;
g_signal_emit (source, signals[PREPARE], 0, x, y);
if (source->content == NULL || source->actions == 0)
return;
source->drag = gdk_drag_begin (surface, device, source->content, source->actions, dx, dy);
if (source->drag == NULL)
{
g_print ("no drag :(\n");
......@@ -439,21 +458,15 @@ gtk_drag_source_drag_begin (GtkDragSource *source,
/**
* gtk_drag_source_new:
* @content: (nullable): the #GdkContentProvider to use, or %NULL
* @actions: the actions to offer
*
* Creates a new #GtkDragSource object.
*
* Returns: the new #GtkDragSource
*/
GtkDragSource *
gtk_drag_source_new (GdkContentProvider *content,
GdkDragAction actions)
gtk_drag_source_new (void)
{
return g_object_new (GTK_TYPE_DRAG_SOURCE,
"content", content,
"actions", actions,
NULL);
return g_object_new (GTK_TYPE_DRAG_SOURCE, NULL);
}
/**
......
......@@ -51,8 +51,7 @@ GDK_AVAILABLE_IN_ALL
GType gtk_drag_source_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_ALL
GtkDragSource *gtk_drag_source_new (GdkContentProvider *content,
GdkDragAction actions);
GtkDragSource *gtk_drag_source_new (void);
GDK_AVAILABLE_IN_ALL
void gtk_drag_source_set_content (GtkDragSource *source,
......
......@@ -1469,7 +1469,9 @@ icon_drag_update_cb (GtkGestureDrag *gesture,
GdkDevice *device;
icon_info->in_drag = TRUE;
source = gtk_drag_source_new (icon_info->content, icon_info->actions);
source = gtk_drag_source_new ();
gtk_drag_source_set_content (source, icon_info->content);
gtk_drag_source_set_actions (source, icon_info->actions);
paintable = gtk_widget_paintable_new (icon_info->widget);
gtk_drag_source_set_icon (source, paintable, -2, -2);
g_object_unref (paintable);
......
......@@ -6465,8 +6465,10 @@ gtk_icon_view_enable_model_drag_source (GtkIconView *icon_view,
g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
icon_view->priv->source = gtk_drag_source_new ();
content = gdk_content_provider_new_with_formats (formats, gtk_icon_view_drag_data_get, icon_view);
icon_view->priv->source = gtk_drag_source_new (content, actions);
gtk_drag_source_set_content (icon_view->priv->source, content);
gtk_drag_source_set_actions (icon_view->priv->source, actions);
g_object_unref (content);
g_signal_connect (icon_view->priv->source, "drag-begin",
......
......@@ -4720,7 +4720,8 @@ gtk_label_drag_gesture_update (GtkGestureDrag *gesture,
GdkPaintable *paintable;
GdkDevice *device;
source = gtk_drag_source_new (info->provider, GDK_ACTION_COPY);
source = gtk_drag_source_new ();
gtk_drag_source_set_content (source, info->provider);
paintable = get_selection_paintable (label);
gtk_drag_source_set_icon (source, paintable, 9, 0);
g_clear_object (&paintable);
......
......@@ -332,9 +332,10 @@ gtk_link_button_init (GtkLinkButton *link_button)
g_signal_connect (link_button, "query-tooltip",
G_CALLBACK (gtk_link_button_query_tooltip_cb), NULL);
source = gtk_drag_source_new ();
content = g_object_new (GTK_TYPE_LINK_CONTENT, NULL);
GTK_LINK_CONTENT (content)->link = link_button;
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
gtk_drag_source_attach (source, GTK_WIDGET (link_button), GDK_BUTTON1_MASK);
......
......@@ -2877,9 +2877,11 @@ gtk_notebook_motion (GtkEventController *controller,
priv->detached_tab = priv->cur_page;
source = gtk_drag_source_new ();
content = gdk_content_provider_new_with_formats (priv->source_targets, gtk_notebook_drag_data_get, widget);
source = gtk_drag_source_new (content, GDK_ACTION_MOVE);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
gtk_drag_source_set_actions (source, GDK_ACTION_MOVE);
g_signal_connect (source, "drag-begin", G_CALLBACK (gtk_notebook_drag_begin), notebook);
g_signal_connect (source, "drag-end", G_CALLBACK (gtk_notebook_drag_end), notebook);
......
......@@ -1366,10 +1366,11 @@ make_directory_button (GtkPathBar *path_bar,
g_value_init (&value, G_TYPE_FILE);
g_value_set_object (&value, button_data->file);
source = gtk_drag_source_new ();
content = gdk_content_provider_new_for_value (&value);
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
gtk_drag_source_attach (source, button_data->button, GDK_BUTTON1_MASK);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
gtk_drag_source_attach (source, button_data->button, GDK_BUTTON1_MASK);
g_value_unset (&value);
return button_data;
......
......@@ -3793,11 +3793,13 @@ on_row_dragged (GtkGestureDrag *gesture,
sidebar->dragging_over = TRUE;
source = gtk_drag_source_new ();
content = gdk_content_provider_new_with_formats (sidebar->source_targets,
drag_data_get_callback,
sidebar);
source = gtk_drag_source_new (content, GDK_ACTION_MOVE);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
gtk_drag_source_set_actions (source, GDK_ACTION_MOVE);
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin_callback), sidebar);
g_signal_connect (source, "drag-end", G_CALLBACK (drag_end_callback), sidebar);
......
......@@ -2846,9 +2846,10 @@ gtk_text_drag_gesture_update (GtkGestureDrag *gesture,
paintable = gtk_text_util_create_drag_icon (widget, text, -1);
source = gtk_drag_source_new (priv->selection_content,
priv->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE
: GDK_ACTION_COPY);
source = gtk_drag_source_new ();
gtk_drag_source_set_content (source, priv->selection_content);
if (priv->editable)
gtk_drag_source_set_actions (source, GDK_ACTION_COPY|GDK_ACTION_MOVE);
gtk_drag_source_set_icon (source,
paintable,
priv->drag_start_x - ranges[0],
......
......@@ -7697,7 +7697,9 @@ gtk_text_view_start_selection_dnd (GtkTextView *text_view,
else
actions = GDK_ACTION_COPY;
content = gtk_text_buffer_get_selection_content (buffer);
source = gtk_drag_source_new (content, actions);
source = gtk_drag_source_new ();
gtk_drag_source_set_content (source, content);
gtk_drag_source_set_actions (source, actions);
g_object_unref (content);
if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
{
......
......@@ -12929,7 +12929,9 @@ gtk_tree_view_enable_model_drag_source (GtkTreeView *tree_view,
di = ensure_info (tree_view);
content = gdk_content_provider_new_with_formats (formats, gtk_tree_view_drag_data_get, tree_view);
di->source = gtk_drag_source_new (content, actions);
di->source = gtk_drag_source_new ();
gtk_drag_source_set_content (di->source, content);
gtk_drag_source_set_actions (di->source, actions);
g_object_unref (content);
g_signal_connect (di->source, "drag-begin", G_CALLBACK (gtk_tree_view_drag_begin), tree_view);
g_signal_connect (di->source, "drag-end", G_CALLBACK (gtk_tree_view_drag_end), tree_view);
......
......@@ -634,11 +634,14 @@ main (int argc, char **argv)
button = gtk_button_new_with_label ("Drag Here\n");
source = gtk_drag_source_new ();
g_value_init (&value, G_TYPE_STRING);
g_value_set_string (&value, "I'm data!");
content = gdk_content_provider_new_for_value (&value);
g_value_unset (&value);
source = gtk_drag_source_new (content, GDK_ACTION_COPY | GDK_ACTION_MOVE);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
gtk_drag_source_set_actions (source, GDK_ACTION_COPY|GDK_ACTION_MOVE);
gtk_drag_source_attach (source, button, GDK_BUTTON1_MASK | GDK_BUTTON3_MASK);
gtk_drag_source_set_icon (source, GDK_PAINTABLE (texture), 0, 0);
......
......@@ -298,7 +298,9 @@ make_image (const gchar *icon_name, int hotspot)
formats = gtk_content_formats_add_text_targets (formats);
content = gdk_content_provider_new_with_formats (formats, get_data, image);
source = gtk_drag_source_new (content, GDK_ACTION_COPY|GDK_ACTION_MOVE|GDK_ACTION_ASK);
source = gtk_drag_source_new ();
gtk_drag_source_set_content (source, content);
gtk_drag_source_set_actions (source, GDK_ACTION_COPY|GDK_ACTION_MOVE|GDK_ACTION_ASK);
g_object_unref (content);
update_source_icon (source, icon_name, hotspot);
......@@ -342,7 +344,8 @@ make_spinner (void)
g_value_init (&value, G_TYPE_STRING);
g_value_set_string (&value, "ACTIVE");
content = gdk_content_provider_new_for_value (&value);
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
source = gtk_drag_source_new ();
gtk_drag_source_set_content (source, content);
g_signal_connect (source, "drag-begin", G_CALLBACK (spinner_drag_begin), spinner);
gtk_drag_source_attach (source, spinner, GDK_BUTTON1_MASK);
......
......@@ -84,7 +84,9 @@ create_row (const gchar *text)
bytes = g_bytes_new (&row, sizeof (gpointer));
content = gdk_content_provider_new_for_bytes ("GTK_LIST_BOX_ROW", bytes);
source = gtk_drag_source_new (content, GDK_ACTION_MOVE);
source = gtk_drag_source_new ();
gtk_drag_source_set_content (source, content);
gtk_drag_source_set_actions (source, GDK_ACTION_MOVE);
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
gtk_drag_source_attach (source, image, GDK_BUTTON1_MASK);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment