App-Music-ChordPro

 view release on metacpan or  search on metacpan

lib/ChordPro/Wx/Main.pm  view on Meta::CPAN


################ Static & Overrides ################

use Wx qw[:everything];
use ChordPro::Wx::Utils;

# Synchronous system call. Used in ChordPro::Utils module.
sub ::sys { Wx::ExecuteArgs( \@_, wxEXEC_SYNC | wxEXEC_HIDE_CONSOLE ); }

use warnings 'redefine';

################ Main ################

use Object::Pad;

class ChordPro::Wx::Main :isa(ChordPro::Wx::Main_wxg);

use ChordPro;	our $VERSION = $ChordPro::VERSION;
use ChordPro::Files;
use ChordPro::Paths;
use ChordPro::Output::Common;
use ChordPro::Utils qw( demarkup );

use Wx qw[:everything];
use Wx::Locale gettext => '_T';

use ChordPro::Wx::Config;
use ChordPro::Wx::Utils;

use Encode qw(decode_utf8);

method log( $level, $msg ) {
    $msg =~ s/\n+$//;

    #    $msg = "[$level] $msg";
    if ( $level eq 'I' ) {
	Wx::LogMessage( "%s", $msg);
    }
    if ( $level eq 'S' ) {
	Wx::LogMessage( "%s", $msg );
    }
    elsif ( $level eq 'W' ) {
	Wx::LogWarning( "%s", $msg);
    }
    elsif ( $level eq 'E' ) {
	Wx::LogError( "%s", $msg);
    }
    elsif ( $level eq 'F' ) {
	Wx::LogFatal( "%s", $msg);
    }
}

BUILD {

    Wx::Event::EVT_IDLE($self, $self->can('OnIdle'));
    Wx::Event::EVT_CLOSE($self, $self->can('OnClose'));

    # Later, the panels will take over logging.
    Wx::Log::SetActiveTarget( Wx::LogStderr->new );
    $self->SetTitle("ChordPro");
    $self->SetIcon( Wx::Icon->new(CP->findres( "chordpro-icon.png", class => "icons" ), wxBITMAP_TYPE_ANY) );

    $self->attach_events;

    # MacOS file dialogs always filters with all wildcards. So if there is
    # an "All files|*.*" at the end, all file will match.
    # So either remove the *.* or use the following code:
    Wx::SystemOptions::SetOption("osx.openfiledialog.always-show-types", 1)
	if 0 && is_macos;
}

method attach_events() {

    # To select actions, we use a panel with a bitmap and a text.
    # We need to attach a mouse click (EVT_LEFT_UP) to the panel and
    # all of its children.

    my %panels =
      ( new	  => "OnNew",
	open      => "OnOpen",
	sbexport  => "OnExportFolder",
	example   => "OnHelp_Example",
	site      => "OnHelp_Site",
	help      => "OnHelp_Docs",
      );
    while ( my ( $p, $handler ) = each %panels ) {
	my $panel = $self->{"pn_$p"};
	#$handler = "OnI".ucfirst($p);
	my $h = $self->can($handler);
	warn("Missing pn handler $handler") unless $h;
	$handler = sub { &$h( $self, undef ) };
	Wx::Event::EVT_LEFT_UP( $panel, $handler );
	foreach my $n ( $panel->GetChildren ) {
	    Wx::Event::EVT_LEFT_UP( $n, $handler );
	}
    }

    Wx::Event::EVT_MAXIMIZE( $self, $self->can("OnMaximized") );
}

method select_mode( $mode ) {
    my @panels = panels;

    if ( $mode eq "initial" ) {
	$self->{$_}->Show(0) for @panels;
	$self->{p_initial}->Show(1);
	$self->refresh;
	$self->SetTitle("ChordPro");
    }
    else {
	$self->{p_initial}->Show(0);
	$self->{$_}->Show( $_ eq "p_$mode" ) for @panels;
	$self->{"p_$mode"}->refresh;
    }
    $self->{sz_main}->Layout;
    $state{mode} = $mode;
    return $state{panel} = $self->{"p_$mode"};
}

# Explicit (re)initialisation of this class.
method init( $options ) {

lib/ChordPro/Wx/Main.pm  view on Meta::CPAN

    my $year = 1900 + (localtime(time))[5];
    if ( $year != $firstyear ) {
	$year = "$firstyear,$year";
    }

    # Sometimes version numbers are localized...
    my $dd = sub { my $v = $_[0]; $v =~ s/,/./g; $v };

    local $ENV{CHORDPRO_LIB} =
      $preferences{enable_customlib} ? $preferences{customlib} : "";
    CP->setup_resdirs;
    my $msg = join
      ( "",
	"ChordPro version ",
	$dd->($ChordPro::VERSION),
	"\n",
	"https://www.chordpro.org\n",
	"Copyright $year Johan Vromans <jvromans\@squirrel.nl>\n",
	"\n",
	"GUI designed with wxGlade by the ChordPro Team\n\n",
	"Run-time information:\n",
	::runtimeinfo() =~ s/CHORDPRO_LIB/Custom lib  /rm
      );

    return $msg;
}

method check_saved() {
    for ( panels ) {
	return unless $self->{$_}->check_source_saved;
	return unless $self->{$_}->check_preview_saved;
    }
    # Panels may save prefs to preferences.
    $self->{$_}->save_preferences for panels;
    1;
}

method setup_statusbar() {
    # Add statusbar.
    return unless $preferences{expert};
    $self->{f_main_statusbar} = $self->CreateStatusBar(1);
    $self->{f_main_statusbar}->SetStatusWidths(-1);
}

################ Event handlers (alphabetic order) ################

# This method is called from the helper panels.
method OnAbout($event) {

    my $info = Wx::AboutDialogInfo->new;
    my $year = 1900 + (localtime(time))[5];
    $info->SetName("ChordPro");
    $info->SetVersion( $VERSION .
		       ( $VERSION =~ /_/ ? " (unsupported development snapshot)" : "" ) );
    $info->SetDescription("ChordPro is free software");
    $info->SetCopyright("â’¸ 2016-$year Johan Vromans\nThe ChordPro Team");
    $info->SetWebSite( "https://www.chordpro.org",
		       "Visit the ChordPro web site");
    my $icon = Wx::Icon->new;
    $info->SetIcon($icon)
      if $icon->LoadFile( CP->findres("chordpro-splash.png",class=>"icons"),wxBITMAP_TYPE_PNG );
    Wx::AboutBox($info);
}

method OnClose($event) {
    return unless $self->check_saved;
    # Save preferences to persistent storage.
    $self->save_preferences;
    # Stop webview process (GTK).
    if ( $self->{panel} && $self->{panel}->{webview} ) {
	$self->{panel}->{webview}->Close;
    }
    $self->Destroy;
}

# SHow the create buttons, or the recents list.
method OnCreateRecent($event) {
    $self->create_or_recent( $self->{rb_createrecent}->GetSelection );
}

method OnExportFolder($event) {

    # We handle this here for the same reasons as OnOpen.
    my $fd = Wx::DirDialog->new
      ( $self,
	_T("Select the folder with the songs"),
	$state{sbe_folder} // $state{songbookexport}{folder} // "",
	wxDD_DIR_MUST_EXIST );
    my $ret = $fd->ShowModal;
    if ( $ret == wxID_OK ) {
	$self->select_mode("sbexport")->open_dir( $fd->GetPath );
    }
    $fd->Destroy;
}

method OnIdle($event) {
    # Cannot check from init. Do it here.
    unless ( ChordPro::Wx::Config->Ok ) {
	ChordPro::Wx::Config->SetOk;
	my $md = Wx::MessageDialog->new
	  ( undef,
	    "Your Settings have been migrated.\n".
	    "Some Settings may have been reset to default values.\n".
	    "\n".
	    "Sorry for the inconvenience.",
	    "Check your Settings",
	    Wx::wxOK|Wx::wxICON_WARNING|Wx::wxDIALOG_NO_PARENT );
	$md->ShowModal;
    }
    return if $self->{p_initial}->IsShown;
    my $mod = $self->{p_editor}->{t_editor}->IsModified;
    my $f = fn_basename($state{windowtitle} // "ChordPro");
    if ( is_macos ) {
	wxTheApp->GetTopWindow->OSXSetModified($mod);
    }
    else {
	$f .= " (modified)" if $mod;
    }
    $f = "ChordPro — $f" if $state{windowtitle};
    $self->SetTitle($f);



( run in 0.866 second using v1.01-cache-2.11-cpan-df04353d9ac )