[pygtk] Check if end of a TextView is visible

Steve McClure smcclure at racemi.com
Tue Sep 8 21:18:35 WST 2009


On Sep 6, 2009, at 4:18 PM, Greenpickle wrote:

> I have a TextView in a ScrolledWindow, and a process writes output to
> it; every time a line is written, it scrolls to the end using
> TextView.scroll_to_iter.  However, this means it's hard to scroll up  
> to
> view earlier output.
>
> What I want to do is check if the last line is visible before  
> printing a
> line, then print the line, then scroll to the end only if the last  
> line
> was visible before:
>
> textbuffer = textview.get_buffer()
> scroll = [if end is visible]
> textbuffer.insert(textbuffer.get_end_iter(), output + '\n')
> if scroll:
> textview.scroll_to_iter(textbuffer.get_end_iter(), .0)
>
> - So that it's possible to view stuff earlier on without being  
> scrolled
> back to the end all the time, but you can return to auto-scrolling by
> scrolling back to the end manually.  I haven't been able found any
> functions for TextViews/TextIters/TextMarks in any reference that  
> would
> do this.

I used this:
=====
     def addData(self, monitorPath, infoPath, eventType, service):
         #if eventType != gnome.vfs.MONITOR_EVENT_CHANGED:
         #   return

         fname = self.getServiceLogFullPath(service)
         source = open(fname, 'r')
         pos = self.position[service]
         source.seek(pos, 0) # put the pointer at the right place
         data = source.readline()
         iter = self.buffer.get_end_iter()

         # before we add any data, check to see if the insert point is
         # visible.  If so, the user hasn't scrolled the window and we
         # want to keep the newest data visible.
         sw = self.tree.get_widget('scrolledwindow2')
         adj = sw.get_vadjustment()
         scrollIfNecessary = (adj.value + adj.page_size) == adj.upper

         while data:
             unistr = unicode(data, errors='ignore')
             self.buffer.insert(iter, unistr.encode("utf-8"))

             data = source.readline()

         # jump to the end of this section
         if scrollIfNecessary:
             # we've added text so reread it.
             mark = self.buffer.get_data('mark')
             self.view.scroll_to_mark(mark, 0.05, True, 0.0, 1.0)
=====

At the moment, I'm not sure why I used a mark, but the 'mark' that  
get_data is fetching is initialized this way at the start of the main:

=====

         self.buffer = gtk.TextBuffer()
         mark = self.buffer.create_mark('end',  
self.buffer.get_end_iter(), False)
         self.buffer.set_data('mark', mark)
         self.view = self.tree.get_widget('textview1')
         self.view.set_buffer(self.buffer)

=====

>
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/

--
Steve McClure
smcclure at racemi.com



More information about the pygtk mailing list