from Plotter import *
from Command import *
from Program import *
import Tkinter as tk
import tkMessageBox
import tkFileDialog
from hpglView import HPGLView
import os.path


class App:
	def __init__(self):
		self.programs=[]
		self.filename=None
		self.window=tk.Tk()
		self.window.title('Mimaki 0.1')
		self.create_menu()
		self.create_lb()
		self.view=HPGLView(self.window)
		self.view.pack(side=tk.RIGHT,expand=True,fill=tk.BOTH)
		self.view.programs=self.programs
		
	def create_menu(self):
		menu=tk.Menu(self.window)
		self.window.config(menu=menu)
		filemenu=tk.Menu(menu)
		filemenu.add_command(label="Open",command=self.file_open)
		filemenu.add_command(label="Save",command=self.file_save)
		filemenu.add_command(label="Save as",command=self.file_saveas)
		menu.add_cascade(label="File", menu=filemenu)
		editmenu=tk.Menu(menu)
		editmenu.add_command(label="Scale",command=self.edit_scale)
		editmenu.add_command(label="Flip",command=self.edit_flip)
		editmenu.add_command(label="Move",command=self.edit_move)
		menu.add_cascade(label="Edit", menu=editmenu)
		plotmenu=tk.Menu(menu)
		plotmenu.add_command(label="Write",command=self.plot_write)
		plotmenu.add_command(label="Centralize",command=self.plot_center)
		plotmenu.add_command(label="Full",command=self.plot_full)
		menu.add_cascade(label="Plot", menu=plotmenu)
		
	def plot_write():
		pass
	
	def plot_center():
		pass
	
	def plot_full():
		pass
		
	def create_lb(self):
		listFrame = tk.Frame(self.window)
		self.listBox = tk.Listbox(listFrame)
		self.listBox.pack(expand=True,fill=tk.BOTH)
		listFrame.pack(side=tk.LEFT,fill=tk.Y)
		scrollBar = tk.Scrollbar(listFrame)
		self.listBox.config(yscrollcommand=scrollBar.set)
		
	
	def run(self):
		self.window.mainloop()
		
	def edit_scale(self):
		pass
		
	
	def edit_flip(self):
		for element in self.programs:
			if element.active:
				element= element.flip()
		
	def edit_move(self):
		pass
		
	def file_open(self):
		filename = tkFileDialog.askopenfilename()
		if filename:
			p = Program.parsefile(filename)
			self.programs.append(p)
			self.filename = filename
			self.listBox.insert(tk.END, os.path.basename(filename))
		
	def file_save(self):
		if self.program:
			if self.filename:
				self.save()
			else:
				self.file_saveas()
		else:
			tkMessageBox.showwarning("No!")
		
	def file_saveas(self):
		filename = tkFileDialog.asksaveasfilename()
		if filename:
			self.filename = filename
			self.save()
		
	def save(self):
		if self.program:
			with open(self.filename,"w") as file:
				file.write(str(self.program))
		else:
			tkMessageBox.showwarning("Nope!")
		
app=App()
app.run()

