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.7 by root, Sun Jan 8 00:29:38 2006 UTC vs.
Revision 1.16 by root, Sun Jun 10 17:31:53 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines