Change the "length of bstream" data type to be a gsize, since it represents
[pidgin-git.git] / finch / libgnt / gnttree.c
blob1095a0ccab0300706c1c658e2d2fc200cd85bce8
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 "gntinternal.h"
24 #include "gntmarshal.h"
25 #include "gntstyle.h"
26 #include "gnttree.h"
27 #include "gntutils.h"
29 #include <string.h>
30 #include <ctype.h>
32 #define SEARCH_TIMEOUT_S 4 /* 4 secs */
33 #define SEARCHING(tree) (tree->priv->search && tree->priv->search->len > 0)
35 #define COLUMN_INVISIBLE(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_INVISIBLE)
36 #define BINARY_DATA(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_BINARY_DATA)
37 #define RIGHT_ALIGNED(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_RIGHT_ALIGNED)
39 enum
41 PROP_0,
42 PROP_COLUMNS,
43 PROP_EXPANDER,
46 enum
48 SIG_SELECTION_CHANGED,
49 SIG_SCROLLED,
50 SIG_TOGGLED,
51 SIG_COLLAPSED,
52 SIGS,
55 struct _GntTreePriv
57 GString *search;
58 int search_timeout;
59 int search_column;
60 gboolean (*search_func)(GntTree *tree, gpointer key, const char *search, const char *current);
62 GCompareFunc compare;
63 int lastvisible;
64 int expander_level;
67 #define TAB_SIZE 3
69 /* XXX: Make this one into a GObject?
70 * ... Probably not */
71 struct _GntTreeRow
73 void *key;
74 void *data; /* XXX: unused */
76 gboolean collapsed;
77 gboolean choice; /* Is this a choice-box?
78 If choice is true, then child will be NULL */
79 gboolean isselected;
80 GntTextFormatFlags flags;
81 int color;
83 GntTreeRow *parent;
84 GntTreeRow *child;
85 GntTreeRow *next;
86 GntTreeRow *prev;
88 GList *columns;
89 GntTree *tree;
92 struct _GntTreeCol
94 char *text;
95 gboolean isbinary;
96 int span; /* How many columns does it span? */
99 static void tree_selection_changed(GntTree *, GntTreeRow *, GntTreeRow *);
100 static void _gnt_tree_init_internals(GntTree *tree, int col);
102 static GntWidgetClass *parent_class = NULL;
103 static guint signals[SIGS] = { 0 };
105 static void
106 readjust_columns(GntTree *tree)
108 int i, col, total;
109 int width;
110 #define WIDTH(i) (tree->columns[i].width_ratio ? tree->columns[i].width_ratio : tree->columns[i].width)
111 gnt_widget_get_size(GNT_WIDGET(tree), &width, NULL);
112 if (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER))
113 width -= 2;
114 width -= 1; /* Exclude the scrollbar from the calculation */
115 for (i = 0, total = 0; i < tree->ncol ; i++) {
116 if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE)
117 continue;
118 if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE)
119 width -= WIDTH(i) + (tree->priv->lastvisible != i);
120 else
121 total += WIDTH(i) + (tree->priv->lastvisible != i);
124 if (total == 0)
125 return;
127 for (i = 0; i < tree->ncol; i++) {
128 if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE)
129 continue;
130 if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE)
131 col = WIDTH(i);
132 else
133 col = (WIDTH(i) * width) / total;
134 gnt_tree_set_col_width(GNT_TREE(tree), i, col);
138 /* Move the item at position old to position new */
139 static GList *
140 g_list_reposition_child(GList *list, int old, int new)
142 gpointer item = g_list_nth_data(list, old);
143 list = g_list_remove(list, item);
144 if (old < new)
145 new--; /* because the positions would have shifted after removing the item */
146 list = g_list_insert(list, item, new);
147 return list;
150 static GntTreeRow *
151 _get_next(GntTreeRow *row, gboolean godeep)
153 if (row == NULL)
154 return NULL;
155 if (godeep && row->child)
156 return row->child;
157 if (row->next)
158 return row->next;
159 return _get_next(row->parent, FALSE);
162 static gboolean
163 row_matches_search(GntTreeRow *row)
165 GntTree *t = row->tree;
166 if (t->priv->search && t->priv->search->len > 0) {
167 GntTreeCol *col = (col = g_list_nth_data(row->columns, t->priv->search_column)) ? col : row->columns->data;
168 char *one, *two, *z;
169 if (t->priv->search_func)
170 return t->priv->search_func(t, row->key, t->priv->search->str, col->text);
171 one = g_utf8_casefold(col->text, -1);
172 two = g_utf8_casefold(t->priv->search->str, -1);
173 z = strstr(one, two);
174 g_free(one);
175 g_free(two);
176 if (z == NULL)
177 return FALSE;
179 return TRUE;
182 static GntTreeRow *
183 get_next(GntTreeRow *row)
185 if (row == NULL)
186 return NULL;
187 while ((row = _get_next(row, !row->collapsed)) != NULL) {
188 if (row_matches_search(row))
189 break;
191 return row;
194 /* Returns the n-th next row. If it doesn't exist, returns NULL */
195 static GntTreeRow *
196 get_next_n(GntTreeRow *row, int n)
198 while (row && n--)
199 row = get_next(row);
200 return row;
203 /* Returns the n-th next row. If it doesn't exist, then the last non-NULL node */
204 static GntTreeRow *
205 get_next_n_opt(GntTreeRow *row, int n, int *pos)
207 GntTreeRow *next = row;
208 int r = 0;
210 if (row == NULL)
211 return NULL;
213 while (row && n--)
215 row = get_next(row);
216 if (row)
218 next = row;
219 r++;
223 if (pos)
224 *pos = r;
226 return next;
229 static GntTreeRow *
230 get_last_child(GntTreeRow *row)
232 if (row == NULL)
233 return NULL;
234 if (!row->collapsed && row->child)
235 row = row->child;
236 else
237 return row;
239 while(row->next)
240 row = row->next;
241 return get_last_child(row);
244 static GntTreeRow *
245 get_prev(GntTreeRow *row)
247 if (row == NULL)
248 return NULL;
249 while (row) {
250 if (row->prev)
251 row = get_last_child(row->prev);
252 else
253 row = row->parent;
254 if (!row || row_matches_search(row))
255 break;
257 return row;
260 static GntTreeRow *
261 get_prev_n(GntTreeRow *row, int n)
263 while (row && n--)
264 row = get_prev(row);
265 return row;
268 /* Distance of row from the root */
269 /* XXX: This is uber-inefficient */
270 static int
271 get_root_distance(GntTreeRow *row)
273 if (row == NULL)
274 return -1;
275 return get_root_distance(get_prev(row)) + 1;
278 /* Returns the distance between a and b.
279 * If a is 'above' b, then the distance is positive */
280 static int
281 get_distance(GntTreeRow *a, GntTreeRow *b)
283 /* First get the distance from a to the root.
284 * Then the distance from b to the root.
285 * Subtract.
286 * It's not that good, but it works. */
287 int ha = get_root_distance(a);
288 int hb = get_root_distance(b);
290 return (hb - ha);
293 static int
294 find_depth(GntTreeRow *row)
296 int dep = -1;
298 while (row)
300 dep++;
301 row = row->parent;
304 return dep;
307 static char *
308 update_row_text(GntTree *tree, GntTreeRow *row)
310 GString *string = g_string_new(NULL);
311 GList *iter;
312 int i;
313 gboolean notfirst = FALSE;
315 for (i = 0, iter = row->columns; i < tree->ncol && iter; i++, iter = iter->next)
317 GntTreeCol *col = iter->data;
318 const char *text;
319 int len;
320 int fl = 0;
321 gboolean cut = FALSE;
322 int width;
323 const char *display;
325 if (COLUMN_INVISIBLE(tree, i))
326 continue;
328 if (BINARY_DATA(tree, i))
329 display = "";
330 else
331 display = col->text;
333 len = gnt_util_onscreen_width(display, NULL);
335 width = tree->columns[i].width;
337 if (i == 0)
339 if (row->choice)
341 g_string_append_printf(string, "[%c] ",
342 row->isselected ? 'X' : ' ');
343 fl = 4;
345 else if (find_depth(row) < tree->priv->expander_level && row->child)
347 if (row->collapsed)
349 string = g_string_append(string, "+ ");
351 else
353 string = g_string_append(string, "- ");
355 fl = 2;
357 else
359 fl = TAB_SIZE * find_depth(row);
360 g_string_append_printf(string, "%*s", fl, "");
362 len += fl;
363 } else if (notfirst && tree->show_separator)
364 g_string_append_c(string, '|');
365 else
366 g_string_append_c(string, ' ');
368 notfirst = TRUE;
370 if (len > width) {
371 len = MAX(1, width - 1);
372 cut = TRUE;
375 if (RIGHT_ALIGNED(tree, i) && len < tree->columns[i].width) {
376 g_string_append_printf(string, "%*s", width - len - cut, "");
379 text = gnt_util_onscreen_width_to_pointer(display, len - fl, NULL);
380 string = g_string_append_len(string, display, text - display);
381 if (cut && width > 1) { /* ellipsis */
382 if (gnt_ascii_only())
383 g_string_append_c(string, '~');
384 else
385 string = g_string_append(string, "\342\200\246");
386 len++;
389 if (!RIGHT_ALIGNED(tree, i) && len < tree->columns[i].width && iter->next)
390 g_string_append_printf(string, "%*s", width - len, "");
392 return g_string_free(string, FALSE);
395 #define NEXT_X x += tree->columns[i].width + (i > 0 ? 1 : 0)
397 static void
398 tree_mark_columns(GntTree *tree, int pos, int y, chtype type)
400 GntWidget *widget = GNT_WIDGET(tree);
401 int i;
402 int x = pos;
403 gboolean notfirst = FALSE;
405 for (i = 0; i < tree->ncol - 1; i++)
407 if (!COLUMN_INVISIBLE(tree, i)) {
408 notfirst = TRUE;
409 NEXT_X;
411 if (!COLUMN_INVISIBLE(tree, i+1) && notfirst)
412 mvwaddch(widget->window, y, x, type);
416 static void
417 redraw_tree(GntTree *tree)
419 int start, i;
420 GntWidget *widget = GNT_WIDGET(tree);
421 GntTreeRow *row;
422 int pos, up, down = 0;
423 int rows, scrcol;
424 int current = 0;
426 if (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED))
427 return;
429 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
430 pos = 0;
431 else
432 pos = 1;
434 if (tree->top == NULL)
435 tree->top = tree->root;
436 if (tree->current == NULL && tree->root != NULL) {
437 tree->current = tree->root;
438 tree_selection_changed(tree, NULL, tree->current);
441 wbkgd(widget->window, gnt_color_pair(GNT_COLOR_NORMAL));
443 start = 0;
444 if (tree->show_title)
446 int i;
447 int x = pos;
449 mvwhline(widget->window, pos + 1, pos, ACS_HLINE | gnt_color_pair(GNT_COLOR_NORMAL),
450 widget->priv.width - pos - 1);
451 mvwhline(widget->window, pos, pos, ' ' | gnt_color_pair(GNT_COLOR_NORMAL),
452 widget->priv.width - pos - 1);
454 for (i = 0; i < tree->ncol; i++)
456 if (COLUMN_INVISIBLE(tree, i)) {
457 continue;
459 mvwaddnstr(widget->window, pos, x + (x != pos), tree->columns[i].title, tree->columns[i].width);
460 NEXT_X;
462 if (pos)
464 tree_mark_columns(tree, pos, 0,
465 (tree->show_separator ? ACS_TTEE : ACS_HLINE) | gnt_color_pair(GNT_COLOR_NORMAL));
466 tree_mark_columns(tree, pos, widget->priv.height - pos,
467 (tree->show_separator ? ACS_BTEE : ACS_HLINE) | gnt_color_pair(GNT_COLOR_NORMAL));
469 tree_mark_columns(tree, pos, pos + 1,
470 (tree->show_separator ? ACS_PLUS : ACS_HLINE) | gnt_color_pair(GNT_COLOR_NORMAL));
471 tree_mark_columns(tree, pos, pos,
472 (tree->show_separator ? ACS_VLINE : ' ') | gnt_color_pair(GNT_COLOR_NORMAL));
473 start = 2;
476 rows = widget->priv.height - pos * 2 - start - 1;
477 tree->bottom = get_next_n_opt(tree->top, rows, &down);
478 if (down < rows)
480 tree->top = get_prev_n(tree->bottom, rows);
481 if (tree->top == NULL)
482 tree->top = tree->root;
485 up = get_distance(tree->top, tree->current);
486 if (up < 0)
487 tree->top = tree->current;
488 else if (up >= widget->priv.height - pos)
489 tree->top = get_prev_n(tree->current, rows);
491 if (tree->top && !row_matches_search(tree->top))
492 tree->top = get_next(tree->top);
493 row = tree->top;
494 scrcol = widget->priv.width - 1 - 2 * pos; /* exclude the borders and the scrollbar */
496 if (tree->current && !row_matches_search(tree->current)) {
497 GntTreeRow *old = tree->current;
498 tree->current = tree->top;
499 tree_selection_changed(tree, old, tree->current);
502 for (i = start + pos; row && i < widget->priv.height - pos;
503 i++, row = get_next(row))
505 char *str;
506 int wr;
508 GntTextFormatFlags flags = row->flags;
509 int attr = 0;
511 if (!row_matches_search(row))
512 continue;
513 str = update_row_text(tree, row);
515 if ((wr = gnt_util_onscreen_width(str, NULL)) > scrcol)
517 char *s = (char*)gnt_util_onscreen_width_to_pointer(str, scrcol, &wr);
518 *s = '\0';
521 if (flags & GNT_TEXT_FLAG_BOLD)
522 attr |= A_BOLD;
523 if (flags & GNT_TEXT_FLAG_UNDERLINE)
524 attr |= A_UNDERLINE;
525 if (flags & GNT_TEXT_FLAG_BLINK)
526 attr |= A_BLINK;
528 if (row == tree->current)
530 current = i;
531 attr |= A_BOLD;
532 if (gnt_widget_has_focus(widget))
533 attr |= gnt_color_pair(GNT_COLOR_HIGHLIGHT);
534 else
535 attr |= gnt_color_pair(GNT_COLOR_HIGHLIGHT_D);
537 else
539 if (flags & GNT_TEXT_FLAG_DIM)
540 if (row->color)
541 attr |= (A_DIM | gnt_color_pair(row->color));
542 else
543 attr |= (A_DIM | gnt_color_pair(GNT_COLOR_DISABLED));
544 else if (flags & GNT_TEXT_FLAG_HIGHLIGHT)
545 attr |= (A_DIM | gnt_color_pair(GNT_COLOR_HIGHLIGHT));
546 else if (row->color)
547 attr |= gnt_color_pair(row->color);
548 else
549 attr |= gnt_color_pair(GNT_COLOR_NORMAL);
552 wbkgdset(widget->window, '\0' | attr);
553 mvwaddstr(widget->window, i, pos, C_(str));
554 whline(widget->window, ' ', scrcol - wr);
555 tree->bottom = row;
556 g_free(str);
557 tree_mark_columns(tree, pos, i,
558 (tree->show_separator ? ACS_VLINE : ' ') | attr);
561 wbkgdset(widget->window, '\0' | gnt_color_pair(GNT_COLOR_NORMAL));
562 while (i < widget->priv.height - pos)
564 mvwhline(widget->window, i, pos, ' ',
565 widget->priv.width - pos * 2 - 1);
566 tree_mark_columns(tree, pos, i,
567 (tree->show_separator ? ACS_VLINE : ' '));
568 i++;
571 scrcol = widget->priv.width - pos - 1; /* position of the scrollbar */
572 rows--;
573 if (rows > 0)
575 int total = 0;
576 int showing, position;
578 get_next_n_opt(tree->root, g_list_length(tree->list), &total);
579 showing = rows * rows / MAX(total, 1) + 1;
580 showing = MIN(rows, showing);
582 total -= rows;
583 up = get_distance(tree->root, tree->top);
584 down = total - up;
586 position = (rows - showing) * up / MAX(1, up + down);
587 position = MAX((tree->top != tree->root), position);
589 if (showing + position > rows)
590 position = rows - showing;
592 if (showing + position == rows && row)
593 position = MAX(0, rows - 1 - showing);
594 else if (showing + position < rows && !row)
595 position = rows - showing;
597 position += pos + start + 1;
599 mvwvline(widget->window, pos + start + 1, scrcol,
600 ' ' | gnt_color_pair(GNT_COLOR_NORMAL), rows);
601 mvwvline(widget->window, position, scrcol,
602 ACS_CKBOARD | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D), showing);
605 mvwaddch(widget->window, start + pos, scrcol,
606 ((tree->top != tree->root) ? ACS_UARROW : ' ') |
607 gnt_color_pair(GNT_COLOR_HIGHLIGHT_D));
609 mvwaddch(widget->window, widget->priv.height - pos - 1, scrcol,
610 (row ? ACS_DARROW : ' ') | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D));
612 /* If there's a search-text, show it in the bottom of the tree */
613 if (tree->priv->search && tree->priv->search->len > 0) {
614 const char *str = gnt_util_onscreen_width_to_pointer(tree->priv->search->str, scrcol - 1, NULL);
615 wbkgdset(widget->window, '\0' | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D));
616 mvwaddnstr(widget->window, widget->priv.height - pos - 1, pos,
617 tree->priv->search->str, str - tree->priv->search->str);
619 wmove(widget->window, current, pos);
621 gnt_widget_queue_update(widget);
624 static void
625 gnt_tree_draw(GntWidget *widget)
627 GntTree *tree = GNT_TREE(widget);
629 redraw_tree(tree);
631 GNTDEBUG;
634 static void
635 gnt_tree_size_request(GntWidget *widget)
637 if (widget->priv.height == 0)
638 widget->priv.height = 10; /* XXX: Why?! */
639 if (widget->priv.width == 0)
641 GntTree *tree = GNT_TREE(widget);
642 int i, width = 0;
643 width = 1 + 2 * (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER));
644 for (i = 0; i < tree->ncol; i++)
645 if (!COLUMN_INVISIBLE(tree, i)) {
646 width = width + tree->columns[i].width;
647 if (tree->priv->lastvisible != i)
648 width++;
650 widget->priv.width = width;
654 static void
655 gnt_tree_map(GntWidget *widget)
657 GntTree *tree = GNT_TREE(widget);
658 if (widget->priv.width == 0 || widget->priv.height == 0)
660 gnt_widget_size_request(widget);
662 tree->top = tree->root;
663 tree->current = tree->root;
664 GNTDEBUG;
667 static void
668 tree_selection_changed(GntTree *tree, GntTreeRow *old, GntTreeRow *current)
670 g_signal_emit(tree, signals[SIG_SELECTION_CHANGED], 0, old ? old->key : NULL,
671 current ? current->key : NULL);
674 static gboolean
675 action_down(GntBindable *bind, GList *null)
677 int dist;
678 GntTree *tree = GNT_TREE(bind);
679 GntTreeRow *old = tree->current;
680 GntTreeRow *row = get_next(tree->current);
681 if (row == NULL)
682 return FALSE;
683 tree->current = row;
684 if ((dist = get_distance(tree->current, tree->bottom)) < 0)
685 gnt_tree_scroll(tree, -dist);
686 else
687 redraw_tree(tree);
688 if (old != tree->current)
689 tree_selection_changed(tree, old, tree->current);
690 return TRUE;
693 static gboolean
694 action_move_parent(GntBindable *bind, GList *null)
696 GntTree *tree = GNT_TREE(bind);
697 GntTreeRow *row = tree->current;
698 int dist;
700 if (!row || !row->parent || SEARCHING(tree))
701 return FALSE;
703 tree->current = row->parent;
704 if ((dist = get_distance(tree->current, tree->top)) > 0)
705 gnt_tree_scroll(tree, -dist);
706 else
707 redraw_tree(tree);
708 tree_selection_changed(tree, row, tree->current);
709 return TRUE;
712 static gboolean
713 action_up(GntBindable *bind, GList *list)
715 int dist;
716 GntTree *tree = GNT_TREE(bind);
717 GntTreeRow *old = tree->current;
718 GntTreeRow *row = get_prev(tree->current);
719 if (!row)
720 return FALSE;
721 tree->current = row;
722 if ((dist = get_distance(tree->current, tree->top)) > 0)
723 gnt_tree_scroll(tree, -dist);
724 else
725 redraw_tree(tree);
726 if (old != tree->current)
727 tree_selection_changed(tree, old, tree->current);
729 return TRUE;
732 static gboolean
733 action_page_down(GntBindable *bind, GList *null)
735 GntTree *tree = GNT_TREE(bind);
736 GntTreeRow *old = tree->current;
737 GntTreeRow *row = get_next(tree->bottom);
738 if (row)
740 int dist = get_distance(tree->top, tree->current);
741 tree->top = tree->bottom;
742 tree->current = get_next_n_opt(tree->top, dist, NULL);
743 redraw_tree(tree);
745 else if (tree->current != tree->bottom)
747 tree->current = tree->bottom;
748 redraw_tree(tree);
751 if (old != tree->current)
752 tree_selection_changed(tree, old, tree->current);
753 return TRUE;
756 static gboolean
757 action_page_up(GntBindable *bind, GList *null)
759 GntWidget *widget = GNT_WIDGET(bind);
760 GntTree *tree = GNT_TREE(bind);
761 GntTreeRow *row;
762 GntTreeRow *old = tree->current;
764 if (tree->top != tree->root)
766 int dist = get_distance(tree->top, tree->current);
767 row = get_prev_n(tree->top, widget->priv.height - 1 -
768 tree->show_title * 2 - 2 * (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER) == 0));
769 if (row == NULL)
770 row = tree->root;
771 tree->top = row;
772 tree->current = get_next_n_opt(tree->top, dist, NULL);
773 redraw_tree(tree);
775 else if (tree->current != tree->top)
777 tree->current = tree->top;
778 redraw_tree(tree);
780 if (old != tree->current)
781 tree_selection_changed(tree, old, tree->current);
782 return TRUE;
785 static void
786 end_search(GntTree *tree)
788 if (tree->priv->search) {
789 g_source_remove(tree->priv->search_timeout);
790 g_string_free(tree->priv->search, TRUE);
791 tree->priv->search = NULL;
792 tree->priv->search_timeout = 0;
793 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(tree), GNT_WIDGET_DISABLE_ACTIONS);
797 static gboolean
798 search_timeout(gpointer data)
800 GntTree *tree = data;
802 end_search(tree);
803 redraw_tree(tree);
805 return FALSE;
808 static gboolean
809 gnt_tree_key_pressed(GntWidget *widget, const char *text)
811 GntTree *tree = GNT_TREE(widget);
812 GntTreeRow *old = tree->current;
814 if (text[0] == '\r' || text[0] == '\n') {
815 end_search(tree);
816 gnt_widget_activate(widget);
817 } else if (tree->priv->search) {
818 gboolean changed = TRUE;
819 if (g_unichar_isprint(*text)) {
820 tree->priv->search = g_string_append_c(tree->priv->search, *text);
821 } else if (g_utf8_collate(text, GNT_KEY_BACKSPACE) == 0) {
822 if (tree->priv->search->len)
823 tree->priv->search->str[--tree->priv->search->len] = '\0';
824 } else
825 changed = FALSE;
826 if (changed) {
827 redraw_tree(tree);
828 } else {
829 gnt_bindable_perform_action_key(GNT_BINDABLE(tree), text);
831 g_source_remove(tree->priv->search_timeout);
832 tree->priv->search_timeout = g_timeout_add_seconds(SEARCH_TIMEOUT_S, search_timeout, tree);
833 return TRUE;
834 } else if (text[0] == ' ' && text[1] == 0) {
835 /* Space pressed */
836 GntTreeRow *row = tree->current;
837 if (row && row->child)
839 row->collapsed = !row->collapsed;
840 redraw_tree(tree);
841 g_signal_emit(tree, signals[SIG_COLLAPSED], 0, row->key, row->collapsed);
843 else if (row && row->choice)
845 row->isselected = !row->isselected;
846 g_signal_emit(tree, signals[SIG_TOGGLED], 0, row->key);
847 redraw_tree(tree);
849 } else {
850 return FALSE;
853 if (old != tree->current)
855 tree_selection_changed(tree, old, tree->current);
858 return TRUE;
861 static void
862 gnt_tree_free_columns(GntTree *tree)
864 int i;
865 for (i = 0; i < tree->ncol; i++) {
866 g_free(tree->columns[i].title);
868 g_free(tree->columns);
871 static void
872 gnt_tree_destroy(GntWidget *widget)
874 GntTree *tree = GNT_TREE(widget);
876 end_search(tree);
877 if (tree->hash)
878 g_hash_table_destroy(tree->hash);
879 g_list_free(tree->list);
880 gnt_tree_free_columns(tree);
881 g_free(tree->priv);
884 static gboolean
885 gnt_tree_clicked(GntWidget *widget, GntMouseEvent event, int x, int y)
887 GntTree *tree = GNT_TREE(widget);
888 GntTreeRow *old = tree->current;
889 if (event == GNT_MOUSE_SCROLL_UP) {
890 action_up(GNT_BINDABLE(widget), NULL);
891 } else if (event == GNT_MOUSE_SCROLL_DOWN) {
892 action_down(GNT_BINDABLE(widget), NULL);
893 } else if (event == GNT_LEFT_MOUSE_DOWN) {
894 GntTreeRow *row;
895 GntTree *tree = GNT_TREE(widget);
896 int pos = 1;
897 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
898 pos = 0;
899 if (tree->show_title)
900 pos += 2;
901 pos = y - widget->priv.y - pos;
902 row = get_next_n(tree->top, pos);
903 if (row && tree->current != row) {
904 GntTreeRow *old = tree->current;
905 tree->current = row;
906 redraw_tree(tree);
907 tree_selection_changed(tree, old, tree->current);
908 } else if (row && row == tree->current) {
909 if (row->choice) {
910 row->isselected = !row->isselected;
911 g_signal_emit(tree, signals[SIG_TOGGLED], 0, row->key);
912 redraw_tree(tree);
913 } else {
914 gnt_widget_activate(widget);
917 } else {
918 return FALSE;
920 if (old != tree->current) {
921 tree_selection_changed(tree, old, tree->current);
923 return TRUE;
926 static void
927 gnt_tree_size_changed(GntWidget *widget, int w, int h)
929 GntTree *tree = GNT_TREE(widget);
930 if (widget->priv.width <= 0)
931 return;
933 readjust_columns(tree);
936 static gboolean
937 start_search(GntBindable *bindable, GList *list)
939 GntTree *tree = GNT_TREE(bindable);
940 if (tree->priv->search)
941 return FALSE;
942 GNT_WIDGET_SET_FLAGS(GNT_WIDGET(tree), GNT_WIDGET_DISABLE_ACTIONS);
943 tree->priv->search = g_string_new(NULL);
944 tree->priv->search_timeout = g_timeout_add_seconds(SEARCH_TIMEOUT_S, search_timeout, tree);
945 return TRUE;
948 static gboolean
949 end_search_action(GntBindable *bindable, GList *list)
951 GntTree *tree = GNT_TREE(bindable);
952 if (tree->priv->search == NULL)
953 return FALSE;
954 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(tree), GNT_WIDGET_DISABLE_ACTIONS);
955 end_search(tree);
956 redraw_tree(tree);
957 return TRUE;
960 static gboolean
961 move_first_action(GntBindable *bind, GList *null)
963 GntTree *tree = GNT_TREE(bind);
964 GntTreeRow *row = tree->root;
965 GntTreeRow *old = tree->current;
966 if (row && !row_matches_search(row))
967 row = get_next(row);
968 if (row) {
969 tree->current = row;
970 redraw_tree(tree);
971 if (old != tree->current)
972 tree_selection_changed(tree, old, tree->current);
975 return TRUE;
978 static gboolean
979 move_last_action(GntBindable *bind, GList *null)
981 GntTree *tree = GNT_TREE(bind);
982 GntTreeRow *old = tree->current;
983 GntTreeRow *row = tree->bottom;
984 GntTreeRow *next;
986 while ((next = get_next(row)))
987 row = next;
989 if (row) {
990 tree->current = row;
991 redraw_tree(tree);
992 if (old != tree->current)
993 tree_selection_changed(tree, old, tree->current);
996 return TRUE;
999 static void
1000 gnt_tree_set_property(GObject *obj, guint prop_id, const GValue *value,
1001 GParamSpec *spec)
1003 GntTree *tree = GNT_TREE(obj);
1004 switch (prop_id) {
1005 case PROP_COLUMNS:
1006 _gnt_tree_init_internals(tree, g_value_get_int(value));
1007 break;
1008 case PROP_EXPANDER:
1009 if (tree->priv->expander_level == g_value_get_int(value))
1010 break;
1011 tree->priv->expander_level = g_value_get_int(value);
1012 g_object_notify(obj, "expander-level");
1013 default:
1014 break;
1018 static void
1019 gnt_tree_get_property(GObject *obj, guint prop_id, GValue *value,
1020 GParamSpec *spec)
1022 GntTree *tree = GNT_TREE(obj);
1023 switch (prop_id) {
1024 case PROP_COLUMNS:
1025 g_value_set_int(value, tree->ncol);
1026 break;
1027 case PROP_EXPANDER:
1028 g_value_set_int(value, tree->priv->expander_level);
1029 break;
1030 default:
1031 break;
1035 static void
1036 gnt_tree_class_init(GntTreeClass *klass)
1038 GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass);
1039 GObjectClass *gclass = G_OBJECT_CLASS(klass);
1041 parent_class = GNT_WIDGET_CLASS(klass);
1042 parent_class->destroy = gnt_tree_destroy;
1043 parent_class->draw = gnt_tree_draw;
1044 parent_class->map = gnt_tree_map;
1045 parent_class->size_request = gnt_tree_size_request;
1046 parent_class->key_pressed = gnt_tree_key_pressed;
1047 parent_class->clicked = gnt_tree_clicked;
1048 parent_class->size_changed = gnt_tree_size_changed;
1050 gclass->set_property = gnt_tree_set_property;
1051 gclass->get_property = gnt_tree_get_property;
1052 g_object_class_install_property(gclass,
1053 PROP_COLUMNS,
1054 g_param_spec_int("columns", "Columns",
1055 "Number of columns in the tree.",
1056 1, G_MAXINT, 1,
1057 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
1060 g_object_class_install_property(gclass,
1061 PROP_EXPANDER,
1062 g_param_spec_int("expander-level", "Expander level",
1063 "Number of levels to show expander in the tree.",
1064 0, G_MAXINT, 1,
1065 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
1069 signals[SIG_SELECTION_CHANGED] =
1070 g_signal_new("selection-changed",
1071 G_TYPE_FROM_CLASS(klass),
1072 G_SIGNAL_RUN_LAST,
1073 G_STRUCT_OFFSET(GntTreeClass, selection_changed),
1074 NULL, NULL,
1075 gnt_closure_marshal_VOID__POINTER_POINTER,
1076 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
1077 signals[SIG_SCROLLED] =
1078 g_signal_new("scrolled",
1079 G_TYPE_FROM_CLASS(klass),
1080 G_SIGNAL_RUN_LAST,
1082 NULL, NULL,
1083 g_cclosure_marshal_VOID__INT,
1084 G_TYPE_NONE, 1, G_TYPE_INT);
1085 signals[SIG_TOGGLED] =
1086 g_signal_new("toggled",
1087 G_TYPE_FROM_CLASS(klass),
1088 G_SIGNAL_RUN_LAST,
1089 G_STRUCT_OFFSET(GntTreeClass, toggled),
1090 NULL, NULL,
1091 g_cclosure_marshal_VOID__POINTER,
1092 G_TYPE_NONE, 1, G_TYPE_POINTER);
1093 signals[SIG_COLLAPSED] =
1094 g_signal_new("collapse-toggled",
1095 G_TYPE_FROM_CLASS(klass),
1096 G_SIGNAL_RUN_LAST,
1098 NULL, NULL,
1099 gnt_closure_marshal_VOID__POINTER_BOOLEAN,
1100 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
1102 gnt_bindable_class_register_action(bindable, "move-up", action_up,
1103 GNT_KEY_UP, NULL);
1104 gnt_bindable_register_binding(bindable, "move-up", GNT_KEY_CTRL_P, NULL);
1105 gnt_bindable_class_register_action(bindable, "move-down", action_down,
1106 GNT_KEY_DOWN, NULL);
1107 gnt_bindable_register_binding(bindable, "move-down", GNT_KEY_CTRL_N, NULL);
1108 gnt_bindable_class_register_action(bindable, "move-parent", action_move_parent,
1109 GNT_KEY_BACKSPACE, NULL);
1110 gnt_bindable_class_register_action(bindable, "page-up", action_page_up,
1111 GNT_KEY_PGUP, NULL);
1112 gnt_bindable_class_register_action(bindable, "page-down", action_page_down,
1113 GNT_KEY_PGDOWN, NULL);
1114 gnt_bindable_class_register_action(bindable, "start-search", start_search,
1115 "/", NULL);
1116 gnt_bindable_class_register_action(bindable, "end-search", end_search_action,
1117 "\033", NULL);
1118 gnt_bindable_class_register_action(bindable, "move-first", move_first_action,
1119 GNT_KEY_HOME, NULL);
1120 gnt_bindable_class_register_action(bindable, "move-last", move_last_action,
1121 GNT_KEY_END, NULL);
1123 gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), bindable);
1124 GNTDEBUG;
1127 static void
1128 gnt_tree_init(GTypeInstance *instance, gpointer class)
1130 GntWidget *widget = GNT_WIDGET(instance);
1131 GntTree *tree = GNT_TREE(widget);
1132 tree->show_separator = TRUE;
1133 tree->priv = g_new0(GntTreePriv, 1);
1134 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X | GNT_WIDGET_GROW_Y |
1135 GNT_WIDGET_CAN_TAKE_FOCUS | GNT_WIDGET_NO_SHADOW);
1136 gnt_widget_set_take_focus(widget, TRUE);
1137 widget->priv.minw = 4;
1138 widget->priv.minh = 1;
1139 GNTDEBUG;
1142 /******************************************************************************
1143 * GntTree API
1144 *****************************************************************************/
1145 GType
1146 gnt_tree_get_gtype(void)
1148 static GType type = 0;
1150 if(type == 0)
1152 static const GTypeInfo info = {
1153 sizeof(GntTreeClass),
1154 NULL, /* base_init */
1155 NULL, /* base_finalize */
1156 (GClassInitFunc)gnt_tree_class_init,
1157 NULL, /* class_finalize */
1158 NULL, /* class_data */
1159 sizeof(GntTree),
1160 0, /* n_preallocs */
1161 gnt_tree_init, /* instance_init */
1162 NULL /* value_table */
1165 type = g_type_register_static(GNT_TYPE_WIDGET,
1166 "GntTree",
1167 &info, 0);
1170 return type;
1173 static void
1174 free_tree_col(gpointer data)
1176 GntTreeCol *col = data;
1177 if (!col->isbinary)
1178 g_free(col->text);
1179 g_free(col);
1182 static void
1183 free_tree_row(gpointer data)
1185 GntTreeRow *row = data;
1187 if (!row)
1188 return;
1190 g_list_foreach(row->columns, (GFunc)free_tree_col, NULL);
1191 g_list_free(row->columns);
1192 g_free(row);
1195 GntWidget *gnt_tree_new()
1197 return gnt_tree_new_with_columns(1);
1200 void gnt_tree_set_visible_rows(GntTree *tree, int rows)
1202 GntWidget *widget = GNT_WIDGET(tree);
1203 widget->priv.height = rows;
1204 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
1205 widget->priv.height += 2;
1208 int gnt_tree_get_visible_rows(GntTree *tree)
1210 GntWidget *widget = GNT_WIDGET(tree);
1211 int ret = widget->priv.height;
1212 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
1213 ret -= 2;
1214 return ret;
1217 GList *gnt_tree_get_rows(GntTree *tree)
1219 return tree->list;
1222 void gnt_tree_scroll(GntTree *tree, int count)
1224 GntTreeRow *row;
1226 if (count < 0)
1228 if (get_root_distance(tree->top) == 0)
1229 return;
1230 row = get_prev_n(tree->top, -count);
1231 if (row == NULL)
1232 row = tree->root;
1233 tree->top = row;
1235 else
1237 get_next_n_opt(tree->bottom, count, &count);
1238 tree->top = get_next_n(tree->top, count);
1241 redraw_tree(tree);
1242 g_signal_emit(tree, signals[SIG_SCROLLED], 0, count);
1245 static gpointer
1246 find_position(GntTree *tree, gpointer key, gpointer parent)
1248 GntTreeRow *row;
1250 if (tree->priv->compare == NULL)
1251 return NULL;
1253 if (parent == NULL)
1254 row = tree->root;
1255 else
1256 row = g_hash_table_lookup(tree->hash, parent);
1258 if (!row)
1259 return NULL;
1261 if (parent)
1262 row = row->child;
1264 while (row)
1266 if (tree->priv->compare(key, row->key) < 0)
1267 return (row->prev ? row->prev->key : NULL);
1268 if (row->next)
1269 row = row->next;
1270 else
1271 return row->key;
1273 return NULL;
1276 void gnt_tree_sort_row(GntTree *tree, gpointer key)
1278 GntTreeRow *row, *q, *s;
1279 int current, newp;
1281 if (!tree->priv->compare)
1282 return;
1284 row = g_hash_table_lookup(tree->hash, key);
1285 g_return_if_fail(row != NULL);
1287 current = g_list_index(tree->list, key);
1289 if (row->parent)
1290 s = row->parent->child;
1291 else
1292 s = tree->root;
1294 q = NULL;
1295 while (s) {
1296 if (tree->priv->compare(row->key, s->key) < 0)
1297 break;
1298 q = s;
1299 s = s->next;
1302 /* Move row between q and s */
1303 if (row == q || row == s)
1304 return;
1306 if (q == NULL) {
1307 /* row becomes the first child of its parent */
1308 row->prev->next = row->next; /* row->prev cannot be NULL at this point */
1309 if (row->next)
1310 row->next->prev = row->prev;
1311 if (row->parent)
1312 row->parent->child = row;
1313 else
1314 tree->root = row;
1315 row->next = s;
1316 s->prev = row; /* s cannot be NULL */
1317 row->prev = NULL;
1318 newp = g_list_index(tree->list, s) - 1;
1319 } else {
1320 if (row->prev) {
1321 row->prev->next = row->next;
1322 } else {
1323 /* row was the first child of its parent */
1324 if (row->parent)
1325 row->parent->child = row->next;
1326 else
1327 tree->top = row->next;
1330 if (row->next)
1331 row->next->prev = row->prev;
1333 q->next = row;
1334 row->prev = q;
1335 if (s)
1336 s->prev = row;
1337 row->next = s;
1338 newp = g_list_index(tree->list, q) + 1;
1340 tree->list = g_list_reposition_child(tree->list, current, newp);
1342 redraw_tree(tree);
1345 GntTreeRow *gnt_tree_add_row_after(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro)
1347 GntTreeRow *pr = NULL;
1349 if (g_hash_table_lookup(tree->hash, key)) {
1350 gnt_tree_remove(tree, key);
1353 row->tree = tree;
1354 row->key = key;
1355 row->data = NULL;
1356 g_hash_table_replace(tree->hash, key, row);
1358 if (bigbro == NULL && tree->priv->compare)
1360 bigbro = find_position(tree, key, parent);
1363 if (tree->root == NULL)
1365 tree->root = row;
1366 tree->list = g_list_prepend(tree->list, key);
1368 else
1370 int position = 0;
1372 if (bigbro)
1374 pr = g_hash_table_lookup(tree->hash, bigbro);
1375 if (pr)
1377 if (pr->next) pr->next->prev = row;
1378 row->next = pr->next;
1379 row->prev = pr;
1380 pr->next = row;
1381 row->parent = pr->parent;
1383 position = g_list_index(tree->list, bigbro);
1387 if (pr == NULL && parent)
1389 pr = g_hash_table_lookup(tree->hash, parent);
1390 if (pr)
1392 if (pr->child) pr->child->prev = row;
1393 row->next = pr->child;
1394 pr->child = row;
1395 row->parent = pr;
1397 position = g_list_index(tree->list, parent);
1401 if (pr == NULL)
1403 GntTreeRow *r = tree->root;
1404 row->next = r;
1405 if (r) r->prev = row;
1406 if (tree->current == tree->root)
1407 tree->current = row;
1408 tree->root = row;
1409 tree->list = g_list_prepend(tree->list, key);
1411 else
1413 tree->list = g_list_insert(tree->list, key, position + 1);
1416 redraw_tree(tree);
1418 return row;
1421 GntTreeRow *gnt_tree_add_row_last(GntTree *tree, void *key, GntTreeRow *row, void *parent)
1423 GntTreeRow *pr = NULL, *br = NULL;
1425 if (parent)
1426 pr = g_hash_table_lookup(tree->hash, parent);
1428 if (pr)
1429 br = pr->child;
1430 else
1431 br = tree->root;
1433 if (br)
1435 while (br->next)
1436 br = br->next;
1439 return gnt_tree_add_row_after(tree, key, row, parent, br ? br->key : NULL);
1442 gpointer gnt_tree_get_selection_data(GntTree *tree)
1444 if (tree->current)
1445 return tree->current->key; /* XXX: perhaps we should just get rid of 'data' */
1446 return NULL;
1449 char *gnt_tree_get_selection_text(GntTree *tree)
1451 if (tree->current)
1452 return update_row_text(tree, tree->current);
1453 return NULL;
1456 GList *gnt_tree_get_row_text_list(GntTree *tree, gpointer key)
1458 GList *list = NULL, *iter;
1459 GntTreeRow *row = key ? g_hash_table_lookup(tree->hash, key) : tree->current;
1460 int i;
1462 if (!row)
1463 return NULL;
1465 for (i = 0, iter = row->columns; i < tree->ncol && iter;
1466 i++, iter = iter->next)
1468 GntTreeCol *col = iter->data;
1469 list = g_list_append(list, BINARY_DATA(tree, i) ? col->text : g_strdup(col->text));
1472 return list;
1475 GList *gnt_tree_get_selection_text_list(GntTree *tree)
1477 return gnt_tree_get_row_text_list(tree, NULL);
1480 void gnt_tree_remove(GntTree *tree, gpointer key)
1482 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1483 static int depth = 0; /* Only redraw after all child nodes are removed */
1484 if (row)
1486 gboolean redraw = FALSE;
1488 if (row->child) {
1489 depth++;
1490 while (row->child) {
1491 gnt_tree_remove(tree, row->child->key);
1493 depth--;
1496 if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0)
1497 redraw = TRUE;
1499 /* Update root/top/current/bottom if necessary */
1500 if (tree->root == row)
1501 tree->root = get_next(row);
1502 if (tree->top == row)
1504 if (tree->top != tree->root)
1505 tree->top = get_prev(row);
1506 else
1507 tree->top = get_next(row);
1509 if (tree->current == row)
1511 if (tree->current != tree->root)
1512 tree->current = get_prev(row);
1513 else
1514 tree->current = get_next(row);
1515 tree_selection_changed(tree, row, tree->current);
1517 if (tree->bottom == row)
1519 tree->bottom = get_prev(row);
1522 /* Fix the links */
1523 if (row->next)
1524 row->next->prev = row->prev;
1525 if (row->parent && row->parent->child == row)
1526 row->parent->child = row->next;
1527 if (row->prev)
1528 row->prev->next = row->next;
1530 g_hash_table_remove(tree->hash, key);
1531 tree->list = g_list_remove(tree->list, key);
1533 if (redraw && depth == 0)
1535 redraw_tree(tree);
1540 static gboolean
1541 return_true(gpointer key, gpointer data, gpointer null)
1543 return TRUE;
1546 void gnt_tree_remove_all(GntTree *tree)
1548 tree->root = NULL;
1549 g_hash_table_foreach_remove(tree->hash, (GHRFunc)return_true, tree);
1550 g_list_free(tree->list);
1551 tree->list = NULL;
1552 tree->current = tree->top = tree->bottom = NULL;
1555 int gnt_tree_get_selection_visible_line(GntTree *tree)
1557 return get_distance(tree->top, tree->current) +
1558 !!(GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER));
1561 void gnt_tree_change_text(GntTree *tree, gpointer key, int colno, const char *text)
1563 GntTreeRow *row;
1564 GntTreeCol *col;
1566 g_return_if_fail(colno < tree->ncol);
1568 row = g_hash_table_lookup(tree->hash, key);
1569 if (row)
1571 col = g_list_nth_data(row->columns, colno);
1572 if (BINARY_DATA(tree, colno)) {
1573 col->text = (gpointer)text;
1574 } else {
1575 g_free(col->text);
1576 col->text = g_strdup(text ? text : "");
1579 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED) &&
1580 get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0)
1581 redraw_tree(tree);
1585 GntTreeRow *gnt_tree_add_choice(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro)
1587 GntTreeRow *r;
1588 r = g_hash_table_lookup(tree->hash, key);
1589 g_return_val_if_fail(!r || !r->choice, NULL);
1591 if (bigbro == NULL) {
1592 if (tree->priv->compare)
1593 bigbro = find_position(tree, key, parent);
1594 else {
1595 r = g_hash_table_lookup(tree->hash, parent);
1596 if (!r)
1597 r = tree->root;
1598 else
1599 r = r->child;
1600 if (r) {
1601 while (r->next)
1602 r = r->next;
1603 bigbro = r->key;
1607 row = gnt_tree_add_row_after(tree, key, row, parent, bigbro);
1608 row->choice = TRUE;
1610 return row;
1613 void gnt_tree_set_choice(GntTree *tree, void *key, gboolean set)
1615 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1617 if (!row)
1618 return;
1619 g_return_if_fail(row->choice);
1621 row->isselected = set;
1622 redraw_tree(tree);
1625 gboolean gnt_tree_get_choice(GntTree *tree, void *key)
1627 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1629 if (!row)
1630 return FALSE;
1631 g_return_val_if_fail(row->choice, FALSE);
1633 return row->isselected;
1636 void gnt_tree_set_row_flags(GntTree *tree, void *key, GntTextFormatFlags flags)
1638 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1639 if (!row || row->flags == flags)
1640 return;
1642 row->flags = flags;
1643 redraw_tree(tree); /* XXX: It shouldn't be necessary to redraw the whole darned tree */
1646 void gnt_tree_set_row_color(GntTree *tree, void *key, int color)
1648 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1649 if (!row || row->color == color)
1650 return;
1652 row->color = color;
1653 redraw_tree(tree);
1656 void gnt_tree_set_selected(GntTree *tree , void *key)
1658 int dist;
1659 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1660 if (!row || row == tree->current)
1661 return;
1663 if (tree->top == NULL)
1664 tree->top = row;
1665 if (tree->bottom == NULL)
1666 tree->bottom = row;
1668 tree->current = row;
1669 if ((dist = get_distance(tree->current, tree->bottom)) < 0)
1670 gnt_tree_scroll(tree, -dist);
1671 else if ((dist = get_distance(tree->current, tree->top)) > 0)
1672 gnt_tree_scroll(tree, -dist);
1673 else
1674 redraw_tree(tree);
1675 tree_selection_changed(tree, row, tree->current);
1678 static void _gnt_tree_init_internals(GntTree *tree, int col)
1680 gnt_tree_free_columns(tree);
1682 tree->ncol = col;
1683 tree->hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_tree_row);
1684 tree->columns = g_new0(struct _GntTreeColInfo, col);
1685 tree->priv->lastvisible = col - 1;
1686 while (col--)
1688 tree->columns[col].width = 15;
1690 tree->list = NULL;
1691 tree->show_title = FALSE;
1692 g_object_notify(G_OBJECT(tree), "columns");
1695 GntWidget *gnt_tree_new_with_columns(int col)
1697 GntWidget *widget = g_object_new(GNT_TYPE_TREE,
1698 "columns", col,
1699 "expander-level", 1,
1700 NULL);
1702 return widget;
1705 GntTreeRow *gnt_tree_create_row_from_list(GntTree *tree, GList *list)
1707 GList *iter;
1708 int i;
1709 GntTreeRow *row = g_new0(GntTreeRow, 1);
1711 for (i = 0, iter = list; i < tree->ncol && iter; iter = iter->next, i++)
1713 GntTreeCol *col = g_new0(GntTreeCol, 1);
1714 col->span = 1;
1715 if (BINARY_DATA(tree, i)) {
1716 col->text = iter->data;
1717 col->isbinary = TRUE;
1718 } else {
1719 col->text = g_strdup(iter->data ? iter->data : "");
1720 col->isbinary = FALSE;
1723 row->columns = g_list_append(row->columns, col);
1726 return row;
1729 GntTreeRow *gnt_tree_create_row(GntTree *tree, ...)
1731 int i;
1732 va_list args;
1733 GList *list = NULL;
1734 GntTreeRow *row;
1736 va_start(args, tree);
1737 for (i = 0; i < tree->ncol; i++)
1739 list = g_list_append(list, va_arg(args, char *));
1741 va_end(args);
1743 row = gnt_tree_create_row_from_list(tree, list);
1744 g_list_free(list);
1746 return row;
1749 void gnt_tree_set_col_width(GntTree *tree, int col, int width)
1751 g_return_if_fail(col < tree->ncol);
1753 tree->columns[col].width = width;
1754 if (tree->columns[col].width_ratio == 0)
1755 tree->columns[col].width_ratio = width;
1758 void gnt_tree_set_column_title(GntTree *tree, int index, const char *title)
1760 g_free(tree->columns[index].title);
1761 tree->columns[index].title = g_strdup(title);
1764 void gnt_tree_set_column_titles(GntTree *tree, ...)
1766 int i;
1767 va_list args;
1769 va_start(args, tree);
1770 for (i = 0; i < tree->ncol; i++)
1772 const char *title = va_arg(args, const char *);
1773 tree->columns[i].title = g_strdup(title);
1775 va_end(args);
1778 void gnt_tree_set_show_title(GntTree *tree, gboolean set)
1780 tree->show_title = set;
1781 GNT_WIDGET(tree)->priv.minh = (set ? 6 : 4);
1784 void gnt_tree_set_compare_func(GntTree *tree, GCompareFunc func)
1786 tree->priv->compare = func;
1789 void gnt_tree_set_expanded(GntTree *tree, void *key, gboolean expanded)
1791 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1792 if (row) {
1793 row->collapsed = !expanded;
1794 if (GNT_WIDGET(tree)->window)
1795 gnt_widget_draw(GNT_WIDGET(tree));
1796 g_signal_emit(tree, signals[SIG_COLLAPSED], 0, key, row->collapsed);
1800 void gnt_tree_set_show_separator(GntTree *tree, gboolean set)
1802 tree->show_separator = set;
1805 void gnt_tree_adjust_columns(GntTree *tree)
1807 GntTreeRow *row = tree->root;
1808 int *widths, i, twidth;
1810 widths = g_new0(int, tree->ncol);
1811 while (row) {
1812 GList *iter;
1813 for (i = 0, iter = row->columns; iter; iter = iter->next, i++) {
1814 GntTreeCol *col = iter->data;
1815 int w = gnt_util_onscreen_width(col->text, NULL);
1816 if (i == 0 && row->choice)
1817 w += 4;
1818 if (i == 0) {
1819 w += find_depth(row) * TAB_SIZE;
1821 if (widths[i] < w)
1822 widths[i] = w;
1824 row = get_next(row);
1827 twidth = 1 + 2 * (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER));
1828 for (i = 0; i < tree->ncol; i++) {
1829 if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE)
1830 widths[i] = tree->columns[i].width;
1831 gnt_tree_set_col_width(tree, i, widths[i]);
1832 if (!COLUMN_INVISIBLE(tree, i)) {
1833 twidth = twidth + widths[i];
1834 if (tree->priv->lastvisible != i)
1835 twidth += 1;
1838 g_free(widths);
1840 gnt_widget_set_size(GNT_WIDGET(tree), twidth, -1);
1843 void gnt_tree_set_hash_fns(GntTree *tree, gpointer hash, gpointer eq, gpointer kd)
1845 g_hash_table_foreach_remove(tree->hash, return_true, NULL);
1846 g_hash_table_destroy(tree->hash);
1847 tree->hash = g_hash_table_new_full(hash, eq, kd, free_tree_row);
1850 static void
1851 set_column_flag(GntTree *tree, int col, GntTreeColumnFlag flag, gboolean set)
1853 if (set)
1854 tree->columns[col].flags |= flag;
1855 else
1856 tree->columns[col].flags &= ~flag;
1859 void gnt_tree_set_column_visible(GntTree *tree, int col, gboolean vis)
1861 g_return_if_fail(col < tree->ncol);
1862 set_column_flag(tree, col, GNT_TREE_COLUMN_INVISIBLE, !vis);
1863 if (vis) {
1864 /* the column is visible */
1865 if (tree->priv->lastvisible < col)
1866 tree->priv->lastvisible = col;
1867 } else {
1868 if (tree->priv->lastvisible == col)
1869 while (tree->priv->lastvisible) {
1870 tree->priv->lastvisible--;
1871 if (!COLUMN_INVISIBLE(tree, tree->priv->lastvisible))
1872 break;
1875 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED))
1876 readjust_columns(tree);
1879 void gnt_tree_set_column_resizable(GntTree *tree, int col, gboolean res)
1881 g_return_if_fail(col < tree->ncol);
1882 set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, !res);
1885 void gnt_tree_set_column_is_binary(GntTree *tree, int col, gboolean bin)
1887 g_return_if_fail(col < tree->ncol);
1888 set_column_flag(tree, col, GNT_TREE_COLUMN_BINARY_DATA, bin);
1891 void gnt_tree_set_column_is_right_aligned(GntTree *tree, int col, gboolean right)
1893 g_return_if_fail(col < tree->ncol);
1894 set_column_flag(tree, col, GNT_TREE_COLUMN_RIGHT_ALIGNED, right);
1897 void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[])
1899 int i;
1900 for (i = 0; i < tree->ncol && cols[i]; i++) {
1901 tree->columns[i].width_ratio = cols[i];
1905 void gnt_tree_set_search_column(GntTree *tree, int col)
1907 g_return_if_fail(col < tree->ncol);
1908 g_return_if_fail(!BINARY_DATA(tree, col));
1909 tree->priv->search_column = col;
1912 gboolean gnt_tree_is_searching(GntTree *tree)
1914 return (tree->priv->search != NULL);
1917 void gnt_tree_set_search_function(GntTree *tree,
1918 gboolean (*func)(GntTree *tree, gpointer key, const char *search, const char *current))
1920 tree->priv->search_func = func;
1923 gpointer gnt_tree_get_parent_key(GntTree *tree, gpointer key)
1925 GntTreeRow *row = g_hash_table_lookup(tree->hash, key);
1926 return (row && row->parent) ? row->parent->key : NULL;
1929 gpointer gnt_tree_row_get_key(GntTree *tree, GntTreeRow *row)
1931 g_return_val_if_fail(row && row->tree == tree, NULL);
1932 return row->key;
1935 GntTreeRow * gnt_tree_row_get_next(GntTree *tree, GntTreeRow *row)
1937 g_return_val_if_fail(row && row->tree == tree, NULL);
1938 return row->next;
1941 GntTreeRow * gnt_tree_row_get_prev(GntTree *tree, GntTreeRow *row)
1943 g_return_val_if_fail(row && row->tree == tree, NULL);
1944 return row->prev;
1947 GntTreeRow * gnt_tree_row_get_child(GntTree *tree, GntTreeRow *row)
1949 g_return_val_if_fail(row && row->tree == tree, NULL);
1950 return row->child;
1953 GntTreeRow * gnt_tree_row_get_parent(GntTree *tree, GntTreeRow *row)
1955 g_return_val_if_fail(row && row->tree == tree, NULL);
1956 return row->parent;