RE: Unable To Keep Showing Last Item In Gtk2::TreeView
Williams, James P2 james.p2.willi...@lmco.com wrote: How can I keep my automatic scrolling, even after these kinds of events? I’ve tried reacting to various signals, but the tree view seems too stale for $tree-get_visible_range() or $tree-scroll_to_cell() to work. You might react to size-allocate events, but a slightly different approach is necessary. Here's a hack around your test code: Your suggestion was enough to get something running. I had to keep track of how many items were added, and had to watch for scroll bar changes. As far as I can tell, the scroll bar stays at the bottom now unless I move it, even if I resize the window to be shorter. My modified version follows, for reference. Thanks, Jim #!/usr/bin/perl use strict; use warnings; use Glib qw(TRUE FALSE); use Gtk2 -init; my($numAdded)=0; 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,$mustScroll); $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')); $tree-signal_connect( size_allocate = sub {sizeCB(\$mustScroll,@_)}); $model-signal_connect( row_inserted = sub {rowCB(\$mustScroll,$tree,@_)}); $scroll-get_vadjustment()-signal_connect( value_changed = sub {$numAdded=0}); addWords($model,100); showLast($tree,\$mustScroll); return ($win,$tree); } # # Called at regular intervals to add more random words to # the bottom of the tree view. If the previous word was # visible beforehand, scrolls the tree view so the new # words are visible. # sub tickCB { my($tree)=@_; addWords($tree-get_model(),100); return TRUE; } # # Adds random words to the bottom of the tree view. # sub addWords { my($model,$numAdd)=@_; my(@cons)=grep !/[aeiou]/,'a' .. 'z'; for (1 .. $numAdd) { $model-set($model-append(),0, $cons[rand @cons] . 'oo'); $numAdded++; } } # # Scrolls the tree view so the last row is visible. # sub showLast { my($tree,$mustScroll)=@_; my($numRows)=$tree-get_model()-iter_n_children(undef); $tree-scroll_to_cell( new Gtk2::TreePath($numRows-1),undef,TRUE,0.0,1.0); $$mustScroll=TRUE; $numAdded=0; } # # Called each time the tree view is resized. This is where # we correct the scroll bar, moving it to the bottom if # appropriate. # sub sizeCB { my($mustScroll,$tree,$rect)=@_; showLast($tree,$mustScroll) if $$mustScroll; } # # Called each time a row is added to the model. # sub rowCB { my($mustScroll,$tree,$model,$path,$it)=@_; my($numRows)=$model-iter_n_children(undef); my($lastVis); if ($tree-realized()) { $lastVis=($tree-get_visible_range())[1]; $$mustScroll=$lastVis $lastVis-get_indices() == $numRows-2-$numAdded; } else { $$mustScroll=TRUE; } } ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Unable To Keep Showing Last Item In Gtk2::TreeView
The code below illustrates a problem I'm having with Gtk2::TreeViewhttp://gtk2-perl.sourceforge.net/doc/pod/Gtk2/TreeView.html (or herehttp://developer.gnome.org/gtk/2.24/GtkTreeView.html). It just displays 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, but only 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::Expanderhttp://gtk2-perl.sourceforge.net/doc/pod/Gtk2/Expander.html (or herehttp://developer.gnome.org/gtk/2.24/GtkExpander.html). If you resize the window to be smaller, you should see the problem. The gravity of the scroll bar, or whatever, moves it slightly off the bottom of the view after the shrink, turning off the automatic scrolling. How can I keep my automatic scrolling, even after these kinds of events? I've tried reacting to various signals, but the tree view seems too stale for $tree-get_visible_range() or $tree-scroll_to_cell() to work. 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::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); } ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Re: Unable To Keep Showing Last Item In Gtk2::TreeView
Williams, James P2 james.p2.willi...@lmco.com wrote: How can I keep my automatic scrolling, even after these kinds of events? I’ve tried reacting to various signals, but the tree view seems too stale for $tree-get_visible_range() or $tree-scroll_to_cell() to work. You might react to size-allocate events, but a slightly different approach is necessary. Here's a hack around your test code: #Glib::Timeout-add( 1000, sub { tickCB($tree) } ); my $mustScroll = 0; Glib::Timeout-add( 1000, sub { addWord( $tree-get_model ); 1 } ); $tree-signal_connect( size_allocate = sub { showLast($tree) if $mustScroll } ); $tree-get_model-signal_connect( row_inserted = sub { my $lastVis = ( $tree-get_visible_range )[1]; $mustScroll = $lastVis $lastVis-get_indices == $tree-get_model-iter_n_children - 2; } ); 'HTH -ab ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list