[gtk/prop-list] column view title: Add sort indicators
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/prop-list] column view title: Add sort indicators
- Date: Tue, 3 Dec 2019 07:28:39 +0000 (UTC)
commit 3d0374effcde2ed86fc629b7d2a27250000b39a3
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Dec 3 02:26:33 2019 -0500
column view title: Add sort indicators
And make the titles clickable to change sort.
gtk/gtkcolumnviewtitle.c | 102 +++++++++++++++++++++++++++++++++--------------
1 file changed, 71 insertions(+), 31 deletions(-)
---
diff --git a/gtk/gtkcolumnviewtitle.c b/gtk/gtkcolumnviewtitle.c
index 7170018eb4..92ee78713e 100644
--- a/gtk/gtkcolumnviewtitle.c
+++ b/gtk/gtkcolumnviewtitle.c
@@ -25,6 +25,9 @@
#include "gtkintl.h"
#include "gtklabel.h"
#include "gtkwidgetprivate.h"
+#include "gtkboxlayout.h"
+#include "gtkimage.h"
+#include "gtkgestureclick.h"
struct _GtkColumnViewTitle
{
@@ -33,6 +36,7 @@ struct _GtkColumnViewTitle
GtkColumnViewColumn *column;
GtkWidget *title;
+ GtkWidget *sort;
};
struct _GtkColumnViewTitleClass
@@ -42,39 +46,14 @@ struct _GtkColumnViewTitleClass
G_DEFINE_TYPE (GtkColumnViewTitle, gtk_column_view_title, GTK_TYPE_WIDGET)
-static void
-gtk_column_view_title_measure (GtkWidget *widget,
- GtkOrientation orientation,
- int for_size,
- int *minimum,
- int *natural,
- int *minimum_baseline,
- int *natural_baseline)
-{
- GtkWidget *child = gtk_widget_get_first_child (widget);
-
- if (child)
- gtk_widget_measure (child, orientation, for_size, minimum, natural, minimum_baseline, natural_baseline);
-}
-
-static void
-gtk_column_view_title_size_allocate (GtkWidget *widget,
- int width,
- int height,
- int baseline)
-{
- GtkWidget *child = gtk_widget_get_first_child (widget);
-
- if (child)
- gtk_widget_allocate (child, width, height, baseline, NULL);
-}
-
static void
gtk_column_view_title_dispose (GObject *object)
{
GtkColumnViewTitle *self = GTK_COLUMN_VIEW_TITLE (object);
- g_clear_pointer(&self->title, gtk_widget_unparent);
+ g_signal_handlers_disconnect_by_func (self->column, gtk_column_view_title_update, self);
+ g_clear_pointer (&self->title, gtk_widget_unparent);
+ g_clear_pointer (&self->sort, gtk_widget_unparent);
g_clear_object (&self->column);
@@ -87,11 +66,9 @@ gtk_column_view_title_class_init (GtkColumnViewTitleClass *klass)
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
- widget_class->measure = gtk_column_view_title_measure;
- widget_class->size_allocate = gtk_column_view_title_size_allocate;
-
gobject_class->dispose = gtk_column_view_title_dispose;
+ gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
gtk_widget_class_set_css_name (widget_class, I_("button"));
}
@@ -104,15 +81,53 @@ gtk_column_view_title_resize_func (GtkWidget *widget)
gtk_column_view_column_queue_resize (self->column);
}
+static void
+click_pressed_cb (GtkGestureClick *gesture,
+ guint n_press,
+ gdouble x,
+ gdouble y,
+ GtkWidget *widget)
+{
+ GtkColumnViewTitle *self = GTK_COLUMN_VIEW_TITLE (widget);
+ GtkSorter *sorter;
+ GtkSorter *active_sorter;
+
+ sorter = gtk_column_view_column_get_sorter (self->column);
+ active_sorter = gtk_column_view_get_sorter (gtk_column_view_column_get_column_view (self->column));
+
+ if (sorter)
+ {
+ if (sorter == active_sorter)
+ {
+ gtk_sorter_set_sort_direction (sorter, 1 - gtk_sorter_get_sort_direction (sorter));
+ }
+ else
+ {
+ gtk_sorter_set_sort_direction (sorter, GTK_SORT_ASCENDING);
+ gtk_column_view_set_sorter (gtk_column_view_column_get_column_view (self->column), sorter);
+ }
+ }
+
+ gtk_column_view_title_update (self);
+}
+
static void
gtk_column_view_title_init (GtkColumnViewTitle *self)
{
GtkWidget *widget = GTK_WIDGET (self);
+ GtkGesture *gesture;
widget->priv->resize_func = gtk_column_view_title_resize_func;
self->title = gtk_label_new (NULL);
gtk_widget_set_parent (self->title, widget);
+
+ self->sort = gtk_image_new ();
+ gtk_widget_set_parent (self->sort, widget);
+
+ gesture = gtk_gesture_click_new ();
+ g_signal_connect (gesture, "pressed", G_CALLBACK (click_pressed_cb), self);
+ gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (gesture));
}
GtkWidget *
@@ -126,13 +141,38 @@ gtk_column_view_title_new (GtkColumnViewColumn *column)
title->column = g_object_ref (column);
gtk_column_view_title_update (title);
+ g_signal_connect_swapped (gtk_column_view_column_get_column_view (column), "notify::sorter",
+ G_CALLBACK (gtk_column_view_title_update), title);
+
return GTK_WIDGET (title);
}
void
gtk_column_view_title_update (GtkColumnViewTitle *self)
{
+ GtkSorter *sorter;
+ GtkSorter *active_sorter;
+
gtk_label_set_label (GTK_LABEL (self->title), gtk_column_view_column_get_title (self->column));
+
+ sorter = gtk_column_view_column_get_sorter (self->column);
+ active_sorter = gtk_column_view_get_sorter (gtk_column_view_column_get_column_view (self->column));
+
+ if (sorter)
+ {
+ gtk_widget_show (self->sort);
+ if (sorter == active_sorter)
+ {
+ if (gtk_sorter_get_sort_direction (sorter) == GTK_SORT_ASCENDING)
+ gtk_image_set_from_icon_name (GTK_IMAGE (self->sort), "pan-down-symbolic");
+ else
+ gtk_image_set_from_icon_name (GTK_IMAGE (self->sort), "pan-up-symbolic");
+ }
+ else
+ gtk_image_clear (GTK_IMAGE (self->sort));
+ }
+ else
+ gtk_widget_hide (self->sort);
}
GtkColumnViewColumn *
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]