Wx::Perl::EntryList - dynamic list that can be displayed in various controls


Wx-Perl-EntryList documentation Contained in the Wx-Perl-EntryList distribution.

Index


Code Index:

NAME

Top

Wx::Perl::EntryList - dynamic list that can be displayed in various controls

SYNOPSIS

Top

  my $list = Wx::Perl::EntryList->new;
  $list->add_entries_at( 0, [ 'a', 'b', 'c', 'd', 'e' ] );

  # create a view to display it
  my $view = Wx::Perl::EntryList::ListBoxView->new
                 ( $list, sub { return $_[0] }, $parent );

DESCRIPTION

Top

A dynamic list that can be observed (using Class::Publisher) for changes and can be displayed in various controls.

METHODS

Top

new

  my $list = Wx::Perl::EntryList->new;

Creates a list object.

get_entry_at

  my $entry = $list->get_entry_at( $index );

Returns the nth entry of the list.

add_entries_at

  $list->add_entries_at( $index, [ $entry1, $entry2, ... ] );

Add some entries to the list, notifying any listeners.

delete_entry

  $list->delete_entry( $index );

Deletes a single entry from the list, notifying any listeners.

move_entry

  $list->move_entry( $from_index, $to_index );

Moves an entry of the list, notifying any listeners.

count

  my $count = $list->count;

The number of items in the list.

AUTHOR

Top

Mattia Barbon <mbarbon@cpan.org>.

LICENSE

Top

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top

Wx::Perl::EntryList::ListBoxView, Wx::Perl::EntryList::VirtualListCtrlView, Wx::Perl::ListView, Wx::Perl::EntryList::Iterator


Wx-Perl-EntryList documentation Contained in the Wx-Perl-EntryList distribution.
package Wx::Perl::EntryList;

use strict;
use base qw(Class::Publisher Class::Accessor::Fast);

our $VERSION = '0.01';

__PACKAGE__->mk_accessors( qw(entries) );

sub new {
    my( $class ) = @_;
    my $self = $class->SUPER::new( { entries => [] } );

    return $self;
}

sub get_entry_at { return $_[0]->entries->[ $_[1] ] }

sub _add_entries_at {
    my( $self, $index, $entries ) = @_;

    splice @{$self->entries}, $index, 0, @$entries;
}

sub add_entries_at {
    my( $self, $index, $entries ) = @_;

    $self->_add_entries_at( $index, $entries );
    $self->notify_subscribers( 'add_entries',
                               index => $index,
                               count => scalar @$entries,
                               );
}

sub _delete_entries {
    my( $self, $index, $count ) = @_;

    return splice @{$self->entries}, $index, $count;
}

sub delete_entry {
    my( $self, $index ) = @_;

    $self->_delete_entries( $index, 1 );
    $self->notify_subscribers( 'delete_entries',
                               index => $index,
                               count => 1,
                               );
}

sub move_entry {
    my( $self, $from, $to ) = @_;
    my( $entry ) = $self->_delete_entries( $from, 1 );
    $self->_add_entries_at( $to, [ $entry ] );
    $self->notify_subscribers( 'move_entries',
                               from  => $from,
                               to    => $to,
                               count => 1,
                               );
}

sub count    { scalar @{$_[0]->entries} }

sub _fixup_iterator {
    my( $self, $it, $event, %args ) = @_;

    if( $event eq 'add_entries' ) {
        if( $it->current >= $args{index} ) {
            $it->current( $it->current + $args{count} );
        }
    } elsif( $event eq 'delete_entries' ) {
        if( $it->current >= $args{index} ) {
            if( $it->current < $args{index} + $args{count} ) {
                $it->current( 0 );
            } else {
                $it->current( $it->current - $args{count} );
            }
        }
    } elsif( $event eq 'move_entries' ) {
        if(    $it->current >= $args{from}
            && $it->current < $args{from} + $args{count} ) {
            $it->current( $it->current - $args{from} + $args{to} );
        } else {
            $self->_fixup_iterator( $it, 'delete_entries',
                                    index => $args{from},
                                    count => $args{count},
                                    );
            $self->_fixup_iterator( $it, 'add_entries',
                                    index => $args{to},
                                    count => $args{count},
                                    );
        }
    }
}

1;