ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/tabbed
Revision: 1.13
Committed: Fri Feb 3 05:47:05 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.12: +5 -0 lines
Log Message:
*** empty log message ***

File Contents

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