# -*- encoding: utf-8 -*-

import os
import gtk
import webkit
import jinja2

def loadHtmlTemplate():
    templateFile = file(os.path.abspath('word_tip_template.html'));
    try:
        templateContent = templateFile.read()
    finally:
        templateFile.close()
    htmlTemplate = jinja2.Template(templateContent) 
    return htmlTemplate

class TipWindow(gtk.Window):
    htmlTemplate = loadHtmlTemplate()
    
    def __init__(self):
        gtk.Window.__init__(self, gtk.WINDOW_POPUP)
        self.set_decorated(False)
        self.set_position(gtk.WIN_POS_MOUSE)
        self.set_default_size(200, 150)
        
        self.vbox = gtk.VBox(False, 0)
        self.add(self.vbox)
        self.vbox.show()
        
        self.webViewScrolledWindow = gtk.ScrolledWindow()
        self.webViewScrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.vbox.pack_start(self.webViewScrolledWindow, expand=True, fill=True, padding=0)
        self.webViewScrolledWindow.show()
        
        self.webview = webkit.WebView()
        self.webViewScrolledWindow.add(self.webview)
        self.webview.show()
        
        self.hbuttonBox = gtk.HButtonBox()
        self.hbuttonBox.set_homogeneous(False)
        self.hbuttonBox.set_layout(gtk.BUTTONBOX_END)
        self.vbox.pack_end(self.hbuttonBox, expand=False, fill=True, padding=0)
        
#         dragWidget=self.hbuttonBox
#         dragWidget.set_events(gtk.gdk.EXPOSURE_MASK|gtk.gdk.LEAVE_NOTIFY_MASK
#                              |gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK
#                              |gtk.gdk.POINTER_MOTION_MASK|gtk.gdk.POINTER_MOTION_HINT_MASK
#                              )
#         dragWidget.connect("button-press-event", self.onButtonPress)
#         dragWidget.connect("motion-notify-event", self.onMotionNotify)
#         dragWidget.connect("button-release-event", self.onButtonRelease)
        
        self.hideButton = gtk.Button()
        self.hideButton.connect("clicked", lambda widget:self.hide())
        self.hideButton.set_tooltip_text("Close")
        self.hideButton.set_image(gtk.image_new_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU))
        self.hideButton.set_relief(gtk.RELIEF_NONE)
#         self.hideButton.set_label("Close")
        self.hbuttonBox.pack_end(self.hideButton, expand=False, fill=False, padding=0)
        style = gtk.RcStyle()
        style.xthickness = 0
        style.ythickness = 0
        self.hideButton.modify_style(style)
#         gtk.rc_parse_string("""
#        style "tab-close-button-style" {
#              GtkWidget::focus-padding = 0
#              GtkWidget::focus-line-width = 0
#              xthickness = 0
#              ythickness = 0
#          }
#          widget "*.tab-close-button" style "tab-close-button-style"
#          """)
#         self.hideButton.set_name("hideButton.tab-close-button")
    
    def showWordTip(self, word, pointer):
        html = TipWindow.htmlTemplate.render(word=word)
        self.webview.load_html_string(html, "./")
        self.move(pointer[1], pointer[2] + 20)
        self.show_all()
        self.show_now()

    def onButtonPress(self,widget,event):
        print 'onButtonPress'
    
    def onMotionNotify(self,widget,event):
#         print 'onMotionNotify'
        pass
    
    def onButtonRelease(self,widget,event):
        print 'onButtonRelease'

