Re: [gtk-list] auto scrolling in text widget




> i am writing a gtk program in which i have a text box that i send text
to. i was
>  wondering, is there a way to make the text box automatically scroll
down with t
> he inserted text, while allowing the user to scroll up to see previous 
stuff in 
> the text widget?

I have the same problem. One way could be:

{
    GtkAdjustment *adj = GTK_TEXT(text)->vadj;
    
    /* Before appending text, check whether the user has
     * scrolled up: 
     */

    if(adj->value < (adj->upper - adj->page_size))
        adj = NULL; /* widget is scrolled up. */

    gtk_text_freeze(text);
    gtk_text_insert(text, ... );
    gtk_text_thaw(text);

    /* This will scroll down the text after appending,
     * but only if the widget isn't scrolled up. 
     */
    if(adj)
         gtk_adjustment_set_value(adj, adj->upper - adj->page_size);
}


Unfortunetly you have to call gtk_text_freeze() / gtk_text_thaw()
which causes a complete redraw and flickering every time you append
text.

Here is another way:

{
    GtkAdjustment *adj = GTK_TEXT(port->text)->vadj;
    int scrolled_up = FALSE;

    if(adj->value < adj->upper - adj->page_size)
    {
	scrolled_up = TRUE;
	gtk_text_freeze(GTK_TEXT(port->text));
    }

    gtk_text_insert(text, ...);

    if(scrolled_up)
	gtk_text_thaw(GTK_TEXT(port->text));
    else
	if(adj->value < adj->upper - adj->page_size)
	    gtk_adjustment_set_value(adj, adj->upper - adj->page_size);
}

This way avoids flickering at least for the (hopefully) usual case
that the widget is not scrolled up.


Anyway, it would be great if the GtkText widget would offer more support
for this problem.


ciao
 Matthias




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