summaryrefslogtreecommitdiffstats
path: root/src/perl/urxvt-popup
blob: c1e5ac08cea4ababbe01081b04faae31bb802b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#! perl

# this extension implements popup-menu functionality for urxvt. it works
# together with the urxvt::popup class - "no user serviceable parts inside".

sub refresh {
   my ($self) = @_;

   my $cmd = "\x1b[H";

   my $row = 1;
   for my $item (@{ $self->{data}{item} }) {
      my $rend = "normal";

      if ($row == $self->{hover}) {
         $rend = $self->{press} ? "active" : "hover";
      }

      $cmd .= "$item->{rend}{$rend}\x1b[K";
      $cmd .= $self->locale_encode ($item->{render}->($item));
      $cmd .= "\015\012";

      $row++;
   }

   $self->cmd_parse (substr $cmd, 0, -2);
}

sub on_motion_notify {
   my ($self, $event) = @_;

   delete $self->{hover};

   my ($row, $col) = ($event->{row}, $event->{col});
   if ($col >= 0 && $col < $self->ncol
       && $row >= 0 && $row < @{ $self->{data}{item} }) {
      $self->{hover} = $event->{row} + 1;
   }
   $self->refresh;

   1
}

sub on_button_press {
   my ($self, $event) = @_;

   $self->{press}[$event->{button}] = 1;
   $self->refresh;

   1
}

sub on_button_release {
   my ($self, $event) = @_;

   $self->{press}[$event->{button}] = 0;

   my ($row, $col) = ($event->{row}, $event->{col});
   if ($col >= 0 && $col < $self->ncol
       && $row >= 0 && $row < @{ $self->{data}{item} }) {
      my $item = $self->{data}{item}[$row];
      $item->{activate}->($event, $item);
   }

   $self->refresh;

   if ($event->{button} == $self->{data}{event}{button}) {
      $self->ungrab;
      $self->destroy;
   }

   1
}

sub on_focus_out {
   my ($self) = @_;

   delete $self->{hover};
   $self->refresh;

   ()
}

sub on_init {
   my ($self) = @_;

   my $data = $self->{data} = $urxvt::popup::self;

   $_->{width} = $self->strwidth ($_->{text})
      for @{ $data->{item} };

   $self->resource (title => "URxvt Popup Menu");
   $self->resource (name => "URxvt.popup");

   $self->resource ($_ => $data->{term}->resource ($_))
      for qw(font boldFont italicFont boldItalicFont color+0 color+1);

   my $width  = List::Util::max map $_->{width}, @{ $data->{item} };
   my $height = @{ $data->{item} };

   my $pos = "";

   if ($data->{event}) {
      my $x = int List::Util::max 0, $data->{event}{x_root} - $width * $data->{term}->fwidth  * 0.5;
      my $y = int List::Util::max 0, $data->{event}{y_root} -          $data->{term}->fheight * 0.5;
      $pos = "+$x+$y";
   }

   $self->resource (geometry => "${width}x${height}$pos");

   $self->{term}{urxvt_popup_init_done} = 1;

   ()
}

sub on_start {
   my ($self) = @_;

   $self->cmd_parse ("\x1b[?25l\x1b[?7l");
   $self->refresh;

   # might fail, but try anyways
   $self->grab ($self->{data}{event}{time}, 1)
      and $self->allow_events_async;

   on_button_press $self, $self->{data}{event} if $self->{data}{event}{button};

   ()
}

sub on_map_notify {
   my ($self, $event) = @_;

   # should definitely not fail
   $self->grab ($self->{data}{event}{time}, 1)
      and $self->allow_events_async;
}