W = root_window.tk.call

#####  search function  #########

def select_g3(event):  
      
    # remove tag 'found' from index 1 to END  
    t.tag_remove('found', '1.0', END)  
      
    # returns to widget currently in focus  
    s = "3"
      
    if (s):  
        idx = '1.0'
        while 1:  
            # searches for desried string from index 1  
            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 red 
          
        t.tag_config('found', foreground ='red') 
    






######  gcode scroll ######
root_window.bind('<Key-1>', select_next)
root_window.bind('<Key-2>', select_prev)
root_window.bind('<Key-3>', rClicker)
root_window.bind('<Key-4>', select_g3)



# change the font

font = 'mono 12'
fname,fsize = font.split()
root_window.tk.call('font','configure','TkDefaultFont','-family',fname,'-size',fsize)

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

root_window.tk.call('.pane.top.tabs','itemconfigure','manual','-text',' Manual - F3 ')
root_window.tk.call('.pane.top.tabs','itemconfigure','mdi','-text',' MDI - F5 ')
root_window.tk.call('.pane.top.right','itemconfigure','preview','-text',' Preview ')
root_window.tk.call('.pane.top.right','itemconfigure','numbers','-text',' DRO ')

###############  choose/remove sliders ########################
root_window.tk.call('grid','forget','.pane.top.feedoverride')
root_window.tk.call('grid','forget','.pane.top.rapidoverride')
root_window.tk.call('grid','forget','.pane.top.spinoverride')
root_window.tk.call('grid','forget','.pane.top.jogspeed')
root_window.tk.call('grid','forget','.pane.top.ajogspeed')
root_window.tk.call('grid','forget','.pane.top.maxvel')
############ remove active gcodes lable and codes ##########
root_window.tk.call('grid','forget','.pane.top.gcodel')
root_window.tk.call('grid','forget','.pane.top.gcodes')

root_window.tk.call('pack','forget','.pane.bottom')

#root_window.tk.call('destroy','.pane.bottom.t')
root_window.tk.call('grid','forget','.pane.top.tabs')
root_window.tk.call('grid','forget','.pane.top')
root_window.tk.call('grid','forget','.pane.bottom')

## -remove bottom frame -##
root_window.tk.call('pack','forget','.pane.bottom.t.text')
root_window.tk.call('pack','forget','.pane.bottom.t.sb')
#root_window.tk.call('grid','.pane.bottom','-column','0','-row','1','-sticky','nesw')

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

###add common frame beside t.test
root_window.tk.call('labelframe','.pane.bottom.common')
root_window.tk.call('grid','.pane.bottom.common','-column','0','-row','1','-sticky','nw')
root_window.tk.call('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')
W('grid','.pane.bottom.common.ucs','-column','0','-row','0','-sticky','w')
 
#### add feedrate lable and grid into bottom.common ####################
W('label','.pane.bottom.common.feedrate-upm','-anchor','se','-width','5','-fg','blue')
W('label','.pane.bottom.common.feedrate-upmlab','-text','FEED','-anchor','w','-width','4')
W('grid','.pane.bottom.common.feedrate-upm','-row','1','-column','1','-rowspan','1','-sticky','se')
W('grid','.pane.bottom.common.feedrate-upmlab','-row','1','-column','0','-sticky','w')
##spindle-speed
W('label','.pane.bottom.common.spindle-speed','-anchor','se','-width','5','-fg','blue')
W('label','.pane.bottom.common.spindle-speedlab','-text','S','-anchor','w','-width','4')
W('grid','.pane.bottom.common.spindle-speed','-row','2','-column','1','-rowspan','1','-sticky','se')
W('grid','.pane.bottom.common.spindle-speedlab','-row','2','-column','0','-sticky','w')
## tool number
W('label','.pane.bottom.common.tool-number','-anchor','se','-width','5','-fg','blue')
W('label','.pane.bottom.common.tool-numberlab','-text','T','-anchor','w','-width','4')
W('grid','.pane.bottom.common.tool-number','-row','3','-column','1','-rowspan','1','-sticky','se')
W('grid','.pane.bottom.common.tool-numberlab','-row','3','-column','0','-sticky','w')


####### update feedrate
def user_live_update():
    root_window.tk.call('.pane.bottom.common.feedrate-upm','configure','-text','%3.0f' % (comp['feedrate-upm']))
    root_window.tk.call('.pane.bottom.common.spindle-speed','configure','-text','%3.0f' % (comp['spindle-speed']))
    root_window.tk.call('.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])


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')
##### end feed test




####  repack bottom ####
root_window.tk.call('pack','.pane.bottom.t.sb','-fill','y','-side','left')
root_window.tk.call('pack','.pane.bottom.t.text','-fill','both','-expand','1','-side','top')
root_window.tk.call('pack','.pane.bottom.t.text','-fill','both','-expand','1','-side','left')
########## resize bottom pane #######
root_window.tk.call('.pane','paneconfigure','.pane.bottom',"-stretch","always","-height","400",'-width','700');

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




######### resize top pane ########
###### smaller manual frame /11 lines of gcode ########
root_window.tk.call('.pane','paneconfigure','.pane.top',"-stretch","never","-minsize","250");
##### bigger manual frame /7 lines of gcode #####
#root_window.tk.call('.pane','paneconfigure','.pane.top',"-stretch","never","-minsize","300");

########### window resize #########
#root_window.tk.call(".pane","configure","-height","350");

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

########### window resize ##################
#root_window.tk.call("wm","geometry",".","700x440")
#root_window.tk.call("wm","geometry",".","800x480")
#root_window.attributes("-fullscreen",1)





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

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


#modal text
root_window.tk.call('.pane.top.gcodes','configure'
,'-foreground','limegreen'
,"-font","mono 11 bold"
,'-background','black')


#gcode preview text
root_window.tk.call(pane_bottom+".t.text","configure"
,"-foreground","Limegreen"
,"-font","mono 11 "
,"-background","black")

#estop button
root_window.tk.call(".toolbar.machine_estop","configure"
,"-image",""
,"-text","ESTOP"
,"-activebackground","red"
,"-background","red"
,"-helptext","Toggle Emergency Stop [F1]"
,"-borderwidth","5"
,"-font","fixedsys 12 bold"
,"-height","2"
,"-width","6"
,"-highlightthickness", 1
,"-highlightcolor","black"
,"-highlightbackground","black"
,"-takefocus",0
,"-padx",5
,"-pady",5
);

#powerbutton
root_window.tk.call(".toolbar.machine_power","configure"
,"-image",""
,"-text","POWER"
,"-activebackground","green"
,"-background","green"
,"-helptext","Toggle Machine power [F2]"
,"-borderwidth","5"
,"-font","fixedsys 12 bold"
,"-height","2"
,"-width","6"
,"-highlightthickness", 1
,"-highlightcolor","black"
,"-highlightbackground","black"
,"-takefocus",0
,"-padx",5
,"-pady",5
);




