#!/usr/bin/env python

#EMBED_TAB_NAME = Setup Sheet
#EMBED_TAB_LOCATION = ntb_preview
#EMBED_TAB_COMMAND = gladevcp -x {XID} -u setup_sheet.py setup_sheet.glade

import gi
import sys
import os
import linuxcnc
from gi.repository import Gtk as gtk

gi.require_version('WebKit2', '4.0')
from gi.repository import WebKit2 as Webkit

def get_handlers(halcomp, builder, useropts):
    return[HandlerClass(halcomp, builder, useropts)]

class HandlerClass():

    def onButtonPressed(self, widget, data=None):
      s = linuxcnc.stat()
      s.poll()

      file_path_full = getattr(s,"file")
    
      file_path = os.path.dirname(file_path_full) +  "/"
    
      file_name_without_ext = os.path.splitext(os.path.basename(file_path_full))[0]

      print("file://" + file_path + file_name_without_ext + ".html")
      
      webview.load_uri("file://" + file_path + file_name_without_ext + ".html")
      
    def __init__(self, halcomp, builder, useropts):
        super(HandlerClass, self).__init__()
        
        self.builder = builder
        
        self.win_1 = self.builder.get_object('window1')
        self.win_1.connect("destroy", gtk.main_quit)

        global webview
        webview = Webkit.WebView()
        
        scrolled_window = builder.get_object("scrolled_window")
        scrolled_window.add_with_viewport(webview)

        #webview.load_uri("file:///home/vb/Schreibtisch/NC/Encoder_Plate_OP1.html")

        self.win_1.show_all()
        

