1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | # this extension implements popup-menu functionality for urxvt. it works |
3 | # this extension implements popup-menu functionality for urxvt. it works |
4 | # together with the urxvt::popup class. |
4 | # together with the urxvt::popup class. |
5 | |
|
|
6 | use List::Util; |
|
|
7 | |
5 | |
8 | sub refresh { |
6 | sub refresh { |
9 | my ($self) = @_; |
7 | my ($self) = @_; |
10 | |
8 | |
11 | my $cmd = "\x1b[H"; |
9 | my $cmd = "\x1b[H"; |
… | |
… | |
16 | |
14 | |
17 | if ($row == $self->{hover}) { |
15 | if ($row == $self->{hover}) { |
18 | $rend = $self->{press} ? "\x1b[m" : "\x1b[30;46m"; |
16 | $rend = $self->{press} ? "\x1b[m" : "\x1b[30;46m"; |
19 | } |
17 | } |
20 | |
18 | |
21 | $cmd .= "$rend\x1b[K" . $self->locale_encode ($item->{text}) . "\015\012"; |
19 | $cmd .= "$rend\x1b[K"; |
|
|
20 | $cmd .= $self->locale_encode ($item->{render}->($item)); |
|
|
21 | $cmd .= "\015\012"; |
22 | |
22 | |
23 | $row++; |
23 | $row++; |
24 | } |
24 | } |
25 | |
25 | |
26 | $self->cmd_parse (substr $cmd, 0, -2); |
26 | $self->cmd_parse (substr $cmd, 0, -2); |
27 | } |
27 | } |
28 | |
28 | |
29 | sub on_motion_notify { |
29 | sub on_motion_notify { |
30 | my ($self, $event) = @_; |
30 | my ($self, $event) = @_; |
31 | |
31 | |
|
|
32 | delete $self->{hover}; |
|
|
33 | |
|
|
34 | my ($row, $col) = ($event->{row}, $event->{col}); |
|
|
35 | if ($col >= 0 && $col < $self->ncol |
|
|
36 | && $row >= 0 && $row < @{ $self->{data}{item} }) { |
32 | $self->{hover} = $event->{row} + 1; |
37 | $self->{hover} = $event->{row} + 1; |
33 | refresh $self; |
38 | } |
|
|
39 | $self->refresh; |
34 | |
40 | |
35 | 1 |
41 | 1 |
36 | } |
42 | } |
37 | |
43 | |
38 | sub on_button_press { |
44 | sub on_button_press { |
39 | my ($self, $event) = @_; |
45 | my ($self, $event) = @_; |
40 | |
46 | |
41 | $self->{press}[$event->{button}] = 1; |
47 | $self->{press}[$event->{button}] = 1; |
42 | refresh $self; |
48 | $self->refresh; |
43 | |
49 | |
44 | 1 |
50 | 1 |
45 | } |
51 | } |
46 | |
52 | |
47 | sub on_button_release { |
53 | sub on_button_release { |
48 | my ($self, $event) = @_; |
54 | my ($self, $event) = @_; |
49 | |
|
|
50 | my $row = $event->{row}; |
|
|
51 | my $col = $event->{col}; |
|
|
52 | |
55 | |
53 | if ($event->{button} == $self->{data}{event}{button}) { |
56 | if ($event->{button} == $self->{data}{event}{button}) { |
54 | $self->ungrab; |
57 | $self->ungrab; |
55 | $self->destroy; |
58 | $self->destroy; |
56 | } |
59 | } |
57 | |
60 | |
58 | $self->{press}[$event->{button}] = 0; |
61 | $self->{press}[$event->{button}] = 0; |
59 | |
62 | |
60 | if ($event->{button} == 1) { |
63 | my ($row, $col) = ($event->{row}, $event->{col}); |
61 | refresh $self; |
64 | if ($col >= 0 && $col < $self->ncol |
|
|
65 | && $row >= 0 && $row < @{ $self->{data}{item} }) { |
|
|
66 | $self->{data}{item}[$row]{activate}->($event); |
|
|
67 | } |
62 | |
68 | |
63 | warn "$event->{row} $event->{col}\n";#d# |
69 | $self->refresh; |
64 | |
|
|
65 | if ($col >= 0 && $col < $self->ncol |
|
|
66 | && $row >= 0 && $row < @{ $self->{data}{item} }) { |
|
|
67 | $self->{data}{item}[$row]{activate}->($event); |
|
|
68 | print "ok\n"; |
|
|
69 | } |
|
|
70 | } |
|
|
71 | |
70 | |
72 | 1 |
71 | 1 |
73 | } |
72 | } |
74 | |
73 | |
75 | sub on_focus_out { |
74 | sub on_focus_out { |
76 | my ($self) = @_; |
75 | my ($self) = @_; |
77 | |
76 | |
78 | delete $self->{hover}; |
77 | delete $self->{hover}; |
79 | refresh $self; |
78 | $self->refresh; |
80 | |
79 | |
81 | () |
80 | () |
82 | } |
81 | } |
83 | |
82 | |
84 | sub on_init { |
83 | sub on_init { |