1 |
#! perl |
2 |
|
3 |
# this extension implements popup-menu functionality for urxvt. it works |
4 |
# together with the urxvt::popup class. |
5 |
|
6 |
sub refresh { |
7 |
my ($self) = @_; |
8 |
|
9 |
my $cmd = "\x1b[H"; |
10 |
|
11 |
my $row = 1; |
12 |
for my $item (@{ $self->{data}{item} }) { |
13 |
my $rend = "\x1b[30;47m"; |
14 |
|
15 |
if ($row == $self->{hover}) { |
16 |
$rend = $self->{press} ? "\x1b[m" : "\x1b[30;46m"; |
17 |
} |
18 |
|
19 |
$cmd .= "$rend\x1b[K"; |
20 |
$cmd .= $self->locale_encode ($item->{render}->($item)); |
21 |
$cmd .= "\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 |
$self->{press}[$event->{button}] = 0; |
59 |
|
60 |
if ($event->{button} == 1) { |
61 |
refresh $self; |
62 |
|
63 |
warn "$event->{row} $event->{col}\n";#d# |
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 |
|
72 |
1 |
73 |
} |
74 |
|
75 |
sub on_focus_out { |
76 |
my ($self) = @_; |
77 |
|
78 |
delete $self->{hover}; |
79 |
refresh $self; |
80 |
|
81 |
() |
82 |
} |
83 |
|
84 |
sub on_init { |
85 |
my ($self) = @_; |
86 |
|
87 |
my $data = $self->{data} = $urxvt::popup::self; |
88 |
|
89 |
$_->{width} = $self->strwidth ($_->{text}) |
90 |
for @{ $data->{item} }; |
91 |
|
92 |
$self->resource ($_ => $data->{term}->resource ($_)) |
93 |
for qw(font boldFont italicFont boldItalicFont color+0 color+1); |
94 |
|
95 |
my $width = List::Util::max map $_->{width}, @{ $data->{item} }; |
96 |
my $height = @{ $data->{item} }; |
97 |
|
98 |
my $pos = ""; |
99 |
|
100 |
if ($data->{event}) { |
101 |
my $x = int List::Util::max 0, $data->{event}{x_root} - $width * $data->{term}->fwidth * 0.5; |
102 |
my $y = int List::Util::max 0, $data->{event}{y_root} - $data->{term}->fheight * 0.5; |
103 |
|
104 |
$pos = "+$x+$y"; |
105 |
} |
106 |
|
107 |
$self->resource (geometry => "${width}x${height}$pos"); |
108 |
|
109 |
() |
110 |
} |
111 |
|
112 |
sub on_start { |
113 |
my ($self) = @_; |
114 |
|
115 |
# might fail, but try anyways |
116 |
$self->grab ($self->{data}{event}{time}, 1) |
117 |
and $self->allow_events_async; |
118 |
|
119 |
on_button_press $self, $self->{data}{event} if $self->{data}{event}{button}; |
120 |
|
121 |
$self->cmd_parse ("\x1b[?25l\x1b[?7l"); |
122 |
refresh $self; |
123 |
|
124 |
() |
125 |
} |
126 |
|
127 |
sub on_map_notify { |
128 |
my ($self, $event) = @_; |
129 |
|
130 |
# should definitely not fail |
131 |
$self->grab ($self->{data}{event}{time}, 1) |
132 |
and $self->allow_events_async; |
133 |
} |
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|