| 1 | package Gtk2::Ex::FormFactory::CheckButtonGroup; | ||
| 2 | |||
| 3 | use Carp; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base qw( Gtk2::Ex::FormFactory::Widget ); | ||
| 7 | use POSIX qw(ceil); | ||
| 8 | |||
| 9 | sub get_type { "check_button_group" } | ||
| 10 | |||
| 11 | sub get_max_columns { shift->{max_columns} } | ||
| 12 | sub get_max_rows { shift->{max_rows} } | ||
| 13 | sub get_attr_max_columns { shift->{attr_max_columns} } | ||
| 14 | sub get_attr_max_rows { shift->{attr_max_rows} } | ||
| 15 | sub get_column_labels { shift->{column_labels} } | ||
| 16 | sub get_attr_column_labels { shift->{attr_column_labels} } | ||
| 17 | sub get_row_labels { shift->{row_labels} } | ||
| 18 | sub get_attr_row_labels { shift->{attr_row_labels} } | ||
| 19 | sub get_homogeneous { shift->{homogeneous} } | ||
| 20 | |||
| 21 | sub set_max_columns { shift->{max_columns} = $_[1] } | ||
| 22 | sub set_max_rows { shift->{max_rows} = $_[1] } | ||
| 23 | sub set_attr_max_columns { shift->{attr_max_columns} = $_[1] } | ||
| 24 | sub set_attr_max_rows { shift->{attr_max_rows} = $_[1] } | ||
| 25 | sub set_column_labels { shift->{column_labels} = $_[1] } | ||
| 26 | sub set_attr_column_labels { shift->{attr_column_labels} = $_[1] } | ||
| 27 | sub set_row_labels { shift->{row_labels} = $_[1] } | ||
| 28 | sub set_attr_row_labels { shift->{attr_row_labels} = $_[1] } | ||
| 29 | sub set_homogeneous { shift->{homogeneous} = $_[1] } | ||
| 30 | |||
| 31 | sub get_gtk_check_buttons { shift->{gtk_check_buttons} } | ||
| 32 | sub get_gtk_table { shift->{gtk_table} } | ||
| 33 | |||
| 34 | sub set_gtk_check_buttons { shift->{gtk_check_buttons} = $_[1] } | ||
| 35 | sub set_gtk_table { shift->{gtk_table} = $_[1] } | ||
| 36 | |||
| 37 | sub get_last_toggled_value { shift->{last_toggled_value} } | ||
| 38 | sub set_last_toggled_value { shift->{last_toggled_value} = $_[1] } | ||
| 39 | |||
| 40 | sub get_in_selection_update { shift->{in_selection_update} } | ||
| 41 | sub set_in_selection_update { shift->{in_selection_update} = $_[1] } | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my $class = shift; | ||
| 45 | my %par = @_; | ||
| 46 | my ($max_columns, $max_rows, $attr_max_columns, $attr_max_rows) = | ||
| 47 | @par{'max_columns','max_rows','attr_max_columns','attr_max_rows'}; | ||
| 48 | my ($column_labels, $attr_column_labels, $row_labels) = | ||
| 49 | @par{'column_labels','attr_column_labels','row_labels'}; | ||
| 50 | my ($attr_row_labels, $homogeneous) = | ||
| 51 | @par{'attr_row_labels','homogeneous'}; | ||
| 52 | |||
| 53 | my $self = $class->SUPER::new(@_); | ||
| 54 | |||
| 55 | $max_rows = 1 if ($max_rows == 0 && $max_columns == 0) && | ||
| 56 | !($attr_max_rows || $attr_max_columns); | ||
| 57 | |||
| 58 | $homogeneous = 1 if not defined $homogeneous; | ||
| 59 | |||
| 60 | $self->set_max_columns ($max_columns); | ||
| 61 | $self->set_max_rows ($max_rows); | ||
| 62 | $self->set_attr_max_columns ($attr_max_columns); | ||
| 63 | $self->set_attr_max_rows ($attr_max_rows); | ||
| 64 | $self->set_column_labels ($column_labels); | ||
| 65 | $self->set_attr_column_labels ($attr_column_labels); | ||
| 66 | $self->set_row_labels ($row_labels); | ||
| 67 | $self->set_attr_row_labels ($attr_row_labels); | ||
| 68 | $self->set_homogeneous ($homogeneous); | ||
| 69 | |||
| 70 | return $self; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub cleanup { | ||
| 74 | my $self = shift; | ||
| 75 | |||
| 76 | $self->SUPER::cleanup(@_); | ||
| 77 | |||
| 78 | $self->set_gtk_check_buttons(undef); | ||
| 79 | $self->set_gtk_table(undef); | ||
| 80 | |||
| 81 | 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub object_to_widget { | ||
| 85 | my $self = shift; | ||
| 86 | |||
| 87 | #-- $checkboxes = [ [0, "Sun"], [1 ,"Mon"], [2,"Tue"], ... ] | ||
| 88 | my $checkboxes = $self->get_proxy->get_attr_list( | ||
| 89 | $self->get_attr, $self->get_name | ||
| 90 | ); | ||
| 91 | |||
| 92 | #-- $selected_href = { 0 => 1, 2 => 1 } - Sun and Tue are selected | ||
| 93 | my $selected_href = $self->get_object_value; | ||
| 94 | |||
| 95 | my $hbox = $self->get_gtk_widget; | ||
| 96 | my @children = $hbox->get_children; | ||
| 97 | $hbox->remove($_) for @children; | ||
| 98 | |||
| 99 | my ($rows, $columns); | ||
| 100 | my $max_rows = $self->get_max_rows; | ||
| 101 | my $max_columns = $self->get_max_columns; | ||
| 102 | my $row_labels = $self->get_row_labels; | ||
| 103 | my $column_labels = $self->get_column_labels; | ||
| 104 | |||
| 105 | my $cnt = @{$checkboxes}; | ||
| 106 | |||
| 107 | if ( $self->get_attr_max_rows ) { | ||
| 108 | $max_rows = $self->get_proxy->get_attr($self->get_attr_max_rows); | ||
| 109 | } elsif ( $self->get_attr_max_columns ) { | ||
| 110 | $max_columns = $self->get_proxy->get_attr($self->get_attr_max_columns); | ||
| 111 | } | ||
| 112 | |||
| 113 | if ( $self->get_attr_row_labels ) { | ||
| 114 | $row_labels = $self->get_proxy->get_attr($self->get_attr_row_labels); | ||
| 115 | } | ||
| 116 | |||
| 117 | if ( $self->get_attr_column_labels ) { | ||
| 118 | $column_labels = $self->get_proxy->get_attr($self->get_attr_column_labels); | ||
| 119 | } | ||
| 120 | |||
| 121 | if ( $max_rows ) { | ||
| 122 | $rows = $max_rows; | ||
| 123 | $rows = $cnt if $rows > $cnt; | ||
| 124 | $columns = ceil($cnt / $rows); | ||
| 125 | } else { | ||
| 126 | $columns = $max_columns; | ||
| 127 | $columns = $cnt if $columns > $cnt; | ||
| 128 | $rows = ceil($cnt / $columns); | ||
| 129 | } | ||
| 130 | |||
| 131 | my %gtk_check_buttons; | ||
| 132 | my $gtk_table = Gtk2::Table->new ($rows, $columns); | ||
| 133 | $gtk_table->set ( homogeneous => $self->get_homogeneous ); | ||
| 134 | |||
| 135 | ++$columns if $row_labels; | ||
| 136 | ++$rows if $column_labels; | ||
| 137 | |||
| 138 | my $i = 0; | ||
| 139 | for ( my $c=0; $c < $columns && $i < $cnt; ++$c ) { | ||
| 140 | for ( my $r=0; $r < $rows && $i < $cnt; ++$r ) { | ||
| 141 | next if $column_labels && $c == 0 && $r == 0; | ||
| 142 | if ( $row_labels && $c==0 && $r > 0 ) { | ||
| 143 | my $gtk_label = Gtk2::Label->new($row_labels->[$r-1]); | ||
| 144 | $gtk_table->attach_defaults($gtk_label, $c, $c+1, $r, $r+1); | ||
| 145 | next; | ||
| 146 | } | ||
| 147 | if ( $column_labels && $r==0 && $c > 0 ) { | ||
| 148 | my $gtk_label = Gtk2::Label->new($column_labels->[$c-1]); | ||
| 149 | $gtk_table->attach_defaults($gtk_label, $c, $c+1, $r, $r+1); | ||
| 150 | next; | ||
| 151 | } | ||
| 152 | my $checkbox = $checkboxes->[$i]; | ||
| 153 | my $gtk_check_button = Gtk2::CheckButton->new($checkbox->[1]); | ||
| 154 | $gtk_check_buttons{$checkbox->[0]} = $gtk_check_button; | ||
| 155 | $gtk_check_button->set_active(1) if $selected_href->{$checkbox->[0]}; | ||
| 156 | $gtk_table->attach_defaults($gtk_check_button, $c, $c+1, $r, $r+1); | ||
| 157 | ++$i; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | $hbox->pack_start($gtk_table, 0, 1, 0); | ||
| 162 | $hbox->show_all; | ||
| 163 | |||
| 164 | $self->set_gtk_check_buttons(\%gtk_check_buttons); | ||
| 165 | $self->set_gtk_table($gtk_table); | ||
| 166 | |||
| 167 | $self->connect_changed_signal_for_all_buttons; | ||
| 168 | |||
| 169 | 1; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub update_selection { | ||
| 173 | my $self = shift; | ||
| 174 | |||
| 175 | $self->set_in_selection_update(1); | ||
| 176 | |||
| 177 | my $selected_href = $self->get_object_value; | ||
| 178 | my $gtk_check_buttons = $self->get_gtk_check_buttons; | ||
| 179 | |||
| 180 | while ( my ($value, $gtk_check_button) = each %{$gtk_check_buttons} ) { | ||
| 181 | $gtk_check_button->set_active( $selected_href->{$value} ); | ||
| 182 | } | ||
| 183 | |||
| 184 | $self->set_in_selection_update(0); | ||
| 185 | |||
| 186 | 1; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub widget_to_object { | ||
| 190 | my $self = shift; | ||
| 191 | |||
| 192 | return if $self->get_in_selection_update; | ||
| 193 | |||
| 194 | my $gtk_check_buttons = $self->get_gtk_check_buttons; | ||
| 195 | my %selected; | ||
| 196 | |||
| 197 | while ( my ($value, $gtk_check_button) = each %{$gtk_check_buttons} ) { | ||
| 198 | $selected{$value} = 1 if $gtk_check_button->get_active; | ||
| 199 | } | ||
| 200 | |||
| 201 | $self->set_object_value(\%selected); | ||
| 202 | |||
| 203 | 1; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub connect_changed_signal_for_all_buttons { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | my $gtk_check_buttons = $self->get_gtk_check_buttons; | ||
| 210 | |||
| 211 | while ( my ($value, $gtk_check_button) = each %{$gtk_check_buttons} ) { | ||
| 212 | $gtk_check_button->signal_connect ( toggled => sub { | ||
| 213 | return 1 if $self->get_in_selection_update; | ||
| 214 | $self->set_last_toggled_value($value); | ||
| 215 | $self->widget_value_changed; | ||
| 216 | 1; | ||
| 217 | } ); | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; | ||
| 221 | } | ||
| 222 | |||
| 223 | 1; | ||
| 224 | |||
| 225 | __END__ |