~ubuntu-branches/ubuntu/quantal/padre/quantal

« back to all changes in this revision

Viewing changes to lib/Padre/Wx/Diff2.pm

  • Committer: Package Import Robot
  • Author(s): Dominique Dumont, gregor herrmann, Dominique Dumont
  • Date: 2012-01-04 12:04:20 UTC
  • mfrom: (1.3.3)
  • Revision ID: package-import@ubuntu.com-20120104120420-i5oybqwf91m1d3il
Tags: 0.92.ds1-1
[ gregor herrmann ]
* Remove debian/source/local-options; abort-on-upstream-changes
  and unapply-patches are default in dpkg-source since 1.16.1.
* Swap order of alternative (build) dependencies after the perl
  5.14 transition.

[ Dominique Dumont ]
* Imported Upstream version 0.92.ds1
* removed fix-spelling patch (applied upstream)
* lintian-override: use wildcard to avoid listing a gazillion files
* updated size of some 'not-real-man-page' entries
* rules: remove dekstop cruft (replaced by a file provided in debian
  directory)
* control: removed Breaks statement. Add /me to uploaders. Updated
  dependencies
* rules: make sure that non-DFSG file (i.e. the cute butterfly, sigh)
  is not distributed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Padre::Wx::Diff2;
 
2
 
 
3
use 5.008;
 
4
use strict;
 
5
use warnings;
 
6
use Padre::Wx               ();
 
7
use Padre::Wx::FBP::Diff    ();
 
8
use Wx::Scintilla::Constant ();
 
9
use Padre::Logger qw(TRACE);
 
10
 
 
11
 
 
12
our $VERSION = '0.92';
 
13
our @ISA     = qw{
 
14
        Padre::Wx
 
15
        Padre::Wx::FBP::Diff
 
16
};
 
17
 
 
18
# Constructor
 
19
sub new {
 
20
        my $class = shift;
 
21
        my $main  = shift;
 
22
        my $self  = $class->SUPER::new($main);
 
23
 
 
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') );
 
29
 
 
30
        # Readonly!
 
31
        $self->{left_editor}->SetReadOnly(1);
 
32
        $self->{right_editor}->SetReadOnly(1);
 
33
 
 
34
        return $self;
 
35
}
 
36
 
 
37
sub show {
 
38
        my $self = shift;
 
39
 
 
40
        # TODO replace these with parameter-based stuff once it is working
 
41
        my $left_text = <<'CODE';
 
42
1
 
43
2
 
44
3
 
45
4
 
46
5
 
47
7
 
48
CODE
 
49
        my $right_text = <<'CODE';
 
50
1
 
51
1.1
 
52
2
 
53
4
 
54
5
 
55
8
 
56
CODE
 
57
 
 
58
        # TODO this should be task-based once it is working
 
59
        my $diffs = $self->find_diffs( $left_text, $right_text );
 
60
 
 
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);
 
67
 
 
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);
 
74
 
 
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 );
 
80
 
 
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');
 
84
 
 
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 );
 
91
 
 
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 );
 
98
 
 
99
        $left_editor->IndicatorSetStyle( 0, Wx::Scintilla::Constant::INDIC_STRIKE );
 
100
        $right_editor->IndicatorSetStyle( 0, Wx::Scintilla::Constant::INDIC_STRIKE );
 
101
 
 
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);
 
106
 
 
107
        for my $diff_chunk (@$diffs) {
 
108
                TRACE("new_chunk");
 
109
 
 
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 '-' ) {
 
115
 
 
116
                                $lines_deleted++;
 
117
 
 
118
                                # left side
 
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) );
 
123
                        } else {
 
124
 
 
125
                                # right side
 
126
                                $lines_added++;
 
127
 
 
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 );
 
132
                        }
 
133
                }
 
134
 
 
135
                # if ( $lines_deleted > 0 && $lines_added > 0 ) {
 
136
                # print "changed!\n";
 
137
                # } elsif ( $lines_deleted > 0 ) {
 
138
                # print "lines deleted\n";
 
139
 
 
140
                # #             } elsif ( $lines_added > 0 ) {
 
141
                # print "lines added\n";
 
142
                # }
 
143
        }
 
144
 
 
145
        $left_editor->AnnotationSetVisible(Wx::Scintilla::Constant::ANNOTATION_STANDARD);
 
146
        $right_editor->AnnotationSetVisible(Wx::Scintilla::Constant::ANNOTATION_STANDARD);
 
147
 
 
148
        $self->Show;
 
149
 
 
150
        return;
 
151
}
 
152
 
 
153
sub show_line_numbers {
 
154
        my $self   = shift;
 
155
        my $editor = shift;
 
156
 
 
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
 
161
 
 
162
        $editor->SetMarginWidth(
 
163
                Padre::Constant::MARGIN_LINE,
 
164
                $width,
 
165
        );
 
166
        return;
 
167
}
 
168
 
 
169
# Find differences between left and right text
 
170
sub find_diffs {
 
171
        my ( $self, $left_text, $right_text ) = @_;
 
172
 
 
173
        my @left_seq  = split /^/, $left_text;
 
174
        my @right_seq = split /^/, $right_text;
 
175
        my @diff = Algorithm::Diff::diff( \@left_seq, \@right_seq );
 
176
        return \@diff;
 
177
}
 
178
 
 
179
sub on_prev_diff_click {
 
180
        $_[0]->main->error('on_prev_diff_click');
 
181
}
 
182
 
 
183
sub on_next_diff_click {
 
184
        $_[0]->main->error('on_next_diff_click');
 
185
}
 
186
 
 
187
sub on_close_click {
 
188
        $_[0]->Destroy;
 
189
}
 
190
 
 
191
1;
 
192
 
 
193
# Copyright 2008-2011 The Padre development team as listed in Padre.pm.
 
194
# LICENSE
 
195
# This program is free software; you can redistribute it and/or
 
196
# modify it under the same terms as Perl 5 itself.