#!/usr/bin/perl -w use Gtk; use strict; set_locale Gtk; init Gtk; my $false = 0; my $true = 1; my $window; my $vbox; my $hbox; my $scrolled_window; my $clist; my $button_add; my $button_clear; my $button_hide_show; my $titles_visible = $false; my @titles = ( "Ingredients", "Amount" ); $window = new Gtk::Window( "toplevel" ); $window->set_usize( 300, 150 ); $window->set_title( "CList Example" ); $window->signal_connect( "destroy", sub { Gtk->exit( 0 ); } ); $vbox = new Gtk::VBox( $false, 5 ); $window->add( $vbox ); $vbox->border_width( 5 ); $vbox->show(); # Create a scrolled window to pack the CList widget into $scrolled_window = new Gtk::ScrolledWindow( undef, undef ); $vbox->pack_start( $scrolled_window, $true, $true, 0 ); $scrolled_window->set_policy( 'automatic', 'always' ); $scrolled_window->show(); # Create the CList. For this example we use 2 columns $clist = new_with_titles Gtk::CList( @titles ); # When a selection is made, we want to know about it. The callback # used is selection_made, and its code can be found further down $clist->signal_connect( "select_row", \&selection_made ); # It isn't necessary to shadow the border, but it looks nice :) $clist->set_shadow_type( 'out' ); # What however is important, is that we set the column widths as # they will never be right otherwise. Note that the columns are # numbered from 0 and up (to 1 in this case). $clist->set_column_width( 0, 150 ); # Add the CList widget to the vertical box and show it. $scrolled_window->add( $clist ); $clist->show(); # Create the buttons and add them to the window. $hbox = new Gtk::HBox( $false, 0 ); $vbox->pack_start( $hbox, $false, $true, 0 ); $hbox->show(); $button_add = new Gtk::Button( "Add List" ); $button_clear = new Gtk::Button( "Clear List" ); $button_hide_show = new Gtk::Button( "Hide/Show Titles" ); $hbox->pack_start( $button_add, $true, $true, 0 ); $hbox->pack_start( $button_clear, $true, $true, 0 ); $hbox->pack_start( $button_hide_show, $true, $true, 0 ); # Connect our callbacks to the three buttons $button_add->signal_connect( "clicked", \&button_add_clicked, $clist ); $button_clear->signal_connect( "clicked", sub { $clist->clear(); } ); $button_hide_show->signal_connect( "clicked", \&button_hide_show_clicked, $clist ); $button_add->show(); $button_clear->show(); $button_hide_show->show(); $window->show(); main Gtk; exit( 0 ); ### Subroutines # User clicked the "Add List" button. sub button_add_clicked { my ( $widget, $clist ) = @_; my $i; # Something silly to add to the list. 4 rows of 2 columns each. my @drink = ( [ Milk => "3 Oz" ], [ Water => "6 l" ], [ Carrots => "2" ], [ Snakes => "55" ] ); # Here we do the actual adding of the text. It's done once for each row. for ( $i = 0; $i < 4; $i++ ) { $clist->append( @{$drink[ $i ]} ); } # For those who may not have understood the above, here is a brief # explanation. @drink is a list of references to lists # (references are scalars holding a memory address, similar to # pointers). This is used because Perl doesn't directly support # two-dimensional arrays. Because of this, $drink[$i] is not a # list, but a reference to one, so we must dereference it by # enclosing it like this: @{ reference } ( we use @ because it is # a reference to a list. For further information on references, # see the perlref(1) man page, or chapter 4 of _Programming Perl_. return; } # The user clicked the "Hide/Show titles" button. sub button_hide_show_clicked { my ( $widget, $clist ) = @_; if ( $titles_visible ) { # Hide the titles and set the flag to 1 $clist->column_titles_hide(); $titles_visible++; } else { # Show the titles and reset flag to 0 $clist->column_titles_show(); $titles_visible--; } return; } # If we come here, then the user has selected a row in the list. sub selection_made { my ( $clist, $row, $column, $event, @data ) = @_; $clist->signal_emit_stop_by_name('select_row'); return 1; } # END EXAMPLE PROGRAM