2014-03-29 01:48:54 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2020-02-15 20:07:26 +00:00
|
|
|
static GdkContentProvider *
|
|
|
|
prepare (GtkDragSource *source,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *row)
|
|
|
|
{
|
2020-02-16 13:24:03 +00:00
|
|
|
return gdk_content_provider_new_typed (GTK_TYPE_LIST_BOX_ROW, row);
|
2020-02-15 20:07:26 +00:00
|
|
|
}
|
2017-04-21 21:02:05 +00:00
|
|
|
|
|
|
|
static void
|
2020-01-01 02:10:15 +00:00
|
|
|
drag_begin (GtkDragSource *source,
|
2020-01-06 18:28:25 +00:00
|
|
|
GdkDrag *drag,
|
2020-01-01 02:10:15 +00:00
|
|
|
GtkWidget *widget)
|
2017-04-21 21:02:05 +00:00
|
|
|
{
|
2017-04-22 12:03:00 +00:00
|
|
|
GtkWidget *row;
|
2017-04-21 21:02:05 +00:00
|
|
|
GtkAllocation alloc;
|
2018-03-26 16:30:34 +00:00
|
|
|
GdkPaintable *paintable;
|
2020-05-17 21:08:01 +00:00
|
|
|
double x, y;
|
2017-04-21 21:02:05 +00:00
|
|
|
|
2017-04-22 12:03:00 +00:00
|
|
|
row = gtk_widget_get_ancestor (widget, GTK_TYPE_LIST_BOX_ROW);
|
|
|
|
gtk_widget_get_allocation (row, &alloc);
|
2018-03-26 16:30:34 +00:00
|
|
|
|
2018-04-02 06:12:37 +00:00
|
|
|
paintable = gtk_widget_paintable_new (row);
|
2017-04-22 12:03:00 +00:00
|
|
|
gtk_widget_translate_coordinates (widget, row, 0, 0, &x, &y);
|
2020-01-01 02:10:15 +00:00
|
|
|
gtk_drag_source_set_icon (source, paintable, -x, -y);
|
2017-04-21 21:02:05 +00:00
|
|
|
|
2018-03-26 16:30:34 +00:00
|
|
|
g_object_unref (paintable);
|
2017-04-21 21:02:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 02:47:17 +00:00
|
|
|
static gboolean
|
|
|
|
drag_drop (GtkDropTarget *dest,
|
|
|
|
const GValue *value,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
gpointer data)
|
2017-04-21 21:02:05 +00:00
|
|
|
{
|
2020-01-02 13:21:49 +00:00
|
|
|
GtkWidget *target = data;
|
2017-04-21 21:02:05 +00:00
|
|
|
GtkWidget *source;
|
|
|
|
int pos;
|
2020-01-03 05:46:31 +00:00
|
|
|
|
2020-02-29 02:47:17 +00:00
|
|
|
source = g_value_get_object (value);
|
2020-02-15 20:07:26 +00:00
|
|
|
if (source == NULL)
|
2020-02-29 02:47:17 +00:00
|
|
|
return FALSE;
|
2017-04-21 21:02:05 +00:00
|
|
|
|
|
|
|
pos = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (target));
|
2017-04-23 01:37:55 +00:00
|
|
|
if (source == target)
|
2020-02-29 02:47:17 +00:00
|
|
|
return FALSE;
|
2017-04-23 01:37:55 +00:00
|
|
|
|
2017-04-21 21:02:05 +00:00
|
|
|
g_object_ref (source);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_remove (GTK_BOX (gtk_widget_get_parent (source)), source);
|
2017-04-21 21:02:05 +00:00
|
|
|
gtk_list_box_insert (GTK_LIST_BOX (gtk_widget_get_parent (target)), source, pos);
|
|
|
|
g_object_unref (source);
|
2020-02-15 20:07:26 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2020-01-03 05:46:31 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 01:48:54 +00:00
|
|
|
static GtkWidget *
|
|
|
|
create_row (const gchar *text)
|
|
|
|
{
|
2017-08-01 22:49:42 +00:00
|
|
|
GtkWidget *row, *box, *label, *image;
|
2020-01-01 02:10:15 +00:00
|
|
|
GtkDragSource *source;
|
2020-01-02 13:21:49 +00:00
|
|
|
GtkDropTarget *dest;
|
2014-03-29 01:48:54 +00:00
|
|
|
|
|
|
|
row = gtk_list_box_row_new ();
|
2017-11-15 00:43:13 +00:00
|
|
|
image = gtk_image_new_from_icon_name ("open-menu-symbolic");
|
2014-03-29 01:48:54 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
|
2017-04-22 12:03:00 +00:00
|
|
|
g_object_set (box, "margin-start", 10, "margin-end", 10, NULL);
|
2014-03-29 01:48:54 +00:00
|
|
|
label = gtk_label_new (text);
|
2020-05-03 04:09:18 +00:00
|
|
|
gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), box);
|
2017-04-21 16:59:59 +00:00
|
|
|
gtk_widget_set_hexpand (label, TRUE);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), label);
|
|
|
|
gtk_box_append (GTK_BOX (box), image);
|
2014-03-29 01:48:54 +00:00
|
|
|
|
2020-01-06 19:46:14 +00:00
|
|
|
source = gtk_drag_source_new ();
|
|
|
|
gtk_drag_source_set_actions (source, GDK_ACTION_MOVE);
|
2020-01-01 02:10:15 +00:00
|
|
|
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
|
2020-02-15 20:07:26 +00:00
|
|
|
g_signal_connect (source, "prepare", G_CALLBACK (prepare), row);
|
2020-01-07 06:13:35 +00:00
|
|
|
gtk_widget_add_controller (image, GTK_EVENT_CONTROLLER (source));
|
2017-11-14 21:32:23 +00:00
|
|
|
|
2020-02-29 02:47:17 +00:00
|
|
|
dest = gtk_drop_target_new (GTK_TYPE_LIST_BOX_ROW, GDK_ACTION_MOVE);
|
|
|
|
g_signal_connect (dest, "drop", G_CALLBACK (drag_drop), row);
|
2020-01-06 05:12:21 +00:00
|
|
|
gtk_widget_add_controller (GTK_WIDGET (row), GTK_EVENT_CONTROLLER (dest));
|
2017-04-21 21:02:05 +00:00
|
|
|
|
2014-03-29 01:48:54 +00:00
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2014-03-29 04:59:05 +00:00
|
|
|
static void
|
|
|
|
on_row_activated (GtkListBox *self,
|
|
|
|
GtkWidget *child)
|
|
|
|
{
|
|
|
|
const char *id;
|
2020-05-03 04:09:18 +00:00
|
|
|
id = g_object_get_data (G_OBJECT (gtk_list_box_row_get_child (GTK_LIST_BOX_ROW (child))), "id");
|
2014-03-29 04:59:05 +00:00
|
|
|
g_message ("Row activated %p: %s", child, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_selected_children_changed (GtkListBox *self)
|
|
|
|
{
|
|
|
|
g_message ("Selection changed");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
a11y_selection_changed (AtkObject *obj)
|
|
|
|
{
|
|
|
|
g_message ("Accessible selection changed");
|
|
|
|
}
|
|
|
|
|
2014-03-29 01:48:54 +00:00
|
|
|
static void
|
|
|
|
selection_mode_changed (GtkComboBox *combo, gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
|
|
|
|
gtk_list_box_set_selection_mode (list, gtk_combo_box_get_active (combo));
|
|
|
|
}
|
|
|
|
|
2017-04-21 21:02:05 +00:00
|
|
|
static const char *css =
|
|
|
|
".during-dnd { "
|
|
|
|
" background: white; "
|
|
|
|
" border: 1px solid black; "
|
|
|
|
"}";
|
|
|
|
|
2014-03-29 01:48:54 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GtkWidget *window, *list, *sw, *row;
|
|
|
|
GtkWidget *hbox, *vbox, *combo, *button;
|
|
|
|
gint i;
|
|
|
|
gchar *text;
|
2017-04-21 21:02:05 +00:00
|
|
|
GtkCssProvider *provider;
|
2014-03-29 01:48:54 +00:00
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2014-03-29 01:48:54 +00:00
|
|
|
|
2017-04-21 21:02:05 +00:00
|
|
|
provider = gtk_css_provider_new ();
|
2017-04-22 15:30:33 +00:00
|
|
|
gtk_css_provider_load_from_data (provider, css, -1);
|
2017-10-31 01:35:21 +00:00
|
|
|
gtk_style_context_add_provider_for_display (gdk_display_get_default (), GTK_STYLE_PROVIDER (provider), 800);
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2014-03-29 01:48:54 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), -1, 300);
|
|
|
|
|
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), hbox);
|
2014-03-29 01:48:54 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (vbox, 12);
|
|
|
|
gtk_widget_set_margin_end (vbox, 12);
|
|
|
|
gtk_widget_set_margin_top (vbox, 12);
|
|
|
|
gtk_widget_set_margin_bottom (vbox, 12);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), vbox);
|
2014-03-29 01:48:54 +00:00
|
|
|
|
|
|
|
list = gtk_list_box_new ();
|
|
|
|
gtk_list_box_set_selection_mode (GTK_LIST_BOX (list), GTK_SELECTION_NONE);
|
|
|
|
|
2014-03-29 04:59:05 +00:00
|
|
|
g_signal_connect (list, "row-activated", G_CALLBACK (on_row_activated), NULL);
|
|
|
|
g_signal_connect (list, "selected-rows-changed", G_CALLBACK (on_selected_children_changed), NULL);
|
|
|
|
g_signal_connect (gtk_widget_get_accessible (list), "selection-changed", G_CALLBACK (a11y_selection_changed), NULL);
|
|
|
|
|
2014-03-29 01:48:54 +00:00
|
|
|
sw = gtk_scrolled_window_new (NULL, NULL);
|
|
|
|
gtk_widget_set_hexpand (sw, TRUE);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), sw);
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), list);
|
2014-03-29 01:48:54 +00:00
|
|
|
|
|
|
|
button = gtk_check_button_new_with_label ("Activate on single click");
|
|
|
|
g_object_bind_property (list, "activate-on-single-click",
|
|
|
|
button, "active",
|
|
|
|
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2014-03-29 01:48:54 +00:00
|
|
|
|
|
|
|
combo = gtk_combo_box_text_new ();
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "None");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Single");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Browse");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Multiple");
|
|
|
|
g_signal_connect (combo, "changed", G_CALLBACK (selection_mode_changed), list);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), combo);
|
2014-03-29 01:48:54 +00:00
|
|
|
gtk_combo_box_set_active (GTK_COMBO_BOX (combo), gtk_list_box_get_selection_mode (GTK_LIST_BOX (list)));
|
|
|
|
|
|
|
|
for (i = 0; i < 20; i++)
|
|
|
|
{
|
|
|
|
text = g_strdup_printf ("Row %d", i);
|
|
|
|
row = create_row (text);
|
|
|
|
gtk_list_box_insert (GTK_LIST_BOX (list), row, -1);
|
|
|
|
}
|
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2014-03-29 01:48:54 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (TRUE)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2014-03-29 01:48:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|