Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Gtk2 2d Table Demo

by zentara (Archbishop)
on Feb 02, 2009 at 18:30 UTC ( #740772=sourcecode: print w/ replies, xml ) Need Help??

Category: GUI Programming
Author/Contact Info zentara of perlmonks
Description: A simple demo, to show how to display data in a nicely colored Table in Gtk2. Mouse actions on elements is demonstrated. A Table in Gtk2 more closely resembles an html table, unlike plain Tk Tables, which are more like cell grids.
#!/usr/bin/perl
use warnings;
use strict;
use Gtk2 -init;

# mouse buttons 1,2 and 3 all do different actions as demo


# hack the col widths by padding header entries
my @AoA = (
     ['','   Header1   ','   Header2   ','   Header3   ','   Header4  
+ '],
     ['row0',1,2,3,4],
     ['row1',5,6,7,8],     #example data
     ['row2',9,10,11,12],
     ['row3',13,14,15,16],
);

# add more random data
my $count = 4;
foreach my $new(1..100){
  my $aref = ['row'.$count, rand_string(), rand_string(),rand_string()
+, rand_string() ];
  push @AoA, $aref;
  $count++;
}

my $rows = scalar @AoA;
my $cols = scalar @{$AoA[0]};
print "$rows $cols\n";

# override any pixmap based themes so color can show
Gtk2::Rc->parse_string(<<__);

style "default"
{

bg_pixmap[NORMAL] = "<none>"
bg_pixmap[INSENSITIVE] = "<none>"
bg_pixmap[ACTIVE] = "<none>"
bg_pixmap[PRELIGHT] = "<none>"

}class "GtkWidget" style "default"
__

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


my $white = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xFFFF);
$window->modify_bg ('normal', $white);

my $vbox = Gtk2::VBox->new( 0, 0 );
#$vbox->modify_bg ('normal', $color);

my $title = Gtk2::Label->new("2D Array Table -w- alternate colored row
+s");
my $font_desc = Gtk2::Pango::FontDescription->from_string ("Times New 
+Roman Italic 20");

$title->modify_font($font_desc);

$title->modify_fg('normal',Gtk2::Gdk::Color->parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title,0, 1, 0);

my $scwin = Gtk2::ScrolledWindow->new();
$scwin->set_policy('automatic', 'automatic');

my $table = Gtk2::Table->new($rows,$cols);

my $header_color = Gtk2::Gdk::Color->parse("#CCFFCC");
my $row_color = Gtk2::Gdk::Color->parse("#DDFFDD");
my $odd_color = Gtk2::Gdk::Color->parse("#FFCC66");
my $even_color = Gtk2::Gdk::Color->parse("#CCFF99");
my $black = Gtk2::Gdk::Color->parse("#000000");
my $red = Gtk2::Gdk::Color->parse("#FFCCCC");

my $font_desc_h = Gtk2::Pango::FontDescription->from_string ("Times Ne
+w Roman 25");

my $tooltip = Gtk2::Tooltips->new;

for (my $row=0; $row < $rows; ++$row) {
        for (my $col=0; $col < $cols; ++$col) {
    
            my $event_box = Gtk2::EventBox->new;
            my $label = Gtk2::Label->new ( $AoA[$row][$col]);
            my $length = length( $AoA[$row][$col] );
            
            # add a tooltip
            my $tip_text = $AoA[$row][$col];
            $tooltip->set_tip($event_box, $tip_text, undef);

            #$event_box->modify_bg (normal => $color);
            $label->set_alignment(0, 0.5);
            $label->set_justify('left'); 
#            $label->set_width_chars( $length );
            $event_box->add ($label);
            $label->show;
            
             
        if ($row == 0) { 
                $label->modify_font($font_desc_h);
                $event_box->modify_bg (normal => $header_color);
             } elsif ($row %2 ==0) { 
                $event_box->modify_bg (normal => $even_color);
             }
        
        if ($col == 0) {
                $label->set_width_chars($length + 5);
             }    
    
        #$table->attach_defaults ($event_box, $col, $col+1, $row, $row
++1);
         $table->attach($event_box, $col, $col+1, $row, $row+1,'fill',
+'fill',1,2);
         
         # add some activities with mouse 1 and 3 on each entry
        $event_box->signal_connect( 'button_release_event', 
              sub{
              my ($item, $event) = @_;
              #print "$item, $event\n";
              my $button_num = $event->button();
              #print "$button_num\n";
        
              if( $button_num == 1){
                     print $label->get_text(),"\n";
                   }
        
            if( $button_num == 2){
                    $item->modify_bg (normal => $red);
                   }
        
            if( $button_num == 3){
                    $item->modify_bg (normal => $black);
                   }
    
          } );
        
    }
}

$scwin->add_with_viewport( $table );
$vbox->pack_start( $scwin, 1, 1, 0 );
$window->add( $vbox );


$window->show_all();
Gtk2->main;

sub rand_string{
  my @charset = ('a'..'z','A'..'Z','0'..'9');
  my $length_desired = 15;
   my $random_string;
   for(1..$length_desired){
     $random_string .= $charset[rand @charset];
    }
return  $random_string;
}

__END__

Comment on Gtk2 2d Table Demo
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-07-26 11:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls