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"; |
12 | |
10 | |
13 | my $row = 1; |
11 | my $row = 1; |
14 | for my $item (@{ $self->{data}{item} }) { |
12 | for my $item (@{ $self->{data}{item} }) { |
15 | my $rend = "\x1b[30;47m"; |
13 | my $rend = "normal"; |
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} ? "active" : "hover"; |
19 | } |
17 | } |
20 | |
18 | |
21 | $cmd .= "$rend\x1b[K" . $self->locale_encode ($item->{text}) . "\015\012"; |
19 | $cmd .= "$item->{rend}{$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 | |
55 | |
50 | my $row = $event->{row}; |
56 | $self->{press}[$event->{button}] = 0; |
51 | my $col = $event->{col}; |
57 | |
|
|
58 | my ($row, $col) = ($event->{row}, $event->{col}); |
|
|
59 | if ($col >= 0 && $col < $self->ncol |
|
|
60 | && $row >= 0 && $row < @{ $self->{data}{item} }) { |
|
|
61 | my $item = $self->{data}{item}[$row]; |
|
|
62 | $item->{activate}->($event, $item); |
|
|
63 | } |
|
|
64 | |
|
|
65 | $self->refresh; |
52 | |
66 | |
53 | if ($event->{button} == $self->{data}{event}{button}) { |
67 | if ($event->{button} == $self->{data}{event}{button}) { |
54 | $self->ungrab; |
68 | $self->ungrab; |
55 | $self->destroy; |
69 | $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 | } |
70 | |
71 | |
71 | 1 |
72 | 1 |
72 | } |
73 | } |
73 | |
74 | |
74 | sub on_focus_out { |
75 | sub on_focus_out { |
75 | my ($self) = @_; |
76 | my ($self) = @_; |
76 | |
77 | |
77 | delete $self->{hover}; |
78 | delete $self->{hover}; |
78 | refresh $self; |
79 | $self->refresh; |
79 | |
80 | |
80 | () |
81 | () |
81 | } |
82 | } |
82 | |
83 | |
83 | sub on_init { |
84 | sub on_init { |
… | |
… | |
86 | my $data = $self->{data} = $urxvt::popup::self; |
87 | my $data = $self->{data} = $urxvt::popup::self; |
87 | |
88 | |
88 | $_->{width} = $self->strwidth ($_->{text}) |
89 | $_->{width} = $self->strwidth ($_->{text}) |
89 | for @{ $data->{item} }; |
90 | for @{ $data->{item} }; |
90 | |
91 | |
|
|
92 | $self->resource (title => "URxvt Popup Menu"); |
|
|
93 | $self->resource (name => "URxvt.popup"); |
|
|
94 | |
91 | $self->resource ($_ => $data->{term}->resource ($_)) |
95 | $self->resource ($_ => $data->{term}->resource ($_)) |
92 | for qw(font boldFont italicFont boldItalicFont color+0 color+1); |
96 | for qw(font boldFont italicFont boldItalicFont color+0 color+1); |
93 | |
97 | |
94 | my $width = List::Util::max map $_->{width}, @{ $data->{item} }; |
98 | my $width = List::Util::max map $_->{width}, @{ $data->{item} }; |
95 | my $height = @{ $data->{item} }; |
99 | my $height = @{ $data->{item} }; |
… | |
… | |
97 | my $pos = ""; |
101 | my $pos = ""; |
98 | |
102 | |
99 | if ($data->{event}) { |
103 | if ($data->{event}) { |
100 | my $x = int List::Util::max 0, $data->{event}{x_root} - $width * $data->{term}->fwidth * 0.5; |
104 | 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; |
105 | my $y = int List::Util::max 0, $data->{event}{y_root} - $data->{term}->fheight * 0.5; |
102 | |
|
|
103 | $pos = "+$x+$y"; |
106 | $pos = "+$x+$y"; |
104 | } |
107 | } |
105 | |
108 | |
106 | $self->resource (geometry => "${width}x${height}$pos"); |
109 | $self->resource (geometry => "${width}x${height}$pos"); |
107 | |
110 | |
… | |
… | |
109 | } |
112 | } |
110 | |
113 | |
111 | sub on_start { |
114 | sub on_start { |
112 | my ($self) = @_; |
115 | my ($self) = @_; |
113 | |
116 | |
|
|
117 | $self->cmd_parse ("\x1b[?25l\x1b[?7l"); |
|
|
118 | $self->refresh; |
|
|
119 | |
114 | # might fail, but try anyways |
120 | # might fail, but try anyways |
115 | $self->grab ($self->{data}{event}{time}, 1) |
121 | $self->grab ($self->{data}{event}{time}, 1) |
116 | and $self->allow_events_sync; |
122 | and $self->allow_events_async; |
117 | |
123 | |
118 | on_button_press $self, $self->{data}{event} if $self->{data}{event}{button}; |
124 | 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 | |
125 | |
123 | () |
126 | () |
124 | } |
127 | } |
125 | |
128 | |
126 | sub on_map_notify { |
129 | sub on_map_notify { |
127 | my ($self, $event) = @_; |
130 | my ($self, $event) = @_; |
128 | |
131 | |
129 | # should definitely not fail |
132 | # should definitely not fail |
130 | $self->grab ($self->{data}{event}{time}, 1) |
133 | $self->grab ($self->{data}{event}{time}, 1) |
131 | and $self->allow_events_sync; |
134 | and $self->allow_events_async; |
132 | } |
|
|
133 | |
|
|
134 | sub on_destroy { |
|
|
135 | } |
135 | } |
136 | |
136 | |
137 | |
137 | |
138 | |
|
|
139 | |
|
|
140 | |
|
|