#!/usr/bin/env /home/beams/BCDA/bin/python
import tkFileDialog
import sys
import os
import string
from Tkinter import *

class pyscan:

    def dialog1D(self):
	t1 = Toplevel(self.root)
	t1.title("yscan 1D")
	filename = StringVar()
	Entry(t1,width=60,textvariable=filename).pack(side=BOTTOM)
	Label(t1,text="Yorick 1D scan file:").pack(side=BOTTOM,padx=5,pady=10)
	Button(t1,text='File...',command=pyscan.pickfile).pack(side=LEFT)
	Button(t1,text='Append',command=pyscan.scan1D).pack(side=LEFT)
	Button(t1,text='New',command=pyscan.scan1D_new).pack(side=LEFT)
	Button(t1,text='Close',command=t1.destroy).pack(side=LEFT)
	filename.set(self.fname)
	self.fname_W = filename

    def pickfile(self):
	fname = tkFileDialog.askopenfilename(initialdir=self.path)
	if string.strip(fname) != "":
		self.fname_W.set(fname)
	(path,file) = os.path.split(fname)
	self.fname = fname
	self.path = path
	self.file = file

    def scan1D_new(self):
	fname = self.fname_W.get()
	st = [ "catcher_scanHeader\n",
		"catcher_1dScan,\"" + fname +"\",new=1\n",
		"quit\n"]
	g = open("y.scan1D","w")
	g.writelines(st)
	g.close()
	if os.path.isfile(fname) == 0:
	  f = open(fname,'w')
	  f.close()
	  os.system("yca < y.scan1D ")
	
    def scan1D(self):
          fname = self.fname_W.get()
	  self.fname = fname
	  st = ["catcher1d_ptr,\""+fname+"\",ptr\n" ,
		"catcher1d_data,\""+ fname +"\",ptr(numberof(ptr)-1)\n",
		"catcher_scanHeader\n",
		"catcher_1dScan,\"" + fname +"\"\n",
		"quit\n"]
	  f = open("y.scan1D","w")
	  f.writelines(st)
	  f.close()
	  if os.path.isfile(fname) == 1:
		os.system("yca < y.scan1D ")


    def __init__(self,path):
	self.path = path
	self.file = 'junk0.yo'
	self.file_sep = '/'
	self.fname = self.path +self.file_sep+ self.file


def makeScanMenu():
    ScanBtn = Menubutton(mBar,text='Scan',underline=0)
    ScanBtn.pack(side=LEFT,padx="2m")
    ScanBtn.menu = Menu(ScanBtn)
    ScanBtn.menu.add_command(label='Catcher 1D',underline=0,command=pyscan.dialog1D)
    ScanBtn['menu'] = ScanBtn.menu
    return ScanBtn

	
#main program here

path = os.getcwd()
pyscan = pyscan(path)

root = Tk()
root.title("yscan 1D")
mBar = Frame(root,relief=RAISED,borderwidth=2)
mBar.pack(fill=X)
ScanBtn = makeScanMenu()
mBar.tk_menuBar(ScanBtn)

pyscan.root = root


root.mainloop()
