| 1 | package Tk::Wizard::Choices; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use warnings::register; | ||
| 6 | |||
| 7 | use vars '$VERSION'; | ||
| 8 | $VERSION = do { my @r = ( q$Revision: 2.77 $ =~ /\d+/g ); sprintf "%d." . "%03d" x $#r, @r }; | ||
| 9 | |||
| 10 | use Carp (); | ||
| 11 | |||
| 12 | # use Scalar::Util qw( reftype ); | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Tk::Wizard::Choices - C<Tk::Wizard> pages to collect end-user choices | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | Currently automatically loaded by C<Tk::Wizard>, though this | ||
| 21 | behaviour is deprecated and is expected to change in 2008. | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Adds a number of methods to C<Tk::Wizard>, to collect choices made | ||
| 26 | by the end-user. | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =head2 addMultipleChoicePage | ||
| 31 | |||
| 32 | Allow the user to make multiple choices among several options: | ||
| 33 | each choice sets a variable passed as reference to this method. | ||
| 34 | |||
| 35 | Accepts the usual parameters plus: | ||
| 36 | |||
| 37 | =over 4 | ||
| 38 | |||
| 39 | =item -relief | ||
| 40 | |||
| 41 | For the checkbox buttons - see L<Tk::options>. | ||
| 42 | |||
| 43 | =item -choices | ||
| 44 | |||
| 45 | A reference to an array of hashes with the following fields: | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item -title | ||
| 50 | |||
| 51 | Title of the option, will be rendered in bold | ||
| 52 | |||
| 53 | =item -subtitle | ||
| 54 | |||
| 55 | Text rendered smaller beneath the title | ||
| 56 | |||
| 57 | =item -variable | ||
| 58 | |||
| 59 | Reference to a variable that will contain the result of the choice. | ||
| 60 | Croaks if none supplied. Your -variable will contain the default | ||
| 61 | L<Tk::Checkbutton|Tk::Checkbutton> values of 1 for checked and 0 for | ||
| 62 | unchecked. | ||
| 63 | |||
| 64 | =item -checked | ||
| 65 | |||
| 66 | Pass a true value to specify that the box should initially | ||
| 67 | appear checked. | ||
| 68 | |||
| 69 | =back | ||
| 70 | |||
| 71 | Here is an example of what the -choices parameter should look like: | ||
| 72 | |||
| 73 | $wizard->addMultipleChoicePage( | ||
| 74 | -title => "Another toy example", | ||
| 75 | -choices => | ||
| 76 | [ | ||
| 77 | { | ||
| 78 | -title => 'choice 1', | ||
| 79 | -variable => \$choice1, | ||
| 80 | }, | ||
| 81 | { | ||
| 82 | -title => 'choice 2, default is checked', | ||
| 83 | -variable => \$choice2, | ||
| 84 | -checked => 1, | ||
| 85 | }, | ||
| 86 | ], | ||
| 87 | ); | ||
| 88 | |||
| 89 | =back | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub Tk::Wizard::addMultipleChoicePage { | ||
| 94 | my $self = shift; | ||
| 95 | my $args = {@_}; | ||
| 96 | # return $self->addPage( sub { $self->_page_multiple_choice($args) } ); | ||
| 97 | |||
| 98 | my %btn_args = | ||
| 99 | map { my $x = delete $args->{$_}; $_ => $x } | ||
| 100 | grep { /ButtonAction$/ } | ||
| 101 | keys %$args; | ||
| 102 | return $self->addPage( sub { $self->_page_multiple_choice($args) }, %btn_args ); | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | sub Tk::Wizard::_page_multiple_choice { | ||
| 107 | my $self = shift; | ||
| 108 | my $args = shift; | ||
| 109 | my $frame = $self->blank_frame(%$args); | ||
| 110 | if ( !ref( $args->{-choices} ) || ( ref( $args->{-choices} ) ne 'ARRAY' ) ) { | ||
| 111 | Carp::croak "-choices should be a ref to an array!"; | ||
| 112 | } | ||
| 113 | |||
| 114 | my $content = $frame->Frame( -background => $self->{background}, )->pack( | ||
| 115 | -side => 'top', | ||
| 116 | -anchor => "n", | ||
| 117 | -padx => 10, | ||
| 118 | -pady => 10, | ||
| 119 | ); | ||
| 120 | |||
| 121 | foreach my $opt ( @{ $args->{-choices} } ) { | ||
| 122 | Carp::croak "Option in -choices array is not a hash!" if not ref $opt or ref $opt ne 'HASH'; | ||
| 123 | Carp::croak "No -variable!" if not $opt->{-variable}; | ||
| 124 | Carp::croak "-variable should be a reference!" if not ref $opt->{-variable}; | ||
| 125 | |||
| 126 | my $b = $content->Checkbutton( | ||
| 127 | -text => $opt->{-title}, | ||
| 128 | -justify => 'left', | ||
| 129 | -relief => $args->{-relief} || 'flat', | ||
| 130 | -font => "RADIO_BOLD", | ||
| 131 | -variable => $opt->{-variable}, | ||
| 132 | -background => $self->{background}, | ||
| 133 | -activebackground => $self->{background}, | ||
| 134 | )->pack(qw/-side top -anchor w /); | ||
| 135 | |||
| 136 | $b->invoke if $opt->{-checked}; | ||
| 137 | |||
| 138 | my $s = $opt->{-subtitle} || ''; | ||
| 139 | |||
| 140 | # Seven spaces indentation is perfect with my Windows XP | ||
| 141 | # default font: | ||
| 142 | $s =~ s!(^|\n)!$1 !g; | ||
| 143 | my $l = $content->Label( | ||
| 144 | -font => $self->{defaultFont}, | ||
| 145 | -text => $s, | ||
| 146 | -anchor => 'w', | ||
| 147 | -justify => 'left', | ||
| 148 | -background => $self->{background}, | ||
| 149 | )->pack(qw/-side top -anchor w/); | ||
| 150 | |||
| 151 | # DEBUG_FRAME && $l->configure( -background => 'light blue' ); | ||
| 152 | } | ||
| 153 | |||
| 154 | return $frame; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | =head2 addSingleChoicePage | ||
| 159 | |||
| 160 | Allow the user to make one choice from among several options | ||
| 161 | (i.e. a group of radio buttons). | ||
| 162 | Each choice sets a variable passed as reference to this method. | ||
| 163 | |||
| 164 | Accepts the usual parameters plus: | ||
| 165 | |||
| 166 | =over 4 | ||
| 167 | |||
| 168 | =item -relief | ||
| 169 | |||
| 170 | For the radio buttons - see L<Tk::options>. | ||
| 171 | |||
| 172 | =item -variable | ||
| 173 | |||
| 174 | Reference to a variable that will contain the result of the choice. | ||
| 175 | Croaks if none supplied. Your -variable will contain the -value of the | ||
| 176 | radio button that is selected when the user clicks "Next". | ||
| 177 | |||
| 178 | =item -choices | ||
| 179 | |||
| 180 | A reference to an array of hashes with the following fields: | ||
| 181 | |||
| 182 | =over 4 | ||
| 183 | |||
| 184 | =item -title | ||
| 185 | |||
| 186 | Title of the option, will be rendered in bold | ||
| 187 | |||
| 188 | =item -subtitle | ||
| 189 | |||
| 190 | Text rendered smaller beneath the title | ||
| 191 | |||
| 192 | =item -value | ||
| 193 | |||
| 194 | This value will be placed in your -variable variable if this button is | ||
| 195 | selected | ||
| 196 | |||
| 197 | =item -selected | ||
| 198 | |||
| 199 | Pass a true value to specify that this radio should initially appear | ||
| 200 | selected. If none of the choices have -selected, then the first | ||
| 201 | choice will be selected by default. | ||
| 202 | |||
| 203 | =back | ||
| 204 | |||
| 205 | Here is an example of what the -choices parameter should look like: | ||
| 206 | |||
| 207 | $wizard->addSingleChoicePage( | ||
| 208 | -title => 'Another toy example', | ||
| 209 | -text => 'Choose one of the following:', | ||
| 210 | -variable => \$choice, | ||
| 211 | -choices => | ||
| 212 | [ | ||
| 213 | { | ||
| 214 | -title => 'choice 1', | ||
| 215 | -value => 1, | ||
| 216 | }, | ||
| 217 | { | ||
| 218 | -title => 'choice two, default this one selected', | ||
| 219 | -value => 'two', | ||
| 220 | -selected => 1, | ||
| 221 | }, | ||
| 222 | ], | ||
| 223 | ); | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub Tk::Wizard::addSingleChoicePage { | ||
| 230 | my $self = shift; | ||
| 231 | |||
| 232 | # We have to make a copy of our args in order for them to get | ||
| 233 | # "saved" in this coderef: | ||
| 234 | my $args = {@_}; | ||
| 235 | |||
| 236 | # return $self->addPage( sub { $self->_page_single_choice($args) } ); | ||
| 237 | my %btn_args = | ||
| 238 | map { my $x = delete $args->{$_}; $_ => $x } | ||
| 239 | grep { /ButtonAction$/ } | ||
| 240 | keys %$args; | ||
| 241 | return $self->addPage( sub { $self->_page_single_choice($args) }, %btn_args ); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub Tk::Wizard::_page_single_choice { | ||
| 245 | my $self = shift; | ||
| 246 | my $args = shift; | ||
| 247 | my $not_first_page = 0; | ||
| 248 | |||
| 249 | if (not defined( $args->{-choices} ) ) { | ||
| 250 | Carp::croak "-choices argument missing"; | ||
| 251 | } | ||
| 252 | if (not ref( $args->{-choices} ) or ( ref( $args->{-choices} ) ne 'ARRAY' ) ) { | ||
| 253 | Carp::croak "-choices must be a ref to an array!"; | ||
| 254 | } | ||
| 255 | Carp::croak "-variable argument missing" if !defined( $args->{-variable} ); | ||
| 256 | Carp::croak "-variable must be a reference!" if !ref $args->{-variable}; | ||
| 257 | |||
| 258 | # Take care of the -title, -text, etc.: | ||
| 259 | my $frame = $self->blank_frame(%$args); | ||
| 260 | my $content = $frame->Frame( -background => $self->{background}, )->pack( | ||
| 261 | -side => 'top', | ||
| 262 | -anchor => "n", | ||
| 263 | -padx => 10, | ||
| 264 | -pady => 10, | ||
| 265 | ); | ||
| 266 | |||
| 267 | foreach my $opt ( @{ $args->{-choices} } ) { | ||
| 268 | if ( ref $opt ne 'HASH' ) { | ||
| 269 | Carp::croak "Option in -choices array must be a hash"; | ||
| 270 | } | ||
| 271 | $opt->{-title} ||= ''; | ||
| 272 | my $sValue = defined($opt->{-value}) ? $opt->{-value} : $opt->{-title}; | ||
| 273 | my $b = $content->Radiobutton( | ||
| 274 | -text => $opt->{-title}, | ||
| 275 | -justify => 'left', | ||
| 276 | -relief => $args->{-relief} || 'flat', | ||
| 277 | -font => "RADIO_BOLD", | ||
| 278 | -variable => $args->{-variable}, | ||
| 279 | -value => $sValue, | ||
| 280 | -background => $self->{background}, | ||
| 281 | -activebackground => $self->{background}, | ||
| 282 | )->pack(qw/-side top -anchor w /); | ||
| 283 | |||
| 284 | ${ $args->{-variable} } ||= $sValue if not $not_first_page++; | ||
| 285 | ${ $args->{-variable} } = $sValue if $opt->{-selected}; | ||
| 286 | |||
| 287 | my $s = $opt->{-subtitle} || ''; | ||
| 288 | |||
| 289 | # Seven spaces indentation is perfect with my Windows XP default font: | ||
| 290 | if ( $s ne '' ) { | ||
| 291 | $s =~ s!(^|\n)!$1 !g; | ||
| 292 | my $l = $content->Label( | ||
| 293 | -font => $self->{defaultFont}, | ||
| 294 | -text => $s, | ||
| 295 | -anchor => 'w', | ||
| 296 | -justify => 'left', | ||
| 297 | -background => $self->{background}, | ||
| 298 | )->pack(qw/-side top -anchor w/); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | return $frame; | ||
| 303 | } | ||
| 304 | |||
| 305 | 1; | ||
| 306 | |||
| 307 | =head1 AUTHOR | ||
| 308 | |||
| 309 | Lee Goddard (lgoddard@cpan.org). | ||
| 310 | |||
| 311 | =head1 COPYRIGHT | ||
| 312 | |||
| 313 | Copyright (C) Lee Goddard, 11/2002 - 01/2008 ff. | ||
| 314 | |||
| 315 | Made available under the same terms as Perl itself. |