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