Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/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__

In reply to Gtk2 2d Table Demo by zentara

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2023-03-29 12:40 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (71 votes). Check out past polls.