ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/urxvt-popup
(Generate patch)

Comparing rxvt-unicode/src/perl/urxvt-popup (file contents):
Revision 1.1 by root, Sat Jan 7 20:23:53 2006 UTC vs.
Revision 1.14 by root, Thu Jan 19 17:37:01 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines