App-Music-ChordPro

 view release on metacpan or  search on metacpan

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

    my $c = $prefs->{editcolour}{$theme};
    my $fg = Wx::Colour->new($c->{fg});
    my $bg = Wx::Colour->new($c->{bg});

    $stc->SetBackgroundColour($bg);
    $stc->SetCaretForeground($fg);

    $stc->StyleSetForeground( wxSTC_STYLE_DEFAULT, $fg );
    $stc->StyleSetBackground( wxSTC_STYLE_DEFAULT, $bg );
    $stc->StyleClearAll;

    # 0 - basic
    $stc->StyleSetForeground( 0, $fg );
    $stc->StyleSetBackground( 0, $bg );
    # 1 - comments (grey)
    $stc->StyleSetForeground( 1, Wx::Colour->new($c->{s1}) );
    $stc->StyleSetBackground( 1, $bg );
    # 2 - Keywords (grey)
    $stc->StyleSetForeground( 2, Wx::Colour->new($c->{s2}) );
    $stc->StyleSetBackground( 2, $bg );
    # 3 - Brackets (grey)
    $stc->StyleSetForeground( 3, Wx::Colour->new($c->{s3}) );
    $stc->StyleSetBackground( 3, $bg );
    # 4 - Chords (red)
    $stc->StyleSetForeground( 4, Wx::Colour->new($c->{s4}) );
    $stc->StyleSetBackground( 4, $bg );
    # 5 - Directives (blue, same as status label colour)
    $stc->StyleSetForeground( 5, Wx::Colour->new($c->{s5}) );
    $stc->StyleSetBackground( 5, $bg );
    # 6 - Directive arguments (orange, same as toolbar icon colour)
    $stc->StyleSetForeground( 6, Wx::Colour->new($c->{s6}) );
    $stc->StyleSetBackground( 6, $bg );
    # 7 - Errors
    $stc->StyleSetForeground( 7, $fg );
    $stc->StyleSetBackground( 7, wxRED );

    # For linenumbers.
    $stc->StyleSetForeground( wxSTC_STYLE_LINENUMBER,
			      Wx::Colour->new( $c->{numfg} ) );
    $stc->StyleSetBackground( wxSTC_STYLE_LINENUMBER,
			      Wx::Colour->new( $c->{numbg} ) );

    # For annotations.
    $self->{astyle} //= 1 + wxSTC_STYLE_LASTPREDEFINED;
    $stc->StyleSetBackground( $self->{astyle}, Wx::Colour->new($c->{annbg}) );
    $stc->StyleSetForeground( $self->{astyle}, Wx::Colour->new($c->{annfg}) );

    $stc->SetFont( Wx::Font->new($prefs->{editfont}) );

    # Wrapping.
    if ( $prefs->{editorwrap} ) {
	$stc->SetWrapMode(3); # wxSTC_WRAP_WHITESPACE );
	$stc->SetWrapStartIndent( $prefs->{editorwrapindent} );
    }
    else {
	$stc->SetWrapMode(0); # wxSTC_WRAP_NONE );
    }

    $self->style_text;
    # Expert...
    $stc->SetViewEOL( $state{vieweol} );
    $stc->SetViewWhiteSpace( $state{viewws} );
    $stc->SetViewLineNumbers(1);

    # Free Ctrl-Shift-U for iBus input (doesn't work).
    # $stc->CmdKeyClear( ord('U'), 3 ); # Ctrl-Shift-U
}

sub SetViewLineNumbers( $self, $b ) {
    $self->SetMarginWidth( 0, $b ? 40 : 0 ); # TODO length
    if ( $self->can("SetMarginBackground") ) { # wxPerl 3.005
	$self->SetMarginType( 1, wxSTC_MARGIN_SYMBOL|wxSTC_MARGIN_COLOUR );
	$self->SetMarginWidth( 1, 3 );
	my $theme = $state{editortheme};
	my $c = $preferences{editcolour}{$theme};
	my $bg = Wx::Colour->new($c->{bg});
	$self->SetMarginBackground( 1, $bg );
    }
    else {
	$self->SetMarginWidth( 1, 0 );
    }
}

sub style_text( $self ) {
    my $stc = $self;

    # Scintilla uses byte indices.
    require Encode;
    my $text  = Encode::encode_utf8($stc->GetText);

    my $style = sub {
	my ( $re, @styles ) = @_;
	pos($text) = 0;
	while ( $text =~ m/$re/g ) {
	    my @s = @styles;
	    die("!!! ", scalar(@{^CAPTURE}), ' ', scalar(@s)) unless @s == @{^CAPTURE};
	    my $end = pos($text);
	    my $start = $end - length($&);
	    my $group = 0;
	    while ( $start < $end ) {
		my $l = length(${^CAPTURE[$group++]});
		if ( $Wx::VERSION < 3.006 ) {
		    $stc->StartStyling( $start, 0 );
		}
		else {
		    $stc->StartStyling( $start );
		}
		$stc->SetStyling( $l, shift(@s) );
		$start += $l;
	    }
	}
    };

    # Comments/
    $style->( qr/^(#.*)/m, 1 );
    # Directives.
    $style->( qr/^([ \t]*)(\{)([-\w!]+)(.*)(\})/m, 7, 3, 5, 6, 3 );
    $style->( qr/^([ \t]*)(\{)([-\w!]+)([: ])(.*)(\})/m, 7, 3, 5, 3, 6, 3 );
    # Chords.
    $style->( qr/(\[)([^\[\]\s]*)(\])/m, 3, 4, 3 );



( run in 0.526 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )