1 |
#! perl |
2 |
|
3 |
# this extension implements popup-menu functionality for urxvt. it works |
4 |
# together with the urxvt::popup class. |
5 |
|
6 |
use List::Util; |
7 |
|
8 |
sub refresh { |
9 |
my ($self) = @_; |
10 |
|
11 |
my $cmd = "\x1b[H"; |
12 |
|
13 |
my $row = 1; |
14 |
for my $item (@{ $self->{data}{item} }) { |
15 |
my $rend = "\x1b[30;47m"; |
16 |
|
17 |
if ($row == $self->{hover}) { |
18 |
$rend = $self->{press} ? "\x1b[m" : "\x1b[30;46m"; |
19 |
} |
20 |
|
21 |
$cmd .= "$rend\x1b[K" . $self->locale_encode ($item->{text}) . "\015\012"; |
22 |
|
23 |
$row++; |
24 |
} |
25 |
|
26 |
$self->cmd_parse (substr $cmd, 0, -2); |
27 |
} |
28 |
|
29 |
sub on_motion_notify { |
30 |
my ($self, $event) = @_; |
31 |
|
32 |
$self->{hover} = $event->{row} + 1; |
33 |
refresh $self; |
34 |
|
35 |
1 |
36 |
} |
37 |
|
38 |
sub on_button_press { |
39 |
my ($self, $event) = @_; |
40 |
|
41 |
$self->{press}[$event->{button}] = 1; |
42 |
refresh $self; |
43 |
|
44 |
1 |
45 |
} |
46 |
|
47 |
sub on_button_release { |
48 |
my ($self, $event) = @_; |
49 |
|
50 |
my $row = $event->{row}; |
51 |
my $col = $event->{col}; |
52 |
|
53 |
if ($event->{button} == $self->{data}{event}{button}) { |
54 |
$self->ungrab; |
55 |
$self->destroy; |
56 |
} |
57 |
|
58 |
if ($event->{button} == 1) { |
59 |
$self->{press}[$event->{button}] = 0; |
60 |
refresh $self; |
61 |
|
62 |
warn "$event->{row} $event->{col}\n";#d# |
63 |
|
64 |
if ($col >= 0 && $col < $self->ncol |
65 |
&& $row >= 0 && $row < @{ $self->{data}{item} }) { |
66 |
$self->{data}{item}[$row]{activate}->($event); |
67 |
print "ok\n"; |
68 |
} |
69 |
} |
70 |
|
71 |
1 |
72 |
} |
73 |
|
74 |
sub on_focus_out { |
75 |
my ($self) = @_; |
76 |
|
77 |
delete $self->{hover}; |
78 |
refresh $self; |
79 |
|
80 |
() |
81 |
} |
82 |
|
83 |
sub on_init { |
84 |
my ($self) = @_; |
85 |
|
86 |
my $data = $self->{data} = $urxvt::popup::self; |
87 |
|
88 |
$_->{width} = $self->strwidth ($_->{text}) |
89 |
for @{ $data->{item} }; |
90 |
|
91 |
$self->resource ($_ => $data->{term}->resource ($_)) |
92 |
for qw(font boldFont italicFont boldItalicFont color+0 color+1); |
93 |
|
94 |
my $width = List::Util::max map $_->{width}, @{ $data->{item} }; |
95 |
my $height = @{ $data->{item} }; |
96 |
|
97 |
my $pos = ""; |
98 |
|
99 |
if ($data->{event}) { |
100 |
my $x = int List::Util::max 0, $data->{event}{x_root} - $width * $data->{term}->fwidth * 0.5; |
101 |
my $y = int List::Util::max 0, $data->{event}{y_root} - $data->{term}->fheight * 0.5; |
102 |
|
103 |
$pos = "+$x+$y"; |
104 |
} |
105 |
|
106 |
$self->resource (geometry => "${width}x${height}$pos"); |
107 |
|
108 |
() |
109 |
} |
110 |
|
111 |
sub on_start { |
112 |
my ($self) = @_; |
113 |
|
114 |
# might fail, but try anyways |
115 |
$self->grab ($self->{data}{event}{time}, 1) |
116 |
and $self->allow_events_sync; |
117 |
|
118 |
on_button_press $self, $self->{data}{event} if $self->{data}{event}{button}; |
119 |
|
120 |
$self->cmd_parse ("\x1b[?25l\x1b[?7l"); |
121 |
refresh $self; |
122 |
|
123 |
() |
124 |
} |
125 |
|
126 |
sub on_map_notify { |
127 |
my ($self, $event) = @_; |
128 |
|
129 |
# should definitely not fail |
130 |
$self->grab ($self->{data}{event}{time}, 1) |
131 |
and $self->allow_events_sync; |
132 |
} |
133 |
|
134 |
sub on_destroy { |
135 |
} |
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|