#common funcs to all mods

from spdirvars import *
from Tkinter import *
import tkFileDialog
import tkMessageBox
import Pmw
import os
from math import *
from string import *

version='0.64'

#file dialog
def ask_for_file(defaults,dir,check=1):
    f = ''
    verbot=['(',')','-','#','&','$','@','!','{','}','+','=']
    if dir=='':
        f = tkFileDialog.askopenfilename(filetypes=defaults)
    else:
        f = tkFileDialog.askopenfilename(filetypes=defaults,initialdir=dir)        
    bad=0
    for char in f:
        if char in verbot: bad=1
    if bad and check:
        tkMessageBox.showwarning("Open File"," WARNING!\n Filename contains characters not\n compatible with IFEFFIT ")
    return f

def ask_save_file(fn,dir):
    f=''
    if dir=='':
        f = tkFileDialog.asksaveasfilename(initialfile=fn)
    else:
        f = tkFileDialog.asksaveasfilename(initialfile=fn,initialdir=dir)        
    return f

def trimdir(f):
    firstind=rfind(f,os.sep)+1
    cf=f[firstind:]
    #cf=replace(cf,'.','_')
    return cf

def trimdirext(f):
    firstind=rfind(f,os.sep)+1
    lastind=rfind(f,'.')
    cf=f[firstind:lastind]
    return cf

#entry replace
def entry_replace(ent, val):
    ent.delete(0,len(ent.get()))
    ent.insert(0,val)

def entry_replace_d(ent, val,dig):
    ent.delete(0,len(ent.get()))
    val=str(val)
    decind=rfind(val,'.')
    eind=rfind(val,"e")
    if eind==-1:
        val=val[:decind+dig]
    else:
        val=val[:4]+val[eind:len(val)]
    ent.insert(0,val)

def valueclip_d(val,dig):
    val=str(val)
    decind=rfind(val,'.')
    eind=rfind(val,"e")
    if eind==-1:
        val=val[:decind+dig]
    else:
        val=val[:4]+val[eind:len(val)]
    return val

#Load filename into an entry
def fileget(master,ent,check=1):
    global lastreaddir
    verbot=['(',')','-','#','&','$','@','!','{','}','+','=']
    fn=ask_for_file([("data files","*.dat *.chi"),("edg files","*.nor *.xmu *.mu"),("all files","*")],lastreaddir)
    if fn!='':
        sdir=rfind(fn,os.sep)
        lastreaddir=fn[:sdir]
        bad=0
        for char in fn:
            if char in verbot: bad=1
        if bad and check:
            tkMessageBox.showwarning("Open File"," WARNING!\n Filename contains characters not\n compatible with IFEFFIT ")
    entry_replace(ent,fn)
    master.focus_set()
    
#functions for updating status bars
def setstatus(n,format):
    n.config(text=format)
    n.update_idletasks()
    
def setstatus_d(n,val,dig):
    val=str(val)
    decind=rfind(val,'.')
    eind=rfind(val,"e")
    if eind==-1:
        val=val[:decind+dig]
    else:
        val=val[:4]+val[eind:len(val)]
    n.config(text=val)
    n.update_idletasks()

def clearstatus(n):
    n.config(text=" ")
    n.update_idletasks()

def programabout(root):
    Pmw.aboutversion(version)
    Pmw.aboutcopyright('Copyright Samuel Webb, 2006\nStanford Synchrotron Radiation Laboratory')
    Pmw.aboutcontact("""SixPACK is Sam's Interface for XAS Package
Unifying SamView with SamXAS

email: swebb@ssrl.slac.stanford.edu
web: http://www.stanford.edu/~swebb

Reference:
Webb S.M. (2005)  "Sixpack: A graphical user interface for XAS analysis using IFEFFIT."
Physica Scripta, T115, 1011-1014. """)
    about=Pmw.AboutDialog(root,applicationname='SixPACK')
