| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | #----------------------------------------------------------------------- | ||
| 5 | # change priorities dialog | ||
| 6 | #----------------------------------------------------------------------- | ||
| 7 | |||
| 8 | package | ||
| 9 | Games::Sudoku::Trainer::GUIprio; | ||
| 10 | |||
| 11 | use version; our $VERSION = qv('0.01'); # PBP | ||
| 12 | |||
| 13 | my $mw; # main window | ||
| 14 | |||
| 15 | # Show and process the priorities list | ||
| 16 | # When done, recreate the priorities list | ||
| 17 | # and the array of strategy function names | ||
| 18 | # Callback of menu Priorities|Change | ||
| 19 | # change_prios($mw); | ||
| 20 | # | ||
| 21 | sub change_prios { | ||
| 22 | $mw = shift; | ||
| 23 | require Tk::DialogBox; | ||
| 24 | my $db = $mw->DialogBox( | ||
| 25 | -title => 'Change Strategy Priorities', | ||
| 26 | -buttons => [qw/Ok Cancel/], | ||
| 27 | -default_button => 'Cancel', | ||
| 28 | ); | ||
| 29 | my $lb = | ||
| 30 | $db->Listbox( -height => 20, -width => 0, -selectmode => 'multiple' ) | ||
| 31 | ->pack(); | ||
| 32 | my $but_fr = $db->Frame()->pack(); | ||
| 33 | $but_fr->Button( | ||
| 34 | -text => 'Fast Up', | ||
| 35 | -command => sub { _move_sel( $lb, -5 ) } | ||
| 36 | )->pack( -side => 'left' ); | ||
| 37 | $but_fr->Button( -text => 'Up', -command => sub { _move_sel( $lb, -1 ) } ) | ||
| 38 | ->pack( -side => 'left' ); | ||
| 39 | $but_fr->Button( | ||
| 40 | -text => 'Down', | ||
| 41 | -command => sub { _move_sel( $lb, +1 ) } | ||
| 42 | )->pack( -side => 'left' ); | ||
| 43 | $but_fr->Button( | ||
| 44 | -text => 'Fast Down', | ||
| 45 | -command => sub { _move_sel( $lb, +5 ) } | ||
| 46 | )->pack( -side => 'left' ); | ||
| 47 | $but_fr->Label()->pack( -side => 'left' ); # separator | ||
| 48 | $but_fr->Button( | ||
| 49 | -text => 'Default', | ||
| 50 | -command => sub { _insert_default($lb) } | ||
| 51 | )->pack( -side => 'left' ); | ||
| 52 | $lb->delete( 0, 'end' ); | ||
| 53 | $lb->insert( 'end', Games::Sudoku::Trainer::Priorities::copy_strats() ); | ||
| 54 | my $answer = $db->Show(); | ||
| 55 | $answer or return; # user hit the kill button of the title bar | ||
| 56 | $answer eq 'Cancel' and return; | ||
| 57 | my @strats = $lb->get( 0, 'end' ); | ||
| 58 | Games::Sudoku::Trainer::Priorities::set_strats( \@strats ); | ||
| 59 | return; | ||
| 60 | } ## end sub change_prios | ||
| 61 | |||
| 62 | # move a block of selected strategies up or down in the Listbox | ||
| 63 | # callback of the Up/Down buttons | ||
| 64 | # | ||
| 65 | sub _move_sel { | ||
| 66 | my ( $lb, $amount ) = @_; | ||
| 67 | my @selidxs = $lb->curselection(); | ||
| 68 | |||
| 69 | @selidxs or do { | ||
| 70 | Games::Sudoku::Trainer::Run::user_err( | ||
| 71 | "Please select the strategies to be moved"); | ||
| 72 | return; | ||
| 73 | }; | ||
| 74 | $selidxs[-1] - $selidxs[0] == $#selidxs or do { | ||
| 75 | Games::Sudoku::Trainer::Run::user_err( | ||
| 76 | "The selection must be contiguous"); | ||
| 77 | return; | ||
| 78 | }; | ||
| 79 | my @movethem = $lb->get( $selidxs[0], $selidxs[-1] ); | ||
| 80 | if ( $amount < 0 ) { | ||
| 81 | my $endpos = $selidxs[0] + $amount; | ||
| 82 | $endpos < 0 and $amount -= $endpos; | ||
| 83 | return if $amount == 0; | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | my $lastpos = $selidxs[-1] + $amount; | ||
| 87 | my $excess = $lastpos - $lb->size() + 1; | ||
| 88 | $excess > 0 and $amount -= $excess; | ||
| 89 | return if $amount == 0; | ||
| 90 | } | ||
| 91 | $lb->delete( $selidxs[0], $selidxs[-1] ); | ||
| 92 | $lb->insert( $selidxs[0] + $amount, @movethem ); | ||
| 93 | $lb->see( $selidxs[0] + $amount ); | ||
| 94 | |||
| 95 | # reselect the moved lines for further move | ||
| 96 | $lb->selectionSet( $selidxs[0] + $amount, $selidxs[-1] + $amount ); | ||
| 97 | return; | ||
| 98 | } ## end sub _move_sel | ||
| 99 | |||
| 100 | # insert the default priority list into the Listbox | ||
| 101 | # callback of the 'Default' button | ||
| 102 | # | ||
| 103 | sub _insert_default { | ||
| 104 | my $lb = shift; | ||
| 105 | |||
| 106 | $lb->delete( 0, 'end' ); | ||
| 107 | $lb->insert( 'end', Games::Sudoku::Trainer::Priorities::copy_default() ); | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; |