changeset 18420:e2b8b17fc62c

Allow storing non-string binary data in tree columns.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Tue, 03 Jul 2007 04:47:24 +0000
parents e79da0369a6d
children e16d097c5739
files finch/libgnt/gnttree.c finch/libgnt/gnttree.h
diffstat 2 files changed, 47 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/finch/libgnt/gnttree.c	Tue Jul 03 01:50:16 2007 +0000
+++ b/finch/libgnt/gnttree.c	Tue Jul 03 04:47:24 2007 +0000
@@ -32,6 +32,7 @@
 #define SEARCHING(tree)  (tree->search && tree->search->len > 0)
 
 #define COLUMN_INVISIBLE(tree, index)  (tree->columns[index].flags & GNT_TREE_COLUMN_INVISIBLE)
+#define BINARY_DATA(tree, index)       (tree->columns[index].flags & GNT_TREE_COLUMN_BINARY_DATA)
 
 enum
 {
@@ -69,6 +70,7 @@
 struct _GntTreeCol
 {
 	char *text;
+	gboolean isbinary;
 	int span;       /* How many columns does it span? */
 };
 
@@ -136,6 +138,8 @@
 {
 	GntTree *t = row->tree;
 	if (t->search && t->search->len > 0) {
+		/* XXX: Allow setting the search column. And make sure the search column
+		 * doesn't contain binary data. */
 		char *one = g_utf8_casefold(((GntTreeCol*)row->columns->data)->text, -1);
 		char *two = g_utf8_casefold(t->search->str, -1);
 		char *z = strstr(one, two);
@@ -288,14 +292,22 @@
 	{
 		GntTreeCol *col = iter->data;
 		const char *text;
-		int len = gnt_util_onscreen_width(col->text, NULL);
+		int len;
 		int fl = 0;
 		gboolean cut = FALSE;
 		int width;
+		const char *display;
 
 		if (COLUMN_INVISIBLE(tree, i))
 			continue;
 
+		if (BINARY_DATA(tree, i))
+			display = "";
+		else
+			display = col->text;
+
+		len = gnt_util_onscreen_width(display, NULL);
+
 		if (i == lastvisible)
 			width = GNT_WIDGET(tree)->priv.width - gnt_util_onscreen_width(string->str, NULL);
 		else
@@ -339,8 +351,8 @@
 			len = width - 1;
 			cut = TRUE;
 		}
-		text = gnt_util_onscreen_width_to_pointer(col->text, len - fl, NULL);
-		string = g_string_append_len(string, col->text, text - col->text);
+		text = gnt_util_onscreen_width_to_pointer(display, len - fl, NULL);
+		string = g_string_append_len(string, display, text - display);
 		if (cut) { /* ellipsis */
 			if (gnt_ascii_only())
 				g_string_append_c(string, '~');
@@ -1003,8 +1015,8 @@
 free_tree_col(gpointer data)
 {
 	GntTreeCol *col = data;
-
-	g_free(col->text);
+	if (col->isbinary)
+		g_free(col->text);
 	g_free(col);
 }
 
@@ -1390,8 +1402,12 @@
 	if (row)
 	{
 		col = g_list_nth_data(row->columns, colno);
-		g_free(col->text);
-		col->text = g_strdup(text ? text : "");
+		if (BINARY_DATA(tree, colno)) {
+			col->text = (gpointer)text;
+		} else {
+			g_free(col->text);
+			col->text = g_strdup(text ? text : "");
+		}
 
 		if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0)
 			redraw_tree(tree);
@@ -1517,7 +1533,13 @@
 	{
 		GntTreeCol *col = g_new0(GntTreeCol, 1);
 		col->span = 1;
-		col->text = g_strdup(iter->data ? iter->data : "");
+		if (BINARY_DATA(tree, i)) {
+			col->text = iter->data;
+			col->isbinary = TRUE;
+		} else {
+			col->text = g_strdup(iter->data ? iter->data : "");
+			col->isbinary = FALSE;
+		}
 
 		row->columns = g_list_append(row->columns, col);
 	}
@@ -1662,6 +1684,12 @@
 	set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, !res);
 }
 
+void gnt_tree_set_column_is_binary(GntTree *tree, int col, gboolean bin)
+{
+	g_return_if_fail(col < tree->ncol);
+	set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, bin);
+}
+
 void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[])
 {
 	int i;
--- a/finch/libgnt/gnttree.h	Tue Jul 03 01:50:16 2007 +0000
+++ b/finch/libgnt/gnttree.h	Tue Jul 03 04:47:24 2007 +0000
@@ -50,6 +50,7 @@
 typedef enum {
 	GNT_TREE_COLUMN_INVISIBLE    = 1 << 0,
 	GNT_TREE_COLUMN_FIXED_SIZE   = 1 << 1,
+	GNT_TREE_COLUMN_BINARY_DATA  = 1 << 2,
 } GntTreeColumnFlag;
 
 struct _GntTree
@@ -469,6 +470,16 @@
 void gnt_tree_set_column_resizable(GntTree *tree, int col, gboolean res);
 
 /**
+ * Set whether data in a column should be considered as binary data, and
+ * not as strings. A column containing binary data will be display empty text.
+ *
+ * @param tree  The tree
+ * @param col   The index of the column
+ * @param bin   @c TRUE if the data for the column is binary
+ */
+void gnt_tree_set_column_is_binary(GntTree *tree, int col, gboolean bin);
+
+/**
  * Set column widths to use when calculating column widths after a tree
  * is resized.
  *