1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | sub refresh { |
3 | sub refresh { |
4 | my ($self) = @_; |
4 | my ($self) = @_; |
5 | |
5 | |
6 | my $cmd = "\x1b[H\x1b[7m\x1b[K"; |
6 | my $ncol = $self->ncol; |
7 | my $txt; |
7 | |
|
|
8 | my $text = " " x $ncol; |
|
|
9 | my $rend = [(urxvt::DEFAULT_RSTYLE | urxvt::RS_RVid) x $ncol]; |
|
|
10 | |
|
|
11 | my @ofs; |
|
|
12 | |
|
|
13 | substr $text, 0, 7, "[NEW] |"; |
|
|
14 | @$rend[0 .. 5] = (urxvt::OVERLAY_RSTYLE) x 6; |
|
|
15 | push @ofs, [0, 6, sub { $_[0]->new_tab }]; |
|
|
16 | |
8 | my @ofs = (0); |
17 | my $ofs = 7; |
|
|
18 | my $idx = 0; |
9 | |
19 | |
10 | for my $tab (@{ $self->{tabs} }) { |
20 | for my $tab (@{ $self->{tabs} }) { |
|
|
21 | $idx++; |
|
|
22 | |
|
|
23 | my $txt = " $idx "; |
|
|
24 | my $len = length $txt; |
|
|
25 | |
|
|
26 | substr $text, $ofs, $len + 1, "$txt|"; |
|
|
27 | @$rend[$ofs .. $ofs + $len - 1] = (urxvt::OVERLAY_RSTYLE) x $len |
11 | if ($tab == $self->{cur}) { |
28 | if $tab == $self->{cur}; |
12 | $txt = " [$tab->{name}] "; |
|
|
13 | } else { |
|
|
14 | $txt = " $tab->{name} "; |
|
|
15 | } |
|
|
16 | |
29 | |
17 | $cmd .= $txt; |
30 | push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ]; |
18 | push @ofs, $ofs[-1] + length $txt; |
31 | |
|
|
32 | $ofs += $len + 1; |
19 | } |
33 | } |
20 | |
34 | |
21 | $self->{tabofs} = \@ofs; |
35 | $self->{tabofs} = \@ofs; |
22 | |
36 | |
23 | $self->cmd_parse ($self->locale_encode ($cmd)); |
37 | $self->ROW_t (0, $text, 0, 0, $ncol); |
|
|
38 | $self->ROW_r (0, $rend, 0, 0, $ncol); |
|
|
39 | |
|
|
40 | $self->want_refresh; |
24 | } |
41 | } |
25 | |
42 | |
26 | sub new_tab { |
43 | sub new_tab { |
27 | my ($self) = @_; |
44 | my ($self) = @_; |
28 | |
45 | |
… | |
… | |
49 | } |
66 | } |
50 | |
67 | |
51 | sub configure { |
68 | sub configure { |
52 | my ($self) = @_; |
69 | my ($self) = @_; |
53 | |
70 | |
|
|
71 | my $tab = $self->{cur}; |
|
|
72 | |
54 | $self->{cur}->XMoveResizeWindow ( |
73 | $tab->XMoveResizeWindow ( |
55 | $self->{cur}->parent, |
74 | $tab->parent, |
56 | 0, $self->{tabheight}, |
75 | 0, $self->{tabheight}, |
57 | $self->width, $self->height - $self->{tabheight} |
76 | $self->width, $self->height - $self->{tabheight} |
58 | ); |
77 | ); |
59 | } |
|
|
60 | |
|
|
61 | sub make_current { |
|
|
62 | my ($self, $tab) = @_; |
|
|
63 | |
|
|
64 | if (my $cur = $self->{cur}) { |
|
|
65 | $cur->XUnmapWindow ($cur->parent) |
|
|
66 | if $cur->mapped; |
|
|
67 | } |
|
|
68 | |
|
|
69 | $self->{cur} = $tab; |
|
|
70 | |
|
|
71 | $self->configure; |
|
|
72 | |
78 | |
73 | my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); |
79 | my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS"); |
74 | |
80 | |
75 | for my $atom ($tab->XListProperties ($tab->parent)) { |
81 | for my $atom ($tab->XListProperties ($tab->parent)) { |
76 | my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); |
82 | my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom); |
77 | |
83 | |
78 | if ($atom == $wm_normal_hints) { |
84 | if ($atom == $wm_normal_hints) { |
79 | my (@hints) = unpack "l!*", $items; |
85 | my (@hints) = unpack "l!*", $items; |
|
|
86 | |
80 | $hints[ 4] += $self->{tabheight}; |
87 | $hints[$_] += $self->{tabheight} for (4, 6, 16); |
81 | $hints[16] += $self->{tabheight}; |
88 | |
82 | $items = pack "l!*", @hints; |
89 | $items = pack "l!*", @hints; |
83 | } |
90 | } |
84 | $self->XChangeWindowProperty ($self->parent, $atom, $type, $format, $items); |
91 | $self->XChangeWindowProperty ($self->parent, $atom, $type, $format, $items); |
85 | } |
92 | } |
86 | |
93 | |
|
|
94 | $self->refresh; |
|
|
95 | } |
|
|
96 | |
|
|
97 | sub make_current { |
|
|
98 | my ($self, $tab) = @_; |
|
|
99 | |
|
|
100 | if (my $cur = $self->{cur}) { |
|
|
101 | $cur->XUnmapWindow ($cur->parent) if $cur->mapped; |
|
|
102 | $cur->focus_out; |
|
|
103 | } |
|
|
104 | |
|
|
105 | $self->{cur} = $tab; |
|
|
106 | |
|
|
107 | $self->configure; |
|
|
108 | $tab->focus_in; |
87 | $tab->XMapWindow ($tab->parent); |
109 | $tab->XMapWindow ($tab->parent); |
88 | |
|
|
89 | $self->refresh; |
110 | $self->refresh; |
90 | |
111 | |
91 | () |
112 | () |
92 | } |
113 | } |
93 | |
114 | |
… | |
… | |
96 | } |
117 | } |
97 | |
118 | |
98 | sub on_button_release { |
119 | sub on_button_release { |
99 | my ($self, $event) = @_; |
120 | my ($self, $event) = @_; |
100 | |
121 | |
101 | my $ofs = $self->{tabofs}; |
|
|
102 | |
|
|
103 | if ($event->{row} == 0) { |
122 | if ($event->{row} == 0) { |
104 | for my $i (0 .. @$ofs - 2) { |
123 | for my $button (@{ $self->{tabofs} }) { |
|
|
124 | $button->[2]->($self, $event) |
105 | if ($event->{col} >= $ofs->[$i] |
125 | if $event->{col} >= $button->[0] |
106 | && $event->{col} < $ofs->[$i+1]) { |
126 | && $event->{col} < $button->[1]; |
107 | $self->make_current ($self->{tabs}[$i]); |
|
|
108 | } |
|
|
109 | } |
127 | } |
110 | } |
128 | } |
111 | |
129 | |
112 | 1 |
130 | 1 |
113 | } |
131 | } |
… | |
… | |
138 | sub on_start { |
156 | sub on_start { |
139 | my ($self) = @_; |
157 | my ($self) = @_; |
140 | |
158 | |
141 | $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; |
159 | $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace; |
142 | |
160 | |
143 | $self->cmd_parse ("\x1b[?25l\x1b[?7l"); |
|
|
144 | $self->new_tab; |
|
|
145 | $self->new_tab; |
|
|
146 | $self->new_tab; |
161 | $self->new_tab; |
147 | |
162 | |
148 | () |
163 | () |
149 | } |
164 | } |
150 | |
165 | |
… | |
… | |
167 | sub tab_start { |
182 | sub tab_start { |
168 | my ($self, $tab) = @_; |
183 | my ($self, $tab) = @_; |
169 | |
184 | |
170 | push @{ $self->{tabs} }, $tab; |
185 | push @{ $self->{tabs} }, $tab; |
171 | |
186 | |
172 | $tab->{name} ||= scalar @{ $self->{tabs} }; |
187 | # $tab->{name} ||= scalar @{ $self->{tabs} }; |
173 | $self->make_current ($tab); |
188 | $self->make_current ($tab); |
174 | |
189 | |
175 | () |
190 | () |
176 | } |
191 | } |
177 | |
192 | |
… | |
… | |
182 | |
197 | |
183 | if (@{ $self->{tabs} }) { |
198 | if (@{ $self->{tabs} }) { |
184 | if ($self->{cur} == $tab) { |
199 | if ($self->{cur} == $tab) { |
185 | delete $self->{cur}; |
200 | delete $self->{cur}; |
186 | $self->make_current ($self->{tabs}[-1]); |
201 | $self->make_current ($self->{tabs}[-1]); |
|
|
202 | } else { |
|
|
203 | $self->refresh; |
187 | } |
204 | } |
188 | } else { |
205 | } else { |
189 | # delay destruction a tiny bit |
206 | # delay destruction a tiny bit |
190 | $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy }); |
207 | $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy }); |
191 | } |
208 | } |
192 | |
209 | |
193 | () |
210 | () |
194 | } |
211 | } |
195 | |
212 | |
|
|
213 | sub tab_key_press { |
|
|
214 | my ($self, $tab, $event, $keysym, $str) = @_; |
|
|
215 | |
|
|
216 | if ($event->{state} & urxvt::ShiftMask |
|
|
217 | && ($keysym == 0xff51 || $keysym == 0xff53)) { |
|
|
218 | my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} }; |
|
|
219 | |
|
|
220 | --$idx if $keysym == 0xff51; |
|
|
221 | ++$idx if $keysym == 0xff53; |
|
|
222 | |
|
|
223 | $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]); |
|
|
224 | $self->refresh; |
|
|
225 | |
|
|
226 | return 1; |
|
|
227 | } |
|
|
228 | |
|
|
229 | () |
|
|
230 | } |
|
|
231 | |
196 | package urxvt::ext::tabbed::tab; |
232 | package urxvt::ext::tabbed::tab; |
197 | |
233 | |
198 | # helper extension implementing the subwindows of a tabbed terminal. |
234 | # helper extension implementing the subwindows of a tabbed terminal. |
199 | # simply proxies all interesting calls back to the tabbed class. |
235 | # simply proxies all interesting calls back to the tabbed class. |
200 | |
236 | |
201 | { |
237 | { |
202 | for my $hook qw(start destroy) { |
238 | for my $hook qw(start destroy key_press) { |
203 | eval qq{ |
239 | eval qq{ |
204 | sub on_$hook { |
240 | sub on_$hook { |
205 | my \$parent = \$_[0]{term}{parent} |
241 | my \$parent = \$_[0]{term}{parent} |
206 | or return; |
242 | or return; |
207 | \$parent->tab_$hook (\@_) |
243 | \$parent->tab_$hook (\@_) |