Re: Gtk2::TextView scrolling




Thomas Christensen said:
I am having a problem with scrolling a page in Gtk2::TextView.  Is
there any way to programmatically scroll a TextView?  For instance if
I have a button that has to make it scroll a page up.  Preferably I
would like to invoke the same code, that a <PAGE UP> button invokes in
the TextView.

pretend you are a scrollbar, and mess with the scrolled window's adjustments.



use strict;
use Glib ':constants';
use Gtk2 -init;

my $window = Gtk2::Window->new;
my $scroll = Gtk2::ScrolledWindow->new;
my $textview = Gtk2::TextView->new;
my $button = Gtk2::Button->new ("scroll me, baby");
my $vbox = Gtk2::VBox->new;

$window->add ($vbox);
$vbox->add ($scroll);
$vbox->pack_start ($button, FALSE, FALSE, 0);
$scroll->add ($textview);

my $buffer = $textview->get_buffer;

{
local $/ = undef;
open IN, $0;
$buffer->insert ($buffer->get_start_iter, scalar <IN>);
close IN;
}

$window->show_all;

my $going_up = TRUE;

$window->signal_connect (destroy => sub {Gtk2->main_quit});
$button->signal_connect (clicked => sub {
        my $adj = $scroll->get_vadjustment;
        my $incr = $adj->page_increment;
        my $current = $adj->get_value;
        my $newval = $going_up
                   ? $current + $incr
                   : $current - $incr;
        if ($newval >= ($adj->upper - $incr)) {
                $going_up = FALSE;
                $newval = $adj->upper - $incr;
        } elsif ($newval <= 0) {
                $going_up = TRUE;
        }
        $adj->set_value ($newval);
});


Gtk2->main;


-- 
muppet <scott at asofyet dot org>



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