| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2012-2022 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | 1 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Object::Pad 0.66; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | package Tickit::Widget::MenuBar 0.16; | ||
| 10 | class Tickit::Widget::MenuBar | ||
| 11 | :strict(params) | ||
| 12 | 0 | ▶ | :isa(Tickit::Widget::Menu::base); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use Tickit::Style; | ||
| 15 | |||
| 16 | use Carp; | ||
| 17 | |||
| 18 | use Tickit::RenderBuffer qw( LINE_SINGLE ); | ||
| 19 | use List::Util qw( sum max ); | ||
| 20 | |||
| 21 | # Re-import the constant for compiletime use | ||
| 22 | use constant separator => __PACKAGE__->separator; | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | C<Tickit::Widget::MenuBar> - display a menu horizontally | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use Tickit; | ||
| 31 | use Tickit::Widget::Menu; | ||
| 32 | use Tickit::Widget::Menu::Item; | ||
| 33 | use Tickit::Widget::MenuBar; | ||
| 34 | use Tickit::Widget::VBox; | ||
| 35 | |||
| 36 | my $tickit = Tickit->new; | ||
| 37 | |||
| 38 | my $vbox = Tickit::Widget::VBox->new; | ||
| 39 | $tickit->set_root_widget( $vbox ); | ||
| 40 | |||
| 41 | $vbox->add( Tickit::Widget::MenuBar->new( | ||
| 42 | items => [ | ||
| 43 | ... | ||
| 44 | ] | ||
| 45 | ); | ||
| 46 | |||
| 47 | $vbox->add( ... ); | ||
| 48 | |||
| 49 | $tickit->run; | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | This widget class acts as a container for menu items similar to | ||
| 54 | L<Tickit::Widget::Menu> but displays them horizonally in a single line. This | ||
| 55 | widget is intended to display long-term, such as in the top line of the root | ||
| 56 | window, rather than being used only transiently as a pop-up menu. | ||
| 57 | |||
| 58 | This widget should be used similarly to L<Tickit::Widget::Menu>, except that | ||
| 59 | its name is never useful, and it should be added to a container widget, such | ||
| 60 | as L<Tickit::Widget::VBox>, for longterm display. It does not have a C<popup> | ||
| 61 | or C<dismiss> method. | ||
| 62 | |||
| 63 | A single separator object can be added as an item, causing all the items after | ||
| 64 | it to be right-justified. | ||
| 65 | |||
| 66 | =head1 STYLE | ||
| 67 | |||
| 68 | The default style pen is used as the widget pen. The following style pen | ||
| 69 | prefixes are also used: | ||
| 70 | |||
| 71 | =over 4 | ||
| 72 | |||
| 73 | =item highlight => PEN | ||
| 74 | |||
| 75 | The pen used to highlight the active menu selection | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | The following style actions are used: | ||
| 80 | |||
| 81 | =over 4 | ||
| 82 | |||
| 83 | =item highlight_next (<Right>) | ||
| 84 | |||
| 85 | =item highlight_prev (<Left>) | ||
| 86 | |||
| 87 | Highlight the next or previous item | ||
| 88 | |||
| 89 | =item highlight_first (<F10>) | ||
| 90 | |||
| 91 | Highlight the first menu item | ||
| 92 | |||
| 93 | =item activate (<Enter>) | ||
| 94 | |||
| 95 | Activate the highlighted item | ||
| 96 | |||
| 97 | =item dismiss (<Escape>) | ||
| 98 | |||
| 99 | Dismiss the menu | ||
| 100 | |||
| 101 | =back | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | style_definition base => | ||
| 106 | rv => 1, | ||
| 107 | highlight_rv => 0, | ||
| 108 | highlight_bg => "green", | ||
| 109 | "<Right>" => "highlight_next", | ||
| 110 | "<Left>" => "highlight_prev", | ||
| 111 | "<F10>" => "highlight_first", | ||
| 112 | "<Enter>" => "activate", | ||
| 113 | "<Escape>" => "dismiss"; | ||
| 114 | |||
| 115 | use constant KEYPRESSES_FROM_STYLE => 1; | ||
| 116 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 117 | |||
| 118 | field @_itempos; | ||
| 119 | |||
| 120 | method lines () | ||
| 121 | { | ||
| 122 | return 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | method cols () | ||
| 126 | { | ||
| 127 | return sum( map { $self->_itemwidth( $_ ) } 0 .. $self->items-1 ) + 2 * ( $self->items - 1 ); | ||
| 128 | } | ||
| 129 | |||
| 130 | method push_item ( $item ) | ||
| 131 | { | ||
| 132 | if( $item == separator and grep { $_ == separator } $self->items ) { | ||
| 133 | croak "Cannot have more than one separator in a MenuBar"; | ||
| 134 | } | ||
| 135 | |||
| 136 | $self->SUPER::push_item( $item ); | ||
| 137 | } | ||
| 138 | |||
| 139 | method reshape () | ||
| 140 | { | ||
| 141 | @_itempos = (); | ||
| 142 | |||
| 143 | my @items = $self->items; | ||
| 144 | my $col = 0; | ||
| 145 | my $separator_at; | ||
| 146 | foreach my $idx ( 0 .. $#items ) { | ||
| 147 | $separator_at = $idx, next if $items[$idx] == separator; | ||
| 148 | |||
| 149 | $_itempos[$idx] = [ $col, undef ]; | ||
| 150 | $col += $self->_itemwidth( $idx ); | ||
| 151 | $_itempos[$idx][1] = $col; | ||
| 152 | $col += 2; | ||
| 153 | } | ||
| 154 | |||
| 155 | if( defined $separator_at ) { | ||
| 156 | $col -= 2; # undo | ||
| 157 | my $spare = $self->window->cols - $col; | ||
| 158 | |||
| 159 | $_itempos[$_][0] += $spare, $_itempos[$_][1] += $spare for $separator_at+1 .. $#items; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | method pos2item ( $line, $col ) | ||
| 164 | { | ||
| 165 | $line == 0 or return (); | ||
| 166 | |||
| 167 | my @items = $self->items; | ||
| 168 | |||
| 169 | foreach my $idx ( 0 .. $#items ) { | ||
| 170 | next if !defined $_itempos[$idx]; # separator | ||
| 171 | last if $col < $_itempos[$idx][0]; | ||
| 172 | next unless $col < $_itempos[$idx][1]; | ||
| 173 | |||
| 174 | $col -= $_itempos[$idx][0]; | ||
| 175 | |||
| 176 | return () if $col < 0; | ||
| 177 | return ( $items[$idx], $idx, $col ); | ||
| 178 | } | ||
| 179 | |||
| 180 | return (); | ||
| 181 | } | ||
| 182 | |||
| 183 | method redraw_item ( $idx ) | ||
| 184 | { | ||
| 185 | $self->window->expose( Tickit::Rect->new( | ||
| 186 | top => 0, lines => 1, | ||
| 187 | left => $_itempos[$idx][0], | ||
| 188 | right => $_itempos[$idx][1], | ||
| 189 | ) ); | ||
| 190 | } | ||
| 191 | |||
| 192 | method render_to_rb ( $rb, $rect ) | ||
| 193 | { | ||
| 194 | if( $rect->top == 0 ) { | ||
| 195 | $rb->goto( 0, 0 ); | ||
| 196 | |||
| 197 | my @items = $self->items; | ||
| 198 | foreach my $idx ( 0 .. $#items ) { | ||
| 199 | my $item = $items[$idx]; | ||
| 200 | next if $item == separator; | ||
| 201 | |||
| 202 | my ( $left, $right ) = $_itempos[$idx]->@*; | ||
| 203 | last if $left > $rect->right; | ||
| 204 | next if $right < $rect->left; | ||
| 205 | |||
| 206 | $rb->erase_to( $left ); | ||
| 207 | |||
| 208 | my $pen = defined $self->_active_idx && $idx == $self->_active_idx | ||
| 209 | ? $self->get_style_pen( "highlight" ) : undef; | ||
| 210 | |||
| 211 | $rb->savepen; | ||
| 212 | $rb->setpen( $pen ); | ||
| 213 | |||
| 214 | $item->render_label( $rb, $right - $left, $self ); | ||
| 215 | |||
| 216 | $rb->restore; | ||
| 217 | } | ||
| 218 | |||
| 219 | $rb->erase_to( $rect->right ); | ||
| 220 | } | ||
| 221 | |||
| 222 | foreach my $line ( $rect->linerange( 1, undef ) ) { | ||
| 223 | $rb->erase_at( $line, $rect->left, $rect->cols ); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | method popup_item ( $idx ) | ||
| 228 | { | ||
| 229 | my $item = $self->item( $idx ); | ||
| 230 | |||
| 231 | my $col = $_itempos[$idx][0]; | ||
| 232 | |||
| 233 | my $rightmost = $self->window->cols - $item->cols; | ||
| 234 | $col = $rightmost if $col > $rightmost; | ||
| 235 | |||
| 236 | $item->popup( $self->window, 1, $col ); | ||
| 237 | } | ||
| 238 | |||
| 239 | method activated () | ||
| 240 | { | ||
| 241 | $self->dismiss; | ||
| 242 | } | ||
| 243 | |||
| 244 | method dismiss () | ||
| 245 | { | ||
| 246 | $self->SUPER::dismiss; | ||
| 247 | |||
| 248 | # Still have a window after ->dismiss | ||
| 249 | $self->redraw; | ||
| 250 | } | ||
| 251 | |||
| 252 | method on_key ( $ ) | ||
| 253 | { | ||
| 254 | # Always eat all the keys as there's never anything higher to pass them to | ||
| 255 | return 1; | ||
| 256 | } | ||
| 257 | |||
| 258 | # MenuBar always expands on highlight | ||
| 259 | method key_highlight_next ( $ev ) | ||
| 260 | { | ||
| 261 | $self->SUPER::key_highlight_next( $ev ); | ||
| 262 | $self->expand_item( $self->_active_idx ); | ||
| 263 | } | ||
| 264 | |||
| 265 | method key_highlight_prev ( $ev ) | ||
| 266 | { | ||
| 267 | $self->SUPER::key_highlight_prev( $ev ); | ||
| 268 | $self->expand_item( $self->_active_idx ); | ||
| 269 | } | ||
| 270 | |||
| 271 | method key_highlight_first ( $ ) | ||
| 272 | { | ||
| 273 | defined $self->_active_idx or $self->expand_item( 0 ); | ||
| 274 | return 1; | ||
| 275 | } | ||
| 276 | |||
| 277 | method on_mouse_item ( $args, $item, $item_idx, $item_col ) | ||
| 278 | { | ||
| 279 | # We only ever care about button 1 | ||
| 280 | return unless $args->button == 1; | ||
| 281 | |||
| 282 | my $event = $args->type; | ||
| 283 | if( $event eq "press" ) { | ||
| 284 | # A second click on an active item deactivates | ||
| 285 | if( defined $self->_active_idx and $item_idx == $self->_active_idx ) { | ||
| 286 | $self->dismiss; | ||
| 287 | } | ||
| 288 | else { | ||
| 289 | $self->expand_item( $item_idx ); | ||
| 290 | } | ||
| 291 | } | ||
| 292 | elsif( $event eq "drag" ) { | ||
| 293 | $self->expand_item( $item_idx ); | ||
| 294 | } | ||
| 295 | |||
| 296 | return 1; | ||
| 297 | } | ||
| 298 | |||
| 299 | =head1 AUTHOR | ||
| 300 | |||
| 301 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | 0x55AA; | ||