Update to 24f58c58bb8d22c0e8e6c5ce43c536c47b719bc6
[gnt.git] / gnttree.c
blob8d8260a8e39b1c9774a531f86ef42da243829fb5
1 /**
2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "gntmarshal.h"
24 #include "gntstyle.h"
25 #include "gnttree.h"
26 #include "gntutils.h"
28 #include <string.h>
29 #include <ctype.h>
31 #define SEARCH_TIMEOUT 4000 /* 4 secs */
32 #define SEARCHING(tree) (tree->priv->search && tree->priv->search->len > 0)
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)
36 #define RIGHT_ALIGNED(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_RIGHT_ALIGNED)
38 enum
40 PROP_0,
41 PROP_COLUMNS,
42 PROP_EXPANDER,
45 enum
47 SIG_SELECTION_CHANGED,
48 SIG_SCROLLED,
49 SIG_TOGGLED,
50 SIG_COLLAPSED,
51 SIGS,
54 struct _GntTreePriv
56 GString *search;
57 int search_timeout;
58 int search_column;
59 gboolean (*search_func)(GntTree *tree, gpointer key, const char *search, const char *current);
61 GCompareFunc compare;
62 int lastvisible;
63 int expander_level;
66 #define TAB_SIZE 3
68 /* XXX: Make this one into a GObject?
69 * ... Probably not */
70 struct _GntTreeRow
72 void *key;
73 void *data; /* XXX: unused */
75 gboolean collapsed;
76 gboolean choice; /* Is this a choice-box?
77 If choice is true, then child will be NULL */
78 gboolean isselected;
79 GntTextFormatFlags flags;
80 int color;
82 GntTreeRow *parent;
83 GntTreeRow *child;
84 GntTreeRow *next;
85 GntTreeRow *prev;
87 GList *columns;
88 GntTree *tree;
91 struct _GntTreeCol
93 char *text;
94 gboolean isbinary;
95 int span; /* How many columns does it span? */
98 static void tree_selection_changed(GntTree *, GntTreeRow *, GntTreeRow *);
99 static void _gnt_tree_init_internals(GntTree *tree, int col);
101 static GntWidgetClass *parent_class = NULL;
102 static guint signals[SIGS] = { 0 };
104 static void
105 readjust_columns(GntTree *tree)
107 int i, col, total;
108 int width;
109 #define WIDTH(i) (tree->columns[i].width_ratio ? tree->columns[i].width_ratio : tree->columns[i].width)
110 gnt_widget_get_size(GNT_WIDGET(tree), &width, NULL);
111 if (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER))
112 width -= 2;
113 for (i = 0, total = 0; i < tree->ncol ; i++) {
114 if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE)
115 continue;
116 if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE)
117 width -= WIDTH(i) + 1;
118 else
119 total += WIDTH(i) + 1;
122 if (total == 0)
123 return;
125 for (i = 0; i < tree->ncol; i++) {
126 if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE)
127 continue;
128 if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE)
129 col = WIDTH(i);
130 else
131 col = (WIDTH(i) * width) / total;
132 gnt_tree_set_col_width(GNT_TREE(tree), i, col);
136 /* Move the item at position old to position new */
137 static GList *
138 g_list_reposition_child(GList *list, int old, int new)
140 gpointer item = g_list_nth_data(list, old);
141 list = g_list_remove(list, item);
142 if (old < new)
143 new--; /* because the positions would have shifted after removing the item */
144 list = g_list_insert(list, item, new);
145 return list;
148 static GntTreeRow *
149 _get_next(GntTreeRow *row, gboolean godeep)
151 if (row == NULL)
152 return NULL;
153 if (godeep && row->child)
154 return row->child;
155 if (row->next)
156 return row->next;
157 return _get_next(row->parent, FALSE);
160 static gboolean
161 row_matches_search(GntTreeRow *row)
163 GntTree *t = row->tree;
164 if (t->priv->search && t->priv->search->len > 0) {
165 GntTreeCol *col = (col = g_list_nth_data(row->columns, t->priv->search_column)) ? col : row->columns->data;
166 char *one, *two, *z;
167 if (t->priv->search_func)
168 return t->priv->search_func(t, row->key, t->priv->search->str, col->text);
169 one = g_utf8_casefold(col->text, -1);
170 two = g_utf8_casefold(t->priv->search->str, -1);
171 z = strstr(one, two);
172 g_free(one);
173 g_free(two);
174 if (z == NULL)
175 return FALSE;
177 return TRUE;
180 static GntTreeRow *
181 get_next(GntTreeRow *row)
183 if (row == NULL)
184 return NULL;
185 while ((row = _get_next(row, !row->collapsed)) != NULL) {
186 if (row_matches_search(row))
187 break;
189 return row;
192 /* Returns the n-th next row. If it doesn't exist, returns NULL */
193 static GntTreeRow *
194 get_next_n(GntTreeRow *row, int n)
196 while (row && n--)
197 row = get_next(row);
198 return row;
201 /* Returns the n-th next row. If it doesn't exist, then the last non-NULL node */
202 static GntTreeRow *
203 get_next_n_opt(GntTreeRow *row, int n, int *pos)
205 GntTreeRow *next = row;
206 int r = 0;
208 if (row == NULL)
209 return NULL;
211 while (row && n--)
213 row = get_next(row);
214 if (row)
216 next = row;
217 r++;
221 if (pos)
222 *pos = r;
224 return next;
227 static GntTreeRow *
228 get_last_child(GntTreeRow *row)
230 if (row == NULL)
231 return NULL;
232 if (!row->collapsed && row->child)
233 row = row->child;
234 else
235 return row;
237 while(row->next)
238 row = row->next;
239 return get_last_child(row);
242 static GntTreeRow *
243 get_prev(GntTreeRow *row)
245 if (row == NULL)
246 return NULL;
247 while (row) {
248 if (row->prev)
249 row = get_last_child(row->prev);
250 else
251 row = row->parent;
252 if (!row || row_matches_search(row))
253 break;
255 return row;
258 static GntTreeRow *
259 get_prev_n(GntTreeRow *row, int n)
261 while (row && n--)
262 row = get_prev(row);
263 return row;
266 /* Distance of row from the root */
267 /* XXX: This is uber-inefficient */
268 static int
269 get_root_distance(GntTreeRow *row)
271 if (row == NULL)
272 return -1;
273 return get_root_distance(get_prev(row)) + 1;
276 /* Returns the distance between a and b.
277 * If a is 'above' b, then the distance is positive */
278 static int
279 get_distance(GntTreeRow *a, GntTreeRow *b)
281 /* First get the distance from a to the root.
282 * Then the distance from b to the root.
283 * Subtract.
284 * It's not that good, but it works. */
285 int ha = get_root_distance(a);
286 int hb = get_root_distance(b);
288 return (hb - ha);
291 static int
292 find_depth(GntTreeRow *row)
294 int dep = -1;
296 while (row)
298 dep++;
299 row = row->parent;
302 return dep;
305 static char *
306 update_row_text(GntTree *tree, GntTreeRow *row)
308 GString *string = g_string_new(NULL);
309 GList *iter;
310 int i;
311 gboolean notfirst = FALSE;
313 for (i = 0, iter = row->columns; i < tree->ncol && iter; i++, iter = iter->next)
315 GntTreeCol *col = iter->data;
316 const char *text;
317 int len;
318 int fl = 0;
319 gboolean cut = FALSE;
320 int width;
321 const char *display;
323 if (COLUMN_INVISIBLE(tree, i))
324 continue;
326 if (BINARY_DATA(tree, i))
327 display = "";
328 else
329 display = col->text;
331 len = gnt_util_onscreen_width(display, NULL);
333 width = tree->columns[i].width;
335 if (i == 0)
337 if (row->choice)
339 g_string_append_printf(string, "[%c] ",
340 row->isselected ? 'X' : ' ');
341 fl = 4;
343 else if (find_depth(row) < tree->priv->expander_level && row->child)
345 if (row->collapsed)
347 string = g_string_append(string, "+ ");
349 else
351 string = g_string_append(string, "- ");
353 fl = 2;
355 else
357 fl = TAB_SIZE * find_depth(row);
358 g_string_append_printf(string, "%*s", fl, "");
360 len += fl;
361 } else if (notfirst && tree->show_separator)
362 g_string_append_c(string, '|');
363 else
364 g_string_append_c(string, ' ');
366 notfirst = TRUE;
368 if (len > width) {
369 len = MAX(1, width - 1);
370 cut = TRUE;
373 if (RIGHT_ALIGNED(tree, i) && len < tree->columns[i].width) {
374 g_string_append_printf(string, "%*s", width - len - cut, "");
377 text = gnt_util_onscreen_width_to_pointer(display, len - fl, NULL);
378 string = g_string_append_len(string, display, text - display);
379 if (cut && width > 1) { /* ellipsis */
380 if (gnt_ascii_only())
381 g_string_append_c(string, '~');
382 else
383 string = g_string_append(string, "\342\200\246");
384 len++;
387 if (!RIGHT_ALIGNED(tree, i) && len < tree->columns[i].width && iter->next)
388 g_string_append_printf(string, "%*s", width - len, "");
390 return g_string_free(string, FALSE);
393 #define NEXT_X x += tree->columns[i].width + (i > 0 ? 1 : 0)
395 static void
396 tree_mark_columns(GntTree *tree, int pos, int y, chtype type)
398 GntWidget *widget = GNT_WIDGET(tree);
399 int i;
400 int x = pos;
401 gboolean notfirst = FALSE;
403 for (i = 0; i < tree->ncol - 1; i++)
405 if (!COLUMN_INVISIBLE(tree, i)) {
406 notfirst = TRUE;
407 NEXT_X;
409 if (!COLUMN_INVISIBLE(tree, i+1) && notfirst)
410 mvwaddch(widget->window, y, x, type);
414 static void
415 redraw_tree(GntTree *tree)
417 int start, i;
418 GntWidget *widget = GNT_WIDGET(tree);
419 GntTreeRow *row;
420 int pos, up, down = 0;
421 int rows, scrcol;
423 if (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED))
424 return;
426 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
427 pos = 0;
428 else
429 pos = 1;
431 if (tree->top == NULL)
432 tree->top = tree->root;
433 if (tree->current == NULL) {
434 tree->current = tree->root;
435 tree_selection_changed(tree, NULL, tree->current);
438 wbkgd(widget->window, gnt_color_pair(GNT_COLOR_NORMAL));
440 start = 0;
441 if (tree->show_title)
443 int i;
444 int x = pos;
446 mvwhline(widget->window, pos + 1, pos, ACS_HLINE | gnt_color_pair(GNT_COLOR_NORMAL),
447 widget->priv.width - pos - 1);
448 mvwhline(widget->window, pos, pos, ' ' | gnt_color_pair(GNT_COLOR_NORMAL),
449 widget->priv.width - pos - 1);
451 for (i = 0; i < tree->ncol; i++)
453 if (COLUMN_INVISIBLE(tree, i)) {
454 continue;
456 mvwaddnstr(widget->window, pos, x + (x != pos), tree->columns[i].title, tree->columns[i].width);
457 NEXT_X;
459 if (pos)
461 tree_mark_columns(tree, pos, 0,
462 (tree->show_separator ? ACS_TTEE : ACS_HLINE) | gnt_color_pair(GNT_COLOR_NORMAL));
463 tree_mark_columns(tree, pos, widget->priv.height - pos,
464 (tree->show_separator ? ACS_BTEE : ACS_HLINE) | gnt_color_pair(GNT_COLOR_NORMAL));
466 tree_mark_columns(tree, pos, pos + 1,
467 (tree->show_separator ? ACS_PLUS : ACS_HLINE) | gnt_color_pair(GNT_COLOR_NORMAL));
468 tree_mark_columns(tree, pos, pos,
469 (tree->show_separator ? ACS_VLINE : ' ') | gnt_color_pair(GNT_COLOR_NORMAL));
470 start = 2;
473 rows = widget->priv.height - pos * 2 - start - 1;
474 tree->bottom = get_next_n_opt(tree->top, rows, &down);
475 if (down < rows)
477 tree->top = get_prev_n(tree->bottom, rows);
478 if (tree->top == NULL)
479 tree->top = tree->root;
482 up = get_distance(tree->top, tree->current);
483 if (up < 0)
484 tree->top = tree->current;
485 else if (up >= widget->priv.height - pos)
486 tree->top = get_prev_n(tree->current, rows);
488 if (tree->top && !row_matches_search(tree->top))
489 tree->top = get_next(tree->top);
490 row = tree->top;
491 scrcol = widget->priv.width - 1 - 2 * pos; /* exclude the borders and the scrollbar */
492 for (i = start + pos; row && i < widget->priv.height - pos;
493 i++, row = get_next(row))
495 char *str;
496 int wr;
498 GntTextFormatFlags flags = row->flags;
499 int attr = 0;
501 if (!row_matches_search(row))
502 continue;
503 str = update_row_text(tree, row);
505 if ((wr = gnt_util_onscreen_width(str, NULL)) > scrcol)
507 char *s = (char*)gnt_util_onscreen_width_to_pointer(str, scrcol, &wr);
508 *s = '\0';
511 if (flags & GNT_TEXT_FLAG_BOLD)
512 attr |= A_BOLD;
513 if (flags & GNT_TEXT_FLAG_UNDERLINE)
514 attr |= A_UNDERLINE;
515 if (flags & GNT_TEXT_FLAG_BLINK)
516 attr |= A_BLINK;
518 if (row == tree->current)
520 if (gnt_widget_has_focus(widget))
521 attr |= gnt_color_pair(GNT_COLOR_HIGHLIGHT);
522 else
523 attr |= gnt_color_pair(GNT_COLOR_HIGHLIGHT_D);
525 else
527 if (flags & GNT_TEXT_FLAG_DIM)
528 if (row->color)
529 attr |= (A_DIM | gnt_color_pair(row->color));
530 else
531 attr |= (A_DIM | gnt_color_pair(GNT_COLOR_DISABLED));
532 else if (flags & GNT_TEXT_FLAG_HIGHLIGHT)
533 attr |= (A_DIM | gnt_color_pair(GNT_COLOR_HIGHLIGHT));
534 else if (row->color)
535 attr |= gnt_color_pair(row->color);
536 else
537 attr |= gnt_color_pair(GNT_COLOR_NORMAL);
540 wbkgdset(widget->window, '\0' | attr);
541 mvwaddstr(widget->window, i, pos, str);
542 whline(widget->window, ' ', scrcol - wr);
543 tree->bottom = row;
544 g_free(str);
545 tree_mark_columns(tree, pos, i,
546 (tree->show_separator ? ACS_VLINE : ' ') | attr);
549 wbkgdset(widget->window, '\0' | gnt_color_pair(GNT_COLOR_NORMAL));
550 while (i < widget->priv.height - pos)
552 mvwhline(widget->window, i, pos, ' ',
553 widget->priv.width - pos * 2 - 1);
554 tree_mark_columns(tree, pos, i,
555 (tree->show_separator ? ACS_VLINE : ' '));
556 i++;
559 scrcol = widget->priv.width - pos - 1; /* position of the scrollbar */
560 rows--;
561 if (rows > 0)
563 int total = 0;
564 int showing, position;
566 get_next_n_opt(tree->root, g_list_length(tree->list), &total);
567 showing = rows * rows / MAX(total, 1) + 1;
568 showing = MIN(rows, showing);
570 total -= rows;
571 up = get_distance(tree->root, tree->top);
572 down = total - up;
574 position = (rows - showing) * up / MAX(1, up + down);
575 position = MAX((tree->top != tree->root), position);
577 if (showing + position > rows)
578 position = rows - showing;
580 if (showing + position == rows && row)
581 position = MAX(0, rows - 1 - showing);
582 else if (showing + position < rows && !row)
583 position = rows - showing;
585 position += pos + start + 1;
587 mvwvline(widget->window, pos + start + 1, scrcol,
588 ' ' | gnt_color_pair(GNT_COLOR_NORMAL), rows);
589 mvwvline(widget->window, position, scrcol,
590 ACS_CKBOARD | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D), showing);
593 mvwaddch(widget->window, start + pos, scrcol,
594 ((tree->top != tree->root) ? ACS_UARROW : ' ') |
595 gnt_color_pair(GNT_COLOR_HIGHLIGHT_D));
597 mvwaddch(widget->window, widget->priv.height - pos - 1, scrcol,
598 (row ? ACS_DARROW : ' ') | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D));
600 /* If there's a search-text, show it in the bottom of the tree */
601 if (tree->priv->search && tree->priv->search->len > 0) {
602 const char *str = gnt_util_onscreen_width_to_pointer(tree->priv->search->str, scrcol - 1, NULL);
603 wbkgdset(widget->window, '\0' | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D));
604 mvwaddnstr(widget->window, widget->priv.height - pos - 1, pos,
605 tree->priv->search->str, str - tree->priv->search->str);
608 gnt_widget_queue_update(widget);
611 static void
612 gnt_tree_draw(GntWidget *widget)
614 GntTree *tree = GNT_TREE(widget);
616 redraw_tree(tree);
618 GNTDEBUG;
621 static void
622 gnt_tree_size_request(GntWidget *widget)
624 if (widget->priv.height == 0)
625 widget->priv.height = 10; /* XXX: Why?! */
626 if (widget->priv.width == 0)
628 GntTree *tree = GNT_TREE(widget);
629 int i, width = 0;
630 width = 1 + 2 * (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER));
631 for (i = 0; i < tree->ncol; i++)
632 if (!COLUMN_INVISIBLE(tree, i)) {
633 width = width + tree->columns[i].width;
634 if (tree->priv->lastvisible != i)
635 width++;
637 widget->priv.width = width;
641 static void
642 gnt_tree_map(GntWidget *widget)
644 GntTree *tree = GNT_TREE(widget);
645 if (widget->priv.width == 0 || widget->priv.height == 0)
647 gnt_widget_size_request(widget);
649 tree->top = tree->root;
650 tree->current = tree->root;
651 GNTDEBUG;
654 static void
655 tree_selection_changed(GntTree *tree, GntTreeRow *old, GntTreeRow *current)
657 g_signal_emit(tree, signals[SIG_SELECTION_CHANGED], 0, old ? old->key : NULL,
658 current ? current->key : NULL);
661 static gboolean
662 action_down(GntBindable *bind, GList *null)
664 int dist;
665 GntTree *tree = GNT_TREE(bind);
666 GntTreeRow *old = tree->current;
667 GntTreeRow *row = get_next(tree->current);
668 if (row == NULL)
669 return FALSE;
670 tree->current = row;
671 if ((dist = get_distance(tree->current, tree->bottom)) < 0)
672 gnt_tree_scroll(tree, -dist);
673 else
674 redraw_tree(tree);
675 if (old != tree->current)
676 tree_selection_changed(tree, old, tree->current);
677 return TRUE;
680 static gboolean
681 action_move_parent(GntBindable *bind, GList *null)
683 GntTree *tree = GNT_TREE(bind);
684 GntTreeRow *row = tree->current;
685 int dist;
687 if (!row || !row->parent || SEARCHING(tree))
688 return FALSE;
690 tree->current = row->parent;
691 if ((dist = get_distance(tree->current, tree->top)) > 0)
692 gnt_tree_scroll(tree, -dist);
693 else
694 redraw_tree(tree);
695 tree_selection_changed(tree, row, tree->current);
696 return TRUE;
699 static gboolean
700 action_up(GntBindable *bind, GList *list)
702 int dist;
703 GntTree *tree = GNT_TREE(bind);
704 GntTreeRow *old = tree->current;
705 GntTreeRow *row = get_prev(tree->current);
706 if (!row)
707 return FALSE;
708 tree->current = row;
709 if ((dist = get_distance(tree->current, tree->top)) > 0)
710 gnt_tree_scroll(tree, -dist);
711 else
712 redraw_tree(tree);
713 if (old != tree->current)
714 tree_selection_changed(tree, old, tree->current);
716 return TRUE;
719 static gboolean
720 action_page_down(GntBindable *bind, GList *null)
722 GntTree *tree = GNT_TREE(bind);
723 GntTreeRow *old = tree->current;
724 GntTreeRow *row = get_next(tree->bottom);
725 if (row)
727 int dist = get_distance(tree->top, tree->current);
728 tree->top = tree->bottom;
729 tree->current = get_next_n_opt(tree->top, dist, NULL);
730 redraw_tree(tree);
732 else if (tree->current != tree->bottom)
734 tree->current = tree->bottom;
735 redraw_tree(tree);
738 if (old != tree->current)
739 tree_selection_changed(tree, old, tree->current);
740 return TRUE;
743 static gboolean
744 action_page_up(GntBindable *bind, GList *null)
746 GntWidget *widget = GNT_WIDGET(bind);
747 GntTree *tree = GNT_TREE(bind);
748 GntTreeRow *row;
749 GntTreeRow *old = tree->current;
751 if (tree->top != tree->root)
753 int dist = get_distance(tree->top, tree->current);
754 row = get_prev_n(tree->top, widget->priv.height - 1 -
755 tree->show_title * 2 - 2 * (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER) == 0));
756 if (row == NULL)
757 row = tree->root;
758 tree->top = row;
759 tree->current = get_next_n_opt(tree->top, dist, NULL);
760 redraw_tree(tree);
762 else if (tree->current != tree->top)
764 tree->current = tree->top;
765 redraw_tree(tree);
767 if (old != tree->current)
768 tree_selection_changed(tree, old, tree->current);
769 return TRUE;
772 static void
773 end_search(GntTree *tree)
775 if (tree->priv->search) {
776 g_source_remove(tree->priv->search_timeout);
777 g_string_free(tree->priv->search, TRUE);
778 tree->priv->search = NULL;
779 tree->priv->search_timeout = 0;
780 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(tree), GNT_WIDGET_DISABLE_ACTIONS);
784 static gboolean
785 search_timeout(gpointer data)
787 GntTree *tree = data;
789 end_search(tree);
790 redraw_tree(tree);
792 return FALSE;
795 static gboolean
796 gnt_tree_key_pressed(GntWidget *widget, const char *text)
798 GntTree *tree = GNT_TREE(widget);
799 GntTreeRow *old = tree->current;
801 if (text[0] == '\r' || text[0] == '\n') {
802 end_search(tree);
803 gnt_widget_activate(widget);
804 } else if (tree->priv->search) {
805 gboolean changed = TRUE;
806 if (isalnum(*text)) {
807 tree->priv->search = g_string_append_c(tree->priv->search, *text);
808 } else if (g_utf8_collate(text, GNT_KEY_BACKSPACE) == 0) {
809 if (tree->priv->search->len)
810 tree->priv->search->str[--tree->priv->search->len] = '\0';
811 } else
812 changed = FALSE;
813 if (changed) {
814 redraw_tree(tree);
815 g_source_remove(tree->priv->search_timeout);
816 tree->priv->search_timeout = g_timeout_add(SEARCH_TIMEOUT, search_timeout, tree);
817 } else {
818 gnt_bindable_perform_action_key(GNT_BINDABLE(tree), text);
820 return TRUE;
821 } else if (text[0] == ' ' && text[1] == 0) {
822 /* Space pressed */
823 GntTreeRow *row = tree->current;
824 if (row && row->child)
826 row->collapsed = !row->collapsed;
827 redraw_tree(tree);
828 g_signal_emit(tree, signals[SIG_COLLAPSED], 0, row->key, row->collapsed);
830 else if (row && row->choice)
832 row->isselected = !row->isselected;
833 g_signal_emit(tree, signals[SIG_TOGGLED], 0, row->key);
834 redraw_tree(tree);
836 } else {
837 return FALSE;
840 if (old != tree->current)
842 tree_selection_changed(tree, old, tree->current);
845 return TRUE;
848 static void
849 gnt_tree_free_columns(GntTree *tree)
851 int i;
852 for (i = 0; i < tree->ncol; i++) {
853 g_free(tree->columns[i].title);
855 g_free(tree->columns);
858 static void
859 gnt_tree_destroy(GntWidget *widget)
861 GntTree *tree = GNT_TREE(widget);
863 end_search(tree);
864 if (tree->hash)
865 g_hash_table_destroy(tree->hash);
866 g_list_free(tree->list);
867 gnt_tree_free_columns(tree);
868 g_free(tree->priv);
871 static gboolean
872 gnt_tree_clicked(GntWidget *widget, GntMouseEvent event, int x, int y)
874 GntTree *tree = GNT_TREE(widget);
875 GntTreeRow *old = tree->current;
876 if (event == GNT_MOUSE_SCROLL_UP) {
877 action_up(GNT_BINDABLE(widget), NULL);
878 } else if (event == GNT_MOUSE_SCROLL_DOWN) {
879 action_down(GNT_BINDABLE(widget), NULL);
880 } else if (event == GNT_LEFT_MOUSE_DOWN) {
881 GntTreeRow *row;
882 GntTree *tree = GNT_TREE(widget);
883 int pos = 1;
884 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
885 pos = 0;
886 if (tree->show_title)
887 pos += 2;
888 pos = y - widget->priv.y - pos;
889 row = get_next_n(tree->top, pos);
890 if (row && tree->current != row) {
891 GntTreeRow *old = tree->current;
892 tree->current = row;
893 redraw_tree(tree);
894 tree_selection_changed(tree, old, tree->current);
895 } else if (row && row == tree->current) {
896 if (row->choice) {
897 row->isselected = !row->isselected;
898 g_signal_emit(tree, signals[SIG_TOGGLED], 0, row->key);
899 redraw_tree(tree);
900 } else {
901 gnt_widget_activate(widget);
904 } else {
905 return FALSE;
907 if (old != tree->current) {
908 tree_selection_changed(tree, old, tree->current);
910 return TRUE;
913 static void
914 gnt_tree_size_changed(GntWidget *widget, int w, int h)
916 GntTree *tree = GNT_TREE(widget);
917 if (widget->priv.width <= 0)
918 return;
920 readjust_columns(tree);
923 static gboolean
924 start_search(GntBindable *bindable, GList *list)
926 GntTree *tree = GNT_TREE(bindable);
927 if (tree->priv->search)
928 return FALSE;
929 GNT_WIDGET_SET_FLAGS(GNT_WIDGET(tree), GNT_WIDGET_DISABLE_ACTIONS);
930 tree->priv->search = g_string_new(NULL);
931 tree->priv->search_timeout = g_timeout_add(SEARCH_TIMEOUT, search_timeout, tree);
932 return TRUE;
935 static gboolean
936 end_search_action(GntBindable *bindable, GList *list)
938 GntTree *tree = GNT_TREE(bindable);
939 if (tree->priv->search == NULL)
940 return FALSE;
941 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(tree), GNT_WIDGET_DISABLE_ACTIONS);
942 end_search(tree);
943 redraw_tree(tree);
944 return TRUE;
947 static void
948 gnt_tree_set_property(GObject *obj, guint prop_id, const GValue *value,
949 GParamSpec *spec)
951 GntTree *tree = GNT_TREE(obj);
952 switch (prop_id) {
953 case PROP_COLUMNS:
954 _gnt_tree_init_internals(tree, g_value_get_int(value));
955 break;
956 case PROP_EXPANDER:
957 if (tree->priv->expander_level == g_value_get_int(value))
958 break;
959 tree->priv->expander_level = g_value_get_int(value);
960 g_object_notify(obj, "expander-level");
961 default:
962 break;
966 static void
967 gnt_tree_get_property(GObject *obj, guint prop_id, GValue *value,
968 GParamSpec *spec)
970 GntTree *tree = GNT_TREE(obj);
971 switch (prop_id) {
972 case PROP_COLUMNS:
973 g_value_set_int(value, tree->ncol);
974 break;
975 case PROP_EXPANDER:
976 g_value_set_int(value, tree->priv->expander_level);
977 break;
978 default:
979 break;
983 static void
984 gnt_tree_class_init(GntTreeClass *klass)
986 GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass);
987 GObjectClass *gclass = G_OBJECT_CLASS(klass);
989 parent_class = GNT_WIDGET_CLASS(klass);
990 parent_class->destroy = gnt_tree_destroy;
991 parent_class->draw = gnt_tree_draw;
992 parent_class->map = gnt_tree_map;
993 parent_class->size_request = gnt_tree_size_request;
994 parent_class->key_pressed = gnt_tree_key_pressed;
995 parent_class->clicked = gnt_tree_clicked;
996 parent_class->size_changed = gnt_tree_size_changed;
998 gclass->set_property = gnt_tree_set_property;
999 gclass->get_property = gnt_tree_get_property;
1000 g_object_class_install_property(gclass,
1001 PROP_COLUMNS,
1002 g_param_spec_int("columns", "Columns",
1003 "Number of columns in the tree.",
1004 1, G_MAXINT, 1,
1005 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
1008 g_object_class_install_property(gclass,
1009 PROP_EXPANDER,
1010 g_param_spec_int("expander-level", "Expander level",
1011 "Number of levels to show expander in the tree.",
1012 0, G_MAXINT, 1,
1013 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
1017 signals[SIG_SELECTION_CHANGED] =
1018 g_signal_new("selection-changed",
1019 G_TYPE_FROM_CLASS(klass),
1020 G_SIGNAL_RUN_LAST,
1021 G_STRUCT_OFFSET(GntTreeClass, selection_changed),
1022 NULL, NULL,
1023 gnt_closure_marshal_VOID__POINTER_POINTER,
1024 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
1025 signals[SIG_SCROLLED] =
1026 g_signal_new("scrolled",
1027 G_TYPE_FROM_CLASS(klass),
1028 G_SIGNAL_RUN_LAST,
1030 NULL, NULL,
1031 g_cclosure_marshal_VOID__INT,
1032 G_TYPE_NONE, 1, G_TYPE_INT);
1033 signals[SIG_TOGGLED] =
1034 g_signal_new("toggled",
1035 G_TYPE_FROM_CLASS(klass),
1036 G_SIGNAL_RUN_LAST,
1037 G_STRUCT_OFFSET(GntTreeClass, toggled),
1038 NULL, NULL,
1039 g_cclosure_marshal_VOID__POINTER,
1040 G_TYPE_NONE, 1, G_TYPE_POINTER);
1041 signals[SIG_COLLAPSED] =
1042 g_signal_new("collapse-toggled",
1043 G_TYPE_FROM_CLASS(klass),
1044 G_SIGNAL_RUN_LAST,
1046 NULL, NULL,
1047 gnt_closure_marshal_VOID__POINTER_BOOLEAN,
1048 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
1050 gnt_bindable_class_register_action(bindable, "move-up", action_up,
1051 GNT_KEY_UP, NULL);
1052 gnt_bindable_register_binding(bindable, "move-up", GNT_KEY_CTRL_P, NULL);
1053 gnt_bindable_class_register_action(bindable, "move-down", action_down,
1054 GNT_KEY_DOWN, NULL);
1055 gnt_bindable_register_binding(bindable, "move-down", GNT_KEY_CTRL_N, NULL);
1056 gnt_bindable_class_register_action(bindable, "move-parent", action_move_parent,
1057 GNT_KEY_BACKSPACE, NULL);
1058 gnt_bindable_class_register_action(bindable, "page-up", action_page_up,
1059 GNT_KEY_PGUP, NULL);
1060 gnt_bindable_class_register_action(bindable, "page-down", action_page_down,
1061 GNT_KEY_PGDOWN, NULL);
1062 gnt_bindable_class_register_action(bindable, "start-search", start_search,
1063 "/", NULL);
1064 gnt_bindable_class_register_action(bindable, "end-search", end_search_action,
1065 "\033", NULL);
1067 gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), bindable);
1068 GNTDEBUG;
1071 static void
1072 gnt_tree_init(GTypeInstance *instance, gpointer class)
1074 GntWidget *widget = GNT_WIDGET(instance);
1075 GntTree *tree = GNT_TREE(widget);
1076 tree->show_separator = TRUE;
1077 tree->priv = g_new0(GntTreePriv, 1);
1078 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X | GNT_WIDGET_GROW_Y |
1079 GNT_WIDGET_CAN_TAKE_FOCUS | GNT_WIDGET_NO_SHADOW);
1080 gnt_widget_set_take_focus(widget, TRUE);
1081 widget->priv.minw = 4;
1082 widget->priv.minh = 1;
1083 GNTDEBUG;
1086 /******************************************************************************
1087 * GntTree API
1088 *****************************************************************************/
1089 GType
1090 gnt_tree_get_gtype(void)
1092 static GType type = 0;
1094 if(type == 0)
1096 static const GTypeInfo info = {
1097 sizeof(GntTreeClass),
1098 NULL, /* base_init */
1099 NULL, /* base_finalize */
1100 (GClassInitFunc)gnt_tree_class_init,
1101 NULL, /* class_finalize */
1102 NULL, /* class_data */
1103 sizeof(GntTree),
1104 0, /* n_preallocs */
1105 gnt_tree_init, /* instance_init */
1106 NULL /* value_table */
1109 type = g_type_register_static(GNT_TYPE_WIDGET,
1110 "GntTree",
1111 &info, 0);
1114 return type;
1117 static void
1118 free_tree_col(gpointer data)
1120 GntTreeCol *col = data;
1121 if (!col->isbinary)
1122 g_free(col->text);
1123 g_free(col);
1126 static void
1127 free_tree_row(gpointer data)
1129 GntTreeRow *row = data;
1131 if (!row)
1132 return;
1134 g_list_foreach(row->columns, (GFunc)free_tree_col, NULL);
1135 g_list_free(row->columns);
1136 g_free(row);
1139 GntWidget *gnt_tree_new()
1141 return gnt_tree_new_with_columns(1);
1144 void gnt_tree_set_visible_rows(GntTree *tree, int rows)
1146 GntWidget *widget = GNT_WIDGET(tree);
1147 widget->priv.height = rows;
1148 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
1149 widget->priv.height += 2;
1152 int gnt_tree_get_visible_rows(GntTree *tree)
1154 GntWidget *widget = GNT_WIDGET(tree);
1155 int ret = widget->priv.height;
1156 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
1157 ret -= 2;
1158 return ret;
1161 GList *gnt_tree_get_rows(GntTree *tree)
1163 return tree->list;
1166 void gnt_tree_scroll(GntTree *tree, int count)
1168 GntTreeRow *row;
1170 if (count < 0)
1172 if (get_root_distance(tree->top) == 0)
1173 return;
1174 row = get_prev_n(tree->top, -count);
1175 if (row == NULL)
1176 row = tree->root;
1177 tree->top = row;
1179 else
1181 get_next_n_opt(tree->bottom, count, &count);
1182 tree->top = get_next_n(tree->top, count);
1185 redraw_tree(tree);
1186 g_signal_emit(tree, signals[SIG_SCROLLED], 0, count);
1189 static gpointer
1190 find_position(GntTree *tree, gpointer key, gpointer parent)
1192 GntTreeRow *row;
1194 if (tree->priv->compare == NULL)
1195 return NULL;
1197 if (parent == NULL)
1198 row = tree->root;
1199 else
1200 row = g_hash_table_lookup(tree->hash, parent);
1202 if (!row)
1203 return NULL;
1205 if (parent)
1206 row = row->child;
1208 while (row)
1210 if (tree->priv->compare(key, row->key) < 0)
1211 return (row->prev ? row->prev->key : NULL);
1212 if (row->next)
1213 row = row->next;
1214 else
1215 return row->key;
1217 return NULL;
1220 void gnt_tree_sort_row(GntTree *tree, gpointer key)
1222 GntTreeRow *row, *q, *s;
1223 int current, newp;
1225 if (!tree->priv->compare)
1226 return;
1228 row = g_hash_table_lookup(tree->hash, key);
1229 g_return_if_fail(row != NULL);
1231 current = g_list_index(tree->list, key);
1233 if (row->parent)
1234 s = row->parent->child;
1235 else
1236 s = tree->root;
1238 q = NULL;
1239 while (s) {
1240 if (tree->priv->compare(row->key, s->key) < 0)
1241 break;
1242 q = s;
1243 s = s->next;
1246 /* Move row between q and s */
1247 if (row == q || row == s)
1248 return;
1250 if (q == NULL) {
1251 /* row becomes the first child of its parent */
1252 row->prev->next = row->next; /* row->prev cannot be NULL at this point */
1253 if (row->next)
1254 row->next->prev = row->prev;
1255 if (row->parent)
1256 row->parent->child = row;
1257 else
1258 tree->root = row;
1259 row->next = s;
1260 s->prev = row; /* s cannot be NULL */
1261 row->prev = NULL;
1262 newp = g_list_index(tree->list, s) - 1;
1263 } else {
1264 if (row->prev) {
1265 row->prev->next = row->next;
1266 } else {
1267 /* row was the first child of its parent */
1268 if (row->parent)
1269 row->parent->child = row->next;
1270 else
1271 tree->top = row->next;
1274 if (row->next)
1275 row->next->prev = row->prev;
1277 q->next = row;
1278 row->prev = q;
1279 if (s)
1280 s->prev = row;
1281 row->next = s;
1282 newp = g_list_index(tree->list, q) + 1;
1284 tree->list = g_list_reposition_child(tree->list, current, newp);
1286 redraw_tree(tree);
1289 GntTreeRow *gnt_tree_add_row_after(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro)
1291 GntTreeRow *pr = NULL;
1293 row->tree = tree;
1294 row->key = key;
1295 row->data = NULL;
1296 g_hash_table_replace(tree->hash, key, row);
1298 if (bigbro == NULL && tree->priv->compare)
1300 bigbro = find_position(tree, key, parent);
1303 if (tree->root == NULL)
1305 tree->root = row;
1306 tree->list = g_list_prepend(tree->list, key);
1308 else
1310 int position = 0;
1312 if (bigbro)
1314 pr = g_hash_table_lookup(tree->hash, bigbro);
1315 if (pr)
1317 if (pr->next) pr->next->prev = row;
1318 row->next = pr->next;
1319 row->prev = pr;
1320 pr->next = row;
1321 row->parent = pr->parent;
1323 position = g_list_index(tree->list, bigbro);
1327 if (pr == NULL && parent)
1329 pr = g_hash_table_lookup(tree->hash, parent);
1330 if (pr)
1332 if (pr->child) pr->child->prev = row;
1333 row->next = pr->child;
1334 pr->child = row;
1335 row->parent = pr;
1337 position = g_list_index(tree->list, parent);
1341 if (pr == NULL)
1343 GntTreeRow *r = tree->root;
1344 row->next = r;
1345 if (r) r->prev = row;
1346 if (tree->current == tree->root)
1347 tree->current = row;
1348 tree->root = row;
1349 tree->list = g_list_prepend(tree->list, key);
1351 else
1353 tree->list = g_list_insert(tree->list, key, position + 1);
1356 redraw_tree(tree);
1358 return row;
1361 GntTreeRow *gnt_tree_add_row_last(GntTree *tree, void *key, GntTreeRow *row, void *parent)
1363 GntTreeRow *pr = NULL, *br = NULL;
1365 if (parent)
1366 pr = g_hash_table_lookup(tree->hash, parent);
1368 if (pr)
1369 br = pr->child;
1370 else
1371 br = tree->root;
1373 if (br)
1375 while (br->next)
1376 br = br->next;
1379 return gnt_tree_add_row_after(tree, key, row, parent, br ? br->key : NULL);
1382 gpointer gnt_tree_get_selection_data(GntTree *tree)
1384 if (tree->current)
1385 return tree->current->key; /* XXX: perhaps we should just get rid of 'data' */
1386 return NULL;
1389 char *gnt_tree_get_selection_text(GntTree *tree)
1391 if (tree->current)
1392 return update_row_text(tree, tree->current);
1393 return NULL;
1396 GList *gnt_tree_get_row_text_list(GntTree *tree, gpointer key)
1398 GList *list = NULL, *iter;
1399 GntTreeRow *row = key ? g_hash_table_lookup(tree->hash, key) : tree->current;
1400 int i;
1402 if (!row)
1403 return NULL;
1405 for (i = 0, iter = row->columns; i < tree->ncol && iter;
1406 i++, iter = iter->next)
1408 GntTreeCol *col = iter->data;
1409 list = g_list_append(list, BINARY_DATA(tree, i) ? col->text : g_strdup(col->text));
1412 return list;
1415 GList *gnt_tree_get_selection_text_list(GntTree *tree)
1417 return gnt_tree_get_row_text_list(tree, NULL);
1420 void gnt_tree_remove(GntTree *tree, gpointer key)
1422 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1423 static int depth = 0; /* Only redraw after all child nodes are removed */
1424 if (row)
1426 gboolean redraw = FALSE;
1428 if (row->child) {
1429 depth++;
1430 while (row->child) {
1431 gnt_tree_remove(tree, row->child->key);
1433 depth--;
1436 if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0)
1437 redraw = TRUE;
1439 /* Update root/top/current/bottom if necessary */
1440 if (tree->root == row)
1441 tree->root = get_next(row);
1442 if (tree->top == row)
1444 if (tree->top != tree->root)
1445 tree->top = get_prev(row);
1446 else
1447 tree->top = get_next(row);
1449 if (tree->current == row)
1451 if (tree->current != tree->root)
1452 tree->current = get_prev(row);
1453 else
1454 tree->current = get_next(row);
1455 tree_selection_changed(tree, row, tree->current);
1457 if (tree->bottom == row)
1459 tree->bottom = get_prev(row);
1462 /* Fix the links */
1463 if (row->next)
1464 row->next->prev = row->prev;
1465 if (row->parent && row->parent->child == row)
1466 row->parent->child = row->next;
1467 if (row->prev)
1468 row->prev->next = row->next;
1470 g_hash_table_remove(tree->hash, key);
1471 tree->list = g_list_remove(tree->list, key);
1473 if (redraw && depth == 0)
1475 redraw_tree(tree);
1480 static gboolean
1481 return_true(gpointer key, gpointer data, gpointer null)
1483 return TRUE;
1486 void gnt_tree_remove_all(GntTree *tree)
1488 tree->root = NULL;
1489 g_hash_table_foreach_remove(tree->hash, (GHRFunc)return_true, tree);
1490 g_list_free(tree->list);
1491 tree->list = NULL;
1492 tree->current = tree->top = tree->bottom = NULL;
1495 int gnt_tree_get_selection_visible_line(GntTree *tree)
1497 return get_distance(tree->top, tree->current) +
1498 !!(GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER));
1501 void gnt_tree_change_text(GntTree *tree, gpointer key, int colno, const char *text)
1503 GntTreeRow *row;
1504 GntTreeCol *col;
1506 g_return_if_fail(colno < tree->ncol);
1508 row = g_hash_table_lookup(tree->hash, key);
1509 if (row)
1511 col = g_list_nth_data(row->columns, colno);
1512 if (BINARY_DATA(tree, colno)) {
1513 col->text = (gpointer)text;
1514 } else {
1515 g_free(col->text);
1516 col->text = g_strdup(text ? text : "");
1519 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED) &&
1520 get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0)
1521 redraw_tree(tree);
1525 GntTreeRow *gnt_tree_add_choice(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro)
1527 GntTreeRow *r;
1528 r = g_hash_table_lookup(tree->hash, key);
1529 g_return_val_if_fail(!r || !r->choice, NULL);
1531 if (bigbro == NULL) {
1532 if (tree->priv->compare)
1533 bigbro = find_position(tree, key, parent);
1534 else {
1535 r = g_hash_table_lookup(tree->hash, parent);
1536 if (!r)
1537 r = tree->root;
1538 else
1539 r = r->child;
1540 if (r) {
1541 while (r->next)
1542 r = r->next;
1543 bigbro = r->key;
1547 row = gnt_tree_add_row_after(tree, key, row, parent, bigbro);
1548 row->choice = TRUE;
1550 return row;
1553 void gnt_tree_set_choice(GntTree *tree, void *key, gboolean set)
1555 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1557 if (!row)
1558 return;
1559 g_return_if_fail(row->choice);
1561 row->isselected = set;
1562 redraw_tree(tree);
1565 gboolean gnt_tree_get_choice(GntTree *tree, void *key)
1567 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1569 if (!row)
1570 return FALSE;
1571 g_return_val_if_fail(row->choice, FALSE);
1573 return row->isselected;
1576 void gnt_tree_set_row_flags(GntTree *tree, void *key, GntTextFormatFlags flags)
1578 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1579 if (!row || row->flags == flags)
1580 return;
1582 row->flags = flags;
1583 redraw_tree(tree); /* XXX: It shouldn't be necessary to redraw the whole darned tree */
1586 void gnt_tree_set_row_color(GntTree *tree, void *key, int color)
1588 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1589 if (!row || row->color == color)
1590 return;
1592 row->color = color;
1593 redraw_tree(tree);
1596 void gnt_tree_set_selected(GntTree *tree , void *key)
1598 int dist;
1599 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1600 if (!row || row == tree->current)
1601 return;
1603 if (tree->top == NULL)
1604 tree->top = row;
1605 if (tree->bottom == NULL)
1606 tree->bottom = row;
1608 tree->current = row;
1609 if ((dist = get_distance(tree->current, tree->bottom)) < 0)
1610 gnt_tree_scroll(tree, -dist);
1611 else if ((dist = get_distance(tree->current, tree->top)) > 0)
1612 gnt_tree_scroll(tree, -dist);
1613 else
1614 redraw_tree(tree);
1615 tree_selection_changed(tree, row, tree->current);
1618 static void _gnt_tree_init_internals(GntTree *tree, int col)
1620 gnt_tree_free_columns(tree);
1622 tree->ncol = col;
1623 tree->hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_tree_row);
1624 tree->columns = g_new0(struct _GntTreeColInfo, col);
1625 tree->priv->lastvisible = col - 1;
1626 while (col--)
1628 tree->columns[col].width = 15;
1630 tree->list = NULL;
1631 tree->show_title = FALSE;
1632 g_object_notify(G_OBJECT(tree), "columns");
1635 GntWidget *gnt_tree_new_with_columns(int col)
1637 GntWidget *widget = g_object_new(GNT_TYPE_TREE,
1638 "columns", col,
1639 "expander-level", 1,
1640 NULL);
1642 return widget;
1645 GntTreeRow *gnt_tree_create_row_from_list(GntTree *tree, GList *list)
1647 GList *iter;
1648 int i;
1649 GntTreeRow *row = g_new0(GntTreeRow, 1);
1651 for (i = 0, iter = list; i < tree->ncol && iter; iter = iter->next, i++)
1653 GntTreeCol *col = g_new0(GntTreeCol, 1);
1654 col->span = 1;
1655 if (BINARY_DATA(tree, i)) {
1656 col->text = iter->data;
1657 col->isbinary = TRUE;
1658 } else {
1659 col->text = g_strdup(iter->data ? iter->data : "");
1660 col->isbinary = FALSE;
1663 row->columns = g_list_append(row->columns, col);
1666 return row;
1669 GntTreeRow *gnt_tree_create_row(GntTree *tree, ...)
1671 int i;
1672 va_list args;
1673 GList *list = NULL;
1674 GntTreeRow *row;
1676 va_start(args, tree);
1677 for (i = 0; i < tree->ncol; i++)
1679 list = g_list_append(list, va_arg(args, char *));
1681 va_end(args);
1683 row = gnt_tree_create_row_from_list(tree, list);
1684 g_list_free(list);
1686 return row;
1689 void gnt_tree_set_col_width(GntTree *tree, int col, int width)
1691 g_return_if_fail(col < tree->ncol);
1693 tree->columns[col].width = width;
1694 if (tree->columns[col].width_ratio == 0)
1695 tree->columns[col].width_ratio = width;
1698 void gnt_tree_set_column_title(GntTree *tree, int index, const char *title)
1700 g_free(tree->columns[index].title);
1701 tree->columns[index].title = g_strdup(title);
1704 void gnt_tree_set_column_titles(GntTree *tree, ...)
1706 int i;
1707 va_list args;
1709 va_start(args, tree);
1710 for (i = 0; i < tree->ncol; i++)
1712 const char *title = va_arg(args, const char *);
1713 tree->columns[i].title = g_strdup(title);
1715 va_end(args);
1718 void gnt_tree_set_show_title(GntTree *tree, gboolean set)
1720 tree->show_title = set;
1721 GNT_WIDGET(tree)->priv.minh = (set ? 6 : 4);
1724 void gnt_tree_set_compare_func(GntTree *tree, GCompareFunc func)
1726 tree->priv->compare = func;
1729 void gnt_tree_set_expanded(GntTree *tree, void *key, gboolean expanded)
1731 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1732 if (row) {
1733 row->collapsed = !expanded;
1734 if (GNT_WIDGET(tree)->window)
1735 gnt_widget_draw(GNT_WIDGET(tree));
1736 g_signal_emit(tree, signals[SIG_COLLAPSED], 0, key, row->collapsed);
1740 void gnt_tree_set_show_separator(GntTree *tree, gboolean set)
1742 tree->show_separator = set;
1745 void gnt_tree_adjust_columns(GntTree *tree)
1747 GntTreeRow *row = tree->root;
1748 int *widths, i, twidth;
1750 widths = g_new0(int, tree->ncol);
1751 while (row) {
1752 GList *iter;
1753 for (i = 0, iter = row->columns; iter; iter = iter->next, i++) {
1754 GntTreeCol *col = iter->data;
1755 int w = gnt_util_onscreen_width(col->text, NULL);
1756 if (i == 0 && row->choice)
1757 w += 4;
1758 if (i == 0) {
1759 w += find_depth(row) * TAB_SIZE;
1761 if (widths[i] < w)
1762 widths[i] = w;
1764 row = get_next(row);
1767 twidth = 1 + 2 * (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER));
1768 for (i = 0; i < tree->ncol; i++) {
1769 if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE)
1770 widths[i] = tree->columns[i].width;
1771 gnt_tree_set_col_width(tree, i, widths[i]);
1772 if (!COLUMN_INVISIBLE(tree, i)) {
1773 twidth = twidth + widths[i];
1774 if (tree->priv->lastvisible != i)
1775 twidth += 1;
1778 g_free(widths);
1780 gnt_widget_set_size(GNT_WIDGET(tree), twidth, -1);
1783 void gnt_tree_set_hash_fns(GntTree *tree, gpointer hash, gpointer eq, gpointer kd)
1785 g_hash_table_foreach_remove(tree->hash, return_true, NULL);
1786 g_hash_table_destroy(tree->hash);
1787 tree->hash = g_hash_table_new_full(hash, eq, kd, free_tree_row);
1790 static void
1791 set_column_flag(GntTree *tree, int col, GntTreeColumnFlag flag, gboolean set)
1793 if (set)
1794 tree->columns[col].flags |= flag;
1795 else
1796 tree->columns[col].flags &= ~flag;
1799 void gnt_tree_set_column_visible(GntTree *tree, int col, gboolean vis)
1801 g_return_if_fail(col < tree->ncol);
1802 set_column_flag(tree, col, GNT_TREE_COLUMN_INVISIBLE, !vis);
1803 if (vis) {
1804 /* the column is visible */
1805 if (tree->priv->lastvisible < col)
1806 tree->priv->lastvisible = col;
1807 } else {
1808 if (tree->priv->lastvisible == col)
1809 while (tree->priv->lastvisible) {
1810 tree->priv->lastvisible--;
1811 if (!COLUMN_INVISIBLE(tree, tree->priv->lastvisible))
1812 break;
1815 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED))
1816 readjust_columns(tree);
1819 void gnt_tree_set_column_resizable(GntTree *tree, int col, gboolean res)
1821 g_return_if_fail(col < tree->ncol);
1822 set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, !res);
1825 void gnt_tree_set_column_is_binary(GntTree *tree, int col, gboolean bin)
1827 g_return_if_fail(col < tree->ncol);
1828 set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, bin);
1831 void gnt_tree_set_column_is_right_aligned(GntTree *tree, int col, gboolean right)
1833 g_return_if_fail(col < tree->ncol);
1834 set_column_flag(tree, col, GNT_TREE_COLUMN_RIGHT_ALIGNED, right);
1837 void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[])
1839 int i;
1840 for (i = 0; i < tree->ncol && cols[i]; i++) {
1841 tree->columns[i].width_ratio = cols[i];
1845 void gnt_tree_set_search_column(GntTree *tree, int col)
1847 g_return_if_fail(col < tree->ncol);
1848 g_return_if_fail(!BINARY_DATA(tree, col));
1849 tree->priv->search_column = col;
1852 gboolean gnt_tree_is_searching(GntTree *tree)
1854 return (tree->priv->search != NULL);
1857 void gnt_tree_set_search_function(GntTree *tree,
1858 gboolean (*func)(GntTree *tree, gpointer key, const char *search, const char *current))
1860 tree->priv->search_func = func;
1863 gpointer gnt_tree_get_parent_key(GntTree *tree, gpointer key)
1865 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1866 return (row && row->parent) ? row->parent->key : NULL;