import(tk)
import(png)

//tk.tclDump()

tk.mainwindow() {|mw|
	mw.Canvas(bg => 'white') {|c|
		c.pack(fill => 'both', expand => true)
		[lastx, lasty] = [0, 0]
		color = 'black'
		c.bind('<1>') {|x:number, y:number|
			[lastx, lasty] = [x, y]
		}
		c.bind('<B1-Motion>') {|x:number, y:number|
			addLine(x, y)
		}
		addLine(x:number, y:number) = {
			extern(lastx, lasty)
			c.Line(lastx, lasty, x, y, fill => color, width => 3)
			[lastx, lasty] = [x, y]
		}
		setColor(colorNew:string) = {
			color:extern = colorNew
		}
		for (color in ['red', 'blue', 'black']) {|i|
			lambda(color:string, y:number) {
				c.Rectangle(10, y, 30, y + 20, fill => color) {|item|
					item.bind('<1>') { setColor(color) }
				}
			}(color, 10 + i * 25)
		}
	}
}
tk.mainloop()
