1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#include <gtk/gtk.h>
static GtkTargetEntry entries[] = {
{ "GTK_LIST_BOX_ROW", GTK_TARGET_SAME_APP, 0 }
};
static void
drag_begin (GtkWidget *widget,
GdkDragContext *context,
gpointer data)
{
GtkAllocation alloc;
cairo_surface_t *surface;
cairo_t *cr;
gtk_widget_get_allocation (widget, &alloc);
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, alloc.width, alloc.height);
cr = cairo_create (surface);
gtk_style_context_add_class (gtk_widget_get_style_context (widget), "during-dnd");
gtk_widget_draw (widget, cr);
gtk_style_context_remove_class (gtk_widget_get_style_context (widget), "during-dnd");
gtk_drag_set_icon_surface (context, surface);
cairo_destroy (cr);
cairo_surface_destroy (surface);
}
void
drag_data_get (GtkWidget *widget,
GdkDragContext *context,
GtkSelectionData *selection_data,
guint info,
guint time,
gpointer data)
{
gtk_selection_data_set (selection_data,
gdk_atom_intern_static_string ("GTK_LIST_BOX_ROW"),
32,
(const guchar *)&widget,
sizeof (gpointer));
}
static void
drag_data_received (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint32 time,
gpointer data)
{
GtkWidget *target;
GtkWidget *row;
GtkWidget *source;
int pos;
target = gtk_widget_get_ancestor (widget, GTK_TYPE_LIST_BOX_ROW);
pos = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (target));
row = (gpointer)* (gpointer*)gtk_selection_data_get_data (selection_data);
source = gtk_widget_get_ancestor (row, GTK_TYPE_LIST_BOX_ROW);
g_object_ref (source);
gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (source)), source);
gtk_list_box_insert (GTK_LIST_BOX (gtk_widget_get_parent (target)), source, pos);
g_object_unref (source);
}
static GtkWidget *
create_row (const gchar *text)
{
GtkWidget *row, *ebox, *box, *label;
row = gtk_list_box_row_new ();
ebox = gtk_event_box_new ();
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
label = gtk_label_new (text);
gtk_container_add (GTK_CONTAINER (row), ebox);
gtk_container_add (GTK_CONTAINER (ebox), box);
gtk_container_add (GTK_CONTAINER (box), label);
gtk_drag_source_set (ebox, GDK_BUTTON1_MASK, entries, 1, GDK_ACTION_MOVE);
g_signal_connect (ebox, "drag-begin", G_CALLBACK (drag_begin), NULL);
g_signal_connect (ebox, "drag-data-get", G_CALLBACK (drag_data_get), NULL);
gtk_drag_dest_set (ebox, GTK_DEST_DEFAULT_ALL, entries, 1, GDK_ACTION_MOVE);
g_signal_connect (ebox, "drag-data-received", G_CALLBACK (drag_data_received), NULL);
return row;
}
static void
on_row_activated (GtkListBox *self,
GtkWidget *child)
{
const char *id;
id = g_object_get_data (G_OBJECT (gtk_bin_get_child (GTK_BIN (child))), "id");
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");
}
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));
}
static const char *css =
".during-dnd { "
" background: white; "
" border: 1px solid black; "
"}";
int
main (int argc, char *argv[])
{
GtkWidget *window, *list, *sw, *row;
GtkWidget *hbox, *vbox, *combo, *button;
gint i;
gchar *text;
GtkCssProvider *provider;
gtk_init ();
provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, css, -1, NULL);
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (), GTK_STYLE_PROVIDER (provider), 800);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), -1, 300);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
gtk_container_add (GTK_CONTAINER (window), hbox);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
g_object_set (vbox, "margin", 12, NULL);
gtk_container_add (GTK_CONTAINER (hbox), vbox);
list = gtk_list_box_new ();
gtk_list_box_set_selection_mode (GTK_LIST_BOX (list), GTK_SELECTION_NONE);
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);
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);
gtk_container_add (GTK_CONTAINER (hbox), sw);
gtk_container_add (GTK_CONTAINER (sw), list);
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);
gtk_container_add (GTK_CONTAINER (vbox), button);
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);
gtk_container_add (GTK_CONTAINER (vbox), combo);
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);
}
gtk_widget_show (window);
gtk_main ();
return 0;
}
|