2017-12-06 00:22:06 +00:00
|
|
|
/* Drag-and-Drop
|
|
|
|
*
|
|
|
|
* I can't believe its not glade!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
typedef struct _GtkDemoWidget GtkDemoWidget;
|
2017-12-06 00:22:06 +00:00
|
|
|
struct _GtkDemoWidget
|
|
|
|
{
|
2017-12-07 04:10:56 +00:00
|
|
|
GType type;
|
|
|
|
union {
|
|
|
|
char *text;
|
|
|
|
gboolean active;
|
|
|
|
};
|
2017-12-06 00:22:06 +00:00
|
|
|
};
|
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
static gpointer
|
|
|
|
copy_demo_widget (gpointer data)
|
2017-12-06 00:22:06 +00:00
|
|
|
{
|
2017-12-07 04:10:56 +00:00
|
|
|
GtkDemoWidget *demo = g_memdup (data, sizeof (GtkDemoWidget));
|
|
|
|
|
|
|
|
if (demo->type == GTK_TYPE_LABEL)
|
|
|
|
demo->text = g_strdup (demo->text);
|
|
|
|
|
|
|
|
return demo;
|
2017-12-06 00:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-12-07 04:10:56 +00:00
|
|
|
free_demo_widget (gpointer data)
|
2017-12-06 00:22:06 +00:00
|
|
|
{
|
2017-12-07 04:10:56 +00:00
|
|
|
GtkDemoWidget *demo = data;
|
|
|
|
|
|
|
|
if (demo->type == GTK_TYPE_LABEL)
|
|
|
|
g_free (demo->text);
|
2017-12-06 00:22:06 +00:00
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
g_free (demo);
|
2017-12-06 00:22:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
#define GTK_TYPE_DEMO_WIDGET (gtk_demo_widget_get_type ())
|
|
|
|
|
|
|
|
G_DEFINE_BOXED_TYPE (GtkDemoWidget, gtk_demo_widget, copy_demo_widget, free_demo_widget)
|
|
|
|
|
|
|
|
static GtkDemoWidget *
|
|
|
|
serialize_widget (GtkWidget *widget)
|
2017-12-06 00:22:06 +00:00
|
|
|
{
|
2017-12-07 04:10:56 +00:00
|
|
|
GtkDemoWidget *demo;
|
|
|
|
|
|
|
|
demo = g_new0 (GtkDemoWidget, 1);
|
|
|
|
demo->type = G_OBJECT_TYPE (widget);
|
2017-12-06 00:22:06 +00:00
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
if (GTK_IS_LABEL (widget))
|
|
|
|
{
|
|
|
|
demo->text = g_strdup (gtk_label_get_text (GTK_LABEL (widget)));
|
|
|
|
}
|
|
|
|
else if (GTK_IS_SPINNER (widget))
|
|
|
|
{
|
|
|
|
g_object_get (widget, "active", &demo->active, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_print ("Type %s not supported\n", g_type_name (demo->type));
|
|
|
|
}
|
2017-12-06 00:22:06 +00:00
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
return demo;
|
2017-12-06 00:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
2017-12-07 04:10:56 +00:00
|
|
|
deserialize_widget (GtkDemoWidget *demo)
|
2017-12-06 00:22:06 +00:00
|
|
|
{
|
2017-12-07 04:10:56 +00:00
|
|
|
GtkWidget *widget = NULL;
|
|
|
|
|
|
|
|
if (demo->type == GTK_TYPE_LABEL)
|
|
|
|
{
|
|
|
|
widget = gtk_label_new (demo->text);
|
|
|
|
}
|
|
|
|
else if (demo->type == GTK_TYPE_SPINNER)
|
|
|
|
{
|
|
|
|
widget = g_object_new (demo->type, "active", demo->active, NULL);
|
2017-12-08 02:25:10 +00:00
|
|
|
gtk_style_context_add_class (gtk_widget_get_style_context (widget), "demo");
|
2017-12-07 04:10:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_print ("Type %s not supported\n", g_type_name (demo->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
return widget;
|
2017-12-06 00:22:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 01:45:39 +00:00
|
|
|
static double pos_x, pos_y;
|
|
|
|
|
2017-12-06 00:22:06 +00:00
|
|
|
static void
|
2017-12-06 01:45:39 +00:00
|
|
|
new_label_cb (GtkMenuItem *item,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkFixed *fixed = data;
|
|
|
|
GtkWidget *widget;
|
|
|
|
|
|
|
|
widget = gtk_label_new ("Label");
|
|
|
|
gtk_fixed_put (fixed, widget, pos_x, pos_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
new_spinner_cb (GtkMenuItem *item,
|
|
|
|
gpointer data)
|
2017-12-06 00:22:06 +00:00
|
|
|
{
|
2017-12-06 01:45:39 +00:00
|
|
|
GtkFixed *fixed = data;
|
2017-12-06 00:22:06 +00:00
|
|
|
GtkWidget *widget;
|
|
|
|
|
2017-12-06 01:45:39 +00:00
|
|
|
widget = gtk_spinner_new ();
|
2017-12-08 02:25:10 +00:00
|
|
|
gtk_style_context_add_class (gtk_widget_get_style_context (widget), "demo");
|
2017-12-06 01:45:39 +00:00
|
|
|
gtk_spinner_start (GTK_SPINNER (widget));
|
|
|
|
gtk_fixed_put (fixed, widget, pos_x, pos_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-12-07 04:36:35 +00:00
|
|
|
copy_cb (GtkWidget *child)
|
2017-12-06 01:45:39 +00:00
|
|
|
{
|
|
|
|
GdkClipboard *clipboard;
|
2017-12-07 04:10:56 +00:00
|
|
|
GtkDemoWidget *demo;
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
g_print ("Copy %s\n", G_OBJECT_TYPE_NAME (child));
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
demo = serialize_widget (child);
|
2017-12-06 01:45:39 +00:00
|
|
|
|
|
|
|
clipboard = gdk_display_get_clipboard (gdk_display_get_default ());
|
2017-12-07 04:10:56 +00:00
|
|
|
gdk_clipboard_set (clipboard, GTK_TYPE_DEMO_WIDGET, demo);
|
2017-12-06 01:45:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 04:36:35 +00:00
|
|
|
static void
|
|
|
|
delete_cb (GtkWidget *child)
|
|
|
|
{
|
|
|
|
gtk_widget_destroy (child);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cut_cb (GtkWidget *child)
|
|
|
|
{
|
|
|
|
copy_cb (child);
|
|
|
|
delete_cb (child);
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:45:39 +00:00
|
|
|
static void
|
|
|
|
value_read (GObject *source,
|
|
|
|
GAsyncResult *res,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GdkClipboard *clipboard = GDK_CLIPBOARD (source);
|
|
|
|
GError *error = NULL;
|
|
|
|
const GValue *value;
|
2017-12-07 04:10:56 +00:00
|
|
|
GtkDemoWidget *demo;
|
|
|
|
GtkWidget *widget = NULL;
|
2017-12-06 01:45:39 +00:00
|
|
|
|
|
|
|
value = gdk_clipboard_read_value_finish (clipboard, res, &error);
|
|
|
|
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
g_print ("error: %s\n", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
if (!G_VALUE_HOLDS (value, GTK_TYPE_DEMO_WIDGET))
|
2017-12-06 01:45:39 +00:00
|
|
|
{
|
2017-12-07 04:10:56 +00:00
|
|
|
g_print ("can't handle clipboard contents\n");
|
2017-12-06 01:45:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
demo = g_value_get_boxed (value);
|
|
|
|
widget = deserialize_widget (demo);
|
|
|
|
|
|
|
|
gtk_fixed_put (GTK_FIXED (data), widget, pos_x, pos_y);
|
2017-12-06 01:45:39 +00:00
|
|
|
}
|
2017-12-07 04:10:56 +00:00
|
|
|
|
2017-12-06 01:45:39 +00:00
|
|
|
static void
|
2017-12-07 04:36:35 +00:00
|
|
|
paste_cb (GtkWidget *fixed)
|
2017-12-06 01:45:39 +00:00
|
|
|
{
|
|
|
|
GdkClipboard *clipboard;
|
|
|
|
|
|
|
|
clipboard = gdk_display_get_clipboard (gdk_display_get_default ());
|
2017-12-07 04:10:56 +00:00
|
|
|
if (gdk_content_formats_contain_gtype (gdk_clipboard_get_formats (clipboard), GTK_TYPE_DEMO_WIDGET))
|
|
|
|
{
|
|
|
|
g_print ("Paste %s\n", g_type_name (GTK_TYPE_DEMO_WIDGET));
|
2017-12-07 04:36:35 +00:00
|
|
|
gdk_clipboard_read_value_async (clipboard, GTK_TYPE_DEMO_WIDGET, 0, NULL, value_read, fixed);
|
2017-12-07 04:10:56 +00:00
|
|
|
}
|
2017-12-06 01:45:39 +00:00
|
|
|
else
|
|
|
|
g_print ("Don't know how to handle clipboard contents\n");
|
|
|
|
}
|
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
static void
|
|
|
|
edit_label_done (GtkWidget *entry, gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *fixed = gtk_widget_get_parent (entry);
|
|
|
|
GtkWidget *label;
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
gtk_container_child_get (GTK_CONTAINER (fixed), entry, "x", &x, "y", &y, NULL);
|
|
|
|
|
|
|
|
label = GTK_WIDGET (g_object_get_data (G_OBJECT (entry), "label"));
|
2019-02-28 19:31:36 +00:00
|
|
|
gtk_label_set_text (GTK_LABEL (label), gtk_editable_get_text (GTK_EDITABLE (entry)));
|
2017-12-07 04:10:56 +00:00
|
|
|
|
|
|
|
gtk_widget_destroy (entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-12-07 04:36:35 +00:00
|
|
|
edit_cb (GtkWidget *child)
|
2017-12-07 04:10:56 +00:00
|
|
|
{
|
|
|
|
GtkWidget *fixed = gtk_widget_get_parent (child);
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
gtk_container_child_get (GTK_CONTAINER (fixed), child, "x", &x, "y", &y, NULL);
|
|
|
|
|
|
|
|
if (GTK_IS_LABEL (child))
|
|
|
|
{
|
|
|
|
GtkWidget *entry = gtk_entry_new ();
|
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (entry), "label", child);
|
|
|
|
|
2019-02-28 19:31:36 +00:00
|
|
|
gtk_editable_set_text (GTK_EDITABLE (entry), gtk_label_get_text (GTK_LABEL (child)));
|
2017-12-07 04:10:56 +00:00
|
|
|
g_signal_connect (entry, "activate", G_CALLBACK (edit_label_done), NULL);
|
|
|
|
gtk_fixed_put (GTK_FIXED (fixed), entry, x, y);
|
|
|
|
gtk_widget_grab_focus (entry);
|
|
|
|
}
|
|
|
|
else if (GTK_IS_SPINNER (child))
|
|
|
|
{
|
|
|
|
gboolean active;
|
|
|
|
|
|
|
|
g_object_get (child, "active", &active, NULL);
|
|
|
|
g_object_set (child, "active", !active, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:45:39 +00:00
|
|
|
static void
|
|
|
|
pressed_cb (GtkGesture *gesture,
|
|
|
|
int n_press,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2017-12-07 04:10:56 +00:00
|
|
|
GtkWidget *widget;
|
|
|
|
GtkWidget *child;
|
|
|
|
|
|
|
|
widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
child = gtk_widget_pick (widget, x, y);
|
|
|
|
|
2017-12-08 02:25:10 +00:00
|
|
|
if (gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture)) == GDK_BUTTON_SECONDARY)
|
2017-12-06 01:45:39 +00:00
|
|
|
{
|
|
|
|
GdkRectangle rect;
|
|
|
|
GtkWidget *menu;
|
|
|
|
GtkWidget *item;
|
|
|
|
GdkClipboard *clipboard;
|
|
|
|
|
|
|
|
pos_x = x;
|
|
|
|
pos_y = y;
|
|
|
|
|
|
|
|
menu = gtk_menu_new ();
|
|
|
|
item = gtk_menu_item_new_with_label ("New Label");
|
|
|
|
g_signal_connect (item, "activate", G_CALLBACK (new_label_cb), widget);
|
|
|
|
gtk_container_add (GTK_CONTAINER (menu), item);
|
|
|
|
item = gtk_menu_item_new_with_label ("New Spinner");
|
|
|
|
g_signal_connect (item, "activate", G_CALLBACK (new_spinner_cb), widget);
|
|
|
|
gtk_container_add (GTK_CONTAINER (menu), item);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
|
|
|
item = gtk_separator_menu_item_new ();
|
2017-12-06 01:45:39 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (menu), item);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
|
|
|
item = gtk_menu_item_new_with_label ("Edit");
|
|
|
|
gtk_widget_set_sensitive (item, child != NULL && child != widget);
|
2017-12-07 04:36:35 +00:00
|
|
|
g_signal_connect_swapped (item, "activate", G_CALLBACK (edit_cb), child);
|
2017-12-07 04:10:56 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (menu), item);
|
|
|
|
|
2017-12-06 01:45:39 +00:00
|
|
|
item = gtk_separator_menu_item_new ();
|
|
|
|
gtk_container_add (GTK_CONTAINER (menu), item);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
2017-12-07 04:36:35 +00:00
|
|
|
item = gtk_menu_item_new_with_label ("Cut");
|
|
|
|
gtk_widget_set_sensitive (item, child != NULL && child != widget);
|
|
|
|
g_signal_connect_swapped (item, "activate", G_CALLBACK (cut_cb), child);
|
|
|
|
gtk_container_add (GTK_CONTAINER (menu), item);
|
2017-12-06 01:45:39 +00:00
|
|
|
item = gtk_menu_item_new_with_label ("Copy");
|
|
|
|
gtk_widget_set_sensitive (item, child != NULL && child != widget);
|
2017-12-07 04:36:35 +00:00
|
|
|
g_signal_connect_swapped (item, "activate", G_CALLBACK (copy_cb), child);
|
2017-12-06 01:45:39 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (menu), item);
|
|
|
|
item = gtk_menu_item_new_with_label ("Paste");
|
|
|
|
clipboard = gdk_display_get_clipboard (gdk_display_get_default ());
|
2017-12-07 04:36:35 +00:00
|
|
|
gtk_widget_set_sensitive (item,
|
|
|
|
gdk_content_formats_contain_gtype (gdk_clipboard_get_formats (clipboard), GTK_TYPE_DEMO_WIDGET));
|
|
|
|
g_signal_connect_swapped (item, "activate", G_CALLBACK (paste_cb), widget);
|
|
|
|
gtk_container_add (GTK_CONTAINER (menu), item);
|
|
|
|
item = gtk_menu_item_new_with_label ("Delete");
|
|
|
|
gtk_widget_set_sensitive (item, child != NULL && child != widget);
|
|
|
|
g_signal_connect_swapped (item, "activate", G_CALLBACK (delete_cb), child);
|
2017-12-06 01:45:39 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (menu), item);
|
|
|
|
|
|
|
|
rect.x = x;
|
|
|
|
rect.y = y;
|
|
|
|
rect.width = 0;
|
|
|
|
rect.height = 0;
|
|
|
|
|
|
|
|
gtk_menu_popup_at_rect (GTK_MENU (menu),
|
2018-03-20 14:21:12 +00:00
|
|
|
gtk_widget_get_surface (widget),
|
2017-12-06 01:45:39 +00:00
|
|
|
&rect,
|
|
|
|
GDK_GRAVITY_NORTH_WEST,
|
|
|
|
GDK_GRAVITY_NORTH_WEST,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2017-12-06 00:22:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 02:25:10 +00:00
|
|
|
static void
|
|
|
|
released_cb (GtkGesture *gesture,
|
|
|
|
int n_press,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *widget;
|
|
|
|
GtkWidget *child;
|
|
|
|
|
|
|
|
widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
child = gtk_widget_pick (widget, x, y);
|
|
|
|
|
|
|
|
if (gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture)) == GDK_BUTTON_PRIMARY)
|
|
|
|
{
|
|
|
|
if (child != NULL && child != widget)
|
|
|
|
edit_cb (child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 00:22:06 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_dnd (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkWidget *vbox, *fixed;
|
|
|
|
GtkGesture *multipress;
|
|
|
|
GtkCssProvider *provider;
|
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Drag-and-drop");
|
|
|
|
|
|
|
|
g_signal_connect (window, "destroy",
|
|
|
|
G_CALLBACK (gtk_widget_destroyed), &window);
|
|
|
|
|
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
|
|
|
|
fixed = gtk_fixed_new ();
|
2018-12-29 08:49:24 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), fixed);
|
2017-12-06 00:22:06 +00:00
|
|
|
gtk_widget_set_hexpand (fixed, TRUE);
|
|
|
|
gtk_widget_set_vexpand (fixed, TRUE);
|
|
|
|
|
2018-03-09 05:14:59 +00:00
|
|
|
multipress = gtk_gesture_multi_press_new ();
|
2017-12-06 00:22:06 +00:00
|
|
|
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (multipress), 0);
|
|
|
|
g_signal_connect (multipress, "pressed", G_CALLBACK (pressed_cb), NULL);
|
2017-12-08 02:25:10 +00:00
|
|
|
g_signal_connect (multipress, "released", G_CALLBACK (released_cb), NULL);
|
2018-03-09 05:14:59 +00:00
|
|
|
gtk_widget_add_controller (fixed, GTK_EVENT_CONTROLLER (multipress));
|
2017-12-06 00:22:06 +00:00
|
|
|
|
|
|
|
provider = gtk_css_provider_new ();
|
|
|
|
gtk_css_provider_load_from_resource (provider, "/dnd/dnd.css");
|
|
|
|
gtk_style_context_add_provider_for_display (gdk_display_get_default (),
|
|
|
|
GTK_STYLE_PROVIDER (provider),
|
|
|
|
800);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
|
|
|
gtk_widget_show (window);
|
|
|
|
else
|
|
|
|
gtk_widget_destroy (window);
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|