1package Term::Choose::Opt::Mouse;
2
3use warnings;
4use strict;
5use 5.10.0;
6
7our $VERSION = '1.745';
8
9use Term::Choose::Constants qw( :all );
10
11
12sub __mouse_info_to_key {
13    my ( $self, $button, $mouse_x, $mouse_y ) = @_;
14    if ( $button == 4 ) {
15        return VK_PAGE_UP;
16    }
17    elsif ( $button == 5 ) {
18        return VK_PAGE_DOWN;
19    }
20    # ..._y, ..._x: absolute position, one-based index
21    my $mouse_row = $mouse_y - 1 - $self->{offset_rows};
22    my $mouse_col = $mouse_x - 1;
23    if ( $mouse_row < 0 || $mouse_row > $#{$self->{rc2idx}} ) {
24        return NEXT_get_key;
25    }
26    my $matched_col;
27    my $begin_this_col = 0;
28    my $row = $mouse_row + $self->{first_page_row};
29
30    COL: for my $col ( 0 .. $#{$self->{rc2idx}[$row]} ) {
31        my $begin_next_col;
32        if ( $self->{current_layout} == -1 ) {
33            my $idx = $self->{rc2idx}[$row][$col];
34            $begin_next_col = $begin_this_col + $self->{width_elements}[$idx] + $self->{pad};
35        }
36        else {
37            $begin_next_col = $begin_this_col + $self->{col_width_plus};
38        }
39        if ( $col == 0 ) {
40            $begin_next_col -= int( $self->{pad} / 2 );
41        }
42        if ( $col == $#{$self->{rc2idx}[$row]} && $begin_next_col > $self->{avail_width} ) {
43            $begin_next_col = $self->{avail_width};
44        }
45        if ( $mouse_col >= $begin_this_col && $mouse_col < $begin_next_col ) {
46            $matched_col = $col;
47            last COL;
48        }
49        $begin_this_col = $begin_next_col;
50    }
51    if ( ! defined $matched_col ) {
52        return NEXT_get_key;
53    }
54    if ( defined $self->{skip_items} ) {
55        my $idx = $self->{rc2idx}[$row][$matched_col];
56        if ( $self->{list}[$idx] =~ /$self->{skip_items}/ ) {
57            return NEXT_get_key;
58        }
59    }
60    if ( $button == 1 ) {
61        $self->{pos}[ROW] = $row;           # writes to $self
62        $self->{pos}[COL] = $matched_col;   # writes to $self
63        return LINE_FEED;
64    }
65    if ( $row != $self->{pos}[ROW] || $matched_col != $self->{pos}[COL] ) {
66        my $not_pos = $self->{pos};
67        $self->{pos} = [ $row, $matched_col ];   # writes to $self
68        $self->__wr_cell( $not_pos->[0], $not_pos->[1] );
69        $self->__wr_cell( $self->{pos}[ROW], $self->{pos}[COL] );
70    }
71    if ( $button == 3 ) {
72        return KEY_SPACE;
73    }
74    else {
75        return NEXT_get_key;
76    }
77}
78
79
80
81
82
83
84
851;
86
87__END__
88