App-Music-ChordPro

 view release on metacpan or  search on metacpan

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

		# Empty line
	    }
	    else {
		$ctrl->LineEnd;
		$ctrl->NewLine;
		$ln = $ctrl->GetCurrentLine;
		$line = $ctrl->GetLine($ln);
		#warn("LINE[$ln]: »$line«\n");
	    }
	}
	$ctrl->AddText($pre);
	my $pos = $ctrl->GetCurrentPos;
	$ctrl->AddText($post);
	$ctrl->SetSelection( $pos, $pos );
    }
}

method nl() {
    ("\r\n","\r","\n")[ $self->{t_editor}->GetEOLMode ];
}

method embrace_directive($dir) {
    $self->embrace( "{$dir: ", "}" );
}

method embrace_section($section) {

    unless ( defined($section) ) {
	my $dialog = Wx::TextEntryDialog->new
	  ( $self, "Enter section name",
	    "",
	    "tab" );

	return if $dialog->ShowModal != wxID_OK;
	$section = $dialog->GetValue;
    }

    my $nl = $self->nl;
    $self->embrace( "{start_of_$section}$nl",
		    $nl."{end_of_$section}" );
}

method save_preferences() { 1 }

method update_preferences() {
    $self->refresh;
}

method set_focus {
    $self->{t_editor}->SetFocus;
}

################ Event Handlers (alphabetic order) ################

method OnInsertSymbol($event) {
    unless ( $preferences{enable_insert_symbols} ) {
	my $md = Wx::MessageDialog->new
	  ( undef,
	    "Note: some symbols might not display properly in PDF".
	    " output, even if visible in the Editor. Make sure that".
	    " the symbols you use are supported by your output fonts.\n".
	    "\n".
	    "Continue and suppress future warnings?",
	    "Advanced operation warning",
	    wxYES_NO|wxNO_DEFAULT|wxICON_WARNING|wxDIALOG_NO_PARENT );
	return unless $md->ShowModal == wxID_YES;
	$preferences{enable_insert_symbols} = 1;

    }
    my $ctrl = $self->{t_editor};
    state $sym = "\x{2665}";
    my $d = Wx::SymbolPickerDialog->new( $sym, "", "", $self );
    if ( $d->ShowModal == wxID_OK ) {
	$ctrl->AddText( $sym = $d->GetSymbol );
    }
}

method OnA2Crd($event) {

    my $ctrl = $self->{t_editor};
    my ( $from, $to ) = $ctrl->GetSelection;
    my $have_selection = $from != $to;
    $ctrl->ConvertEOLs(wxSTC_EOL_LF);
    my $text = $have_selection ? $ctrl->GetSelectedText : $ctrl->GetText;

    require ChordPro::A2Crd;
    $::options->{nosysconfig} = 1;
    $::options->{nouserconfig} = 1;
    $::options->{noconfig} = 1;
    $::options->{fragment} = $have_selection;

    # Often text that is pasted from web has additional newlines.
    $text =~ s/^\n+//;
    if ( $text =~ m/(.+\n\n)+/ ) {
	$text =~ s/(.+\n)\n/$1/g;
    }

    my $cho = join
      ( "\n",
	@{ ChordPro::A2Crd::a2crd
	    ( { lines => [ split( /\n/, $text ) ] } ) } ) . "\n";

    if ( $have_selection ) {
	$ctrl->Replace( $from, $to, $cho );
    }
    else {
	$ctrl->Clear;
	$ctrl->SetText($cho);
    }
    $ctrl->SetCurrentPos($from);
}

method OnCharAdded( $event ) {
    my $stc = $self->{t_editor};
    my $key = $event->GetKey;
    return unless chr($key) =~ /[\]\n :\}]/;

    #warn("KEY: ", sprintf("%d 0x%x (%c)\n", $key, $key, $key ));
    my $ln = $stc->GetCurrentLine;
    my $line = $stc->GetLine($ln);
    #$stc->CallTipShow( $stc->GetCurrentPos, "LINE: »$line«");



( run in 0.787 second using v1.01-cache-2.11-cpan-5735350b133 )