2015-06-05 12:33:59 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
typedef GtkListStore MyModel;
|
|
|
|
typedef GtkListStoreClass MyModelClass;
|
|
|
|
|
|
|
|
static void my_model_drag_source_init (GtkTreeDragSourceIface *iface);
|
|
|
|
|
2020-03-06 17:05:43 +00:00
|
|
|
static GType my_model_get_type (void);
|
2015-06-05 12:33:59 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (MyModel, my_model, GTK_TYPE_LIST_STORE,
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
|
|
|
|
my_model_drag_source_init))
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_model_class_init (MyModelClass *class)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_model_init (MyModel *object)
|
|
|
|
{
|
|
|
|
GType types[1] = { G_TYPE_STRING };
|
|
|
|
|
|
|
|
gtk_list_store_set_column_types (GTK_LIST_STORE (object), G_N_ELEMENTS (types), types);
|
|
|
|
}
|
|
|
|
|
2020-02-18 00:41:42 +00:00
|
|
|
static GdkContentProvider *
|
2015-06-05 12:33:59 +00:00
|
|
|
my_model_drag_data_get (GtkTreeDragSource *source,
|
2020-02-18 00:41:42 +00:00
|
|
|
GtkTreePath *path)
|
2015-06-05 12:33:59 +00:00
|
|
|
{
|
2020-02-18 00:41:42 +00:00
|
|
|
GdkContentProvider *content;
|
2015-06-05 12:33:59 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
gtk_tree_model_get_iter (GTK_TREE_MODEL (source), &iter, path);
|
|
|
|
gtk_tree_model_get (GTK_TREE_MODEL (source), &iter, 0, &text, -1);
|
2020-02-18 00:41:42 +00:00
|
|
|
content = gdk_content_provider_new_typed (G_TYPE_STRING, text);
|
2015-06-05 12:33:59 +00:00
|
|
|
g_free (text);
|
|
|
|
|
2020-02-18 00:41:42 +00:00
|
|
|
return content;
|
2015-06-05 12:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_model_drag_source_init (GtkTreeDragSourceIface *iface)
|
|
|
|
{
|
|
|
|
static GtkTreeDragSourceIface *parent;
|
|
|
|
|
|
|
|
parent = g_type_interface_peek_parent (iface);
|
|
|
|
|
|
|
|
iface->row_draggable = parent->row_draggable;
|
|
|
|
iface->drag_data_delete = parent->drag_data_delete;
|
|
|
|
iface->drag_data_get = my_model_drag_data_get;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkTreeModel *
|
|
|
|
get_model (void)
|
|
|
|
{
|
|
|
|
MyModel *model;
|
|
|
|
|
|
|
|
model = g_object_new (my_model_get_type (), NULL);
|
|
|
|
gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1, 0, "Item 1", -1);
|
|
|
|
gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1, 0, "Item 2", -1);
|
|
|
|
gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1, 0, "Item 3", -1);
|
|
|
|
|
|
|
|
return GTK_TREE_MODEL (model);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
get_dragsource (void)
|
|
|
|
{
|
|
|
|
GtkTreeView *tv;
|
|
|
|
GtkCellRenderer *renderer;
|
|
|
|
GtkTreeViewColumn *column;
|
2017-11-18 01:19:53 +00:00
|
|
|
GdkContentFormats *targets;
|
2015-06-05 12:33:59 +00:00
|
|
|
|
|
|
|
tv = (GtkTreeView*) gtk_tree_view_new ();
|
|
|
|
renderer = gtk_cell_renderer_text_new ();
|
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Text", renderer, "text", 0, NULL);
|
|
|
|
gtk_tree_view_append_column (tv, column);
|
|
|
|
|
|
|
|
gtk_tree_view_set_model (tv, get_model ());
|
2020-02-18 00:41:42 +00:00
|
|
|
targets = gdk_content_formats_new_for_gtype (G_TYPE_STRING);
|
2017-11-14 21:32:23 +00:00
|
|
|
gtk_tree_view_enable_model_drag_source (tv, GDK_BUTTON1_MASK, targets, GDK_ACTION_COPY);
|
2017-11-18 01:19:53 +00:00
|
|
|
gdk_content_formats_unref (targets);
|
2015-06-05 12:33:59 +00:00
|
|
|
|
|
|
|
return GTK_WIDGET (tv);
|
|
|
|
}
|
|
|
|
|
2020-01-03 06:06:28 +00:00
|
|
|
static void
|
|
|
|
drag_drop (GtkDropTarget *dest,
|
2020-02-29 02:47:17 +00:00
|
|
|
const GValue *value,
|
2020-01-03 06:06:28 +00:00
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
gpointer dada)
|
|
|
|
{
|
2020-02-18 00:41:42 +00:00
|
|
|
GtkWidget *widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (dest));
|
|
|
|
|
2020-02-29 02:47:17 +00:00
|
|
|
gtk_label_set_label (GTK_LABEL (widget), g_value_get_string (value));
|
2015-06-05 12:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
get_droptarget (void)
|
|
|
|
{
|
|
|
|
GtkWidget *label;
|
2020-01-02 13:53:33 +00:00
|
|
|
GtkDropTarget *dest;
|
2015-06-05 12:33:59 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("Drop here");
|
2020-02-29 02:47:17 +00:00
|
|
|
dest = gtk_drop_target_new (G_TYPE_STRING, GDK_ACTION_COPY);
|
|
|
|
g_signal_connect (dest, "drop", G_CALLBACK (drag_drop), NULL);
|
2020-01-06 05:12:21 +00:00
|
|
|
gtk_widget_add_controller (label, GTK_EVENT_CONTROLLER (dest));
|
2015-06-05 12:33:59 +00:00
|
|
|
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *box;
|
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2015-06-05 12:33:59 +00:00
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2015-06-05 12:33:59 +00:00
|
|
|
|
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), box);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), get_dragsource ());
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), get_droptarget ());
|
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2015-06-05 12:33:59 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (TRUE)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2015-06-05 12:33:59 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|