comparison finch/libgnt/gnttree.c @ 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 69cc1a4ef6ab
children be10fc22d649
comparison
equal deleted inserted replaced
18419:e79da0369a6d 18420:e2b8b17fc62c
30 30
31 #define SEARCH_TIMEOUT 4000 /* 4 secs */ 31 #define SEARCH_TIMEOUT 4000 /* 4 secs */
32 #define SEARCHING(tree) (tree->search && tree->search->len > 0) 32 #define SEARCHING(tree) (tree->search && tree->search->len > 0)
33 33
34 #define COLUMN_INVISIBLE(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_INVISIBLE) 34 #define COLUMN_INVISIBLE(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_INVISIBLE)
35 #define BINARY_DATA(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_BINARY_DATA)
35 36
36 enum 37 enum
37 { 38 {
38 SIG_SELECTION_CHANGED, 39 SIG_SELECTION_CHANGED,
39 SIG_SCROLLED, 40 SIG_SCROLLED,
67 }; 68 };
68 69
69 struct _GntTreeCol 70 struct _GntTreeCol
70 { 71 {
71 char *text; 72 char *text;
73 gboolean isbinary;
72 int span; /* How many columns does it span? */ 74 int span; /* How many columns does it span? */
73 }; 75 };
74 76
75 static void tree_selection_changed(GntTree *, GntTreeRow *, GntTreeRow *); 77 static void tree_selection_changed(GntTree *, GntTreeRow *, GntTreeRow *);
76 78
134 static gboolean 136 static gboolean
135 row_matches_search(GntTreeRow *row) 137 row_matches_search(GntTreeRow *row)
136 { 138 {
137 GntTree *t = row->tree; 139 GntTree *t = row->tree;
138 if (t->search && t->search->len > 0) { 140 if (t->search && t->search->len > 0) {
141 /* XXX: Allow setting the search column. And make sure the search column
142 * doesn't contain binary data. */
139 char *one = g_utf8_casefold(((GntTreeCol*)row->columns->data)->text, -1); 143 char *one = g_utf8_casefold(((GntTreeCol*)row->columns->data)->text, -1);
140 char *two = g_utf8_casefold(t->search->str, -1); 144 char *two = g_utf8_casefold(t->search->str, -1);
141 char *z = strstr(one, two); 145 char *z = strstr(one, two);
142 g_free(one); 146 g_free(one);
143 g_free(two); 147 g_free(two);
286 290
287 for (i = 0, iter = row->columns; i < tree->ncol && iter; i++, iter = iter->next) 291 for (i = 0, iter = row->columns; i < tree->ncol && iter; i++, iter = iter->next)
288 { 292 {
289 GntTreeCol *col = iter->data; 293 GntTreeCol *col = iter->data;
290 const char *text; 294 const char *text;
291 int len = gnt_util_onscreen_width(col->text, NULL); 295 int len;
292 int fl = 0; 296 int fl = 0;
293 gboolean cut = FALSE; 297 gboolean cut = FALSE;
294 int width; 298 int width;
299 const char *display;
295 300
296 if (COLUMN_INVISIBLE(tree, i)) 301 if (COLUMN_INVISIBLE(tree, i))
297 continue; 302 continue;
303
304 if (BINARY_DATA(tree, i))
305 display = "";
306 else
307 display = col->text;
308
309 len = gnt_util_onscreen_width(display, NULL);
298 310
299 if (i == lastvisible) 311 if (i == lastvisible)
300 width = GNT_WIDGET(tree)->priv.width - gnt_util_onscreen_width(string->str, NULL); 312 width = GNT_WIDGET(tree)->priv.width - gnt_util_onscreen_width(string->str, NULL);
301 else 313 else
302 width = tree->columns[i].width; 314 width = tree->columns[i].width;
337 349
338 if (len > width) { 350 if (len > width) {
339 len = width - 1; 351 len = width - 1;
340 cut = TRUE; 352 cut = TRUE;
341 } 353 }
342 text = gnt_util_onscreen_width_to_pointer(col->text, len - fl, NULL); 354 text = gnt_util_onscreen_width_to_pointer(display, len - fl, NULL);
343 string = g_string_append_len(string, col->text, text - col->text); 355 string = g_string_append_len(string, display, text - display);
344 if (cut) { /* ellipsis */ 356 if (cut) { /* ellipsis */
345 if (gnt_ascii_only()) 357 if (gnt_ascii_only())
346 g_string_append_c(string, '~'); 358 g_string_append_c(string, '~');
347 else 359 else
348 string = g_string_append(string, "\342\200\246"); 360 string = g_string_append(string, "\342\200\246");
1001 1013
1002 static void 1014 static void
1003 free_tree_col(gpointer data) 1015 free_tree_col(gpointer data)
1004 { 1016 {
1005 GntTreeCol *col = data; 1017 GntTreeCol *col = data;
1006 1018 if (col->isbinary)
1007 g_free(col->text); 1019 g_free(col->text);
1008 g_free(col); 1020 g_free(col);
1009 } 1021 }
1010 1022
1011 static void 1023 static void
1012 free_tree_row(gpointer data) 1024 free_tree_row(gpointer data)
1388 1400
1389 row = g_hash_table_lookup(tree->hash, key); 1401 row = g_hash_table_lookup(tree->hash, key);
1390 if (row) 1402 if (row)
1391 { 1403 {
1392 col = g_list_nth_data(row->columns, colno); 1404 col = g_list_nth_data(row->columns, colno);
1393 g_free(col->text); 1405 if (BINARY_DATA(tree, colno)) {
1394 col->text = g_strdup(text ? text : ""); 1406 col->text = (gpointer)text;
1407 } else {
1408 g_free(col->text);
1409 col->text = g_strdup(text ? text : "");
1410 }
1395 1411
1396 if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0) 1412 if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0)
1397 redraw_tree(tree); 1413 redraw_tree(tree);
1398 } 1414 }
1399 } 1415 }
1515 1531
1516 for (i = 0, iter = list; i < tree->ncol && iter; iter = iter->next, i++) 1532 for (i = 0, iter = list; i < tree->ncol && iter; iter = iter->next, i++)
1517 { 1533 {
1518 GntTreeCol *col = g_new0(GntTreeCol, 1); 1534 GntTreeCol *col = g_new0(GntTreeCol, 1);
1519 col->span = 1; 1535 col->span = 1;
1520 col->text = g_strdup(iter->data ? iter->data : ""); 1536 if (BINARY_DATA(tree, i)) {
1537 col->text = iter->data;
1538 col->isbinary = TRUE;
1539 } else {
1540 col->text = g_strdup(iter->data ? iter->data : "");
1541 col->isbinary = FALSE;
1542 }
1521 1543
1522 row->columns = g_list_append(row->columns, col); 1544 row->columns = g_list_append(row->columns, col);
1523 } 1545 }
1524 1546
1525 return row; 1547 return row;
1660 { 1682 {
1661 g_return_if_fail(col < tree->ncol); 1683 g_return_if_fail(col < tree->ncol);
1662 set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, !res); 1684 set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, !res);
1663 } 1685 }
1664 1686
1687 void gnt_tree_set_column_is_binary(GntTree *tree, int col, gboolean bin)
1688 {
1689 g_return_if_fail(col < tree->ncol);
1690 set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, bin);
1691 }
1692
1665 void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[]) 1693 void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[])
1666 { 1694 {
1667 int i; 1695 int i;
1668 for (i = 0; i < tree->ncol && cols[i]; i++) { 1696 for (i = 0; i < tree->ncol && cols[i]; i++) {
1669 tree->columns[i].width_ratio = cols[i]; 1697 tree->columns[i].width_ratio = cols[i];