[pygobject/pygi] add override for TextBuffer



commit 53c355d2cc0894e7f551e9b4eb719b89188a978e
Author: John (J5) Palmieri <johnp redhat com>
Date:   Mon Jun 21 11:42:12 2010 -0400

    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

 gi/overrides/Gtk.py     |   51 +++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_overrides.py |   25 +++++++++++++++++++++++
 2 files changed, 76 insertions(+), 0 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 3e3ab4d..9f073c2 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -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)
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index e0290a1..a7903f7 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -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')



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]