ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/urxvt-popup
Revision: 1.8
Committed: Sun Jan 8 00:34:57 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.7: +5 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3 root 1.2 # this extension implements popup-menu functionality for urxvt. it works
4     # together with the urxvt::popup class.
5    
6 root 1.1 sub refresh {
7     my ($self) = @_;
8    
9     my $cmd = "\x1b[H";
10    
11     my $row = 1;
12     for my $item (@{ $self->{data}{item} }) {
13     my $rend = "\x1b[30;47m";
14    
15     if ($row == $self->{hover}) {
16     $rend = $self->{press} ? "\x1b[m" : "\x1b[30;46m";
17     }
18    
19 root 1.5 $cmd .= "$rend\x1b[K";
20     $cmd .= $self->locale_encode ($item->{render}->($item));
21     $cmd .= "\015\012";
22 root 1.1
23     $row++;
24     }
25    
26     $self->cmd_parse (substr $cmd, 0, -2);
27     }
28    
29     sub on_motion_notify {
30     my ($self, $event) = @_;
31    
32     $self->{hover} = $event->{row} + 1;
33 root 1.8 $self->refresh;
34 root 1.1
35     1
36     }
37    
38     sub on_button_press {
39     my ($self, $event) = @_;
40    
41     $self->{press}[$event->{button}] = 1;
42 root 1.8 $self->refresh;
43 root 1.1
44     1
45     }
46    
47     sub on_button_release {
48     my ($self, $event) = @_;
49    
50     my $row = $event->{row};
51     my $col = $event->{col};
52    
53     if ($event->{button} == $self->{data}{event}{button}) {
54     $self->ungrab;
55     $self->destroy;
56     }
57    
58 root 1.3 $self->{press}[$event->{button}] = 0;
59    
60 root 1.7 if ($col >= 0 && $col < $self->ncol
61     && $row >= 0 && $row < @{ $self->{data}{item} }) {
62     $self->{data}{item}[$row]{activate}->($event);
63 root 1.1 }
64    
65 root 1.8 $self->refresh;
66    
67 root 1.1 1
68     }
69    
70     sub on_focus_out {
71     my ($self) = @_;
72    
73     delete $self->{hover};
74 root 1.8 $self->refresh;
75 root 1.1
76     ()
77     }
78    
79     sub on_init {
80     my ($self) = @_;
81    
82     my $data = $self->{data} = $urxvt::popup::self;
83    
84     $_->{width} = $self->strwidth ($_->{text})
85     for @{ $data->{item} };
86    
87     $self->resource ($_ => $data->{term}->resource ($_))
88     for qw(font boldFont italicFont boldItalicFont color+0 color+1);
89    
90     my $width = List::Util::max map $_->{width}, @{ $data->{item} };
91     my $height = @{ $data->{item} };
92    
93     my $pos = "";
94    
95     if ($data->{event}) {
96     my $x = int List::Util::max 0, $data->{event}{x_root} - $width * $data->{term}->fwidth * 0.5;
97     my $y = int List::Util::max 0, $data->{event}{y_root} - $data->{term}->fheight * 0.5;
98    
99     $pos = "+$x+$y";
100     }
101    
102     $self->resource (geometry => "${width}x${height}$pos");
103    
104     ()
105     }
106    
107     sub on_start {
108     my ($self) = @_;
109    
110     # might fail, but try anyways
111     $self->grab ($self->{data}{event}{time}, 1)
112 root 1.3 and $self->allow_events_async;
113 root 1.1
114     on_button_press $self, $self->{data}{event} if $self->{data}{event}{button};
115    
116     $self->cmd_parse ("\x1b[?25l\x1b[?7l");
117     refresh $self;
118    
119     ()
120     }
121    
122     sub on_map_notify {
123     my ($self, $event) = @_;
124    
125     # should definitely not fail
126     $self->grab ($self->{data}{event}{time}, 1)
127 root 1.3 and $self->allow_events_async;
128 root 1.1 }
129    
130    
131    
132    
133    
134