ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/tabbed
Revision: 1.12
Committed: Sun Jan 22 12:21:27 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
CVS Tags: rel-7_3, rel-7_2, rel-7_5, rel-7_4, rel-7_3a
Changes since 1.11: +1 -0 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 $self->refresh;
186
187 ()
188 }
189
190 sub on_wm_delete_window {
191 my ($self) = @_;
192
193 $_->destroy for @{ $self->{tabs} };
194
195 1
196 }
197
198 sub tab_start {
199 my ($self, $tab) = @_;
200
201 $tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask);
202
203 push @{ $self->{tabs} }, $tab;
204
205 # $tab->{name} ||= scalar @{ $self->{tabs} };
206 $self->make_current ($tab);
207
208 ()
209 }
210
211 sub tab_destroy {
212 my ($self, $tab) = @_;
213
214 $self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ];
215
216 if (@{ $self->{tabs} }) {
217 if ($self->{cur} == $tab) {
218 delete $self->{cur};
219 $self->make_current ($self->{tabs}[-1]);
220 } else {
221 $self->refresh;
222 }
223 } else {
224 # delay destruction a tiny bit
225 $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy });
226 }
227
228 ()
229 }
230
231 sub tab_key_press {
232 my ($self, $tab, $event, $keysym, $str) = @_;
233
234 if ($event->{state} & urxvt::ShiftMask) {
235 if ($keysym == 0xff51 || $keysym == 0xff53) {
236 my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
237
238 --$idx if $keysym == 0xff51;
239 ++$idx if $keysym == 0xff53;
240
241 $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]);
242
243 return 1;
244 } elsif ($keysym == 0xff54) {
245 $self->new_tab;
246
247 return 1;
248 }
249 }
250
251 ()
252 }
253
254 sub tab_property_notify {
255 my ($self, $tab, $event) = @_;
256
257 $self->copy_properties
258 if $event->{window} == $tab->parent;
259
260 ()
261 }
262
263 sub tab_activity {
264 my ($self, $tab) = @_;
265
266 $self->refresh;
267 }
268
269 package urxvt::ext::tabbed::tab;
270
271 # helper extension implementing the subwindows of a tabbed terminal.
272 # simply proxies all interesting calls back to the tabbed class.
273
274 {
275 for my $hook qw(start destroy key_press property_notify) {
276 eval qq{
277 sub on_$hook {
278 my \$parent = \$_[0]{term}{parent}
279 or return;
280 \$parent->tab_$hook (\@_)
281 }
282 };
283 die if $@;
284 }
285 }
286
287 sub on_add_lines {
288 $_[0]->{activity}++
289 or $_[0]{term}{parent}->tab_activity ($_[0]);
290 ()
291 }
292
293