#!/usr/bin/env python
import wx

(START,INTER,END) = xrange(3)

class Circle(object):
    def __init__(self,x,y,circleType):
        self.x = x
        self.y = y
        self.radius = 25
        self.circleType = circleType
        self.region = self.setRegion()

    def setRegion(self):
        return wx.Region(self.x,self.y,self.radius*2,self.radius*2)

    def onPaint(self,gc,x,y):
        gc.SetPen(wx.Pen("black", 1))
        self.path = gc.CreatePath()
        self.path.AddCircle(x+self.radius, y+self.radius, self.radius)
        if self.circleType == START:
            gc.StrokePath(self.path)
        else:
            self.path.AddCircle(x+self.radius, y+self.radius, self.radius-4)
            if self.circleType == END:
                gc.SetBrush(wx.Brush("black"))
            else:
                gc.SetBrush(wx.TRANSPARENT_BRUSH)
            gc.DrawPath(self.path)

    def HitTest(self,x,y):
        return self.region.Contains(x,y)

class Circle2(Circle):

    def setRegion(self):
        bmp = wx.EmptyBitmap(self.radius*2, self.radius*2)
        dc = wx.MemoryDC()
        dc.SelectObject(bmp)

        # black will be our transparent colour
        dc.SetBackground(wx.Brush("black"))
        dc.Clear()

        # draw the outer circle (opaque)
        dc.SetBrush(wx.Brush("white"))
        dc.SetPen(wx.Pen("white"))
        dc.DrawCircle(self.radius, self.radius, self.radius)

        dc.SelectObject(wx.NullBitmap)
        del dc

        bmp.SetMaskColour("black")
        return wx.RegionFromBitmap(bmp)

class MyPanel(wx.Panel):
    def __init__(self,frame):
        wx.Panel.__init__(self,frame,-1)
        self.obj = []
        self.currentObj = None

        self.obj.append(Circle(100,100,START))
        self.obj.append(Circle(200,100,INTER))
        self.obj.append(Circle(300,100,END))
        self.obj.append(Circle2(0,0,START))

        self.Bind(wx.EVT_PAINT,self.onPaint)
        self.Bind(wx.EVT_LEFT_DOWN,self.onLeftDown)

    def onPaint(self,evt):
        dc = wx.PaintDC(self)
        gc = wx.GraphicsContext.Create(dc)
        for obj in self.obj:
            obj.onPaint(gc,obj.x,obj.y)

    def onLeftDown(self,evt):
        self.currentObj = None
        for obj in self.obj:
            if obj.HitTest(evt.X,evt.Y):
                self.currentObj = obj
                break
        print self.currentObj

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(600,400))
        self.panel = MyPanel(self)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, "BPMN Symbols")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)     # Create an instance of the application class
app.MainLoop()     # Tell it to start processing events
