'''
Select coordinates and draw with mouse selection
'''

# import wxPython
import wx
# import the floatcanvas module
import floatcanvas2 as fc

class Drawer(object):
    def __init__(self, canvas):
        self.point = None
        self.canvas = canvas
        #Event handlers
        self.canvas.subscribe( self.OnLeftDown, 'left_down' )
        #canvas.subscribe( self.OnMove, 'move' )
        #canvas.subscribe( self.OnRightUp, 'left_up' )
        self.canvas.subscribe( self.OnRightDown, 'right_down' )
        #Show image
        img = wx.Image( 'floatcanvas2/data/toucan.png' )
        ## Matthias: Note you can use img directly now
        #imgData = fc.arrayFromImage( img )
        self.canvas.create( 'Bitmap', img, pos = (0,0), look = ('white','white') )
        self.canvas.create( 'Circle', 300, pos = (0,0), look=('red', 'red'), where = 'front' )
        ##why the bitmap is in front of everything? How to set the order appearance of the objects?
        self.canvas.zoomToExtents()

    def OnLeftDown(self, evt):
        pos = evt.coords
        self.point = (pos[0], pos[1])
        self.drawNode = self.canvas.create('Circle', 10, pos=self.point, look=('black', 'white'))
        self.drawNode.subscribe( self.OnRightDown, 'right_down' )
        print 'Point: ', self.point
        print self.drawNode

    def onMove(self, evt):
        evt.Skip()

    def onRightUp(self, evt):
        evt.Skip()

    def OnRightDown(self, evt):
        #print evt.coords
        print evt.wx_event.GetPosition()
        nodes = self.canvas.hitTest(evt.wx_event.GetPosition())
        print nodes
        if not nodes:
            return
        self.canvas.removeChild(nodes[0])

class Frame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, title="Xc3", size=(800,600))

        #Status bar
        self.statusbar = self.CreateStatusBar()
        
        #MenuBar
        self.menuBar = wx.MenuBar()
        file_menu = wx.Menu()
        item = file_menu.Append(-1, "&Exit")
        self.Bind(wx.EVT_MENU, self.OnExit, item)
        self.menuBar.Append(file_menu, "&File")
        self.SetMenuBar(self.menuBar)

        #The canvas
        canvas = fc.NavCanvas( self, backgroundColor = 'white' )
        drawer = Drawer(canvas)

        #Event handlers
        ##Neither of these event bindings work after toolbar (guimode) is used, why?:
        canvas.Bind(wx.EVT_MOTION, self.OnMotion)
        #canvas.canvasPanel.Bind(wx.EVT_MOTION, self.OnMotion)

    def OnMotion(self, event):
        #print self.canvas.events.
        self.statusbar.SetStatusText("Coordinates: %s" % str(event.GetPositionTuple()), 0)
        event.Skip()

    def OnExit(self,event):
        self.Close(True)
        #self.Destroy()


class App(wx.App):
    """Application class"""
    def OnInit(self):
        self.frame = Frame(None)
        self.frame.Show(True)
        self.SetTopWindow(self.frame)
        return True

def main():
    app = App(redirect = False)
    app.MainLoop()

if __name__ == '__main__':
    main()
