| 1 | package Gtk2::Ex::FormFactory::List; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | use base qw( Gtk2::Ex::FormFactory::Widget ); | ||
| 7 | |||
| 8 | sub get_type { "list" } | ||
| 9 | |||
| 10 | sub get_attr_select { shift->{attr_select} } | ||
| 11 | sub get_attr_select_column { shift->{attr_select_column} } | ||
| 12 | sub get_update_selection_only { shift->{update_selection_only} } | ||
| 13 | sub get_columns { shift->{columns} } | ||
| 14 | sub get_types { shift->{types} } | ||
| 15 | sub get_visible { shift->{visible} } | ||
| 16 | sub get_editable { shift->{editable} } | ||
| 17 | sub get_selection_mode { shift->{selection_mode} } | ||
| 18 | sub get_is_editable { shift->{is_editable} } | ||
| 19 | sub get_selection_backup { shift->{selection_backup} } | ||
| 20 | sub get_no_header { shift->{no_header} } | ||
| 21 | sub get_last_applied_selection { shift->{last_applied_selection} } | ||
| 22 | |||
| 23 | sub set_attr_select { shift->{attr_select} = $_[1] } | ||
| 24 | sub set_attr_select_column { shift->{attr_select_column} = $_[1] } | ||
| 25 | sub set_update_selection_only { shift->{update_selection_only}= $_[1] } | ||
| 26 | sub set_columns { shift->{columns} = $_[1] } | ||
| 27 | sub set_types { shift->{types} = $_[1] } | ||
| 28 | sub set_visible { shift->{visible} = $_[1] } | ||
| 29 | sub set_editable { shift->{editable} = $_[1] } | ||
| 30 | sub set_selection_mode { shift->{selection_mode} = $_[1] } | ||
| 31 | sub set_is_editable { shift->{is_editable} = $_[1] } | ||
| 32 | sub set_selection_backup { shift->{selection_backup} = $_[1] } | ||
| 33 | sub set_no_header { shift->{no_header} = $_[1] } | ||
| 34 | sub set_last_applied_selection { shift->{last_applied_selection}= $_[1]} | ||
| 35 | |||
| 36 | sub has_additional_attrs { [ "select" ] } | ||
| 37 | |||
| 38 | sub get_selected_rows { | ||
| 39 | my $self = shift; | ||
| 40 | my @sel = $self->get_gtk_widget->get_selected_indices; | ||
| 41 | return \@sel; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub get_data { | ||
| 45 | my $self = shift; | ||
| 46 | return $self->get_gtk_widget->{data}; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub select_row_by_attr { | ||
| 50 | my $self = shift; | ||
| 51 | my ($attr_value) = @_; | ||
| 52 | |||
| 53 | my $column = $self->get_attr_select_column; | ||
| 54 | my $data = $self->get_data; | ||
| 55 | |||
| 56 | for ( my $i=0; $i < @{$data}; ++$i ) { | ||
| 57 | if ( $data->[$i][$column] eq $attr_value ) { | ||
| 58 | $self->get_gtk_widget->select($i); | ||
| 59 | last; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub new { | ||
| 67 | my $class = shift; | ||
| 68 | my %par = @_; | ||
| 69 | my ($attr_select, $columns, $types, $editable, $visible) = | ||
| 70 | @par{'attr_select','columns','types','editable','visible'}; | ||
| 71 | my ($update_selection_only, $selection_mode) = | ||
| 72 | @par{'update_selection_only','selection_mode'}; | ||
| 73 | my ($attr_select_column, $no_header) = | ||
| 74 | @par{'attr_select_column','no_header'}; | ||
| 75 | |||
| 76 | croak "'columns' attribute is mandatory" unless $columns; | ||
| 77 | |||
| 78 | my $self = $class->SUPER::new(@_); | ||
| 79 | |||
| 80 | $self->set_attr_select ($attr_select); | ||
| 81 | $self->set_attr_select_column ($attr_select_column); | ||
| 82 | $self->set_columns ($columns); | ||
| 83 | $self->set_visible ($visible); | ||
| 84 | $self->set_types ($types); | ||
| 85 | $self->set_editable ($editable); | ||
| 86 | $self->set_selection_mode ($selection_mode); | ||
| 87 | $self->set_update_selection_only($update_selection_only); | ||
| 88 | $self->set_no_header ($no_header); | ||
| 89 | |||
| 90 | my $is_editable = 0; | ||
| 91 | map { $is_editable = 1 if $_ } @{$editable}; | ||
| 92 | |||
| 93 | $self->set_is_editable($is_editable); | ||
| 94 | |||
| 95 | return $self; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub object_to_widget { | ||
| 99 | my $self = shift; | ||
| 100 | |||
| 101 | my $object_value = $self->get_object_value || []; | ||
| 102 | |||
| 103 | my $slist_was_empty = ! scalar(@{$self->get_gtk_widget->{data}}); | ||
| 104 | |||
| 105 | if ( not $self->get_update_selection_only ) { | ||
| 106 | $self->get_gtk_widget | ||
| 107 | ->set_data_array($object_value); | ||
| 108 | } else { | ||
| 109 | $self->get_gtk_widget | ||
| 110 | ->set_data_array($object_value) | ||
| 111 | if @{$self->get_gtk_widget->{data}} == 0; | ||
| 112 | } | ||
| 113 | |||
| 114 | if ( $self->get_attr_select ) { | ||
| 115 | my $proxy = $self->get_proxy; | ||
| 116 | my $idx = $proxy->get_attr ( | ||
| 117 | $self->get_attr_select | ||
| 118 | ); | ||
| 119 | my $sel; | ||
| 120 | $sel = join("\t",@{$idx}) if $idx; | ||
| 121 | $self->set_last_applied_selection($sel); | ||
| 122 | my $gtk_simple_list = $self->get_gtk_widget; | ||
| 123 | if ( defined $idx and @{$idx} and @{$self->get_gtk_widget->{data}} != 0 ) { | ||
| 124 | if ( defined $self->get_attr_select_column ) { | ||
| 125 | my $i = 0; | ||
| 126 | my $col = $self->get_attr_select_column; | ||
| 127 | my $data = $self->get_gtk_widget->{data}; | ||
| 128 | my %col2idx = map { ($_->[$col], $i++) } @{$data}; | ||
| 129 | my @idx = map { $col2idx{$_} } @{$idx}; | ||
| 130 | $idx = \@idx; | ||
| 131 | } | ||
| 132 | |||
| 133 | if ( @{$idx} && defined $idx->[0] ) { | ||
| 134 | $gtk_simple_list->select(@{$idx}); | ||
| 135 | $gtk_simple_list->scroll_to_cell( | ||
| 136 | Gtk2::TreePath->new_from_string($idx->[0]), | ||
| 137 | ($gtk_simple_list->get_columns)[0], | ||
| 138 | 0, 0 | ||
| 139 | ) if $slist_was_empty; | ||
| 140 | } else { | ||
| 141 | $gtk_simple_list->get_selection->unselect_all; | ||
| 142 | $proxy->set_attr($self->get_attr_select, undef); | ||
| 143 | } | ||
| 144 | } else { | ||
| 145 | $gtk_simple_list->get_selection->unselect_all; | ||
| 146 | } | ||
| 147 | |||
| 148 | Glib::Idle->add (sub { | ||
| 149 | $self->widget_selection_to_object; | ||
| 150 | 0; | ||
| 151 | }); | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub widget_to_object { | ||
| 158 | my $self = shift; | ||
| 159 | |||
| 160 | if ( $self->get_is_editable ) { | ||
| 161 | my $data = $self->get_gtk_widget->{data}; | ||
| 162 | my @value = @{$data}; | ||
| 163 | $self->set_object_value (\@value); | ||
| 164 | } | ||
| 165 | |||
| 166 | $self->widget_selection_to_object | ||
| 167 | if $self->get_attr_select; | ||
| 168 | |||
| 169 | 1; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub widget_selection_to_object { | ||
| 173 | my $self = shift; | ||
| 174 | |||
| 175 | return 1 if ! $self->get_proxy->get_object($self->get_object); | ||
| 176 | |||
| 177 | my @sel = $self->get_gtk_widget->get_selected_indices; | ||
| 178 | |||
| 179 | if ( defined $self->get_attr_select_column ) { | ||
| 180 | my $column = $self->get_attr_select_column; | ||
| 181 | my $data = $self->get_gtk_widget->{data}; | ||
| 182 | $_ = $data->[$_][$column] for @sel; | ||
| 183 | } | ||
| 184 | |||
| 185 | my $sel = join("\t",@sel); | ||
| 186 | return if $sel eq $self->get_last_applied_selection; | ||
| 187 | $self->set_last_applied_selection($sel); | ||
| 188 | |||
| 189 | $self->get_proxy->set_attr ( | ||
| 190 | $self->get_attr_select, \@sel | ||
| 191 | ); | ||
| 192 | |||
| 193 | 1; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub empty_widget { | ||
| 197 | my $self = shift; | ||
| 198 | |||
| 199 | $self->get_gtk_widget->set_data_array([]); | ||
| 200 | $self->get_gtk_widget->get_selection->unselect_all; | ||
| 201 | |||
| 202 | 1; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub backup_widget_value { | ||
| 206 | my $self = shift; | ||
| 207 | |||
| 208 | if ( $self->get_is_editable ) { | ||
| 209 | my $data = $self->get_gtk_widget->{data}; | ||
| 210 | my @value = @{$data}; | ||
| 211 | $self->set_backup_widget_value (\@value); | ||
| 212 | } | ||
| 213 | |||
| 214 | if ( $self->get_attr_select ) { | ||
| 215 | my @sel = $self->get_gtk_widget->get_selected_indices; | ||
| 216 | $self->set_selection_backup(\@sel); | ||
| 217 | } | ||
| 218 | |||
| 219 | 1; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub restore_widget_value { | ||
| 223 | my $self = shift; | ||
| 224 | |||
| 225 | if ( $self->get_is_editable ) { | ||
| 226 | $self->get_gtk_widget | ||
| 227 | ->set_data_array($self->get_backup_widget_value||[]); | ||
| 228 | } | ||
| 229 | |||
| 230 | if ( $self->get_attr_select ) { | ||
| 231 | my $idx = $self->get_selection_backup; | ||
| 232 | $self->get_gtk_widget->select(@{$idx}); | ||
| 233 | } | ||
| 234 | |||
| 235 | 1; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub get_widget_check_value { | ||
| 239 | $_[0]->get_gtk_widget->{data}; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub connect_changed_signal { | ||
| 243 | my $self = shift; | ||
| 244 | |||
| 245 | if ( $self->get_is_editable ) { | ||
| 246 | $self->get_gtk_widget->get_model->signal_connect ( | ||
| 247 | 'row-changed' => sub { $self->widget_value_changed }, | ||
| 248 | ); | ||
| 249 | } | ||
| 250 | |||
| 251 | if ( $self->get_attr_select ) { | ||
| 252 | $self->get_gtk_widget->get_selection->signal_connect ( | ||
| 253 | 'changed' => sub { $self->widget_value_changed }, | ||
| 254 | ); | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | 1; | ||
| 259 | } | ||
| 260 | |||
| 261 | 1; | ||
| 262 | |||
| 263 | __END__ |