# -*- coding: utf-8 -*-#

import wx
import wx.stc as st

class STC(st.StyledTextCtrl):
    def __init__(self, *args, **kwargs):
        super(STC, self).__init__(*args, **kwargs)

    def OnRightDown(self, e):
        control = e.GetEventObject()
        self.PopupMenu(MyPopupMenu(self.GetParent(), control), e.GetPosition())

# deleting the control from Autosizer
class MyPopupMenu(wx.Menu):
   
    def __init__(self, parent, object):
        super(MyPopupMenu, self).__init__()

        mmi = wx.MenuItem(self, wx.NewId(), 'Remove')
        self.AppendItem(mmi)
        self.Bind(wx.EVT_MENU, self.OnRemove, mmi)

    def OnRemove(self, e):
        self.parent.Autosizer.Remove(self.object)
        self.parent.Layout()

# adding the control to Autosizer.
class TestPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        self.Autosizer = wx.FlexGridSizer(cols=2)
        self.Autosizer.SetFlexibleDirection(wx.VERTICAL)

        self.SetSizer(self.Autosizer)
        
        btn = wx.Button(self, wx.ID_ANY, label="add controls")
        btn.Bind(wx.EVT_BUTTON, self.OnAddPython)
        
        self.Autosizer.Add(btn)
        self.Autosizer.AddSpacer(10)

    def OnAddPython(self, event):
        self.text = wx.StaticText(self, label='P')
        self.editor = STC(self)
        self.Autosizer.Add(self.text,0)
        self.Autosizer.Add(self.editor,1, wx.EXPAND)
        self.Layout()


from wx.lib.mixins.inspection import InspectionMixin

class AnApp(wx.App, InspectionMixin):
    def OnInit(self):
        self.Init() # InspectionMixin
        
        return True

if __name__ == '__main__':
    
    app = AnApp(1)
    frame = wx.Frame(None)
    panel = TestPanel(frame)
    frame.Show()
    app.MainLoop()
    
