# -*- coding: UTF-8 -*-

import time

import gtk
import gobject

                           
def on_test_button_clicked(button, buffer):
    print 'inserting some text'
    buffer.insert(buffer.get_start_iter(), 'Hello,\n\ntest!')

    while gtk.events_pending():
        gtk.main_iteration(False)
    time.sleep(2)

    print 'serializing data'
    data = buffer.serialize(buffer,
                           "application/x-gtk-text-buffer-rich-text",
                           buffer.get_start_iter(),
                           buffer.get_end_iter())  
    
    print 'clearing text'
    buffer.delete(buffer.get_start_iter(), buffer.get_end_iter())

    while gtk.events_pending():
        gtk.main_iteration(False)

    time.sleep(2)
    
    print 'deserializing data'
    result = buffer.deserialize(buffer,
                 "application/x-gtk-text-buffer-rich-text",
                 buffer.get_start_iter(),
                 data)


if __name__ == '__main__':
    window = gtk.Window()
    window.connect('delete-event', gtk.main_quit)
    vbox = gtk.VBox()
    window.add(vbox)

    buffer = gtk.TextBuffer()
    buffer.register_deserialize_tagset()
    view = gtk.TextView(buffer)
    vbox.pack_start(view)

    button = gtk.Button('Test')
    button.connect('clicked', on_test_button_clicked, buffer)
    vbox.pack_start(button)

    window.show_all()

    gtk.main()
