2
# -*- coding: UTF-8 -*-
5
This module contains the code to manage the diff window which shows
6
the changes made between two revisions on a branch.
9
__copyright__ = "Copyright © 2005 Canonical Ltd."
10
__author__ = "Scott James Remnant <scott@ubuntu.com>"
13
from cStringIO import StringIO
20
have_gtksourceview = True
22
have_gtksourceview = False
24
from bzrlib.delta import compare_trees
25
from bzrlib.diff import show_diff_trees
28
class DiffWindow(gtk.Window):
31
This object represents and manages a single window containing the
32
differences between two revisions on a branch.
36
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
37
self.set_border_width(0)
38
self.set_title("bzrk diff")
40
# Use two thirds of the screen by default
41
screen = self.get_screen()
42
monitor = screen.get_monitor_geometry(0)
43
width = int(monitor.width * 0.66)
44
height = int(monitor.height * 0.66)
45
self.set_default_size(width, height)
50
"""Construct the window contents."""
51
hbox = gtk.HBox(spacing=6)
52
hbox.set_border_width(12)
56
scrollwin = gtk.ScrolledWindow()
57
scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
58
scrollwin.set_shadow_type(gtk.SHADOW_IN)
59
hbox.pack_start(scrollwin, expand=False, fill=True)
62
self.model = gtk.TreeStore(str, str)
63
self.treeview = gtk.TreeView(self.model)
64
self.treeview.set_headers_visible(False)
65
self.treeview.set_search_column(1)
66
self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
67
scrollwin.add(self.treeview)
70
cell = gtk.CellRendererText()
71
cell.set_property("width-chars", 20)
72
column = gtk.TreeViewColumn()
73
column.pack_start(cell, expand=True)
74
column.add_attribute(cell, "text", 0)
75
self.treeview.append_column(column)
78
scrollwin = gtk.ScrolledWindow()
79
scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
80
scrollwin.set_shadow_type(gtk.SHADOW_IN)
81
hbox.pack_start(scrollwin, expand=True, fill=True)
84
if have_gtksourceview:
85
self.buffer = gtksourceview.SourceBuffer()
86
slm = gtksourceview.SourceLanguagesManager()
87
gsl = slm.get_language_from_mime_type("text/x-patch")
88
self.buffer.set_language(gsl)
89
self.buffer.set_highlight(True)
91
sourceview = gtksourceview.SourceView(self.buffer)
93
self.buffer = gtk.TextBuffer()
94
sourceview = gtk.TextView(self.buffer)
96
sourceview.set_editable(False)
97
sourceview.modify_font(pango.FontDescription("Monospace"))
98
scrollwin.add(sourceview)
101
def set_diff(self, description, rev_tree, parent_tree):
102
"""Set the differences showed by this window.
104
Compares the two trees and populates the window with the
107
self.rev_tree = rev_tree
108
self.parent_tree = parent_tree
111
delta = compare_trees(self.parent_tree, self.rev_tree)
113
self.model.append(None, [ "Complete Diff", "" ])
116
titer = self.model.append(None, [ "Added", None ])
117
for path, id, kind in delta.added:
118
self.model.append(titer, [ path, path ])
120
if len(delta.removed):
121
titer = self.model.append(None, [ "Removed", None ])
122
for path, id, kind in delta.removed:
123
self.model.append(titer, [ path, path ])
125
if len(delta.renamed):
126
titer = self.model.append(None, [ "Renamed", None ])
127
for oldpath, newpath, id, kind, text_modified, meta_modified \
129
self.model.append(titer, [ oldpath, oldpath ])
131
if len(delta.modified):
132
titer = self.model.append(None, [ "Modified", None ])
133
for path, id, kind, text_modified, meta_modified in delta.modified:
134
self.model.append(titer, [ path, path ])
136
self.treeview.expand_all()
137
self.set_title(description + " - bzrk diff")
139
def _treeview_cursor_cb(self, *args):
140
"""Callback for when the treeview cursor changes."""
141
(path, col) = self.treeview.get_cursor()
142
specific_files = [ self.model[path][1] ]
143
if specific_files == [ None ]:
145
elif specific_files == [ "" ]:
149
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
150
self.buffer.set_text(s.getvalue())