Fix build
soc.2013.gobjectification.plugins
2014-04-29, Ankit Vani
* GNT - The GLib Ncurses Toolkit * GNT is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA #define SEARCH_TIMEOUT_S 4 /* 4 secs */ #define SEARCHING(tree) (tree->priv->search && tree->priv->search->len > 0) #define COLUMN_INVISIBLE(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_INVISIBLE) #define BINARY_DATA(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_BINARY_DATA) #define RIGHT_ALIGNED(tree, index) (tree->columns[index].flags & GNT_TREE_COLUMN_RIGHT_ALIGNED) gboolean (*search_func)(GntTree *tree, gpointer key, const char *search, const char *current); /* XXX: Make this one into a GObject? void *data; /* XXX: unused */ gboolean choice; /* Is this a choice-box? If choice is true, then child will be NULL */ GntTextFormatFlags flags; int span; /* How many columns does it span? */ static void tree_selection_changed(GntTree *, GntTreeRow *, GntTreeRow *); static void _gnt_tree_init_internals(GntTree *tree, int col); static GntWidgetClass *parent_class = NULL; static guint signals[SIGS] = { 0 }; readjust_columns(GntTree *tree) #define WIDTH(i) (tree->columns[i].width_ratio ? tree->columns[i].width_ratio : tree->columns[i].width) gnt_widget_get_size(GNT_WIDGET(tree), &width, NULL); if (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER)) width -= 1; /* Exclude the scrollbar from the calculation */ for (i = 0, total = 0; i < tree->ncol ; i++) { if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE) if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE) width -= WIDTH(i) + (tree->priv->lastvisible != i); total += WIDTH(i) + (tree->priv->lastvisible != i); for (i = 0; i < tree->ncol; i++) { if (tree->columns[i].flags & GNT_TREE_COLUMN_INVISIBLE) if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE) col = (WIDTH(i) * width) / total; gnt_tree_set_col_width(GNT_TREE(tree), i, col); /* Move the item at position old to position new */ g_list_reposition_child(GList *list, int old, int new) gpointer item = g_list_nth_data(list, old); list = g_list_remove(list, item); new--; /* because the positions would have shifted after removing the item */ list = g_list_insert(list, item, new); _get_next(GntTreeRow *row, gboolean godeep) if (godeep && row->child) return _get_next(row->parent, FALSE); row_matches_search(GntTreeRow *row) if (t->priv->search && t->priv->search->len > 0) { GntTreeCol *col = (col = g_list_nth_data(row->columns, t->priv->search_column)) ? col : row->columns->data; if (t->priv->search_func) return t->priv->search_func(t, row->key, t->priv->search->str, col->text); one = g_utf8_casefold(col->text, -1); two = g_utf8_casefold(t->priv->search->str, -1); get_next(GntTreeRow *row) while ((row = _get_next(row, !row->collapsed)) != NULL) { if (row_matches_search(row)) /* Returns the n-th next row. If it doesn't exist, returns NULL */ get_next_n(GntTreeRow *row, int n) /* Returns the n-th next row. If it doesn't exist, then the last non-NULL node */ get_next_n_opt(GntTreeRow *row, int n, int *pos) get_last_child(GntTreeRow *row) if (!row->collapsed && row->child) return get_last_child(row); get_prev(GntTreeRow *row) row = get_last_child(row->prev); if (!row || row_matches_search(row)) get_prev_n(GntTreeRow *row, int n) /* Distance of row from the root */ /* XXX: This is uber-inefficient */ get_root_distance(GntTreeRow *row) return get_root_distance(get_prev(row)) + 1; /* Returns the distance between a and b. * If a is 'above' b, then the distance is positive */ get_distance(GntTreeRow *a, GntTreeRow *b) /* First get the distance from a to the root. * Then the distance from b to the root. * It's not that good, but it works. */ int ha = get_root_distance(a); int hb = get_root_distance(b); find_depth(GntTreeRow *row) update_row_text(GntTree *tree, GntTreeRow *row) GString *string = g_string_new(NULL); gboolean notfirst = FALSE; for (i = 0, iter = row->columns; i < tree->ncol && iter; i++, iter = iter->next) GntTreeCol *col = iter->data; if (COLUMN_INVISIBLE(tree, i)) if (BINARY_DATA(tree, i)) len = gnt_util_onscreen_width(display, NULL); width = tree->columns[i].width; g_string_append_printf(string, "[%c] ", row->isselected ? 'X' : ' '); else if (find_depth(row) < tree->priv->expander_level && row->child) string = g_string_append(string, "+ "); string = g_string_append(string, "- "); fl = TAB_SIZE * find_depth(row); g_string_append_printf(string, "%*s", fl, ""); } else if (notfirst && tree->show_separator) g_string_append_c(string, '|'); g_string_append_c(string, ' '); if (RIGHT_ALIGNED(tree, i) && len < tree->columns[i].width) { g_string_append_printf(string, "%*s", width - len - cut, ""); text = gnt_util_onscreen_width_to_pointer(display, len - fl, NULL); string = g_string_append_len(string, display, text - display); if (cut && width > 1) { /* ellipsis */ g_string_append_c(string, '~'); string = g_string_append(string, "\342\200\246"); if (!RIGHT_ALIGNED(tree, i) && len < tree->columns[i].width && iter->next) g_string_append_printf(string, "%*s", width - len, ""); return g_string_free(string, FALSE); #define NEXT_X x += tree->columns[i].width + (i > 0 ? 1 : 0) tree_mark_columns(GntTree *tree, int pos, int y, chtype type) GntWidget *widget = GNT_WIDGET(tree); gboolean notfirst = FALSE; for (i = 0; i < tree->ncol - 1; i++) if (!COLUMN_INVISIBLE(tree, i)) { if (!COLUMN_INVISIBLE(tree, i+1) && notfirst) mvwaddch(widget->window, y, x, type); redraw_tree(GntTree *tree) GntWidget *widget = GNT_WIDGET(tree); if (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED)) if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) if (tree->current == NULL && tree->root != NULL) { tree->current = tree->root; tree_selection_changed(tree, NULL, tree->current); wbkgd(widget->window, gnt_color_pair(GNT_COLOR_NORMAL)); mvwhline(widget->window, pos + 1, pos, ACS_HLINE | gnt_color_pair(GNT_COLOR_NORMAL), widget->priv.width - pos - 1); mvwhline(widget->window, pos, pos, ' ' | gnt_color_pair(GNT_COLOR_NORMAL), widget->priv.width - pos - 1); for (i = 0; i < tree->ncol; i++) if (COLUMN_INVISIBLE(tree, i)) { mvwaddnstr(widget->window, pos, x + (x != pos), tree->columns[i].title, tree->columns[i].width); tree_mark_columns(tree, pos, 0, (tree->show_separator ? ACS_TTEE : ACS_HLINE) | gnt_color_pair(GNT_COLOR_NORMAL)); tree_mark_columns(tree, pos, widget->priv.height - pos, (tree->show_separator ? ACS_BTEE : ACS_HLINE) | gnt_color_pair(GNT_COLOR_NORMAL)); tree_mark_columns(tree, pos, pos + 1, (tree->show_separator ? ACS_PLUS : ACS_HLINE) | gnt_color_pair(GNT_COLOR_NORMAL)); tree_mark_columns(tree, pos, pos, (tree->show_separator ? ACS_VLINE : ' ') | gnt_color_pair(GNT_COLOR_NORMAL)); rows = widget->priv.height - pos * 2 - start - 1; tree->bottom = get_next_n_opt(tree->top, rows, &down); tree->top = get_prev_n(tree->bottom, rows); up = get_distance(tree->top, tree->current); tree->top = tree->current; else if (up >= widget->priv.height - pos) tree->top = get_prev_n(tree->current, rows); if (tree->top && !row_matches_search(tree->top)) tree->top = get_next(tree->top); scrcol = widget->priv.width - 1 - 2 * pos; /* exclude the borders and the scrollbar */ if (tree->current && !row_matches_search(tree->current)) { GntTreeRow *old = tree->current; tree->current = tree->top; tree_selection_changed(tree, old, tree->current); for (i = start + pos; row && i < widget->priv.height - pos; i++, row = get_next(row)) GntTextFormatFlags flags = row->flags; if (!row_matches_search(row)) str = update_row_text(tree, row); if ((wr = gnt_util_onscreen_width(str, NULL)) > scrcol) char *s = (char*)gnt_util_onscreen_width_to_pointer(str, scrcol, &wr); if (flags & GNT_TEXT_FLAG_BOLD) if (flags & GNT_TEXT_FLAG_UNDERLINE) if (flags & GNT_TEXT_FLAG_BLINK) if (row == tree->current) if (gnt_widget_has_focus(widget)) attr |= gnt_color_pair(GNT_COLOR_HIGHLIGHT); attr |= gnt_color_pair(GNT_COLOR_HIGHLIGHT_D); if (flags & GNT_TEXT_FLAG_DIM) attr |= (A_DIM | gnt_color_pair(row->color)); attr |= (A_DIM | gnt_color_pair(GNT_COLOR_DISABLED)); else if (flags & GNT_TEXT_FLAG_HIGHLIGHT) attr |= (A_DIM | gnt_color_pair(GNT_COLOR_HIGHLIGHT)); attr |= gnt_color_pair(row->color); attr |= gnt_color_pair(GNT_COLOR_NORMAL); wbkgdset(widget->window, '\0' | attr); mvwaddstr(widget->window, i, pos, C_(str)); whline(widget->window, ' ', scrcol - wr); tree_mark_columns(tree, pos, i, (tree->show_separator ? ACS_VLINE : ' ') | attr); wbkgdset(widget->window, '\0' | gnt_color_pair(GNT_COLOR_NORMAL)); while (i < widget->priv.height - pos) mvwhline(widget->window, i, pos, ' ', widget->priv.width - pos * 2 - 1); tree_mark_columns(tree, pos, i, (tree->show_separator ? ACS_VLINE : ' ')); scrcol = widget->priv.width - pos - 1; /* position of the scrollbar */ get_next_n_opt(tree->root, g_list_length(tree->list), &total); showing = rows * rows / MAX(total, 1) + 1; showing = MIN(rows, showing); up = get_distance(tree->root, tree->top); position = (rows - showing) * up / MAX(1, up + down); position = MAX((tree->top != tree->root), position); if (showing + position > rows) position = rows - showing; if (showing + position == rows && row) position = MAX(0, rows - 1 - showing); else if (showing + position < rows && !row) position = rows - showing; position += pos + start + 1; mvwvline(widget->window, pos + start + 1, scrcol, ' ' | gnt_color_pair(GNT_COLOR_NORMAL), rows); mvwvline(widget->window, position, scrcol, ACS_CKBOARD | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D), showing); mvwaddch(widget->window, start + pos, scrcol, ((tree->top != tree->root) ? ACS_UARROW : ' ') | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D)); mvwaddch(widget->window, widget->priv.height - pos - 1, scrcol, (row ? ACS_DARROW : ' ') | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D)); /* If there's a search-text, show it in the bottom of the tree */ if (tree->priv->search && tree->priv->search->len > 0) { const char *str = gnt_util_onscreen_width_to_pointer(tree->priv->search->str, scrcol - 1, NULL); wbkgdset(widget->window, '\0' | gnt_color_pair(GNT_COLOR_HIGHLIGHT_D)); mvwaddnstr(widget->window, widget->priv.height - pos - 1, pos, tree->priv->search->str, str - tree->priv->search->str); wmove(widget->window, current, pos); gnt_widget_queue_update(widget); gnt_tree_draw(GntWidget *widget) GntTree *tree = GNT_TREE(widget); gnt_tree_size_request(GntWidget *widget) if (widget->priv.height == 0) widget->priv.height = 10; /* XXX: Why?! */ if (widget->priv.width == 0) GntTree *tree = GNT_TREE(widget); width = 1 + 2 * (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER)); for (i = 0; i < tree->ncol; i++) if (!COLUMN_INVISIBLE(tree, i)) { width = width + tree->columns[i].width; if (tree->priv->lastvisible != i) widget->priv.width = width; gnt_tree_map(GntWidget *widget) GntTree *tree = GNT_TREE(widget); if (widget->priv.width == 0 || widget->priv.height == 0) gnt_widget_size_request(widget); tree->current = tree->root; tree_selection_changed(GntTree *tree, GntTreeRow *old, GntTreeRow *current) g_signal_emit(tree, signals[SIG_SELECTION_CHANGED], 0, old ? old->key : NULL, current ? current->key : NULL); action_down(GntBindable *bind, GList *null) GntTree *tree = GNT_TREE(bind); GntTreeRow *old = tree->current; GntTreeRow *row = get_next(tree->current); if ((dist = get_distance(tree->current, tree->bottom)) < 0) gnt_tree_scroll(tree, -dist); if (old != tree->current) tree_selection_changed(tree, old, tree->current); action_move_parent(GntBindable *bind, GList *null) GntTree *tree = GNT_TREE(bind); GntTreeRow *row = tree->current; if (!row || !row->parent || SEARCHING(tree)) tree->current = row->parent; if ((dist = get_distance(tree->current, tree->top)) > 0) gnt_tree_scroll(tree, -dist); tree_selection_changed(tree, row, tree->current); action_up(GntBindable *bind, GList *list) GntTree *tree = GNT_TREE(bind); GntTreeRow *old = tree->current; GntTreeRow *row = get_prev(tree->current); if ((dist = get_distance(tree->current, tree->top)) > 0) gnt_tree_scroll(tree, -dist); if (old != tree->current) tree_selection_changed(tree, old, tree->current); action_page_down(GntBindable *bind, GList *null) GntTree *tree = GNT_TREE(bind); GntTreeRow *old = tree->current; GntTreeRow *row = get_next(tree->bottom); int dist = get_distance(tree->top, tree->current); tree->top = tree->bottom; tree->current = get_next_n_opt(tree->top, dist, NULL); else if (tree->current != tree->bottom) tree->current = tree->bottom; if (old != tree->current) tree_selection_changed(tree, old, tree->current); action_page_up(GntBindable *bind, GList *null) GntWidget *widget = GNT_WIDGET(bind); GntTree *tree = GNT_TREE(bind); GntTreeRow *old = tree->current; if (tree->top != tree->root) int dist = get_distance(tree->top, tree->current); row = get_prev_n(tree->top, widget->priv.height - 1 - tree->show_title * 2 - 2 * (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER) == 0)); tree->current = get_next_n_opt(tree->top, dist, NULL); else if (tree->current != tree->top) tree->current = tree->top; if (old != tree->current) tree_selection_changed(tree, old, tree->current); end_search(GntTree *tree) if (tree->priv->search) { g_source_remove(tree->priv->search_timeout); g_string_free(tree->priv->search, TRUE); tree->priv->search = NULL; tree->priv->search_timeout = 0; GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(tree), GNT_WIDGET_DISABLE_ACTIONS); search_timeout(gpointer data) gnt_tree_key_pressed(GntWidget *widget, const char *text) GntTree *tree = GNT_TREE(widget); GntTreeRow *old = tree->current; if (text[0] == '\r' || text[0] == '\n') { gnt_widget_activate(widget); } else if (tree->priv->search) { if (g_unichar_isprint(*text)) { tree->priv->search = g_string_append_c(tree->priv->search, *text); } else if (g_utf8_collate(text, GNT_KEY_BACKSPACE) == 0) { if (tree->priv->search->len) tree->priv->search->str[--tree->priv->search->len] = '\0'; gnt_bindable_perform_action_key(GNT_BINDABLE(tree), text); g_source_remove(tree->priv->search_timeout); tree->priv->search_timeout = g_timeout_add_seconds(SEARCH_TIMEOUT_S, search_timeout, tree); } else if (text[0] == ' ' && text[1] == 0) { GntTreeRow *row = tree->current; row->collapsed = !row->collapsed; g_signal_emit(tree, signals[SIG_COLLAPSED], 0, row->key, row->collapsed); else if (row && row->choice) row->isselected = !row->isselected; g_signal_emit(tree, signals[SIG_TOGGLED], 0, row->key); if (old != tree->current) tree_selection_changed(tree, old, tree->current); gnt_tree_free_columns(GntTree *tree) for (i = 0; i < tree->ncol; i++) { g_free(tree->columns[i].title); gnt_tree_destroy(GntWidget *widget) GntTree *tree = GNT_TREE(widget); g_hash_table_destroy(tree->hash); gnt_tree_free_columns(tree); gnt_tree_clicked(GntWidget *widget, GntMouseEvent event, int x, int y) GntTree *tree = GNT_TREE(widget); GntTreeRow *old = tree->current; if (event == GNT_MOUSE_SCROLL_UP) { action_up(GNT_BINDABLE(widget), NULL); } else if (event == GNT_MOUSE_SCROLL_DOWN) { action_down(GNT_BINDABLE(widget), NULL); } else if (event == GNT_LEFT_MOUSE_DOWN) { GntTree *tree = GNT_TREE(widget); if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) pos = y - widget->priv.y - pos; row = get_next_n(tree->top, pos); if (row && tree->current != row) { GntTreeRow *old = tree->current; tree_selection_changed(tree, old, tree->current); } else if (row && row == tree->current) { row->isselected = !row->isselected; g_signal_emit(tree, signals[SIG_TOGGLED], 0, row->key); gnt_widget_activate(widget); if (old != tree->current) { tree_selection_changed(tree, old, tree->current); gnt_tree_size_changed(GntWidget *widget, int w, int h) GntTree *tree = GNT_TREE(widget); if (widget->priv.width <= 0) start_search(GntBindable *bindable, GList *list) GntTree *tree = GNT_TREE(bindable); GNT_WIDGET_SET_FLAGS(GNT_WIDGET(tree), GNT_WIDGET_DISABLE_ACTIONS); tree->priv->search = g_string_new(NULL); tree->priv->search_timeout = g_timeout_add_seconds(SEARCH_TIMEOUT_S, search_timeout, tree); end_search_action(GntBindable *bindable, GList *list) GntTree *tree = GNT_TREE(bindable); if (tree->priv->search == NULL) GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(tree), GNT_WIDGET_DISABLE_ACTIONS); move_first_action(GntBindable *bind, GList *null) GntTree *tree = GNT_TREE(bind); GntTreeRow *row = tree->root; GntTreeRow *old = tree->current; if (row && !row_matches_search(row)) if (old != tree->current) tree_selection_changed(tree, old, tree->current); move_last_action(GntBindable *bind, GList *null) GntTree *tree = GNT_TREE(bind); GntTreeRow *old = tree->current; GntTreeRow *row = tree->bottom; while ((next = get_next(row))) if (old != tree->current) tree_selection_changed(tree, old, tree->current); gnt_tree_set_property(GObject *obj, guint prop_id, const GValue *value, GntTree *tree = GNT_TREE(obj); _gnt_tree_init_internals(tree, g_value_get_int(value)); if (tree->priv->expander_level == g_value_get_int(value)) tree->priv->expander_level = g_value_get_int(value); gnt_tree_get_property(GObject *obj, guint prop_id, GValue *value, GntTree *tree = GNT_TREE(obj); g_value_set_int(value, tree->ncol); g_value_set_int(value, tree->priv->expander_level); gnt_tree_class_init(GntTreeClass *klass) GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass); GObjectClass *gclass = G_OBJECT_CLASS(klass); parent_class = GNT_WIDGET_CLASS(klass); parent_class->destroy = gnt_tree_destroy; parent_class->draw = gnt_tree_draw; parent_class->map = gnt_tree_map; parent_class->size_request = gnt_tree_size_request; parent_class->key_pressed = gnt_tree_key_pressed; parent_class->clicked = gnt_tree_clicked; parent_class->size_changed = gnt_tree_size_changed; gclass->set_property = gnt_tree_set_property; gclass->get_property = gnt_tree_get_property; g_object_class_install_property(gclass, g_param_spec_int("columns", "Columns", "Number of columns in the tree.", G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS g_object_class_install_property(gclass, g_param_spec_int("expander-level", "Expander level", "Number of levels to show expander in the tree.", G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS signals[SIG_SELECTION_CHANGED] = g_signal_new("selection-changed", G_TYPE_FROM_CLASS(klass), G_STRUCT_OFFSET(GntTreeClass, selection_changed), gnt_closure_marshal_VOID__POINTER_POINTER, G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER); G_TYPE_FROM_CLASS(klass), g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT); G_TYPE_FROM_CLASS(klass), G_STRUCT_OFFSET(GntTreeClass, toggled), g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER); g_signal_new("collapse-toggled", G_TYPE_FROM_CLASS(klass), gnt_closure_marshal_VOID__POINTER_BOOLEAN, G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_BOOLEAN); gnt_bindable_class_register_action(bindable, "move-up", action_up, gnt_bindable_register_binding(bindable, "move-up", GNT_KEY_CTRL_P, NULL); gnt_bindable_class_register_action(bindable, "move-down", action_down, gnt_bindable_register_binding(bindable, "move-down", GNT_KEY_CTRL_N, NULL); gnt_bindable_class_register_action(bindable, "move-parent", action_move_parent, GNT_KEY_BACKSPACE, NULL); gnt_bindable_class_register_action(bindable, "page-up", action_page_up, gnt_bindable_class_register_action(bindable, "page-down", action_page_down, gnt_bindable_class_register_action(bindable, "start-search", start_search, gnt_bindable_class_register_action(bindable, "end-search", end_search_action, gnt_bindable_class_register_action(bindable, "move-first", move_first_action, gnt_bindable_class_register_action(bindable, "move-last", move_last_action, gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), bindable); gnt_tree_init(GTypeInstance *instance, gpointer class) GntWidget *widget = GNT_WIDGET(instance); GntTree *tree = GNT_TREE(widget); tree->show_separator = TRUE; tree->priv = g_new0(GntTreePriv, 1); GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X | GNT_WIDGET_GROW_Y | GNT_WIDGET_CAN_TAKE_FOCUS | GNT_WIDGET_NO_SHADOW); gnt_widget_set_take_focus(widget, TRUE); /****************************************************************************** *****************************************************************************/ static const GTypeInfo info = { NULL, /* base_finalize */ (GClassInitFunc)gnt_tree_class_init, NULL, /* class_finalize */ gnt_tree_init, /* instance_init */ type = g_type_register_static(GNT_TYPE_WIDGET, free_tree_col(gpointer data) free_tree_row(gpointer data) g_list_foreach(row->columns, (GFunc)free_tree_col, NULL); g_list_free(row->columns); GntWidget *gnt_tree_new() return gnt_tree_new_with_columns(1); void gnt_tree_set_visible_rows(GntTree *tree, int rows) GntWidget *widget = GNT_WIDGET(tree); widget->priv.height = rows; if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) widget->priv.height += 2; int gnt_tree_get_visible_rows(GntTree *tree) GntWidget *widget = GNT_WIDGET(tree); int ret = widget->priv.height; if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) GList *gnt_tree_get_rows(GntTree *tree) void gnt_tree_scroll(GntTree *tree, int count) if (get_root_distance(tree->top) == 0) row = get_prev_n(tree->top, -count); get_next_n_opt(tree->bottom, count, &count); tree->top = get_next_n(tree->top, count); g_signal_emit(tree, signals[SIG_SCROLLED], 0, count); find_position(GntTree *tree, gpointer key, gpointer parent) if (tree->priv->compare == NULL) row = g_hash_table_lookup(tree->hash, parent); if (tree->priv->compare(key, row->key) < 0) return (row->prev ? row->prev->key : NULL); void gnt_tree_sort_row(GntTree *tree, gpointer key) if (!tree->priv->compare) row = g_hash_table_lookup(tree->hash, key); g_return_if_fail(row != NULL); current = g_list_index(tree->list, key); if (tree->priv->compare(row->key, s->key) < 0) /* Move row between q and s */ if (row == q || row == s) /* row becomes the first child of its parent */ row->prev->next = row->next; /* row->prev cannot be NULL at this point */ row->next->prev = row->prev; row->parent->child = row; g_return_if_fail(s != NULL); /* s cannot be NULL */ newp = g_list_index(tree->list, s) - 1; row->prev->next = row->next; /* row was the first child of its parent */ row->parent->child = row->next; row->next->prev = row->prev; newp = g_list_index(tree->list, q) + 1; tree->list = g_list_reposition_child(tree->list, current, newp); GntTreeRow *gnt_tree_add_row_after(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro) if (g_hash_table_lookup(tree->hash, key)) { gnt_tree_remove(tree, key); g_hash_table_replace(tree->hash, key, row); if (bigbro == NULL && tree->priv->compare) bigbro = find_position(tree, key, parent); tree->list = g_list_prepend(tree->list, key); pr = g_hash_table_lookup(tree->hash, bigbro); if (pr->next) pr->next->prev = row; row->parent = pr->parent; position = g_list_index(tree->list, bigbro); if (pr == NULL && parent) pr = g_hash_table_lookup(tree->hash, parent); if (pr->child) pr->child->prev = row; position = g_list_index(tree->list, parent); GntTreeRow *r = tree->root; if (tree->current == tree->root) tree->list = g_list_prepend(tree->list, key); tree->list = g_list_insert(tree->list, key, position + 1); GntTreeRow *gnt_tree_add_row_last(GntTree *tree, void *key, GntTreeRow *row, void *parent) GntTreeRow *pr = NULL, *br = NULL; pr = g_hash_table_lookup(tree->hash, parent); return gnt_tree_add_row_after(tree, key, row, parent, br ? br->key : NULL); gpointer gnt_tree_get_selection_data(GntTree *tree) return tree->current->key; /* XXX: perhaps we should just get rid of 'data' */ char *gnt_tree_get_selection_text(GntTree *tree) return update_row_text(tree, tree->current); GList *gnt_tree_get_row_text_list(GntTree *tree, gpointer key) GList *list = NULL, *iter; GntTreeRow *row = key ? g_hash_table_lookup(tree->hash, key) : tree->current; for (i = 0, iter = row->columns; i < tree->ncol && iter; GntTreeCol *col = iter->data; list = g_list_append(list, BINARY_DATA(tree, i) ? col->text : g_strdup(col->text)); GList *gnt_tree_get_selection_text_list(GntTree *tree) return gnt_tree_get_row_text_list(tree, NULL); void gnt_tree_remove(GntTree *tree, gpointer key) GntTreeRow *row = g_hash_table_lookup(tree->hash, key); static int depth = 0; /* Only redraw after all child nodes are removed */ gnt_tree_remove(tree, row->child->key); if (get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0) /* Update root/top/current/bottom if necessary */ tree->root = get_next(row); if (tree->top != tree->root) tree->top = get_prev(row); tree->top = get_next(row); if (tree->current == row) if (tree->current != tree->root) tree->current = get_prev(row); tree->current = get_next(row); tree_selection_changed(tree, row, tree->current); tree->bottom = get_prev(row); row->next->prev = row->prev; if (row->parent && row->parent->child == row) row->parent->child = row->next; row->prev->next = row->next; g_hash_table_remove(tree->hash, key); tree->list = g_list_remove(tree->list, key); if (redraw && depth == 0) return_true(gpointer key, gpointer data, gpointer null) void gnt_tree_remove_all(GntTree *tree) g_hash_table_foreach_remove(tree->hash, (GHRFunc)return_true, tree); tree->current = tree->top = tree->bottom = NULL; int gnt_tree_get_selection_visible_line(GntTree *tree) return get_distance(tree->top, tree->current) + !!(GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER)); void gnt_tree_change_text(GntTree *tree, gpointer key, int colno, const char *text) g_return_if_fail(colno < tree->ncol); row = g_hash_table_lookup(tree->hash, key); col = g_list_nth_data(row->columns, colno); if (BINARY_DATA(tree, colno)) { col->text = (gpointer)text; col->text = g_strdup(text ? text : ""); if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED) && get_distance(tree->top, row) >= 0 && get_distance(row, tree->bottom) >= 0) GntTreeRow *gnt_tree_add_choice(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro) r = g_hash_table_lookup(tree->hash, key); g_return_val_if_fail(!r || !r->choice, NULL); bigbro = find_position(tree, key, parent); r = g_hash_table_lookup(tree->hash, parent); row = gnt_tree_add_row_after(tree, key, row, parent, bigbro); void gnt_tree_set_choice(GntTree *tree, void *key, gboolean set) GntTreeRow *row = g_hash_table_lookup(tree->hash, key); g_return_if_fail(row->choice); gboolean gnt_tree_get_choice(GntTree *tree, void *key) GntTreeRow *row = g_hash_table_lookup(tree->hash, key); g_return_val_if_fail(row->choice, FALSE); void gnt_tree_set_row_flags(GntTree *tree, void *key, GntTextFormatFlags flags) GntTreeRow *row = g_hash_table_lookup(tree->hash, key); if (!row || row->flags == flags) redraw_tree(tree); /* XXX: It shouldn't be necessary to redraw the whole darned tree */ void gnt_tree_set_row_color(GntTree *tree, void *key, int color) GntTreeRow *row = g_hash_table_lookup(tree->hash, key); if (!row || row->color == color) void gnt_tree_set_selected(GntTree *tree , void *key) GntTreeRow *row = g_hash_table_lookup(tree->hash, key); if (!row || row == tree->current) if (tree->bottom == NULL) if ((dist = get_distance(tree->current, tree->bottom)) < 0) gnt_tree_scroll(tree, -dist); else if ((dist = get_distance(tree->current, tree->top)) > 0) gnt_tree_scroll(tree, -dist); tree_selection_changed(tree, row, tree->current); static void _gnt_tree_init_internals(GntTree *tree, int col) gnt_tree_free_columns(tree); tree->hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_tree_row); tree->columns = g_new0(struct _GntTreeColInfo, col); tree->priv->lastvisible = col - 1; tree->columns[col].width = 15; tree->show_title = FALSE; g_object_notify(G_OBJECT(tree), "columns"); GntWidget *gnt_tree_new_with_columns(int col) GntWidget *widget = g_object_new(GNT_TYPE_TREE, GntTreeRow *gnt_tree_create_row_from_list(GntTree *tree, GList *list) GntTreeRow *row = g_new0(GntTreeRow, 1); for (i = 0, iter = list; i < tree->ncol && iter; iter = iter->next, i++) GntTreeCol *col = g_new0(GntTreeCol, 1); if (BINARY_DATA(tree, i)) { col->text = g_strdup(iter->data ? iter->data : ""); row->columns = g_list_append(row->columns, col); GntTreeRow *gnt_tree_create_row(GntTree *tree, ...) for (i = 0; i < tree->ncol; i++) list = g_list_append(list, va_arg(args, char *)); row = gnt_tree_create_row_from_list(tree, list); void gnt_tree_set_col_width(GntTree *tree, int col, int width) g_return_if_fail(col < tree->ncol); tree->columns[col].width = width; if (tree->columns[col].width_ratio == 0) tree->columns[col].width_ratio = width; void gnt_tree_set_column_title(GntTree *tree, int index, const char *title) g_free(tree->columns[index].title); tree->columns[index].title = g_strdup(title); void gnt_tree_set_column_titles(GntTree *tree, ...) for (i = 0; i < tree->ncol; i++) const char *title = va_arg(args, const char *); tree->columns[i].title = g_strdup(title); void gnt_tree_set_show_title(GntTree *tree, gboolean set) GNT_WIDGET(tree)->priv.minh = (set ? 6 : 4); void gnt_tree_set_compare_func(GntTree *tree, GCompareFunc func) tree->priv->compare = func; void gnt_tree_set_expanded(GntTree *tree, void *key, gboolean expanded) GntTreeRow *row = g_hash_table_lookup(tree->hash, key); row->collapsed = !expanded; if (GNT_WIDGET(tree)->window) gnt_widget_draw(GNT_WIDGET(tree)); g_signal_emit(tree, signals[SIG_COLLAPSED], 0, key, row->collapsed); void gnt_tree_set_show_separator(GntTree *tree, gboolean set) tree->show_separator = set; void gnt_tree_adjust_columns(GntTree *tree) GntTreeRow *row = tree->root; widths = g_new0(int, tree->ncol); for (i = 0, iter = row->columns; iter; iter = iter->next, i++) { GntTreeCol *col = iter->data; int w = gnt_util_onscreen_width(col->text, NULL); if (i == 0 && row->choice) w += find_depth(row) * TAB_SIZE; twidth = 1 + 2 * (!GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_NO_BORDER)); for (i = 0; i < tree->ncol; i++) { if (tree->columns[i].flags & GNT_TREE_COLUMN_FIXED_SIZE) widths[i] = tree->columns[i].width; gnt_tree_set_col_width(tree, i, widths[i]); if (!COLUMN_INVISIBLE(tree, i)) { twidth = twidth + widths[i]; if (tree->priv->lastvisible != i) gnt_widget_set_size(GNT_WIDGET(tree), twidth, -1); void gnt_tree_set_hash_fns(GntTree *tree, gpointer hash, gpointer eq, gpointer kd) g_hash_table_foreach_remove(tree->hash, return_true, NULL); g_hash_table_destroy(tree->hash); tree->hash = g_hash_table_new_full(hash, eq, kd, free_tree_row); set_column_flag(GntTree *tree, int col, GntTreeColumnFlag flag, gboolean set) tree->columns[col].flags |= flag; tree->columns[col].flags &= ~flag; void gnt_tree_set_column_visible(GntTree *tree, int col, gboolean vis) g_return_if_fail(col < tree->ncol); set_column_flag(tree, col, GNT_TREE_COLUMN_INVISIBLE, !vis); /* the column is visible */ if (tree->priv->lastvisible < col) tree->priv->lastvisible = col; if (tree->priv->lastvisible == col) while (tree->priv->lastvisible) { tree->priv->lastvisible--; if (!COLUMN_INVISIBLE(tree, tree->priv->lastvisible)) if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(tree), GNT_WIDGET_MAPPED)) void gnt_tree_set_column_resizable(GntTree *tree, int col, gboolean res) g_return_if_fail(col < tree->ncol); set_column_flag(tree, col, GNT_TREE_COLUMN_FIXED_SIZE, !res); void gnt_tree_set_column_is_binary(GntTree *tree, int col, gboolean bin) g_return_if_fail(col < tree->ncol); set_column_flag(tree, col, GNT_TREE_COLUMN_BINARY_DATA, bin); void gnt_tree_set_column_is_right_aligned(GntTree *tree, int col, gboolean right) g_return_if_fail(col < tree->ncol); set_column_flag(tree, col, GNT_TREE_COLUMN_RIGHT_ALIGNED, right); void gnt_tree_set_column_width_ratio(GntTree *tree, int cols[]) for (i = 0; i < tree->ncol && cols[i]; i++) { tree->columns[i].width_ratio = cols[i]; void gnt_tree_set_search_column(GntTree *tree, int col) g_return_if_fail(col < tree->ncol); g_return_if_fail(!BINARY_DATA(tree, col)); tree->priv->search_column = col; gboolean gnt_tree_is_searching(GntTree *tree) return (tree->priv->search != NULL); void gnt_tree_set_search_function(GntTree *tree, gboolean (*func)(GntTree *tree, gpointer key, const char *search, const char *current)) tree->priv->search_func = func; gpointer gnt_tree_get_parent_key(GntTree *tree, gpointer key) GntTreeRow *row = g_hash_table_lookup(tree->hash, key); return (row && row->parent) ? row->parent->key : NULL; gpointer gnt_tree_row_get_key(GntTree *tree, GntTreeRow *row) g_return_val_if_fail(row && row->tree == tree, NULL); GntTreeRow * gnt_tree_row_get_next(GntTree *tree, GntTreeRow *row) g_return_val_if_fail(row && row->tree == tree, NULL); GntTreeRow * gnt_tree_row_get_prev(GntTree *tree, GntTreeRow *row) g_return_val_if_fail(row && row->tree == tree, NULL); GntTreeRow * gnt_tree_row_get_child(GntTree *tree, GntTreeRow *row) g_return_val_if_fail(row && row->tree == tree, NULL); GntTreeRow * gnt_tree_row_get_parent(GntTree *tree, GntTreeRow *row) g_return_val_if_fail(row && row->tree == tree, NULL); /************************************************************************** **************************************************************************/ gnt_tree_row_ref(GntTreeRow *row) g_return_val_if_fail(row != NULL, NULL); gnt_tree_row_unref(GntTreeRow *row) g_return_if_fail(row != NULL); g_return_if_fail(row->box_count >= 0); gnt_tree_row_get_type(void) type = g_boxed_type_register_static("GntTreeRow", (GBoxedCopyFunc)gnt_tree_row_ref, (GBoxedFreeFunc)gnt_tree_row_unref);