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-05-22 16:35:18 +00:00
|
|
|
G_DECLARE_FINAL_TYPE (CanvasItem, canvas_item, CANVAS, ITEM, GtkWidget)
|
|
|
|
|
|
|
|
struct _CanvasItem {
|
|
|
|
GtkWidget parent;
|
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
GtkWidget *fixed;
|
2020-05-22 16:35:18 +00:00
|
|
|
GtkWidget *label;
|
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
double r;
|
2020-05-22 16:35:18 +00:00
|
|
|
double angle;
|
|
|
|
double delta;
|
2020-05-22 18:06:00 +00:00
|
|
|
|
|
|
|
GtkWidget *editor;
|
2020-05-22 16:35:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _CanvasItemClass {
|
|
|
|
GtkWidgetClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (CanvasItem, canvas_item, GTK_TYPE_WIDGET)
|
|
|
|
|
|
|
|
static int n_items = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_color (CanvasItem *item,
|
|
|
|
GdkRGBA *color)
|
|
|
|
{
|
|
|
|
char *css;
|
|
|
|
char *str;
|
|
|
|
GtkStyleContext *context;
|
|
|
|
GtkCssProvider *provider;
|
2020-06-19 23:18:45 +00:00
|
|
|
const char *old_class;
|
2020-05-22 16:35:18 +00:00
|
|
|
|
|
|
|
str = gdk_rgba_to_string (color);
|
2020-06-19 23:18:45 +00:00
|
|
|
css = g_strdup_printf ("* { background: %s; }", str);
|
2020-05-22 16:35:18 +00:00
|
|
|
|
|
|
|
context = gtk_widget_get_style_context (item->label);
|
|
|
|
provider = g_object_get_data (G_OBJECT (context), "style-provider");
|
|
|
|
if (provider)
|
|
|
|
gtk_style_context_remove_provider (context, GTK_STYLE_PROVIDER (provider));
|
|
|
|
|
2020-06-19 23:18:45 +00:00
|
|
|
old_class = (const char *)g_object_get_data (G_OBJECT (item->label), "css-class");
|
|
|
|
if (old_class)
|
|
|
|
gtk_widget_remove_css_class (item->label, old_class);
|
|
|
|
|
2020-05-22 16:35:18 +00:00
|
|
|
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->label), 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);
|
|
|
|
}
|
|
|
|
|
2020-06-19 23:18:45 +00:00
|
|
|
static void
|
|
|
|
set_css (CanvasItem *item,
|
|
|
|
const char *class)
|
|
|
|
{
|
|
|
|
GtkStyleContext *context;
|
|
|
|
GtkCssProvider *provider;
|
|
|
|
const char *old_class;
|
|
|
|
|
|
|
|
context = gtk_widget_get_style_context (item->label);
|
|
|
|
provider = g_object_get_data (G_OBJECT (context), "style-provider");
|
|
|
|
if (provider)
|
|
|
|
gtk_style_context_remove_provider (context, GTK_STYLE_PROVIDER (provider));
|
|
|
|
|
|
|
|
old_class = (const char *)g_object_get_data (G_OBJECT (item->label), "css-class");
|
|
|
|
if (old_class)
|
|
|
|
gtk_widget_remove_css_class (item->label, old_class);
|
|
|
|
|
|
|
|
g_object_set_data_full (G_OBJECT (item->label), "css-class", g_strdup (class), g_free);
|
|
|
|
gtk_widget_add_css_class (item->label, class);
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:35:18 +00:00
|
|
|
static gboolean
|
|
|
|
item_drag_drop (GtkDropTarget *dest,
|
|
|
|
const GValue *value,
|
|
|
|
double x,
|
|
|
|
double y)
|
|
|
|
{
|
2020-05-22 22:52:56 +00:00
|
|
|
GtkWidget *label = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (dest));
|
|
|
|
CanvasItem *item = CANVAS_ITEM (gtk_widget_get_parent (gtk_widget_get_parent (label)));
|
2020-05-22 16:35:18 +00:00
|
|
|
|
2020-06-19 23:18:45 +00:00
|
|
|
if (G_VALUE_TYPE (value) == GDK_TYPE_RGBA)
|
|
|
|
set_color (item, g_value_get_boxed (value));
|
|
|
|
else if (G_VALUE_TYPE (value) == G_TYPE_STRING)
|
|
|
|
set_css (item, g_value_get_string (value));
|
2020-05-22 16:35:18 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
apply_transform (CanvasItem *item)
|
|
|
|
{
|
|
|
|
GskTransform *transform;
|
2020-05-22 19:38:02 +00:00
|
|
|
double x, y;
|
2020-05-22 16:35:18 +00:00
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
x = gtk_widget_get_allocated_width (item->label) / 2.0;
|
|
|
|
y = gtk_widget_get_allocated_height (item->label) / 2.0;
|
|
|
|
item->r = sqrt (x*x + y*y);
|
|
|
|
|
|
|
|
transform = gsk_transform_translate (
|
|
|
|
gsk_transform_rotate (
|
|
|
|
gsk_transform_translate (NULL,
|
|
|
|
&(graphene_point_t) { item->r, item->r }),
|
|
|
|
item->angle + item->delta),
|
|
|
|
&(graphene_point_t) { - x, - y });
|
|
|
|
|
|
|
|
gtk_fixed_set_child_transform (GTK_FIXED (item->fixed), item->label, transform);
|
2020-05-22 16:35:18 +00:00
|
|
|
gsk_transform_unref (transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
angle_changed (GtkGestureRotate *gesture,
|
|
|
|
double angle,
|
|
|
|
double delta)
|
|
|
|
{
|
|
|
|
CanvasItem *item = CANVAS_ITEM (gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)));
|
|
|
|
|
|
|
|
item->delta = angle / M_PI * 180.0;
|
|
|
|
|
|
|
|
apply_transform (item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rotate_done (GtkGesture *gesture)
|
|
|
|
{
|
|
|
|
CanvasItem *item = CANVAS_ITEM (gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)));
|
|
|
|
|
|
|
|
item->angle = item->angle + item->delta;
|
|
|
|
item->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 void
|
|
|
|
canvas_item_init (CanvasItem *item)
|
|
|
|
{
|
|
|
|
char *text;
|
|
|
|
char *id;
|
|
|
|
GdkRGBA rgba;
|
|
|
|
GtkDropTarget *dest;
|
|
|
|
GtkGesture *gesture;
|
2020-06-19 23:18:45 +00:00
|
|
|
GType types[2] = { GDK_TYPE_RGBA, G_TYPE_STRING };
|
2020-05-22 16:35:18 +00:00
|
|
|
|
|
|
|
n_items++;
|
|
|
|
|
|
|
|
text = g_strdup_printf ("Item %d", n_items);
|
|
|
|
item->label = gtk_label_new (text);
|
2020-06-19 23:18:45 +00:00
|
|
|
gtk_widget_add_css_class (item->label, "canvasitem");
|
2020-05-22 16:35:18 +00:00
|
|
|
g_free (text);
|
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
item->fixed = gtk_fixed_new ();
|
|
|
|
gtk_widget_set_parent (item->fixed, GTK_WIDGET (item));
|
|
|
|
gtk_fixed_put (GTK_FIXED (item->fixed), item->label, 0, 0);
|
2020-05-22 16:35:18 +00:00
|
|
|
|
|
|
|
gtk_widget_add_css_class (item->label, "frame");
|
|
|
|
|
|
|
|
id = g_strdup_printf ("item%d", n_items);
|
|
|
|
gtk_widget_set_name (item->label, id);
|
|
|
|
g_free (id);
|
|
|
|
|
|
|
|
gdk_rgba_parse (&rgba, "yellow");
|
|
|
|
set_color (item, &rgba);
|
|
|
|
|
|
|
|
item->angle = 0;
|
|
|
|
|
2020-06-19 23:18:45 +00:00
|
|
|
dest = gtk_drop_target_new (G_TYPE_INVALID, GDK_ACTION_COPY);
|
|
|
|
gtk_drop_target_set_gtypes (dest, types, G_N_ELEMENTS (types));
|
2020-05-22 16:35:18 +00:00
|
|
|
g_signal_connect (dest, "drop", G_CALLBACK (item_drag_drop), NULL);
|
2020-05-22 22:52:56 +00:00
|
|
|
gtk_widget_add_controller (GTK_WIDGET (item->label), GTK_EVENT_CONTROLLER (dest));
|
2020-05-22 16:35:18 +00:00
|
|
|
|
|
|
|
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 (GTK_WIDGET (item), GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
|
|
|
|
gesture = gtk_gesture_click_new ();
|
|
|
|
g_signal_connect (gesture, "released", G_CALLBACK (click_done), NULL);
|
|
|
|
gtk_widget_add_controller (GTK_WIDGET (item), GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
canvas_item_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
CanvasItem *item = CANVAS_ITEM (object);
|
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
g_clear_pointer (&item->fixed, gtk_widget_unparent);
|
2020-05-22 18:06:00 +00:00
|
|
|
g_clear_pointer (&item->editor, gtk_widget_unparent);
|
2020-05-22 16:35:18 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (canvas_item_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
static void
|
|
|
|
canvas_item_map (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
CanvasItem *item = CANVAS_ITEM (widget);
|
|
|
|
|
|
|
|
GTK_WIDGET_CLASS (canvas_item_parent_class)->map (widget);
|
|
|
|
|
|
|
|
apply_transform (item);
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:35:18 +00:00
|
|
|
static void
|
|
|
|
canvas_item_class_init (CanvasItemClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
|
|
|
|
|
|
|
|
object_class->dispose = canvas_item_dispose;
|
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
widget_class->map = canvas_item_map;
|
|
|
|
|
2020-05-22 16:35:18 +00:00
|
|
|
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
|
2020-05-22 19:38:02 +00:00
|
|
|
gtk_widget_class_set_css_name (widget_class, "item");
|
2020-05-22 16:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
2020-05-22 19:38:02 +00:00
|
|
|
canvas_item_new (void)
|
2020-05-22 16:35:18 +00:00
|
|
|
{
|
|
|
|
CanvasItem *item = g_object_new (canvas_item_get_type (), NULL);
|
|
|
|
|
|
|
|
return GTK_WIDGET (item);
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:25:17 +00:00
|
|
|
static GdkPaintable *
|
|
|
|
canvas_item_get_drag_icon (CanvasItem *item)
|
|
|
|
{
|
2020-05-22 19:38:02 +00:00
|
|
|
return gtk_widget_paintable_new (item->fixed);
|
2020-05-22 17:25:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 18:06:00 +00:00
|
|
|
static gboolean
|
|
|
|
canvas_item_is_editing (CanvasItem *item)
|
|
|
|
{
|
|
|
|
return item->editor != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
scale_changed (GtkRange *range,
|
|
|
|
CanvasItem *item)
|
|
|
|
{
|
|
|
|
item->angle = gtk_range_get_value (range);
|
|
|
|
apply_transform (item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_changed (GtkEditable *editable,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
CanvasItem *item)
|
|
|
|
{
|
|
|
|
gtk_label_set_text (GTK_LABEL (item->label), gtk_editable_get_text (editable));
|
2020-05-22 19:38:02 +00:00
|
|
|
apply_transform (item);
|
2020-05-22 18:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
canvas_item_stop_editing (CanvasItem *item)
|
|
|
|
{
|
|
|
|
GtkWidget *scale;
|
|
|
|
|
|
|
|
if (!item->editor)
|
|
|
|
return;
|
|
|
|
|
|
|
|
scale = gtk_widget_get_last_child (item->editor);
|
|
|
|
g_signal_handlers_disconnect_by_func (scale, scale_changed, item);
|
|
|
|
|
|
|
|
gtk_fixed_remove (GTK_FIXED (gtk_widget_get_parent (item->editor)), item->editor);
|
|
|
|
item->editor = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
canvas_item_start_editing (CanvasItem *item)
|
|
|
|
{
|
|
|
|
GtkWidget *canvas = gtk_widget_get_parent (GTK_WIDGET (item));
|
|
|
|
GtkWidget *entry;
|
|
|
|
GtkWidget *scale;
|
2020-05-22 19:38:02 +00:00
|
|
|
double x, y;
|
2020-05-22 18:06:00 +00:00
|
|
|
|
|
|
|
if (item->editor)
|
|
|
|
return;
|
|
|
|
|
|
|
|
item->editor = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
|
|
|
|
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
|
|
|
|
gtk_editable_set_text (GTK_EDITABLE (entry),
|
|
|
|
gtk_label_get_text (GTK_LABEL (item->label)));
|
|
|
|
|
|
|
|
gtk_editable_set_width_chars (GTK_EDITABLE (entry), 12);
|
|
|
|
g_signal_connect (entry, "notify::text", G_CALLBACK (text_changed), item);
|
|
|
|
g_signal_connect_swapped (entry, "activate", G_CALLBACK (canvas_item_stop_editing), item);
|
|
|
|
|
|
|
|
gtk_box_append (GTK_BOX (item->editor), entry);
|
|
|
|
|
|
|
|
scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 360, 1);
|
|
|
|
gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
|
|
|
|
gtk_range_set_value (GTK_RANGE (scale), fmod (item->angle, 360));
|
|
|
|
|
|
|
|
g_signal_connect (scale, "value-changed", G_CALLBACK (scale_changed), item);
|
|
|
|
|
|
|
|
gtk_box_append (GTK_BOX (item->editor), scale);
|
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
gtk_widget_translate_coordinates (GTK_WIDGET (item), canvas, 0, 0, &x, &y);
|
|
|
|
gtk_fixed_put (GTK_FIXED (canvas), item->editor, x, y + 2 * item->r);
|
2020-05-22 18:06:00 +00:00
|
|
|
gtk_widget_grab_focus (entry);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-05-22 16:35:18 +00:00
|
|
|
item = gtk_widget_get_ancestor (item, canvas_item_get_type ());
|
|
|
|
if (!item)
|
2020-04-26 02:14:25 +00:00
|
|
|
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;
|
2020-05-22 19:38:02 +00:00
|
|
|
CanvasItem *item;
|
2020-05-22 14:50:09 +00:00
|
|
|
GdkPaintable *paintable;
|
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));
|
2020-05-22 19:38:02 +00:00
|
|
|
item = CANVAS_ITEM (g_object_get_data (G_OBJECT (canvas), "dragged-item"));
|
2019-12-27 03:30:23 +00:00
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
paintable = canvas_item_get_drag_icon (item);
|
|
|
|
gtk_drag_source_set_icon (source, paintable, item->r, item->r);
|
2020-05-22 14:50:09 +00:00
|
|
|
g_object_unref (paintable);
|
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
gtk_widget_set_opacity (GTK_WIDGET (item), 0.3);
|
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
|
|
|
}
|
|
|
|
|
2020-05-14 08:45:38 +00:00
|
|
|
static gboolean
|
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-05-14 08:45:38 +00:00
|
|
|
return FALSE;
|
2017-12-06 01:45:39 +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-05-22 16:35:18 +00:00
|
|
|
CanvasItem *item;
|
2020-04-26 02:14:25 +00:00
|
|
|
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);
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2020-05-22 16:35:18 +00:00
|
|
|
canvas = gtk_widget_get_parent (GTK_WIDGET (item));
|
2020-04-26 02:14:25 +00:00
|
|
|
last_child = gtk_widget_get_last_child (canvas);
|
2020-05-22 16:35:18 +00:00
|
|
|
if (GTK_WIDGET (item) != last_child)
|
|
|
|
gtk_widget_insert_after (GTK_WIDGET (item), canvas, last_child);
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
gtk_fixed_move (GTK_FIXED (canvas), GTK_WIDGET (item), x - item->r, y - item->r);
|
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
|
|
|
|
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;
|
2020-05-22 19:38:02 +00:00
|
|
|
GtkWidget *popover;
|
2020-04-26 02:14:25 +00:00
|
|
|
GtkWidget *item;
|
2020-05-22 19:38:02 +00:00
|
|
|
GdkRectangle rect;
|
2017-12-06 01:45:39 +00:00
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
popover = gtk_widget_get_ancestor (button, GTK_TYPE_POPOVER);
|
|
|
|
gtk_popover_get_pointing_to (GTK_POPOVER (popover), &rect);
|
|
|
|
|
|
|
|
item = canvas_item_new ();
|
|
|
|
gtk_fixed_put (GTK_FIXED (canvas), item, rect.x, rect.y);
|
2020-05-22 16:35:18 +00:00
|
|
|
apply_transform (CANVAS_ITEM (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
|
2019-12-27 03:30:23 +00:00
|
|
|
edit_cb (GtkWidget *button, GtkWidget *child)
|
2017-12-07 04:10:56 +00:00
|
|
|
{
|
2020-05-22 16:35:18 +00:00
|
|
|
CanvasItem *item = CANVAS_ITEM (child);
|
2019-12-27 03:30:23 +00:00
|
|
|
|
|
|
|
if (button)
|
|
|
|
gtk_popover_popdown (GTK_POPOVER (gtk_widget_get_ancestor (button, GTK_TYPE_POPOVER)));
|
2020-05-22 18:06:00 +00:00
|
|
|
|
|
|
|
if (!canvas_item_is_editing (item))
|
|
|
|
canvas_item_start_editing (item);
|
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);
|
2020-05-22 16:35:18 +00:00
|
|
|
child = gtk_widget_get_ancestor (child, canvas_item_get_type ());
|
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;
|
|
|
|
|
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);
|
2020-05-14 08:13:05 +00:00
|
|
|
gtk_popover_set_child (GTK_POPOVER (menu), box);
|
2019-12-27 03:30:23 +00:00
|
|
|
|
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);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), item);
|
2019-12-27 03:30:23 +00:00
|
|
|
|
|
|
|
item = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), item);
|
2019-12-27 03:30:23 +00:00
|
|
|
|
|
|
|
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);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (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);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (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);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (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;
|
2020-05-22 18:06:00 +00:00
|
|
|
CanvasItem *item;
|
2017-12-08 02:25:10 +00:00
|
|
|
|
|
|
|
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);
|
2020-05-22 18:06:00 +00:00
|
|
|
item = (CanvasItem *)gtk_widget_get_ancestor (child, canvas_item_get_type ());
|
|
|
|
if (!item)
|
|
|
|
return;
|
2017-12-08 02:25:10 +00:00
|
|
|
|
|
|
|
if (gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture)) == GDK_BUTTON_PRIMARY)
|
|
|
|
{
|
2020-05-22 18:06:00 +00:00
|
|
|
if (canvas_item_is_editing (item))
|
|
|
|
canvas_item_stop_editing (item);
|
|
|
|
else
|
|
|
|
canvas_item_start_editing (item);
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-19 23:18:45 +00:00
|
|
|
static GdkContentProvider *
|
|
|
|
css_drag_prepare (GtkDragSource *source,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *button)
|
|
|
|
{
|
|
|
|
const char *class;
|
|
|
|
GdkPaintable *paintable;
|
|
|
|
|
|
|
|
class = (const char *)g_object_get_data (G_OBJECT (button), "css-class");
|
|
|
|
|
|
|
|
paintable = gtk_widget_paintable_new (button);
|
|
|
|
gtk_drag_source_set_icon (source, paintable, 0, 0);
|
|
|
|
g_object_unref (paintable);
|
|
|
|
|
|
|
|
return gdk_content_provider_new_typed (G_TYPE_STRING, class);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
css_button_new (const char *class)
|
|
|
|
{
|
|
|
|
GtkWidget *button;
|
|
|
|
GtkDragSource *source;
|
|
|
|
|
|
|
|
button = gtk_image_new ();
|
|
|
|
gtk_widget_set_size_request (button, 48, 32);
|
|
|
|
gtk_widget_add_css_class (button, class);
|
|
|
|
g_object_set_data (G_OBJECT (button), "css-class", (gpointer)class);
|
|
|
|
|
|
|
|
source = gtk_drag_source_new ();
|
|
|
|
g_signal_connect (source, "prepare", G_CALLBACK (css_drag_prepare), button);
|
|
|
|
gtk_widget_add_controller (button, GTK_EVENT_CONTROLLER (source));
|
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2020-07-18 00:44:06 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GtkWidget parent_instance;
|
|
|
|
GdkRGBA color;
|
|
|
|
} ColorSwatch;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GtkWidgetClass parent_class;
|
|
|
|
} ColorSwatchClass;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (ColorSwatch, color_swatch, GTK_TYPE_WIDGET)
|
|
|
|
|
|
|
|
static GdkContentProvider *
|
|
|
|
color_swatch_drag_prepare (GtkDragSource *source,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
ColorSwatch *swatch)
|
|
|
|
{
|
|
|
|
return gdk_content_provider_new_typed (GDK_TYPE_RGBA, &swatch->color);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
color_swatch_init (ColorSwatch *swatch)
|
|
|
|
{
|
|
|
|
GtkDragSource *source = gtk_drag_source_new ();
|
|
|
|
g_signal_connect (source, "prepare", G_CALLBACK (color_swatch_drag_prepare), swatch);
|
|
|
|
gtk_widget_add_controller (GTK_WIDGET (swatch), GTK_EVENT_CONTROLLER (source));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
color_swatch_snapshot (GtkWidget *widget,
|
|
|
|
GtkSnapshot *snapshot)
|
|
|
|
{
|
|
|
|
ColorSwatch *swatch = (ColorSwatch *)widget;
|
|
|
|
float w = gtk_widget_get_width (widget);
|
|
|
|
float h = gtk_widget_get_height (widget);
|
|
|
|
|
|
|
|
gtk_snapshot_append_color (snapshot, &swatch->color,
|
|
|
|
&GRAPHENE_RECT_INIT(0, 0, w, h));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
color_swatch_measure (GtkWidget *widget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum_size,
|
|
|
|
int *natural_size,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline)
|
|
|
|
{
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
*minimum_size = *natural_size = 48;
|
|
|
|
else
|
|
|
|
*minimum_size = *natural_size = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
color_swatch_class_init (ColorSwatchClass *class)
|
|
|
|
{
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
|
|
|
|
|
|
|
|
widget_class->snapshot = color_swatch_snapshot;
|
|
|
|
widget_class->measure = color_swatch_measure;
|
|
|
|
gtk_widget_class_set_css_name (widget_class, "colorswatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
color_swatch_new (const char *color)
|
|
|
|
{
|
|
|
|
ColorSwatch *swatch = g_object_new (color_swatch_get_type (), NULL);
|
|
|
|
|
|
|
|
gdk_rgba_parse (&swatch->color, color);
|
|
|
|
|
|
|
|
return GTK_WIDGET (swatch);
|
|
|
|
}
|
|
|
|
|
2017-12-06 00:22:06 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_dnd (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
if (!window)
|
|
|
|
{
|
2020-05-14 05:16:56 +00:00
|
|
|
GtkWidget *button;
|
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-06-19 23:18:45 +00:00
|
|
|
GtkCssProvider *provider;
|
2020-04-26 02:14:25 +00:00
|
|
|
|
2020-05-14 05:16:56 +00:00
|
|
|
button = gtk_color_button_new ();
|
|
|
|
g_object_unref (g_object_ref_sink (button));
|
2020-05-09 14:33:02 +00:00
|
|
|
|
2020-06-19 23:18:45 +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);
|
|
|
|
g_object_unref (provider);
|
|
|
|
|
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);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), box2);
|
2020-04-26 02:14:25 +00:00
|
|
|
|
|
|
|
canvas = canvas_new ();
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box2), canvas);
|
2020-04-26 02:14:25 +00:00
|
|
|
|
|
|
|
n_items = 0;
|
|
|
|
|
|
|
|
x = y = 40;
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
GtkWidget *item;
|
|
|
|
|
2020-05-22 19:38:02 +00:00
|
|
|
item = canvas_item_new ();
|
|
|
|
gtk_fixed_put (GTK_FIXED (canvas), item, x, y);
|
2020-05-22 16:35:18 +00:00
|
|
|
apply_transform (CANVAS_ITEM (item));
|
2020-04-26 02:14:25 +00:00
|
|
|
|
|
|
|
x += 150;
|
|
|
|
y += 100;
|
|
|
|
}
|
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2020-04-26 02:14:25 +00:00
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_NEVER);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), sw);
|
2020-04-26 02:14:25 +00:00
|
|
|
|
|
|
|
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++)
|
2020-07-18 00:44:06 +00:00
|
|
|
gtk_box_append (GTK_BOX (box3), color_swatch_new (colors[i]));
|
2020-06-19 23:18:45 +00:00
|
|
|
|
|
|
|
gtk_box_append (GTK_BOX (box3), css_button_new ("rainbow1"));
|
|
|
|
gtk_box_append (GTK_BOX (box3), css_button_new ("rainbow2"));
|
|
|
|
gtk_box_append (GTK_BOX (box3), css_button_new ("rainbow3"));
|
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;
|
|
|
|
}
|