2007-02-11 05:10:48 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2008-03-29 04:52:46 +05:30
|
|
|
# Copyright (C) 2007-2008 Zsolt Foldvari
|
2007-02-11 05:10:48 +05:30
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
|
2010-08-31 04:30:28 +05:30
|
|
|
"""Text buffer subclassed from gtk.TextBuffer handling L{StyledText}."""
|
2007-02-13 21:17:56 +05:30
|
|
|
|
2008-05-28 01:23:25 +05:30
|
|
|
__all__ = ["ALLOWED_STYLES", "MATCH_START", "MATCH_END", "MATCH_FLAVOR",
|
|
|
|
"MATCH_STRING", "StyledTextBuffer"]
|
|
|
|
|
2007-02-11 05:10:48 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2007-02-13 21:17:56 +05:30
|
|
|
# Python modules
|
2007-02-11 05:10:48 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2010-01-18 10:12:17 +05:30
|
|
|
from gen.ggettext import gettext as _
|
2007-02-13 01:23:30 +05:30
|
|
|
import re
|
2007-02-11 05:10:48 +05:30
|
|
|
|
|
|
|
import logging
|
2008-05-09 18:48:10 +05:30
|
|
|
_LOG = logging.getLogger(".widgets.styledtextbuffer")
|
2007-02-11 05:10:48 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2007-02-13 21:17:56 +05:30
|
|
|
# GTK modules
|
2007-02-11 05:10:48 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-04-23 14:37:02 +05:30
|
|
|
import gobject
|
2007-02-11 05:10:48 +05:30
|
|
|
import gtk
|
2010-08-31 01:05:51 +05:30
|
|
|
from gui.widgets.undoablebuffer import UndoableBuffer
|
2007-04-27 05:48:09 +05:30
|
|
|
from pango import WEIGHT_BOLD, STYLE_ITALIC, UNDERLINE_SINGLE
|
2007-02-11 05:10:48 +05:30
|
|
|
|
2007-03-14 02:01:50 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2008-03-29 04:52:46 +05:30
|
|
|
# GRAMPS modules
|
2007-03-14 02:01:50 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-03-29 04:52:46 +05:30
|
|
|
from gen.lib import (StyledText, StyledTextTag, StyledTextTagType)
|
2007-03-14 02:01:50 +05:30
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Constants
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-05-02 15:44:39 +05:30
|
|
|
ALLOWED_STYLES = (
|
|
|
|
StyledTextTagType.BOLD,
|
|
|
|
StyledTextTagType.ITALIC,
|
|
|
|
StyledTextTagType.UNDERLINE,
|
|
|
|
StyledTextTagType.FONTCOLOR,
|
|
|
|
StyledTextTagType.HIGHLIGHT,
|
|
|
|
StyledTextTagType.FONTFACE,
|
|
|
|
StyledTextTagType.FONTSIZE,
|
2011-03-28 13:56:47 +05:30
|
|
|
StyledTextTagType.LINK,
|
2008-05-02 15:44:39 +05:30
|
|
|
)
|
2008-04-23 14:37:02 +05:30
|
|
|
|
|
|
|
STYLE_TO_PROPERTY = {
|
|
|
|
StyledTextTagType.BOLD: 'weight', # permanent tag is used instead
|
|
|
|
StyledTextTagType.ITALIC: 'style', # permanent tag is used instead
|
|
|
|
StyledTextTagType.UNDERLINE: 'underline', # permanent tag is used instead
|
|
|
|
StyledTextTagType.FONTCOLOR: 'foreground',
|
|
|
|
StyledTextTagType.HIGHLIGHT: 'background',
|
|
|
|
StyledTextTagType.FONTFACE: 'family',
|
|
|
|
StyledTextTagType.FONTSIZE: 'size-points',
|
2011-03-28 13:56:47 +05:30
|
|
|
StyledTextTagType.LINK: 'link',
|
2008-04-23 14:37:02 +05:30
|
|
|
}
|
|
|
|
|
2007-07-06 15:41:29 +05:30
|
|
|
(MATCH_START,
|
|
|
|
MATCH_END,
|
|
|
|
MATCH_FLAVOR,
|
|
|
|
MATCH_STRING,) = range(4)
|
|
|
|
|
2010-05-06 21:24:33 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# LinkTag class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class LinkTag(gtk.TextTag):
|
|
|
|
"""
|
|
|
|
Class for keeping track of link data.
|
|
|
|
"""
|
|
|
|
lid = 0
|
|
|
|
def __init__(self, buffer, data, **properties):
|
|
|
|
LinkTag.lid += 1
|
|
|
|
self.data = data
|
|
|
|
gtk.TextTag.__init__(self, "link-%d" % LinkTag.lid)
|
|
|
|
tag_table = buffer.get_tag_table()
|
|
|
|
for property in properties:
|
|
|
|
self.set_property(property, properties[property])
|
|
|
|
tag_table.add(self)
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GtkSpellState class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-05-21 22:49:50 +05:30
|
|
|
class GtkSpellState(object):
|
2007-07-03 15:06:48 +05:30
|
|
|
"""A simple state machine kinda thingy.
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
Trying to track gtk.Spell activities on a buffer and re-apply formatting
|
2007-07-03 15:06:48 +05:30
|
|
|
after gtk.Spell replaces a misspelled word.
|
2008-03-29 04:52:46 +05:30
|
|
|
|
2007-07-03 15:06:48 +05:30
|
|
|
"""
|
|
|
|
(STATE_NONE,
|
|
|
|
STATE_CLICKED,
|
|
|
|
STATE_DELETED,
|
|
|
|
STATE_INSERTING) = range(4)
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
def __init__(self, textbuffer):
|
|
|
|
if not isinstance(textbuffer, gtk.TextBuffer):
|
2007-07-03 15:06:48 +05:30
|
|
|
raise TypeError("Init parameter must be instance of gtk.TextBuffer")
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
textbuffer.connect('mark-set', self.on_buffer_mark_set)
|
|
|
|
textbuffer.connect('delete-range', self.on_buffer_delete_range)
|
|
|
|
textbuffer.connect('insert-text', self.on_buffer_insert_text)
|
|
|
|
textbuffer.connect_after('insert-text', self.after_buffer_insert_text)
|
2007-07-03 15:06:48 +05:30
|
|
|
|
|
|
|
self.reset_state()
|
|
|
|
|
|
|
|
def reset_state(self):
|
|
|
|
self.state = self.STATE_NONE
|
|
|
|
self.start = 0
|
|
|
|
self.end = 0
|
|
|
|
self.tags = None
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
def on_buffer_mark_set(self, textbuffer, iter, mark):
|
2007-07-03 15:06:48 +05:30
|
|
|
mark_name = mark.get_name()
|
|
|
|
if mark_name == 'gtkspell-click':
|
|
|
|
self.state = self.STATE_CLICKED
|
2008-03-29 04:52:46 +05:30
|
|
|
self.start, self.end = self.get_word_extents_from_mark(textbuffer,
|
|
|
|
mark)
|
|
|
|
_LOG.debug("SpellState got start %d end %d" % (self.start, self.end))
|
2007-07-03 15:06:48 +05:30
|
|
|
elif mark_name == 'insert':
|
|
|
|
self.reset_state()
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
def on_buffer_delete_range(self, textbuffer, start, end):
|
2007-07-03 15:06:48 +05:30
|
|
|
if ((self.state == self.STATE_CLICKED) and
|
|
|
|
(start.get_offset() == self.start) and
|
|
|
|
(end.get_offset() == self.end)):
|
|
|
|
self.state = self.STATE_DELETED
|
|
|
|
self.tags = start.get_tags()
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
def on_buffer_insert_text(self, textbuffer, iter, text, length):
|
2007-07-03 15:06:48 +05:30
|
|
|
if self.state == self.STATE_DELETED and iter.get_offset() == self.start:
|
|
|
|
self.state = self.STATE_INSERTING
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
def after_buffer_insert_text(self, textbuffer, iter, text, length):
|
2007-07-03 15:06:48 +05:30
|
|
|
if self.state == self.STATE_INSERTING:
|
2008-03-29 04:52:46 +05:30
|
|
|
mark = textbuffer.get_mark('gtkspell-insert-start')
|
|
|
|
insert_start = textbuffer.get_iter_at_mark(mark)
|
2007-07-03 15:06:48 +05:30
|
|
|
for tag in self.tags:
|
2008-03-29 04:52:46 +05:30
|
|
|
textbuffer.apply_tag(tag, insert_start, iter)
|
2007-07-03 15:06:48 +05:30
|
|
|
|
|
|
|
self.reset_state()
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
def get_word_extents_from_mark(self, textbuffer, mark):
|
2007-07-03 15:06:48 +05:30
|
|
|
"""Get the word extents as gtk.Spell does.
|
|
|
|
|
|
|
|
Used to get the beginning of the word, in which user right clicked.
|
|
|
|
Formatting found at that position used after gtk.Spell replaces
|
|
|
|
misspelled words.
|
|
|
|
|
|
|
|
"""
|
2008-03-29 04:52:46 +05:30
|
|
|
start = textbuffer.get_iter_at_mark(mark)
|
2007-07-03 15:06:48 +05:30
|
|
|
if not start.starts_word():
|
|
|
|
#start.backward_word_start()
|
|
|
|
self.backward_word_start(start)
|
|
|
|
end = start.copy()
|
|
|
|
if end.inside_word():
|
|
|
|
#end.forward_word_end()
|
|
|
|
self.forward_word_end(end)
|
|
|
|
return start.get_offset(), end.get_offset()
|
|
|
|
|
|
|
|
def forward_word_end(self, iter):
|
|
|
|
"""gtk.Spell style gtk.TextIter.forward_word_end.
|
|
|
|
|
|
|
|
The parameter 'iter' is changing as side effect.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not iter.forward_word_end():
|
|
|
|
return False
|
|
|
|
|
|
|
|
if iter.get_char() != "'":
|
|
|
|
return True
|
|
|
|
|
|
|
|
i = iter.copy()
|
|
|
|
if i.forward_char():
|
|
|
|
if i.get_char().isalpha():
|
|
|
|
return iter.forward_word_end()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def backward_word_start(self, iter):
|
|
|
|
"""gtk.Spell style gtk.TextIter.backward_word_start.
|
|
|
|
|
|
|
|
The parameter 'iter' is changing as side effect.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not iter.backward_word_start():
|
|
|
|
return False
|
|
|
|
|
|
|
|
i = iter.copy()
|
|
|
|
if i.backward_char():
|
|
|
|
if i.get_char() == "'":
|
|
|
|
if i.backward_char():
|
|
|
|
if i.get_char().isalpha():
|
|
|
|
return iter.backward_word_start()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# StyledTextBuffer class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2010-08-31 01:05:51 +05:30
|
|
|
class StyledTextBuffer(UndoableBuffer):
|
2008-03-29 04:52:46 +05:30
|
|
|
"""An extended TextBuffer for handling StyledText strings.
|
|
|
|
|
|
|
|
StyledTextBuffer is an interface between GRAMPS' L{StyledText} format
|
2008-04-03 02:04:29 +05:30
|
|
|
and gtk.TextBuffer. To set and get the text use the L{set_text} and
|
|
|
|
L{get_text} methods.
|
2008-03-29 04:52:46 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
To set a style to (a portion of) the text (e.g. from GUI) use the
|
|
|
|
L{apply_style} and L{remove_style} methods.
|
|
|
|
|
|
|
|
To receive information about the style of the text at the cursor position
|
|
|
|
StyledTextBuffer provides two mechanism: message driven and polling.
|
|
|
|
To receive notification of style change as cursor moves connect to the
|
|
|
|
C{style-changed} signal. To get the value of a certain style at the cursor
|
|
|
|
use the L{get_style_at_cursor) method.
|
2007-02-13 01:23:30 +05:30
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
StyledTextBuffer has a regexp pattern matching mechanism too. To add a
|
|
|
|
regexp pattern to match in the text use the L{match_add} method. To check
|
|
|
|
if there's a match at a certain position in the text use the L{match_check}
|
2008-04-03 02:04:29 +05:30
|
|
|
method. For an example how to use the matching see L{EditNote}.
|
2007-02-13 01:23:30 +05:30
|
|
|
|
|
|
|
"""
|
2008-03-29 04:52:46 +05:30
|
|
|
__gtype_name__ = 'StyledTextBuffer'
|
2007-02-13 01:23:30 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
__gsignals__ = {
|
|
|
|
'style-changed': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, #return value
|
|
|
|
(gobject.TYPE_PYOBJECT,)), # arguments
|
|
|
|
}
|
2007-02-11 05:10:48 +05:30
|
|
|
|
|
|
|
def __init__(self):
|
2010-08-16 17:14:26 +05:30
|
|
|
super(StyledTextBuffer, self).__init__()
|
2007-02-11 05:10:48 +05:30
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
# Create fix tags.
|
|
|
|
# Other tags (e.g. color) have to be created on the fly
|
2008-04-23 14:37:02 +05:30
|
|
|
# see self._find_tag_by_name
|
|
|
|
self.create_tag(str(StyledTextTagType.BOLD), weight=WEIGHT_BOLD)
|
|
|
|
self.create_tag(str(StyledTextTagType.ITALIC), style=STYLE_ITALIC)
|
|
|
|
self.create_tag(str(StyledTextTagType.UNDERLINE),
|
|
|
|
underline=UNDERLINE_SINGLE)
|
|
|
|
|
2007-07-03 15:06:48 +05:30
|
|
|
# internal format state attributes
|
|
|
|
## 1. are used to format inserted characters (self.after_insert_text)
|
|
|
|
## 2. are set each time the Insert marker is set (self.do_mark_set)
|
2008-04-23 14:37:02 +05:30
|
|
|
## 3. are set when a style is set (self._apply_style_to_selection)
|
2008-04-23 19:04:13 +05:30
|
|
|
self.style_state = StyledTextTagType.STYLE_DEFAULT.copy()
|
2007-07-03 15:06:48 +05:30
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
# internally used attribute
|
|
|
|
self._insert = self.get_insert()
|
2007-07-03 15:06:48 +05:30
|
|
|
|
2007-07-06 15:41:29 +05:30
|
|
|
# create a mark used for text formatting
|
2007-07-03 15:06:48 +05:30
|
|
|
start, end = self.get_bounds()
|
|
|
|
self.mark_insert = self.create_mark('insert-start', start, True)
|
|
|
|
|
2007-07-06 15:41:29 +05:30
|
|
|
# pattern matching attributes
|
|
|
|
self.patterns = []
|
|
|
|
self.matches = []
|
|
|
|
|
2007-07-03 15:06:48 +05:30
|
|
|
# hook up on some signals whose default handler cannot be overriden
|
|
|
|
self.connect('insert-text', self.on_insert_text)
|
|
|
|
self.connect_after('insert-text', self.after_insert_text)
|
|
|
|
self.connect_after('delete-range', self.after_delete_range)
|
2007-07-06 15:41:29 +05:30
|
|
|
|
2007-07-03 15:06:48 +05:30
|
|
|
# init gtkspell "state machine"
|
|
|
|
self.gtkspell_state = GtkSpellState(self)
|
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
# Virtual methods
|
2007-02-13 01:23:30 +05:30
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
def on_insert_text(self, textbuffer, iter, text, length):
|
|
|
|
_LOG.debug("Will insert at %d length %d" % (iter.get_offset(), length))
|
2007-07-03 15:06:48 +05:30
|
|
|
|
|
|
|
# let's remember where we started inserting
|
|
|
|
self.move_mark(self.mark_insert, iter)
|
2007-02-11 05:10:48 +05:30
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
def after_insert_text(self, textbuffer, iter, text, length):
|
2007-07-03 15:06:48 +05:30
|
|
|
"""Format inserted text."""
|
2008-03-29 04:52:46 +05:30
|
|
|
_LOG.debug("Have inserted at %d length %d (%s)" %
|
2007-07-03 15:06:48 +05:30
|
|
|
(iter.get_offset(), length, text))
|
|
|
|
|
|
|
|
if not length:
|
|
|
|
return
|
|
|
|
|
|
|
|
# where did we start inserting
|
|
|
|
insert_start = self.get_iter_at_mark(self.mark_insert)
|
2007-02-11 05:10:48 +05:30
|
|
|
|
2007-07-03 15:06:48 +05:30
|
|
|
# apply active formats for the inserted text
|
2008-04-23 14:37:02 +05:30
|
|
|
for style in ALLOWED_STYLES:
|
|
|
|
value = self.style_state[style]
|
2008-04-23 19:04:13 +05:30
|
|
|
if value and (value != StyledTextTagType.STYLE_DEFAULT[style]):
|
2008-04-23 14:37:02 +05:30
|
|
|
self.apply_tag(self._find_tag_by_name(style, value),
|
2007-07-03 15:06:48 +05:30
|
|
|
insert_start, iter)
|
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
def after_delete_range(self, textbuffer, start, end):
|
|
|
|
_LOG.debug("Deleted from %d till %d" %
|
2007-07-03 15:06:48 +05:30
|
|
|
(start.get_offset(), end.get_offset()))
|
|
|
|
|
|
|
|
# move 'insert' marker to have the format attributes updated
|
|
|
|
self.move_mark(self._insert, start)
|
|
|
|
|
2007-07-06 15:41:29 +05:30
|
|
|
def do_changed(self):
|
|
|
|
"""Parse for patterns in the text."""
|
|
|
|
self.matches = []
|
2010-08-16 17:14:26 +05:30
|
|
|
text = unicode(super(StyledTextBuffer, self).get_text(
|
2007-07-06 15:41:29 +05:30
|
|
|
self.get_start_iter(),
|
|
|
|
self.get_end_iter()))
|
|
|
|
for regex, flavor in self.patterns:
|
|
|
|
iter = regex.finditer(text)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
match = iter.next()
|
|
|
|
self.matches.append((match.start(), match.end(),
|
|
|
|
flavor, match.group()))
|
2008-03-29 04:52:46 +05:30
|
|
|
_LOG.debug("Matches: %d, %d: %s [%d]" %
|
2007-07-06 15:41:29 +05:30
|
|
|
(match.start(), match.end(),
|
|
|
|
match.group(), flavor))
|
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
def do_mark_set(self, iter, mark):
|
2008-04-23 14:37:02 +05:30
|
|
|
"""Update style state each time the cursor moves."""
|
2008-03-29 04:52:46 +05:30
|
|
|
_LOG.debug("Setting mark %s at %d" %
|
2007-04-27 05:48:09 +05:30
|
|
|
(mark.get_name(), iter.get_offset()))
|
2007-02-13 01:23:30 +05:30
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
if mark.get_name() != 'insert':
|
|
|
|
return
|
2007-02-13 01:23:30 +05:30
|
|
|
|
2007-07-03 15:06:48 +05:30
|
|
|
if not iter.starts_line():
|
|
|
|
iter.backward_char()
|
|
|
|
|
|
|
|
tag_names = [tag.get_property('name') for tag in iter.get_tags()]
|
2008-04-23 14:37:02 +05:30
|
|
|
changed_styles = {}
|
|
|
|
|
|
|
|
for style in ALLOWED_STYLES:
|
2008-04-23 19:04:13 +05:30
|
|
|
if StyledTextTagType.STYLE_TYPE[style] == bool:
|
2008-04-23 14:37:02 +05:30
|
|
|
value = str(style) in tag_names
|
2007-07-03 15:06:48 +05:30
|
|
|
else:
|
2008-04-23 19:04:13 +05:30
|
|
|
value = StyledTextTagType.STYLE_DEFAULT[style]
|
2007-07-03 15:06:48 +05:30
|
|
|
for tname in tag_names:
|
2008-04-23 14:37:02 +05:30
|
|
|
if tname.startswith(str(style)):
|
2007-07-03 15:06:48 +05:30
|
|
|
value = tname.split(' ', 1)[1]
|
2008-04-23 19:04:13 +05:30
|
|
|
value = StyledTextTagType.STYLE_TYPE[style](value)
|
2008-04-23 14:37:02 +05:30
|
|
|
|
|
|
|
if self.style_state[style] != value:
|
|
|
|
changed_styles[style] = value
|
|
|
|
|
|
|
|
self.style_state[style] = value
|
2007-07-03 15:06:48 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
if changed_styles:
|
|
|
|
self.emit('style-changed', changed_styles)
|
2007-02-11 05:10:48 +05:30
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
# Private
|
|
|
|
|
|
|
|
##def get_tag_value_at_insert(self, name):
|
|
|
|
##"""Get the value of the given tag at the insertion point."""
|
|
|
|
##tags = self.get_iter_at_mark(self._insert).get_tags()
|
|
|
|
|
|
|
|
##if name in self.toggle_actions:
|
|
|
|
##for tag in tags:
|
|
|
|
##if tag.get_name() == name:
|
|
|
|
##return True
|
|
|
|
##return False
|
|
|
|
##else:
|
|
|
|
##for tag in tags:
|
2007-07-03 15:06:48 +05:30
|
|
|
##if tag.get_name().startswith(name):
|
2007-04-27 05:48:09 +05:30
|
|
|
##return tag.get_name().split()[1]
|
|
|
|
##return None
|
|
|
|
|
2008-04-03 02:04:29 +05:30
|
|
|
def _get_selection(self):
|
2007-02-11 05:10:48 +05:30
|
|
|
bounds = self.get_selection_bounds()
|
|
|
|
if not bounds:
|
2007-04-27 05:48:09 +05:30
|
|
|
iter = self.get_iter_at_mark(self._insert)
|
2007-02-11 05:10:48 +05:30
|
|
|
if iter.inside_word():
|
|
|
|
start_pos = iter.get_offset()
|
|
|
|
iter.forward_word_end()
|
|
|
|
word_end = iter.get_offset()
|
|
|
|
iter.backward_word_start()
|
|
|
|
word_start = iter.get_offset()
|
|
|
|
iter.set_offset(start_pos)
|
|
|
|
bounds = (self.get_iter_at_offset(word_start),
|
2007-04-27 05:48:09 +05:30
|
|
|
self.get_iter_at_offset(word_end))
|
2007-02-11 05:10:48 +05:30
|
|
|
else:
|
|
|
|
bounds = (iter, self.get_iter_at_offset(iter.get_offset() + 1))
|
|
|
|
return bounds
|
|
|
|
|
2008-04-03 02:04:29 +05:30
|
|
|
def _apply_tag_to_selection(self, tag):
|
|
|
|
selection = self._get_selection()
|
2007-02-11 05:10:48 +05:30
|
|
|
if selection:
|
|
|
|
self.apply_tag(tag, *selection)
|
|
|
|
|
2008-04-03 02:04:29 +05:30
|
|
|
def _remove_tag_from_selection(self, tag):
|
|
|
|
selection = self._get_selection()
|
2007-02-11 05:10:48 +05:30
|
|
|
if selection:
|
|
|
|
self.remove_tag(tag, *selection)
|
2007-04-27 05:48:09 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
def _apply_style_to_selection(self, style, value):
|
|
|
|
# FIXME can this be unified?
|
2008-04-23 19:04:13 +05:30
|
|
|
if StyledTextTagType.STYLE_TYPE[style] == bool:
|
2008-04-23 14:37:02 +05:30
|
|
|
start, end = self._get_selection()
|
|
|
|
|
|
|
|
if value:
|
|
|
|
self.apply_tag_by_name(str(style), start, end)
|
|
|
|
else:
|
|
|
|
self.remove_tag_by_name(str(style), start, end)
|
2008-04-23 19:04:13 +05:30
|
|
|
elif StyledTextTagType.STYLE_TYPE[style] == str:
|
2008-04-23 14:37:02 +05:30
|
|
|
tag = self._find_tag_by_name(style, value)
|
|
|
|
self._remove_style_from_selection(style)
|
|
|
|
self._apply_tag_to_selection(tag)
|
2008-04-23 19:04:13 +05:30
|
|
|
elif StyledTextTagType.STYLE_TYPE[style] == int:
|
2008-04-23 14:37:02 +05:30
|
|
|
tag = self._find_tag_by_name(style, value)
|
|
|
|
self._remove_style_from_selection(style)
|
|
|
|
self._apply_tag_to_selection(tag)
|
|
|
|
else:
|
|
|
|
# we should never get until here
|
|
|
|
return
|
|
|
|
|
|
|
|
self.style_state[style] = value
|
|
|
|
|
|
|
|
def _remove_style_from_selection(self, style):
|
2008-04-03 02:04:29 +05:30
|
|
|
start, end = self._get_selection()
|
|
|
|
tags = self._get_tag_from_range(start.get_offset(), end.get_offset())
|
2009-05-27 02:18:09 +05:30
|
|
|
for tag_name, tag_data in tags.iteritems():
|
2008-04-23 14:37:02 +05:30
|
|
|
if tag_name.startswith(str(style)):
|
2009-05-27 02:18:09 +05:30
|
|
|
for start, end in tag_data:
|
2007-04-27 05:48:09 +05:30
|
|
|
self.remove_tag_by_name(tag_name,
|
|
|
|
self.get_iter_at_offset(start),
|
|
|
|
self.get_iter_at_offset(end+1))
|
2010-05-06 21:24:33 +05:30
|
|
|
|
|
|
|
def clear_selection(self):
|
|
|
|
"""
|
|
|
|
Clear tags from selection.
|
|
|
|
"""
|
|
|
|
start, end = self._get_selection()
|
|
|
|
tags = self._get_tag_from_range(start.get_offset(), end.get_offset())
|
|
|
|
removed_something = False
|
|
|
|
for tag_name, tag_data in tags.iteritems():
|
|
|
|
if tag_name.startswith("link"):
|
|
|
|
for start_pos, end_pos in tag_data:
|
|
|
|
self.remove_tag_by_name(tag_name,
|
|
|
|
self.get_iter_at_offset(start_pos),
|
|
|
|
self.get_iter_at_offset(end_pos+1))
|
|
|
|
removed_something = True
|
|
|
|
|
|
|
|
for style in ALLOWED_STYLES:
|
|
|
|
value = self.style_state[style]
|
|
|
|
if value and (value != StyledTextTagType.STYLE_DEFAULT[style]):
|
|
|
|
self.remove_tag(self._find_tag_by_name(style, value),
|
|
|
|
start, end)
|
|
|
|
removed_something = True
|
|
|
|
return removed_something
|
2007-04-27 05:48:09 +05:30
|
|
|
|
2008-04-03 02:04:29 +05:30
|
|
|
def _get_tag_from_range(self, start=None, end=None):
|
2007-07-03 15:06:48 +05:30
|
|
|
"""Extract gtk.TextTags from buffer.
|
2007-04-27 05:48:09 +05:30
|
|
|
|
2007-07-03 15:06:48 +05:30
|
|
|
Return only the name of the TextTag from the specified range.
|
|
|
|
If range is not given, tags extracted from the whole buffer.
|
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
@note: TextTag names are always composed like: (%s %s) % (style, value)
|
|
|
|
|
2007-07-03 15:06:48 +05:30
|
|
|
@param start: an offset pointing to the start of the range of text
|
|
|
|
@param type: int
|
|
|
|
@param end: an offset pointing to the end of the range of text
|
|
|
|
@param type: int
|
2007-04-27 05:48:09 +05:30
|
|
|
@return: tagdict
|
|
|
|
@rtype: {TextTag_Name: [(start, end),]}
|
|
|
|
|
|
|
|
"""
|
|
|
|
if start is None:
|
|
|
|
start = 0
|
|
|
|
if end is None:
|
|
|
|
end = self.get_char_count()
|
|
|
|
|
|
|
|
tagdict = {}
|
|
|
|
for pos in range(start, end):
|
|
|
|
iter = self.get_iter_at_offset(pos)
|
|
|
|
for tag in iter.get_tags():
|
|
|
|
name = tag.get_property('name')
|
2008-07-17 23:40:32 +05:30
|
|
|
if name in tagdict:
|
2007-04-27 05:48:09 +05:30
|
|
|
if tagdict[name][-1][1] == pos - 1:
|
|
|
|
tagdict[name][-1] = (tagdict[name][-1][0], pos)
|
|
|
|
else:
|
|
|
|
tagdict[name].append((pos, pos))
|
|
|
|
else:
|
|
|
|
tagdict[name]=[(pos, pos)]
|
|
|
|
return tagdict
|
2007-02-11 05:10:48 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
def _find_tag_by_name(self, style, value):
|
2007-04-27 05:48:09 +05:30
|
|
|
"""Fetch TextTag from buffer's tag table by it's name.
|
|
|
|
|
|
|
|
If TextTag does not exist yet, it is created.
|
|
|
|
|
|
|
|
"""
|
2010-05-06 21:24:33 +05:30
|
|
|
if style not in StyledTextTagType.STYLE_TYPE:
|
|
|
|
return None
|
|
|
|
elif StyledTextTagType.STYLE_TYPE[style] == bool:
|
2008-04-23 14:37:02 +05:30
|
|
|
tag_name = str(style)
|
2008-04-23 19:04:13 +05:30
|
|
|
elif StyledTextTagType.STYLE_TYPE[style] == str:
|
2008-04-23 14:37:02 +05:30
|
|
|
tag_name = "%d %s" % (style, value)
|
2008-04-23 19:04:13 +05:30
|
|
|
elif StyledTextTagType.STYLE_TYPE[style] == int:
|
2008-04-23 14:37:02 +05:30
|
|
|
tag_name = "%d %d" % (style, value)
|
2007-04-27 05:48:09 +05:30
|
|
|
else:
|
2008-04-23 14:37:02 +05:30
|
|
|
raise ValueError("Unknown style (%s) value type: %s" %
|
|
|
|
(style, value.__class__))
|
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
tag = self.get_tag_table().lookup(tag_name)
|
2008-04-23 14:37:02 +05:30
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
if not tag:
|
2008-04-23 19:04:13 +05:30
|
|
|
if StyledTextTagType.STYLE_TYPE[style] != bool:
|
2008-04-23 14:37:02 +05:30
|
|
|
# bool style tags are not created here, but in constuctor
|
2007-04-27 05:48:09 +05:30
|
|
|
tag = self.create_tag(tag_name)
|
2008-04-23 14:37:02 +05:30
|
|
|
tag.set_property(STYLE_TO_PROPERTY[style], value)
|
2007-04-27 05:48:09 +05:30
|
|
|
else:
|
|
|
|
return None
|
|
|
|
return tag
|
2007-02-11 05:10:48 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
# Public API
|
2007-04-27 05:48:09 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
def set_text(self, s_text):
|
|
|
|
"""Set the content of the buffer with markup tags.
|
2007-04-27 05:48:09 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
@note: 's_' prefix means StyledText*, while 'g_' prefix means gtk.*.
|
2007-04-27 05:48:09 +05:30
|
|
|
|
|
|
|
"""
|
2010-08-16 17:14:26 +05:30
|
|
|
super(StyledTextBuffer, self).set_text(str(s_text))
|
2010-08-31 01:05:51 +05:30
|
|
|
#self.remove_all_tags(self.get_start_iter(), self.get_end_iter())
|
2008-03-29 04:52:46 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
s_tags = s_text.get_tags()
|
|
|
|
for s_tag in s_tags:
|
2011-03-28 13:56:47 +05:30
|
|
|
if s_tag.name == _('Link'):
|
2010-05-06 21:24:33 +05:30
|
|
|
g_tag = LinkTag(self, s_tag.value,
|
|
|
|
foreground="blue",
|
|
|
|
underline=UNDERLINE_SINGLE)
|
|
|
|
else:
|
|
|
|
g_tag = self._find_tag_by_name(int(s_tag.name), s_tag.value)
|
2008-03-29 04:52:46 +05:30
|
|
|
if g_tag is not None:
|
2008-04-23 14:37:02 +05:30
|
|
|
for (start, end) in s_tag.ranges:
|
2007-07-16 01:33:51 +05:30
|
|
|
start_iter = self.get_iter_at_offset(start)
|
|
|
|
end_iter = self.get_iter_at_offset(end)
|
2008-03-29 04:52:46 +05:30
|
|
|
self.apply_tag(g_tag, start_iter, end_iter)
|
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
def get_text(self, start=None, end=None, include_hidden_chars=True):
|
2008-04-23 14:37:02 +05:30
|
|
|
"""Return the buffer text.
|
|
|
|
|
|
|
|
@note: 's_' prefix means StyledText*, while 'g_' prefix means gtk.*.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if start is None:
|
2007-04-27 05:48:09 +05:30
|
|
|
start = self.get_start_iter()
|
2008-04-23 14:37:02 +05:30
|
|
|
if end is None:
|
2007-04-27 05:48:09 +05:30
|
|
|
end = self.get_end_iter()
|
|
|
|
|
2010-08-16 17:14:26 +05:30
|
|
|
txt = super(StyledTextBuffer, self).get_text(start, end, include_hidden_chars)
|
2008-03-29 04:52:46 +05:30
|
|
|
txt = unicode(txt)
|
|
|
|
|
2007-04-27 05:48:09 +05:30
|
|
|
# extract tags out of the buffer
|
2008-04-03 02:04:29 +05:30
|
|
|
g_tags = self._get_tag_from_range()
|
2008-04-23 14:37:02 +05:30
|
|
|
s_tags = []
|
2007-04-27 05:48:09 +05:30
|
|
|
|
2008-03-29 04:52:46 +05:30
|
|
|
for g_tagname, g_ranges in g_tags.items():
|
2010-05-06 21:24:33 +05:30
|
|
|
if g_tagname.startswith('link'):
|
|
|
|
tag = self.get_tag_table().lookup(g_tagname)
|
|
|
|
s_ranges = [(start, end+1) for (start, end) in g_ranges]
|
|
|
|
s_value = tag.data
|
|
|
|
s_tag = StyledTextTag('Link', s_value, s_ranges)
|
|
|
|
s_tags.append(s_tag)
|
|
|
|
else:
|
|
|
|
style_and_value = g_tagname.split(' ', 1)
|
2008-03-29 04:52:46 +05:30
|
|
|
|
2010-05-06 21:24:33 +05:30
|
|
|
try:
|
|
|
|
style = int(style_and_value[0])
|
|
|
|
if len(style_and_value) == 1:
|
|
|
|
s_value = None
|
|
|
|
else:
|
|
|
|
s_value = StyledTextTagType.STYLE_TYPE[style]\
|
|
|
|
(style_and_value[1])
|
|
|
|
|
|
|
|
if style in ALLOWED_STYLES:
|
|
|
|
s_ranges = [(start, end+1) for (start, end) in g_ranges]
|
|
|
|
s_tag = StyledTextTag(style, s_value, s_ranges)
|
|
|
|
|
|
|
|
s_tags.append(s_tag)
|
|
|
|
except ValueError:
|
|
|
|
_LOG.debug("silently skipping gtk.TextTag '%s'" % g_tagname)
|
2008-04-23 14:37:02 +05:30
|
|
|
|
|
|
|
return StyledText(txt, s_tags)
|
|
|
|
|
|
|
|
def apply_style(self, style, value):
|
|
|
|
"""Apply a style with the given value to the selection.
|
|
|
|
|
|
|
|
@param style: style type to apply
|
|
|
|
@type style: L{StyledTextTagStyle} int value
|
|
|
|
@param value: value of the style type
|
|
|
|
@type value: depends on the I{style} type
|
|
|
|
|
|
|
|
"""
|
2008-04-23 19:04:13 +05:30
|
|
|
if not isinstance(value, StyledTextTagType.STYLE_TYPE[style]):
|
2008-04-23 14:37:02 +05:30
|
|
|
raise TypeError("Style (%d) value must be %s and not %s" %
|
2008-04-23 19:04:13 +05:30
|
|
|
(style, StyledTextTagType.STYLE_TYPE[style],
|
|
|
|
value.__class__))
|
2008-04-23 14:37:02 +05:30
|
|
|
|
|
|
|
self._apply_style_to_selection(style, value)
|
|
|
|
|
|
|
|
def remove_style(self, style):
|
|
|
|
"""Delete all occurences with any value of the given style.
|
|
|
|
|
|
|
|
@param style: style type to apply
|
|
|
|
@type style: L{StyledTextTagStyle} int value
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._remove_style_from_selection(style)
|
2007-04-27 05:48:09 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
def get_style_at_cursor(self, style):
|
|
|
|
"""Get the actual value of the given style at the cursor position.
|
2007-04-27 05:48:09 +05:30
|
|
|
|
2008-04-23 14:37:02 +05:30
|
|
|
@param style: style type to apply
|
|
|
|
@type style: L{StyledTextTagStyle} int value
|
|
|
|
@returns: value of the style type
|
|
|
|
@returntype: depends on the C{style} type
|
|
|
|
|
|
|
|
"""
|
|
|
|
return self.style_state[style]
|
|
|
|
|
2007-07-06 15:41:29 +05:30
|
|
|
def match_add(self, pattern, flavor):
|
|
|
|
"""Add a pattern to look for in the text."""
|
|
|
|
regex = re.compile(pattern)
|
|
|
|
self.patterns.append((regex, flavor))
|
|
|
|
|
|
|
|
def match_check(self, pos):
|
|
|
|
"""Check if pos falls into any of the matched patterns."""
|
|
|
|
for match in self.matches:
|
2008-03-29 04:52:46 +05:30
|
|
|
if pos >= match[MATCH_START] and pos <= match[MATCH_END]:
|
2007-07-06 15:41:29 +05:30
|
|
|
return match
|
|
|
|
|
|
|
|
return None
|