Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Unable To Keep Showing Last Item In Gtk2::TreeView

by williams (Beadle)
on Apr 06, 2012 at 23:26 UTC ( #963866=perlquestion: print w/ replies, xml ) Need Help??
williams has asked for the wisdom of the Perl Monks concerning the following question:

The code below illustrates a problem I'm having with Gtk2::TreeView (or here). It just shows a list of random words, and adds a new one at the bottom once every second. I'd like to automatically scroll the new word into view if the one just before it was visible. That way, this automatic scrolling won't annoy someone looking elsewhere in the tree view.

Everything words fine until something causes the tree view to shrink. Making the window smaller is an example. The actual application I'm working on has other examples, e.g., a Gtk2::Expander (or here). If you resize the window to be smaller, you should see the problem. The "gravity" of the scroll bar, I guess, moves it off the bottom after the shrink, turning off the automatic scrolling.

How can I keep my automatic scrolling, even after these kinds of events?

Thanks,

Jim

#!/usr/bin/perl use strict; use warnings; use Glib qw(TRUE FALSE); use Gtk2 -init; my($win,$tree)=createWin(); Glib::Timeout->add(1000,sub {tickCB($tree)}); $win->show_all(); Gtk2->main(); # # Creates the widgets in the application. Returns the main # window and tree view. # sub createWin { my($win,$scroll,$tree,$model); $win=new Gtk2::MainWindow(); $win->set_default_size(250,300); $win->signal_connect(destroy => \&Gtk2::main_quit); $win->add($scroll=new Gtk2::ScrolledWindow()); $scroll->add($tree=new Gtk2::TreeView()); $tree->set_rules_hint(TRUE); $tree->insert_column_with_attributes(-1,'Goo', new Gtk2::CellRendererText(),text => 0); $tree->set_model($model= new Gtk2::ListStore('Glib::String')); addWord($model) for 0 .. 100; showLast($tree); return ($win,$tree); } # # Called at regular intervals to add another random "word" # to the bottom of the tree view. If the previous word was # visible beforehand, scrolls the tree view so the new word # is visible. # sub tickCB { my($tree)=@_; my($model)=$tree->get_model(); my($numRows)=$model->iter_n_children(undef); my($lastVis)=($tree->get_visible_range())[1]; my($mustScroll)=$lastVis && $lastVis->get_indices() == $numRows-1; addWord($model); showLast($tree) if $mustScroll; return TRUE; } # # Adds a random "word" to the bottom of the tree view. # sub addWord { my($model)=@_; my(@cons)=grep !/[aeiou]/,'a' .. 'z'; $model->set($model->append(),0, $cons[rand @cons] . 'oo'); } # # Scrolls the tree view so the last row is visible. # sub showLast { my($tree)=@_; my($numRows)=$tree->get_model()->iter_n_children(undef); $tree->scroll_to_cell( new Gtk2::TreePath($numRows-1),undef,TRUE,0.0,1.0); }

Comment on Unable To Keep Showing Last Item In Gtk2::TreeView
Download Code
Re: Unable To Keep Showing Last Item In Gtk2::TreeView
by zentara (Archbishop) on Apr 07, 2012 at 16:45 UTC
    Hi, that was a tricky one, and had me scratching my head for a bit. :-)

    First, I don't know how the line

    $win=new Gtk2::MainWindow();
    ran for you. It should be
    $win= Gtk2::Window->new();
    that was easy. :-) But now to the hard problem.

    I got a clue from how the TextView scrolled to end by needing a signal connect on it's underlying buffer, which without it, would exhibit the exact same symptoms of the scroll-to-end going off target after the is window resized smaller. So here is a working version of your script, notice the $model->signal_connect( 'row-inserted' => sub{}).

    #!/usr/bin/perl use strict; use warnings; use Glib qw(TRUE FALSE); use Gtk2 -init; my($win,$tree)=createWin(); Glib::Timeout->add(1000,sub {tickCB($tree)}); $win->show_all(); Gtk2->main(); # # Creates the widgets in the application. Returns the main # window and tree view. # my $model; sub createWin { my($win,$scroll,$tree); #,$model); #$win=new Gtk2::MainWindow(); $win = Gtk2::Window->new (); $win->set_default_size(250,300); $win->signal_connect(destroy => \&Gtk2::main_quit); $win->add($scroll=new Gtk2::ScrolledWindow()); $scroll->add($tree=new Gtk2::TreeView()); $tree->set_rules_hint(TRUE); $tree->insert_column_with_attributes(-1,'Goo', new Gtk2::CellRendererText(),text => 0); $tree->set_model($model= new Gtk2::ListStore('Glib::String')); # without this signal connect, making the window bigger # will be ok for scrolling, but will screw up # on making window smaller $model->signal_connect( 'row-inserted' => sub { my ( $view, $iter, $path ) = @_; # print "@_\n"; my($numRows)=$tree->get_model()->iter_n_children(undef); $tree->scroll_to_cell( new Gtk2::TreePath($numRows-1),undef,TRUE,0.0,1.0); } ); addWord($model) for 0 .. 100; showLast($tree); return ($win,$tree); } # # Called at regular intervals to add another random "word" # to the bottom of the tree view. If the previous word was # visible beforehand, scrolls the tree view so the new word # is visible. # sub tickCB { my($tree)=@_; my($model)=$tree->get_model(); my($numRows)=$model->iter_n_children(undef); my($lastVis)=($tree->get_visible_range())[1]; my($mustScroll)=$lastVis && $lastVis->get_indices() == $numRows-1; addWord($model); showLast($tree) if $mustScroll; return TRUE; } # # Adds a random "word" to the bottom of the tree view. # sub addWord { my($model)=@_; my(@cons)=grep !/[aeiou]/,'a' .. 'z'; $model->set($model->append(),0, $cons[rand @cons] . 'oo'); } # # Scrolls the tree view so the last row is visible. # sub showLast { my($tree)=@_; my($numRows)=$tree->get_model()->iter_n_children(undef); $tree->scroll_to_cell( new Gtk2::TreePath($numRows-1),undef,TRUE,0.0,1.0); $tree->set( vadjustment => $tree->get_vadjustment ); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thanks for the suggestion. And, oops, on the Window/MainWindow thing. See the corrected version below, still with the shrink problem.

      What you suggested is not quite what I'm after, since it unconditionally moves the scroll bar to the bottom of the view anytime a new item is added. I'm trying to move the scroll bar only if it is already at the bottom just before the new item is added. That way, I can be looking around somewhere else with the scroll bar, without it jumping to the bottom everytime another item is added.

      So it's an annoyance issue: I want this auto-scrolling, but not while I'm looking somewhere else in the tree view. What I've got works, except when the tree view shrinks. That causes the scroll bar to move slightly off the bottom, mistakenly turning off auto-scrolling.

      By the way, I've tried attaching to many different signals, but the tree view internals seem too stale for $tree->get_visible_range(), $tree->scroll_to_cell(), or whatever to work.

      Still searching,

      Jim

      #!/usr/bin/perl use strict; use warnings; use Glib qw(TRUE FALSE); use Gtk2 -init; my($win,$tree)=createWin(); Glib::Timeout->add(1000,sub {tickCB($tree)}); $win->show_all(); Gtk2->main(); # # Creates the widgets in the application. Returns the main # window and tree view. # sub createWin { my($win,$scroll,$tree,$model); $win=new Gtk2::Window(); $win->set_default_size(250,300); $win->signal_connect(destroy => \&Gtk2::main_quit); $win->add($scroll=new Gtk2::ScrolledWindow()); $scroll->add($tree=new Gtk2::TreeView()); $tree->set_rules_hint(TRUE); $tree->insert_column_with_attributes(-1,'Goo', new Gtk2::CellRendererText(),text => 0); $tree->set_model($model= new Gtk2::ListStore('Glib::String')); addWord($model) for 0 .. 100; showLast($tree); return ($win,$tree); } # # Called at regular intervals to add another random "word" # to the bottom of the tree view. If the previous word was # visible beforehand, scrolls the tree view so the new word # is visible. # sub tickCB { my($tree)=@_; my($model)=$tree->get_model(); my($numRows)=$model->iter_n_children(undef); my($lastVis)=($tree->get_visible_range())[1]; my($mustScroll)=$lastVis && $lastVis->get_indices() == $numRows-1; addWord($model); showLast($tree) if $mustScroll; return TRUE; } # # Adds a random "word" to the bottom of the tree view. # sub addWord { my($model)=@_; my(@cons)=grep !/[aeiou]/,'a' .. 'z'; $model->set($model->append(),0, $cons[rand @cons] . 'oo'); } # # Scrolls the tree view so the last row is visible. # sub showLast { my($tree)=@_; my($numRows)=$tree->get_model()->iter_n_children(undef); $tree->scroll_to_cell( new Gtk2::TreePath($numRows-1),undef,TRUE,0.0,1.0); }
        Hi again, this is about as far as I got experimenting with maintaining scroll position. This script isn't considered bullet proof, it has a runaway condition if you click too fast on the zoom control, and not all the Labels work as intended. You might get some experience with Adjustments by playing with it. It also is on a Goo canvas, not a TreeView.:-)

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963866]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-09-21 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (165 votes), past polls