#!/usr/bin/env python
"""
Footprint PCB layouting program
Copyright (C) 2012 Danny Milosavljevic
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import metafile

import Tkinter
from Tkconstants import *
tk = Tkinter.Tk()
#    frame = Tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
#    frame.pack(fill=BOTH,expand=1)
#    label = Tkinter.Label(frame, text="Hello, World")
#    label.pack(fill=X, expand=1)
#    button = Tkinter.Button(frame,text="Exit",command=tk.destroy)
#    button.pack(side=BOTTOM)
#    tk.mainloop()

#tk.bind("<Control-c>", finish)

class ViewAdapter(Tkinter.PanedWindow):
	def __init__(self, parent, view):
		Tkinter.PanedWindow.__init__(self, parent, relief = FLAT, borderwidth=2, showhandle = True, cursor = "cross")
		self.view = view
		def adaptRedraw(widget, event, self=self):
			# TODO check and pass event area
			area = []
			self.repaint(area)
		def adaptButtonPress(widget, event):
			coo = event.x, event.y
			return self.view.handleEvent("mouseButtonPress", coo)	
		def adaptButtonRelease(widget, event):
			coo = event.x, event.y	
			return self.view.handleEvent("mouseButtonRelease", coo)
		#self.connect("expose-event", adaptRedraw)
		#self.connect("button-press-event", adaptButtonPress)
		#self.connect("button-release-event", adaptButtonRelease)
		#self.add_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK)
		self.pack()
	def repaint(self, area):
		view = self.view
		for operation in view.repaint(area):
			operator = metafile.getOperator(operation)
			operands = metafile.getOperands(operation)
			print(operator)
			print(operands)

class WorkbenchAdapter(Tkinter.Frame):
	def __init__(self, workbench):
		Tkinter.Frame.__init__(self, tk, relief=RIDGE, borderwidth=2)
		self.windowDeleted = []
		self.view = ViewAdapter(self, workbench.canvasView)
		#self.view.pack(fill = BOTH, expand = 1)
		self.pack()
		self.workbench = workbench
		workbench.toolbox.items.monitors.cons(self.handleToolboxChange)
		self.show()
	def handleToolboxChange(self, *args):
		menu = gtk.Menu()
		for item in self.workbench.toolbox.items:
			menuItem = gtk.MenuItem(item.name or "???")
			menuItem.show()
			menu.append(menuItem)
                self.toolMenuI.set_submenu(menu)
	def handleDeleteEvent(self):
		return any([callback() for callback in self.windowDeleted])

def run(args):
	tk.mainloop()
	tk.destroy()

