[gtk/prop-list: 35/96] columnviewcolumn: Add a fixed-width property



commit 640f165a8156ec4340c2198a09b78cfbc630f53e
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Dec 17 22:58:49 2019 -0500

    columnviewcolumn: Add a fixed-width property
    
    When fixed-width is set to a value > -1, we make the
    measure function of cell and title widgets return this
    width, and we set overflow to hidden.

 docs/reference/gtk/gtk4-sections.txt |  2 +
 gtk/gtkcolumnviewcell.c              |  8 ++++
 gtk/gtkcolumnviewcolumn.c            | 92 ++++++++++++++++++++++++++++++++++++
 gtk/gtkcolumnviewcolumn.h            |  6 +++
 gtk/gtkcolumnviewtitle.c             |  8 ++++
 5 files changed, 116 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index ac5b7bc775..84bded825a 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -628,6 +628,8 @@ gtk_column_view_column_set_sorter
 gtk_column_view_column_get_sorter
 gtk_column_view_column_set_visible
 gtk_column_view_column_get_visible
+gtk_column_view_column_set_fixed_width
+gtk_column_view_column_get_fixed_width
 <SUBSECTION Standard>
 GTK_COLUMN_VIEW_COLUMN
 GTK_COLUMN_VIEW_COLUMN_CLASS
diff --git a/gtk/gtkcolumnviewcell.c b/gtk/gtkcolumnviewcell.c
index 4df02a0194..4088222359 100644
--- a/gtk/gtkcolumnviewcell.c
+++ b/gtk/gtkcolumnviewcell.c
@@ -53,10 +53,18 @@ gtk_column_view_cell_measure (GtkWidget      *widget,
                               int            *minimum_baseline,
                               int            *natural_baseline)
 {
+  GtkColumnViewCell *cell = GTK_COLUMN_VIEW_CELL (widget);
   GtkWidget *child = gtk_widget_get_first_child (widget);
 
   if (child)
     gtk_widget_measure (child, orientation, for_size, minimum, natural, minimum_baseline, natural_baseline);
+
+  if (orientation == GTK_ORIENTATION_HORIZONTAL)
+    {
+      int fixed_width = gtk_column_view_column_get_fixed_width (cell->column);
+      if (fixed_width > -1)
+        *minimum = *natural = fixed_width;
+    }
 }
 
 static void
diff --git a/gtk/gtkcolumnviewcolumn.c b/gtk/gtkcolumnviewcolumn.c
index 7111d0c35f..a48df79046 100644
--- a/gtk/gtkcolumnviewcolumn.c
+++ b/gtk/gtkcolumnviewcolumn.c
@@ -61,6 +61,8 @@ struct _GtkColumnViewColumn
   int allocation_offset;
   int allocation_size;
 
+  int fixed_width;
+
   gboolean visible;
 
   /* This list isn't sorted - this is just caching for performance */
@@ -80,6 +82,7 @@ enum
   PROP_TITLE,
   PROP_SORTER,
   PROP_VISIBLE,
+  PROP_FIXED_WIDTH,
 
   N_PROPS
 };
@@ -133,6 +136,10 @@ gtk_column_view_column_get_property (GObject    *object,
       g_value_set_boolean (value, self->visible);
       break;
 
+    case PROP_FIXED_WIDTH:
+      g_value_set_int (value, self->fixed_width);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -165,6 +172,10 @@ gtk_column_view_column_set_property (GObject      *object,
       gtk_column_view_column_set_visible (self, g_value_get_boolean (value));
       break;
 
+    case PROP_FIXED_WIDTH:
+      gtk_column_view_column_set_fixed_width (self, g_value_get_int (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -235,6 +246,19 @@ gtk_column_view_column_class_init (GtkColumnViewColumnClass *klass)
                           TRUE,
                           G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
+  /**
+   * GtkColumnViewColumn:fixed-width:
+   *
+   * If not -1, this is the width that the column is allocated,
+   * regardless of the size of its content.
+   */
+  properties[PROP_FIXED_WIDTH] =
+    g_param_spec_int ("fixed-width",
+                      P_("Fixed width"),
+                      P_("Fixed width of this column"),
+                      -1, G_MAXINT, -1,
+                      G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
   g_object_class_install_properties (gobject_class, N_PROPS, properties);
 }
 
@@ -244,6 +268,7 @@ gtk_column_view_column_init (GtkColumnViewColumn *self)
   self->minimum_size_request = -1;
   self->natural_size_request = -1;
   self->visible = TRUE;
+  self->fixed_width = -1;
 }
 
 /**
@@ -352,6 +377,12 @@ gtk_column_view_column_measure (GtkColumnViewColumn *self,
                                 int                 *minimum,
                                 int                 *natural)
 {
+  if (self->fixed_width > -1)
+    {
+      self->minimum_size_request  = self->fixed_width;
+      self->natural_size_request  = self->fixed_width;
+    }
+
   if (self->minimum_size_request < 0)
     {
       GtkColumnViewCell *cell;
@@ -723,4 +754,65 @@ gtk_column_view_column_get_visible (GtkColumnViewColumn *self)
   return self->visible;
 }
 
+/**
+ * gtk_column_view_column_set_fixed_width:
+ * @self: a #GtkColumnViewColumn
+ * @fixed_width: the new fixed width, or -1
+ *
+ * If @fixed_width is not -1, sets the fixed width of @column; otherwise
+ * unsets it.
+ *
+ * Setting a fixed width overrides the automatically calculated width.
+ * Interactive resizing also sets the “fixed-width” property.
+ */
+void
+gtk_column_view_column_set_fixed_width (GtkColumnViewColumn *self,
+                                        int                  fixed_width)
+{
+  GtkOverflow overflow;
+  GtkColumnViewCell *cell;
+
+  g_return_if_fail (GTK_IS_COLUMN_VIEW_COLUMN (self));
+  g_return_if_fail (fixed_width >= -1);
+
+  if (self->fixed_width == fixed_width)
+    return;
 
+  self->fixed_width = fixed_width;
+
+  if (fixed_width > -1)
+    overflow = GTK_OVERFLOW_HIDDEN;
+  else
+    overflow = GTK_OVERFLOW_VISIBLE;
+
+  if (overflow != gtk_widget_get_overflow (GTK_WIDGET (self->header)))
+    {
+      if (self->header)
+        gtk_widget_set_overflow (GTK_WIDGET (self->header), overflow);
+
+      for (cell = self->first_cell; cell; cell = gtk_column_view_cell_get_next (cell))
+        {
+          gtk_widget_set_overflow (GTK_WIDGET (cell), overflow);
+        }
+    }
+
+  gtk_column_view_column_queue_resize (self);
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FIXED_WIDTH]);
+}
+
+/**
+ * gtk_column_view_column_get_fixed_width:
+ * @self: a #GtkColumnViewColumn
+ *
+ * Gets the fixed width of the column.
+ *
+ * Returns: the fixed with of the column
+ */
+int
+gtk_column_view_column_get_fixed_width (GtkColumnViewColumn *self)
+{
+  g_return_val_if_fail (GTK_IS_COLUMN_VIEW_COLUMN (self), -1);
+
+  return self->fixed_width;
+}
diff --git a/gtk/gtkcolumnviewcolumn.h b/gtk/gtkcolumnviewcolumn.h
index f0005809be..32392612b2 100644
--- a/gtk/gtkcolumnviewcolumn.h
+++ b/gtk/gtkcolumnviewcolumn.h
@@ -78,6 +78,12 @@ void                    gtk_column_view_column_set_visible              (GtkColu
 GDK_AVAILABLE_IN_ALL
 gboolean                gtk_column_view_column_get_visible              (GtkColumnViewColumn    *self);
 
+GDK_AVAILABLE_IN_ALL
+void                    gtk_column_view_column_set_fixed_width          (GtkColumnViewColumn    *self,
+                                                                         int                     
fixed_width);
+GDK_AVAILABLE_IN_ALL
+int                     gtk_column_view_column_get_fixed_width          (GtkColumnViewColumn    *self);
+
 G_END_DECLS
 
 #endif  /* __GTK_COLUMN_VIEW_COLUMN_H__ */
diff --git a/gtk/gtkcolumnviewtitle.c b/gtk/gtkcolumnviewtitle.c
index c6ea7c9402..edab3c0d30 100644
--- a/gtk/gtkcolumnviewtitle.c
+++ b/gtk/gtkcolumnviewtitle.c
@@ -58,10 +58,18 @@ gtk_column_view_title_measure (GtkWidget      *widget,
                                int            *minimum_baseline,
                                int            *natural_baseline)
 {
+  GtkColumnViewTitle *self = GTK_COLUMN_VIEW_TITLE (widget);
   GtkWidget *child = gtk_widget_get_first_child (widget);
 
   if (child)
     gtk_widget_measure (child, orientation, for_size, minimum, natural, minimum_baseline, natural_baseline);
+
+  if (orientation == GTK_ORIENTATION_HORIZONTAL)
+    {
+      int fixed_width = gtk_column_view_column_get_fixed_width (self->column);
+      if (fixed_width > -1)
+        *minimum = *natural = fixed_width;
+    }
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]