[gtk/prop-list: 22/70] columnview: Add a sort-by api
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/prop-list: 22/70] columnview: Add a sort-by api
- Date: Sun, 22 Dec 2019 03:25:19 +0000 (UTC)
commit 5b015e32cd5ce59cca3255392e8040ffe5a77610
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Dec 3 21:31:57 2019 -0500
columnview: Add a sort-by api
docs/reference/gtk/gtk4-sections.txt | 1 +
gtk/gtkcolumnview.c | 33 +++++++++++++++++++++++++++
gtk/gtkcolumnview.h | 5 +++++
gtk/gtkcolumnviewsorter.c | 43 +++++++++++++++++++++++++++++++++++-
gtk/gtkcolumnviewsorterprivate.h | 4 ++++
5 files changed, 85 insertions(+), 1 deletion(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index fdf7cadeb4..83d6dae1ea 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -599,6 +599,7 @@ gtk_column_view_set_model
gtk_column_view_get_sorter
gtk_column_view_get_show_separators
gtk_column_view_set_show_separators
+gtk_column_view_sort_by_column
<SUBSECTION Standard>
GTK_COLUMN_VIEW
GTK_COLUMN_VIEW_CLASS
diff --git a/gtk/gtkcolumnview.c b/gtk/gtkcolumnview.c
index 9d3bf91299..32a2bea66c 100644
--- a/gtk/gtkcolumnview.c
+++ b/gtk/gtkcolumnview.c
@@ -736,3 +736,36 @@ gtk_column_view_get_sorter (GtkColumnView *self)
return self->sorter;
}
+/**
+ * gtk_column_view_sort_by_column:
+ * @self: a #GtkColumnView
+ * @column: (allow-none): the #GtkColumnViewColumn to sort by, or %NULL
+ * @direction: the direction to sort in
+ *
+ * Sets the sorting of the view.
+ *
+ * This function should be used to set up the initial sorting. At runtime,
+ * users can change the sorting of a column view by clicking on the list headers.
+ *
+ * This call only has an effect if the sorter returned by gtk_column_view_get_sorter()
+ * is set on a sort model, and gtk_column_view_column_set_sorter() has been called
+ * on @column to associate a sorter with the column.
+ *
+ * If @column is %NULL, the view will be unsorted.
+ */
+void
+gtk_column_view_sort_by_column (GtkColumnView *self,
+ GtkColumnViewColumn *column,
+ GtkSortType direction)
+{
+ g_return_if_fail (GTK_IS_COLUMN_VIEW (self));
+ g_return_if_fail (column == NULL || GTK_IS_COLUMN_VIEW_COLUMN (column));
+ g_return_if_fail (column == NULL || gtk_column_view_column_get_column_view (column) == self);
+
+ if (column == NULL)
+ gtk_column_view_sorter_clear (GTK_COLUMN_VIEW_SORTER (self->sorter));
+ else
+ gtk_column_view_sorter_set_column (GTK_COLUMN_VIEW_SORTER (self->sorter),
+ column,
+ direction == GTK_SORT_DESCENDING);
+}
diff --git a/gtk/gtkcolumnview.h b/gtk/gtkcolumnview.h
index 6ec91319e8..fd0e99a4d3 100644
--- a/gtk/gtkcolumnview.h
+++ b/gtk/gtkcolumnview.h
@@ -77,6 +77,11 @@ void gtk_column_view_set_show_separators (GtkColumnView
GDK_AVAILABLE_IN_ALL
GtkSorter * gtk_column_view_get_sorter (GtkColumnView *self);
+GDK_AVAILABLE_IN_ALL
+void gtk_column_view_sort_by_column (GtkColumnView *self,
+ GtkColumnViewColumn *column,
+ GtkSortType direction);
+
G_END_DECLS
#endif /* __GTK_COLUMN_VIEW_H__ */
diff --git a/gtk/gtkcolumnviewsorter.c b/gtk/gtkcolumnviewsorter.c
index d431de0cb7..587731e1cd 100644
--- a/gtk/gtkcolumnviewsorter.c
+++ b/gtk/gtkcolumnviewsorter.c
@@ -239,6 +239,43 @@ gtk_column_view_sorter_remove_column (GtkColumnViewSorter *self,
return FALSE;
}
+gboolean
+gtk_column_view_sorter_set_column (GtkColumnViewSorter *self,
+ GtkColumnViewColumn *column,
+ gboolean inverted)
+{
+ GtkSorter *sorter;
+ Sorter *s;
+
+ g_return_val_if_fail (GTK_IS_COLUMN_VIEW_SORTER (self), FALSE);
+ g_return_val_if_fail (GTK_IS_COLUMN_VIEW_COLUMN (column), FALSE);
+
+ sorter = gtk_column_view_column_get_sorter (column);
+ if (sorter == NULL)
+ return FALSE;
+
+ g_object_ref (column);
+
+ g_sequence_remove_range (g_sequence_get_begin_iter (self->sorters),
+ g_sequence_get_end_iter (self->sorters));
+
+ s = g_new (Sorter, 1);
+ s->column = g_object_ref (column);
+ s->sorter = g_object_ref (sorter);
+ s->changed_id = g_signal_connect (sorter, "changed", G_CALLBACK (gtk_column_view_sorter_changed_cb), self);
+ s->inverted = inverted;
+
+ g_sequence_prepend (self->sorters, s);
+
+ gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_DIFFERENT);
+
+ gtk_column_view_column_notify_sort (column);
+
+ g_object_unref (column);
+
+ return TRUE;
+}
+
void
gtk_column_view_sorter_clear (GtkColumnViewSorter *self)
{
@@ -253,12 +290,16 @@ gtk_column_view_sorter_clear (GtkColumnViewSorter *self)
iter = g_sequence_get_begin_iter (self->sorters);
s = g_sequence_get (iter);
- column = s->column;
+
+ column = g_object_ref (s->column);
+
g_sequence_remove_range (iter, g_sequence_get_end_iter (self->sorters));
gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_DIFFERENT);
gtk_column_view_column_notify_sort (column);
+
+ g_object_unref (column);
}
GtkColumnViewColumn *
diff --git a/gtk/gtkcolumnviewsorterprivate.h b/gtk/gtkcolumnviewsorterprivate.h
index ce89952170..c81c565252 100644
--- a/gtk/gtkcolumnviewsorterprivate.h
+++ b/gtk/gtkcolumnviewsorterprivate.h
@@ -46,6 +46,10 @@ void gtk_column_view_sorter_clear (GtkColumnViewSo
GtkColumnViewColumn * gtk_column_view_sorter_get_sort_column (GtkColumnViewSorter *self,
gboolean *inverted);
+gboolean gtk_column_view_sorter_set_column (GtkColumnViewSorter *self,
+ GtkColumnViewColumn *column,
+ gboolean inverted);
+
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]