#!/usr/bin/env python
import Tix,math,tkFont

class Punkt:
	def __init__(self,x=0,y=0):
		self.x=x
		self.y=y

	def text(self):
		return "[%9.4f,%9.4f]"%(self.x,self.y)

class Appn(Tix.Frame):
	def arc_frame(self,mx,my,Radius,StartWinkel,EndWinkel):
		n=math.radians(StartWinkel)
		Punkt1=Punkt(math.cos(n)*Radius+mx,math.sin(n)*Radius+my)

		n=math.radians(EndWinkel)
		Punkt2=Punkt(math.cos(n)*Radius+mx,math.sin(n)*Radius+my)

		Minimum=Punkt(Punkt2.x,Punkt2.y)
		Maximum=Punkt(Punkt1.x,Punkt1.y)

		if Punkt1.x < Punkt2.x:
			Minimum.x=Punkt1.x
			Maximum.x=Punkt2.x

		if Punkt1.y < Punkt2.y:
			Minimum.y=Punkt1.y
			Maximum.y=Punkt2.y

		if StartWinkel > EndWinkel: StartWinkel-=360
			
		if StartWinkel<=0<=EndWinkel: Maximum.x=mx+Radius
		if StartWinkel<=90<=EndWinkel or StartWinkel<=-270<=EndWinkel: Maximum.y=my+Radius
		if StartWinkel<=180<=EndWinkel or StartWinkel<=-180<=EndWinkel: Minimum.x=mx-Radius
		if StartWinkel<=270<=EndWinkel or StartWinkel<=-90<=EndWinkel: Minimum.y=my-Radius

		return [Minimum,Maximum]
		
	def PushPop(self, event, v, b): self.move[v]=b

	def MouseMove(self, event):
		if self.move[0]:
			w=math.atan2(event.y-200,event.x-200)
			newposy=math.sin(w)*180+200
			newposx=math.cos(w)*180+200
			self.c.coords('w1',newposx-4,newposy-4,newposx+4,newposy+4)
			self.c.itemconfig('arc',start=-math.degrees(w))
			w2=w-math.radians(float(self.c.itemcget('arc','extent')))
			newposy=math.sin(w2)*180+200
			newposx=math.cos(w2)*180+200
			self.c.coords('w2',newposx-4,newposy-4,newposx+4,newposy+4)

		if self.move[1]:
			w=math.atan2(event.y-200,event.x-200)
			newposy=math.sin(w)*180+200
			newposx=math.cos(w)*180+200
			self.c.coords('w2',newposx-4,newposy-4,newposx+4,newposy+4)
			w=360-(float(self.c.itemcget('arc','start'))-math.degrees(-w))%360
			self.c.itemconfig('arc',extent=w)

		winkel1=float(self.c.itemcget('arc','start'))
		winkel2=(float(self.c.itemcget('arc','extent'))+winkel1)%360
		self.c.itemconfig("t2",text="%03.0f" % winkel1)
		self.c.itemconfig("t1",text="%03.0f" % winkel2)

		z=self.arc_frame(200,-200,180,winkel1,winkel2)
		self.c.coords('frame',z[0].x,-z[0].y,z[1].x,-z[1].y)
		
	def __init__(self,master=None):
		Tix.Frame.__init__(self,master)
		self.pack()
		self.c=Tix.Canvas(width=400,height=400,bg="white")

		self.c.create_rectangle(200,20,380,200,tag="frame",dash=2,outline="red")
		self.c.create_arc(20,20,380,380,tag='arc',style=Tix.ARC)

		self.c.create_oval(380-4,200-4,380+4,200+4,fill="orange",tag="w1")
		self.c.create_oval(200-4,20-4,200+4,20+4,fill="lightgreen",tag="w2")

		self.c.create_text(50,0,text="090",fill="green",font=("Times", "14", "bold"),anchor=Tix.NW,tag="t1")
		self.c.create_text(10,0,text="000",fill="orange",font=("Times", "14", "bold"),anchor=Tix.NW,tag="t2")

		self.c.tag_bind('w1', '<ButtonRelease-1>', lambda e,s=self:s.PushPop(e,0,False))
		self.c.tag_bind('w1', '<Button-1>', lambda e,s=self:s.PushPop(e,0,True))
		self.c.bind("<Motion>",self.MouseMove)
		self.c.tag_bind('w2', '<ButtonRelease-1>', lambda e,s=self:s.PushPop(e,1,False))
		self.c.tag_bind('w2', '<Button-1>', lambda e,s=self:s.PushPop(e,1,True))
		self.c.pack()
		self.move=[False,False]

root=Tix.Tk()
app=Appn(master=root)
app.master.title("TEST")
app.mainloop()
