W = root_window.tk.call
import linuxcnc
c = linuxcnc.command()
#s = linuxcnc.stat()




############################################
#######    FIX select_next    #################
#####    for some reason it works usercommand but not axis.py  #####
###################################


def select_next_line():
    if o.highlight_line is None:
        i = 1
    else:
        i = max(o.last_line, o.highlight_line + 1)
    o.set_highlight_line(i)
    o.tkRedraw()
##############################################
#####   and do select_prev also #############
########################################
def select_prev_line():
    if o.highlight_line is None:
        i = o.last_line
    else:
        i = max(1, o.highlight_line - 1)
    o.set_highlight_line(i)
    o.tkRedraw()


##############################################
##########   end NEXT PREV LINE  ############
##################################





##########################################################
########       LOAS_LAST FILE   PY3        ##################
########   In ini file under [DISPLAY]  ##################
########     LOAD_LASTFILE = 	YES       ##################
##########################################################

loadlast = inifile.find('DISPLAY', 'LOAD_LASTFILE')
if loadlast == "YES" :
    load_lastfile = True
else:
    load_lastfile = False


lastfile = ""
recent = ap.getpref('recentfiles', [], repr)
if len(recent):
    lastfile = recent.pop(0)

code = []
addrecent = True
if args:
    initialfile = args[0]
elif "AXIS_OPEN_FILE" in os.environ:
    initialfile = os.environ["AXIS_OPEN_FILE"]
elif inifile.find("DISPLAY", "OPEN_FILE"):
    initialfile = inifile.find("DISPLAY", "OPEN_FILE")
elif os.path.exists(lastfile) and load_lastfile:
    initialfile = lastfile
    print ("Loading ") 
    print (initialfile)
elif lathe:
    initialfile = os.path.join(BASE, "share", "axis", "images","axis-lathe.ngc")
    addrecent = False
else:
    initialfile = os.path.join(BASE, "share", "axis", "images", "axis.ngc")
    addrecent = False

if os.path.exists(initialfile):
    open_file_guts(initialfile, False, addrecent)



##########################################################
###################   LOAS_LAST FILE END #################
##########################################################




















###########################################################
##  add mdi and manual buttons to toolbar for easy access
###########################################################
W('button','.toolbar.man','-command','show_manual')
W('button','.toolbar.gcode','-command','show_gcode')
W('button','.toolbar.view','-command','show_view')
W('button','.toolbar.home','-command','home_all_joints')
W('frame','.toolbar.spacer2','-borderwidth','1','-height','6','-width','2','-relief','sunken')     

W('pack','.toolbar.spacer2','-after','.toolbar.machine_power','-side','left','-fill','y','-padx','4','-pady','4')
W('pack','.toolbar.man','-before','.toolbar.rule0','-side','left','-fill','y')
W('pack','.toolbar.gcode','-before','.toolbar.rule0','-side','left','-fill','both')
W('pack','.toolbar.view','-before','.toolbar.rule0','-side','left','-fill','both')
W('pack','.toolbar.home','-before','.toolbar.rule0','-side','left','-fill','both')


W('image','create','photo','tool_man','-file','image/tool_man.gif')
W('image','create','photo','tool_gcode','-file','image/tool_gcode.gif')
W('image','create','photo','tool_preview','-file','image/tool_preview.gif')

W('image','create','photo','tool_home','-file','image/tool_home.gif')

########  BUTTON CONFIG MOVED TO TOOLBAR BUTTON CONFIG SECTION    ############




#####################################
######
####################################

###   also add spindle speed and feed to toolbar

#W('labelframe','.toolbar.feed')
#W('label','.toolbar.feed.feedrate-upm','-anchor','e','-width','5','-fg','slategray4','-font','mono 16')
#W('label','.toolbar.feed.feedrate-upmlab','-text','FEED','-anchor','w','-width','4','-font','mono 12')
#W('pack','.toolbar.feed.feedrate-upmlab','-side','left','-fill','x')
#W('pack','.toolbar.feed.feedrate-upm','-side','right','-fill','x')
#W('pack','.toolbar.feed','-side','top','-fill','x')

#W('labelframe','.toolbar.speed')
#W('label','.toolbar.speed.spindle-speed','-anchor','e','-width','5','-fg','slategray4','-font','mono 16')
#W('label','.toolbar.speed.spindle-speedlab','-text','SPEED','-anchor','w','-width','6','-font','mono 12')
#W('pack','.toolbar.speed.spindle-speedlab','-side','left','-fill','y')
#W('pack','.toolbar.speed.spindle-speed','-side','right','-fill','x')
#W('pack','.toolbar.speed','-side','bottom','-fill','x')


################################
#### tab packs to the left now
#####   PACKIONG THE DYNAMIC TAB  ##########
######################################
def _dynamic_tab(name, text):
    tab = widgets.tabs.insert("end", name, text=text)
    tab.configure(borderwidth=1, highlightthickness=0)
    return tab

#######################################################


####################################################
#######      USER TAB           ##########
### ADD NEW TAB TO LEFT SIDE  #######

W('.pane.top.tabs','insert','end', 'user', '-text',' user','-activebackground','darkgrey','-background','grey')
#W('NoteBook','.pane.top.tabs','-side','bottom')
#W('.pane.top.tabs','configure','-side','bottom','-width','450','-arcradius','8','-font','mono 12','-tabpady','1''1')

W('.pane.top.tabs','configure','-width','450','-arcradius','8','-font','mono 12','-tabpady','1''1')
##W('.pane.top.tabs','configure','-width','350','-arcradius','8','-font','mono 12','-tabpady','1''1')





###########  add buttons to tab  #########
#W('labelframe','.pane.top.tabs.fuser')

#W('grid','.pane.top.tabs.fuser','-column','0','-row','1','-sticky','nw')
W('grid','columnconfigure','.pane.top.tabs.fuser','0','-weight','1')

W('button','.pane.top.tabs.fuser.scrl','-text','^','-command','tk_getOpenFile')
W('grid','.pane.top.tabs.fuser.scrl','-column','1','-row','1','-sticky','ne')

#W('grid','.pane.top.tabs.fuser','-column','0','-row','1','-sticky','nw')
#W('grid','columnconfigure','.pane.top.tabs.user.frame','0','-weight','1')
W('label','.pane.top.tabs.fuser.ucs','-text','G5x')
W('grid','.pane.top.tabs.fuser.ucs','-column','0','-row','1','-sticky','nw')
#W('grid','columnconfigure','.pane.top.tabs.user','0','-weight','1')

W('text','.pane.top.tabs.fuser.t','-width','15','-height','8')
W('grid','.pane.top.tabs.fuser.t','-column','0','-row','2','-sticky','nw')






##########################################
#######      USER TAB           ##########
### ADD NEW TAB TO RIGHT SIDE  #######
######################################


W('.pane.top.right','insert','end', 'user', '-text',' user')
W('.pane.top.right','configure','-arcradius','8','-font','mono 12')

###########  add buttons to tab  #########


W('grid','columnconfigure','.pane.top.right.fuser','0','-weight','1')

W('button','.pane.top.right.fuser.scrl','-text','^')
W('grid','.pane.top.right.fuser.scrl','-column','1','-row','1','-sticky','ne')

W('label','.pane.top.right.fuser.ucs','-text','G5x')
W('grid','.pane.top.right.fuser.ucs','-column','0','-row','1','-sticky','nw')

W('text','.pane.top.right.fuser.t','-width','15','-height','8')
W('grid','.pane.top.right.fuser.t','-column','0','-row','2','-sticky','nw')

W('button','.pane.top.right.fuser.gcode_blue','-text','blue','-command','gcode_plotblue')
W('button','.pane.top.right.fuser.gcode_red','-text','red','-command','backplot')
W('grid','.pane.top.right.fuser.gcode_red','-column','1','-row','2','-sticky','nw')
W('grid','.pane.top.right.fuser.gcode_blue','-column','2','-row','2','-sticky','nw')

###############################################################
####   RAPID CHECK BOX   #######################
##########################################
W('labelframe','.pane.top.right.fuser.rap','-text','Rapid')
W('grid','.pane.top.right.fuser.rap','-column','0','-row','6','-sticky','sw')
W('grid','columnconfigure','.pane.top.right.fuser.rap','0','-weight','1')

def set_rapid10():
      c.rapidrate(.1)

def set_rapid25():
      c.rapidrate(.25)

def set_rapid50():
      c.rapidrate(.5)
      
def set_rapid100():
      c.rapidrate(1.0)    

W('button','.pane.top.right.fuser.rap.rap10','-text','10','-command','set_rapid10')
W('pack','.pane.top.right.fuser.rap.rap10','-fill','x','-side','left')

W('button','.pane.top.right.fuser.rap.rap25','-text','25','-command','set_rapid25')
W('pack','.pane.top.right.fuser.rap.rap25','-fill','x','-side','left')

W('button','.pane.top.right.fuser.rap.rap50','-text','50','-command','set_rapid50')
W('pack','.pane.top.right.fuser.rap.rap50','-fill','x','-side','left')

W('button','.pane.top.right.fuser.rap.rap100','-text','100','-command','set_rapid100')
W('pack','.pane.top.right.fuser.rap.rap100','-fill','x','-side','left')



###############################################################
####   RAPID CHECK BOX   #######################
######  so you can hide scroll bar ##############
#######    end          #############################
##########################################

sliders = IntVar()
sliders.set(1)

gcode_hide = IntVar()
gcode_hide.set(1)

W('checkbutton','.pane.top.right.fuser.sliders','-text','Hide_sliders','-command','hide_sliders')
W('grid','.pane.top.right.fuser.sliders','-column','1','-row','6','-sticky','nw')

W('checkbutton','.pane.top.right.fuser.gcode_hide','-text','Hide_Gcode','-command','hide_gcode')
W('grid','.pane.top.right.fuser.gcode_hide','-column','1','-row','7','-sticky','nw')
W('text','.pane.bottom.t2','-width','15','-height','8')
W('grid','remove','.pane.bottom.t2')

def hide_sliders():
               # need to toggle variable manually for keyboard shortcut
        if IntVar() > 0:
            sliders.set(not sliders.get())

        if sliders.get():
            W('grid','.pane.top.feedoverride','-sticky','new')
            W('grid','.pane.top.rapidoverride')
            W('grid','.pane.top.spinoverride')
            W('grid','.pane.top.jogspeed')
            #W('grid','.pane.top.ajogspeed')
            #W('grid','.pane.top.maxvel')
         
        else:
            W('grid','remove','.pane.top.feedoverride')
            W('grid','remove','.pane.top.rapidoverride')
            W('grid','remove','.pane.top.spinoverride')
            W('grid','remove','.pane.top.jogspeed')
            W('grid','remove','.pane.top.ajogspeed')
            W('grid','remove','.pane.top.maxvel')
          
        o.tkRedraw()
      
     
def hide_gcode():
               # need to toggle variable manually for keyboard shortcut
        if IntVar() > 0:
            gcode_hide.set(not gcode_hide.get())

        if gcode_hide.get():
            #W('grid','.pane.bottom.t')
            W('grid','remove','.pane.bottom.t2')
            W('grid','.pane.bottom.t','-row','1','-column','1','-sticky','nesw')
            

         
        else:
            W('grid','remove','.pane.bottom.t')
            #W('text','.pane.bottom.t2','-width','15','-height','8')
            W('grid','.pane.bottom.t2','-column','1','-row','1','-sticky','nw')
          
        o.tkRedraw()

######################
##### END RIGHT TAB ###########################
##########################


########################################################
###############  choose/remove sliders ########################
########################################################

#W('grid','forget','.pane.top.feedoverride')
W('grid','forget','.pane.top.rapidoverride')
#W('grid','forget','.pane.top.spinoverride')
W('grid','forget','.pane.top.jogspeed')
#W('grid','forget','.pane.top.ajogspeed')
W('grid','forget','.pane.top.maxvel')

########################################################
############ remove active gcodes lable and codes ##########
########################################################

W('grid','forget','.pane.top.gcodel')
W('pack','forget','.pane.bottom')
W('grid','forget','.pane.bottom')

## -remove bottom frame -##
W('pack','forget','.pane.bottom.t.text')
W('pack','forget','.pane.bottom.t.sb')





###  pack something under gcode  #######
###W('grid','.pane.bottom.common','-column','1','-row','2','-sticky','nw')


#################################
###  grid something above gcode  #######
####  FILENAME AND RUNTIME      #######
##### .pane.bottom.program   ########
####################################

W('labelframe','.pane.bottom.program','-text','File')
W('grid','.pane.bottom.program','-column','1','-row','5','-sticky','nesw')
W('grid','columnconfigure','.pane.bottom.program','0','-weight','1')
#W('label','.pane.bottom.program.line','-text','line','-font','mono 12','-justify','left')
#W('grid','.pane.bottom.program.line','-column','0','-row','0')
W('label','.pane.bottom.program.name','-text','file','-font','mono 12','-justify','left')
W('grid','.pane.bottom.program.name','-column','0','-row','0')
W('label','.pane.bottom.program.time','-text','HH:MM:SS','-font','mono 12','-justify','right')
W('grid','.pane.bottom.program.time','-column','2','-row','0')





###############################################
##############  PANE BOTTOM COMMON   ####################
###    add common frame beside t.test   ################
####################################################

W('labelframe','.pane.bottom.common')
#W('labelframe','.pane.bottom.common','-text','user_frame')
W('grid','.pane.bottom.common','-column','0','-row','1','-sticky','sw')
W('grid','columnconfigure','.pane.bottom.common','0','-weight','1')




##### add userframe corrdinate display as baseline working test ######
W = root_window.tk.call
W('label','.pane.bottom.common.ucs','-text','G5x','-font','mono 12')
W('grid','.pane.bottom.common.ucs','-column','2','-row','0','-sticky','w')
 
#### add feedrate lable and grid into bottom.common ####################
W('label','.pane.bottom.common.feedrate-upm','-anchor','e','-width','5','-fg','blue','-font','mono 12')
W('label','.pane.bottom.common.feedrate-upmlab','-text','FEED','-anchor','w','-width','5','-font','mono 12')
W('grid','.pane.bottom.common.feedrate-upm','-row','1','-column','3','-rowspan','1','-sticky','se')
W('grid','.pane.bottom.common.feedrate-upmlab','-row','1','-column','2','-sticky','w')
##spindle-speed
W('label','.pane.bottom.common.spindle-speed','-anchor','e','-width','5','-fg','blue','-font','mono 12')
W('label','.pane.bottom.common.spindle-speedlab','-text','SPEED','-anchor','w','-width','5','-font','mono 12')
W('grid','.pane.bottom.common.spindle-speed','-row','2','-column','3','-rowspan','1','-sticky','se')
W('grid','.pane.bottom.common.spindle-speedlab','-row','2','-column','2','-sticky','w')
## tool number
W('label','.pane.bottom.common.tool-number','-anchor','e','-width','5','-fg','blue','-font','mono 12')
W('label','.pane.bottom.common.tool-numberlab','-text','TOOL','-anchor','w','-width','5','-font','mono 12')
W('grid','.pane.bottom.common.tool-number','-row','3','-column','3','-rowspan','1','-sticky','se')
W('grid','.pane.bottom.common.tool-numberlab','-row','3','-column','2','-sticky','w')



#############################
#######  add search_bar  #########
###########################

## add search entry widget ###
W('entry','.pane.bottom.common.srch_bar','-width','14')

###  add gcode scroll buttons   ####

W('button','.pane.bottom.common.scrl_up','-text','V','-command','select_next_line')
W('button','.pane.bottom.common.scrl_dwn','-text','^','-command','select_prev_line')
W('button','.pane.bottom.common.srch_btn','-text','search','-command','search_gcode','-relief','flat','-bg','darkgrey','-highlightbackground','black')
W('button','.pane.bottom.common.run_line','-text','RUN FROM LINE','-command','task_run_line')

#    search
W(".pane.bottom.common.srch_btn","configure"
,"-image",""
,"-text","SEARCH"
,"-command","search_gcode"
,"-activebackground","lightgrey"
,"-background","grey30"
,"-foreground","grey90"
,"-borderwidth","1"
,"-font","mono 12"
,"-height","1"
,"-width","12"
,"-highlightthickness", 1
,"-highlightcolor","lightgrey"
,"-highlightbackground","lightgrey"
,"-relief","flat"
);



# scroll up
W(".pane.bottom.common.scrl_up","configure"
,"-image",""
,"-text","V"
,'-command','select_next_line'
,"-activebackground","lightgrey"
,"-background","grey30"
,"-foreground","grey90"
,"-borderwidth","1"
,"-font","mono 12"
,"-height","1"
,"-width","12"
,"-highlightthickness", 1
,"-highlightcolor","lightgrey"
,"-highlightbackground","lightgrey"
,"-relief","flat"
,"-takefocus",0
);

# scroll down
W(".pane.bottom.common.scrl_dwn","configure"
,"-image",""
,"-text","^"
,"-activebackground","lightgrey"
,"-background","grey30"
,"-foreground","grey90"
,"-borderwidth","1"
,"-font","mono 12"
,"-height","1"
,"-width","12"
,"-highlightthickness", 1
,"-highlightcolor","lightgrey"
,"-highlightbackground","lightgrey"
,"-relief","flat"
);


#  RUN FROM LINE
W(".pane.bottom.common.run_line","configure"
,"-image",""
,"-text","RUN FROM LINE"
,"-activebackground","lightgrey"
,"-background","grey30"
,"-foreground","grey90"
,"-borderwidth","1"
,"-font","mono 12"
,"-height","1"
,"-width","12"
,"-highlightthickness", 1
,"-highlightcolor","lightgrey"
,"-highlightbackground","lightgrey"
,"-relief","flat"
);


#####  GCODE COLOUR BUTTONMS
#W('button','.pane.bottom.common.gcode_blue','-text','blue','-command','gcode_plotblue')
#W('button','.pane.bottom.common.gcode_red','-text','red','-command','backplot')
#W('grid','.pane.bottom.common.gcode_red','-column','1','-row','4','-sticky','nw')
#W('grid','.pane.bottom.common.gcode_blue','-column','1','-row','3','-sticky','nw')

W('grid','.pane.bottom.common.srch_bar','-column','0','-row','0','-sticky','nw')
W('grid','.pane.bottom.common.srch_btn','-column','0','-row','1','-sticky','nw')
W('grid','.pane.bottom.common.scrl_dwn','-column','0','-row','2','-sticky','nw')
W('grid','.pane.bottom.common.scrl_up','-column','0','-row','3','-sticky','nw')
W('grid','.pane.bottom.common.run_line','-column','0','-row','4','-sticky','nw')


#########################################
##############################################3
############################################

########################################
##### change gcode plot colour   ###################
######################################
# sets plotter colors to default if arguments left out
def set_plot_colors(self, jog=None,traverse=None,feed=None,
                    arc=None,toolchange=None,probe=None):
        def C(s):
            a = self.colors[s + "_alpha"]
            s = self.colors[s]
            return [int(x * 255) for x in s + (a,)]

        if jog is None:
            jog = C('backplotjog')
        if traverse is None:
            traverse = C('backplottraverse')
        if feed is None:
           feed = C('backplotfeed')
        if arc is None:
            arc = C('backplotarc')
        if toolchange is None:
            toolchange = C('backplottoolchange')
        if probe is None:
            probe = C('backplotprobing')
        try:
            self.logger.set_colors(
                jog,
                traverse,
                feed,
                arc,
                toolchange,
                probe)
        except Exception as e:
            print(e)


def gcode_plotblue():
       try:
            live_plotter.logger.set_colors((10,135,207,255),
                                    (128,128,0,255),
                                    (10,135,207,255),
                                    (10,135,207,255),
                                    (255,255,255,255),
                                    (0,255,255,255))
       except Exception as e:
            print (e)


def backplot():
     
     # tool 1 = #Red (255,0,0)
      if s.tool_in_spindle == 1:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (255,0,0,255),
                                    (255,0,0,255),
                                    (255,255,255,255),
                                    (0,255,255,255))
      #tool 2 = #Lime (0,255,0)
      elif s.tool_in_spindle == 2:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (0,255,0,255),
                                    (0,255,0,255),
                                    (255,255,255,255),
                                    (0,255,255,255))
                                    
                                    
      #tool 8 = #Blue (0,0,255)
      elif s.tool_in_spindle == 8:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (0,0,255,255),
                                    (0,0,255,255),
                                    (255,255,255,255),
                                    (0,255,255,255))                             
       #tool 22 = #Yellow (255,255,0)
      elif s.tool_in_spindle == 22:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (255,255,0,255),
                                    (255,255,0,255),
                                    (255,255,255,255),
                                    (0,255,255,255))  
       #tool 24 = #Cyan (0,255,255)
      elif s.tool_in_spindle == 24:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (0,255,255,255),
                                    (0,255,255,255),
                                    (255,255,255,255),
                                    (0,255,255,255))                               
                                    
                                    
       #tool 7 = #Magenta / (255,0,255)
      elif s.tool_in_spindle == 7:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (255,0,255,255),
                                    (255,0,255,255),
                                    (255,255,255,255),
                                    (0,255,255,255))                               
       #tool 18 = #Maroon (128,0,0)
      elif s.tool_in_spindle == 18:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (128,0,0,255),
                                    (128,0,0,255),
                                    (255,255,255,255),
                                    (0,255,255,255))                               
        
           #tool 3 = #Olive (128,128,0)
      elif s.tool_in_spindle == 3:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (128,128,0,255),
                                    (128,128,0,255),
                                    (255,255,255,255),
                                    (0,255,255,255))                               
                                                                 
                                    
      else: 
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (0,255,0,255),
                                    (0,255,0,255),
                                    (255,255,255,255),
                                    (0,255,255,255))
      

#
#
#


def gcode_plotred():
       try:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    "backplot",
                                    "backplot",
                                    (255,255,255,255),
                                    (0,255,255,255))
       except Exception as e:
            print (e)


##########################
                #jog,
                #traverse,
                #feed,
                #arc,
                #toolchange,
                #probe
                
#################################################                
      #Black #000000	(0,0,0)
 	#White #FFFFFF	(255,255,255)
 	#Red	#FF0000	(255,0,0)
 	#Lime	#00FF00	(0,255,0)
 	#Blue	#0000FF	(0,0,255)
 	#Yellow #FFFF00	(255,255,0)
 	#Cyan / Aqua	(0,255,255)
 	#Magenta /  	(255,0,255)
 	#Silver		(192,192,192)
 	#Gray	      	(128,128,128)
 	#Maroon		(128,0,0)
 	#Olive		(128,128,0)
 	#Green		(0,128,0)
 	#Purple		(128,0,128)
 	#Teal	#008080	(0,128,128)
 	#Navy	#000080	(0,0,128)



def gcode_colours():
       try:
            live_plotter.logger.set_colors((255,0,0,255),
                                    (0,255,0,255),
                                    (0,0,255,255),
                                    (255,255,0,255),
                                    (255,255,255,255),
                                    (0,255,255,255))
       except Exception as e:
            print (e)




####################################################
#################   SEARCH GCODE COMMAND    ########
##################  still needs work for ############
##################     when not found  ##############
####################################################
###     to do    : using the while loop/break doesnt work becaused it goes to the end
###               I think the problem is how it breaks down the last line
#######             and sends it to linuxcnc

#####  search function  #########
se = '.pane.bottom.common.srch_bar'
      

def search_next():
      se.set()

def search_gcode():  
   
    # remove tag 'found' from index 1 to END  
    t.tag_remove('found', '1.0', END)  
    s =  W('.pane.bottom.common.srch_bar','get')
    if (s):         
        # get highlighted line from linuxcnc
        line = o.get_highlight_line()
        # start the search from current line +1 or you never go anywhere
        idx = ("%d.0" % (line+1))
        #while ("%d.0" % (line+1)):
            # searches for desried string from index highlighted line  
        idx = t.search(s, idx, nocase = 1,
                            forwards = 1,
                            stopindex = END)
        #if not idx: break
        # last index sum of current index and  
        # length of text  
        lastidx = '% s+% dc' % (idx, len(s))               
  
                  # overwrite 'Found' at idx  
        t.tag_add('found', idx, lastidx)  
        idx = lastidx 
        t.see(idx)
            # mark located string as yellow 
    t.tag_config('found', background ='lightsteelblue3')
      # split idx into something linuxcnc can use to set highlighted line
    idxh = int(idx.split('.')[0])
    o.set_highlight_line(idxh)
    o.tkRedraw()
    # clears the search bar once found 
    W('.pane.bottom.common.srch_bar','delete','0','end')
    
 
#############################
#### end search stuff #########
##############################



#################################################
##### USER LIVE UPDATE ############################
#####  this is where pins get updated in ui  ###########
##################################################



####### update feedrate
def user_live_update():
    #W('.toolbar.feed.feedrate-upm','configure','-text','%3.0f' % (comp['feedrate-upm']))
    #W('.toolbar.speed.spindle-speed','configure','-text','%3.0f' % (comp['spindle-speed']))
    W('.pane.bottom.common.feedrate-upm','configure','-text','%3.0f' % (comp['feedrate-upm']))
    W('.pane.bottom.common.spindle-speed','configure','-text','%3.0f' % (comp['spindle-speed']))
    W('.pane.bottom.common.tool-number','configure','-text','%3.0f' % (comp['tool-number']))

    ###coordinate
    W('.pane.bottom.common.ucs','configure','-text','U:' + all_systems[o.last_g5x_index].split()[1])
   ## loaded file
    W('.pane.bottom.program.name','configure','-text', os.path.basename(loaded_file))


#################################################
##### USER LIVE UPDATE ############################
#####         end             ######################
##################################################




####################################
######### USER HAL PINS  #####################
##########################################


def user_hal_pins():
    # create new hal pins
    #mycomp = hal.component('feed')
    comp.newpin('feedrate-upm', hal.HAL_FLOAT, hal.HAL_IN)
    comp.newpin('spindle-speed', hal.HAL_FLOAT, hal.HAL_IN)
    comp.newpin('tool-number', hal.HAL_U32, hal.HAL_IN)
    comp.ready()
    # create new signals and connect pins
    
    hal.new_sig('feedrate-upm',hal.HAL_FLOAT)
    #hal.new_sig('spindle-speed',hal.HAL_FLOAT)
    hal.new_sig('tool-number',hal.HAL_U32)

    hal.connect('motion.feed-upm','feedrate-upm')
    hal.connect('axisui.feedrate-upm','feedrate-upm')
    #hal.connect('spindle.0.speed-out','spindle-speed')
    hal.connect('axisui.spindle-speed','spindle-vel-cmd-rpm')
    hal.connect('halui.tool.number','tool-number')
    hal.connect('axisui.tool-number','tool-number')
    
####################################
#########  USER HAL PINS END  #######
####################################




####  repack bottom ####
#W('pack','.pane.bottom.t.program','-side','top','-anchor','nw','-fill','x','-expand','1')

W('pack','.pane.bottom.t.sb','-fill','y','-side','left')
W('pack','.pane.bottom.t.text','-fill','both','-expand','1','-side','top')
W('pack','.pane.bottom.t.text','-fill','both','-expand','1','-side','left')
#W('pack','.pane.bottom.t.program','-before','.pane.bottom.t.text','-side','top','-anchor','nw','-expand','1')

########## resize bottom pane #######
W('.pane','paneconfigure','.pane.bottom',"-stretch","always","-height","350",'-width','500');

#### end new common frame test #####







############################################
#####   REDO HOW MAN AND GCODE BUTTONS   ######
#########  MANUAL AND GCODE BUTTTONS #######
#############################################
W('pack','forget','.toolbar.rule4')
W('pack','forget','.toolbar.rule8')
W('pack','forget','.toolbar.rule9')

W('pack','forget','.toolbar.rule12')
W('frame','.toolbar.spacer4','-borderwidth','1','-height','6','-width','2','-relief','sunken')     
W('frame','.toolbar.spacer8','-borderwidth','1','-height','6','-width','2','-relief','sunken')  
W('frame','.toolbar.spacer9','-borderwidth','1','-height','6','-width','2','-relief','sunken')     
W('frame','.toolbar.spacer12','-borderwidth','1','-height','6','-width','2','-relief','sunken')     
#W('frame','.toolbar.spacer4','-borderwidth','1','-height','6','-width','2','-relief','sunken')     
#W('frame','.toolbar.spacer4','-borderwidth','1','-height','6','-width','2','-relief','sunken')     


toolbar_right = inifile.find('DISPLAY', 'TOOLBAR_RIGHT')

def lower_gcode():
            W('pack','forget','.toolbar.file_open')
            W('pack','forget','.toolbar.reload')
            #W('pack','forget','.toolbar.rule4')
            W('pack','forget','.toolbar.spacer4')
            W('pack','forget','.toolbar.program_run')
            W('pack','forget','.toolbar.program_step')
            W('pack','forget','.toolbar.program_pause')
            W('pack','forget','.toolbar.program_stop')
            #W('pack','forget','.toolbar.rule8')
            W('pack','forget','.toolbar.spacer8')
            W('pack','forget','.toolbar.program_blockdelete')
            W('pack','forget','.toolbar.program_optpause')

def raise_gcode():
            W('pack','.toolbar.file_open','-side','left')
            W('pack','.toolbar.reload','-side','left')
            W('pack','.toolbar.spacer4','-after','.toolbar.reload','-side','left','-fill','y','-padx','4','-pady','4')
            W('pack','.toolbar.program_run','-side','left')
            W('pack','.toolbar.program_pause','-side','left')
            W('pack','.toolbar.program_stop','-side','left')
            W('pack','.toolbar.program_step','-side','left')
            W('pack','.toolbar.spacer8','-side','left','-fill','y','-padx','4','-pady','4')
            W('pack','.toolbar.program_blockdelete','-side','left')
            W('pack','.toolbar.program_optpause','-side','left')
        

def raise_gcode_r():
            W('pack','.toolbar.file_open','-side','left')
            W('pack','.toolbar.reload','-side','left')
            #W('pack','.toolbar.spacer4','-after','.toolbar.reload','-side','left','-fill','y','-padx','4','-pady','4')
            W('pack','.toolbar.program_run','-side','left')
            W('pack','.toolbar.program_pause','-side','left')
            W('pack','.toolbar.program_stop','-side','left')
            W('pack','.toolbar.program_step','-side','left')
            #W('pack','.toolbar.spacer8','-side','left','-fill','y','-padx','4','-pady','4')
            W('pack','.toolbar.program_blockdelete','-side','left')
            W('pack','.toolbar.program_optpause','-side','left')
            
            W('pack','configure','.toolbar.file_open','-side','top','-fill','y')
            W('pack','configure','.toolbar.reload','-side','top','-fill','y')
            W('pack','configure','.toolbar.program_run','-side','top','-fill','y')
            W('pack','configure','.toolbar.program_pause','-side','top','-fill','y')
            W('pack','configure','.toolbar.program_stop','-side','top','-fill','y')
            W('pack','configure','.toolbar.program_step','-side','top','-fill','y')
            W('pack','configure','.toolbar.program_blockdelete','-side','top','-fill','y')
            W('pack','configure','.toolbar.program_optpause','-side','top','-fill','y')


def show_gcode():
            
            W('grid','remove','.pane.bottom.common')
            W('grid','remove','.pane.bottom.program')
            W('grid','forget','.pane.top.tabs')
            W('grid','.pane.top.right','-column','1','-sticky','nesw')
            W('.pane','paneconfigure','.pane.bottom',"-stretch","never",'-hide','0');
            W('grid','.pane.bottom.program','-row','0','-column','1','-sticky','nesw')
            W('grid','.pane.bottom.common','-row','3','-column','1','-sticky','sew')
            W('grid','.pane.bottom.t','-row','1','-column','1','-sticky','nesw')
            if toolbar_right == "YES" :
              raise_gcode_r()
            else :
              raise_gcode()
            
            lower_view()
            o.tkRedraw()
            
            
            
            
         





def show_manual():

            W('grid','.pane.top.tabs','-row','1','-sticky','nsw','-rowspan','10')
            W('.pane','paneconfigure','.pane.bottom',"-stretch","never",'-hide','1');
            o.tkRedraw()
        
 
def raise_view():
            W('pack','.toolbar.view_zoomin','-side','left')
            W('pack','.toolbar.view_zoomout','-side','left')
            W('pack','.toolbar.spacer9','-after','.toolbar.view_zoomout','-side','left','-fill','y','-padx','4','-pady','4')
            W('pack','.toolbar.view_z','-side','left')
            #W('pack','.toolbar.view_z2','-side','left')
            W('pack','.toolbar.view_x','-side','left')
            W('pack','.toolbar.view_y','-side','left')
            W('pack','.toolbar.view_p','-side','left')
            W('pack','.toolbar.rotate','-side','left')
            W('pack','.toolbar.spacer12','-after','.toolbar.rotate','-side','left','-fill','y','-padx','4','-pady','4')
            W('pack','.toolbar.clear_plot','-side','left')
            
       
            
def raise_view_r():
            W('pack','.toolbar.view_zoomin','-side','left')
            W('pack','.toolbar.view_zoomout','-side','left')
            #W('pack','.toolbar.spacer9','-after','.toolbar.view_zoomout','-side','left','-fill','y','-padx','4','-pady','4')
            W('pack','.toolbar.view_z','-side','left')
            #W('pack','.toolbar.view_z2','-side','left')
            W('pack','.toolbar.view_x','-side','left')
            W('pack','.toolbar.view_y','-side','left')
            W('pack','.toolbar.view_p','-side','left')
            W('pack','.toolbar.rotate','-side','left')
            #W('pack','.toolbar.spacer12','-after','.toolbar.rotate','-side','left','-fill','y','-padx','4','-pady','4')
            W('pack','.toolbar.clear_plot','-side','left')
            
            W('pack','configure','.toolbar.view_zoomin','-side','top','-fill','y')
            W('pack','configure','.toolbar.view_zoomout','-side','top','-fill','y')
            W('pack','configure','.toolbar.view_z','-side','top','-fill','y')
            #W('pack','configure','.toolbar.view_z2','-side','top','-fill','y')
            W('pack','configure','.toolbar.view_x','-side','top','-fill','y')
            W('pack','configure','.toolbar.view_y','-side','top','-fill','y')
            W('pack','configure','.toolbar.view_p','-side','top','-fill','y')
            W('pack','configure','.toolbar.rotate','-side','top','-fill','y')
            W('pack','configure','.toolbar.clear_plot','-side','top','-fill','y')
 
def lower_view():
            W('pack','forget','.toolbar.view_zoomin')
            W('pack','forget','.toolbar.view_zoomout')
            W('pack','forget','.toolbar.spacer9')
            W('pack','forget','.toolbar.view_z')
            W('pack','forget','.toolbar.view_z2')
            W('pack','forget','.toolbar.view_x')
            W('pack','forget','.toolbar.view_y')
            W('pack','forget','.toolbar.view_p')
            W('pack','forget','.toolbar.rotate')
            #W('pack','forget','.toolbar.rule12')
            W('pack','forget','.toolbar.spacer12')
            W('pack','forget','.toolbar.clear_plot')
 
def show_view():
            if toolbar_right == "YES" :
              raise_view_r()
            else :
              raise_view()
            
            lower_gcode()
            o.tkRedraw()
            
            


  
  
show_gcode()

          
#######################################################################
#######          END     ###########################
################# MANAUL GCODE BUTTONS   #############
#################################################






def gcode_lower(event):
      W('lower','.pane.bottom')




######### resize top pane ########
###### smaller manual frame /11 lines of gcode ########
W('.pane','paneconfigure','.pane.top',"-stretch","never","-minsize","350");

########### display z plane in preview  ###########
commands.set_view_z()

########### window resize ##################
#W("wm","geometry",".","700x420")
#W("wm","geometry",".","800x480")
#root_window.attributes("-fullscreen",1)

# disable the do you want to close dialog
W("wm","protocol",".","WM_DELETE_WINDOW","destroy .")


###############################
#####  configure sliders #######
##############################

#W('grid','forget','.pane.top.spinoverride.l')
#W('grid','forget','.pane.top.feedoverride.l')
#W('pack','forget','.pane.top.feedoverride.l')
#W('pack','forget','.pane.top.spinoverride.l')
#W('pack','forget','.pane.top.feedoverride.foscale')
#W('pack','forget','.pane.top.spinoverride.foscale')
W('.pane.top.feedoverride.foscale','configure','-resolution','10','-length','170','-width','20')
W('.pane.top.spinoverride.foscale','configure','-resolution','10','-length','170','-width','20','-sliderrelief','flat','-troughcolor','lightgray','-background','darkblue','-foreground','lightgray')
W('.pane.top.rapidoverride.foscale','configure','-resolution','10','-length','170','-width','20')
W('.pane.top.jogspeed.s','configure','-length','170','-width','20','-sliderrelief','flat','-troughcolor','lightgray','-background','darkblue','-foreground','lightgray')

#W('pack','.pane.top.feedoverride.foscale','-expand','1')
#W('pack','.pane.top.spinoverride.foscale','-expand','1')


##########################
#########  config scroll bars  #########
################################

W('.pane.bottom.t.sb','configure','-relief','flat','-troughcolor','grey30','-background','grey90','-width','20','-elementborderwidth','1')


####################
# change the font
##################
font = 'mono 12'
fname,fsize = font.split()
W('font','configure','TkDefaultFont','-family',fname,'-size',fsize)

# redo the text in tabs so they resize for the new default font

W('.pane.top.tabs','itemconfigure','manual','-text',' MANUAL ')
W('.pane.top.tabs','itemconfigure','mdi','-text',' MDI ')
W('.pane.top.right','itemconfigure','preview','-text',' PREVIEW ')
W('.pane.top.right','itemconfigure','numbers','-text',' DRO ')


# change dro screen
W('.pane.top.right.fnumbers.text','configure','-foreground','limegreen','-background','black')



#modal text
W('.pane.top.gcodes','configure'
,'-foreground','slategray1'
,"-font","mono 12 bold"
,"-height","2"
,'-background','gray30'
,"-relief","flat")


#gcode preview text
W(pane_bottom+".t.text","configure"
,"-foreground","gray90"
,"-font","mono 12 "
,"-background","gray30"
,"-wrap","word"
,"-relief","flat")


######################################################################
##################     TOOLBAR  BUTTON   CONFIG    ####################
################################################################

W('image','create','photo','tool_power2','-file','image/tool_power2.gif')
W('image','create','photo','tool_open2','-file','image/tool_open2.gif')
W('image','create','photo','tool_reload2','-file','image/tool_reload2.gif')


if toolbar_right == "YES" :
        #estop button
    W(".toolbar.machine_estop","configure"
    #,"-text","ESTOP"
    ,"-activebackground","tomato2"
    #,"-background","red"
    ,"-helptext","Toggle Emergency Stop [F1]"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","26"
    ,"-relief","flat"
    );

    #powerbutton
    W(".toolbar.machine_power","configure"
    #,"-text","POWER"
    ,"-image","tool_power2"
    ,"-activebackground","palegreen3"
    ,"-helptext","Toggle Machine power [F2]"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","27"
    ,"-relief","flat"
    );


    #  open file
    W(".toolbar.file_open","configure"
    #,"-text","OPEN"
    ,"-image","tool_open2"
    ,"-activebackground","grey90"
    ,"-helptext","OPEN FILE"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","26"
    ,"-relief","flat"
    );


    #  reload
    W(".toolbar.reload","configure"
    #,"-text","RELOAD"
    ,"-image","tool_reload2"
    ,"-activebackground","palegreen1"
    ,"-helptext","RELOAD FILE"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","26"
    ,"-relief","flat"
    );

    #  run
    W(".toolbar.program_run","configure"
    #,"-text","RUN"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","RUN"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","20"
    ,"-relief","flat"
    );

    #  STEp
    W(".toolbar.program_step","configure"
    #,"-text","STEP"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","STEP"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","20"
    );

    #  Pause
    W(".toolbar.program_pause","configure"
    #,"-text","PAUSE"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","PAUSE"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","20"
    );

    #  stop
    W(".toolbar.program_stop","configure"
    #,"-text","STOP"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","ST0P"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","20"
    ,"-relief","flat"
    );


    #  block skip
    W(".toolbar.program_blockdelete","configure"
    #,"-text","SKIP"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","BLOCK SKIP"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","18"
    );

    #  m1 optional
    W(".toolbar.program_optpause","configure"
    #,"-text","M1"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","M1 OPTIONAL STOP"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","20"
    ,"-relief","solid"
    );

    #  zoom+
    W(".toolbar.view_zoomin","configure"
    #,"-text","ZOOM"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","ZOOM IN"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","26"
    ,"-relief","flat"
    );

    #  zoom - 
    W(".toolbar.view_zoomout","configure"
    #,"-text","ZOOM"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","ZOOM OUT"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","26 "
    ,"-relief","flat"
    );

    #  view z
    W(".toolbar.view_z","configure"
    #,"-text","VIEW"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","VIEW Z"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","20"
    ,"-relief","flat"
    );

    W('pack','forget','.toolbar.view_z2')

    #  view x
    W(".toolbar.view_x","configure"
    #,"-text","VIEW"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","VIEW X"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","20"
    ,"-relief","flat"
    );


    #  view y
    W(".toolbar.view_y","configure"
    #,"-text","VIEW"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","VIEW Y"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","20"
    ,"-relief","flat"
    );


    #  view p
    W(".toolbar.view_p","configure"
    #,"-text","VIEW"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","VIEW P"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","20"
    ,"-relief","flat"
    );

    #  view rotate
    W(".toolbar.rotate","configure"
    #,"-text","ROTATE"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","TOGGLE ROTATE"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","1"
    ,"-width","26"
    );

    #   clear plot
    W(".toolbar.clear_plot","configure"
    #,"-text","CLEAR"
    ,"-activebackground","lightsteelblue1"
    ,"-helptext","CLEAR PLOT"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","20"
    ,"-relief","flat"
    );
    
    # toolbar manual
    W(".toolbar.man","configure"
    ,"-image","tool_man"
    #,"-text","MANUAL"
    ,"-activebackground","grey90"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","28"
    ,"-relief","flat"
    );




    # toolbar gcode 
    W(".toolbar.gcode","configure"
    ,"-image","tool_gcode"
    #,"-text","GCODE"
    ,"-activebackground","grey90"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","26"
    ,"-relief","flat"
    );


    # toolbar view
    W(".toolbar.view","configure"
    ,"-image","tool_preview"
    #,"-text","PREVIEW"
    ,"-activebackground","lightsteelblue1"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","32"
    ,"-relief","solid"
    );



    # toolbar HOME all
    W(".toolbar.home","configure"
    ,"-image","tool_home"
    #,"-text","HOME"
    ,"-activebackground","lightsteelblue1"
    ,"-font","mono 10 "
    ,"-compound","top"
    ,"-borderwidth","0"
    ,"-width","26"
    ,"-relief","solid"
    );
    W('grid','.toolbar','-row','1','-column','6','-sticky','nsew')
    W('grid','forget','.pane.bottom.common') 

########### move/choose toolbar buttons  to righjt ################
    W('pack','configure','.toolbar.machine_estop','-side','top','-fill','y')
    W('pack','configure','.toolbar.machine_power','-side','top','-fill','y')
    W('pack','configure','.toolbar.file_open','-side','top','-fill','y')
    W('pack','configure','.toolbar.reload','-side','top','-fill','y')
    W('pack','configure','.toolbar.program_run','-side','top','-fill','y')
    W('pack','configure','.toolbar.program_pause','-side','top','-fill','y')
    W('pack','configure','.toolbar.program_stop','-side','top','-fill','y')
    W('pack','configure','.toolbar.program_step','-side','top','-fill','y')
    W('pack','configure','.toolbar.program_blockdelete','-side','top','-fill','y')
    W('pack','configure','.toolbar.program_optpause','-side','top','-fill','y')
    #W('pack','configure','.toolbar.view_zoomin','-side','top','-fill','y')
    #W('pack','configure','.toolbar.view_zoomout','-side','top','-fill','y')
    #W('pack','configure','.toolbar.view_z','-side','top','-fill','y')
    #W('pack','configure','.toolbar.view_z2','-side','top','-fill','y')
    #W('pack','configure','.toolbar.view_x','-side','top','-fill','y')
    #W('pack','configure','.toolbar.view_y','-side','top','-fill','y')
    #W('pack','configure','.toolbar.view_p','-side','top','-fill','y')
    #W('pack','configure','.toolbar.rotate','-side','top','-fill','y')
    #W('pack','configure','.toolbar.clear_plot','-side','top','-fill','y')
    W('pack','configure','.toolbar.man','-side','top','-fill','y')
    W('pack','configure','.toolbar.gcode','-side','top','-fill','y')
    W('pack','configure','.toolbar.view','-side','top','-fill','y')
    W('pack','configure','.toolbar.home','-side','top','-fill','y')
    W('pack','forget','.toolbar.rule0')
    W('pack','forget','.toolbar.rule2')
    W('pack','forget','.toolbar.rule4')
    W('pack','forget','.toolbar.rule8')
    W('pack','forget','.toolbar.rule9')
    W('pack','forget','.toolbar.rule12')
    W('pack','forget','.toolbar.rule16')
    W('pack','forget','.toolbar.spacer2')
    
    ## rewsize the panes for 800x480
    #W('.pane','paneconfigure','.pane.bottom',"-stretch","always","-height","350",'-width','350');
    W('.pane','configure','-orient','h','-width','750');
    W('.pane.top.tabs','configure','-width','300','-arcradius','8','-font','mono 12','-tabpady','1''1')
    W('.pane.top.right','configure','-width','350','-arcradius','8','-font','mono 12','-tabpady','1''1')
    W('.pane','paneconfigure','.pane.bottom',"-stretch","never",'-width','300','-before','.pane.top');
    W('grid','forget','.pane.top.gcodel')
    W('grid','forget','.pane.top.rapidoverride')
    W('grid','forget','.pane.top.jogspeed')
    ##### remove info  #######
    W('grid','forget','.info')


else:
    
      #estop button
      W(".toolbar.machine_estop","configure"
      ,"-text","ESTOP"
      ,"-activebackground","tomato2"
      #,"-background","red"
      ,"-helptext","Toggle Emergency Stop [F1]"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","26"
      ,"-relief","flat"
      );

      #powerbutton
      W(".toolbar.machine_power","configure"
      ,"-text","POWER"
      ,"-image","tool_power2"
      ,"-activebackground","palegreen3"
      ,"-helptext","Toggle Machine power [F2]"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","27"
      ,"-relief","flat"
      );


      #  open file
      W(".toolbar.file_open","configure"
      ,"-text","OPEN"
      ,"-image","tool_open2"
      ,"-activebackground","grey90"
      ,"-helptext","OPEN FILE"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","26"
      ,"-relief","flat"
      );


      #  reload
      W(".toolbar.reload","configure"
      ,"-text","RELOAD"
      ,"-image","tool_reload2"
      ,"-activebackground","palegreen1"
      ,"-helptext","RELOAD FILE"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","26"
      ,"-relief","flat"
      );

      #  run
      W(".toolbar.program_run","configure"
      ,"-text","RUN"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","RUN"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","20"
      ,"-relief","flat"
      );

      #  STEp
      W(".toolbar.program_step","configure"
      ,"-text","STEP"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","STEP"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","20"
      );

      #  Pause
      W(".toolbar.program_pause","configure"
      ,"-text","PAUSE"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","PAUSE"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","20"
      );

      #  stop
      W(".toolbar.program_stop","configure"
      ,"-text","STOP"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","ST0P"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","20"
      ,"-relief","flat"
      );


      #  block skip
      W(".toolbar.program_blockdelete","configure"
      ,"-text","SKIP"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","BLOCK SKIP"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","18"
      );

      #  m1 optional
      W(".toolbar.program_optpause","configure"
      ,"-text","M1"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","M1 OPTIONAL STOP"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","20"
      ,"-relief","solid"
      );

      #  zoom+
      W(".toolbar.view_zoomin","configure"
      ,"-text","ZOOM"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","ZOOM IN"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","26"
      ,"-relief","flat"
      );

      #  zoom - 
      W(".toolbar.view_zoomout","configure"
      ,"-text","ZOOM"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","ZOOM OUT"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","26 "
      ,"-relief","flat"
      );

      #  view z
      W(".toolbar.view_z","configure"
      ,"-text","VIEW"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","VIEW Z"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","20"
      ,"-relief","flat"
      );

      W('pack','forget','.toolbar.view_z2')

      #  view x
      W(".toolbar.view_x","configure"
      ,"-text","VIEW"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","VIEW X"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","20"
      ,"-relief","flat"
      );


      #  view y
      W(".toolbar.view_y","configure"
      ,"-text","VIEW"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","VIEW Y"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","20"
      ,"-relief","flat"
      );


      #  view p
      W(".toolbar.view_p","configure"
      ,"-text","VIEW"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","VIEW P"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","20"
      ,"-relief","flat"
      );

      #  view rotate
      W(".toolbar.rotate","configure"
      ,"-text","ROTATE"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","TOGGLE ROTATE"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","1"
      ,"-width","26"
      );

      #   clear plot
      W(".toolbar.clear_plot","configure"
      ,"-text","CLEAR"
      ,"-activebackground","lightsteelblue1"
      ,"-helptext","CLEAR PLOT"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","20"
      ,"-relief","flat"
      );

      # toolbar manual
      W(".toolbar.man","configure"
      ,"-image","tool_man"
      ,"-text","MANUAL"
      ,"-activebackground","grey90"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","28"
      ,"-relief","flat"
      );




      # toolbar gcode 
      W(".toolbar.gcode","configure"
      ,"-image","tool_gcode"
      ,"-text","GCODE"
      ,"-activebackground","grey90"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","26"
      ,"-relief","flat"
      );


      # toolbar view
      W(".toolbar.view","configure"
      ,"-image","tool_preview"
      ,"-text","PREVIEW"
      ,"-activebackground","lightsteelblue1"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","32"
      ,"-relief","solid"
      );



      # toolbar HOME all
      W(".toolbar.home","configure"
      ,"-image","tool_home"
      ,"-text","HOME"
      ,"-activebackground","lightsteelblue1"
      ,"-font","mono 10 "
      ,"-compound","top"
      ,"-borderwidth","0"
      ,"-width","26"
      ,"-relief","solid"
      );
     ### stuff from new_layout
      W('.pane.top.right','configure','-width','580','-arcradius','8','-font','mono 12','-tabpady','1''1')
      W('.pane','paneconfigure','.pane.bottom',"-stretch","never",'-width','450','-before','.pane.top');
      W('.pane','configure','-orient','h','-width','1000');
      W('grid','.pane.top.gcodel','-sticky','nesw','-column','1')
      W('grid','.pane.top.rapidoverride','-sticky','nesw','-column','1')

      
      

####  move toolbar to bottom   
#W('grid','.toolbar','-row','3','-sticky','nsew')

  ###  or left, but need destroy repact





########################
#### new layout ########
#########################

W('grid','remove','.pane.bottom.common')
W('grid','remove','.pane.bottom.program')

W('grid','forget','.pane.top.tabs')
W('grid','remove','.pane.top.right')
W('grid','forget','.pane.top.feedoverride')
#W('grid','forget','.pane.top.rapidoverride')
W('grid','forget','.pane.top.spinoverride')
W('grid','forget','.pane.top.jogspeed')
W('grid','forget','.pane.top.ajogspeed')
W('grid','forget','.pane.top.maxvel')
            ############ remove active gcodes lable and codes ##########
W('grid','remove','.pane.top.gcodel')
W('grid','forget','.pane.top.gcodes')
W('grid','.pane.top.right','-column','1','-sticky','nesw')
W('grid','.pane.top.feedoverride','-sticky','nesw','-column','1')
W('grid','.pane.top.spinoverride','-sticky','nesw','-column','1')
W('grid','.pane.top.jogspeed','-sticky','nesw','-column','1')
W('grid','.pane.top.ajogspeed','-sticky','nesw','-column','1')
W('grid','.pane.top.maxvel','-sticky','nesw','-column','1')
            ############ remove active gcodes lable and codes ##########
W('grid','.pane.top.gcodes','-column','1','-sticky','new')
W('.pane','paneconfigure','.pane.top',"-stretch","always");
W('grid','.pane.bottom.program','-row','0','-column','1','-sticky','nesw')
W('grid','.pane.bottom.common','-row','3','-column','1','-sticky','sew')
W('grid','.pane.bottom.t','-row','1','-column','1','-sticky','nesw')
#W('grid','forget','.pane.top.feedoverride')
#W('grid','forget','.pane.top.rapidoverride')
#W('grid','forget','.pane.top.spinoverride')
#W('grid','forget','.pane.top.jogspeed')
W('grid','forget','.pane.top.ajogspeed')
W('grid','forget','.pane.top.maxvel')

#####  moved to iniconfig 
#W('.pane.top.right','configure','-width','580','-arcradius','8','-font','mono 12','-tabpady','1''1')
#W('.pane','paneconfigure','.pane.bottom',"-stretch","never",'-width','450','-before','.pane.top');
#W('.pane','configure','-orient','h','-width','1000');
#W('grid','.pane.top.gcodel','-sticky','nesw','-column','1')
#W('grid','.pane.top.rapidoverride','-sticky','nesw','-column','1')

########################
#### new layout ########
#########################





root_window.bind('<Key-1>', select_next)
root_window.bind('<Key-2>', select_prev)
root_window.bind('<Key-3>', rClicker)



TclCommands.select_prev_line = select_prev_line
TclCommands.select_next_line = select_next_line
TclCommands.show_manual = show_manual
TclCommands.show_gcode = show_gcode
TclCommands.show_view = show_view
TclCommands.lower_view = lower_view
TclCommands.raise_view = raise_view
TclCommands.lower_gcode = lower_gcode
TclCommands.raise_gcode = raise_gcode
TclCommands.raise_gcode_r = raise_gcode_r
TclCommands.raise_view_r = raise_view_r


TclCommands.search_next = search_next
TclCommands.search_gcode = search_gcode

TclCommands.set_rapid10 = set_rapid10
TclCommands.set_rapid25 = set_rapid25
TclCommands.set_rapid50 = set_rapid50
TclCommands.set_rapid100 = set_rapid100


TclCommands.backplot = backplot
TclCommands.gcode_plotblue = gcode_plotblue
TclCommands.gcode_plotred = gcode_plotred



TclCommands.gcode_lower = gcode_lower
TclCommands.hide_gcode = hide_gcode
TclCommands.hide_sliders = hide_sliders


commands = TclCommands(root_window)
#search_command = StringVar()


