App-Music-ChordPro

 view release on metacpan or  search on metacpan

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

	  ( $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«");
    if ( $key eq ord("]") ) {
	# Complete a chord.
	my $pos = $stc->PositionBefore($stc->GetCurrentPos);
	my $p0 = $stc->BraceMatch($pos);
	return if $p0 < $stc->PositionFromLine($ln);
	$p0 = $stc->PositionAfter($p0);
	my $t = $stc->GetTextRange( $p0, $pos );
	return if $t =~ /\s/;
	if ( $t =~ s/(^|\/)([a-hu])/sprintf("%s%s", $1, uc($2))/ge ) {
	    $stc->SetSelection( $p0, $pos );
	    $stc->ReplaceSelection($t);
	    $stc->CharRight;
	}
    }

    elsif ( $key eq ord("\n") ) {
	# Move newline before trailing } to next line.
	if ( $line =~ /^\}(\r\n|\r|\n)?\Z/ ) {
	    my $nl = $self->nl;
	    my $pos = $stc->GetCurrentPos;
	    $stc->SetSelection( $stc->PositionBefore($pos),
				$stc->PositionAfter($pos) );
	    $stc->CharRightExtend if length($nl) == 2;
	    $stc->ReplaceSelection("}" . $nl);
	}
	elsif ( $ln > 1 && $stc->GetLine($ln-1) =~ /^\{\s*start_of_(\w+).*\}$/ ) {
	    $stc->InsertText( -1, "\n{end_of_$1}" );
	}
    }



( run in 0.759 second using v1.01-cache-2.11-cpan-b16cb0d3907 )