--- a/gnttree.c Tue Apr 23 20:44:47 2019 +0000
+++ b/gnttree.c Sat Apr 20 21:11:56 2019 -0400
@@ -29,7 +29,7 @@
#define SEARCH_TIMEOUT_S 4 /* 4 secs */
-#define SEARCHING(tree) (tree->priv->search && tree->priv->search->len > 0)
+#define SEARCHING(priv) (priv->search && 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)
@@ -51,7 +51,7 @@
@@ -61,7 +61,7 @@
@@ -102,11 +102,12 @@
static guint signals[SIGS] = { 0 };
-G_DEFINE_TYPE(GntTree, gnt_tree, GNT_TYPE_WIDGET)
+G_DEFINE_TYPE_WITH_PRIVATE(GntTree, gnt_tree, GNT_TYPE_WIDGET) readjust_columns(GntTree *tree)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); #define WIDTH(i) (tree->columns[i].width_ratio ? tree->columns[i].width_ratio : tree->columns[i].width)
@@ -119,9 +120,9 @@
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);
+ width -= WIDTH(i) + (priv->lastvisible != i); - total += WIDTH(i) + (tree->priv->lastvisible != i);
+ total += WIDTH(i) + (priv->lastvisible != i); @@ -165,14 +166,25 @@
row_matches_search(GntTreeRow *row)
- GntTree *t = row->tree;
- 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;
+ GntTree *tree = row->tree; + GntTreePrivate *priv = gnt_tree_get_instance_private(tree); - if (t->priv->search_func)
- return t->priv->search_func(t, row->key, t->priv->search->str, col->text);
+ col = g_list_nth_data(row->columns, priv->search_column); + col = row->columns->data; + if (priv->search_func) { + return priv->search_func(tree, row->key, + priv->search->str, col->text); one = g_utf8_casefold(col->text, -1);
- two = g_utf8_casefold(t->priv->search->str, -1);
+ two = g_utf8_casefold(priv->search->str, -1); @@ -310,6 +322,7 @@
update_row_text(GntTree *tree, GntTreeRow *row)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); GString *string = g_string_new(NULL);
@@ -344,9 +357,8 @@
g_string_append_printf(string, "[%c] ",
row->isselected ? 'X' : ' ');
- else if (find_depth(row) < tree->priv->expander_level && row->child)
+ } else if (find_depth(row) < priv->expander_level && string = g_string_append(string, "+ ");
@@ -356,9 +368,7 @@
string = g_string_append(string, "- ");
fl = TAB_SIZE * find_depth(row);
g_string_append_printf(string, "%*s", fl, "");
@@ -419,6 +429,7 @@
redraw_tree(GntTree *tree)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); GntWidget *widget = GNT_WIDGET(tree);
@@ -610,11 +621,12 @@
(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);
+ const char *str = gnt_util_onscreen_width_to_pointer( + 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);
+ priv->search->str, str - priv->search->str); wmove(widget->window, current, pos);
@@ -639,14 +651,17 @@
if (widget->priv.width == 0)
GntTree *tree = GNT_TREE(widget);
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); width = gnt_widget_get_has_border(GNT_WIDGET(tree)) ? 3 : 1;
- for (i = 0; i < tree->ncol; i++)
+ for (i = 0; i < tree->ncol; i++) { if (!COLUMN_INVISIBLE(tree, i)) {
width = width + tree->columns[i].width;
- if (tree->priv->lastvisible != i)
+ if (priv->lastvisible != i) { widget->priv.width = width;
@@ -694,11 +709,13 @@
action_move_parent(GntBindable *bind, G_GNUC_UNUSED GList *unused)
GntTree *tree = GNT_TREE(bind);
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); GntTreeRow *row = tree->current;
- if (!row || !row->parent || SEARCHING(tree))
+ if (!row || !row->parent || SEARCHING(priv)) { tree->current = row->parent;
if ((dist = get_distance(tree->current, tree->top)) > 0)
@@ -787,11 +804,12 @@
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;
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); + g_source_remove(priv->search_timeout); + g_string_free(priv->search, TRUE); + priv->search_timeout = 0; gnt_widget_set_disable_actions(GNT_WIDGET(tree), FALSE);
@@ -811,18 +829,20 @@
gnt_tree_key_pressed(GntWidget *widget, const char *text)
GntTree *tree = GNT_TREE(widget);
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); GntTreeRow *old = tree->current;
if (text[0] == '\r' || text[0] == '\n') {
gnt_widget_activate(widget);
- } else if (tree->priv->search) {
+ } else if (priv->search) { if (g_unichar_isprint(*text)) {
- tree->priv->search = g_string_append_c(tree->priv->search, *text);
+ priv->search = g_string_append_c(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';
+ if (priv->search->len) { + priv->search->str[--priv->search->len] = '\0'; @@ -830,8 +850,9 @@
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);
+ g_source_remove(priv->search_timeout); + priv->search_timeout = g_timeout_add_seconds( + SEARCH_TIMEOUT_S, search_timeout, tree); } else if (text[0] == ' ' && text[1] == 0) {
@@ -880,7 +901,6 @@
g_hash_table_destroy(tree->hash);
gnt_tree_free_columns(tree);
@@ -939,11 +959,16 @@
start_search(GntBindable *bindable, G_GNUC_UNUSED GList *params)
GntTree *tree = GNT_TREE(bindable);
- if (tree->priv->search)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); gnt_widget_set_disable_actions(GNT_WIDGET(tree), TRUE);
- tree->priv->search = g_string_new(NULL);
- tree->priv->search_timeout = g_timeout_add_seconds(SEARCH_TIMEOUT_S, search_timeout, tree);
+ priv->search = g_string_new(NULL); + g_timeout_add_seconds(SEARCH_TIMEOUT_S, search_timeout, tree); @@ -951,8 +976,12 @@
end_search_action(GntBindable *bindable, G_GNUC_UNUSED GList *params)
GntTree *tree = GNT_TREE(bindable);
- if (tree->priv->search == NULL)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); + if (priv->search == NULL) { gnt_widget_set_disable_actions(GNT_WIDGET(tree), FALSE);
@@ -1003,14 +1032,17 @@
G_GNUC_UNUSED GParamSpec *spec)
GntTree *tree = GNT_TREE(obj);
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); _gnt_tree_init_internals(tree, g_value_get_int(value));
- if (tree->priv->expander_level == g_value_get_int(value))
+ if (priv->expander_level == g_value_get_int(value)) { - tree->priv->expander_level = g_value_get_int(value);
+ priv->expander_level = g_value_get_int(value); @@ -1021,12 +1053,14 @@
G_GNUC_UNUSED GParamSpec *spec)
GntTree *tree = GNT_TREE(obj);
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); g_value_set_int(value, tree->ncol);
- g_value_set_int(value, tree->priv->expander_level);
+ g_value_set_int(value, priv->expander_level); @@ -1127,7 +1161,6 @@
GntWidget *widget = GNT_WIDGET(tree);
tree->show_separator = TRUE;
- tree->priv = g_new0(GntTreePriv, 1);
gnt_widget_set_grow_x(widget, TRUE);
gnt_widget_set_grow_y(widget, TRUE);
@@ -1217,10 +1250,12 @@
find_position(GntTree *tree, gpointer key, gpointer parent)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); - if (tree->priv->compare == NULL)
+ if (priv->compare == NULL) { @@ -1235,8 +1270,9 @@
- if (tree->priv->compare(key, row->key) < 0)
+ if (priv->compare(key, row->key) < 0) { return (row->prev ? row->prev->key : NULL);
@@ -1247,11 +1283,13 @@
void gnt_tree_sort_row(GntTree *tree, gpointer key)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); - if (!tree->priv->compare)
row = g_hash_table_lookup(tree->hash, key);
g_return_if_fail(row != NULL);
@@ -1265,8 +1303,9 @@
- if (tree->priv->compare(row->key, s->key) < 0)
+ if (priv->compare(row->key, s->key) < 0) { @@ -1317,6 +1356,7 @@
GntTreeRow *gnt_tree_add_row_after(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); if (g_hash_table_lookup(tree->hash, key)) {
@@ -1328,8 +1368,7 @@
g_hash_table_replace(tree->hash, key, row);
- if (bigbro == NULL && tree->priv->compare)
+ if (bigbro == NULL && priv->compare) { bigbro = find_position(tree, key, parent);
@@ -1553,14 +1592,15 @@
GntTreeRow *gnt_tree_add_choice(GntTree *tree, void *key, GntTreeRow *row, void *parent, void *bigbro)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); r = g_hash_table_lookup(tree->hash, key);
g_return_val_if_fail(!r || !r->choice, NULL);
- if (tree->priv->compare)
bigbro = find_position(tree, key, parent);
r = g_hash_table_lookup(tree->hash, parent);
@@ -1646,12 +1686,13 @@
static void _gnt_tree_init_internals(GntTree *tree, int col)
+ GntTreePrivate *priv = gnt_tree_get_instance_private(tree); 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;
+ priv->lastvisible = col - 1; tree->columns[col].width = 15;
@@ -1752,7 +1793,12 @@
void gnt_tree_set_compare_func(GntTree *tree, GCompareFunc func)
- tree->priv->compare = func;
+ GntTreePrivate *priv = NULL; + g_return_if_fail(GNT_IS_TREE(tree)); + priv = gnt_tree_get_instance_private(tree); void gnt_tree_set_expanded(GntTree *tree, void *key, gboolean expanded)
@@ -1773,10 +1819,15 @@
void gnt_tree_adjust_columns(GntTree *tree)
- GntTreeRow *row = tree->root;
+ GntTreePrivate *priv = NULL; + g_return_if_fail(GNT_IS_TREE(tree)); + priv = gnt_tree_get_instance_private(tree); widths = g_new0(int, tree->ncol);
for (i = 0, iter = row->columns; iter; iter = iter->next, i++) {
@@ -1800,8 +1851,9 @@
gnt_tree_set_col_width(tree, i, widths[i]);
if (!COLUMN_INVISIBLE(tree, i)) {
twidth = twidth + widths[i];
- if (tree->priv->lastvisible != i)
+ if (priv->lastvisible != i) { @@ -1826,19 +1878,29 @@
void gnt_tree_set_column_visible(GntTree *tree, int col, gboolean vis)
+ GntTreePrivate *priv = NULL; + g_return_if_fail(GNT_IS_TREE(tree)); g_return_if_fail(col < tree->ncol);
+ priv = gnt_tree_get_instance_private(tree); set_column_flag(tree, col, GNT_TREE_COLUMN_INVISIBLE, !vis);
/* the column is visible */
- if (tree->priv->lastvisible < col)
- tree->priv->lastvisible = col;
+ if (priv->lastvisible < col) { + priv->lastvisible = col; - if (tree->priv->lastvisible == col)
- while (tree->priv->lastvisible) {
- tree->priv->lastvisible--;
- if (!COLUMN_INVISIBLE(tree, tree->priv->lastvisible))
+ if (priv->lastvisible == col) { + while (priv->lastvisible) { + if (!COLUMN_INVISIBLE(tree, if (gnt_widget_get_mapped(GNT_WIDGET(tree)))
@@ -1872,20 +1934,36 @@
void gnt_tree_set_search_column(GntTree *tree, int col)
+ GntTreePrivate *priv = NULL; + g_return_if_fail(GNT_IS_TREE(tree)); g_return_if_fail(col < tree->ncol);
g_return_if_fail(!BINARY_DATA(tree, col));
- tree->priv->search_column = col;
+ priv = gnt_tree_get_instance_private(tree); + priv->search_column = col; gboolean gnt_tree_is_searching(GntTree *tree)
- return (tree->priv->search != NULL);
+ GntTreePrivate *priv = NULL; + g_return_val_if_fail(GNT_IS_TREE(tree), FALSE); + priv = gnt_tree_get_instance_private(tree); + return (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;
+ GntTreePrivate *priv = NULL; + g_return_if_fail(GNT_IS_TREE(tree)); + priv = gnt_tree_get_instance_private(tree); + priv->search_func = func; gpointer gnt_tree_get_parent_key(GntTree *tree, gpointer key)