-/*
+/*
* Copyright (C) 2019, Red Hat, Inc.
* Authors: Matthias Clasen <mclasen@redhat.com>
*
return g_string_free (string, FALSE);
}
+static char *
+section_model_to_string (GListModel *model)
+{
+ GString *string = g_string_new (NULL);
+ guint i, s, e, n;
+
+ if (!GTK_IS_SECTION_MODEL (model))
+ return model_to_string (model);
+
+ n = g_list_model_get_n_items (model);
+
+ i = 0;
+ while (i < n)
+ {
+ gtk_section_model_get_section (GTK_SECTION_MODEL (model), i, &s, &e);
+
+ if (i > 0)
+ g_string_append (string, " ");
+
+ g_string_append (string, "[");
+
+ for (; i < e; i++)
+ {
+ if (i > s)
+ g_string_append (string, " ");
+
+ g_string_append_printf (string, "%u", get (model, i));
+ }
+
+ g_string_append (string, "]");
+ i = e;
+ }
+
+ return g_string_free (string, FALSE);
+}
+
static char *
selection_to_string (GListModel *model)
{
g_free (s); \
}G_STMT_END
+#define assert_section_model(model, expected) G_STMT_START{ \
+ char *s = section_model_to_string (G_LIST_MODEL (model)); \
+ if (!g_str_equal (s, expected)) \
+ g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+ #model " == " #expected, s, "==", expected); \
+ g_free (s); \
+}G_STMT_END
+
#define ignore_changes(model) G_STMT_START{ \
GString *changes = g_object_get_qdata (G_OBJECT (model), changes_quark); \
g_string_set_size (changes, 0); \
}
}
+static void
+sections_changed (GListModel *model,
+ guint position,
+ guint n_items,
+ GString *changes)
+{
+ g_assert_true (n_items != 0);
+
+ if (changes->len)
+ g_string_append (changes, ", ");
+
+ g_string_append_printf (changes, "s%u:%u", position, n_items);
+}
+
static void
notify_n_items (GObject *object,
GParamSpec *pspec,
}
static GtkSelectionModel *
-new_model (GListStore *store)
+new_model (GListModel *store)
{
GtkSelectionModel *result;
GString *changes;
- result = GTK_SELECTION_MODEL (gtk_multi_selection_new (g_object_ref (G_LIST_MODEL (store))));
+ result = GTK_SELECTION_MODEL (gtk_multi_selection_new (g_object_ref (store)));
changes = g_string_new ("");
- g_object_set_qdata_full (G_OBJECT(result), changes_quark, changes, free_changes);
+ g_object_set_qdata_full (G_OBJECT (result), changes_quark, changes, free_changes);
g_signal_connect (result, "items-changed", G_CALLBACK (items_changed), changes);
+ g_signal_connect (result, "sections-changed", G_CALLBACK (sections_changed), changes);
g_signal_connect (result, "notify::n-items", G_CALLBACK (notify_n_items), changes);
changes = g_string_new ("");
guint start, end;
store = new_store (1, 5, 2);
- selection = new_model (store);
+ selection = new_model (G_LIST_MODEL (store));
assert_model (selection, "1 3 5");
assert_changes (selection, "");
gboolean ret;
store = new_store (1, 5, 1);
- selection = new_model (store);
+ selection = new_model (G_LIST_MODEL (store));
assert_model (selection, "1 2 3 4 5");
assert_changes (selection, "");
assert_selection (selection, "");
gboolean ret;
store = new_store (1, 5, 1);
- selection = new_model (store);
+ selection = new_model (G_LIST_MODEL (store));
assert_selection (selection, "");
assert_selection_changes (selection, "");
gboolean ret;
store = new_store (1, 5, 1);
- selection = new_model (store);
+ selection = new_model (G_LIST_MODEL (store));
assert_selection (selection, "");
assert_selection_changes (selection, "");
store = new_store (1, 5, 1);
- selection = new_model (store);
+ selection = new_model (G_LIST_MODEL (store));
assert_model (selection, "1 2 3 4 5");
assert_selection (selection, "");
assert_selection_changes (selection, "");
store = new_store (1, 10, 1);
- selection = new_model (store);
+ selection = new_model (G_LIST_MODEL (store));
assert_model (selection, "1 2 3 4 5 6 7 8 9 10");
assert_selection (selection, "");
assert_selection_changes (selection, "");
gboolean ret;
store = new_store (1, 5, 1);
- selection = new_model (store);
+ selection = new_model (G_LIST_MODEL (store));
assert_selection (selection, "");
assert_selection_changes (selection, "");
store = new_store (1, 5, 1);
m1 = G_LIST_MODEL (store);
m2 = G_LIST_MODEL (gtk_slice_list_model_new (g_object_ref (m1), 0, 3));
- selection = new_model (store);
+ selection = new_model (G_LIST_MODEL (store));
assert_selection (selection, "");
assert_selection_changes (selection, "");
g_object_unref (selection);
}
+static int
+by_n (gconstpointer p1,
+ gconstpointer p2,
+ gpointer data)
+{
+ guint n1 = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (p1), number_quark));
+ guint n2 = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (p2), number_quark));
+ unsigned int n = GPOINTER_TO_UINT (data);
+
+ n1 = n1 / n;
+ n2 = n2 / n;
+
+ if (n1 < n2)
+ return -1;
+ else if (n1 > n2)
+ return 1;
+ else
+ return 0;
+}
+
+static int
+compare (gconstpointer first,
+ gconstpointer second,
+ gpointer unused)
+{
+ return GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (first), number_quark))
+ - GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (second), number_quark));
+}
+
+static void
+test_sections (void)
+{
+ GListStore *store;
+ GtkSortListModel *sorted;
+ GtkSelectionModel *selection;
+ GtkSorter *sorter;
+
+ store = new_store (1, 10, 1);
+ sorted = gtk_sort_list_model_new (G_LIST_MODEL (store),
+ GTK_SORTER (gtk_custom_sorter_new (compare, NULL, NULL)));
+ selection = new_model (G_LIST_MODEL (sorted));
+ assert_model (selection, "1 2 3 4 5 6 7 8 9 10");
+ assert_section_model (selection, "[1 2 3 4 5 6 7 8 9 10]");
+ assert_changes (selection, "");
+
+ sorter = GTK_SORTER (gtk_custom_sorter_new (by_n, GUINT_TO_POINTER (3), NULL));
+ gtk_sort_list_model_set_section_sorter (sorted, sorter);
+ g_object_unref (sorter);
+
+ assert_section_model (selection, "[1 2] [3 4 5] [6 7 8] [9 10]");
+ assert_changes (selection, "s0:10");
+
+ gtk_section_model_sections_changed (GTK_SECTION_MODEL (sorted), 0, 3);
+ assert_changes (selection, "s0:3");
+
+ gtk_section_model_sections_changed (GTK_SECTION_MODEL (sorted), 5, 3);
+ assert_changes (selection, "s5:3");
+
+ g_object_unref (selection);
+}
+
int
main (int argc, char *argv[])
{
g_test_add_func ("/multiselection/set-model", test_set_model);
g_test_add_func ("/multiselection/empty", test_empty);
g_test_add_func ("/multiselection/selection-filter/empty", test_empty_filter);
+ g_test_add_func ("/multiselection/sections", test_sections);
return g_test_run ();
}