import wx

class PaintPanel(wx.Panel):
    def __init__(self, parent, id, sub = 0, *args, **kwds):
        wx.Panel.__init__(self, parent, id, *args, **kwds)
        self.parent = parent
        self.sub =sub
        self.items = []
        
        if self.sub:
            self.parent.AddItem(self)
            self.Hide()
        else:
            pass
            self.Bind(wx.EVT_PAINT, self.OnPaint)
            self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        
    def AddItem(self, item):
        ## Add Item
        self.items.append(item)
        
    def RemoveItem(self, item):
        ## Remove Item
        for i in self.items:
            if(i == item):
                self.items.remove(i)
        
    def RemoveAll(self):
        ## Remove All Items
        self.items.clear()
        
    def OnPaint(self, event):
        self.sizeX, self.sizeY  = self.GetSize()
        print "position is: " + str(self.parent.GetPosition())
        print "size is: " + str(self.parent.GetSize())
                
        ## Setup Double Buffer DC
        buffer = wx.EmptyBitmap(self.sizeX, self.sizeY)        
        memDC = wx.MemoryDC()
        memDC.SelectObject(buffer)
        dc = wx.PaintDC(self)
        self.PrepareDC(dc)
        
        ##memDC.SetBackground(wx.Brush("White"))
        ##memDC.Clear()
        
        self.Draw(memDC)
        
        memDC.EndDrawing()
        
        ## Copy DC from MemoryDC
        dc.Blit(0, 0, self.sizeX, self.sizeY,  memDC, 0, 0, wx.COPY, True)
    
        ## Clean Up The DC
        ##del memDC
        ##del dc
    
    def Draw(self, dc):
        ## Draw All Attached Items
        for item in self.items:
            item.Draw(dc)
        
    def OnEraseBackground(self, event):
        pass

class PaintItem:
    def __init__(self, parent, id, name = ""):
        self.parent = parent
            
        self.name = name
        
        self.parent.AddItem(self)
    def Draw(self, dc):
        self.posX, self.posY  = self.parent.GetPosition()
        dc.SetTextForeground("BLUE")
        dc.SetFont(wx.Font(50, wx.SWISS, wx.NORMAL, wx.BOLD))
        dc.DrawText( self.name, self.posX, self.posY)
        
if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    frame = wx.Frame(None, -1, size = (50,50))
    frame.SetBackgroundColour((0,0,0))
    
    panel = PaintPanel(frame, -1, size = (50,50))
    item = PaintItem(panel, -1, name = "test1")
    
    app.SetTopWindow(frame)
    frame.Show()
    app.MainLoop()