File: Multiselect.pm

package info (click to toggle)
debconf 1.5.11etch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 3,364 kB
  • ctags: 714
  • sloc: perl: 8,347; sh: 286; makefile: 174; python: 117
file content (113 lines) | stat: -rw-r--r-- 2,875 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl -w

=head1 NAME

Debconf::Element::Gnome::Multiselect - a check list in a dialog box

=cut

package Debconf::Element::Gnome::Multiselect;
use strict;
use Gtk2;
use utf8;
use Debconf::Encoding qw(to_Unicode);
use base qw(Debconf::Element::Gnome Debconf::Element::Multiselect);

use constant SELECTED_COLUMN => 0;
use constant CHOICES_COLUMN  => 1;

sub init {
	my $this=shift;
	my @choices = map { to_Unicode($_) } $this->question->choices_split;
        my %default=map { to_Unicode($_) => 1 } $this->translate_default;

	$this->SUPER::init(@_);
	$this->multiline(1);

	$this->adddescription;

        $this->widget(Gtk2::ScrolledWindow->new);
        $this->widget->show;
        $this->widget->set_policy('automatic', 'automatic');
	
	my $list_store = Gtk2::ListStore->new('Glib::Boolean', 'Glib::String');
	$this->list_view(Gtk2::TreeView->new($list_store));
	$this->list_view->set_headers_visible(0);

	my $renderer_toggle = Gtk2::CellRendererToggle->new;
	$renderer_toggle->signal_connect(toggled => sub {
		my $path_string = $_[1];
		my $model = $_[2];
		my $iter = $model->get_iter_from_string($path_string);
		$model->set($iter, SELECTED_COLUMN,
		            not $model->get($iter, SELECTED_COLUMN));
	}, $list_store);

	$this->list_view->append_column(
		Gtk2::TreeViewColumn->new_with_attributes('Selected',
			$renderer_toggle, 'active', SELECTED_COLUMN));
	$this->list_view->append_column(
		Gtk2::TreeViewColumn->new_with_attributes('Choices',
			Gtk2::CellRendererText->new, 'text', CHOICES_COLUMN)); 
	$this->list_view->show;

	$this->widget->add($this->list_view);

	for (my $i=0; $i <= $#choices; $i++) {
		my $iter = $list_store->append();
		$list_store->set($iter, CHOICES_COLUMN, $choices[$i]);
		if ($default{$choices[$i]}) {
			$list_store->set($iter, SELECTED_COLUMN, 1);
		}
	}
	$this->addwidget($this->widget);
	$this->tip($this->list_view);
	$this->addhelp;

	# we want to be both expanded and filled
	$this->fill(1);
	$this->expand(1);

}

=item value

The value is just the value field of the widget, translated back to the C
locale.

=cut

sub value {
	my $this=shift;
	my $list_view = $this->list_view;
	my $list_store = $list_view->get_model ();
	my ($ret, $val);
	
	my @vals;
	# we need untranslated templates for this
	$this->question->template->i18n('');
	my @choices=$this->question->choices_split;
	$this->question->template->i18n(1);
	
	my $iter = $list_store->get_iter_first();
	for (my $i=0; $i <= $#choices; $i++) {
		if ($list_store->get($iter, SELECTED_COLUMN)) {
			push @vals, $choices[$i];
		}
		$iter = $list_store->iter_next($iter);
	}

	return join(', ', $this->order_values(@vals));
}

# Multiple inheritance means we get Debconf::Element::visible by default.
*visible = \&Debconf::Element::Multiselect::visible;

=head1 AUTHOR

Eric Gillespie <epg@debian.org>
Gustavo Noronha Silva <kov@debian.org>

=cut

1