2018-09-19 02:29:33 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2018-09-22 20:14:25 +00:00
|
|
|
#ifdef SMALL
|
|
|
|
#define AVERAGE 15
|
|
|
|
#define VARIANCE 10
|
|
|
|
#else
|
2018-09-19 02:29:33 +00:00
|
|
|
#define AVERAGE 300
|
|
|
|
#define VARIANCE 200
|
2018-09-22 20:14:25 +00:00
|
|
|
#endif
|
2018-09-19 02:29:33 +00:00
|
|
|
|
2018-09-24 02:42:15 +00:00
|
|
|
static void
|
2020-06-29 20:37:14 +00:00
|
|
|
setup_list_item (GtkSignalListItemFactory *factory,
|
|
|
|
GtkListItem *list_item)
|
2018-09-19 02:29:33 +00:00
|
|
|
{
|
2018-09-24 02:42:15 +00:00
|
|
|
GtkWidget *label = gtk_label_new ("");
|
|
|
|
|
|
|
|
gtk_list_item_set_child (list_item, label);
|
2018-09-19 02:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-06-29 20:37:14 +00:00
|
|
|
bind_list_item (GtkSignalListItemFactory *factory,
|
|
|
|
GtkListItem *list_item)
|
2018-09-19 02:29:33 +00:00
|
|
|
{
|
2018-09-24 02:42:15 +00:00
|
|
|
GtkWidget *label;
|
|
|
|
gpointer item;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
item = gtk_list_item_get_item (list_item);
|
2018-09-19 02:29:33 +00:00
|
|
|
|
2018-09-24 02:42:15 +00:00
|
|
|
if (item)
|
|
|
|
s = g_strdup_printf ("%u: %s",
|
|
|
|
gtk_list_item_get_position (list_item),
|
|
|
|
(const char *) g_object_get_data (item, "message"));
|
|
|
|
else
|
|
|
|
s = NULL;
|
|
|
|
|
|
|
|
label = gtk_list_item_get_child (list_item);
|
|
|
|
gtk_label_set_text (GTK_LABEL (label), s);
|
|
|
|
|
|
|
|
g_free (s);
|
2018-09-19 02:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
create_widget_for_listbox (gpointer item,
|
|
|
|
gpointer unused)
|
|
|
|
{
|
2018-09-24 02:42:15 +00:00
|
|
|
const char *message = g_object_get_data (item, "message");
|
2018-09-19 02:29:33 +00:00
|
|
|
GtkWidget *widget;
|
|
|
|
|
2018-09-24 02:42:15 +00:00
|
|
|
widget = gtk_label_new (message);
|
2018-09-19 02:29:33 +00:00
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
2018-09-22 20:14:25 +00:00
|
|
|
static guint
|
|
|
|
get_number (GObject *item)
|
|
|
|
{
|
|
|
|
return GPOINTER_TO_UINT (g_object_get_data (item, "counter")) % 1000;
|
|
|
|
}
|
|
|
|
|
2018-09-19 02:29:33 +00:00
|
|
|
static void
|
|
|
|
add (GListStore *store)
|
|
|
|
{
|
|
|
|
static guint counter;
|
|
|
|
GObject *o;
|
|
|
|
char *message;
|
|
|
|
guint pos;
|
|
|
|
|
2018-09-22 20:14:25 +00:00
|
|
|
counter++;
|
2018-09-19 02:29:33 +00:00
|
|
|
o = g_object_new (G_TYPE_OBJECT, NULL);
|
2018-09-22 20:14:25 +00:00
|
|
|
g_object_set_data (o, "counter", GUINT_TO_POINTER (counter));
|
|
|
|
message = g_strdup_printf ("Item %u", counter);
|
2018-09-19 02:29:33 +00:00
|
|
|
g_object_set_data_full (o, "message", message, g_free);
|
|
|
|
|
|
|
|
pos = g_random_int_range (0, g_list_model_get_n_items (G_LIST_MODEL (store)) + 1);
|
|
|
|
g_list_store_insert (store, pos, o);
|
|
|
|
g_object_unref (o);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
delete (GListStore *store)
|
|
|
|
{
|
|
|
|
guint pos;
|
|
|
|
|
|
|
|
pos = g_random_int_range (0, g_list_model_get_n_items (G_LIST_MODEL (store)));
|
|
|
|
g_list_store_remove (store, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
do_stuff (gpointer store)
|
|
|
|
{
|
|
|
|
if (g_random_int_range (AVERAGE - VARIANCE, AVERAGE + VARIANCE) < g_list_model_get_n_items (store))
|
|
|
|
delete (store);
|
|
|
|
else
|
|
|
|
add (store);
|
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2018-09-22 20:14:25 +00:00
|
|
|
static gboolean
|
|
|
|
revert_sort (gpointer sorter)
|
|
|
|
{
|
|
|
|
if (gtk_numeric_sorter_get_sort_order (sorter) == GTK_SORT_ASCENDING)
|
|
|
|
gtk_numeric_sorter_set_sort_order (sorter, GTK_SORT_DESCENDING);
|
|
|
|
else
|
|
|
|
gtk_numeric_sorter_set_sort_order (sorter, GTK_SORT_ASCENDING);
|
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2018-09-19 02:29:33 +00:00
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char *argv[])
|
|
|
|
{
|
|
|
|
GtkWidget *win, *hbox, *vbox, *sw, *listview, *listbox, *label;
|
|
|
|
GListStore *store;
|
|
|
|
GListModel *toplevels;
|
2018-09-22 20:14:25 +00:00
|
|
|
GtkSortListModel *sort;
|
|
|
|
GtkSorter *sorter;
|
2018-09-19 02:29:33 +00:00
|
|
|
guint i;
|
2020-06-29 20:37:14 +00:00
|
|
|
GtkListItemFactory *factory;
|
2020-08-31 21:06:48 +00:00
|
|
|
GtkSelectionModel *selection;
|
2018-09-19 02:29:33 +00:00
|
|
|
|
|
|
|
gtk_init ();
|
|
|
|
|
2018-09-22 20:14:25 +00:00
|
|
|
store = g_list_store_new (G_TYPE_OBJECT);
|
|
|
|
for (i = 0; i < AVERAGE; i++)
|
|
|
|
add (store);
|
2020-08-03 19:54:42 +00:00
|
|
|
sorter = GTK_SORTER (gtk_numeric_sorter_new (gtk_cclosure_expression_new (G_TYPE_UINT, NULL, 0, NULL, (GCallback)get_number, NULL, NULL)));
|
2018-09-22 20:14:25 +00:00
|
|
|
sort = gtk_sort_list_model_new (G_LIST_MODEL (store), sorter);
|
|
|
|
|
2018-09-19 02:29:33 +00:00
|
|
|
win = gtk_window_new ();
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (win), 400, 600);
|
|
|
|
|
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
|
|
|
|
gtk_window_set_child (GTK_WINDOW (win), hbox);
|
|
|
|
|
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
|
|
|
|
gtk_box_append (GTK_BOX (hbox), vbox);
|
|
|
|
|
|
|
|
label = gtk_label_new ("GtkListView");
|
|
|
|
gtk_box_append (GTK_BOX (vbox), label);
|
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2018-09-19 02:29:33 +00:00
|
|
|
gtk_widget_set_hexpand (sw, TRUE);
|
|
|
|
gtk_widget_set_vexpand (sw, TRUE);
|
|
|
|
gtk_box_append (GTK_BOX (vbox), sw);
|
|
|
|
|
2020-06-29 20:37:14 +00:00
|
|
|
factory = gtk_signal_list_item_factory_new ();
|
|
|
|
g_signal_connect (factory, "setup", G_CALLBACK (setup_list_item), NULL);
|
|
|
|
g_signal_connect (factory, "bind", G_CALLBACK (bind_list_item), NULL);
|
2020-09-01 16:24:06 +00:00
|
|
|
listview = gtk_list_view_new (NULL, factory);
|
2020-06-29 20:37:14 +00:00
|
|
|
|
2018-09-19 02:29:33 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);
|
|
|
|
|
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
|
|
|
|
gtk_box_append (GTK_BOX (hbox), vbox);
|
|
|
|
|
|
|
|
label = gtk_label_new ("GtkListBox");
|
|
|
|
gtk_box_append (GTK_BOX (vbox), label);
|
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2018-09-19 02:29:33 +00:00
|
|
|
gtk_widget_set_hexpand (sw, TRUE);
|
|
|
|
gtk_widget_set_vexpand (sw, TRUE);
|
|
|
|
gtk_box_append (GTK_BOX (vbox), sw);
|
|
|
|
|
|
|
|
listbox = gtk_list_box_new ();
|
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listbox);
|
|
|
|
|
2020-08-31 21:06:48 +00:00
|
|
|
selection = GTK_SELECTION_MODEL (gtk_single_selection_new (G_LIST_MODEL (sort)));
|
|
|
|
gtk_list_view_set_model (GTK_LIST_VIEW (listview), selection);
|
|
|
|
g_object_unref (selection);
|
2018-09-19 02:29:33 +00:00
|
|
|
gtk_list_box_bind_model (GTK_LIST_BOX (listbox),
|
2018-09-22 20:14:25 +00:00
|
|
|
G_LIST_MODEL (sort),
|
2018-09-19 02:29:33 +00:00
|
|
|
create_widget_for_listbox,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
g_timeout_add (100, do_stuff, store);
|
2018-09-22 20:14:25 +00:00
|
|
|
g_timeout_add_seconds (3, revert_sort, sorter);
|
2018-09-19 02:29:33 +00:00
|
|
|
|
|
|
|
gtk_widget_show (win);
|
|
|
|
|
|
|
|
toplevels = gtk_window_get_toplevels ();
|
|
|
|
while (g_list_model_get_n_items (toplevels))
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|