Better column resizing for GtkTreeView
- From: John Lindgren <john lindgren aol com>
- To: gtk-devel-list gnome org
- Subject: Better column resizing for GtkTreeView
- Date: Sun, 13 Jan 2013 11:59:06 -0500
Hi all,
There are several problems with the current support for resizable
columns in GtkTreeView, which I have written some patches to fix:
* There is no way to set distinct minimum and natural widths for a
GtkTreeViewColumn. I propose that the "fixed-width" property be
interpreted as the natural width of the column rather than the minimum
width (we already have the "min-width" property for that).
* Column resizing is implemented using a hidden "resized-width" property
that is not visible to applications. Hence, user changes to column size
are lost every time the application is closed. I propose to get rid of
the "resized-width" property and instead modify the "fixed-width" and
"expand" properties to get the desired column size. Applications can
then preserve user-set column sizes by saving and restoring these two
properties.
* The code that currently handles the "resized-width" property is
convoluted and causes unexpected results when the size of the entire
treeview is changed (see [1]). After my patches, the code is simpler
and behaves as expected (see [2]).
Please look over the attached patches and comment.
John Lindgren
[1] screencast before (ogv): https://www.box.com/s/gz23ksylzjp046dn8ah5
[2] screencast after (ogv): https://www.box.com/s/c6zhov1igiqss7x97k5r
>From 9c8ee6c3a1977a9341455e6e5c18aff9d99e1e46 Mon Sep 17 00:00:00 2001
From: John Lindgren <john lindgren aol com>
Date: Tue, 18 Dec 2012 00:23:27 -0500
Subject: [PATCH 1/6] Remove extraneous size request
---
gtk/gtktreeview.c | 25 +------------------------
1 file changed, 1 insertion(+), 24 deletions(-)
diff --git a/gtk/gtktreeview.c b/gtk/gtktreeview.c
index b1528da..1e64582 100644
--- a/gtk/gtktreeview.c
+++ b/gtk/gtktreeview.c
@@ -715,7 +715,7 @@ static gboolean validate_row (GtkTreeView *tree_view,
static void validate_visible_area (GtkTreeView *tree_view);
static gboolean validate_rows_handler (GtkTreeView *tree_view);
static gboolean do_validate_rows (GtkTreeView *tree_view,
- gboolean size_request);
+ gboolean queue_resize);
static gboolean validate_rows (GtkTreeView *tree_view);
static gboolean presize_handler_callback (gpointer data);
static void install_presize_handler (GtkTreeView *tree_view);
@@ -6701,35 +6701,12 @@ do_validate_rows (GtkTreeView *tree_view, gboolean queue_resize)
done:
if (validated_area)
{
- GtkRequisition requisition;
-
- /* We temporarily guess a size, under the assumption that it will be the
- * same when we get our next size_allocate. If we don't do this, we'll be
- * in an inconsistent state when we call top_row_to_dy. */
-
- /* FIXME: This is called from size_request, for some reason it is not infinitely
- * recursing, we cannot call gtk_widget_get_preferred_size() here because that's
- * not allowed (from inside ->get_preferred_width/height() implementations, one
- * should call the vfuncs directly). However what is desired here is the full
- * size including any margins and limited by any alignment (i.e. after
- * GtkWidget:adjust_size_request() is called).
- *
- * Currently bypassing this but the real solution is to not update the scroll adjustments
- * untill we've recieved an allocation (never update scroll adjustments from size-requests).
- */
- gtk_tree_view_size_request (GTK_WIDGET (tree_view), &requisition, FALSE);
-
/* If rows above the current position have changed height, this has
* affected the current view and thus needs a redraw.
*/
if (y != -1 && y < gtk_adjustment_get_value (tree_view->priv->vadjustment))
gtk_widget_queue_draw (GTK_WIDGET (tree_view));
- gtk_adjustment_set_upper (tree_view->priv->hadjustment,
- MAX (gtk_adjustment_get_upper (tree_view->priv->hadjustment), requisition.width));
- gtk_adjustment_set_upper (tree_view->priv->vadjustment,
- MAX (gtk_adjustment_get_upper (tree_view->priv->vadjustment), requisition.height));
-
if (queue_resize)
gtk_widget_queue_resize_no_redraw (GTK_WIDGET (tree_view));
}
--
1.8.0.2
>From ad8dc277807d359536026ad27cee8b83512fb1b3 Mon Sep 17 00:00:00 2001
From: John Lindgren <john lindgren aol com>
Date: Tue, 18 Dec 2012 00:47:07 -0500
Subject: [PATCH 2/6] Kill gtk_tree_view_size_request
---
gtk/gtktreeview.c | 94 +++++++++++++++----------------------------------------
1 file changed, 25 insertions(+), 69 deletions(-)
diff --git a/gtk/gtktreeview.c b/gtk/gtktreeview.c
index 1e64582..f4776bf 100644
--- a/gtk/gtktreeview.c
+++ b/gtk/gtktreeview.c
@@ -385,8 +385,6 @@ struct _GtkTreeViewPrivate
gint x_drag;
/* Non-interactive Header Resizing, expand flag support */
- gint prev_width;
-
gint last_extra_space;
gint last_extra_space_per_column;
gint last_number_of_expand_columns;
@@ -584,9 +582,6 @@ static void gtk_tree_view_get_preferred_width (GtkWidget *widget,
static void gtk_tree_view_get_preferred_height (GtkWidget *widget,
gint *minimum,
gint *natural);
-static void gtk_tree_view_size_request (GtkWidget *widget,
- GtkRequisition *requisition,
- gboolean may_validate);
static void gtk_tree_view_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static gboolean gtk_tree_view_draw (GtkWidget *widget,
@@ -2371,9 +2366,9 @@ gtk_tree_view_unrealize (GtkWidget *widget)
GTK_WIDGET_CLASS (gtk_tree_view_parent_class)->unrealize (widget);
}
-/* GtkWidget::size_request helper */
+/* GtkWidget::get_preferred_height helper */
static void
-gtk_tree_view_size_request_columns (GtkTreeView *tree_view)
+gtk_tree_view_update_height (GtkTreeView *tree_view)
{
GList *list;
@@ -2391,76 +2386,39 @@ gtk_tree_view_size_request_columns (GtkTreeView *tree_view)
gtk_widget_get_preferred_size (button, &requisition, NULL);
tree_view->priv->header_height = MAX (tree_view->priv->header_height, requisition.height);
}
-}
+ if (tree_view->priv->tree == NULL)
+ tree_view->priv->height = 0;
+ else
+ tree_view->priv->height = tree_view->priv->tree->root->offset;
+}
-/* Called only by ::size_request */
static void
-gtk_tree_view_update_size (GtkTreeView *tree_view)
+gtk_tree_view_get_preferred_width (GtkWidget *widget,
+ gint *minimum,
+ gint *natural)
{
+ GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
GList *list;
GtkTreeViewColumn *column;
- gint i;
-
- if (tree_view->priv->model == NULL)
- {
- tree_view->priv->width = 0;
- tree_view->priv->prev_width = 0;
- tree_view->priv->height = 0;
- return;
- }
+ gint width = 0;
- tree_view->priv->prev_width = tree_view->priv->width;
- tree_view->priv->width = 0;
+ /* we validate some rows initially just to make sure we have some size.
+ * In practice, with a lot of static lists, this should get a good width.
+ */
+ do_validate_rows (tree_view, FALSE);
/* keep this in sync with size_allocate below */
- for (list = tree_view->priv->columns, i = 0; list; list = list->next, i++)
+ for (list = tree_view->priv->columns; list; list = list->next)
{
column = list->data;
if (!gtk_tree_view_column_get_visible (column))
continue;
- tree_view->priv->width += _gtk_tree_view_column_request_width (column);
- }
-
- if (tree_view->priv->tree == NULL)
- tree_view->priv->height = 0;
- else
- tree_view->priv->height = tree_view->priv->tree->root->offset;
-}
-
-static void
-gtk_tree_view_size_request (GtkWidget *widget,
- GtkRequisition *requisition,
- gboolean may_validate)
-{
- GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
-
- if (may_validate)
- {
- /* we validate some rows initially just to make sure we have some size.
- * In practice, with a lot of static lists, this should get a good width.
- */
- do_validate_rows (tree_view, FALSE);
+ width += _gtk_tree_view_column_request_width (column);
}
- gtk_tree_view_size_request_columns (tree_view);
- gtk_tree_view_update_size (GTK_TREE_VIEW (widget));
-
- requisition->width = tree_view->priv->width;
- requisition->height = tree_view->priv->height + gtk_tree_view_get_effective_header_height (tree_view);
-}
-
-static void
-gtk_tree_view_get_preferred_width (GtkWidget *widget,
- gint *minimum,
- gint *natural)
-{
- GtkRequisition requisition;
-
- gtk_tree_view_size_request (widget, &requisition, TRUE);
-
- *minimum = *natural = requisition.width;
+ *minimum = *natural = width;
}
static void
@@ -2468,11 +2426,14 @@ gtk_tree_view_get_preferred_height (GtkWidget *widget,
gint *minimum,
gint *natural)
{
- GtkRequisition requisition;
+ GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
+ gint height;
+
+ gtk_tree_view_update_height (tree_view);
- gtk_tree_view_size_request (widget, &requisition, TRUE);
+ height = tree_view->priv->height + gtk_tree_view_get_effective_header_height (tree_view);
- *minimum = *natural = requisition.height;
+ *minimum = *natural = height;
}
static int
@@ -2726,11 +2687,6 @@ gtk_tree_view_size_allocate (GtkWidget *widget,
0,
tree_view->priv->width - allocation->width));
}
- else
- gtk_adjustment_set_value (tree_view->priv->hadjustment,
- CLAMP (tree_view->priv->width - (tree_view->priv->prev_width - gtk_adjustment_get_value (tree_view->priv->hadjustment)),
- 0,
- tree_view->priv->width - allocation->width));
}
else
{
--
1.8.0.2
>From a5088321cbe384c4523b24f2a6c87b5874c4a8f5 Mon Sep 17 00:00:00 2001
From: John Lindgren <john lindgren aol com>
Date: Tue, 18 Dec 2012 01:26:37 -0500
Subject: [PATCH 3/6] Use fixed width for resizing
---
gtk/gtktreeprivate.h | 6 --
gtk/gtktreeview.c | 161 +++++++++---------------------------------------
gtk/gtktreeviewcolumn.c | 59 +++---------------
3 files changed, 37 insertions(+), 189 deletions(-)
diff --git a/gtk/gtktreeprivate.h b/gtk/gtktreeprivate.h
index de4e2ad..9817c5c 100644
--- a/gtk/gtktreeprivate.h
+++ b/gtk/gtktreeprivate.h
@@ -147,12 +147,6 @@ GdkWindow *_gtk_tree_view_column_get_window (GtkTreeViewColumn *co
void _gtk_tree_view_column_push_padding (GtkTreeViewColumn *column,
gint padding);
gint _gtk_tree_view_column_get_requested_width (GtkTreeViewColumn *column);
-void _gtk_tree_view_column_set_resized_width (GtkTreeViewColumn *column,
- gint width);
-gint _gtk_tree_view_column_get_resized_width (GtkTreeViewColumn *column);
-void _gtk_tree_view_column_set_use_resized_width (GtkTreeViewColumn *column,
- gboolean use_resized_width);
-gboolean _gtk_tree_view_column_get_use_resized_width (GtkTreeViewColumn *column);
gint _gtk_tree_view_column_get_drag_x (GtkTreeViewColumn *column);
GtkCellAreaContext *_gtk_tree_view_column_get_context (GtkTreeViewColumn *column);
void _gtk_tree_view_reset_header_styles (GtkTreeView *tree_view);
diff --git a/gtk/gtktreeview.c b/gtk/gtktreeview.c
index f4776bf..592cd41 100644
--- a/gtk/gtktreeview.c
+++ b/gtk/gtktreeview.c
@@ -748,9 +748,6 @@ static void gtk_tree_view_get_arrow_xrange (GtkTreeView
GtkRBTree *tree,
gint *x1,
gint *x2);
-static gint gtk_tree_view_new_column_width (GtkTreeView *tree_view,
- gint i,
- gint *x);
static void gtk_tree_view_adjustment_changed (GtkAdjustment *adjustment,
GtkTreeView *tree_view);
static void gtk_tree_view_build_tree (GtkTreeView *tree_view,
@@ -3175,11 +3172,13 @@ gtk_tree_view_button_press (GtkWidget *widget,
_gtk_tree_view_column_get_window (column))
{
gpointer drag_data;
+ gint column_width, x;
if (event->type == GDK_2BUTTON_PRESS &&
gtk_tree_view_column_get_sizing (column) != GTK_TREE_VIEW_COLUMN_AUTOSIZE)
{
- _gtk_tree_view_column_set_use_resized_width (column, FALSE);
+ gtk_tree_view_column_set_fixed_width (column, -1);
+ gtk_tree_view_column_set_expand (column, FALSE);
_gtk_tree_view_column_autosize (tree_view, column);
return TRUE;
}
@@ -3198,9 +3197,6 @@ gtk_tree_view_button_press (GtkWidget *widget,
gtk_grab_add (widget);
tree_view->priv->in_column_resize = TRUE;
- _gtk_tree_view_column_set_resized_width (column, gtk_tree_view_column_get_width (column) -
- tree_view->priv->last_extra_space_per_column);
-
/* block attached dnd signal handler */
drag_data = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
if (drag_data)
@@ -3209,8 +3205,16 @@ gtk_tree_view_button_press (GtkWidget *widget,
0, 0, NULL, NULL,
drag_data);
+ column_width = gtk_tree_view_column_get_width (column);
+ gtk_tree_view_column_set_fixed_width (column, column_width);
+ gtk_tree_view_column_set_expand (column, FALSE);
+
+ gdk_window_get_device_position (tree_view->priv->bin_window,
+ gdk_event_get_device ((GdkEvent *) event),
+ &x, NULL, NULL);
+
tree_view->priv->drag_pos = i;
- tree_view->priv->x_drag = gtk_tree_view_column_get_x_offset (column) + (rtl ? 0 : gtk_tree_view_column_get_width (column));
+ tree_view->priv->x_drag = x + (rtl ? column_width : -column_width);
if (!gtk_widget_has_focus (widget))
gtk_widget_grab_focus (widget);
@@ -3934,31 +3938,17 @@ gtk_tree_view_motion_resize_column (GtkWidget *widget,
column = gtk_tree_view_get_column (tree_view, tree_view->priv->drag_pos);
- if (event->is_hint || event->window != gtk_widget_get_window (widget))
- gdk_window_get_device_position (gtk_widget_get_window (widget),
- gdk_event_get_device ((GdkEvent *) event),
- &x, NULL, NULL);
+ gdk_window_get_device_position (tree_view->priv->bin_window,
+ gdk_event_get_device ((GdkEvent *) event),
+ &x, NULL, NULL);
+
+ if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
+ new_width = MAX (tree_view->priv->x_drag - x, 0);
else
- x = event->x;
-
- if (tree_view->priv->hadjustment)
- x += gtk_adjustment_get_value (tree_view->priv->hadjustment);
-
- new_width = gtk_tree_view_new_column_width (tree_view,
- tree_view->priv->drag_pos, &x);
- if (x != tree_view->priv->x_drag &&
- (new_width != gtk_tree_view_column_get_fixed_width (column)))
- {
- _gtk_tree_view_column_set_use_resized_width (column, TRUE);
-
- if (gtk_tree_view_column_get_expand (column))
- new_width -= tree_view->priv->last_extra_space_per_column;
-
- _gtk_tree_view_column_set_resized_width (column, new_width);
-
-
- gtk_widget_queue_resize (widget);
- }
+ new_width = MAX (x - tree_view->priv->x_drag, 0);
+
+ if (new_width != gtk_tree_view_column_get_fixed_width (column))
+ gtk_tree_view_column_set_fixed_width (column, new_width);
return FALSE;
}
@@ -5628,7 +5618,7 @@ gtk_tree_view_key_press (GtkWidget *widget,
|| event->keyval == GDK_KEY_Right || event->keyval == GDK_KEY_KP_Right))
{
GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (focus_column->data);
- gint max_width, min_width;
+ gint column_width;
if (!gtk_tree_view_column_get_resizable (column))
{
@@ -5636,70 +5626,21 @@ gtk_tree_view_key_press (GtkWidget *widget,
return TRUE;
}
+ column_width = gtk_tree_view_column_get_width (column);
+
if (event->keyval == (rtl ? GDK_KEY_Right : GDK_KEY_Left)
|| event->keyval == (rtl ? GDK_KEY_KP_Right : GDK_KEY_KP_Left))
{
- GtkRequisition button_req;
- gint old_width = _gtk_tree_view_column_get_resized_width (column);
- gint new_width;
-
- button = gtk_tree_view_column_get_button (column);
-
- gtk_widget_get_preferred_size (button, &button_req, NULL);
-
- new_width = MAX (old_width, gtk_tree_view_column_get_width (column));
- new_width -= 2;
- if (new_width < 0)
- new_width = 0;
-
- _gtk_tree_view_column_set_resized_width (column, new_width);
-
- min_width = gtk_tree_view_column_get_min_width (column);
- if (min_width == -1)
- new_width = MAX (button_req.width, new_width);
- else
- {
- new_width = MAX (min_width, new_width);
- }
-
- max_width = gtk_tree_view_column_get_max_width (column);
- if (max_width != -1)
- new_width = MIN (new_width, max_width);
-
- _gtk_tree_view_column_set_use_resized_width (column, TRUE);
-
- if (new_width != old_width)
- {
- _gtk_tree_view_column_set_resized_width (column, new_width);
- gtk_widget_queue_resize (widget);
- }
- else
- gtk_widget_error_bell (widget);
+ column_width = MAX (column_width - 2, 0);
}
else if (event->keyval == (rtl ? GDK_KEY_Left : GDK_KEY_Right)
|| event->keyval == (rtl ? GDK_KEY_KP_Left : GDK_KEY_KP_Right))
{
- gint old_width = _gtk_tree_view_column_get_resized_width (column);
- gint new_width;
-
- new_width = MAX (old_width, gtk_tree_view_column_get_width (column));
- new_width += 2;
-
- max_width = gtk_tree_view_column_get_max_width (column);
- if (max_width != -1)
- new_width = MIN (new_width, max_width);
-
- _gtk_tree_view_column_set_use_resized_width (column, TRUE);
-
- if (new_width != old_width)
- {
- _gtk_tree_view_column_set_resized_width (column, new_width);
- gtk_widget_queue_resize (widget);
- }
- else
- gtk_widget_error_bell (widget);
+ column_width = column_width + 2;
}
+ gtk_tree_view_column_set_fixed_width (column, column_width);
+ gtk_tree_view_column_set_expand (column, FALSE);
return TRUE;
}
@@ -11044,50 +10985,6 @@ gtk_tree_view_start_interactive_search (GtkTreeView *tree_view)
TRUE);
}
-/* this function returns the new width of the column being resized given
- * the column and x position of the cursor; the x cursor position is passed
- * in as a pointer and automagicly corrected if it's beyond min/max limits
- */
-static gint
-gtk_tree_view_new_column_width (GtkTreeView *tree_view,
- gint i,
- gint *x)
-{
- GtkAllocation allocation;
- GtkTreeViewColumn *column;
- GtkRequisition button_req;
- gint max_width, min_width;
- gint width;
- gboolean rtl;
-
- /* first translate the x position from widget->window
- * to clist->clist_window
- */
- rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
- column = g_list_nth (tree_view->priv->columns, i)->data;
- gtk_widget_get_allocation (gtk_tree_view_column_get_button (column), &allocation);
- width = rtl ? (allocation.x + allocation.width - *x) : (*x - allocation.x);
-
- /* Clamp down the value */
- min_width = gtk_tree_view_column_get_min_width (column);
- if (min_width == -1)
- {
- gtk_widget_get_preferred_size (gtk_tree_view_column_get_button (column), &button_req, NULL);
- width = MAX (button_req.width, width);
- }
- else
- width = MAX (min_width, width);
-
- max_width = gtk_tree_view_column_get_max_width (column);
- if (max_width != -1)
- width = MIN (width, max_width);
-
- *x = rtl ? (allocation.x + allocation.width - width) : (allocation.x + width);
-
- return width;
-}
-
-
/* FIXME this adjust_allocation is a big cut-and-paste from
* GtkCList, needs to be some "official" way to do this
* factored out.
diff --git a/gtk/gtktreeviewcolumn.c b/gtk/gtktreeviewcolumn.c
index c9c0be5..245bbdf 100644
--- a/gtk/gtktreeviewcolumn.c
+++ b/gtk/gtktreeviewcolumn.c
@@ -140,7 +140,6 @@ struct _GtkTreeViewColumnPrivate
/* see gtk+/doc/tree-column-sizing.txt for more information on them */
GtkTreeViewColumnSizing column_type;
gint padding;
- gint resized_width;
gint x_offset;
gint width;
gint fixed_width;
@@ -174,7 +173,6 @@ struct _GtkTreeViewColumnPrivate
guint show_sort_indicator : 1;
guint maybe_reordered : 1;
guint reorderable : 1;
- guint use_resized_width : 1;
guint expand : 1;
};
@@ -299,9 +297,9 @@ gtk_tree_view_column_class_init (GtkTreeViewColumnClass *class)
g_param_spec_int ("fixed-width",
P_("Fixed Width"),
P_("Current fixed width of the column"),
- 1,
+ -1,
G_MAXINT,
- 1, /* not useful */
+ -1,
GTK_PARAM_READWRITE));
g_object_class_install_property (object_class,
@@ -472,7 +470,6 @@ gtk_tree_view_column_init (GtkTreeViewColumn *tree_column)
priv->padding = -1;
priv->min_width = -1;
priv->max_width = -1;
- priv->resized_width = 0;
priv->column_type = GTK_TREE_VIEW_COLUMN_GROW_ONLY;
priv->visible = TRUE;
priv->resizable = FALSE;
@@ -487,8 +484,7 @@ gtk_tree_view_column_init (GtkTreeViewColumn *tree_column)
priv->sort_column_id = -1;
priv->reorderable = FALSE;
priv->maybe_reordered = FALSE;
- priv->fixed_width = 1;
- priv->use_resized_width = FALSE;
+ priv->fixed_width = -1;
priv->title = g_strdup ("");
}
@@ -2102,11 +2098,7 @@ _gtk_tree_view_column_request_width (GtkTreeViewColumn *tree_column)
priv = tree_column->priv;
- if (priv->use_resized_width)
- {
- real_requested_width = priv->resized_width;
- }
- else if (priv->column_type == GTK_TREE_VIEW_COLUMN_FIXED)
+ if (priv->fixed_width != -1)
{
real_requested_width = priv->fixed_width;
}
@@ -2204,16 +2196,15 @@ gtk_tree_view_column_set_fixed_width (GtkTreeViewColumn *tree_column,
GtkTreeViewColumnPrivate *priv;
g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
- g_return_if_fail (fixed_width > 0);
+ g_return_if_fail (fixed_width >= -1);
priv = tree_column->priv;
priv->fixed_width = fixed_width;
- priv->use_resized_width = FALSE;
- if (priv->tree_view &&
- gtk_widget_get_realized (priv->tree_view) &&
- priv->column_type == GTK_TREE_VIEW_COLUMN_FIXED)
+ if (priv->visible &&
+ priv->tree_view != NULL &&
+ gtk_widget_get_realized (priv->tree_view))
{
gtk_widget_queue_resize (priv->tree_view);
}
@@ -2461,13 +2452,6 @@ gtk_tree_view_column_set_expand (GtkTreeViewColumn *tree_column,
priv->tree_view != NULL &&
gtk_widget_get_realized (priv->tree_view))
{
- /* We want to continue using the original width of the
- * column that includes additional space added by the user
- * resizing the columns and possibly extra (expanded) space, which
- * are not included in the resized width.
- */
- priv->use_resized_width = FALSE;
-
gtk_widget_queue_resize (priv->tree_view);
}
@@ -3207,33 +3191,6 @@ _gtk_tree_view_column_get_requested_width (GtkTreeViewColumn *column)
return requested_width + column->priv->padding;
}
-
-void
-_gtk_tree_view_column_set_resized_width (GtkTreeViewColumn *column,
- gint width)
-{
- column->priv->resized_width = width;
-}
-
-gint
-_gtk_tree_view_column_get_resized_width (GtkTreeViewColumn *column)
-{
- return column->priv->resized_width;
-}
-
-void
-_gtk_tree_view_column_set_use_resized_width (GtkTreeViewColumn *column,
- gboolean use_resized_width)
-{
- column->priv->use_resized_width = use_resized_width;
-}
-
-gboolean
-_gtk_tree_view_column_get_use_resized_width (GtkTreeViewColumn *column)
-{
- return column->priv->use_resized_width;
-}
-
gint
_gtk_tree_view_column_get_drag_x (GtkTreeViewColumn *column)
{
--
1.8.0.2
>From 94f1dd426d71675728740d2fbc6fac9de8d68c9f Mon Sep 17 00:00:00 2001
From: John Lindgren <john lindgren aol com>
Date: Tue, 18 Dec 2012 01:34:55 -0500
Subject: [PATCH 4/6] Minor refactoring
---
gtk/gtktreeview.c | 44 +++++++++++++++++++++++---------------------
1 file changed, 23 insertions(+), 21 deletions(-)
diff --git a/gtk/gtktreeview.c b/gtk/gtktreeview.c
index 592cd41..461b382 100644
--- a/gtk/gtktreeview.c
+++ b/gtk/gtktreeview.c
@@ -2409,7 +2409,7 @@ gtk_tree_view_get_preferred_width (GtkWidget *widget,
for (list = tree_view->priv->columns; list; list = list->next)
{
column = list->data;
- if (!gtk_tree_view_column_get_visible (column))
+ if (!gtk_tree_view_column_get_visible (column) || column == tree_view->priv->drag_column)
continue;
width += _gtk_tree_view_column_request_width (column);
@@ -2492,7 +2492,7 @@ gtk_tree_view_size_allocate_columns (GtkWidget *widget,
{
column = (GtkTreeViewColumn *)list->data;
- if (!gtk_tree_view_column_get_visible (column))
+ if (!gtk_tree_view_column_get_visible (column) || column == tree_view->priv->drag_column)
continue;
full_requested_width += _gtk_tree_view_column_request_width (column);
@@ -2546,27 +2546,9 @@ gtk_tree_view_size_allocate_columns (GtkWidget *widget,
column = list->data;
- if (!gtk_tree_view_column_get_visible (column))
+ if (!gtk_tree_view_column_get_visible (column) || column == tree_view->priv->drag_column)
continue;
- /* We need to handle the dragged button specially.
- */
- if (column == tree_view->priv->drag_column)
- {
- GtkAllocation drag_allocation;
- GtkWidget *button;
-
- button = gtk_tree_view_column_get_button (tree_view->priv->drag_column);
-
- drag_allocation.x = 0;
- drag_allocation.y = 0;
- drag_allocation.width = gdk_window_get_width (tree_view->priv->drag_window);
- drag_allocation.height = gdk_window_get_height (tree_view->priv->drag_window);
- gtk_widget_size_allocate (button, &drag_allocation);
- width += drag_allocation.width;
- continue;
- }
-
column_width = _gtk_tree_view_column_request_width (column);
if (gtk_tree_view_column_get_expand (column))
@@ -2613,6 +2595,25 @@ gtk_tree_view_size_allocate_columns (GtkWidget *widget,
}
}
+/* GtkWidget::size_allocate helper */
+static void
+gtk_tree_view_size_allocate_drag_column (GtkWidget *widget)
+{
+ GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
+ GtkAllocation drag_allocation;
+ GtkWidget *button;
+
+ if (tree_view->priv->drag_column == NULL)
+ return;
+
+ button = gtk_tree_view_column_get_button (tree_view->priv->drag_column);
+
+ drag_allocation.x = 0;
+ drag_allocation.y = 0;
+ drag_allocation.width = gdk_window_get_width (tree_view->priv->drag_window);
+ drag_allocation.height = gdk_window_get_height (tree_view->priv->drag_window);
+ gtk_widget_size_allocate (button, &drag_allocation);
+}
static void
gtk_tree_view_size_allocate (GtkWidget *widget,
@@ -2652,6 +2653,7 @@ gtk_tree_view_size_allocate (GtkWidget *widget,
* tree view (used in updating the adjustments below) might change.
*/
gtk_tree_view_size_allocate_columns (widget, &width_changed);
+ gtk_tree_view_size_allocate_drag_column (widget);
g_object_freeze_notify (G_OBJECT (tree_view->priv->hadjustment));
gtk_adjustment_set_page_size (tree_view->priv->hadjustment,
--
1.8.0.2
>From 73fb9ebc798e72d60bd594668df0ccb86a517074 Mon Sep 17 00:00:00 2001
From: John Lindgren <john lindgren aol com>
Date: Tue, 18 Dec 2012 02:06:12 -0500
Subject: [PATCH 5/6] Use minimum/natural size semantics
---
gtk/gtktreeprivate.h | 4 +-
gtk/gtktreeview.c | 170 +++++++++++++++++++-----------------------------
gtk/gtktreeviewcolumn.c | 62 +++++++++---------
3 files changed, 101 insertions(+), 135 deletions(-)
diff --git a/gtk/gtktreeprivate.h b/gtk/gtktreeprivate.h
index 9817c5c..fcdcf81 100644
--- a/gtk/gtktreeprivate.h
+++ b/gtk/gtktreeprivate.h
@@ -106,7 +106,9 @@ void _gtk_tree_view_column_unrealize_button (GtkTreeViewColumn *column);
void _gtk_tree_view_column_set_tree_view (GtkTreeViewColumn *column,
GtkTreeView *tree_view);
-gint _gtk_tree_view_column_request_width (GtkTreeViewColumn *tree_column);
+void _gtk_tree_view_column_request_width (GtkTreeViewColumn *tree_column,
+ gint *minimum,
+ gint *natural);
void _gtk_tree_view_column_allocate (GtkTreeViewColumn *tree_column,
int x_offset,
int width);
diff --git a/gtk/gtktreeview.c b/gtk/gtktreeview.c
index 461b382..dd54be4 100644
--- a/gtk/gtktreeview.c
+++ b/gtk/gtktreeview.c
@@ -384,10 +384,10 @@ struct _GtkTreeViewPrivate
gint drag_pos;
gint x_drag;
- /* Non-interactive Header Resizing, expand flag support */
- gint last_extra_space;
- gint last_extra_space_per_column;
- gint last_number_of_expand_columns;
+ /* Column width allocation */
+ gint minimum_width;
+ gint natural_width;
+ gint n_expand_columns;
/* ATK Hack */
GtkTreeDestroyCountFunc destroy_count_func;
@@ -491,8 +491,6 @@ struct _GtkTreeViewPrivate
guint in_grab : 1;
- guint post_validation_flag : 1;
-
/* Whether our key press handler is to avoid sending an unhandled binding to the search entry */
guint search_entry_avoid_unhandled_binding : 1;
@@ -1718,6 +1716,9 @@ gtk_tree_view_init (GtkTreeView *tree_view)
tree_view->priv->header_height = 1;
tree_view->priv->x_drag = 0;
tree_view->priv->drag_pos = -1;
+ tree_view->priv->minimum_width = 0;
+ tree_view->priv->natural_width = 0;
+ tree_view->priv->n_expand_columns = 0;
tree_view->priv->header_has_focus = FALSE;
tree_view->priv->pressed_button = -1;
tree_view->priv->press_start_x = -1;
@@ -1750,8 +1751,6 @@ gtk_tree_view_init (GtkTreeView *tree_view)
tree_view->priv->tooltip_column = -1;
- tree_view->priv->post_validation_flag = FALSE;
-
tree_view->priv->last_button_x = -1;
tree_view->priv->last_button_y = -1;
@@ -2398,12 +2397,16 @@ gtk_tree_view_get_preferred_width (GtkWidget *widget,
GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
GList *list;
GtkTreeViewColumn *column;
- gint width = 0;
+ gint column_minimum, column_natural;
/* we validate some rows initially just to make sure we have some size.
* In practice, with a lot of static lists, this should get a good width.
*/
do_validate_rows (tree_view, FALSE);
+
+ tree_view->priv->minimum_width = 0;
+ tree_view->priv->natural_width = 0;
+ tree_view->priv->n_expand_columns = 0;
/* keep this in sync with size_allocate below */
for (list = tree_view->priv->columns; list; list = list->next)
@@ -2412,10 +2415,18 @@ gtk_tree_view_get_preferred_width (GtkWidget *widget,
if (!gtk_tree_view_column_get_visible (column) || column == tree_view->priv->drag_column)
continue;
- width += _gtk_tree_view_column_request_width (column);
+ _gtk_tree_view_column_request_width (column, &column_minimum, &column_natural);
+ tree_view->priv->minimum_width += column_minimum;
+ tree_view->priv->natural_width += column_natural;
+
+ if (gtk_tree_view_column_get_expand (column))
+ tree_view->priv->n_expand_columns++;
}
- *minimum = *natural = width;
+ if (minimum != NULL)
+ *minimum = tree_view->priv->minimum_width;
+ if (natural != NULL)
+ *natural = tree_view->priv->natural_width;
}
static void
@@ -2430,7 +2441,10 @@ gtk_tree_view_get_preferred_height (GtkWidget *widget,
height = tree_view->priv->height + gtk_tree_view_get_effective_header_height (tree_view);
- *minimum = *natural = height;
+ if (minimum != NULL)
+ *minimum = height;
+ if (natural != NULL)
+ *natural = height;
}
static int
@@ -2462,12 +2476,9 @@ gtk_tree_view_size_allocate_columns (GtkWidget *widget,
GList *list, *first_column, *last_column;
GtkTreeViewColumn *column;
GtkAllocation widget_allocation;
- gint width = 0;
- gint extra, extra_per_column, extra_for_last;
- gint full_requested_width = 0;
- gint number_of_expand_columns = 0;
+ gint minimum_width, natural_width, n_expand_columns, width;
+ gint column_minimum, column_natural, column_width;
gboolean rtl;
- gboolean update_expand;
tree_view = GTK_TREE_VIEW (widget);
@@ -2487,111 +2498,65 @@ gtk_tree_view_size_allocate_columns (GtkWidget *widget,
rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
- /* find out how many extra space and expandable columns we have */
- for (list = tree_view->priv->columns; list != last_column->next; list = list->next)
- {
- column = (GtkTreeViewColumn *)list->data;
-
- if (!gtk_tree_view_column_get_visible (column) || column == tree_view->priv->drag_column)
- continue;
-
- full_requested_width += _gtk_tree_view_column_request_width (column);
-
- if (gtk_tree_view_column_get_expand (column))
- number_of_expand_columns++;
- }
-
- /* Only update the expand value if the width of the widget has changed,
- * or the number of expand columns has changed, or if there are no expand
- * columns, or if we didn't have an size-allocation yet after the
- * last validated node.
- */
- update_expand = (width_changed && *width_changed == TRUE)
- || number_of_expand_columns != tree_view->priv->last_number_of_expand_columns
- || number_of_expand_columns == 0
- || tree_view->priv->post_validation_flag == TRUE;
-
- tree_view->priv->post_validation_flag = FALSE;
-
gtk_widget_get_allocation (widget, &widget_allocation);
- if (!update_expand)
- {
- extra = tree_view->priv->last_extra_space;
- extra_for_last = MAX (widget_allocation.width - full_requested_width - extra, 0);
- }
- else
- {
- extra = MAX (widget_allocation.width - full_requested_width, 0);
- extra_for_last = 0;
- tree_view->priv->last_extra_space = extra;
- }
+ minimum_width = tree_view->priv->minimum_width;
+ natural_width = tree_view->priv->natural_width;
+ n_expand_columns = tree_view->priv->n_expand_columns;
- if (number_of_expand_columns > 0)
- extra_per_column = extra/number_of_expand_columns;
- else
- extra_per_column = 0;
+ width = MAX (widget_allocation.width, minimum_width);
- if (update_expand)
+ /* We change the width here. The user might have been resizing columns,
+ * which changes the total width of the tree view. This is of
+ * importance for getting the horizontal scroll bar right.
+ */
+ if (tree_view->priv->width != width)
{
- tree_view->priv->last_extra_space_per_column = extra_per_column;
- tree_view->priv->last_number_of_expand_columns = number_of_expand_columns;
+ tree_view->priv->width = width;
+ if (width_changed)
+ *width_changed = TRUE;
}
- for (list = (rtl ? last_column : first_column);
- list != (rtl ? first_column->prev : last_column->next);
- list = (rtl ? list->prev : list->next))
+ /* iterate through columns in reverse order */
+ for (list = (rtl ? first_column : last_column);
+ list != (rtl ? last_column->next : first_column->prev);
+ list = (rtl ? list->next : list->prev))
{
- gint column_width;
-
column = list->data;
if (!gtk_tree_view_column_get_visible (column) || column == tree_view->priv->drag_column)
continue;
- column_width = _gtk_tree_view_column_request_width (column);
+ _gtk_tree_view_column_request_width (column, &column_minimum, &column_natural);
+
+ column_width = column_natural;
- if (gtk_tree_view_column_get_expand (column))
- {
- if (number_of_expand_columns == 1)
+ if (width > natural_width)
+ {
+ /* We need to expand some columns. If there are none marked to
+ * expand, give all the extra space to the last column. */
+ if (n_expand_columns == 0)
{
- /* We add the remander to the last column as
- * */
- column_width += extra;
+ column_width = column_natural + (width - natural_width);
}
- else
+ else if (gtk_tree_view_column_get_expand (column))
{
- column_width += extra_per_column;
- extra -= extra_per_column;
- number_of_expand_columns --;
+ column_width = column_natural + (width - natural_width) / n_expand_columns;
+ n_expand_columns--;
}
- }
- else if (number_of_expand_columns == 0 &&
- list == last_column)
+ }
+ else if (width < natural_width)
{
- column_width += extra;
+ /* We need to shrink some columns. Starting with later columns,
+ * shrink each one down to its minimum width as necessary. */
+ column_width = MAX (column_natural + (width - natural_width), column_minimum);
}
-
- /* In addition to expand, the last column can get even more
- * extra space so all available space is filled up.
- */
- if (extra_for_last > 0 && list == last_column)
- column_width += extra_for_last;
-
- _gtk_tree_view_column_allocate (column, width, column_width);
-
- width += column_width;
- }
-
- /* We change the width here. The user might have been resizing columns,
- * which changes the total width of the tree view. This is of
- * importance for getting the horizontal scroll bar right.
- */
- if (tree_view->priv->width != width)
- {
- tree_view->priv->width = width;
- if (width_changed)
- *width_changed = TRUE;
+
+ _gtk_tree_view_column_allocate (column, width - column_width, column_width);
+
+ minimum_width -= column_minimum;
+ natural_width -= column_natural;
+ width -= column_width;
}
}
@@ -6056,7 +6021,6 @@ validate_row (GtkTreeView *tree_view,
_gtk_rbtree_node_set_height (tree, node, height);
}
_gtk_rbtree_node_mark_valid (tree, node);
- tree_view->priv->post_validation_flag = TRUE;
return retval;
}
diff --git a/gtk/gtktreeviewcolumn.c b/gtk/gtktreeviewcolumn.c
index 245bbdf..23d2607 100644
--- a/gtk/gtktreeviewcolumn.c
+++ b/gtk/gtktreeviewcolumn.c
@@ -2090,48 +2090,48 @@ gtk_tree_view_column_get_x_offset (GtkTreeViewColumn *tree_column)
return tree_column->priv->x_offset;
}
-gint
-_gtk_tree_view_column_request_width (GtkTreeViewColumn *tree_column)
+void
+_gtk_tree_view_column_request_width (GtkTreeViewColumn *tree_column,
+ gint *minimum,
+ gint *natural)
{
- GtkTreeViewColumnPrivate *priv;
- gint real_requested_width;
-
- priv = tree_column->priv;
+ GtkTreeViewColumnPrivate *priv = tree_column->priv;
+ gint minimum_width = 1, natural_width = 1;
+ gint button_minimum, button_natural;
- if (priv->fixed_width != -1)
- {
- real_requested_width = priv->fixed_width;
- }
- else if (gtk_tree_view_get_headers_visible (GTK_TREE_VIEW (priv->tree_view)))
+ if (priv->column_type != GTK_TREE_VIEW_COLUMN_FIXED)
{
- gint button_request;
- gint requested_width;
+ gtk_cell_area_context_get_preferred_width (priv->cell_area_context, &minimum_width, &natural_width);
+ minimum_width += priv->padding;
+ natural_width += priv->padding;
- gtk_cell_area_context_get_preferred_width (priv->cell_area_context, &requested_width, NULL);
- requested_width += priv->padding;
-
- gtk_widget_get_preferred_width (priv->button, &button_request, NULL);
- real_requested_width = MAX (requested_width, button_request);
+ if (gtk_tree_view_get_headers_visible (GTK_TREE_VIEW (priv->tree_view)))
+ {
+ gtk_widget_get_preferred_width (priv->button, &button_minimum, &button_natural);
+ minimum_width = MAX (minimum_width, button_minimum);
+ natural_width = MAX (natural_width, button_natural);
+ }
}
- else
- {
- gint requested_width;
- gtk_cell_area_context_get_preferred_width (priv->cell_area_context, &requested_width, NULL);
- requested_width += priv->padding;
-
- real_requested_width = requested_width;
- if (real_requested_width < 0)
- real_requested_width = 0;
- }
+ if (priv->fixed_width != -1)
+ natural_width = MAX (priv->fixed_width, minimum_width);
if (priv->min_width != -1)
- real_requested_width = MAX (real_requested_width, priv->min_width);
+ {
+ minimum_width = MAX (minimum_width, priv->min_width);
+ natural_width = MAX (natural_width, priv->min_width);
+ }
if (priv->max_width != -1)
- real_requested_width = MIN (real_requested_width, priv->max_width);
+ {
+ minimum_width = MIN (minimum_width, priv->max_width);
+ natural_width = MIN (natural_width, priv->max_width);
+ }
- return real_requested_width;
+ if (minimum != NULL)
+ *minimum = minimum_width;
+ if (natural != NULL)
+ *natural = natural_width;
}
void
--
1.8.0.2
>From 9d5a27b2ab554eb1d2df13dd981799c311dee026 Mon Sep 17 00:00:00 2001
From: John Lindgren <john lindgren aol com>
Date: Thu, 20 Dec 2012 02:08:12 -0500
Subject: [PATCH 6/6] Better resize of expandable columns
---
gtk/gtktreeview.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 69 insertions(+), 9 deletions(-)
diff --git a/gtk/gtktreeview.c b/gtk/gtktreeview.c
index dd54be4..dc61361 100644
--- a/gtk/gtktreeview.c
+++ b/gtk/gtktreeview.c
@@ -2447,6 +2447,70 @@ gtk_tree_view_get_preferred_height (GtkWidget *widget,
*natural = height;
}
+static void
+gtk_tree_view_modify_column_width (GtkTreeView *tree_view,
+ GtkTreeViewColumn *column,
+ gint width)
+{
+ gboolean is_expand;
+ gint n_expand_others;
+ gint minimum, natural, natural_others;
+
+ is_expand = gtk_tree_view_column_get_expand (column);
+ n_expand_others = tree_view->priv->n_expand_columns - (is_expand ? 1 : 0);
+
+ _gtk_tree_view_column_request_width (column, &minimum, &natural);
+ natural_others = tree_view->priv->natural_width - natural;
+
+ if (natural_others + width < tree_view->priv->width)
+ {
+ /* There is extra space that needs to be taken up by letting some other
+ * column(s) expand: by default, the last column. */
+ if (!n_expand_others)
+ {
+ GList *last = g_list_last (tree_view->priv->columns);
+ while (!gtk_tree_view_column_get_visible (last->data))
+ last = last->prev;
+
+ if (column == last->data)
+ return;
+
+ gtk_tree_view_column_set_expand (last->data, TRUE);
+ n_expand_others++;
+ }
+
+ /* Now try to make this column expandable also. Solving the following
+ * equations reveals what the natural width should be to achieve the
+ * desired width after expanding:
+ *
+ * 1. natural + expand = desired_width
+ * 2. natural + natural_others + expand * (n_expand_others + 1) = total_width
+ *
+ * Solution:
+ * expand = (total_width - natural_others - desired_width) / n_expand_others
+ *
+ * It is possible for the solved natural width to be less than the
+ * minimum; in that case, we cannot let the column expand.
+ */
+ gint expand = (tree_view->priv->width - natural_others - width) / n_expand_others;
+
+ if (minimum + expand > width)
+ {
+ if (is_expand)
+ gtk_tree_view_column_set_expand (column, FALSE);
+ }
+ else
+ {
+ if (!is_expand)
+ gtk_tree_view_column_set_expand (column, TRUE);
+
+ width -= expand;
+ }
+ }
+
+ gtk_tree_view_column_set_fixed_width (column, width);
+}
+
static int
gtk_tree_view_calculate_width_before_expander (GtkTreeView *tree_view)
{
@@ -3173,9 +3237,6 @@ gtk_tree_view_button_press (GtkWidget *widget,
drag_data);
column_width = gtk_tree_view_column_get_width (column);
- gtk_tree_view_column_set_fixed_width (column, column_width);
- gtk_tree_view_column_set_expand (column, FALSE);
-
gdk_window_get_device_position (tree_view->priv->bin_window,
gdk_event_get_device ((GdkEvent *) event),
&x, NULL, NULL);
@@ -3908,14 +3969,14 @@ gtk_tree_view_motion_resize_column (GtkWidget *widget,
gdk_window_get_device_position (tree_view->priv->bin_window,
gdk_event_get_device ((GdkEvent *) event),
&x, NULL, NULL);
-
+
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
new_width = MAX (tree_view->priv->x_drag - x, 0);
else
new_width = MAX (x - tree_view->priv->x_drag, 0);
-
- if (new_width != gtk_tree_view_column_get_fixed_width (column))
- gtk_tree_view_column_set_fixed_width (column, new_width);
+
+ if (new_width != gtk_tree_view_column_get_width (column))
+ gtk_tree_view_modify_column_width (tree_view, column, new_width);
return FALSE;
}
@@ -5606,8 +5667,7 @@ gtk_tree_view_key_press (GtkWidget *widget,
column_width = column_width + 2;
}
- gtk_tree_view_column_set_fixed_width (column, column_width);
- gtk_tree_view_column_set_expand (column, FALSE);
+ gtk_tree_view_modify_column_width (tree_view, column, column_width);
return TRUE;
}
--
1.8.0.2
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]