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