App-Wx-PodEditor

 view release on metacpan or  search on metacpan

lib/App/Wx/PodEditor/Frame.pm  view on Meta::CPAN

package App::Wx::PodEditor::Frame;

use strict;
use warnings;

use Wx qw(wxOK wxID_ABOUT wxID_EXIT wxICON_INFORMATION wxVERTICAL wxTOP wxOPEN wxSAVE :everything);
use Wx::Event qw(EVT_MENU EVT_CLOSE EVT_SIZE EVT_UPDATE_UI EVT_KEY_DOWN :everything);

our @ISA = qw(Wx::Frame);

use App::Wx::PodEditor::Actions         qw(:all);
use App::Wx::PodEditor::Actions::File   qw(:all);
use App::Wx::PodEditor::Actions::Format qw(:all);

use File::Basename;
use File::Spec;
use Wx::Perl::PodEditor;
use YAML::Tiny;

our $VERSION = 0.01;

sub new {
    my( $class ) = shift;
    
    my( $this ) = $class->SUPER::new( @_ );

    $this->CreateMyMenuBar();
    
    $this->CreateStatusBar(1);
    $this->SetToolBar( $this->_toolbar );
    $this->GetToolBar->Realize;
    $this->SetStatusText("Welcome!", 0);
    
    # insert main window here
        
    my $main_sizer   = Wx::BoxSizer->new( wxVERTICAL );
    my $editor       = $this->_editor;
    
    $main_sizer->Add( $editor->sizer, 0, wxTOP, 0 );
    
    EVT_MENU( $this, wxID_ABOUT, \&OnAbout );
    EVT_MENU( $this, wxID_EXIT, \&OnQuit );
    EVT_CLOSE( $this, \&OnCloseWindow );
    
    $this;
}

sub _editor {
    my ($self, $panel) = @_;
    
    unless( $self->{editor} ){
        $self->{editor} = Wx::Perl::PodEditor->create( $self, [500,220] );
    }

    $self->{editor};
}

sub CreateMyMenuBar {
    my( $this ) = shift;
    
    my $menuconf    = do{ local $/; <DATA> };
    my $config      = YAML::Tiny->read_string( $menuconf );
    
    my $menubar_def = $config->[0]->{full_menubar};
    my $menubar     = Wx::MenuBar->new();
    
    Wx::InitAllImageHandlers();
    
    for my $menu_def ( @$menubar_def ){
        for my $menu_id (keys %$menu_def){
            my $label = $menu_def->{$menu_id}->{label};
            my $items = $menu_def->{$menu_id}->{items};
            my $menu  = Wx::Menu->new();
            $this->create_static( $menu, $items );
            $menubar->Append( $menu, $label );
        }
    }
    
    $this->SetMenuBar( $menubar );
}

sub create_static{
    my $self     = shift;
    my $menu     = shift;
    my $menu_def = shift;

    return unless ref $menu_def eq 'ARRAY';
    
    for my $elem ( @$menu_def ){
        if( $elem->{type} eq 'item' ){
            my $id    = $elem->{id};
            my $label = $elem->{label};
            $menu->Append( $id, $elem->{label} );
            
            if( $elem->{icon} ){
                my $icon      = _icon( $elem->{icon} );
                my ($tooltip) = (split /\t/, $elem->{label});
                
                $self->_toolbar->AddTool( $elem->{id}, '', $icon, $tooltip );
            }
            
            my ($sub, @params) = split / /, $elem->{sub};
            my $callback = __PACKAGE__->can( $sub );
            
            EVT_MENU( $self, $id, sub{ my ($self,$event) = @_; $callback->( $self,$event, @params) } );
        }
        elsif( $elem->{type} eq 'separator' ){
            $menu->AppendSeparator();
        }
    }
}

sub _toolbar {
    my ($self) = @_;
    
    unless( $self->{toolbar} ){
        $self->{toolbar} = Wx::ToolBar->new( 
            $self, 
            -1, 
            wxDefaultPosition, 
            wxDefaultSize,
            wxNO_BORDER | wxTB_HORIZONTAL | wxTB_FLAT | wxTB_DOCKABLE
        );
    }

    $self->{toolbar};
}

sub _icon {
    my ($file) = @_;
    
    my $dir     = File::Spec->rel2abs( dirname( $0 ) );
    my $icon    = File::Spec->catfile( $dir, 'icons', $file . '.xpm' );
    my $xpm     = Wx::Bitmap->new( $icon, wxBITMAP_TYPE_XPM );
    
    return $xpm;
}

1;



( run in 0.537 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )