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

import wx
import wx.stc as st

controlpos = 0

class MyPopupMenu(wx.Menu):
    
    def __init__(self, parent, id):
        super(MyPopupMenu, self).__init__()
        
        self.grandparent = parent
        self.id = id

#        mmi = wx.MenuItem(self, wx.NewId(), 'Remove')
#        self.AppendItem(mmi)
#        self.Bind(wx.EVT_MENU, self.OnRemove, mmi)
        
        frmmi = wx.MenuItem(self, wx.NewId(), 'Add Fortran')
        self.AppendItem(frmmi)
        self.Bind(wx.EVT_MENU, self.OnAddFr, frmmi)

    def OnAddFr(self, e):
        wx.CallAfter(self.grandparent.OnAddControl, self)

#    def OnRemove(self, e):
#        wx.CallAfter(self.grandparent.RemoveMe, self.id)

class STC(st.StyledTextCtrl):
    def __init__(self, *args, **kwargs):
        super(STC, self).__init__(*args, **kwargs)
        
###        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
###
###    def OnRightDown(self, e):
###        index = self.GetParent().Autosizer.GetItemIndex(self)
###        print index
#        control = e.GetEventObject()
#        wx.CallAfter(self.GetParent().RemoveMe, self.GetId())

        self.InitUI()
        
    def InitUI(self):

        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)

        self.SetSize((250, 200))
#        self.SetTitle('Context menu')
        self.Centre()
        self.Show(True)
        
    def OnRightDown(self, e):
        control = e.GetEventObject()
#        wx.CallAfter(self.GetParent().RemoveMe, self.GetId())
        self.PopupMenu(MyPopupMenu(self.GetParent(), self.GetId()))
        
# 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 P")
        btn.Bind(wx.EVT_BUTTON, self.OnAddPython)
        
        self.Autosizer.Add(btn)
        self.Autosizer.AddSpacer(10)
        
        btn = wx.Button(self, wx.ID_ANY, label="add controls F")
        btn.Bind(wx.EVT_BUTTON, self.OnAddFortran)
        
        self.Autosizer.Add(btn)
        self.Autosizer.AddSpacer(10)
        
        self.ctrlsDict = {}

    def GetItemIndex(self, item):
        sItem = self.GetItem(item)
        assert sItem is not None, "Item not found in the sizer."
        allItems = self.Children
        idx = 0
        for i in allItems:
            if i.this == sItem.this:
                break
                idx += 1
        return idx
       
        
    def GetPosInSizer(self, control):
        chil = self.Autosizer.GetChildren()
        for pos in range(len(chil)):
            szitem = chil[pos]
            if szitem.GetId() == control:
                return pos
            
    def OnAddPython(self, event):
        text = wx.StaticText(self, label='P')
        editor = STC(self)
        self.Autosizer.Add(text, 0)
        self.Autosizer.Add(editor, 1, wx.EXPAND)
        self.Layout()
        self.ctrlsDict[editor.GetId()] = text.GetId()

    def OnAddControl(self, event):
        text = wx.StaticText(self, label='Added control')
        editor = STC(self)
        
        if controlpos != 0:
            self.Autosizer.Insert(controlpos, text)
            self.Autosizer.Insert(controlpos + 1, editor)
            
        self.Layout()
        self.ctrlsDict[editor.GetId()] = text.GetId()

    def OnAddFortran(self, event):
        text = wx.StaticText(self, label='F')
        editor = STC(self)
        self.Autosizer.Add(text, 0)
        self.Autosizer.Add(editor, 1, wx.EXPAND)
        
        if controlpos != 0:
            self.Autosizer.Insert(controlpos, text)
            self.Autosizer.Insert(controlpos + 1, editor)
            
        self.Layout()
        self.ctrlsDict[editor.GetId()] = text.GetId()
        
    def RemoveMe(self, ctrlid):
        editor = wx.FindWindowById(ctrlid)
        text = wx.FindWindowById(self.ctrlsDict[ctrlid])
        
        editor.Destroy()
        text.Destroy()
        del editor
        del text
        del self.ctrlsDict[ctrlid]
        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()
    frame = wx.Frame(None)
    panel = TestPanel(frame)
    frame.Show()
    app.MainLoop()
    
