Padre

 view release on metacpan or  search on metacpan

lib/Padre/Wx/Role/Context.pm  view on Meta::CPAN

package Padre::Wx::Role::Context;

=pod

=head1 NAME

Padre::Wx::Role::Context - Role for Wx objects that implement context menus

=head1 DESCRIPTION

=head1 METHODS

=cut

use 5.008;
use strict;
use warnings;
use Padre::Wx ();

our $VERSION    = '1.02';
our $COMPATIBLE = '0.95';





######################################################################
# Main Methods

=pod

=head2 context_bind

    sub new {
        my $class = shift;
        my $self  = $class->SUPER::new(@_);
    
        $self->context_bind('my_menu');
    
        return $self;
    }
    
    sub my_menu {
        # Fill the menu here
    }

The C<context_bind> method binds an context menu event to default
menu creation and popup logic, and specifies the method that should be
called to fill the context menu with the menu entries.

It takes a single optional parameter of the method to be called to fill
the menu.

If no method is provided then the method C<context_menu> will be bound
to the context menu event by default.

=cut

sub context_bind {
	my $self = shift;
	my $method = shift || 'context_menu';
	unless ( defined $method and $self->can($method) ) {
		die "Missing or invalid content menu method '$method'";
	}
	Wx::Event::EVT_CONTEXT(
		$self,
		sub {
			shift->context_popup($method);
		},
	);
}

=pod

=head2 context_popup

    $self->context_popup('context_menu');

The C<context_popup> menu triggers the immediate display of the popup
menu for the object. It takes a compulsory single parameter, which should
be the method to be used to fill the menu with entries.

=cut

sub context_popup {
	my $self   = shift;
	my $method = shift;

	# Create the empty menu
	my $menu = Wx::Menu->new;

	# Fill the menu
	$self->$method($menu);

	# Show the menu at the current cursor position
	$self->PopupMenu( $menu => Wx::DefaultPosition );
}

=pod

=head2 context_menu

The C<context_menu> method is the default method called to fill a context
menu with menu entries.

It should be overloaded in any class that uses the context menu role.

A minimalist default implementation is provided which will show a single
meny entry to launch the C<About Padre> dialog.

=cut

sub context_menu {
	my $self = shift;
	my $menu = shift;
	$self->context_append_action( $menu => 'help.about' );
}





######################################################################
# Menu Construction Methods

=pod

=head2 context_append_function

    $self->context_append_function(
        $menu,
        Wx::gettext('Do Something'),
        sub {
            # Do something
        },
    );

The C<context_append_function> method adds a menu entry bound to an
arbitrary function call.

The function will be passed the parent object (C<$self> in the above
example) and the event object.

=cut



( run in 1.657 second using v1.01-cache-2.11-cpan-364913b4093 )