view editgraph.py @ 75:0083b2f7b3c7

Partially working implementation of List. Modified build scripts to allow use of other compilers. Fixed some bugs involving method implementations on different types returning different numbers of outputs. Added Fold to the 'builtins' in the comipler.
author Mike Pavone <pavone@retrodev.com>
date Tue, 06 Jul 2010 07:52:59 -0400
parents 0534bb8ee7ad
children
line wrap: on
line source

import Tkinter
from tkFont import Font

class Call(object):
	def __init__(self, canvas, center, text = None, chars=None, padw = 20, padh = 4, fill="gray", outline="black",selectoutline="red"):
		x,y = center
		self.x = x
		self.y = y
		self.text = Tkinter.StringVar()
		self.textlen = 0
		if text != None:
			self.text.set(text)
			self.textlen = len(text)
			if chars == None:
				chars = len(text)+3
		elif chars == None:
			chars = 3
		self.textwidget = Tkinter.Entry(canvas, width=chars, textvariable=self.text, font=Font(family="Courier New", size=10))
		self.textwidget.bind('<Key>', self.textchanged, '+')
		self.canvastext = canvas.create_window(x, y, window=self.textwidget)
		box = canvas.bbox(self.canvastext)
		self.width = box[2]-box[0]+padw
		self.height = box[3]-box[1]+padh
		hw = self.width / 2
		hh = self.height / 2
		ow = hw+10
		self.poly = canvas.create_polygon(x-hw,y-hh, x+hw,y-hh, x+ow,y, x+hw,y+hh, x-hw,y+hh, x-ow,y, fill=fill, outline=outline)
		self.outline = outline
		self.selectoutline = selectoutline
		self.mousex = self.mousey = 0
		
	def hittest(self, x, y):
		ow = self.width / 2 + 10
		hh = self.height / 2
		return abs(x-self.x) <= ow and abs(y-self.y) <= hh
		
	def onclick(self, x, y):
		canvas.itemconfig(self.poly, outline=self.selectoutline)
		self.mousex = x
		self.mousey = y
		
	def lostfocus(self):
		canvas.itemconfig(self.poly, outline=self.outline)
		
	def ondrag(self, x, y):
		self.x += x-self.mousex
		self.y += y-self.mousey
		self.mousex = x
		self.mousey = y
		hw = self.width / 2
		hh = self.height / 2
		ow = hw+10
		canvas.coords(self.poly, self.x-hw,self.y-hh, self.x+hw,self.y-hh, self.x+ow,self.y, self.x+hw,self.y+hh, self.x-hw,self.y+hh, self.x-ow,self.y)
		canvas.coords(self.canvastext, self.x, self.y)
		
	def textchanged(self,event):
		text = self.text.get()
		newlen = len(text)
		print text
		if newlen != self.textlen:
			print 'length changed'
			self.textlen = newlen

grabbed = None
def onclick(event):
	global grabbed
	if grabbed != None:
		grabbed.lostfocus()
		grabbed = None
	print "Click"
	#TODO: Consider using quadtrees for performance
	for i in range(len(nodes)-1,-1,-1):
		node = nodes[i]
		if node.hittest(event.x, event.y):
			grabbed = node
			print "Found hit!"
			node.onclick(event.x, event.y)
			break
			
def ondrag(event):
	global grabbed
	if grabbed != None:
		grabbed.ondrag(event.x, event.y)
		
def ondoubleclick(event):
	global nodes
	nodes.append(Call(canvas, (event.x, event.y)))

root = Tkinter.Tk()
canvas = Tkinter.Canvas(root, width=302,height=302)
nodes = []
nodes.append(Call(canvas, (50,40), "Foo"))
canvas.bind('<Button-1>', onclick)
canvas.bind('<B1-Motion>', ondrag)
canvas.bind('<Double-Button-1>', ondoubleclick)
canvas.pack()
root.mainloop()