Re: Appending to treestore - how to set selection on new entry?



On 08/27/2018 01:24 AM, David C. Rankin wrote:
  "How do I use the pointer stored in column 2 of the model to set the
selection on that treeview entry?"

(the full source of the interface is https://github.com/drankinatty/gtkate
which is just a concept at the moment, the project will provide the
functionality of https://github.com/drankinatty/gtkwrite within a
multi-document/multi-view interface when done)

  I have not worked with tree models/stores a great deal and I am a bit
bewildered by how to get an iter or treepath given the value of column 2 to
then set the selection on that row/entry in the treeview. I guess I could do a
for_each and loop over the pointers, but I'm unsure if that is the correct way
or one of my logical hacks that would be better implemented some other way.


Well,

  I'm not sure this was the best way to go about it, but from the callback
from the "focus-in-event" signal from the textview, I call the following which
uses the pointer to the textview buffer to compare for equality against the
pointer held in each treemodel instance pointer. This gives me the GtkTreeIter
that I can then use with gtk_tree_selection_select_iter(), e.g.

/** given inst use tree iter to set focus on current view
 *  (widget is textview associated with window clicked inside)
 */
void tree_get_inst_iter (GtkWidget *widget, gpointer data)
{
    mainwin_t *app = data;
    GtkTreeModel *model = NULL;
    GtkTreeSelection *selection;
    GtkTreeIter iter;
    gpointer buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW(widget));
    gboolean valid, found = FALSE;

    /* initialize selection to current */
    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(app->treeview));

    /* get treemodel, validate */
    model = gtk_tree_view_get_model(GTK_TREE_VIEW(app->treeview));
    if (!model) {
        g_print ("error: tree_view_get_model - failed.\n");
        return;
    }

    /* get first iter, return in validate */
    valid = gtk_tree_model_get_iter_first (model, &iter);
    while (valid) {         /* loop over each entry in model */
        gchar *str = NULL;
        kinst_t *inst = NULL;

        /* TODO - remove COLNAME & str after debugging done */
        gtk_tree_model_get (model, &iter,   /* get name & inst */
                            COLNAME, &str, COLINST, &inst, -1);

        /* compare pointer to sourceview with buf from textview (widget) */
        if ((gpointer)(inst->buf) == buf) {
            gtk_tree_selection_select_iter (selection, &iter);
            g_free (str);
            found = TRUE;
            break;
        }
        g_free (str);

        valid = gtk_tree_model_iter_next (model, &iter);
    }

    if (!found)
        g_warning ("tree_get_inst_iter inst not found.");

    if (widget) {}
}


-- 
David C. Rankin, J.D.,P.E.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]