Commit 53c355d2 authored by John (J5) Palmieri's avatar John (J5) Palmieri

add override for TextBuffer

* TextBuffer.create_tag takes vargs which we can't bind yet so change it
  to except a keyword list of properties
* override the insert* methods so the developer does not have to enter a length
  - lengths are already encapsulated by a string in Python

https://bugzilla.gnome.org/show_bug.cgi?id=620583
parent 1d89a88b
......@@ -289,6 +289,57 @@ class Dialog(Gtk.Dialog):
Dialog = override(Dialog)
__all__.append('Dialog')
class TextBuffer(Gtk.TextBuffer):
def _get_or_create_tag_table(self):
table = self.get_tag_table()
if table is None:
table = Gtk.TextTagTable()
self.set_tag_table(table)
return table
def create_tag(self, tag_name=None, **properties):
"""
@tag_name: name of the new tag, or None
@properties: keyword list of properties and their values
Creates a tag and adds it to the tag table of the TextBuffer.
Equivalent to creating a Gtk.TextTag and then adding the
tag to the buffer's tag table. The returned tag is owned by
the buffer's tag table.
If @tag_name is None, the tag is anonymous.
If @tag_name is not None, a tag called @tag_name must not already
exist in the tag table for this buffer.
Properties are passed as a keyword list of names and values (e.g.
foreground = 'DodgerBlue', weight = Pango.Weight.BOLD)
Return value: a new tag
"""
tag = Gtk.TextTag(name=tag_name, **properties)
self._get_or_create_tag_table().add(tag)
return tag
def insert(self, iter, text):
if not isinstance(text , basestring):
raise TypeError('text must be a string, not %s' % type(text))
length = len(text)
Gtk.TextBuffer.insert(self, iter, text, length)
def insert_at_cursor(self, text):
if not isinstance(text , basestring):
raise TypeError('text must be a string, not %s' % type(text))
length = len(text)
Gtk.TextBuffer.insert_at_cursor(self, text, length)
TextBuffer = override(TextBuffer)
__all__.append('TextBuffer')
import sys
initialized, argv = Gtk.init_check(sys.argv)
......
......@@ -146,3 +146,28 @@ class TestGtk(unittest.TestCase):
button = dialog.get_widget_for_response (Gtk.ResponseType.CLOSE)
self.assertEquals(Gtk.STOCK_CLOSE, button.get_label())
def test_text_buffer(self):
self.assertEquals(Gtk.TextBuffer, overrides.Gtk.TextBuffer)
buffer = Gtk.TextBuffer()
tag = buffer.create_tag ('title', font = 'Sans 18')
self.assertEquals(tag.props.name, 'title')
self.assertEquals(tag.props.font, 'Sans 18')
start = Gtk.TextIter()
end = Gtk.TextIter()
(start, end) = buffer.get_bounds()
buffer.insert(end, 'HelloHello')
buffer.insert(end, ' Bob')
cursor_iter = end.copy()
cursor_iter.backward_chars(9)
buffer.place_cursor(cursor_iter)
buffer.insert_at_cursor(' Jane ')
(start, end) = buffer.get_bounds()
text = buffer.get_text(start, end, False)
self.assertEquals(text, 'Hello Jane Hello Bob')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment