2019-11-08 20:23:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2019 Benjamin Otte
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors: Benjamin Otte <otte@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkcolumnviewtitleprivate.h"
|
|
|
|
|
2019-12-04 13:52:12 +00:00
|
|
|
#include "gtkcolumnviewprivate.h"
|
2019-11-08 20:23:03 +00:00
|
|
|
#include "gtkcolumnviewcolumnprivate.h"
|
2019-12-04 13:52:12 +00:00
|
|
|
#include "gtkcolumnviewsorterprivate.h"
|
2019-11-08 20:23:03 +00:00
|
|
|
#include "gtkintl.h"
|
|
|
|
#include "gtklabel.h"
|
|
|
|
#include "gtkwidgetprivate.h"
|
2019-12-04 13:52:12 +00:00
|
|
|
#include "gtkbox.h"
|
2021-01-19 19:24:19 +00:00
|
|
|
#include "gtkbuiltiniconprivate.h"
|
2019-12-04 13:52:12 +00:00
|
|
|
#include "gtkgestureclick.h"
|
2019-12-22 03:21:27 +00:00
|
|
|
#include "gtkpopovermenu.h"
|
|
|
|
#include "gtknative.h"
|
2020-09-24 18:18:38 +00:00
|
|
|
#include "gtkcssnodeprivate.h"
|
|
|
|
#include "gtkcssnumbervalueprivate.h"
|
2019-11-08 20:23:03 +00:00
|
|
|
|
|
|
|
struct _GtkColumnViewTitle
|
|
|
|
{
|
|
|
|
GtkWidget parent_instance;
|
|
|
|
|
|
|
|
GtkColumnViewColumn *column;
|
|
|
|
|
2019-12-04 13:52:12 +00:00
|
|
|
GtkWidget *box;
|
2019-11-08 20:23:03 +00:00
|
|
|
GtkWidget *title;
|
2019-12-04 13:52:12 +00:00
|
|
|
GtkWidget *sort;
|
2019-12-22 03:21:27 +00:00
|
|
|
GtkWidget *popup_menu;
|
2019-11-08 20:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkColumnViewTitleClass
|
|
|
|
{
|
|
|
|
GtkWidgetClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkColumnViewTitle, gtk_column_view_title, GTK_TYPE_WIDGET)
|
|
|
|
|
2020-09-24 18:18:38 +00:00
|
|
|
static int
|
|
|
|
get_number (GtkCssValue *value)
|
|
|
|
{
|
|
|
|
double d = _gtk_css_number_value_get (value, 100);
|
|
|
|
|
|
|
|
if (d < 1)
|
|
|
|
return ceil (d);
|
|
|
|
else
|
|
|
|
return floor (d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
unadjust_width (GtkWidget *widget,
|
|
|
|
int width)
|
|
|
|
{
|
|
|
|
GtkCssStyle *style;
|
|
|
|
int widget_margins;
|
|
|
|
int css_extra;
|
|
|
|
|
|
|
|
style = gtk_css_node_get_style (gtk_widget_get_css_node (widget));
|
|
|
|
css_extra = get_number (style->size->margin_left) +
|
|
|
|
get_number (style->size->margin_right) +
|
|
|
|
get_number (style->border->border_left_width) +
|
|
|
|
get_number (style->border->border_right_width) +
|
|
|
|
get_number (style->size->padding_left) +
|
|
|
|
get_number (style->size->padding_right);
|
|
|
|
widget_margins = widget->priv->margin.left + widget->priv->margin.right;
|
|
|
|
|
|
|
|
return MAX (0, width - widget_margins - css_extra);
|
|
|
|
}
|
|
|
|
|
2019-11-08 20:23:03 +00:00
|
|
|
static void
|
|
|
|
gtk_column_view_title_measure (GtkWidget *widget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline)
|
|
|
|
{
|
2020-06-01 01:07:38 +00:00
|
|
|
GtkColumnViewTitle *self = GTK_COLUMN_VIEW_TITLE (widget);
|
2019-11-08 20:23:03 +00:00
|
|
|
GtkWidget *child = gtk_widget_get_first_child (widget);
|
2020-06-05 13:37:59 +00:00
|
|
|
int fixed_width = gtk_column_view_column_get_fixed_width (self->column);
|
2020-09-24 18:18:38 +00:00
|
|
|
int unadj_width;
|
|
|
|
|
|
|
|
unadj_width = unadjust_width (widget, fixed_width);
|
2020-06-05 13:37:59 +00:00
|
|
|
|
|
|
|
if (orientation == GTK_ORIENTATION_VERTICAL)
|
|
|
|
{
|
|
|
|
if (fixed_width > -1)
|
|
|
|
{
|
|
|
|
if (for_size == -1)
|
2020-09-24 18:18:38 +00:00
|
|
|
for_size = unadj_width;
|
2020-06-05 13:37:59 +00:00
|
|
|
else
|
2020-09-24 18:18:38 +00:00
|
|
|
for_size = MIN (for_size, unadj_width);
|
2020-06-05 13:37:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 20:23:03 +00:00
|
|
|
|
|
|
|
if (child)
|
|
|
|
gtk_widget_measure (child, orientation, for_size, minimum, natural, minimum_baseline, natural_baseline);
|
2020-06-01 01:07:38 +00:00
|
|
|
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
{
|
|
|
|
if (fixed_width > -1)
|
2020-09-24 18:18:38 +00:00
|
|
|
{
|
|
|
|
*minimum = 0;
|
|
|
|
*natural = unadj_width;
|
|
|
|
}
|
2020-06-01 01:07:38 +00:00
|
|
|
}
|
2019-11-08 20:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_column_view_title_size_allocate (GtkWidget *widget,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int baseline)
|
|
|
|
{
|
2019-12-22 03:21:27 +00:00
|
|
|
GtkColumnViewTitle *self = GTK_COLUMN_VIEW_TITLE (widget);
|
2019-11-08 20:23:03 +00:00
|
|
|
GtkWidget *child = gtk_widget_get_first_child (widget);
|
|
|
|
|
|
|
|
if (child)
|
2020-09-24 18:18:38 +00:00
|
|
|
{
|
2020-10-19 12:42:33 +00:00
|
|
|
int min;
|
2020-09-24 18:18:38 +00:00
|
|
|
|
2020-10-19 12:42:33 +00:00
|
|
|
gtk_widget_measure (child, GTK_ORIENTATION_HORIZONTAL, height, &min, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
gtk_widget_allocate (child, MAX (min, width), height, baseline, NULL);
|
2020-09-24 18:18:38 +00:00
|
|
|
}
|
2019-12-22 03:21:27 +00:00
|
|
|
|
|
|
|
if (self->popup_menu)
|
2020-12-04 08:20:50 +00:00
|
|
|
gtk_popover_present (GTK_POPOVER (self->popup_menu));
|
2019-11-08 20:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_column_view_title_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkColumnViewTitle *self = GTK_COLUMN_VIEW_TITLE (object);
|
|
|
|
|
2019-12-04 13:52:12 +00:00
|
|
|
g_clear_pointer (&self->box, gtk_widget_unparent);
|
2019-12-22 03:21:27 +00:00
|
|
|
g_clear_pointer (&self->popup_menu, gtk_widget_unparent);
|
2019-11-08 20:23:03 +00:00
|
|
|
|
|
|
|
g_clear_object (&self->column);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_column_view_title_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
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_css_name (widget_class, I_("button"));
|
2020-10-15 04:09:06 +00:00
|
|
|
gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_COLUMN_HEADER);
|
2019-11-08 20:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_column_view_title_resize_func (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GtkColumnViewTitle *self = GTK_COLUMN_VIEW_TITLE (widget);
|
|
|
|
|
|
|
|
if (self->column)
|
|
|
|
gtk_column_view_column_queue_resize (self->column);
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:52:12 +00:00
|
|
|
static void
|
2019-12-22 03:21:27 +00:00
|
|
|
activate_sort (GtkColumnViewTitle *self)
|
2019-12-04 13:52:12 +00:00
|
|
|
{
|
|
|
|
GtkSorter *sorter;
|
|
|
|
GtkColumnView *view;
|
|
|
|
GtkColumnViewSorter *view_sorter;
|
|
|
|
|
|
|
|
sorter = gtk_column_view_column_get_sorter (self->column);
|
|
|
|
if (sorter == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
view = gtk_column_view_column_get_column_view (self->column);
|
|
|
|
view_sorter = GTK_COLUMN_VIEW_SORTER (gtk_column_view_get_sorter (view));
|
|
|
|
gtk_column_view_sorter_add_column (view_sorter, self->column);
|
|
|
|
}
|
|
|
|
|
2019-12-22 03:21:27 +00:00
|
|
|
static void
|
|
|
|
show_menu (GtkColumnViewTitle *self,
|
|
|
|
double x,
|
|
|
|
double y)
|
|
|
|
{
|
|
|
|
if (!self->popup_menu)
|
|
|
|
{
|
|
|
|
GMenuModel *model;
|
|
|
|
|
|
|
|
model = gtk_column_view_column_get_header_menu (self->column);
|
|
|
|
if (!model)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self->popup_menu = gtk_popover_menu_new_from_model (model);
|
|
|
|
gtk_widget_set_parent (self->popup_menu, GTK_WIDGET (self));
|
|
|
|
gtk_popover_set_position (GTK_POPOVER (self->popup_menu), GTK_POS_BOTTOM);
|
|
|
|
|
|
|
|
gtk_popover_set_has_arrow (GTK_POPOVER (self->popup_menu), FALSE);
|
|
|
|
gtk_widget_set_halign (self->popup_menu, GTK_ALIGN_START);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x != -1 && y != -1)
|
|
|
|
{
|
|
|
|
GdkRectangle rect = { x, y, 1, 1 };
|
|
|
|
gtk_popover_set_pointing_to (GTK_POPOVER (self->popup_menu), &rect);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
gtk_popover_set_pointing_to (GTK_POPOVER (self->popup_menu), NULL);
|
|
|
|
|
|
|
|
gtk_popover_popup (GTK_POPOVER (self->popup_menu));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
click_released_cb (GtkGestureClick *gesture,
|
|
|
|
guint n_press,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GtkColumnViewTitle *self = GTK_COLUMN_VIEW_TITLE (widget);
|
|
|
|
guint button;
|
|
|
|
|
|
|
|
button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));
|
|
|
|
|
|
|
|
if (button == GDK_BUTTON_PRIMARY)
|
|
|
|
activate_sort (self);
|
|
|
|
else if (button == GDK_BUTTON_SECONDARY)
|
|
|
|
show_menu (self, x, y);
|
|
|
|
}
|
|
|
|
|
2019-11-08 20:23:03 +00:00
|
|
|
static void
|
|
|
|
gtk_column_view_title_init (GtkColumnViewTitle *self)
|
|
|
|
{
|
|
|
|
GtkWidget *widget = GTK_WIDGET (self);
|
2019-12-04 13:52:12 +00:00
|
|
|
GtkGesture *gesture;
|
2019-11-08 20:23:03 +00:00
|
|
|
|
|
|
|
widget->priv->resize_func = gtk_column_view_title_resize_func;
|
|
|
|
|
2020-09-24 18:30:40 +00:00
|
|
|
gtk_widget_set_overflow (widget, GTK_OVERFLOW_HIDDEN);
|
|
|
|
|
2019-12-04 13:52:12 +00:00
|
|
|
self->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
gtk_widget_set_parent (self->box, widget);
|
|
|
|
|
2019-11-08 20:23:03 +00:00
|
|
|
self->title = gtk_label_new (NULL);
|
2019-12-04 13:52:12 +00:00
|
|
|
gtk_box_append (GTK_BOX (self->box), self->title);
|
|
|
|
|
2021-01-19 19:24:19 +00:00
|
|
|
self->sort = gtk_builtin_icon_new ("sort-indicator");
|
2019-12-04 13:52:12 +00:00
|
|
|
gtk_box_append (GTK_BOX (self->box), self->sort);
|
|
|
|
|
|
|
|
gesture = gtk_gesture_click_new ();
|
2019-12-22 03:21:27 +00:00
|
|
|
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (gesture), 0);
|
|
|
|
g_signal_connect (gesture, "released", G_CALLBACK (click_released_cb), self);
|
2019-12-04 13:52:12 +00:00
|
|
|
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (gesture));
|
2019-11-08 20:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
gtk_column_view_title_new (GtkColumnViewColumn *column)
|
|
|
|
{
|
|
|
|
GtkColumnViewTitle *title;
|
|
|
|
|
2019-12-04 13:52:12 +00:00
|
|
|
title = g_object_new (GTK_TYPE_COLUMN_VIEW_TITLE, NULL);
|
2019-11-08 20:23:03 +00:00
|
|
|
|
|
|
|
title->column = g_object_ref (column);
|
|
|
|
gtk_column_view_title_update (title);
|
|
|
|
|
|
|
|
return GTK_WIDGET (title);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_column_view_title_update (GtkColumnViewTitle *self)
|
|
|
|
{
|
2019-12-04 13:52:12 +00:00
|
|
|
GtkSorter *sorter;
|
|
|
|
GtkColumnView *view;
|
|
|
|
GtkColumnViewSorter *view_sorter;
|
|
|
|
gboolean inverted;
|
|
|
|
GtkColumnViewColumn *active;
|
|
|
|
|
2019-11-08 20:23:03 +00:00
|
|
|
gtk_label_set_label (GTK_LABEL (self->title), gtk_column_view_column_get_title (self->column));
|
2019-12-04 13:52:12 +00:00
|
|
|
|
|
|
|
sorter = gtk_column_view_column_get_sorter (self->column);
|
|
|
|
|
|
|
|
if (sorter)
|
|
|
|
{
|
|
|
|
view = gtk_column_view_column_get_column_view (self->column);
|
|
|
|
view_sorter = GTK_COLUMN_VIEW_SORTER (gtk_column_view_get_sorter (view));
|
|
|
|
active = gtk_column_view_sorter_get_sort_column (view_sorter, &inverted);
|
|
|
|
|
|
|
|
gtk_widget_show (self->sort);
|
2021-01-19 19:24:19 +00:00
|
|
|
gtk_widget_remove_css_class (self->sort, "ascending");
|
|
|
|
gtk_widget_remove_css_class (self->sort, "descending");
|
|
|
|
gtk_widget_remove_css_class (self->sort, "unsorted");
|
|
|
|
if (self->column != active)
|
|
|
|
gtk_widget_add_css_class (self->sort, "unsorted");
|
|
|
|
else if (inverted)
|
|
|
|
gtk_widget_add_css_class (self->sort, "descending");
|
2019-12-04 13:52:12 +00:00
|
|
|
else
|
2021-01-19 19:24:19 +00:00
|
|
|
gtk_widget_add_css_class (self->sort, "ascending");
|
2019-12-04 13:52:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
gtk_widget_hide (self->sort);
|
2019-12-22 03:21:27 +00:00
|
|
|
|
|
|
|
g_clear_pointer (&self->popup_menu, gtk_widget_unparent);
|
2019-11-08 20:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkColumnViewColumn *
|
|
|
|
gtk_column_view_title_get_column (GtkColumnViewTitle *self)
|
|
|
|
{
|
|
|
|
return self->column;
|
|
|
|
}
|