#!/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 ); }