Re: GtkTextView scroll preservation

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

 



Thanks. Here's how I interpreted your idea. But unfortunately it still doesn't work.

//======================================================================
//  scroll-test.c - Test scrolling
//
// compile with: gcc -o scroll-test scroll-test.c `pkg-config --cflags --libs gtk+-2.0`
//----------------------------------------------------------------------

#include <gtk/gtk.h>

GtkTextBuffer *buf;
GtkWidget *tv, *sw;
double preserve_pos;
GtkAdjustment *vadj;

void on_insert_text(GtkTextBuffer *textbuffer,
                    GtkTextIter   *location,
                    gchar         *text,
                    gint           len,
                    gpointer       user_data)
{
    // Reinstall the vadj position
    gtk_adjustment_set_value(vadj, preserve_pos);
}

/**
 * This function should insert text into tv but preserve the
 * display of the first line in the buffer.
 *
 */
void insert_text()
{
    int i;

    // Remember the current position of vadj and reinstall it in
    // on_insert_text
    preserve_pos = gtk_adjustment_get_value(vadj);

    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();
}

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);
    vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(sw));
    gtk_box_pack_start(GTK_BOX(box),sw,TRUE,TRUE,0);
    tv = gtk_text_view_new();
    buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
    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), NULL);
    g_signal_connect_after(buf, "insert-text",
                           G_CALLBACK(on_insert_text), NULL);

    gtk_widget_show_all(top);
    insert_text();

    gtk_main();
}


On Fri, Jan 15, 2010 at 11:43, Tadej Borovšak <tadeboro@xxxxxxxxx> wrote:
Hello.

Actual text insertion happens in default handler for
GtkTextBuffer::insert-text signal. You may be able to set adjustment
value from signal handler, connected to insert-text after the default
handler (at the moment your function will be called, sizes should be
already updated).

Tadej


--
Tadej Borovšak
tadeboro.blogspot.com
tadeboro@xxxxxxxxx
tadej.borovsak@xxxxxxxxx

_______________________________________________
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