gtk_tree_view_scroll_to_cell scrolls to the wrong cell.




This is not the first time I've posted this question to the list, and and
much as I hate send it out again, I just have to.  I haven't heard any
responses and this problem is so critical and obvious in my app that I
really need some help.  This time I've attached some compilable code to make
it easier to show the problem.

First, I populate a GtkTreeStore with a some rows.  Then, I iterate through
the store, looking for an arbitrary row.  When it finds it, it scrolls to
and selects it.  It works like a charm for low-numbered rows.  For
high-numbered rows the correct row is selected, but then scroll doesn't work
correctly.

The strange thing is that when you run the program, you see the treeview
scroll to the correct row.  It then pauses there for a while and then
scrolls back down to some random row.

I'll list the code below for those who don't want to deal with attachments.

Thanks in advance!

----------------------------------------------------------------------------
----------------

#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>


enum {
        TITLE_COL,
        NUM_COLUMNS
};

gint delete_event( GtkWidget *widget,
                    GdkEvent  *event,
                    gpointer   data )
{
    gtk_main_quit ();
    return TRUE;
}


void
scroll(GtkWidget * tree, char * searchtext) {

   GtkTreeModel *store = gtk_tree_view_get_model(GTK_TREE_VIEW(tree));
   GtkTreeIter iter;
   gboolean valid;
   char *rowtext;

   valid = gtk_tree_model_get_iter_first (store, &iter);

   while (valid) {

       gtk_tree_model_get (store, &iter, TITLE_COL, &rowtext, -1);
       if (rowtext) {
            if (strcmp (rowtext, searchtext) == 0) {
                   GtkTreePath *treepath = gtk_tree_model_get_path (store,
&iter);
                   gtk_tree_view_set_cursor (GTK_TREE_VIEW(tree), treepath,
NULL, FALSE);
                   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW(tree),
treepath, 0, TRUE, 0.3, 0);
                   gtk_tree_path_free(treepath);
                   return;
            }
       }
        valid = gtk_tree_model_iter_next(store, &iter);
   }
}


int main( int   argc, char *argv[] )
{

GtkWidget *window;
GtkTreeStore *store;

GtkTreeSelection *select;

GtkCellRenderer *text_renderer;
GtkCellRenderer *pixbuf_renderer;
GtkTreeViewColumn *column;

GdkPixbuf *buf;
int i = 1;

GtkWidget *tree;
gtk_init (&argc, &argv);

/*Set up the toplevel window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
GtkWidget * tree_scroll;
gtk_window_set_title (GTK_WINDOW (window), "scroll_to!");
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);


/*Set up the scrolled window */
tree_scroll = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_ref (tree_scroll);
gtk_container_add (GTK_CONTAINER (window), tree_scroll);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (tree_scroll),
        GTK_POLICY_NEVER, GTK_POLICY_NEVER);


/*make and populate the tree store*/
store = gtk_tree_store_new (NUM_COLUMNS,  G_TYPE_STRING);
GtkTreeIter iter;

/*define the treeview*/
tree = gtk_tree_view_new_with_model( GTK_TREE_MODEL (store));
select = gtk_tree_view_get_selection (GTK_TREE_VIEW(tree));
gtk_tree_selection_set_mode (select, GTK_SELECTION_MULTIPLE);

text_renderer = gtk_cell_renderer_text_new();

column = gtk_tree_view_column_new();

gtk_tree_view_column_pack_end(column, text_renderer, TRUE);

gtk_tree_view_column_add_attribute (column, text_renderer, "text",
TITLE_COL);

gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
gtk_container_add (GTK_CONTAINER (tree_scroll), tree);
gtk_widget_set_size_request (tree, 100, 300);

gtk_widget_show(tree);
gtk_widget_show (tree_scroll);
gtk_widget_show (window);

i=0;
while (i < 150) {

    gchar *rowname = g_strdup_printf("Row %i", i);
    gtk_tree_store_append (store, &iter, NULL);
    gtk_tree_store_set (store, &iter, TITLE_COL, rowname,  -1);
    i++;
}
scroll( tree, "Row 95");

gtk_main ();

return 0;
}

Attachment: scroll.c
Description: Binary data



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