# JV website generator
# (c) 2008, Johan Tuitman
# Licentie: GPL
#
# Subroutines voor GUI
import wx
import pict
import os

myEVT_NEW_ITEM = wx.NewEventType()
EVT_NEW_ITEM = wx.PyEventBinder(myEVT_NEW_ITEM,1)

myEVT_DEL_ITEM = wx.NewEventType()
EVT_DEL_ITEM = wx.PyEventBinder(myEVT_DEL_ITEM,1)

myEVT_CHANGE_ITEM = wx.NewEventType()
EVT_CHANGE_ITEM = wx.PyEventBinder(myEVT_CHANGE_ITEM,1)

class ChangeListItem(wx.PyCommandEvent):
    def __init__(self,evtType,idd):
        wx.PyCommandEvent.__init__(self,evtType,idd)
        self.Item = ""

    def GetItem(self):
        return self.Item

    def SetItem(self,item):
        self.Item = item

class extraListBox:
    """create a listbox item with additional functionality"""

    def __init__(self, parent, panel, lt, lang, order=True):
        """
        Special listBox version, argument:

        parent     : the wx.Frame to add the list box
        panel      : the wx.Panel to add the list box
        lt         : The initial list to be used
        lang       : Language for dialogs  (EN and NL implemented"""

        self.parent = parent
        self.panel = panel
        self.lt = lt
        self.lb = wx.ListBox(self.panel,-1,choices=self.lt)
        self.messa = self.Messages(lang)

        picAdd = pict.getAddBitmap()
        self.buttonA = wx.BitmapButton(self.panel,-1,picAdd)

        picDel = pict.getDelBitmap()
        self.buttonDe = wx.BitmapButton(self.panel,-1,picDel)

        if order:
            picUp = pict.getUpBitmap()
            self.buttonU = wx.BitmapButton(self.panel,-1,picUp)

            picDown = pict.getDownBitmap()
            self.buttonDo = wx.BitmapButton(self.panel,-1,picDown)

        self.parent.Bind(wx.EVT_BUTTON,self.OnButtonA,self.buttonA)
        self.parent.Bind(wx.EVT_BUTTON,self.OnButtonDe,self.buttonDe)
        if order:
            self.parent.Bind(wx.EVT_BUTTON,self.OnButtonU,self.buttonU)
            self.parent.Bind(wx.EVT_BUTTON,self.OnButtonDo,self.buttonDo)
        self.parent.Bind(wx.EVT_LISTBOX,self.changeLB,self.lb)

    def GetLb(self):
        return self.lb

    def GetButtonA(self):
        return self.buttonA

    def GetButtonDe(self):
        return self.buttonDe

    def GetButtonU(self):
        return self.buttonU

    def GetButtonDo(self):
        return self.buttonDo

    def checkSel(self):
        sel = self.lb.GetSelections()
        if len(sel) == 0:
            dlg = wx.MessageDialog(self.parent, self.messa['SelErr'][0] \
                    , self.messa['SelErr'][1],  wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            dlg.Destroy()
            return None
        return sel[0]

    def OnButtonA(self,event):
        if len(self.lt) > 0:
            sel = self.checkSel()
            if sel == None:
                return
        else:
            sel = 0

        dlg = wx.TextEntryDialog(self.parent, self.messa['NewItem'][0], self.messa['NewItem'][1])

        if dlg.ShowModal() == wx.ID_OK:
            self.lt.insert(sel,dlg.GetValue())
            self.lb.Set(self.lt)
            self.lb.Select(sel)
            evt = ChangeListItem(myEVT_NEW_ITEM,self.lb.GetId())
            evt.SetItem(dlg.GetValue())
            self.lb.GetEventHandler().ProcessEvent(evt)

        dlg.Destroy()

    def OnButtonDe(self,event):
        sel = self.checkSel()
        if sel == None:
            return

        dlg = wx.MessageDialog(self.parent, self.messa['DelItm'][0]+ \
                self.lt[sel]+self.messa['DelItm'][1],self.messa['DelItm'][2])

        if dlg.ShowModal() == wx.ID_OK:
            delItem = str(self.lt[sel])
            del self.lt[sel]
            self.lb.Set(self.lt)
            if sel == len(self.lt):
                self.lb.Select(sel-1)
            else:
                self.lb.Select(sel)

            evt = ChangeListItem(myEVT_DEL_ITEM,self.lb.GetId())
            evt.SetItem(delItem)
            self.lb.GetEventHandler().ProcessEvent(evt)

        dlg.Destroy()


    def OnButtonU(self,event):
        sel = self.checkSel()
        if sel == None:
            return

        if sel == 0:
            self.lt = self.lt[1:] + [self.lt[0]]
            nxs = len(self.lt)-1
        else:
            self.lt[sel], self.lt[sel-1] = self.lt[sel-1], self.lt[sel]
            nxs = sel-1

        self.lb.Set(self.lt)
        self.lb.Select(nxs)

        evt = ChangeListItem(myEVT_CHANGE_ITEM,self.lb.GetId())
        evt.SetItem(nxs)
        self.lb.GetEventHandler().ProcessEvent(evt)
        

    def OnButtonDo(self,event):
        sel = self.checkSel()
        if sel == None:
            return

        if sel == len(self.lt)-1:
            self.lt = [self.lt[-1]] + self.lt[:-1]
            nxs = 0
        else:
            self.lt[sel], self.lt[sel+1] = self.lt[sel+1], self.lt[sel]
            nxs = sel+1

        self.lb.Set(self.lt)
        self.lb.Select(nxs)

        evt = ChangeListItem(myEVT_CHANGE_ITEM,self.lb.GetId())
        evt.SetItem(nxs)
        self.lb.GetEventHandler().ProcessEvent(evt)

    def changeLB(self,event):
        """only sent change event when there is a selection"""
        sel = self.lb.GetSelections()
        if len(sel)> 0:
            evt = ChangeListItem(myEVT_CHANGE_ITEM,self.lb.GetId())
            evt.SetItem(sel[0])
            self.lb.GetEventHandler().ProcessEvent(evt)

    def Messages(self, lang):
        """The messages"""

        mes = {}
        if lang.lower() == 'en':
            mes['SelErr'] = ['Select an item first', 'Selection error']
            mes['NewItem'] = ['Enter the name for new item','New item']
            mes['DelItm'] = ['Are you sure to delete item "', '"?', 'Delete item']

        elif lang.lower() == 'nl':
            mes['SelErr'] = ['Selecteer eerst een item', 'Selectie fout']
            mes['NewItem'] = ['Voer naam voor het nieuwe item in.','Nieuwe item']
            mes['DelItm'] = ['Weet je zeker dat het item "','" wilt verwijderen?','Werwijder item']

        return mes

    def SetLB(self,lb):
        self.lt = lb
        self.lb.Set(self.lt)

    def GetLB(self):
        return self.lt

class extraListBox2(extraListBox):
    """create a listbox item with additional functionality"""

    def __init__(self, parent, panel, lt, lang):
        extraListBox.__init__(self, parent, panel, lt, lang,False)
        self.IsAssinged = False

    def OnButtonA(self,event):
        evt = ChangeListItem(myEVT_NEW_ITEM,self.lb.GetId())
        self.lb.GetEventHandler().ProcessEvent(evt)

    def AddItem(self,item):
        self.lt.append(item)
        self.lb.Set(self.lt)

    def getAssingend(self):
        return self.IsAssinged

    def setAssinged(self,assinged):
        self.IsAssinged = assinged

def relPath(base,path):
    if os.name == 'posix':
        sep = '/'
    else:
        sep = '\\'

    cbase = base.split(sep)
    cpath = path.split(sep)

    i = 0
    while cbase[i] == cpath[i]:
        i += 1
        if len(cbase) == i:
            break
        if len(cpath) == i:
            break

    st = len(cbase[i:])*('..'+sep)

    for p in cpath[i:]:
        st += p+sep

    return st