#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:

    # This is a callback function. The data arguments are ignored
    # in this example. More on callbacks below.
    def hello(self, widget, data=None):
        print "Hello World"

    def delete_event(self, widget, event, data=None):
        # If you return FALSE in the "delete_event" signal handler,
        # GTK will emit the "destroy" signal. Returning TRUE means
        # you don't want the window to be destroyed.
        # This is useful for popping up 'are you sure you want to quit?'
        # type dialogs.
        print "delete event occurred"

        # Change FALSE to TRUE and the main window will not be destroyed
        # with a "delete_event".
        return False

    def destroy(self, widget, data=None):
        print "destroy signal occurred"
        gtk.main_quit()

    def __init__(self):
        # create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(0)

        # create a layout
        table = gtk.Table(2, 1, True)
        self.window.add(table)

        # create a textentry object
        self.textentry = gtk.Entry(max=0)
        self.textentry.connect("changed", self.enter_callback, self.textentry)
        table.attach(self.textentry, 0, 1, 0, 1)
 
        # Creates a new button with the label "Hello World".
        self.textview = gtk.TextView()
    
        # When the button receives the "clicked" signal, it will call the
        # function hello() passing it None as its argument.  The hello()
        # function is defined above.
        #self.button.connect("clicked", self.hello, None)
    
        # This will cause the window to be destroyed by calling
        # gtk_widget_destroy(window) when "clicked".  Again, the destroy
        # signal could come from here, or the window manager.
        #self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
    
        # This packs the button into the window (a GTK container).
        table.attach(self.textview, 0, 1, 1, 2)
    
        # The final step is to display this newly created widgets.
        self.textentry.show()
        self.textview.show()
        table.show()
        self.window.show()

    def enter_callback(self, widget, entry):
        text = entry.get_text()
        buffer = self.textview.get_buffer()
        #tag = buffer.create_tag("hyperlink", foreground='blue')
        #tag.connect('event', self.hyperlink_handler)
        buffer.set_text("")
        buffer.insert_at_cursor(text)
        buffer.insert_at_cursor("\n")
        buffer.insert_at_cursor(text)

    def hyperlink_handler(tag, textview, event, iter):
        if event.type == gtk.gdk.BUTTON_RELEASE:
            print tag

    def main(self):
        # All PyGTK applications must have a gtk.main(). Control ends here
        # and waits for an event to occur (like a key press or mouse event).
        gtk.main()

# If the program is run directly or passed as an argument to the python
# interpreter then create a HelloWorld instance and show it
if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()
