1
package Padre::Wx::Diff2;
7
use Padre::Wx::FBP::Diff ();
8
use Wx::Scintilla::Constant ();
9
use Padre::Logger qw(TRACE);
12
our $VERSION = '0.92';
22
my $self = $class->SUPER::new($main);
24
# Bitmap tooltips and icons
25
$self->{prev_diff}->SetBitmapLabel( Padre::Wx::Icon::find("actions/go-up") );
26
$self->{prev_diff}->SetToolTip( Wx::gettext('Previous difference') );
27
$self->{next_diff}->SetBitmapLabel( Padre::Wx::Icon::find("actions/go-down") );
28
$self->{next_diff}->SetToolTip( Wx::gettext('Next difference') );
31
$self->{left_editor}->SetReadOnly(1);
32
$self->{right_editor}->SetReadOnly(1);
40
# TODO replace these with parameter-based stuff once it is working
41
my $left_text = <<'CODE';
49
my $right_text = <<'CODE';
58
# TODO this should be task-based once it is working
59
my $diffs = $self->find_diffs( $left_text, $right_text );
61
# Set the left side text
62
my $left_editor = $self->{left_editor};
63
$self->show_line_numbers($left_editor);
64
$left_editor->SetReadOnly(0);
65
$left_editor->SetText($left_text);
66
$left_editor->SetReadOnly(1);
68
# Set the right side text
69
my $right_editor = $self->{right_editor};
70
$self->show_line_numbers($right_editor);
71
$right_editor->SetReadOnly(0);
72
$right_editor->SetText($right_text);
73
$right_editor->SetReadOnly(1);
75
my $font = Wx::Font->new( 10, Wx::TELETYPE, Wx::NORMAL, Wx::NORMAL );
76
$left_editor->SetFont($font);
77
$right_editor->SetFont($font);
78
$left_editor->StyleSetFont( Wx::Scintilla::Constant::STYLE_DEFAULT, $font );
79
$right_editor->StyleSetFont( Wx::Scintilla::Constant::STYLE_DEFAULT, $font );
81
my $deleted_color = Wx::Colour->new( 0xFF, 0xD8, 0xD8 );
82
my $added_color = Wx::Colour->new( 0xDD, 0xF8, 0xCC );
83
my $text_color = Wx::Colour->new('black');
85
$left_editor->StyleSetForeground( 1, $text_color );
86
$left_editor->StyleSetBackground( 1, $deleted_color );
87
$left_editor->StyleSetEOLFilled( 1, 1 );
88
$left_editor->StyleSetForeground( 2, $text_color );
89
$left_editor->StyleSetBackground( 2, $added_color );
90
$left_editor->StyleSetEOLFilled( 2, 1 );
92
$right_editor->StyleSetForeground( 1, $text_color );
93
$right_editor->StyleSetBackground( 1, $deleted_color );
94
$right_editor->StyleSetEOLFilled( 1, 1 );
95
$right_editor->StyleSetForeground( 2, $text_color );
96
$right_editor->StyleSetBackground( 2, $added_color );
97
$right_editor->StyleSetEOLFilled( 2, 1 );
99
$left_editor->IndicatorSetStyle( 0, Wx::Scintilla::Constant::INDIC_STRIKE );
100
$right_editor->IndicatorSetStyle( 0, Wx::Scintilla::Constant::INDIC_STRIKE );
102
$left_editor->SetCaretLineBackground( Wx::Colour->new('gray') );
103
$right_editor->SetCaretLineBackground( Wx::Colour->new('gray') );
104
$left_editor->SetCaretLineVisible(1);
105
$right_editor->SetCaretLineVisible(1);
107
for my $diff_chunk (@$diffs) {
110
my ( $lines_added, $lines_deleted ) = ( 0, 0 );
111
for my $diff (@$diff_chunk) {
112
my ( $type, $line, $text ) = @$diff;
113
TRACE("$type, $line, $text");
114
if ( $type eq '-' ) {
119
$left_editor->StartStyling( $left_editor->PositionFromLine($line), 0xFF );
120
$left_editor->SetStyling( length($text), 1 );
121
$left_editor->SetIndicatorCurrent(0);
122
$left_editor->IndicatorFillRange( $left_editor->PositionFromLine($line), length($text) );
128
my @lines = split /^/, $text;
129
$left_editor->AnnotationSetText( $line-1, "\n" x (scalar @lines - 1) );
130
$right_editor->StartStyling( $right_editor->PositionFromLine($line), 0xFF );
131
$right_editor->SetStyling( length($text), 2 );
135
# if ( $lines_deleted > 0 && $lines_added > 0 ) {
136
# print "changed!\n";
137
# } elsif ( $lines_deleted > 0 ) {
138
# print "lines deleted\n";
140
# # } elsif ( $lines_added > 0 ) {
141
# print "lines added\n";
145
$left_editor->AnnotationSetVisible(Wx::Scintilla::Constant::ANNOTATION_STANDARD);
146
$right_editor->AnnotationSetVisible(Wx::Scintilla::Constant::ANNOTATION_STANDARD);
153
sub show_line_numbers {
157
my $width = $editor->TextWidth(
158
Wx::Scintilla::Constant::STYLE_LINENUMBER,
159
"m" x List::Util::max( 2, length $editor->GetLineCount )
160
) + 5; # 5 pixel left "margin of the margin
162
$editor->SetMarginWidth(
163
Padre::Constant::MARGIN_LINE,
169
# Find differences between left and right text
171
my ( $self, $left_text, $right_text ) = @_;
173
my @left_seq = split /^/, $left_text;
174
my @right_seq = split /^/, $right_text;
175
my @diff = Algorithm::Diff::diff( \@left_seq, \@right_seq );
179
sub on_prev_diff_click {
180
$_[0]->main->error('on_prev_diff_click');
183
sub on_next_diff_click {
184
$_[0]->main->error('on_next_diff_click');
193
# Copyright 2008-2011 The Padre development team as listed in Padre.pm.
195
# This program is free software; you can redistribute it and/or
196
# modify it under the same terms as Perl 5 itself.