# Scrolledwindow: scrolling automatically to the bottom

**URL:** <https://www.ruby-forum.com/t/scrolledwindow-scrolling-automatically-to-the-bottom/110681>\
**Category:** Ruby-Gnome 2\
**Created:** [July 29, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/scrolledwindow-scrolling-automatically-to-the-bottom/110681 "2007-07-29T15:50:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![giorgian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@giorgian](https://www.ruby-forum.com/u/giorgian)\
**Post date:** [July 29, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/scrolledwindow-scrolling-automatically-to-the-bottom/110681/1 "2007-07-29T15:50:40Z")

</div>

hi,  
i have a textview inside a scrolledwindow; the textview is not editable,  
and only the scripts adds text to it.

when new text is added, i want to scroll to the bottom, so that the new  
text is always visible.

as you can see, i tried:  
scroll.vadjustment.value = scroll.vadjustment.upper -  
scroll.vadjustment.page\_size

this doesn’t work: the window scrolls, but the more recent part is  
always below the window.

i believe that the value of scroll.vadjustment is not updated  
immediately…

how can i solve this?

thanks

```
@edialog = Gtk::Window.new
@edialog.signal_connect("delete-event") do
  @edialog.hide
  true
end
vbox = Gtk::VBox.new
@edialog.add(vbox)
@ebuffer = Gtk::TextBuffer.new
@ebuffer.create_tag("query", "family" => "monospace", "weight" =>

```

Pango::FontDescription::WEIGHT\_BOLD)  
@ebuffer.create\_tag(“result”, “family” =\> “monospace”, “wrap\_mode”  
=\> Gtk::TextTag::WRAP\_WORD)  
@ebuffer.create\_tag(“backtrace”, “family” =\> “monospace”, “weight”  
=\> Pango::FontDescription::WEIGHT\_BOLD, “foreground” =\> “blue”)

```
textview = Gtk::TextView.new(@ebuffer)
textview.set_editable false
textview.cursor_visible = false
scroll = Gtk::ScrolledWindow.new
scroll.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
scroll.add(textview)
vbox.pack_start(scroll)
@eentry = Gtk::Entry.new
@ehistory = []
@ecurrent = 0
@eentry.signal_connect("key_press_event") do |widget, event|
  if event.state.control_mask? and event.keyval ==

```

Gdk::Keyval::GDK\_p  
@ecurrent = (@ecurrent + @ehistory.length - 1) %  
@ehistory.length  
@eentry.text = @ehistory[@ecurrent]  
true  
elsif event.state.control\_mask? and event.keyval ==  
Gdk::Keyval::GDK\_n  
@ecurrent = (@ecurrent + @ehistory.length + 1) %  
@ehistory.length  
@eentry.text = @ehistory[@ecurrent]  
true  
end  
end  
vbox.pack\_start(@eentry, false, true, 0)  
here = binding  
@eentry.signal\_connect(“activate”) do  
begin  
result = eval(@eentry.text, here)  
@ebuffer.insert(@ebuffer.end\_iter, “#{@eentry.text}\n”, “query”)  
@ebuffer.insert(@ebuffer.end\_iter, “#{result.inspect}\n”,  
“result”)  
rescue Exception =\> e  
@ebuffer.insert(@ebuffer.end\_iter, “#{e.message}\n”,  
“backtrace”)  
e.backtrace.each do |line|  
@ebuffer.insert(@ebuffer.end\_iter, “#{line}\n”, “backtrace”)  
end  
end  
@ehistory \<\< @eentry.text  
@eacurrent = -1  
@eentry.text = “”  
scroll.vadjustment.value = scroll.vadjustment.upper -  
scroll.vadjustment.page\_size  
end

```
@edialog.set_default_size(512, 384)
```

---

<div class="post-metadata">

**Author:** ![giorgian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@giorgian](https://www.ruby-forum.com/u/giorgian)\
**Post date:** [July 29, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/scrolledwindow-scrolling-automatically-to-the-bottom/110681/2 "2007-07-29T19:50:30Z")

</div>

i find the solution!

textview.signal\_connect(“size-allocate”) do |widget, step, arg2|  
@scroll.vadjustment.value = @scroll.vadjustment.upper -  
@scroll.vadjustment.page\_size  
end

---

<div class="post-metadata">

**Author:** ![giorgian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@giorgian](https://www.ruby-forum.com/u/giorgian)\
**Post date:** [July 30, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/scrolledwindow-scrolling-automatically-to-the-bottom/110681/3 "2007-07-30T19:28:20Z")

</div>

Note that you could also use something like (not tested, but it captures  
the essense of the idea):

textview.scroll\_to\_iter(textview.buffer.end\_iter, 0.5, true, 0.0, 0.0)

[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ATextView#scroll\_to\_iter](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ATextView#scroll_to_iter)

Note per the documentation, you may want to instead get the mark for the  
end\_iter, and use scroll\_to\_mark instead.

-pete

On Sun, Jul 29, 2007 at 07:50:30PM +0200, Pietro G. wrote:

> * * *
> 
> This [SF.net](http://SF.net) email is sponsored by: Splunk Inc.  
> Still grepping through log files to find problems? Stop.  
> Now Search log events and configuration files using AJAX and a browser.  
> Download your FREE copy of Splunk now \>\> [http://get.splunk.com/](http://get.splunk.com/)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> removed\_email\_address@domain.invalid  
> [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–

* * *

This [SF.net](http://SF.net) email is sponsored by: Splunk Inc.  
Still grepping through log files to find problems? Stop.  
Now Search log events and configuration files using AJAX and a browser.  
Download your FREE copy of Splunk now \>\> [http://get.splunk.com/](http://get.splunk.com/)

---

<div class="post-metadata">

**Author:** ![giorgian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@giorgian](https://www.ruby-forum.com/u/giorgian)\
**Post date:** [April 18, 2010, 10:23am UTC](https://www.ruby-forum.com/t/scrolledwindow-scrolling-automatically-to-the-bottom/110681/4 "2010-04-18T10:23:20Z")

</div>

On 18 April 2010 07:18, Marijn Van vliet  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> see the logic in it, but GTK is seriously lacking that a hack like this  
> is necessary. Thank you so much for sharing.

If I remember correctly (and I might not), the page\_size is only  
set correctly after the page is rendered (makes sense). So if  
you draw into the window you have to wait until the GTK  
events are cleared before scrolling to the bottom.

I have an app with a split pane and scrolled windows in each  
pane and wanted to move the adjustment on the panes so  
that the maximum of each pane was displayed. It took me  
ages to figure out why the page size was always wrong.

```
      MikeC

```

---

<div class="post-metadata">

**Author:** ![giorgian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@giorgian](https://www.ruby-forum.com/u/giorgian)\
**Post date:** [April 19, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/scrolledwindow-scrolling-automatically-to-the-bottom/110681/5 "2010-04-19T18:39:45Z")

</div>

I solved the issues like this…

@buffer.signal\_connect(“changed”) {  
eob\_mark =  
@output.buffer.create\_mark(nil,@output.buffer.start\_iter.forward\_to\_end,false)  
@output.scroll\_mark\_onscreen(eob\_mark)  
}

When I see a change to the window I set a mark at the end of the file  
and then scroll to put the mark onscreen. This updates a across my  
application that monitors 15-20 console connections and makes sure when  
I bring the scrolled window to the front I am looking at the latest  
output.

---

<div class="post-metadata">

**Author:** ![giorgian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@giorgian](https://www.ruby-forum.com/u/giorgian)\
**Post date:** [April 18, 2010, 12:18am UTC](https://www.ruby-forum.com/t/scrolledwindow-scrolling-automatically-to-the-bottom/110681/6 "2010-04-18T00:18:10Z")

</div>

Pietro G. wrote:

> i find the solution!
> 
> textview.signal\_connect(“size-allocate”) do |widget, step, arg2|  
> @scroll.vadjustment.value = @scroll.vadjustment.upper -  
> @scroll.vadjustment.page\_size  
> end

We’ve been looking for ages to find the answer to this problem. We can  
see the logic in it, but GTK is seriously lacking that a hack like this  
is necessary. Thank you so much for sharing.
