ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/tabbed
Revision: 1.31
Committed: Wed Jun 6 15:06:41 2012 UTC (11 years, 11 months ago) by root
Branch: MAIN
Changes since 1.30: +5 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3 root 1.31 #:META:X_RESOURCE:tabbar-fg:tab bar foreground colour
4     #:META:X_RESOURCE:tabbar-bg:tab bar background colour
5     #:META:X_RESOURCE:tab-fg:tab foreground colour
6     #:META:X_RESOURCE:tab-bg:tab background colour
7    
8 root 1.1 sub refresh {
9     my ($self) = @_;
10    
11 root 1.6 my $ncol = $self->ncol;
12    
13     my $text = " " x $ncol;
14 root 1.15 my $rend = [($self->{rs_tabbar}) x $ncol];
15 root 1.6
16     my @ofs;
17 root 1.1
18 root 1.7 substr $text, 0, 7, "[NEW] |";
19 root 1.15 @$rend[0 .. 5] = ($self->{rs_tab}) x 6;
20 root 1.7 push @ofs, [0, 6, sub { $_[0]->new_tab }];
21    
22     my $ofs = 7;
23     my $idx = 0;
24    
25 root 1.1 for my $tab (@{ $self->{tabs} }) {
26 root 1.7 $idx++;
27    
28 root 1.9 my $act = $tab->{activity} && $tab != $self->{cur}
29     ? "*" : " ";
30    
31     my $txt = "$act$idx$act";
32 root 1.6 my $len = length $txt;
33    
34     substr $text, $ofs, $len + 1, "$txt|";
35 root 1.15 @$rend[$ofs .. $ofs + $len - 1] = ($self->{rs_tab}) x $len
36 root 1.7 if $tab == $self->{cur};
37 root 1.6
38 root 1.7 push @ofs, [ $ofs, $ofs + $len, sub { $_[0]->make_current ($tab) } ];
39 root 1.1
40 root 1.6 $ofs += $len + 1;
41 root 1.1 }
42    
43     $self->{tabofs} = \@ofs;
44    
45 root 1.7 $self->ROW_t (0, $text, 0, 0, $ncol);
46     $self->ROW_r (0, $rend, 0, 0, $ncol);
47    
48     $self->want_refresh;
49 root 1.1 }
50    
51     sub new_tab {
52 root 1.21 my ($self, @argv) = @_;
53 root 1.1
54     # save a backlink to us, make sure tabbed is inactive
55     push @urxvt::TERM_INIT, sub {
56     my ($term) = @_;
57     $term->{parent} = $self;
58    
59 root 1.25 for (0 .. urxvt::NUM_RESOURCES - 1) {
60     my $value = $self->{resource}[$_];
61    
62     $term->resource ("+$_" => $value)
63     if defined $value;
64     }
65 root 1.1
66     $term->resource (perl_ext_2 => $term->resource ("perl_ext_2") . ",-tabbed");
67     };
68    
69     push @urxvt::TERM_EXT, urxvt::ext::tabbed::tab::;
70    
71     my $term = new urxvt::term
72     $self->env, $urxvt::RXVTNAME,
73     -embed => $self->parent,
74 root 1.21 @argv,
75 root 1.1 ;
76     }
77    
78     sub configure {
79     my ($self) = @_;
80    
81 root 1.5 my $tab = $self->{cur};
82    
83 root 1.14 # this is an extremely dirty way to force a configurenotify, but who cares
84 root 1.5 $tab->XMoveResizeWindow (
85     $tab->parent,
86 root 1.14 0, $self->{tabheight} + 1,
87 mikachu 1.30 $self->width, $self->height - $self->{tabheight}
88 root 1.13 );
89     $tab->XMoveResizeWindow (
90     $tab->parent,
91 root 1.4 0, $self->{tabheight},
92 mikachu 1.30 $self->width, $self->height - $self->{tabheight}
93 root 1.1 );
94 root 1.11 }
95    
96 root 1.19 sub on_resize_all_windows {
97     my ($self, $width, $height) = @_;
98    
99     1
100     }
101    
102 root 1.11 sub copy_properties {
103     my ($self) = @_;
104     my $tab = $self->{cur};
105 root 1.5
106     my $wm_normal_hints = $self->XInternAtom ("WM_NORMAL_HINTS");
107    
108 root 1.24 my $current = delete $self->{current_properties};
109 root 1.16
110 root 1.24 # pass 1: copy over properties different or nonexisting
111 root 1.5 for my $atom ($tab->XListProperties ($tab->parent)) {
112     my ($type, $format, $items) = $self->XGetWindowProperty ($tab->parent, $atom);
113 sf-exg 1.27
114 root 1.24 # fix up size hints
115 root 1.5 if ($atom == $wm_normal_hints) {
116     my (@hints) = unpack "l!*", $items;
117 sf-exg 1.27
118 root 1.19 $hints[$_] += $self->{tabheight} for (4, 6, 16);
119 sf-exg 1.27
120 root 1.19 $items = pack "l!*", @hints;
121     }
122 root 1.5
123 root 1.24 my $cur = delete $current->{$atom};
124 root 1.6
125 root 1.24 # update if changed, we assume empty items and zero type and format will not happen
126     $self->XChangeProperty ($self->parent, $atom, $type, $format, $items)
127     if $cur->[0] != $type or $cur->[1] != $format or $cur->[2] ne $items;
128    
129     $self->{current_properties}{$atom} = [$type, $format, $items];
130 root 1.5 }
131 root 1.6
132 root 1.24 # pass 2, delete all extraneous properties
133     $self->XDeleteProperty ($self->parent, $_) for keys %$current;
134 root 1.1 }
135    
136     sub make_current {
137     my ($self, $tab) = @_;
138    
139     if (my $cur = $self->{cur}) {
140 root 1.9 delete $cur->{activity};
141 root 1.7 $cur->XUnmapWindow ($cur->parent) if $cur->mapped;
142     $cur->focus_out;
143 root 1.1 }
144 sf-exg 1.27
145 root 1.1 $self->{cur} = $tab;
146    
147     $self->configure;
148 root 1.11 $self->copy_properties;
149 sf-exg 1.27
150 root 1.20 $tab->focus_out; # just in case, should be a nop
151     $tab->focus_in if $self->focus;
152 sf-exg 1.27
153 root 1.1 $tab->XMapWindow ($tab->parent);
154 root 1.10 delete $tab->{activity};
155 root 1.1 $self->refresh;
156    
157     ()
158     }
159    
160 root 1.15 sub on_focus_in {
161     my ($self, $event) = @_;
162    
163     $self->{cur}->focus_in;
164    
165     ()
166     }
167    
168     sub on_focus_out {
169     my ($self, $event) = @_;
170    
171     $self->{cur}->focus_out;
172    
173     ()
174     }
175    
176 sf-exg 1.26 sub on_tt_write {
177     my ($self, $octets) = @_;
178    
179     $self->{cur}->tt_write ($octets);
180    
181     1
182     }
183    
184 root 1.15 sub on_key_press {
185     my ($self, $event) = @_;
186    
187     $self->{cur}->key_press ($event->{state}, $event->{keycode}, $event->{time});
188    
189     1
190     }
191    
192     sub on_key_release {
193     my ($self, $event) = @_;
194    
195     $self->{cur}->key_release ($event->{state}, $event->{keycode}, $event->{time});
196    
197     1
198     }
199    
200 root 1.1 sub on_button_press {
201     1
202     }
203    
204     sub on_button_release {
205     my ($self, $event) = @_;
206    
207     if ($event->{row} == 0) {
208 root 1.6 for my $button (@{ $self->{tabofs} }) {
209     $button->[2]->($self, $event)
210     if $event->{col} >= $button->[0]
211     && $event->{col} < $button->[1];
212 root 1.1 }
213     }
214    
215     1
216     }
217    
218     sub on_motion_notify {
219     1
220     }
221    
222     sub on_init {
223     my ($self) = @_;
224    
225 root 1.25 $self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1];
226 root 1.1
227     $self->resource (int_bwidth => 0);
228 root 1.15 $self->resource (name => "URxvt.tabbed");
229 root 1.1 $self->resource (pty_fd => -1);
230    
231     $self->option ($urxvt::OPTION{scrollBar}, 0);
232    
233 root 1.15 my $fg = $self->x_resource ("tabbar-fg");
234     my $bg = $self->x_resource ("tabbar-bg");
235     my $tabfg = $self->x_resource ("tab-fg");
236     my $tabbg = $self->x_resource ("tab-bg");
237    
238     defined $fg or $fg = 3;
239     defined $bg or $bg = 0;
240     defined $tabfg or $tabfg = 0;
241     defined $tabbg or $tabbg = 1;
242    
243     $self->{rs_tabbar} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $fg + 2, $bg + 2);
244     $self->{rs_tab} = urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $tabfg + 2, $tabbg + 2);
245    
246 root 1.1 ()
247     }
248    
249     sub on_start {
250     my ($self) = @_;
251    
252 root 1.4 $self->{tabheight} = $self->int_bwidth + $self->fheight + $self->lineSpace;
253    
254 root 1.15 $self->cmd_parse ("\033[?25l");
255    
256 root 1.21 my @argv = $self->argv;
257    
258     do {
259     shift @argv;
260     } while @argv && $argv[0] ne "-e";
261    
262     $self->new_tab (@argv);
263 root 1.1
264     ()
265     }
266    
267     sub on_configure_notify {
268     my ($self, $event) = @_;
269    
270     $self->configure;
271 root 1.12 $self->refresh;
272 root 1.1
273     ()
274     }
275    
276     sub on_wm_delete_window {
277     my ($self) = @_;
278    
279     $_->destroy for @{ $self->{tabs} };
280    
281     1
282     }
283    
284     sub tab_start {
285     my ($self, $tab) = @_;
286    
287 root 1.11 $tab->XChangeInput ($tab->parent, urxvt::PropertyChangeMask);
288    
289 root 1.1 push @{ $self->{tabs} }, $tab;
290    
291 root 1.7 # $tab->{name} ||= scalar @{ $self->{tabs} };
292 root 1.1 $self->make_current ($tab);
293    
294     ()
295     }
296    
297     sub tab_destroy {
298     my ($self, $tab) = @_;
299    
300     $self->{tabs} = [ grep $_ != $tab, @{ $self->{tabs} } ];
301    
302     if (@{ $self->{tabs} }) {
303     if ($self->{cur} == $tab) {
304     delete $self->{cur};
305     $self->make_current ($self->{tabs}[-1]);
306 root 1.7 } else {
307     $self->refresh;
308 root 1.1 }
309     } else {
310     # delay destruction a tiny bit
311     $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy });
312     }
313    
314     ()
315     }
316    
317 root 1.8 sub tab_key_press {
318     my ($self, $tab, $event, $keysym, $str) = @_;
319    
320 root 1.10 if ($event->{state} & urxvt::ShiftMask) {
321     if ($keysym == 0xff51 || $keysym == 0xff53) {
322     my ($idx) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
323    
324     --$idx if $keysym == 0xff51;
325     ++$idx if $keysym == 0xff53;
326    
327     $self->make_current ($self->{tabs}[$idx % @{ $self->{tabs}}]);
328 sf-exg 1.27
329 root 1.10 return 1;
330     } elsif ($keysym == 0xff54) {
331     $self->new_tab;
332    
333     return 1;
334     }
335 root 1.8 }
336 root 1.22 elsif ($event->{state} & urxvt::ControlMask) {
337     if ($keysym == 0xff51 || $keysym == 0xff53) {
338     my ($idx1) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
339     my $idx2 = ($idx1 + ($keysym == 0xff51 ? -1 : +1)) % @{ $self->{tabs} };
340    
341     ($self->{tabs}[$idx1], $self->{tabs}[$idx2]) =
342     ($self->{tabs}[$idx2], $self->{tabs}[$idx1]);
343    
344     $self->make_current ($self->{tabs}[$idx2]);
345    
346     return 1;
347     }
348     }
349 root 1.8
350     ()
351     }
352    
353 root 1.11 sub tab_property_notify {
354     my ($self, $tab, $event) = @_;
355    
356     $self->copy_properties
357     if $event->{window} == $tab->parent;
358    
359     ()
360     }
361    
362 root 1.9 sub tab_activity {
363     my ($self, $tab) = @_;
364    
365     $self->refresh;
366     }
367    
368 root 1.1 package urxvt::ext::tabbed::tab;
369    
370     # helper extension implementing the subwindows of a tabbed terminal.
371     # simply proxies all interesting calls back to the tabbed class.
372    
373     {
374 root 1.11 for my $hook qw(start destroy key_press property_notify) {
375 root 1.1 eval qq{
376     sub on_$hook {
377     my \$parent = \$_[0]{term}{parent}
378     or return;
379     \$parent->tab_$hook (\@_)
380     }
381     };
382     die if $@;
383     }
384     }
385    
386 root 1.9 sub on_add_lines {
387     $_[0]->{activity}++
388     or $_[0]{term}{parent}->tab_activity ($_[0]);
389     ()
390     }
391 root 1.1
392