Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Gtk2 linked scrolled Canvases in table

by zentara (Archbishop)
on Nov 17, 2006 at 18:39 UTC ( #584779=snippet: print w/replies, xml ) Need Help??
Description: This is just a useful little construct to show how easy it is to setup a table of Gnome2 canvases, and link their scrollbars so they scroll like a big spreadsheet. The precision is pretty good. You can also use the canvas_aa (anti-aliased canvas) to get transparent colors, but it takes significantly longer to startup.

The secret to figuring out the table setup, is to draw a set of grid-lines on paper, and notice the left,right,top,and bottom line numbers, which are the attach points.

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gnome2::Canvas;
use Gtk2 -init;

my $window = new Gtk2::Window ( "toplevel" );
$window->signal_connect ("delete_event", sub { Gtk2->main_quit; });
#$window->set_default_size( 350, 350 );
$window->set_border_width (10);

my $table = new Gtk2::Table (4, 4, FALSE);
$window->add ($table);

my %canvas;

# top ###############################################
$canvas{'top'}   = Gnome2::Canvas->new;
$canvas{'top'}->set_scroll_region( 0, 0, 1500, 50 );
$canvas{'top'}->modify_bg('normal',Gtk2::Gdk::Color->new (0xDDDD,0xDDD
+D,0xFFFF));

$canvas{'top'}->set_size_request (400, 50);

my $root = $canvas{'top'}->root;
my $line = Gnome2::Canvas::Item->new(
   $root, 'Gnome2::Canvas::Line',
     points => [0, 25, 1500, 25],
     fill_color => '#ff0000',
     #fill_color_rgba => 0x00000080,  # for anti-aliased canvas
     width_units => 3.0,              # 6 digits plus 2 for transparen
+cy level
);


for(my $x = 0; $x<=1500; $x += 100){
 Gnome2::Canvas::Item->new(
   $root, 'Gnome2::Canvas::Line',
     points => [$x, 20, $x, 30],
     fill_color => '#ff0000',
     width_units => 1.0,
  );

 Gnome2::Canvas::Item->new(
   $root, 'Gnome2::Canvas::Text',
   x          => $x,
   y          => 5,
   fill_color => '#ff0000',
   font       => 'Sans 14',
   anchor     => 'GTK_ANCHOR_CENTER',
   text       => $x, 
);
 }


$table->attach ($canvas{'top'},
                1, 3,               0, 1,
               ['expand','shrink', 'fill'], [],
               0,                  0);

### left ###################################################

$canvas{'left'}  = Gnome2::Canvas->new;
$canvas{'left'}->set_scroll_region( 0, 0, 50, 1500 );
$canvas{'left'}->modify_bg('normal',Gtk2::Gdk::Color->new (0xDDDD,0xFF
+FF,0xDDDD));
$canvas{'left'}->set_size_request (50, 400);

$root = $canvas{'left'}->root;

$line = Gnome2::Canvas::Item->new(
   $root, 'Gnome2::Canvas::Line',
     points => [25, 0, 25, 1500],
     fill_color => '#0000ff',
     width_units => 3.0,
);


for(my $y = 0; $y<=1500; $y += 50){
 
  Gnome2::Canvas::Item->new(
   $root, 'Gnome2::Canvas::Line',
     points => [20, $y, 30, $y],
     fill_color => '#0000ff',
     width_units => 1.0,
  );

  Gnome2::Canvas::Item->new(
    $root, 'Gnome2::Canvas::Text',
    x          => 10,
    y          => $y,
    fill_color => '#0000ff',
    font       => 'Sans 14',
    anchor     => 'GTK_ANCHOR_NW',
    text       => $y, 
);

 }


$table->attach ($canvas{'left'},
                0, 1,               1, 3,
                [], ['expand','shrink', 'fill'],
            0,                  0);

### main ###################################################

$canvas{'main'}  = Gnome2::Canvas->new;
$canvas{'main'}->set_scroll_region( 0, 0, 1500, 1500 );
$canvas{'main'}->modify_bg('normal',Gtk2::Gdk::Color->new (0xFFFE,0xFF
+FE,0xFFFE));

$root = $canvas{'main'}->root;

for(my $y = 0; $y<=1500; $y += 50){

   Gnome2::Canvas::Item->new(
     $root, 'Gnome2::Canvas::Line',
      points => [0, $y, 1500, $y],
      fill_color => '#000000',
      width_units => 2.0,
  );
  

  for(my $x = 0; $x<=1500; $x += 100){

   Gnome2::Canvas::Item->new(
   $root, 'Gnome2::Canvas::Line',
     points => [$x, $y-3, $x, $y+3],
     fill_color => '#000000',
     width_units => 1.0,
  );

  Gnome2::Canvas::Item->new(
    $root, 'Gnome2::Canvas::Text',
    x          => $x,
    y          => $y + 10,
    fill_color => 'black',
    font       => 'Sans 14',
    anchor     => 'GTK_ANCHOR_CENTER',
    text       => $x, 
   );
 }
}

$table->attach ($canvas{'main'},
                1, 3,               1, 3,
                ['expand', 'fill'], ['expand', 'fill'],
                0,                  0);

##################################################################
my $hadj1 = $canvas{'main'}->get_hadjustment;
my $hscrollbar = Gtk2::HScrollBar->new($hadj1);
$hscrollbar->set_update_policy('continuous');

$table->attach ($hscrollbar,
                1, 3,                         3, 4,
                ['expand', 'shrink', 'fill'], [],
                0,                            0 );

my $vadj1 = $canvas{'main'}->get_vadjustment;
my $vscrollbar = Gtk2::VScrollBar->new($vadj1);
$vscrollbar->set_update_policy('continuous');

$table->attach ($vscrollbar,
                3, 4,    1, 3,
                [],      ['fill', 'expand', 'shrink'],
                0,       0 );

# inter-connect scrollbars to the main canvas
$canvas{'left'}->set_vadjustment($vadj1);
$canvas{'top'}->set_hadjustment($hadj1);

$window->show_all;

Gtk2->main;
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://584779]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-07-04 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?