2017-12-06 00:22:06 +00:00
|
|
|
/* Drag-and-Drop
|
|
|
|
*
|
2020-04-26 02:14:25 +00:00
|
|
|
* This demo shows dragging colors and widgets.
|
|
|
|
* The items in this demo can be moved, recolored
|
|
|
|
* and rotated.
|
2017-12-06 00:22:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
static GdkContentProvider *
|
|
|
|
prepare (GtkDragSource *source,
|
|
|
|
double x,
|
|
|
|
double y)
|
2017-12-06 00:22:06 +00:00
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *canvas;
|
|
|
|
GtkWidget *item;
|
2017-12-06 00:22:06 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
canvas = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (source));
|
|
|
|
item = gtk_widget_pick (canvas, x, y, GTK_PICK_DEFAULT);
|
2017-12-06 00:22:06 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
if (!GTK_IS_LABEL (item))
|
|
|
|
return NULL;
|
2017-12-07 04:10:56 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
g_object_set_data (G_OBJECT (canvas), "dragged-item", item);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
return gdk_content_provider_new_typed (GTK_TYPE_WIDGET, item);
|
2017-12-06 00:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-04-26 02:14:25 +00:00
|
|
|
drag_begin (GtkDragSource *source,
|
|
|
|
GdkDrag *drag)
|
2017-12-06 01:45:39 +00:00
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *canvas;
|
|
|
|
GtkWidget *item;
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
canvas = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (source));
|
|
|
|
item = g_object_get_data (G_OBJECT (canvas), "dragged-item");
|
2019-12-27 03:30:23 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
gtk_widget_set_opacity (item, 0.5);
|
2017-12-06 01:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-04-26 02:14:25 +00:00
|
|
|
drag_end (GtkDragSource *source,
|
|
|
|
GdkDrag *drag)
|
2017-12-06 00:22:06 +00:00
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *canvas;
|
|
|
|
GtkWidget *item;
|
2017-12-06 00:22:06 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
canvas = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (source));
|
|
|
|
item = g_object_get_data (G_OBJECT (canvas), "dragged-item");
|
|
|
|
g_object_set_data (G_OBJECT (canvas), "dragged-item", NULL);
|
2019-12-27 03:30:23 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
gtk_widget_set_opacity (item, 1.0);
|
2017-12-06 01:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-04-26 02:14:25 +00:00
|
|
|
drag_cancel (GtkDragSource *source,
|
|
|
|
GdkDrag *drag,
|
|
|
|
GdkDragCancelReason reason)
|
2017-12-06 01:45:39 +00:00
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
drag_end (source, drag);
|
2017-12-06 01:45:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
typedef struct {
|
|
|
|
double x, y;
|
|
|
|
double angle;
|
|
|
|
double delta;
|
|
|
|
} TransformData;
|
2017-12-07 04:36:35 +00:00
|
|
|
|
|
|
|
static void
|
2020-04-26 02:14:25 +00:00
|
|
|
apply_transform (GtkWidget *item)
|
2017-12-07 04:36:35 +00:00
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *canvas = gtk_widget_get_parent (item);
|
|
|
|
TransformData *data;
|
|
|
|
GskTransform *transform;
|
|
|
|
|
|
|
|
data = g_object_get_data (G_OBJECT (item), "transform-data");
|
|
|
|
transform = gsk_transform_rotate (gsk_transform_translate (NULL, &(graphene_point_t){data->x, data->y}),
|
|
|
|
data->angle + data->delta);
|
|
|
|
gtk_fixed_set_child_transform (GTK_FIXED (canvas), item, transform);
|
|
|
|
gsk_transform_unref (transform);
|
2017-12-07 04:36:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
static gboolean
|
|
|
|
drag_drop (GtkDropTarget *target,
|
|
|
|
const GValue *value,
|
|
|
|
double x,
|
|
|
|
double y)
|
2017-12-06 01:45:39 +00:00
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *item;
|
|
|
|
TransformData *transform_data;
|
|
|
|
GtkWidget *canvas;
|
|
|
|
GtkWidget *last_child;
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
item = g_value_get_object (value);
|
|
|
|
transform_data = g_object_get_data (G_OBJECT (item), "transform-data");
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
transform_data->x = x;
|
|
|
|
transform_data->y = y;
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
canvas = gtk_widget_get_parent (item);
|
|
|
|
last_child = gtk_widget_get_last_child (canvas);
|
|
|
|
if (item != last_child)
|
|
|
|
gtk_widget_insert_after (item, canvas, last_child);
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
apply_transform (item);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
return TRUE;
|
2017-12-06 01:45:39 +00:00
|
|
|
}
|
2017-12-07 04:10:56 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
static double pos_x, pos_y;
|
|
|
|
|
|
|
|
static GtkWidget * canvas_item_new (double x, double y);
|
|
|
|
|
2017-12-06 01:45:39 +00:00
|
|
|
static void
|
2020-04-26 02:14:25 +00:00
|
|
|
new_item_cb (GtkWidget *button, gpointer data)
|
2017-12-06 01:45:39 +00:00
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *canvas = data;
|
|
|
|
GtkWidget *item;
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
item = canvas_item_new (pos_x, pos_y);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_fixed_put (GTK_FIXED (canvas), item, 0, 0);
|
2020-04-26 02:14:25 +00:00
|
|
|
apply_transform (item);
|
2019-12-27 03:30:23 +00:00
|
|
|
|
|
|
|
gtk_popover_popdown (GTK_POPOVER (gtk_widget_get_ancestor (button, GTK_TYPE_POPOVER)));
|
2017-12-06 01:45:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 04:10:56 +00:00
|
|
|
static void
|
|
|
|
edit_label_done (GtkWidget *entry, gpointer data)
|
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *canvas = gtk_widget_get_parent (entry);
|
2017-12-07 04:10:56 +00:00
|
|
|
GtkWidget *label;
|
|
|
|
int x, y;
|
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
gtk_fixed_get_child_position (GTK_FIXED (canvas), entry, &x, &y);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
|
|
|
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)));
|
2020-04-26 02:14:25 +00:00
|
|
|
gtk_widget_show (label);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
2020-05-07 19:09:28 +00:00
|
|
|
gtk_fixed_remove (GTK_FIXED (canvas), entry);
|
2017-12-07 04:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-12-27 03:30:23 +00:00
|
|
|
edit_cb (GtkWidget *button, GtkWidget *child)
|
2017-12-07 04:10:56 +00:00
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *canvas = gtk_widget_get_parent (child);
|
2017-12-07 04:10:56 +00:00
|
|
|
int x, y;
|
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
gtk_fixed_get_child_position (GTK_FIXED (canvas), child, &x, &y);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
|
|
|
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)));
|
2019-12-24 03:14:52 +00:00
|
|
|
gtk_editable_set_width_chars (GTK_EDITABLE (entry), 12);
|
2017-12-07 04:10:56 +00:00
|
|
|
g_signal_connect (entry, "activate", G_CALLBACK (edit_label_done), NULL);
|
2020-04-26 02:14:25 +00:00
|
|
|
gtk_fixed_put (GTK_FIXED (canvas), entry, x, y);
|
2017-12-07 04:10:56 +00:00
|
|
|
gtk_widget_grab_focus (entry);
|
2020-04-26 02:14:25 +00:00
|
|
|
gtk_widget_hide (child);
|
2017-12-07 04:10:56 +00:00
|
|
|
}
|
2019-12-27 03:30:23 +00:00
|
|
|
|
|
|
|
if (button)
|
|
|
|
gtk_popover_popdown (GTK_POPOVER (gtk_widget_get_ancestor (button, GTK_TYPE_POPOVER)));
|
2017-12-07 04:10:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
static void
|
|
|
|
delete_cb (GtkWidget *button, GtkWidget *child)
|
|
|
|
{
|
2020-05-09 14:33:02 +00:00
|
|
|
GtkWidget *canvas = gtk_widget_get_parent (child);
|
|
|
|
|
2020-05-07 19:09:28 +00:00
|
|
|
gtk_fixed_remove (GTK_FIXED (canvas), child);
|
2020-04-26 02:14:25 +00:00
|
|
|
|
|
|
|
gtk_popover_popdown (GTK_POPOVER (gtk_widget_get_ancestor (button, GTK_TYPE_POPOVER)));
|
|
|
|
}
|
|
|
|
|
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));
|
2019-04-07 21:36:40 +00:00
|
|
|
child = gtk_widget_pick (widget, x, y, GTK_PICK_DEFAULT);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
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
|
|
|
{
|
|
|
|
GtkWidget *menu;
|
2019-12-27 03:30:23 +00:00
|
|
|
GtkWidget *box;
|
2017-12-06 01:45:39 +00:00
|
|
|
GtkWidget *item;
|
|
|
|
|
|
|
|
pos_x = x;
|
|
|
|
pos_y = y;
|
|
|
|
|
2020-02-24 12:21:46 +00:00
|
|
|
menu = gtk_popover_new ();
|
|
|
|
gtk_widget_set_parent (menu, widget);
|
2019-12-27 03:30:23 +00:00
|
|
|
gtk_popover_set_has_arrow (GTK_POPOVER (menu), FALSE);
|
|
|
|
gtk_popover_set_pointing_to (GTK_POPOVER (menu), &(GdkRectangle){ x, y, 1, 1});
|
|
|
|
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (menu), box);
|
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
item = gtk_button_new_with_label ("New");
|
2020-04-17 04:20:09 +00:00
|
|
|
gtk_button_set_has_frame (GTK_BUTTON (item), FALSE);
|
2020-04-26 02:14:25 +00:00
|
|
|
g_signal_connect (item, "clicked", G_CALLBACK (new_item_cb), widget);
|
2019-12-27 03:30:23 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (box), item);
|
|
|
|
|
|
|
|
item = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), item);
|
|
|
|
|
|
|
|
item = gtk_button_new_with_label ("Edit");
|
2020-04-17 04:20:09 +00:00
|
|
|
gtk_button_set_has_frame (GTK_BUTTON (item), FALSE);
|
2017-12-07 04:10:56 +00:00
|
|
|
gtk_widget_set_sensitive (item, child != NULL && child != widget);
|
2019-12-27 03:30:23 +00:00
|
|
|
g_signal_connect (item, "clicked", G_CALLBACK (edit_cb), child);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), item);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
2019-12-27 03:30:23 +00:00
|
|
|
item = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), item);
|
2017-12-07 04:10:56 +00:00
|
|
|
|
2019-12-27 03:30:23 +00:00
|
|
|
item = gtk_button_new_with_label ("Delete");
|
2020-04-17 04:20:09 +00:00
|
|
|
gtk_button_set_has_frame (GTK_BUTTON (item), FALSE);
|
2017-12-07 04:36:35 +00:00
|
|
|
gtk_widget_set_sensitive (item, child != NULL && child != widget);
|
2019-12-27 03:30:23 +00:00
|
|
|
g_signal_connect (item, "clicked", G_CALLBACK (delete_cb), child);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), item);
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2019-12-27 03:30:23 +00:00
|
|
|
gtk_popover_popup (GTK_POPOVER (menu));
|
2017-12-06 01:45:39 +00:00
|
|
|
}
|
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));
|
2019-04-07 17:19:09 +00:00
|
|
|
child = gtk_widget_pick (widget, x, y, 0);
|
2017-12-08 02:25:10 +00:00
|
|
|
|
|
|
|
if (gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture)) == GDK_BUTTON_PRIMARY)
|
|
|
|
{
|
|
|
|
if (child != NULL && child != widget)
|
2019-12-27 03:30:23 +00:00
|
|
|
edit_cb (NULL, child);
|
2017-12-08 02:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
static GtkWidget *
|
|
|
|
canvas_new (void)
|
|
|
|
{
|
|
|
|
GtkWidget *canvas;
|
|
|
|
GtkDragSource *source;
|
|
|
|
GtkDropTarget *dest;
|
|
|
|
GtkGesture *gesture;
|
|
|
|
|
|
|
|
canvas = gtk_fixed_new ();
|
|
|
|
gtk_widget_set_hexpand (canvas, TRUE);
|
|
|
|
gtk_widget_set_vexpand (canvas, TRUE);
|
|
|
|
gtk_widget_add_css_class (canvas, "frame");
|
|
|
|
|
|
|
|
source = gtk_drag_source_new ();
|
|
|
|
gtk_drag_source_set_actions (source, GDK_ACTION_MOVE);
|
|
|
|
g_signal_connect (source, "prepare", G_CALLBACK (prepare), NULL);
|
|
|
|
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), NULL);
|
|
|
|
g_signal_connect (source, "drag-end", G_CALLBACK (drag_end), NULL);
|
|
|
|
g_signal_connect (source, "drag-cancel", G_CALLBACK (drag_cancel), NULL);
|
|
|
|
gtk_widget_add_controller (canvas, GTK_EVENT_CONTROLLER (source));
|
|
|
|
|
|
|
|
dest = gtk_drop_target_new (GTK_TYPE_WIDGET, GDK_ACTION_MOVE);
|
|
|
|
g_signal_connect (dest, "drop", G_CALLBACK (drag_drop), NULL);
|
|
|
|
gtk_widget_add_controller (canvas, GTK_EVENT_CONTROLLER (dest));
|
|
|
|
|
|
|
|
gesture = gtk_gesture_click_new ();
|
|
|
|
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (gesture), 0);
|
|
|
|
g_signal_connect (gesture, "pressed", G_CALLBACK (pressed_cb), NULL);
|
|
|
|
g_signal_connect (gesture, "released", G_CALLBACK (released_cb), NULL);
|
|
|
|
gtk_widget_add_controller (canvas, GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_color (GtkWidget *item,
|
|
|
|
GdkRGBA *color)
|
|
|
|
{
|
|
|
|
char *css;
|
|
|
|
char *str;
|
|
|
|
GtkStyleContext *context;
|
|
|
|
GtkCssProvider *provider;
|
|
|
|
|
|
|
|
str = gdk_rgba_to_string (color);
|
|
|
|
css = g_strdup_printf ("* { background: %s; padding: 10px; }", str);
|
|
|
|
|
|
|
|
context = gtk_widget_get_style_context (item);
|
|
|
|
provider = g_object_get_data (G_OBJECT (context), "style-provider");
|
|
|
|
if (provider)
|
|
|
|
gtk_style_context_remove_provider (context, GTK_STYLE_PROVIDER (provider));
|
|
|
|
|
|
|
|
provider = gtk_css_provider_new ();
|
|
|
|
gtk_css_provider_load_from_data (provider, css, -1);
|
|
|
|
gtk_style_context_add_provider (gtk_widget_get_style_context (item), GTK_STYLE_PROVIDER (provider), 800);
|
|
|
|
g_object_set_data_full (G_OBJECT (context), "style-provider", provider, g_object_unref);
|
|
|
|
|
|
|
|
g_free (str);
|
|
|
|
g_free (css);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
item_drag_drop (GtkDropTarget *dest,
|
|
|
|
const GValue *value,
|
|
|
|
double x,
|
|
|
|
double y)
|
|
|
|
{
|
|
|
|
GtkWidget *item = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (dest));
|
|
|
|
|
|
|
|
set_color (item, g_value_get_boxed (value));
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
angle_changed (GtkGestureRotate *gesture,
|
|
|
|
double angle,
|
|
|
|
double delta)
|
|
|
|
{
|
|
|
|
GtkWidget *item = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
TransformData *data = g_object_get_data (G_OBJECT (item), "transform-data");
|
|
|
|
|
|
|
|
data->delta = angle / M_PI * 180.0;
|
|
|
|
|
|
|
|
apply_transform (item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rotate_done (GtkGesture *gesture)
|
|
|
|
{
|
|
|
|
GtkWidget *item = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
TransformData *data = g_object_get_data (G_OBJECT (item), "transform-data");
|
|
|
|
|
|
|
|
data->angle = data->angle + data->delta;
|
|
|
|
data->delta = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
click_done (GtkGesture *gesture)
|
|
|
|
{
|
|
|
|
GtkWidget *item = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
GtkWidget *canvas = gtk_widget_get_parent (item);
|
|
|
|
GtkWidget *last_child;
|
|
|
|
|
|
|
|
last_child = gtk_widget_get_last_child (canvas);
|
|
|
|
if (item != last_child)
|
|
|
|
gtk_widget_insert_after (item, canvas, last_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int n_items = 0;
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
canvas_item_new (double x,
|
|
|
|
double y)
|
|
|
|
{
|
|
|
|
GtkWidget *widget;
|
|
|
|
char *label;
|
|
|
|
char *id;
|
|
|
|
TransformData *transform_data;
|
|
|
|
GdkRGBA rgba;
|
|
|
|
GtkDropTarget *dest;
|
|
|
|
GtkGesture *gesture;
|
|
|
|
|
|
|
|
n_items++;
|
|
|
|
|
|
|
|
label = g_strdup_printf ("Item %d", n_items);
|
|
|
|
id = g_strdup_printf ("item%d", n_items);
|
|
|
|
|
|
|
|
gdk_rgba_parse (&rgba, "yellow");
|
|
|
|
|
|
|
|
widget = gtk_label_new (label);
|
|
|
|
gtk_widget_add_css_class (widget, "frame");
|
|
|
|
gtk_widget_set_name (widget, id);
|
|
|
|
|
|
|
|
set_color (widget, &rgba);
|
|
|
|
transform_data = g_new0 (TransformData, 1);
|
|
|
|
transform_data->x = x;
|
|
|
|
transform_data->y = y;
|
|
|
|
transform_data->angle = 0.0;
|
|
|
|
g_object_set_data_full (G_OBJECT (widget), "transform-data", transform_data, g_free);
|
|
|
|
|
|
|
|
g_free (label);
|
|
|
|
g_free (id);
|
|
|
|
|
|
|
|
dest = gtk_drop_target_new (GDK_TYPE_RGBA, GDK_ACTION_COPY);
|
|
|
|
g_signal_connect (dest, "drop", G_CALLBACK (item_drag_drop), NULL);
|
|
|
|
gtk_widget_add_controller (widget, GTK_EVENT_CONTROLLER (dest));
|
|
|
|
|
|
|
|
gesture = gtk_gesture_rotate_new ();
|
|
|
|
g_signal_connect (gesture, "angle-changed", G_CALLBACK (angle_changed), NULL);
|
|
|
|
g_signal_connect (gesture, "end", G_CALLBACK (rotate_done), NULL);
|
|
|
|
gtk_widget_add_controller (widget, GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
|
|
|
|
gesture = gtk_gesture_click_new ();
|
|
|
|
g_signal_connect (gesture, "released", G_CALLBACK (click_done), NULL);
|
|
|
|
gtk_widget_add_controller (widget, GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
2017-12-06 00:22:06 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_dnd (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
if (!window)
|
|
|
|
{
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *sw;
|
|
|
|
GtkWidget *canvas;
|
|
|
|
GtkWidget *box, *box2, *box3;
|
|
|
|
const char *colors[] = {
|
|
|
|
"red", "green", "blue", "magenta", "orange", "gray", "black", "yellow",
|
|
|
|
"white", "gray", "brown", "pink", "cyan", "bisque", "gold", "maroon",
|
|
|
|
"navy", "orchid", "olive", "peru", "salmon", "silver", "wheat",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
int x, y;
|
|
|
|
|
2020-05-09 14:33:02 +00:00
|
|
|
g_type_ensure (GTK_TYPE_COLOR_BUTTON);
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2017-12-06 00:22:06 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2020-04-26 02:14:25 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Drag-and-Drop");
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2017-12-06 00:22:06 +00:00
|
|
|
|
2020-04-26 02:14:25 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), box);
|
2020-04-26 02:14:25 +00:00
|
|
|
|
|
|
|
box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), box2);
|
|
|
|
|
|
|
|
canvas = canvas_new ();
|
|
|
|
gtk_container_add (GTK_CONTAINER (box2), canvas);
|
|
|
|
|
|
|
|
n_items = 0;
|
|
|
|
|
|
|
|
x = y = 40;
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
GtkWidget *item;
|
|
|
|
|
|
|
|
item = canvas_item_new (x, y);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_fixed_put (GTK_FIXED (canvas), item, 0, 0);
|
2020-04-26 02:14:25 +00:00
|
|
|
apply_transform (item);
|
|
|
|
|
|
|
|
x += 150;
|
|
|
|
y += 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw = gtk_scrolled_window_new (NULL, NULL);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_NEVER);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), sw);
|
|
|
|
|
|
|
|
box3 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
gtk_widget_add_css_class (box3, "linked");
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), box3);
|
2020-04-26 02:14:25 +00:00
|
|
|
|
|
|
|
for (i = 0; colors[i]; i++)
|
|
|
|
{
|
|
|
|
GdkRGBA rgba;
|
|
|
|
GtkWidget *swatch;
|
|
|
|
|
|
|
|
gdk_rgba_parse (&rgba, colors[i]);
|
|
|
|
|
|
|
|
swatch = g_object_new (g_type_from_name ("GtkColorSwatch"),
|
|
|
|
"rgba", &rgba,
|
|
|
|
"selectable", FALSE,
|
|
|
|
NULL);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box3), swatch);
|
|
|
|
}
|
2017-12-06 00:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
|
|
|
gtk_widget_show (window);
|
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2017-12-06 00:22:06 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|