GtkTextView scroll preservation

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

 



I'm using a GtkTextView to display various texts that are changing but have the same length.  The problem is that I would like to preserve the scroll position when changing the text, but so far I haven't managed to do that. Here is a test program:

#include <gtk/gtk.h>

GtkWidget *tv, *sw;

/**
 * This function should insert text into tv but preserve the
 * display of the first line in the buffer.
 *
 */
void insert_text()
{
    int i;
    GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(sw));
    GString *s = g_string_new("");
    for (i=0; i<5000; i++)
        g_string_append_printf(s, "Line %d\n", i);
    GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
    gtk_text_buffer_set_text(buf, s->str,-1);
    g_string_free(s,TRUE);
}

void on_clicked(GtkWidget *button,
                gpointer *userdata)
{
    insert_text(GTK_WIDGET(userdata));
}

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

    GtkWidget *top = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    GtkWidget *box = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(top), box);
    sw = gtk_scrolled_window_new(NULL,NULL);
    gtk_box_pack_start(GTK_BOX(box),sw,TRUE,TRUE,0);
    tv = gtk_text_view_new();
    gtk_container_add(GTK_CONTAINER(sw), tv);
    gtk_widget_set_size_request(sw, 400, 300);
    GtkWidget *btn = gtk_button_new_with_label("Insert text");
    gtk_box_pack_start(GTK_BOX(box),btn,TRUE,TRUE,0);
    g_signal_connect(btn, "clicked", G_CALLBACK(on_clicked), tv);

    gtk_widget_show_all(top);
    insert_text();

    gtk_main();
}

(Please excuse my use of global variables, but it is the shortest way of illustrating the problem). The problem is how to get insert_text to save the position of  the vertical GtkAdjustment of the scrolled window.

I tried getting the value of the vadj before ...set_text() and then setting it to the same value,  but it didn't work. Presumably because the line heights haven't been calculated yet.

So how can I get this to work?

Thanks!
Dov

_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux