Debian Codegraph @

Debconf/Element/Gnome/Multiselect.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::Element::Gnome::Multiselect - a check list in a dialog box
6
7 =cut
8
9 package Debconf::Element::Gnome::Multiselect;
10 use warnings;
11 use strict;
12 use Gtk3;
13 use utf8;
14 use Debconf::Encoding qw(to_Unicode);
15 use base qw(Debconf::Element::Gnome Debconf::Element::Multiselect);
16
17 use constant SELECTED_COLUMN => 0;
18 use constant CHOICES_COLUMN => 1;
19
20 sub init {
21 my $this=shift;
22 my @choices = map { to_Unicode($_) } $this->question->choices_split;
23 my %default=map { to_Unicode($_) => 1 } $this->translate_default;
24
25 $this->SUPER::init(@_);
26 $this->multiline(1);
27
28 $this->adddescription;
29
30 $this->widget(Gtk3::ScrolledWindow->new);
31 $this->widget->show;
32 $this->widget->set_policy('automatic', 'automatic');
33
34 my $list_store = Gtk3::ListStore->new('Glib::Boolean', 'Glib::String');
35 $this->list_view(Gtk3::TreeView->new($list_store));
36 $this->list_view->set_headers_visible(0);
37
38 my $renderer_toggle = Gtk3::CellRendererToggle->new;
39 $renderer_toggle->signal_connect(toggled => sub {
40 my $path_string = $_[1];
41 my $model = $_[2];
42 my $iter = $model->get_iter_from_string($path_string);
43 $model->set($iter, SELECTED_COLUMN,
44 not $model->get($iter, SELECTED_COLUMN));
45 }, $list_store);
46
47 $this->list_view->append_column(
48 Gtk3::TreeViewColumn->new_with_attributes('Selected',
49 $renderer_toggle, 'active', SELECTED_COLUMN));
50 $this->list_view->append_column(
51 Gtk3::TreeViewColumn->new_with_attributes('Choices',
52 Gtk3::CellRendererText->new, 'text', CHOICES_COLUMN));
53 $this->list_view->show;
54
55 $this->widget->add($this->list_view);
56
57 for (my $i=0; $i <= $#choices; $i++) {
58 my $iter = $list_store->append();
59 $list_store->set($iter, CHOICES_COLUMN, $choices[$i]);
60 if ($default{$choices[$i]}) {
61 $list_store->set($iter, SELECTED_COLUMN, 1);
62 }
63 }
64 $this->addwidget($this->widget);
65 $this->tip($this->list_view);
66 $this->addhelp;
67
68 # we want to be both expanded and filled
69 $this->fill(1);
70 $this->expand(1);
71
72 }
73
74 =item value
75
76 The value is just the value field of the widget, translated back to the C
77 locale.
78
79 =cut
80
81 sub value {
82 my $this=shift;
83 my $list_view = $this->list_view;
84 my $list_store = $list_view->get_model();
85 my ($ret, $val);
86
87 my @vals;
88 # we need untranslated templates for this
89 $this->question->template->i18n('');
90 my @choices=$this->question->choices_split;
91 $this->question->template->i18n(1);
92
93 my $iter = $list_store->get_iter_first();
94 for (my $i=0; $i <= $#choices; $i++) {
95 if ($list_store->get($iter, SELECTED_COLUMN)) {
96 push @vals, $choices[$i];
97 }
98 $list_store->iter_next($iter) or last;
99 }
100
101 return join(', ', $this->order_values(@vals));
102 }
103
104 # Multiple inheritance means we get Debconf::Element::visible by default.
105 *visible = \&Debconf::Element::Multiselect::visible;
106
107 =head1 AUTHOR
108
109 Eric Gillespie <[email protected]>
110 Gustavo Noronha Silva <[email protected]>
111
112 =cut
113
114 1