Gtk2-Ex-ComboBox

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

Changes
MANIFEST
META.yml # Will be created by "make dist"
Makefile.PL
README
lib/Gtk2/Ex/ComboBox.pm
examples/combobox.pl
t/test.1.t
t/test.2.t
t/test.3.t
t/test.4.t
t/test.5.t
t/test.6.t

examples/combobox.pl  view on Meta::CPAN

my $hbox = Gtk2::HBox->new (FALSE, 10);
$hbox->pack_start (Gtk2::Label->new('One'), FALSE, TRUE, 0);    
$hbox->pack_start ($eventbox1, FALSE, TRUE, 0);    
$hbox->pack_start ($eventbox2, FALSE, TRUE, 0);    
$hbox->pack_start ($eventbox3, FALSE, TRUE, 0);    
$hbox->pack_start (Gtk2::Label->new('Three'), FALSE, TRUE, 0);    
$hbox->pack_start (Gtk2::Label->new('Four'), FALSE, TRUE, 0);    
$hbox->pack_start (Gtk2::Label->new('Five'), FALSE, TRUE, 0);    
$hbox->pack_start (Gtk2::Label->new('Six'), FALSE, TRUE, 0);    

my $combobox1 = Gtk2::Ex::ComboBox->new($label1, 'with-buttons');
$combobox1->set_list(['this', 'that', 'what']);
$combobox1->signal_connect('changed' => 
	sub {
		print "combobox1 selection changed\n";
	}
);
my $combobox2 = Gtk2::Ex::ComboBox->new($label2, 'with-checkbox');
$combobox2->set_list_preselected([[0,'how'], [1,'when'], [1,'where']]);
$combobox2->signal_connect('changed' => 
	sub {
		print "combobox2 selection changed\n";
	}
);

my $combobox3 = Gtk2::Ex::ComboBox->new($label3, 'no-checkbox');
$combobox3->set_list_preselected([[1,'how'], [0,'when'], [1,'where']]);
$combobox3->signal_connect('changed' => 
	sub {
		print "combobox3 selection changed\n";
	}
);

my $text = Gtk2::TextView->new;
my $dumpbutton = Gtk2::Button->new('Show Details');
my $vbox = Gtk2::VBox->new (FALSE, 0);
$vbox->pack_start ($hbox, FALSE, TRUE, 0);
$vbox->pack_start ($text, TRUE, TRUE, 0);
$vbox->pack_start ($dumpbutton, FALSE, TRUE, 0);
$window->add ($vbox);

$eventbox1->signal_connect('button-release-event' => sub { $combobox1->show; } );
$eventbox2->signal_connect('button-release-event' => sub { $combobox2->show; } );
$eventbox3->signal_connect('button-release-event' => sub { $combobox3->show; } );
$dumpbutton->signal_connect('button-release-event' => 
	sub {
		print Dumper $combobox1->get_selected_values;
		print Dumper $combobox2->get_selected_values;
		print Dumper $combobox3->get_selected_values;
		print Dumper $combobox1->get_selected_indices;
		print Dumper $combobox2->get_selected_indices;
		print Dumper $combobox3->get_selected_indices;
	}
);

$window->set_default_size(500, 200);
$window->show_all;

Gtk2->main;

sub add_arrow {
	my ($label) = @_;

lib/Gtk2/Ex/ComboBox.pm  view on Meta::CPAN

This widget also serves as an example implementation using the Gtk2::Ex::PopupWindow
module.

=head1 SYNOPSIS

	use Glib qw(TRUE FALSE);
	use Gtk2 qw/-init/;
	use Gtk2::Ex::ComboBox;

	my $button = Gtk2::Button->new('click me');
	my $combobox = Gtk2::Ex::ComboBox->new($button);
	# Or you can call the constructor with two arguments
	# my $combobox = Gtk2::Ex::ComboBox->new($button, 'no-checkbox' );
	$combobox->set_list(['this', 'that', 'what']);
	$button->signal_connect('button-release-event' => sub { $combobox->show; } );

=head1 METHODS

=head2 new($parent, <$type>);

The C<$parent> refers to the widget to which the ComboBox is attached.

The C<$type> parameter is optional. It can be of one of the three types

	'with-buttons' (this is the default option if none specified)
	'with-checkbox' 
	'no-checkbox'
	

These three types control the way the user selects the multiple entries.
Please refer to the C<examples/combobox.pl> for a demonstration.

=head2 set_list([$list]);

The list of choices is entered using this method. Accepts an array (of strings)
as the argument.

	$combobox->set_list(['this', 'that', 'what']);

=head2 set_list_preselected([$list]);

The list of choices is entered using this method. Accepts an array of arrays
as the argument. Each element array should have a boolean and a string.
The boolean denotes whether this element should be marked as selected or not.

	$combobox->set_list([[0,'this'], [1,'that'], [1,'what']]);

In the example shown above, the elements C<'that'> and C<'what'> will be marked
as I<selected> in the dropdown list.

=head2 get_treeview;

Returns the treeview that serves as the model for the ComboBox. This widget
internally uses Gtk2::Ex::Simple::List and therefore the return object will
be of that class.

lib/Gtk2/Ex/ComboBox.pm  view on Meta::CPAN

as I<selected>.

C<$hash{'unselected-indices'}> contains a list of all the indices that are B<not>
marked as I<selected>.

=head2 show;

Call this method to display the ComboBox. Typically on a C<'button-release-event'>
on the parent widget.

	$button->signal_connect('button-release-event' => sub { $combobox->show; } );

=head2 hide;

Call this method to hide the ComboBox. This method does not have to be called 
explicitly since the ComboBox is automatically closed if the user clicks anywhere
outside.

=head1 SIGNALS

C<changed> - This signal gets emitted whenever the selection is changed.

	$combobox1->signal_connect('changed' => 
		sub {
			print "combobox1 selection changed\n";
		}
	);

=head1 AUTHOR

Ofey Aikon

=head1 ACKNOWLEDGEMENTS

To the wonderful gtk-perl-list.

t/test.1.t  view on Meta::CPAN


use Gtk2::TestHelper tests => 7;

my $window = Gtk2::Window->new;
$window->signal_connect('destroy', sub {Gtk2->main_quit;});

my $label = Gtk2::Label->new('With-Buttons');
my $hbox = Gtk2::HBox->new (FALSE, 10);
$hbox->pack_start ($label, FALSE, TRUE, 0);    

my $combobox = Gtk2::Ex::ComboBox->new($label, 'with-buttons');
isa_ok($combobox, "Gtk2::Ex::ComboBox");
ok(!$combobox->set_list(['this', 'that', 'what']));

my $slist = $combobox->get_treeview;
isa_ok($slist, "Gtk2::Ex::Simple::List");

my $selected_indices = $combobox->get_selected_indices;
is(Dumper($selected_indices->{'selected-indices'}), Dumper([]));
is(Dumper($selected_indices->{'unselected-indices'}), Dumper([0,1,2]));

my $selected_values = $combobox->get_selected_values;
is(Dumper($selected_values->{'selected-values'}), Dumper([]));
is(Dumper($selected_values->{'unselected-values'}), Dumper(['this', 'that', 'what']));

my $text = Gtk2::TextView->new;

my $vbox = Gtk2::VBox->new (FALSE, 0);
$vbox->pack_start ($hbox, FALSE, TRUE, 0);
$vbox->pack_start ($text, TRUE, TRUE, 0);
$window->add ($vbox);
$window->set_default_size(500, 200);
$window->show_all;

$combobox->show;

t/test.2.t  view on Meta::CPAN


use Gtk2::TestHelper tests => 7;

my $window = Gtk2::Window->new;
$window->signal_connect('destroy', sub {Gtk2->main_quit;});

my $label = Gtk2::Label->new('With-Buttons');
my $hbox = Gtk2::HBox->new (FALSE, 10);
$hbox->pack_start ($label, FALSE, TRUE, 0);    

my $combobox = Gtk2::Ex::ComboBox->new($label, 'with-checkbox');
isa_ok($combobox, "Gtk2::Ex::ComboBox");
ok(!$combobox->set_list(['this', 'that', 'what']));

my $slist = $combobox->get_treeview;
isa_ok($slist, "Gtk2::Ex::Simple::List");

my $selected_indices = $combobox->get_selected_indices;
is(Dumper($selected_indices->{'selected-indices'}), Dumper([]));
is(Dumper($selected_indices->{'unselected-indices'}), Dumper([0,1,2]));

my $selected_values = $combobox->get_selected_values;
is(Dumper($selected_values->{'selected-values'}), Dumper([]));
is(Dumper($selected_values->{'unselected-values'}), Dumper(['this', 'that', 'what']));

my $text = Gtk2::TextView->new;

my $vbox = Gtk2::VBox->new (FALSE, 0);
$vbox->pack_start ($hbox, FALSE, TRUE, 0);
$vbox->pack_start ($text, TRUE, TRUE, 0);
$window->add ($vbox);
$window->set_default_size(500, 200);
$window->show_all;

$combobox->show;

t/test.3.t  view on Meta::CPAN


use Gtk2::TestHelper tests => 7;

my $window = Gtk2::Window->new;
$window->signal_connect('destroy', sub {Gtk2->main_quit;});

my $label = Gtk2::Label->new('With-Buttons');
my $hbox = Gtk2::HBox->new (FALSE, 10);
$hbox->pack_start ($label, FALSE, TRUE, 0);    

my $combobox = Gtk2::Ex::ComboBox->new($label, 'no-checkbox');
isa_ok($combobox, "Gtk2::Ex::ComboBox");
ok(!$combobox->set_list(['this', 'that', 'what']));

my $slist = $combobox->get_treeview;
isa_ok($slist, "Gtk2::Ex::Simple::List");

my $selected_indices = $combobox->get_selected_indices;
is(Dumper($selected_indices->{'selected-indices'}), Dumper([]));
is(Dumper($selected_indices->{'unselected-indices'}), Dumper([0,1,2]));

my $selected_values = $combobox->get_selected_values;
is(Dumper($selected_values->{'selected-values'}), Dumper([]));
is(Dumper($selected_values->{'unselected-values'}), Dumper(['this', 'that', 'what']));

my $text = Gtk2::TextView->new;

my $vbox = Gtk2::VBox->new (FALSE, 0);
$vbox->pack_start ($hbox, FALSE, TRUE, 0);
$vbox->pack_start ($text, TRUE, TRUE, 0);
$window->add ($vbox);
$window->set_default_size(500, 200);
$window->show_all;

$combobox->show;

t/test.4.t  view on Meta::CPAN


use Gtk2::TestHelper tests => 7;

my $window = Gtk2::Window->new;
$window->signal_connect('destroy', sub {Gtk2->main_quit;});

my $label = Gtk2::Label->new('With-Buttons');
my $hbox = Gtk2::HBox->new (FALSE, 10);
$hbox->pack_start ($label, FALSE, TRUE, 0);    

my $combobox = Gtk2::Ex::ComboBox->new($label, 'with-buttons');
isa_ok($combobox, "Gtk2::Ex::ComboBox");
ok(!$combobox->set_list_preselected([[0,'this'], [1,'that'], [1,'what']]));

my $slist = $combobox->get_treeview;
isa_ok($slist, "Gtk2::Ex::Simple::List");

my $selected_indices = $combobox->get_selected_indices;
is(Dumper($selected_indices->{'selected-indices'}), Dumper([1,2]));
is(Dumper($selected_indices->{'unselected-indices'}), Dumper([0]));

my $selected_values = $combobox->get_selected_values;
is(Dumper($selected_values->{'selected-values'}), Dumper(['that', 'what']));
is(Dumper($selected_values->{'unselected-values'}), Dumper(['this']));

my $text = Gtk2::TextView->new;

my $vbox = Gtk2::VBox->new (FALSE, 0);
$vbox->pack_start ($hbox, FALSE, TRUE, 0);
$vbox->pack_start ($text, TRUE, TRUE, 0);
$window->add ($vbox);
$window->set_default_size(500, 200);
$window->show_all;

$combobox->show;

t/test.5.t  view on Meta::CPAN


use Gtk2::TestHelper tests => 7;

my $window = Gtk2::Window->new;
$window->signal_connect('destroy', sub {Gtk2->main_quit;});

my $label = Gtk2::Label->new('With-Buttons');
my $hbox = Gtk2::HBox->new (FALSE, 10);
$hbox->pack_start ($label, FALSE, TRUE, 0);    

my $combobox = Gtk2::Ex::ComboBox->new($label, 'with-checkbox');
isa_ok($combobox, "Gtk2::Ex::ComboBox");
ok(!$combobox->set_list_preselected([[0,'this'], [1,'that'], [1,'what']]));

my $slist = $combobox->get_treeview;
isa_ok($slist, "Gtk2::Ex::Simple::List");

my $selected_indices = $combobox->get_selected_indices;
is(Dumper($selected_indices->{'selected-indices'}), Dumper([1,2]));
is(Dumper($selected_indices->{'unselected-indices'}), Dumper([0]));

my $selected_values = $combobox->get_selected_values;
is(Dumper($selected_values->{'selected-values'}), Dumper(['that', 'what']));
is(Dumper($selected_values->{'unselected-values'}), Dumper(['this']));

my $text = Gtk2::TextView->new;

my $vbox = Gtk2::VBox->new (FALSE, 0);
$vbox->pack_start ($hbox, FALSE, TRUE, 0);
$vbox->pack_start ($text, TRUE, TRUE, 0);
$window->add ($vbox);
$window->set_default_size(500, 200);
$window->show_all;

$combobox->show;

t/test.6.t  view on Meta::CPAN


use Gtk2::TestHelper tests => 7;

my $window = Gtk2::Window->new;
$window->signal_connect('destroy', sub {Gtk2->main_quit;});

my $label = Gtk2::Label->new('With-Buttons');
my $hbox = Gtk2::HBox->new (FALSE, 10);
$hbox->pack_start ($label, FALSE, TRUE, 0);    

my $combobox = Gtk2::Ex::ComboBox->new($label, 'no-checkbox');
isa_ok($combobox, "Gtk2::Ex::ComboBox");
ok(!$combobox->set_list_preselected([[0,'this'], [1,'that'], [1,'what']]));

my $slist = $combobox->get_treeview;
isa_ok($slist, "Gtk2::Ex::Simple::List");

my $selected_indices = $combobox->get_selected_indices;
is(Dumper($selected_indices->{'selected-indices'}), Dumper([1,2]));
is(Dumper($selected_indices->{'unselected-indices'}), Dumper([0]));

my $selected_values = $combobox->get_selected_values;
is(Dumper($selected_values->{'selected-values'}), Dumper(['that', 'what']));
is(Dumper($selected_values->{'unselected-values'}), Dumper(['this']));

my $text = Gtk2::TextView->new;

my $vbox = Gtk2::VBox->new (FALSE, 0);
$vbox->pack_start ($hbox, FALSE, TRUE, 0);
$vbox->pack_start ($text, TRUE, TRUE, 0);
$window->add ($vbox);
$window->set_default_size(500, 200);
$window->show_all;

$combobox->show;



( run in 1.822 second using v1.01-cache-2.11-cpan-8dfa8b56332 )