How to scroll to a specific line

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to scroll to a specific line

John Coppens
Hi all...

I've been looking at many posts and articles from all over Google, didn't find
any solution that works.

I'm using Gtk-3, introspection, and Python 3. I've tried many combinations of

get_iter_at_line, place_cursor, place_cursor_onscreen, and others. Even tried to
more or less calculate the scroll position and manipulate the vadjustments.

An example:
        itr = self.tbff.get_iter_at_line(linenr)
        self.tbff.place_cursor(itr)
        self.tview.place_cursor_onscreen()

I suspect that the problem may come from the fact that the widget has not
been rendered yet. I also tried to defer the actual scroll to GLib.idle_add,
but that didn't work out either.

Can anyone point me in the right direction?

John
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: How to scroll to a specific line

Reuben Rissler
On 07/26/2018 02:17 PM, John Coppens wrote:

> get_iter_at_line, place_cursor, place_cursor_onscreen, and others. Even tried to
> more or less calculate the scroll position and manipulate the vadjustments.
>
> An example:
>          itr = self.tbff.get_iter_at_line(linenr)
>          self.tbff.place_cursor(itr)
>          self.tview.place_cursor_onscreen()
>
> I suspect that the problem may come from the fact that the widget has not
> been rendered yet. I also tried to defer the actual scroll to GLib.idle_add,
> but that didn't work out either.
>

This has worked for me:


         text_iter = self.source_buffer.get_iter_at_line (linenr)
         self.source_buffer.place_cursor(text_iter)
         self.source_view.scroll_to_iter(text_iter, 0.1, True, 0.0, 0.5)

I have also seen place_cursor_onscreen() work well. Now the thing is,
none of this will work until the Gtk.SourceView has properly been
rendered. This means doing something like:

         self.source_buffer.set_text("my text")
         GLib.idle_add(self.scroll_function)

     def scroll_function (self):
         # your scroll code here, like you use already

So you see, the scroll part is not the problem, but rather the
calculations of iters fails, because these iters are not yet, or are
invalidated when the buffer/view changes.

Cheers, Reuben


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: How to scroll to a specific line

John Coppens
On Fri, 27 Jul 2018 07:32:13 -0400
Reuben Rissler <[hidden email]> wrote:

> I have also seen place_cursor_onscreen() work well. Now the thing is,
> none of this will work until the Gtk.SourceView has properly been
> rendered. This means doing something like:
>
>          self.source_buffer.set_text("my text")
>          GLib.idle_add(self.scroll_function)
>
>      def scroll_function (self):
>          # your scroll code here, like you use already
>
> So you see, the scroll part is not the problem, but rather the
> calculations of iters fails, because these iters are not yet, or are
> invalidated when the buffer/view changes.

Hello Reuben.

Thanks for the suggestion. Believe it or not, I had just arrived at that
same solution! I first tried to use GLib.timeout_add(), and that worked too,
if the time was larger than 300 - 400 ms, which is on the verge of annoying.
I then tried idle_add(), and I guess that makes the 300-400ms 'built-in'.

Now, can this visible delay be eliminated? Maybe disabling the scroll
animation alltogether? (it's really visually a scrolling issue) I haven't seen
this problem in Geany (which I believe uses scintilla).

I did a more complete description of the issue on Stackoverflow, because, at
first, I didn't see my question appearing on the mailing list here:

https://stackoverflow.com/questions/51558047/how-to-select-and-scroll-a-specific-line-in-gtksource

John
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: How to scroll to a specific line

Reuben Rissler

> Thanks for the suggestion. Believe it or not, I had just arrived at that
> same solution! I first tried to use GLib.timeout_add(), and that worked too,
> if the time was larger than 300 - 400 ms, which is on the verge of annoying.
> I then tried idle_add(), and I guess that makes the 300-400ms 'built-in'.
>
How many lines of text do you have? With 500 - 700 lines of text, my
scrolling is instantaneous. The scroll feature itself has a 'soft' stop,
but that is quite acceptable for me, as it is easier to adjust my eyes
to the change.

Reuben

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: How to scroll to a specific line

John Coppens
On Sat, 28 Jul 2018 07:42:16 -0400
Reuben Rissler <[hidden email]> wrote:

> > Thanks for the suggestion. Believe it or not, I had just arrived at
> > that same solution! I first tried to use GLib.timeout_add(), and
> > that worked too, if the time was larger than 300 - 400 ms, which is
> > on the verge of annoying. I then tried idle_add(), and I guess that
> > makes the 300-400ms 'built-in'.
> >
> How many lines of text do you have? With 500 - 700 lines of text, my
> scrolling is instantaneous. The scroll feature itself has a 'soft'
> stop, but that is quite acceptable for me, as it is easier to adjust
> my eyes to the change.

Reuben,

The code I'm using to test is short (about 250 lines). I'm using the system
to single step program execution, so I will do many steps. The problem at
the moment is when loading the program, and scrolling to the start of
the program (the 'reset' position), but I see that the slowing down delay
is quite visible even when scrolling just one line with the arrow keys.

I have some Gtk-2 programs where the delay is not apparent. Maybe this
is configurable?

John

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: How to scroll to a specific line

David C. Rankin
On 07/28/2018 08:29 AM, John Coppens wrote:

> On Sat, 28 Jul 2018 07:42:16 -0400
> Reuben Rissler <[hidden email]> wrote:
>
>>> Thanks for the suggestion. Believe it or not, I had just arrived at
>>> that same solution! I first tried to use GLib.timeout_add(), and
>>> that worked too, if the time was larger than 300 - 400 ms, which is
>>> on the verge of annoying. I then tried idle_add(), and I guess that
>>> makes the 300-400ms 'built-in'.
>>>
>> How many lines of text do you have? With 500 - 700 lines of text, my
>> scrolling is instantaneous. The scroll feature itself has a 'soft'
>> stop, but that is quite acceptable for me, as it is easier to adjust
>> my eyes to the change.
>
> Reuben,
>
> The code I'm using to test is short (about 250 lines). I'm using the system
> to single step program execution, so I will do many steps. The problem at
> the moment is when loading the program, and scrolling to the start of
> the program (the 'reset' position), but I see that the slowing down delay
> is quite visible even when scrolling just one line with the arrow keys.
>
> I have some Gtk-2 programs where the delay is not apparent. Maybe this
> is configurable?
>
> John
>
> _______________________________________________
> gtk-app-devel-list mailing list
> [hidden email]
> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>

John,

  Maybe we can trade help. There are actually two functions you can use to
scroll the window to insure a text mark (usually the 'insert' mark) is visible
within your view, e.g.

(the following is from a find/replace widget I made)
https://github.com/drankinatty/gtkwrite/blob/master/gtk_findreplace.c#L920

            /* scroll window to mark to insure match is visible */
#ifndef TOMARK
            gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (app->view),
                                        app->last_pos, 0.0, TRUE, 0.95, 0.8);
#else
            gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (app->view),
                                                app->last_pos);
#endif

  The gtk_text_view_scroll_to_mark give you finer control over how far from
the bottom/top of your view the mark is displayed, the
gtk_text_view_scroll_mark_onscreen just makes sure it is visible (but
generally does a fine job) I left a #define in the code when I wrote it so I
could compare the behavior of each. I use the gtk_text_view_scroll_to_mark by
default. Both give instantaneous performance, even in a longer file (for me
that is 2000 - 5000 lines)

  Also note it is called from 'btnreplace_activate'
https://github.com/drankinatty/gtkwrite/blob/master/gtk_findreplace.c#L1001

and the scroll is wrapped within begin/end user action to prevent any
selection bound changes being momentarily visible during the find/replace
operation:

  gtk_text_buffer_begin_user_action (buffer);
     (code for gtk_text_view_scroll_to_mark
  gtk_text_buffer_end_user_action (buffer);

  You shouldn't notice any delay when implementing the scroll in any
reasonable size file.

--
David C. Rankin, J.D.,P.E.
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list