gtk2/gtk/gtkdragsource.c

464 lines
14 KiB
C
Raw Normal View History

/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include "config.h"
#include "gtkdragsource.h"
#include "gtkdnd.h"
#include "gtkdndprivate.h"
#include "gtkgesturedrag.h"
#include "gtkimagedefinitionprivate.h"
#include "gtkintl.h"
typedef struct _GtkDragSourceSite GtkDragSourceSite;
struct _GtkDragSourceSite
{
GdkModifierType start_button_mask;
GtkTargetList *target_list; /* Targets for drag data */
GdkDragAction actions; /* Possible actions */
GtkImageDefinition *image_def;
GtkGesture *drag_gesture;
};
static void
gtk_drag_source_gesture_begin (GtkGesture *gesture,
GdkEventSequence *sequence,
gpointer data)
{
GtkDragSourceSite *site = data;
guint button;
if (gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture)))
button = 1;
else
button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));
g_assert (button >= 1);
if (!site->start_button_mask ||
!(site->start_button_mask & (GDK_BUTTON1_MASK << (button - 1))))
gtk_gesture_set_state (gesture, GTK_EVENT_SEQUENCE_DENIED);
}
static gboolean
gtk_drag_source_event_cb (GtkWidget *widget,
GdkEvent *event,
gpointer data)
{
gdouble start_x, start_y, offset_x, offset_y;
GtkDragSourceSite *site = data;
gtk_event_controller_handle_event (GTK_EVENT_CONTROLLER (site->drag_gesture), event);
if (gtk_gesture_is_recognized (site->drag_gesture))
{
gtk_gesture_drag_get_start_point (GTK_GESTURE_DRAG (site->drag_gesture),
&start_x, &start_y);
gtk_gesture_drag_get_offset (GTK_GESTURE_DRAG (site->drag_gesture),
&offset_x, &offset_y);
if (gtk_drag_check_threshold (widget, start_x, start_y,
start_x + offset_x, start_y + offset_y))
{
GdkEventSequence *sequence;
GdkEvent *last_event;
guint button;
gboolean needs_icon;
GdkDragContext *context;
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (site->drag_gesture));
last_event = gdk_event_copy (gtk_gesture_get_last_event (site->drag_gesture, sequence));
button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (site->drag_gesture));
gtk_event_controller_reset (GTK_EVENT_CONTROLLER (site->drag_gesture));
context = gtk_drag_begin_internal (widget, &needs_icon, site->target_list,
site->actions, button, last_event,
start_x, start_y);
if (context != NULL && needs_icon)
gtk_drag_set_icon_definition (context, site->image_def, 0, 0);
gdk_event_free (last_event);
return TRUE;
}
}
return FALSE;
}
static void
gtk_drag_source_site_destroy (gpointer data)
{
GtkDragSourceSite *site = data;
if (site->target_list)
gtk_target_list_unref (site->target_list);
gtk_image_definition_unref (site->image_def);
g_clear_object (&site->drag_gesture);
g_slice_free (GtkDragSourceSite, site);
}
/**
* gtk_drag_source_set: (method)
* @widget: a #GtkWidget
* @start_button_mask: the bitmask of buttons that can start the drag
* @targets: (allow-none) (array length=n_targets): the table of targets
* that the drag will support, may be %NULL
* @n_targets: the number of items in @targets
* @actions: the bitmask of possible actions for a drag from this widget
*
* 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.
*/
void
gtk_drag_source_set (GtkWidget *widget,
GdkModifierType start_button_mask,
const GtkTargetEntry *targets,
gint n_targets,
GdkDragAction actions)
{
GtkDragSourceSite *site;
g_return_if_fail (GTK_IS_WIDGET (widget));
site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
gtk_widget_add_events (widget,
gtk_widget_get_events (widget) |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_BUTTON_MOTION_MASK);
if (site)
{
if (site->target_list)
gtk_target_list_unref (site->target_list);
}
else
{
site = g_slice_new0 (GtkDragSourceSite);
site->image_def = gtk_image_definition_new_empty ();
site->drag_gesture = gtk_gesture_drag_new (widget);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (site->drag_gesture),
GTK_PHASE_NONE);
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (site->drag_gesture), 0);
g_signal_connect (site->drag_gesture, "begin",
G_CALLBACK (gtk_drag_source_gesture_begin),
site);
g_signal_connect (widget, "button-press-event",
G_CALLBACK (gtk_drag_source_event_cb),
site);
g_signal_connect (widget, "button-release-event",
G_CALLBACK (gtk_drag_source_event_cb),
site);
g_signal_connect (widget, "motion-notify-event",
G_CALLBACK (gtk_drag_source_event_cb),
site);
g_object_set_data_full (G_OBJECT (widget),
I_("gtk-site-data"),
site, gtk_drag_source_site_destroy);
}
site->start_button_mask = start_button_mask;
site->target_list = gtk_target_list_new (targets, n_targets);
site->actions = actions;
}
/**
* gtk_drag_source_unset: (method)
* @widget: a #GtkWidget
*
* Undoes the effects of gtk_drag_source_set().
*/
void
gtk_drag_source_unset (GtkWidget *widget)
{
GtkDragSourceSite *site;
g_return_if_fail (GTK_IS_WIDGET (widget));
site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
if (site)
{
g_signal_handlers_disconnect_by_func (widget,
gtk_drag_source_event_cb,
site);
g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
}
}
/**
* gtk_drag_source_get_target_list: (method)
* @widget: a #GtkWidget
*
* Gets the list of targets this widget can provide for
* drag-and-drop.
*
introspection: This patch fixes nullable return values fixes for the following symbols in gtk gtk_accel_group_query gtk_accel_group_from_accel_closure gtk_accel_label_get_accel_widget gtk_accessible_get_widget gtk_actionable_get_action_name gtk_app_chooser_get_app_info gtk_app_chooser_button_get_heading gtk_app_chooser_dialog_get_heading gtk_application_get_window_by_id gtk_assistant_get_nth_page gtk_binding_set_find gtk_builder_get_object gtk_builder_lookup_callback_symbol gtk_builder_get_application gtk_button_get_image gtk_cell_area_get_focus_from_sibling gtk_cell_renderer_start_editing gtk_cell_view_get_model gtk_cell_view_get_displayed_row gtk_clipboard_get_owner gtk_container_get_focus_child gtk_container_get_focus_vadjustment gtk_container_get_focus_hadjustment gtk_dialog_get_widget_for_response gtk_drag_get_source_widget gtk_drag_dest_get_target_list gtk_drag_source_get_target_list gtk_entry_completion_get_model gtk_entry_completion_compute_prefix gtk_expander_get_label_widget gtk_file_chooser_get_filename gtk_file_chooser_get_current_folder gtk_file_chooser_get_uri gtk_file_chooser_get_current_folder_uri gtk_file_chooser_get_preview_widget gtk_file_chooser_get_preview_file gtk_file_chooser_get_preview_filename gtk_file_chooser_get_preview_uri gtk_file_chooser_get_extra_widget gtk_file_chooser_get_filter gtk_file_chooser_native_get_accept_label gtk_file_chooser_native_get_cancel_label gtk_file_filter_get_name gtk_font_chooser_get_font_family gtk_font_chooser_get_font_face gtk_font_chooser_get_font gtk_font_chooser_get_font_desc gtk_font_chooser_get_font_map gtk_frame_get_label gtk_gesture_get_device gtk_gesture_get_window gtk_gl_area_get_error gtk_header_bar_get_title gtk_header_bar_get_subtitle gtk_header_bar_get_custom_title gtk_icon_info_get_filename gtk_icon_view_get_path_at_pos gtk_icon_view_get_model gtk_image_get_pixbuf gtk_image_get_animation gtk_label_get_mnemonic_widget gtk_label_get_attributes gtk_check_version gtk_menu_button_get_popup gtk_menu_button_get_menu_model gtk_menu_button_get_align_widget gtk_menu_button_get_popover gtk_menu_item_get_submenu gtk_menu_item_get_accel_path gtk_native_dialog_get_title gtk_native_dialog_get_transient_for gtk_notebook_get_nth_page gtk_notebook_get_tab_label_text gtk_notebook_get_menu_label gtk_notebook_get_menu_label_text gtk_notebook_get_group_name gtk_notebook_get_action_widget gtk_offscreen_window_get_surface gtk_offscreen_window_get_pixbuf gtk_paned_get_child1 gtk_paned_get_child2 gtk_places_sidebar_get_location gtk_places_sidebar_get_nth_bookmark gtk_plug_get_socket_window gtk_popover_get_default_widget gtk_progress_bar_get_text gtk_recent_filter_get_name gtk_recent_manager_lookup_item gtk_settings_get_default gtk_socket_get_plug_window gtk_stack_sidebar_get_stack gtk_stack_switcher_get_stack gtk_style_context_get_section gtk_style_context_get_parent gtk_style_context_get_frame_clock gtk_test_find_widget gtk_text_buffer_get_mark gtk_text_tag_table_lookup gtk_text_view_get_tabs gtk_text_view_toggle_cursor_visible gtk_text_view_get_window gtk_toolbar_get_nth_item gtk_tool_button_get_label gtk_tool_button_get_icon_name gtk_tool_button_get_label_widget gtk_tool_button_get_icon_widget gtk_tool_palette_get_drop_item gtk_tool_palette_get_drop_group gtk_tree_model_filter_convert_child_path_to_path gtk_tree_model_filter_convert_path_to_child_path gtk_tree_model_sort_convert_child_path_to_path gtk_tree_model_sort_convert_path_to_child_path gtk_tree_view_get_column gtk_tree_view_get_bin_window gtk_tree_view_column_get_widget gtk_tree_view_column_get_tree_view gtk_widget_get_frame_clock gtk_window_group_get_current_device_grab GtkTextBufferSerializeFunc
2015-12-28 20:14:08 +00:00
* Returns: (nullable) (transfer none): the #GtkTargetList, or %NULL if none
*
* Since: 2.4
*/
GtkTargetList *
gtk_drag_source_get_target_list (GtkWidget *widget)
{
GtkDragSourceSite *site;
g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
return site ? site->target_list : NULL;
}
/**
* gtk_drag_source_set_target_list: (method)
* @widget: a #GtkWidget thats a drag source
* @target_list: (allow-none): list of draggable targets, or %NULL for none
*
* 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().
*
* Since: 2.4
*/
void
gtk_drag_source_set_target_list (GtkWidget *widget,
GtkTargetList *target_list)
{
GtkDragSourceSite *site;
g_return_if_fail (GTK_IS_WIDGET (widget));
site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
if (site == NULL)
{
g_warning ("gtk_drag_source_set_target_list() requires the widget "
"to already be a drag source.");
return;
}
if (target_list)
gtk_target_list_ref (target_list);
if (site->target_list)
gtk_target_list_unref (site->target_list);
site->target_list = target_list;
}
/**
* gtk_drag_source_add_text_targets: (method)
* @widget: a #GtkWidget thats is a drag source
*
* 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_target_list_add_text_targets() and
* gtk_drag_source_set_target_list().
*
* Since: 2.6
*/
void
gtk_drag_source_add_text_targets (GtkWidget *widget)
{
GtkTargetList *target_list;
target_list = gtk_drag_source_get_target_list (widget);
if (target_list)
gtk_target_list_ref (target_list);
else
target_list = gtk_target_list_new (NULL, 0);
gtk_target_list_add_text_targets (target_list, 0);
gtk_drag_source_set_target_list (widget, target_list);
gtk_target_list_unref (target_list);
}
/**
* gtk_drag_source_add_image_targets: (method)
* @widget: a #GtkWidget thats is a drag source
*
* 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().
*
* Since: 2.6
*/
void
gtk_drag_source_add_image_targets (GtkWidget *widget)
{
GtkTargetList *target_list;
target_list = gtk_drag_source_get_target_list (widget);
if (target_list)
gtk_target_list_ref (target_list);
else
target_list = gtk_target_list_new (NULL, 0);
gtk_target_list_add_image_targets (target_list, 0, TRUE);
gtk_drag_source_set_target_list (widget, target_list);
gtk_target_list_unref (target_list);
}
/**
* gtk_drag_source_add_uri_targets: (method)
* @widget: a #GtkWidget thats is a drag source
*
* 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_target_list_add_uri_targets() and
* gtk_drag_source_set_target_list().
*
* Since: 2.6
*/
void
gtk_drag_source_add_uri_targets (GtkWidget *widget)
{
GtkTargetList *target_list;
target_list = gtk_drag_source_get_target_list (widget);
if (target_list)
gtk_target_list_ref (target_list);
else
target_list = gtk_target_list_new (NULL, 0);
gtk_target_list_add_uri_targets (target_list, 0);
gtk_drag_source_set_target_list (widget, target_list);
gtk_target_list_unref (target_list);
}
/**
* gtk_drag_source_set_icon_pixbuf: (method)
* @widget: a #GtkWidget
* @pixbuf: the #GdkPixbuf for the drag icon
*
* Sets the icon that will be used for drags from a particular widget
* from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
* release it when it is no longer needed.
*/
void
gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
GdkPixbuf *pixbuf)
{
GtkDragSourceSite *site;
g_return_if_fail (GTK_IS_WIDGET (widget));
g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
g_return_if_fail (site != NULL);
g_clear_pointer (&site->image_def, gtk_image_definition_unref);
site->image_def = gtk_image_definition_new_pixbuf (pixbuf, 1);
}
/**
* gtk_drag_source_set_icon_stock: (method)
* @widget: a #GtkWidget
* @stock_id: the ID of the stock icon to use
*
* Sets the icon that will be used for drags from a particular source
* to a stock icon.
*
* Deprecated: 3.10: Use gtk_drag_source_set_icon_name() instead.
*/
void
gtk_drag_source_set_icon_stock (GtkWidget *widget,
const gchar *stock_id)
{
GtkDragSourceSite *site;
g_return_if_fail (GTK_IS_WIDGET (widget));
g_return_if_fail (stock_id != NULL);
site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
g_return_if_fail (site != NULL);
gtk_image_definition_unref (site->image_def);
site->image_def = gtk_image_definition_new_stock (stock_id);
}
/**
* gtk_drag_source_set_icon_name: (method)
* @widget: a #GtkWidget
* @icon_name: name of icon to use
*
* 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.
*
* Since: 2.8
*/
void
gtk_drag_source_set_icon_name (GtkWidget *widget,
const gchar *icon_name)
{
GtkDragSourceSite *site;
g_return_if_fail (GTK_IS_WIDGET (widget));
g_return_if_fail (icon_name != NULL);
site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
g_return_if_fail (site != NULL);
gtk_image_definition_unref (site->image_def);
site->image_def = gtk_image_definition_new_icon_name (icon_name);
}
/**
* gtk_drag_source_set_icon_gicon: (method)
* @widget: a #GtkWidget
* @icon: A #GIcon
*
* Sets the icon that will be used for drags from a particular source
* to @icon. See the docs for #GtkIconTheme for more details.
*
* Since: 3.2
*/
void
gtk_drag_source_set_icon_gicon (GtkWidget *widget,
GIcon *icon)
{
GtkDragSourceSite *site;
g_return_if_fail (GTK_IS_WIDGET (widget));
g_return_if_fail (icon != NULL);
site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
g_return_if_fail (site != NULL);
gtk_image_definition_unref (site->image_def);
site->image_def = gtk_image_definition_new_gicon (icon);
}