import hal
import os



# path to TCL for external programs eg. halshow
try:
    TCLPATH = os.environ['LINUXCNC_TCL_DIR']
except:
    pass

# This is a handler file for using Gscreen's infrastructure
# to load a completely custom glade screen.
# The only things that really matters is that it's saved as a GTK builder project,
# the toplevel window is caller window1 (The default name) and you connect a destroy
# window signal else you can't close down linuxcnc 

# standard handler call
def get_handlers(halcomp,builder,useropts,gscreen):
     return [HandlerClass(halcomp,builder,useropts,gscreen)]


            
class HandlerClass:

    # This will be pretty standard to gain access to everything
    # emc is for control and status of linuxcnc
    # data is important data from gscreen and linuxcnc
    # widgets is all the widgets from the glade files
    # gscreen is for access to gscreens methods
    def __init__(self, halcomp,builder,useropts,gscreen):
            self.emc = gscreen.emc
            self.data = gscreen.data
            self.widgets = gscreen.widgets
            self.gscreen = gscreen
           



    # This connects siganals without using glade's autoconnect method
    # in this case to destroy the window
    # it calls the method in gscreen: gscreen.on_window_destroy()
    def connect_signals(self,handlers):
        signal_list = [ ["window1","destroy", "on_window1_destroy"],]
        for i in signal_list:
            if len(i) == 3:
                self.gscreen.widgets[i[0]].connect(i[1], self.gscreen[i[2]])
            elif len(i) == 4:
                self.gscreen.widgets[i[0]].connect(i[1], self.gscreen[i[2]],i[3])
                
    #zoom in
    def on_zoom_in_pressed(self,*args):
        self.widgets.gremlin.zoom_in()
        
    #zoom out
    def on_zoom_out_pressed(self,*args):
        self.widgets.gremlin.zoom_out()
    
    #view d3
    def on_view_3d_pressed(self, widget, data=None):
        self.widgets.gremlin.current_view = 'p'
        self.widgets.gremlin.set_current_view()
        
    #halmeter
    def on_halmeter_pressed(self, widget, data=None):
        p = os.popen( "halmeter &" )

    #classicladder
    def on_classicladder_pressed(self, widget, data=None):
        p = os.popen( "classicladder  &", "w" )

    #halshow
    def on_halshow_pressed(self, widget, data=None):
        p = os.popen( "tclsh %s/bin/halshow.tcl &" % TCLPATH )
                
                
    def on_home_all_pressed(self, widget, data=None):
       print " home all pressed "
       self.gscreen.home_all()
       
    def on_unhome_all_pressed(self, widget, data=None):
       print " unhome all pressed "
       self.gscreen.unhome_all()
   
    def on_x_home_pressed(self, widget, data=None):
       print " x home pressed "
       self.emc.home_selected(0)
       
    def on_y_home_pressed(self, widget, data=None):
       print " y home pressed "
       self.emc.home_selected(1)
       
    def on_x_plus_pressed(self, widget, data=None):
       print " x plus pressed "         
       self.emc.continuous_jog(0,1)  
    def on_x_plus_released(self, widget, data=None):
       print " x plus released "         
       self.emc.continuous_jog(0,0)
       
    def on_x_min_pressed(self, widget, data=None):
       print " x min pressed "         
       self.emc.continuous_jog(0,-1) 
    def on_x_min_released(self, widget, data=None):
       print " x min released "
       self.emc.continuous_jog(0,0)

    # set linear jog rate at the jog_speed value.
    def on_jog_speed_value_changed(self, widget,value):
        self.emc.continuous_jog_velocity(velocity = self.widgets.jog_speed.get_value())
        
    def on_program_speed_value_changed(self,widget,value):
        self.emc.feed_override(self.widgets.program_speed.get_value())
    
    def on_reset_program_speed_pressed(self, widget, data=None):
        self.widgets.program_speed.set_value(1)
        # update program_speed widget to linuxcnc
        self.emc.feed_override(self.widgets.program_speed.get_value())
    
    # We don't want Gscreen to initialize it's regular widgets because this custom
    # screen doesn't have most of them. So we add this function call.
    # Since this custom screen uses gladeVCP magic for its interaction with linuxcnc
    # We don't add much to this function, but we do want the window to display.
    # init_show_window will do this
    def initialize_widgets(self):
        self.gscreen.init_show_windows()
        # set the initial linear jog rate at the jog_speed initial value.
        self.emc.continuous_jog_velocity(self.widgets.jog_speed.get_value())
        # set the initial program speed override.
        self.emc.feed_override(self.widgets.program_speed.get_value())
        
        self.widgets.gremlin.current_view = 'p'
        self.widgets.gremlin.set_current_view()
 
    # If we need extra HAL pins here is where we do it.
    # Note you must import hal at the top of this script to do it.
    # For gaxis there is no extra pins but since we don't want gscreen to
    # add it's default pins we added this dunmmy function
    def initialize_pins(self):
        pass

    # every 100 milli seconds this gets called
    # add pass so gscreen doesn't try to update it's regular widgets or
    # add the individual function names that you would like to call.
    def periodic(self):
        #self.widgets.led_emergency_stop.set_active(self.data.estopped)
        self.widgets.led_machine_on.set_active(self.data.machine_on) 
      


