ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/tabbed
Revision: 1.11
Committed: Fri Jan 20 18:50:49 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.10: +23 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! perl
2
3 sub refresh {
4 my ($self) = @_;
5
6 my $ncol = $self->ncol;
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
17 my $ofs = 7;
18 my $idx = 0;
19
20 for my $tab (@{ $self->{tabs} }) {
21 $idx++;
22
23 my $act = $tab->{activity} && $tab != $self->{cur}
24 ? "*" : " ";
25
26 my $txt = "$act$idx$act";
27 my $len = length $txt;
28
29 substr $text, $ofs, $len + 1, "$txt|";
30 @$rend[$ofs .. $ofs + $len - 1] = (urxvt::OVERLAY_RSTYLE) x $len
31 if $tab == $self->{cur};
32
33 push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ];
34
35 $ofs += $len + 1;
36 }
37
38 $self->{tabofs} = \@ofs;
39
40 $self->ROW_t (0, $text, 0, 0, $ncol);
41 $self->ROW_r (0, $rend, 0, 0, $ncol);
42
43 $self->want_refresh;
44 }
45
46 sub new_tab {
47 my ($self) = @_;
48
49 my $offset = $self->fheight;
50
51 # save a backlink to us, make sure tabbed is inactive
52 push @urxvt::TERM_INIT, sub {
53 my ($term) = @_;
54 $term->{parent} = $self;
55
56 $term->resource ($_->[0] => $_->[1])
57 for @{ $self->{resource} || [] };
58
59 $term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed");
60
61 };
62
63 push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::;
64
65 my $term = new urxvt::term
66 $self->env, $urxvt::RXVTNAME,
67 -embed => $self->parent,
68 ;
69 }
70
71 sub configure {
72 my ($self) = @_;
73
74 my $tab = $self->{cur};
75
76 $tab->XMoveResizeWindow (
77 $tab->parent,
78 0, $self->{tabheight},
79 $self->width, $self->height - $self->{tabheight}
80 );
81 }
82
83 sub copy_properties {
84 my ($self) = @_;
85 my $tab = $self->{cur};
86
87 my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS");
88
89 my %our_props = map +($_ => undef), $self->XListProperties ($self->parent);
90
91 for my $atom ($tab->XListProperties ($tab->parent)) {
92 my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom);
93
94 delete $our_props{$atom};
95
96 if ($atom == $wm_normal_hints) {
97 my (@hints) = unpack "l!*", $items;
98
99 $hints[$_] += $self->{tabheight} for (4, 6, 16);
100
101 $items = pack "l!*", @hints;
102 }
103 $self->XChangeWindowProperty ($self->parent, $atom, $type, $format, $items);
104 }
105
106 $self->XDeleteProperty ($self->parent, $_) for keys %our_props;
107 }
108
109 sub make_current {
110 my ($self, $tab) = @_;
111
112 if (my $cur = $self->{cur}) {
113 delete $cur->{activity};
114 $cur->XUnmapWindow ($cur->parent) if $cur->mapped;
115 $cur->focus_out;
116 }
117
118 $self->{cur} = $tab;
119
120 $self->configure;
121 $self->copy_properties;
122 $tab->focus_in;
123 $tab->XMapWindow ($tab->parent);
124 delete $tab->{activity};
125 $self->refresh;
126
127 ()
128 }
129
130 sub on_button_press {
131 1
132 }
133
134 sub on_button_release {
135 my ($self, $event) = @_;
136
137 if ($event->{row} == 0) {
138 for my $button (@{ $self->{tabofs} }) {
139 $button->[2]->($self, $event)
140 if $event->{col} >= $button->[0]
141 && $event->{col} < $button->[1];
142 }
143 }
144
145 1
146 }
147
148 sub on_motion_notify {
149 1
150 }
151
152 sub on_init {
153 my ($self) = @_;
154
155 for (qw(name perl_ext_1 perl_ext_2)) {
156 my $val = $self->resource ($_);
157
158 push @{ $self->{resource} }, [$_ => $val]
159 if defined $val;
160 }
161
162 $self->resource (int_bwidth => 0);
163 $self->resource (name => "URxvt.tab");
164 $self->resource (pty_fd => -1);
165
166 $self->option ($urxvt::OPTION{scrollBar}, 0);
167
168 ()
169 }
170
171 sub on_start {
172 my ($self) = @_;
173
174 $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace;
175
176 $self->new_tab;
177
178 ()
179 }
180
181 sub on_configure_notify {
182 my ($self, $event) = @_;
183
184 $self->configure;
185
186 ()
187 }
188
189 sub on_wm_delete_window {
190 my ($self) = @_;
191
192 $_->destroy for @{ $self->{tabs} };
193
194 1
195 }
196
197 sub tab_start {
198 my ($self, $tab) = @_;
199
200 $tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask);
201
202 push @{ $self->{tabs} }, $tab;
203
204 # $tab->{name} ||= scalar @{ $self->{tabs} };
205 $self->make_current ($tab);
206
207 ()
208 }
209
210 sub tab_destroy {
211 my ($self, $tab) = @_;
212
213 $self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ];
214
215 if (@{ $self->{tabs} }) {
216 if ($self->{cur} == $tab) {
217 delete $self->{cur};
218 $self->make_current ($self->{tabs}[-1]);
219 } else {
220 $self->refresh;
221 }
222 } else {
223 # delay destruction a tiny bit
224 $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy });
225 }
226
227 ()
228 }
229
230 sub tab_key_press {
231 my ($self, $tab, $event, $keysym, $str) = @_;
232
233 if ($event->{state} & urxvt::ShiftMask) {
234 if ($keysym == 0xff51 || $keysym == 0xff53) {
235 my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
236
237 --$idx if $keysym == 0xff51;
238 ++$idx if $keysym == 0xff53;
239
240 $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]);
241
242 return 1;
243 } elsif ($keysym == 0xff54) {
244 $self->new_tab;
245
246 return 1;
247 }
248 }
249
250 ()
251 }
252
253 sub tab_property_notify {
254 my ($self, $tab, $event) = @_;
255
256 $self->copy_properties
257 if $event->{window} == $tab->parent;
258
259 ()
260 }
261
262 sub tab_activity {
263 my ($self, $tab) = @_;
264
265 $self->refresh;
266 }
267
268 package urxvt::ext::tabbed::tab;
269
270 # helper extension implementing the subwindows of a tabbed terminal.
271 # simply proxies all interesting calls back to the tabbed class.
272
273 {
274 for my $hook qw(start destroy key_press property_notify) {
275 eval qq{
276 sub on_$hook {
277 my \$parent = \$_[0]{term}{parent}
278 or return;
279 \$parent->tab_$hook (\@_)
280 }
281 };
282 die if $@;
283 }
284 }
285
286 sub on_add_lines {
287 $_[0]->{activity}++
288 or $_[0]{term}{parent}->tab_activity ($_[0]);
289 ()
290 }
291
292