~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/VersionControl/MonoDevelop.VersionControl/MonoDevelop.VersionControl.Views/DiffWidget.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
Import upstream version 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// DiffWidget.cs
 
3
//  
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Linq;
 
28
using MonoDevelop.Core;
 
29
 
 
30
namespace MonoDevelop.VersionControl.Views
 
31
{
 
32
        [System.ComponentModel.ToolboxItem(false)]
 
33
        public partial class DiffWidget : Gtk.Bin
 
34
        {
 
35
                VersionControlDocumentInfo info;
 
36
                Mono.TextEditor.TextEditor diffTextEditor;
 
37
                MonoDevelop.VersionControl.Views.ComparisonWidget comparisonWidget;
 
38
 
 
39
                internal ComparisonWidget ComparisonWidget {
 
40
                        get {
 
41
                                return comparisonWidget;
 
42
                        }
 
43
                }
 
44
                
 
45
                string LabelText {
 
46
                        get {
 
47
                                if (comparisonWidget.Diff.Count == 0)
 
48
                                        return GettextCatalog.GetString ("Both files are equal");
 
49
                                int added=0, removed=0;
 
50
                                foreach (var h in comparisonWidget.Diff) {
 
51
                                        added += h.Inserted;
 
52
                                        removed += h.Removed;
 
53
                                }
 
54
                                string changes = string.Format (GettextCatalog.GetPluralString ("{0} change", "{0} changes", comparisonWidget.Diff.Count), comparisonWidget.Diff.Count);
 
55
                                string additions = string.Format (GettextCatalog.GetPluralString ("{0} line added", "{0} lines added", added), added);
 
56
                                string removals = string.Format (GettextCatalog.GetPluralString ("{0} line removed", "{0} lines removed", removed), removed);
 
57
                                
 
58
                                return changes + " (" + additions + ", " + removals + ")";
 
59
                        }
 
60
                }
 
61
                
 
62
                public Mono.TextEditor.TextEditor FocusedEditor {
 
63
                        get {
 
64
                                return comparisonWidget.FocusedEditor;
 
65
                        }
 
66
                }
 
67
                
 
68
                public DiffWidget (VersionControlDocumentInfo info) : this (info, false)
 
69
                {
 
70
                }
 
71
                
 
72
                public DiffWidget (VersionControlDocumentInfo info, bool viewOnly)
 
73
                {
 
74
                        this.info = info;
 
75
                        this.Build ();
 
76
                        comparisonWidget = new MonoDevelop.VersionControl.Views.ComparisonWidget (viewOnly);
 
77
                        
 
78
                        fixed1.SetSizeRequest (16, 16);
 
79
                        this.buttonNext.Clicked += (sender, args) => ComparisonWidget.GotoNext ();
 
80
                        this.buttonPrev.Clicked += (sender, args) => ComparisonWidget.GotoPrev ();
 
81
                        notebook1.Page = 0;
 
82
                        vboxComparisonView.PackStart (comparisonWidget, true, true, 0);
 
83
                        comparisonWidget.Show ();
 
84
                        
 
85
                        comparisonWidget.DiffChanged += delegate {
 
86
                                labelOverview.Markup = LabelText;
 
87
                                SetButtonSensitivity ();
 
88
                        };
 
89
                        comparisonWidget.SetVersionControlInfo (info);
 
90
                        this.buttonDiff.Clicked += HandleButtonDiffhandleClicked;
 
91
                        diffTextEditor = new global::Mono.TextEditor.TextEditor ();
 
92
                        diffTextEditor.Document.MimeType = "text/x-diff";
 
93
                        if (info.Document != null) {
 
94
                                diffTextEditor.Options.FontName = info.Document.Editor.Options.FontName;
 
95
                                diffTextEditor.Options.ColorScheme = info.Document.Editor.Options.ColorScheme;
 
96
                                diffTextEditor.Options.ShowTabs = info.Document.Editor.Options.ShowTabs;
 
97
                                diffTextEditor.Options.ShowSpaces = info.Document.Editor.Options.ShowSpaces;
 
98
                                diffTextEditor.Options.ShowInvalidLines = info.Document.Editor.Options.ShowInvalidLines;
 
99
                                diffTextEditor.Options.ShowInvalidLines = info.Document.Editor.Options.ShowInvalidLines;
 
100
                        } else {
 
101
                                var options = MonoDevelop.SourceEditor.DefaultSourceEditorOptions.Instance;
 
102
                                diffTextEditor.Options.FontName = options.FontName;
 
103
                                diffTextEditor.Options.ColorScheme = options.ColorScheme;
 
104
                                diffTextEditor.Options.ShowTabs = options.ShowTabs;
 
105
                                diffTextEditor.Options.ShowSpaces = options.ShowSpaces;
 
106
                                diffTextEditor.Options.ShowInvalidLines = options.ShowInvalidLines;
 
107
                                diffTextEditor.Options.ShowInvalidLines = options.ShowInvalidLines;
 
108
                        }
 
109
                        
 
110
                        diffTextEditor.Options.ShowFoldMargin = false;
 
111
                        diffTextEditor.Options.ShowIconMargin = false;
 
112
                        diffTextEditor.Document.ReadOnly = true;
 
113
                        scrolledwindow1.Child = diffTextEditor;
 
114
                        diffTextEditor.Show ();
 
115
                        SetButtonSensitivity ();
 
116
                }
 
117
                
 
118
                void SetButtonSensitivity ()
 
119
                {
 
120
                        this.buttonNext.Sensitive = this.buttonPrev.Sensitive = notebook1.Page == 0 &&  comparisonWidget.Diff != null && comparisonWidget.Diff.Count > 0;
 
121
                }
 
122
                
 
123
                void HandleButtonDiffhandleClicked (object sender, EventArgs e)
 
124
                {
 
125
                        if (notebook1.Page == 0) {
 
126
                                buttonDiff.Label = GettextCatalog.GetString ("_Compare");
 
127
                                diffTextEditor.Document.Text = Mono.TextEditor.Utils.Diff.GetDiffString (comparisonWidget.Diff,
 
128
                                        comparisonWidget.DiffEditor.Document,
 
129
                                        comparisonWidget.OriginalEditor.Document,
 
130
                                        (info.Item.Path) + "\t\t"+ GetRevisionText (comparisonWidget.DiffEditor, comparisonWidget.diffRevision),
 
131
                                        (info.Item.Path) + "\t\t"+ GetRevisionText (comparisonWidget.OriginalEditor, comparisonWidget.originalRevision)
 
132
                                );
 
133
                                
 
134
                                notebook1.Page = 1;
 
135
                        } else {
 
136
                                buttonDiff.Label = GettextCatalog.GetString ("_Patch");
 
137
                                notebook1.Page = 0;
 
138
                        }
 
139
                        
 
140
                        SetButtonSensitivity ();
 
141
                }
 
142
                
 
143
                string GetRevisionText (Mono.TextEditor.TextEditor editor, Revision rev)
 
144
                {
 
145
                        if (!editor.Document.ReadOnly)
 
146
                                return GettextCatalog.GetString ("(working copy)");
 
147
                        if (rev == null)
 
148
                                return GettextCatalog.GetString ("(base)");
 
149
                        return string.Format (GettextCatalog.GetString ("(revision {0})"), rev.ToString ());
 
150
                }
 
151
                        
 
152
        }
 
153
}
 
154