import wx  
import wx.lib.ogl as ogl

class RoundedRectangleShape(ogl.RectangleShape):
    def __init__(self, w=0.0, h=0.0):
        ogl.RectangleShape.__init__(self, w, h)
        self.SetCornerRadius(10)  

class MyEvtHandler(ogl.ShapeEvtHandler):
    def __init__(self, frame):
        ogl.ShapeEvtHandler.__init__(self)

    def OnLeftClick(self, x, y, keys=0, attachment=0):
        shape = self.GetShape()
        canvas = shape.GetCanvas()
        dc = wx.ClientDC(canvas)
        canvas.PrepareDC(dc)

        if shape.Selected():
            shape.Select(False, dc)
            canvas.Refresh(False)
        else:
            shape.Select(True, dc)
            shapeList = canvas.GetDiagram().GetShapeList()
            toUnselect = [s for s in shapeList if s.Selected()]
            for s in toUnselect:
                s.Select(False, dc)

            canvas.Refresh(False)

    def OnEndDragLeft(self, x, y, keys=0, attachment=0):
        shape = self.GetShape()
        ogl.ShapeEvtHandler.OnEndDragLeft(self, x, y, keys, attachment)

        if not shape.Selected():
            self.OnLeftClick(x, y, keys, attachment)

    def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
        ogl.ShapeEvtHandler.OnSizingEndDragLeft(self, pt, x, y, keys, attch)

    def OnMovePost(self, dc, x, y, oldX, oldY, display):
        shape = self.GetShape()
        ogl.ShapeEvtHandler.OnMovePost(self, dc, x, y, oldX, oldY, display)
        if "wxMac" in wx.PlatformInfo:
            shape.GetCanvas().Refresh(False)

class TestWindow(ogl.ShapeCanvas):
    def __init__(self, parent):
        ogl.ShapeCanvas.__init__(self, parent)

        maxWidth = 1000  
        maxHeight = 1000  
        self.SetScrollbars(20, 20, maxWidth // 20, maxHeight // 20)

        self.frame = parent  
        self.SetBackgroundColour("LIGHT BLUE")
        self.diagram = ogl.Diagram()
        self.SetDiagram(self.diagram)
        self.diagram.SetCanvas(self)
        self.shapes = []

        rRectBrush = wx.Brush("MEDIUM TURQUOISE", wx.BRUSHSTYLE_SOLID)
        dsBrush = wx.Brush("WHEAT", wx.BRUSHSTYLE_SOLID)

        self.MyAddShape(ogl.RectangleShape(85, 50), 305, 60, wx.BLACK_PEN, wx.LIGHT_GREY_BRUSH, "Rectangle")
        self.MyAddShape(RoundedRectangleShape(95, 70), 345, 145, wx.Pen(wx.RED, 2), rRectBrush, "Rounded Rect")

        for x in range(len(self.shapes)):
            fromShape = self.shapes[x]
            toShape = self.shapes[(x + 1) % len(self.shapes)]

            line = ogl.LineShape()
            line.SetCanvas(self)
            line.SetPen(wx.BLACK_PEN)
            line.SetBrush(wx.BLACK_BRUSH)
            line.AddArrow(ogl.ARROW_ARROW)
            line.MakeLineControlPoints(2)
            fromShape.AddLine(line, toShape)
            self.diagram.AddShape(line)
            line.Show(True)

    def MyAddShape(self, shape, x, y, pen, brush, text):
        shape.SetCanvas(self)
        shape.SetX(x)
        shape.SetY(y)
        if pen:
            shape.SetPen(pen)
        if brush:
            shape.SetBrush(brush)
        if text:
            for line in text.split('\n'):
                shape.AddText(line)
        self.diagram.AddShape(shape)
        shape.Show(True)

        evthandler = MyEvtHandler(self.frame)
        evthandler.SetShape(shape)
        evthandler.SetPreviousHandler(shape.GetEventHandler())
        shape.SetEventHandler(evthandler)
        self.shapes.append(shape)

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="OGL Example", size=(800, 600))
        self.panel = TestWindow(self)

if __name__ == "__main__":
    app = wx.App(False)
    ogl.OGLInitialize()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()
    
