changeset 18404:9a0f99ea664d

Resize tree-columns nicely when the tree is resized. We can tell it to set a fixed with for a column, or maintain the width-ratio when resizing.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sun, 01 Jul 2007 12:21:30 +0000
parents 8ee1f173253c
children 69cc1a4ef6ab
files ChangeLog.API finch/gntft.c finch/gntstatus.c finch/libgnt/gntmenu.c finch/libgnt/gnttree.c finch/libgnt/gnttree.h
diffstat 6 files changed, 117 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog.API	Sun Jul 01 07:15:16 2007 +0000
+++ b/ChangeLog.API	Sun Jul 01 12:21:30 2007 +0000
@@ -102,11 +102,15 @@
 			  gnt_text_view_set_flag
 			* gnt_style_get_from_name
 			* gnt_window_present
+			* gnt_tree_set_column_width_ratio
+			* gnt_tree_set_column_resizable
 
 			Changed:
 			* gnt_tree_get_rows() now returns a GList* instead of a const
 			  GList*, as const is not very useful with GLists.  The
 			  returned value still must not be modified or freed.
+			* Instead of keeping an 'invisible' item, the GntTreeColumns now
+			  maintain 'flags' with the appropriate flags set
 
 version 2.0.2 (6/14/2007):
 	Pidgin:
--- a/finch/gntft.c	Sun Jul 01 07:15:16 2007 +0000
+++ b/finch/gntft.c	Sun Jul 01 12:21:30 2007 +0000
@@ -178,6 +178,7 @@
 	GntWidget *button;
 	GntWidget *checkbox;
 	GntWidget *tree;
+	int widths[] = {8, 12, 8, 8, 8, 8, -1};
 
 	if (!xfer_dialog)
 		xfer_dialog = g_new0(PurpleGntXferDialog, 1);
@@ -195,12 +196,12 @@
 
 	xfer_dialog->tree = tree = gnt_tree_new_with_columns(NUM_COLUMNS);
 	gnt_tree_set_column_titles(GNT_TREE(tree), _("Progress"), _("Filename"), _("Size"), _("Speed"), _("Remaining"), _("Status"));
-	gnt_tree_set_col_width(GNT_TREE(tree), COLUMN_PROGRESS, 8);
-	gnt_tree_set_col_width(GNT_TREE(tree), COLUMN_FILENAME, 8);
-	gnt_tree_set_col_width(GNT_TREE(tree), COLUMN_SIZE, 10);
-	gnt_tree_set_col_width(GNT_TREE(tree), COLUMN_SPEED, 10);
-	gnt_tree_set_col_width(GNT_TREE(tree), COLUMN_REMAINING, 10);
-	gnt_tree_set_col_width(GNT_TREE(tree), COLUMN_STATUS, 10);
+	gnt_tree_set_column_width_ratio(GNT_TREE(tree), widths);
+	gnt_tree_set_column_resizable(GNT_TREE(tree), COLUMN_PROGRESS, FALSE);
+	gnt_tree_set_column_resizable(GNT_TREE(tree), COLUMN_SIZE, FALSE);
+	gnt_tree_set_column_resizable(GNT_TREE(tree), COLUMN_SPEED, FALSE);
+	gnt_tree_set_column_resizable(GNT_TREE(tree), COLUMN_REMAINING, FALSE);
+	gnt_widget_set_size(tree, 70, -1);
 	gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
 	gnt_box_add_widget(GNT_BOX(window), tree);
 
--- a/finch/gntstatus.c	Sun Jul 01 07:15:16 2007 +0000
+++ b/finch/gntstatus.c	Sun Jul 01 12:21:30 2007 +0000
@@ -163,6 +163,7 @@
 void finch_savedstatus_show_all()
 {
 	GntWidget *window, *tree, *box, *button;
+	int widths[] = {25, 12, 35};
 	if (statuses.window) {
 		gnt_window_present(statuses.window);
 		return;
@@ -179,9 +180,8 @@
 	statuses.tree = tree = gnt_tree_new_with_columns(3);
 	gnt_tree_set_column_titles(GNT_TREE(tree), _("Title"), _("Type"), _("Message"));
 	gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
-	gnt_tree_set_col_width(GNT_TREE(tree), 0, 25);
-	gnt_tree_set_col_width(GNT_TREE(tree), 1, 12);
-	gnt_tree_set_col_width(GNT_TREE(tree), 2, 35);
+	gnt_tree_set_column_width_ratio(GNT_TREE(tree), widths);
+	gnt_widget_set_size(tree, 72, 0);
 	gnt_box_add_widget(GNT_BOX(window), tree);
 
 	populate_statuses(GNT_TREE(tree));
--- a/finch/libgnt/gntmenu.c	Sun Jul 01 07:15:16 2007 +0000
+++ b/finch/libgnt/gntmenu.c	Sun Jul 01 12:21:30 2007 +0000
@@ -329,6 +329,7 @@
 		GNT_TREE(widget)->show_separator = FALSE;
 		_gnt_tree_init_internals(GNT_TREE(widget), 2);
 		gnt_tree_set_col_width(GNT_TREE(widget), 1, 1);  /* The second column is to indicate that it has a submenu */
+		gnt_tree_set_column_resizable(GNT_TREE(widget), 1, FALSE);
 		GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_NO_BORDER);
 	}
 
--- a/finch/libgnt/gnttree.c	Sun Jul 01 07:15:16 2007 +0000
+++ b/finch/libgnt/gnttree.c	Sun Jul 01 12:21:30 2007 +0000
@@ -31,6 +31,8 @@
 #define SEARCH_TIMEOUT 4000   /* 4 secs */
 #define SEARCHING(tree)  (tree->search && tree->search->len > 0)
 
+#define COLUMN_INVISIBLE(tree, index)  (tree->columns[index].flags & GNT_TREE_COLUMN_INVISIBLE)
+
 enum
 {
 	SIG_SELECTION_CHANGED,
@@ -75,6 +77,36 @@
 static GntWidgetClass *parent_class = NULL;
 static guint signals[SIGS] = { 0 };
 
+static void
+readjust_columns(GntTree *tree)
+{
+	int i, col, total;
+	int width;
+#define WIDTH(i) tree->columns[i].width_ratio ? tree->columns[i].width_ratio : tree->columns[i].width
+	gnt_widget_get_size(GNT_WIDGET(tree), &width, NULL);
+	for (i = 0, total = 0; i < tree->ncol ; i++) {
+		if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE)
+			continue;
+		if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE)
+			width -= WIDTH(i);
+		else
+			total += WIDTH(i);
+	}
+
+	if (total == 0)
+		return;
+
+	for (i = 0; i < tree->ncol; i++) {
+		if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE)
+			continue;
+		if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE)
+			col = WIDTH(i);
+		else
+			col = WIDTH(i) * width / total;
+		gnt_tree_set_col_width(GNT_TREE(tree), i, col);
+	}
+}
+
 /* Move the item at position old to position new */
 static GList *
 g_list_reposition_child(GList *list, int old, int new)
@@ -249,7 +281,7 @@
 	gboolean notfirst = FALSE;
 	int lastvisible = tree->ncol;
 
-	while (lastvisible && tree->columns[lastvisible].invisible)
+	while (lastvisible && COLUMN_INVISIBLE(tree, lastvisible))
 		lastvisible--;
 
 	for (i = 0, iter = row->columns; i < tree->ncol && iter; i++, iter = iter->next)
@@ -261,7 +293,7 @@
 		gboolean cut = FALSE;
 		int width;
 
-		if (tree->columns[i].invisible)
+		if (COLUMN_INVISIBLE(tree, i))
 			continue;
 
 		if (i == lastvisible)
@@ -335,11 +367,11 @@
 
 	for (i = 0; i < tree->ncol - 1; i++)
 	{
-		if (!tree->columns[i].invisible) {
+		if (!COLUMN_INVISIBLE(tree, i)) {
 			notfirst = TRUE;
 			NEXT_X;
 		}
-		if (!tree->columns[i+1].invisible && notfirst)
+		if (!COLUMN_INVISIBLE(tree, i+1) && notfirst)
 			mvwaddch(widget->window, y, x, type);
 	}
 }
@@ -383,7 +415,7 @@
 
 		for (i = 0; i < tree->ncol; i++)
 		{
-			if (tree->columns[i].invisible) {
+			if (COLUMN_INVISIBLE(tree, i)) {
 				continue;
 			}
 			mvwaddstr(widget->window, pos, x + 1, tree->columns[i].title);
@@ -555,7 +587,7 @@
 		GntTree *tree = GNT_TREE(widget);
 		int i, width = 0;
 		for (i = 0; i < tree->ncol; i++)
-			if (!tree->columns[i].invisible)
+			if (!COLUMN_INVISIBLE(tree, i))
 				width += tree->columns[i].width + 1;
 		widget->priv.width = width;
 	}
@@ -829,16 +861,10 @@
 gnt_tree_size_changed(GntWidget *widget, int w, int h)
 {
 	GntTree *tree = GNT_TREE(widget);
-	int i;
-	int n = 0;
 	if (widget->priv.width <= 0)
 		return;
-	for (i = 0; i < tree->ncol; ++i)
-		n += tree->columns[i].width;
-	if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
-		tree->columns[tree->ncol - 1].width += widget->priv.width - n - 1 * tree->ncol;
-	else
-		tree->columns[tree->ncol - 1].width += widget->priv.width - n - 2 - 1 * tree->ncol;
+
+	readjust_columns(tree);
 }
 
 static gboolean
@@ -1524,6 +1550,8 @@
 	g_return_if_fail(col < tree->ncol);
 
 	tree->columns[col].width = width;
+	if (tree->columns[col].width_ratio == 0)
+		tree->columns[col].width_ratio = width;
 }
 
 void gnt_tree_set_column_title(GntTree *tree, int index, const char *title)
@@ -1598,7 +1626,7 @@
 	twidth = 1 + 2 * (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER));
 	for (i = 0; i < tree->ncol; i++) {
 		gnt_tree_set_col_width(tree, i, widths[i]);
-		if (!tree->columns[i].invisible)
+		if (!COLUMN_INVISIBLE(tree, i))
 			twidth += widths[i] + (tree->show_separator ? 1 : 0) + 1;
 	}
 	g_free(widths);
@@ -1613,9 +1641,32 @@
 	tree->hash = g_hash_table_new_full(hash, eq, kd, free_tree_row);
 }
 
+static void
+set_column_flag(GntTree *tree, int col, GntTreeColumnFlag flag, gboolean set)
+{
+	if (set)
+		tree->columns[col].flags |= flag;
+	else
+		tree->columns[col].flags &= ~flag;
+}
+
 void gnt_tree_set_column_visible(GntTree *tree, int col, gboolean vis)
 {
 	g_return_if_fail(col < tree->ncol);
-	tree->columns[col].invisible = !vis;
+	set_column_flag(tree, col, GNT_TREE_COLUMN_INVISIBLE, !vis);
+}
+
+void gnt_tree_set_column_resizable(GntTree *tree, int col, gboolean res)
+{
+	g_return_if_fail(col < tree->ncol);
+	set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, !res);
 }
 
+void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[])
+{
+	int i;
+	for (i = 0; i < tree->ncol && cols[i]; i++) {
+		tree->columns[i].width_ratio = cols[i];
+	}
+}
+
--- a/finch/libgnt/gnttree.h	Sun Jul 01 07:15:16 2007 +0000
+++ b/finch/libgnt/gnttree.h	Sun Jul 01 12:21:30 2007 +0000
@@ -40,10 +40,6 @@
 #define GNT_IS_TREE_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE((klass), GNT_TYPE_TREE))
 #define GNT_TREE_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj), GNT_TYPE_TREE, GntTreeClass))
 
-#define GNT_TREE_FLAGS(obj)				(GNT_TREE(obj)->priv.flags)
-#define GNT_TREE_SET_FLAGS(obj, flags)		(GNT_TREE_FLAGS(obj) |= flags)
-#define GNT_TREE_UNSET_FLAGS(obj, flags)	(GNT_TREE_FLAGS(obj) &= ~(flags))
-
 typedef struct _GntTree			GntTree;
 typedef struct _GntTreePriv		GntTreePriv;
 typedef struct _GntTreeClass		GntTreeClass;
@@ -51,6 +47,11 @@
 typedef struct _GntTreeRow		GntTreeRow;
 typedef struct _GntTreeCol		GntTreeCol;
 
+typedef enum {
+	GNT_TREE_COLUMN_INVISIBLE    = 1 << 0,
+	GNT_TREE_COLUMN_FIXED_SIZE   = 1 << 1,
+} GntTreeColumnFlag;
+
 struct _GntTree
 {
 	GntWidget parent;
@@ -74,7 +75,8 @@
 	{
 		int width;
 		char *title;
-		gboolean invisible;
+		int width_ratio;
+		GntTreeColumnFlag flags;
 	} *columns;             /* Would a GList be better? */
 	gboolean show_title;
 	gboolean show_separator; /* Whether to show column separators */
@@ -368,16 +370,39 @@
  */
 void gnt_tree_set_hash_fns(GntTree *tree, gpointer hash, gpointer eq, gpointer kd);
 
-/* This can be useful when, for example, we want to store some data
- * which we don't want/need to display. */
 /**
+ * Set whether a column is visible or not.
+ * This can be useful when, for example, we want to store some data
+ * which we don't want/need to display.
  * 
- * @param tree
- * @param col
- * @param vis
+ * @param tree  The tree
+ * @param col   The index of the column
+ * @param vis   If @c FALSE, the column will not be displayed
  */
 void gnt_tree_set_column_visible(GntTree *tree, int col, gboolean vis);
 
+/**
+ * Set whether a column can be resized to keep the same ratio when the
+ * tree is resized.
+ * 
+ * @param tree  The tree
+ * @param col   The index of the column
+ * @param res   If @c FALSE, the column will not be resized when the
+ *              tree is resized
+ */
+void gnt_tree_set_column_resizable(GntTree *tree, int col, gboolean res);
+
+/**
+ * Set column widths to use when calculating column widths after a tree
+ * is resized.
+ *
+ * @param tree   The tree
+ * @param cols   Array of widths. The width must have the same number
+ *               of entries as the number of columns in the tree, or
+ *               end with a negative value for a column-width.
+ */
+void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[]);
+
 G_END_DECLS
 
 /* The following functions should NOT be used by applications. */