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

# 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.4 0, $self->{tabheight},
79     $self->width, $self->height - $self->{tabheight}
80 root 1.1 );
81 root 1.11 }
82    
83     sub copy_properties {
84     my ($self) = @_;
85     my $tab = $self->{cur};
86 root 1.5
87     my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS");
88    
89 root 1.11 my %our_props = map +($_ => undef), $self->XListProperties ($self->parent);
90    
91 root 1.5 for my $atom ($tab->XListProperties ($tab->parent)) {
92     my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom);
93 root 1.11
94     delete $our_props{$atom};
95 root 1.5
96     if ($atom == $wm_normal_hints) {
97     my (@hints) = unpack "l!*", $items;
98    
99 root 1.6 $hints[$_] += $self->{tabheight} for (4, 6, 16);
100    
101     $items = pack "l!*", @hints;
102 root 1.5 }
103     $self->XChangeWindowProperty ($self->parent, $atom, $type, $format, $items);
104     }
105 root 1.6
106 root 1.11 $self->XDeleteProperty ($self->parent, $_) for keys %our_props;
107 root 1.1 }
108    
109     sub make_current {
110     my ($self, $tab) = @_;
111    
112     if (my $cur = $self->{cur}) {
113 root 1.9 delete $cur->{activity};
114 root 1.7 $cur->XUnmapWindow ($cur->parent) if $cur->mapped;
115     $cur->focus_out;
116 root 1.1 }
117    
118     $self->{cur} = $tab;
119    
120     $self->configure;
121 root 1.11 $self->copy_properties;
122 root 1.7 $tab->focus_in;
123 root 1.1 $tab->XMapWindow ($tab->parent);
124 root 1.10 delete $tab->{activity};
125 root 1.1 $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 root 1.6 for my $button (@{ $self->{tabofs} }) {
139     $button->[2]->($self, $event)
140     if $event->{col} >= $button->[0]
141     && $event->{col} < $button->[1];
142 root 1.1 }
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 root 1.4 $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace;
175    
176 root 1.1 $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 root 1.11 $tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask);
201    
202 root 1.1 push @{ $self->{tabs} }, $tab;
203    
204 root 1.7 # $tab->{name} ||= scalar @{ $self->{tabs} };
205 root 1.1 $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 root 1.7 } else {
220     $self->refresh;
221 root 1.1 }
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 root 1.8 sub tab_key_press {
231     my ($self, $tab, $event, $keysym, $str) = @_;
232    
233 root 1.10 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 root 1.8 }
249    
250     ()
251     }
252    
253 root 1.11 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 root 1.9 sub tab_activity {
263     my ($self, $tab) = @_;
264    
265     $self->refresh;
266     }
267    
268 root 1.1 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 root 1.11 for my $hook qw(start destroy key_press property_notify) {
275 root 1.1 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 root 1.9 sub on_add_lines {
287     $_[0]->{activity}++
288     or $_[0]{term}{parent}->tab_activity ($_[0]);
289     ()
290     }
291 root 1.1
292