1 |
#! perl |
2 |
|
3 |
sub refresh { |
4 |
my ($self) = @_; |
5 |
|
6 |
my $cmd = "\x1b[H\x1b[7m\x1b[K"; |
7 |
my $txt; |
8 |
my @ofs = (0); |
9 |
|
10 |
for my $tab (@{ $self->{tabs} }) { |
11 |
if ($tab == $self->{cur}) { |
12 |
$txt = " [$tab->{name}] "; |
13 |
} else { |
14 |
$txt = " $tab->{name} "; |
15 |
} |
16 |
|
17 |
$cmd .= $txt; |
18 |
push @ofs, $ofs[-1] + length $txt; |
19 |
} |
20 |
|
21 |
$self->{tabofs} = \@ofs; |
22 |
|
23 |
$self->cmd_parse ($self->locale_encode ($cmd)); |
24 |
} |
25 |
|
26 |
sub new_tab { |
27 |
my ($self) = @_; |
28 |
|
29 |
my $offset = $self->fheight; |
30 |
|
31 |
# save a backlink to us, make sure tabbed is inactive |
32 |
push @urxvt::TERM_INIT, sub { |
33 |
my ($term) = @_; |
34 |
$term->{parent} = $self; |
35 |
|
36 |
$term->resource ($_->[0] => $_->[1]) |
37 |
for @{ $self->{resource} || [] }; |
38 |
|
39 |
$term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed"); |
40 |
|
41 |
}; |
42 |
|
43 |
push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::; |
44 |
|
45 |
my $term = new urxvt::term |
46 |
$self->env, $urxvt::RXVTNAME, |
47 |
-embed => $self->parent, |
48 |
; |
49 |
} |
50 |
|
51 |
sub configure { |
52 |
my ($self) = @_; |
53 |
|
54 |
my $tabheight = $self->int_bwidth + $self->fheight + $self->lineSpace; |
55 |
|
56 |
$self->{cur}->XMoveResizeWindow ( |
57 |
$self->{cur}->parent, |
58 |
0, $tabheight, |
59 |
$self->width, $self->height - $tabheight |
60 |
); |
61 |
} |
62 |
|
63 |
sub make_current { |
64 |
my ($self, $tab) = @_; |
65 |
|
66 |
if (my $cur = $self->{cur}) { |
67 |
$cur->XUnmapWindow ($cur->parent) |
68 |
if $cur->mapped; |
69 |
} |
70 |
|
71 |
$self->{cur} = $tab; |
72 |
|
73 |
$self->configure; |
74 |
|
75 |
for my $atom ($tab->XListProperties) { |
76 |
warn "$atom\n"; |
77 |
} |
78 |
|
79 |
$tab->XMapWindow ($tab->parent); |
80 |
|
81 |
$self->refresh; |
82 |
|
83 |
() |
84 |
} |
85 |
|
86 |
sub on_button_press { |
87 |
1 |
88 |
} |
89 |
|
90 |
sub on_button_release { |
91 |
my ($self, $event) = @_; |
92 |
|
93 |
my $ofs = $self->{tabofs}; |
94 |
|
95 |
if ($event->{row} == 0) { |
96 |
for my $i (0 .. @$ofs - 2) { |
97 |
if ($event->{col} >= $ofs->[$i] |
98 |
&& $event->{col} < $ofs->[$i+1]) { |
99 |
$self->make_current ($self->{tabs}[$i]); |
100 |
} |
101 |
} |
102 |
} |
103 |
|
104 |
1 |
105 |
} |
106 |
|
107 |
sub on_motion_notify { |
108 |
1 |
109 |
} |
110 |
|
111 |
sub on_init { |
112 |
my ($self) = @_; |
113 |
|
114 |
for (qw(name perl_ext_1 perl_ext_2)) { |
115 |
my $val = $self->resource ($_); |
116 |
|
117 |
push @{ $self->{resource} }, [$_ => $val] |
118 |
if defined $val; |
119 |
} |
120 |
|
121 |
$self->resource (int_bwidth => 0); |
122 |
$self->resource (name => "URxvt.tab"); |
123 |
$self->resource (pty_fd => -1); |
124 |
|
125 |
$self->option ($urxvt::OPTION{scrollBar}, 0); |
126 |
|
127 |
() |
128 |
} |
129 |
|
130 |
sub on_start { |
131 |
my ($self) = @_; |
132 |
|
133 |
$self->cmd_parse ("\x1b[?25l\x1b[?7l"); |
134 |
$self->new_tab; |
135 |
$self->new_tab; |
136 |
$self->new_tab; |
137 |
|
138 |
() |
139 |
} |
140 |
|
141 |
sub on_configure_notify { |
142 |
my ($self, $event) = @_; |
143 |
|
144 |
$self->configure; |
145 |
|
146 |
() |
147 |
} |
148 |
|
149 |
sub on_wm_delete_window { |
150 |
my ($self) = @_; |
151 |
|
152 |
$_->destroy for @{ $self->{tabs} }; |
153 |
|
154 |
1 |
155 |
} |
156 |
|
157 |
sub tab_start { |
158 |
my ($self, $tab) = @_; |
159 |
|
160 |
push @{ $self->{tabs} }, $tab; |
161 |
|
162 |
$tab->{name} ||= scalar @{ $self->{tabs} }; |
163 |
$self->make_current ($tab); |
164 |
|
165 |
() |
166 |
} |
167 |
|
168 |
sub tab_destroy { |
169 |
my ($self, $tab) = @_; |
170 |
|
171 |
$self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ]; |
172 |
|
173 |
if (@{ $self->{tabs} }) { |
174 |
if ($self->{cur} == $tab) { |
175 |
delete $self->{cur}; |
176 |
$self->make_current ($self->{tabs}[-1]); |
177 |
} |
178 |
} else { |
179 |
# delay destruction a tiny bit |
180 |
$self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy }); |
181 |
} |
182 |
|
183 |
() |
184 |
} |
185 |
|
186 |
package urxvt::ext::tabbed::tab; |
187 |
|
188 |
# helper extension implementing the subwindows of a tabbed terminal. |
189 |
# simply proxies all interesting calls back to the tabbed class. |
190 |
|
191 |
{ |
192 |
for my $hook qw(start destroy) { |
193 |
eval qq{ |
194 |
sub on_$hook { |
195 |
my \$parent = \$_[0]{term}{parent} |
196 |
or return; |
197 |
\$parent->tab_$hook (\@_) |
198 |
} |
199 |
}; |
200 |
die if $@; |
201 |
} |
202 |
} |
203 |
|
204 |
|
205 |
|