gajim-plural/src/message_textview.py

344 lines
10 KiB
Python
Raw Normal View History

# -*- coding:utf-8 -*-
## src/message_textview.py
2005-10-29 16:51:47 +02:00
##
## Copyright (C) 2003-2007 Yann Leboulanger <asterix AT lagaule.org>
## Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com>
## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
2009-01-22 21:43:15 +01:00
## Copyright (C) 2008-2009 Julien Pivotto <roidelapluie AT gmail.com>
2005-10-29 16:51:47 +02:00
##
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
2005-10-29 16:51:47 +02:00
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
2005-10-29 16:51:47 +02:00
##
## Gajim is distributed in the hope that it will be useful,
2005-10-29 16:51:47 +02:00
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2005-10-29 16:51:47 +02:00
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
##
2005-10-29 16:51:47 +02:00
2009-11-25 12:39:05 +01:00
import gc
2005-10-29 16:51:47 +02:00
import gtk
import gobject
2008-11-14 12:13:15 +01:00
import pango
2009-11-25 12:39:05 +01:00
2008-11-14 12:13:15 +01:00
import gtkgui_helpers
from common import gajim
2005-10-29 16:51:47 +02:00
class MessageTextView(gtk.TextView):
2009-11-25 12:39:05 +01:00
"""
Class for the message textview (where user writes new messages) for
chat/groupchat windows
"""
2005-10-29 16:51:47 +02:00
__gsignals__ = dict(
mykeypress = (gobject.SIGNAL_RUN_LAST | gobject.SIGNAL_ACTION,
None, # return value
(int, gtk.gdk.ModifierType ) # arguments
2005-10-29 16:51:47 +02:00
)
)
2008-12-03 22:56:12 +01:00
2005-10-29 16:51:47 +02:00
def __init__(self):
gtk.TextView.__init__(self)
2008-11-14 12:13:15 +01:00
# set properties
self.set_border_width(1)
self.set_accepts_tab(True)
self.set_editable(True)
self.set_cursor_visible(True)
self.set_wrap_mode(gtk.WRAP_WORD_CHAR)
2005-10-29 17:03:50 +02:00
self.set_left_margin(2)
self.set_right_margin(2)
self.set_pixels_above_lines(2)
self.set_pixels_below_lines(2)
2005-10-29 16:51:47 +02:00
self.lang = None # Lang used for spell checking
2010-01-04 18:28:49 +01:00
_buffer = self.get_buffer()
2008-11-14 12:13:15 +01:00
self.begin_tags = {}
self.end_tags = {}
self.color_tags = []
self.fonts_tags = []
2008-12-03 22:56:12 +01:00
self.other_tags = {}
2010-01-04 18:28:49 +01:00
self.other_tags['bold'] = _buffer.create_tag('bold')
2008-11-14 12:13:15 +01:00
self.other_tags['bold'].set_property('weight', pango.WEIGHT_BOLD)
self.begin_tags['bold'] = '<strong>'
self.end_tags['bold'] = '</strong>'
2010-01-04 18:28:49 +01:00
self.other_tags['italic'] = _buffer.create_tag('italic')
2008-11-14 12:13:15 +01:00
self.other_tags['italic'].set_property('style', pango.STYLE_ITALIC)
self.begin_tags['italic'] = '<em>'
self.end_tags['italic'] = '</em>'
2010-01-04 18:28:49 +01:00
self.other_tags['underline'] = _buffer.create_tag('underline')
2008-11-14 12:13:15 +01:00
self.other_tags['underline'].set_property('underline', pango.UNDERLINE_SINGLE)
self.begin_tags['underline'] = '<span style="text-decoration: underline;">'
self.end_tags['underline'] = '</span>'
2010-01-04 18:28:49 +01:00
self.other_tags['strike'] = _buffer.create_tag('strike')
2008-11-14 12:13:15 +01:00
self.other_tags['strike'].set_property('strikethrough', True)
self.begin_tags['strike'] = '<span style="text-decoration: line-through;">'
self.end_tags['strike'] = '</span>'
def make_clickable_urls(self, text):
2010-01-04 18:28:49 +01:00
_buffer = self.get_buffer()
2008-11-14 12:13:15 +01:00
start = 0
end = 0
index = 0
2008-12-03 22:56:12 +01:00
2008-11-14 12:13:15 +01:00
new_text = ''
iterator = gajim.interface.link_pattern_re.finditer(text)
for match in iterator:
start, end = match.span()
url = text[start:end]
if start != 0:
text_before_special_text = text[index:start]
else:
text_before_special_text = ''
2010-01-04 18:28:49 +01:00
end_iter = _buffer.get_end_iter()
2008-11-14 12:13:15 +01:00
# we insert normal text
new_text += text_before_special_text + \
'<a href="'+ url +'">' + url + '</a>'
2008-12-03 22:56:12 +01:00
2008-11-14 12:13:15 +01:00
index = end # update index
if end < len(text):
new_text += text[end:]
return new_text # the position after *last* special text
def get_active_tags(self):
2009-01-22 21:43:15 +01:00
start, finish = self.get_active_iters()
2008-11-14 12:13:15 +01:00
active_tags = []
for tag in start.get_tags():
active_tags.append(tag.get_property('name'))
2010-01-04 18:28:49 +01:00
return active_tags
2008-11-14 12:13:15 +01:00
2009-01-22 21:43:15 +01:00
def get_active_iters(self):
2010-01-04 18:28:49 +01:00
_buffer = self.get_buffer()
return_val = _buffer.get_selection_bounds()
2008-11-14 12:13:15 +01:00
if return_val: # if sth was selected
start, finish = return_val[0], return_val[1]
else:
2010-01-04 18:28:49 +01:00
start, finish = _buffer.get_bounds()
2009-01-22 21:43:15 +01:00
return (start, finish)
2009-06-15 21:00:14 +02:00
2009-01-22 21:43:15 +01:00
def set_tag(self, widget, tag):
2010-01-04 18:28:49 +01:00
_buffer = self.get_buffer()
2009-01-22 21:43:15 +01:00
start, finish = self.get_active_iters()
2008-11-14 12:13:15 +01:00
if start.has_tag(self.other_tags[tag]):
2010-01-04 18:28:49 +01:00
_buffer.remove_tag_by_name(tag, start, finish)
2008-11-14 12:13:15 +01:00
else:
if tag == 'underline':
2010-01-04 18:28:49 +01:00
_buffer.remove_tag_by_name('strike', start, finish)
2008-11-14 12:13:15 +01:00
elif tag == 'strike':
2010-01-04 18:28:49 +01:00
_buffer.remove_tag_by_name('underline', start, finish)
_buffer.apply_tag_by_name(tag, start, finish)
2008-11-14 12:13:15 +01:00
def clear_tags(self, widget):
2010-01-04 18:28:49 +01:00
_buffer = self.get_buffer()
2009-01-22 21:43:15 +01:00
start, finish = self.get_active_iters()
2010-01-04 18:28:49 +01:00
_buffer.remove_all_tags(start, finish)
2008-11-14 12:13:15 +01:00
def color_set(self, widget, response, color):
if response == -6:
widget.destroy()
return
2010-01-04 18:28:49 +01:00
_buffer = self.get_buffer()
2008-11-14 12:13:15 +01:00
color = color.get_current_color()
widget.destroy()
color_string = gtkgui_helpers.make_color_string(color)
tag_name = 'color' + color_string
if not tag_name in self.color_tags:
2010-01-04 18:28:49 +01:00
tagColor = _buffer.create_tag(tag_name)
2008-11-14 12:13:15 +01:00
tagColor.set_property('foreground', color_string)
self.begin_tags[tag_name] = '<span style="color: ' + color_string + ';">'
self.end_tags[tag_name] = '</span>'
self.color_tags.append(tag_name)
2009-01-22 21:43:15 +01:00
start, finish = self.get_active_iters()
2008-11-14 12:13:15 +01:00
for tag in self.color_tags:
2010-01-04 18:28:49 +01:00
_buffer.remove_tag_by_name(tag, start, finish)
2008-11-14 12:13:15 +01:00
2010-01-04 18:28:49 +01:00
_buffer.apply_tag_by_name(tag_name, start, finish)
2008-11-14 12:13:15 +01:00
def font_set(self, widget, response, font):
if response == -6:
widget.destroy()
return
2010-01-04 18:28:49 +01:00
_buffer = self.get_buffer()
2008-11-14 12:13:15 +01:00
font = font.get_font_name()
font_desc = pango.FontDescription(font)
family = font_desc.get_family()
size = font_desc.get_size()
size = size / pango.SCALE
weight = font_desc.get_weight()
style = font_desc.get_style()
widget.destroy()
tag_name = 'font' + font
if not tag_name in self.fonts_tags:
2010-01-04 18:28:49 +01:00
tagFont = _buffer.create_tag(tag_name)
2008-11-14 12:13:15 +01:00
tagFont.set_property('font', family + ' ' + str(size))
self.begin_tags[tag_name] = \
'<span style="font-family: ' + family + '; ' + \
'font-size: ' + str(size) + 'px">'
2008-12-03 22:56:12 +01:00
self.end_tags[tag_name] = '</span>'
2008-11-14 12:13:15 +01:00
self.fonts_tags.append(tag_name)
2009-01-22 21:43:15 +01:00
start, finish = self.get_active_iters()
2008-12-03 22:56:12 +01:00
2008-11-14 12:13:15 +01:00
for tag in self.fonts_tags:
2010-01-04 18:28:49 +01:00
_buffer.remove_tag_by_name(tag, start, finish)
2008-11-14 12:13:15 +01:00
2010-01-04 18:28:49 +01:00
_buffer.apply_tag_by_name(tag_name, start, finish)
2008-11-14 12:13:15 +01:00
if weight == pango.WEIGHT_BOLD:
2010-01-04 18:28:49 +01:00
_buffer.apply_tag_by_name('bold', start, finish)
2008-11-14 12:13:15 +01:00
else:
2010-01-04 18:28:49 +01:00
_buffer.remove_tag_by_name('bold', start, finish)
2008-11-14 12:13:15 +01:00
if style == pango.STYLE_ITALIC:
2010-01-04 18:28:49 +01:00
_buffer.apply_tag_by_name('italic', start, finish)
2008-11-14 12:13:15 +01:00
else:
2010-01-04 18:28:49 +01:00
_buffer.remove_tag_by_name('italic', start, finish)
2008-11-14 12:13:15 +01:00
def get_xhtml(self):
2010-01-04 18:28:49 +01:00
_buffer = self.get_buffer()
old = _buffer.get_start_iter()
2008-11-14 12:13:15 +01:00
tags = {}
tags['bold'] = False
2010-01-04 18:28:49 +01:00
iter = _buffer.get_start_iter()
old = _buffer.get_start_iter()
2009-01-21 19:00:33 +01:00
text = ''
2008-11-14 12:13:15 +01:00
modified = False
2008-11-14 12:13:15 +01:00
def xhtml_special(text):
text = text.replace('<', '&lt;')
text = text.replace('>', '&gt;')
2009-01-27 21:33:54 +01:00
text = text.replace('&', '&amp;')
2008-11-14 12:13:15 +01:00
text = text.replace('\n', '<br />')
return text
for tag in iter.get_toggled_tags(True):
2008-11-15 09:23:32 +01:00
tag_name = tag.get_property('name')
if tag_name not in self.begin_tags:
continue
2009-01-21 19:00:33 +01:00
text += self.begin_tags[tag_name]
2008-11-14 12:13:15 +01:00
modified = True
while (iter.forward_to_tag_toggle(None) and not iter.is_end()):
2010-01-04 18:28:49 +01:00
text += xhtml_special(_buffer.get_text(old, iter))
2008-11-14 12:13:15 +01:00
old.forward_to_tag_toggle(None)
new_tags, old_tags, end_tags = [], [], []
2008-11-14 12:13:15 +01:00
for tag in iter.get_toggled_tags(True):
2008-11-15 09:23:32 +01:00
tag_name = tag.get_property('name')
if tag_name not in self.begin_tags:
continue
new_tags.append(tag_name)
modified = True
2008-12-03 22:56:12 +01:00
2008-11-14 12:13:15 +01:00
for tag in iter.get_tags():
2008-11-15 09:23:32 +01:00
tag_name = tag.get_property('name')
if tag_name not in self.begin_tags or tag_name not in self.end_tags:
continue
if tag_name not in new_tags:
old_tags.append(tag_name)
2008-12-03 22:56:12 +01:00
2008-11-14 12:13:15 +01:00
for tag in iter.get_toggled_tags(False):
2008-11-15 09:23:32 +01:00
tag_name = tag.get_property('name')
if tag_name not in self.end_tags:
continue
end_tags.append(tag_name)
2008-11-14 12:13:15 +01:00
for tag in old_tags:
2009-01-21 19:00:33 +01:00
text += self.end_tags[tag]
2008-11-14 12:13:15 +01:00
for tag in end_tags:
2009-01-21 19:00:33 +01:00
text += self.end_tags[tag]
2008-11-14 12:13:15 +01:00
for tag in new_tags:
2009-01-21 19:00:33 +01:00
text += self.begin_tags[tag]
2008-11-14 12:13:15 +01:00
for tag in old_tags:
2009-01-21 19:00:33 +01:00
text += self.begin_tags[tag]
2008-11-14 12:13:15 +01:00
2010-01-04 18:28:49 +01:00
text += xhtml_special(_buffer.get_text(old, _buffer.get_end_iter()))
2008-11-14 12:13:15 +01:00
for tag in iter.get_toggled_tags(False):
2008-11-15 09:23:32 +01:00
tag_name = tag.get_property('name')
if tag_name not in self.end_tags:
continue
2009-01-21 19:00:33 +01:00
text += self.end_tags[tag_name]
2008-11-14 12:13:15 +01:00
if modified:
2009-01-21 19:00:33 +01:00
return '<p>' + self.make_clickable_urls(text) + '</p>'
2008-11-14 12:13:15 +01:00
else:
return None
2008-12-03 22:56:12 +01:00
def destroy(self):
2009-11-25 12:39:05 +01:00
gobject.idle_add(gc.collect)
def clear(self, widget = None):
2009-11-25 12:39:05 +01:00
"""
Clear text in the textview
"""
2010-01-04 18:28:49 +01:00
_buffer = self.get_buffer()
start, end = _buffer.get_bounds()
_buffer.delete(start, end)
2005-10-29 16:51:47 +02:00
# We register depending on keysym and modifier some bindings
# but we also pass those as param so we can construct fake Event
2005-11-02 20:01:17 +01:00
# Here we register bindings for those combinations that there is NO DEFAULT
# action to be done by gtk TextView. In such case we should not add a binding
# as the default action comes first and our bindings is useless. In that case
# we catch and do stuff before default action in normal key_press_event
# and we also return True there to stop the default action from running
2005-10-29 16:51:47 +02:00
# CTRL + SHIFT + TAB
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.ISO_Left_Tab,
gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.ISO_Left_Tab,
gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)
# CTRL + TAB
2008-12-03 22:56:12 +01:00
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Tab,
2005-10-29 16:51:47 +02:00
gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.Tab,
gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)
2008-12-03 22:56:12 +01:00
# TAB
2008-12-03 22:56:12 +01:00
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Tab,
0, 'mykeypress', int, gtk.keysyms.Tab, gtk.gdk.ModifierType, 0)
2005-10-29 16:51:47 +02:00
# CTRL + UP
2008-12-03 22:56:12 +01:00
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Up,
2005-10-29 16:51:47 +02:00
gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.Up,
gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)
# CTRL + DOWN
2008-12-03 22:56:12 +01:00
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Down,
2005-10-29 16:51:47 +02:00
gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.Down,
gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)
# ENTER
2008-12-03 22:56:12 +01:00
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Return,
2005-10-29 16:51:47 +02:00
0, 'mykeypress', int, gtk.keysyms.Return,
gtk.gdk.ModifierType, 0)
# Ctrl + Enter
2008-12-03 22:56:12 +01:00
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.Return,
2005-10-29 16:51:47 +02:00
gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.Return,
gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)
# Keypad Enter
2008-12-03 22:56:12 +01:00
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.KP_Enter,
2005-10-29 16:51:47 +02:00
0, 'mykeypress', int, gtk.keysyms.KP_Enter,
gtk.gdk.ModifierType, 0)
# Ctrl + Keypad Enter
2008-12-03 22:56:12 +01:00
gtk.binding_entry_add_signal(MessageTextView, gtk.keysyms.KP_Enter,
2005-10-29 16:51:47 +02:00
gtk.gdk.CONTROL_MASK, 'mykeypress', int, gtk.keysyms.KP_Enter,
gtk.gdk.ModifierType, gtk.gdk.CONTROL_MASK)
# vim: se ts=3: