Re: Find out whether a GtkTextMark is in the visible area



On Fri, 12 Aug 2011 02:57:05 +0200
Michael Tänzer <taenzermichi googlemail com> wrote:
> I know there is a gtk_text_view_scroll_to_mark() and
> gtk_text_view_scroll_mark_onscreen() function but I don't want to
> scroll to a mark but check whether a mark is already on the screen.
> There is gtk_text_mark_get_visible() but that only returns whether
> the mark is painted.
> 
> Is there an easy way to do that? I probably can jump through through
> various loopholes to get this (involving
> gtk_text_buffer_get_iter_at_mark(), gtk_text_view_get_iter_location()
> and gtk_text_view_get_visible_rect()) but then it might be just too
> much work not worth the feature.
> 
> The background is, that I want to do an intelligent auto-scroll for a
> chat log. If the scroll position is all the way on the bottom the
> TextView should do the normal auto-scroll via
> gtk_text_view_scroll_mark_onscreen() but if the user scrolled to a
> different area (e.g. to read past posts) it should stop auto-scrolling
> and just stay where it was. The easiest way I can think of right now
> would be to have a right-weighted mark at the end of the log and
> whenever a new message is posted check if the mark is in the visible
> area, then add the new message and only when the mark was on screen
> before make the TextView scroll_mark_onscreen()

That is a vastly over-elaborate way to do it.  You can detect whether
the user has scrolled back, by means of the scrolled window's
adjustment.  An expression such as this should tell you what
you want:

(gtk_adjustment_get_value(adjustment) >=
   gtk_adjustment_get_upper(adjustment) -
   gtk_adjustment_get_page_size(adjustment) - 1e-12)

This expression will be true if the user has not scrolled back manually.
If the user has not scrolled back manually you will need to scroll to
the end mark after inserting text, otherwise you leave the scrolled
window position where it is.

Chris




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