64
63
self.set_shadow_type(gtk.SHADOW_IN)
66
65
if have_gtksourceview:
67
self.buffer = gtksourceview.SourceBuffer()
68
slm = gtksourceview.SourceLanguagesManager()
69
gsl = slm.get_language_from_mime_type("text/x-patch")
66
self.buffer = gtksourceview2.Buffer()
67
slm = gtksourceview2.LanguageManager()
68
gsl = slm.guess_language(content_type="text/x-patch")
71
self.apply_gedit_colors(gsl)
70
self.apply_gedit_colors(self.buffer)
72
71
self.apply_colordiff_colors(gsl)
73
72
self.buffer.set_language(gsl)
74
self.buffer.set_highlight(True)
73
self.buffer.set_highlight_syntax(True)
76
sourceview = gtksourceview.SourceView(self.buffer)
75
self.sourceview = gtksourceview2.View(self.buffer)
78
77
self.buffer = gtk.TextBuffer()
79
sourceview = gtk.TextView(self.buffer)
78
self.sourceview = gtk.TextView(self.buffer)
81
sourceview.set_editable(False)
82
sourceview.modify_font(pango.FontDescription("Monospace"))
80
self.sourceview.set_editable(False)
81
self.sourceview.modify_font(pango.FontDescription("Monospace"))
82
self.add(self.sourceview)
83
self.sourceview.show()
87
def apply_gedit_colors(lang):
88
"""Set style for lang to that specified in gedit configuration.
86
def apply_gedit_colors(buf):
87
"""Set style to that specified in gedit configuration.
90
89
This method needs the gconf module.
92
:param lang: a gtksourceview.SourceLanguage object.
91
:param buf: a gtksourceview2.Buffer object.
94
GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
95
GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
93
GEDIT_SCHEME_PATH = '/apps/gedit-2/preferences/editor/colors/scheme'
97
95
client = gconf.client_get_default()
98
client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
100
for tag in lang.get_tags():
101
tag_id = tag.get_id()
102
gconf_key = GEDIT_LANG_PATH + '/' + tag_id
103
style_string = client.get_string(gconf_key)
105
if style_string is None:
108
# function to get a bool from a string that's either '0' or '1'
109
string_bool = lambda x: bool(int(x))
111
# style_string is a string like "2/#FFCCAA/#000000/0/1/0/0"
112
# values are: mask, fg, bg, italic, bold, underline, strike
113
# this packs them into (str_value, attr_name, conv_func) tuples
114
items = zip(style_string.split('/'), ['mask', 'foreground',
115
'background', 'italic', 'bold', 'underline', 'strikethrough' ],
116
[ int, gtk.gdk.color_parse, gtk.gdk.color_parse, string_bool,
117
string_bool, string_bool, string_bool ]
120
style = gtksourceview.SourceTagStyle()
122
# XXX The mask attribute controls whether the present values of
123
# foreground and background color should in fact be used. Ideally
124
# (and that's what gedit does), one could set all three attributes,
125
# and let the TagStyle object figure out which colors to use.
126
# However, in the GtkSourceview python bindings, the mask attribute
127
# is read-only, and it's derived instead from the colors being
128
# set or not. This means that we have to sometimes refrain from
129
# setting fg or bg colors, depending on the value of the mask.
130
# This code could go away if mask were writable.
131
mask = int(items[0][0])
132
if not (mask & 1): # GTK_SOURCE_TAG_STYLE_USE_BACKGROUND
134
if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
136
items[0:1] = [] # skip the mask unconditionally
138
for value, attr, func in items:
142
warning('gconf key %s contains an invalid value: %s'
145
setattr(style, attr, value)
147
lang.set_tag_style(tag_id, style)
96
style_scheme_name = client.get_string(GEDIT_SCHEME_PATH)
97
style_scheme = gtksourceview2.StyleSchemeManager().get_scheme(style_scheme_name)
99
buf.set_style_scheme(style_scheme)
150
102
def apply_colordiff_colors(klass, lang):
431
392
self.vbox = gtk.VBox()
432
393
self.add(self.vbox)
395
self.diff = DiffWidget()
396
self.vbox.pack_end(self.diff, True, True, 0)
398
# Build after DiffWidget to connect signals
399
menubar = self._get_menu_bar()
400
self.vbox.pack_start(menubar, False, False, 0)
434
401
hbox = self._get_button_bar(operations)
435
402
if hbox is not None:
436
self.vbox.pack_start(hbox, expand=False, fill=True)
437
self.diff = DiffWidget()
438
self.vbox.add(self.diff)
403
self.vbox.pack_start(hbox, False, True, 0)
406
def _get_menu_bar(self):
407
menubar = gtk.MenuBar()
409
mb_view = gtk.MenuItem(_i18n("_View"))
410
mb_view_menu = gtk.Menu()
411
mb_view_wrapsource = gtk.CheckMenuItem(_i18n("Wrap _Long Lines"))
412
mb_view_wrapsource.connect('activate', self.diff._on_wraplines_toggled)
413
mb_view_wrapsource.show()
414
mb_view_menu.append(mb_view_wrapsource)
416
mb_view.set_submenu(mb_view_menu)
418
menubar.append(mb_view)
441
422
def _get_button_bar(self, operations):
442
423
"""Return a button bar to use.