#   Programmer: Franz Steinhaeusler
#   E-mail:     francescoa@users.sourceforge.net
#   Note:       You must reply to the verification e-mail to get through.
#
#   Copyright 2005 Franz Steinhaeusler
#
#   Distributed under the terms of the GPL (GNU Public License)
#
#    DrPython is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Version 0.0.0 23.01.2005 ... initial version
#Plugin
#GetLine

import wx

def Plugin(DrFrame):

    class GetLineDialog (wx.Dialog):

        def __init__(self, parent, id, title):
            ypos = DrFrame.GetSize()[1] + DrFrame.GetPosition()[1] - 40
            xpos = DrFrame.GetPosition()[0] + 150

            wx.Dialog.__init__(self, parent, id, title, (xpos, ypos), (600, 30), style = 0)
            self.parent = parent
            self.curline = self.parent.txtDocument.GetCurrentLine()
            self.curcol = self.parent.txtDocument.GetColumn(self.parent.txtDocument.GetCurrentPos())
            self.curxposinline = self.parent.txtDocument.GetCurrentPos() - (self.parent.txtDocument.GetLineEndPosition(self.curline) - self.parent.txtDocument.LineLength(self.curline))
            linetext = parent.txtDocument.GetLine(self.curline)
            #self.linetextctrl = wx.TextCtrl (self, -1, linetext.strip(), size = (450, 20), pos = (120, 5), style = wx.TE_READONLY)
            #ugly workaround for wxpyhton 2.8 (otherwise, the enter key cannot be catched.
            self.linetextctrl = wx.TextCtrl (self, -1, linetext.strip(), size = (450, 20), pos = (120, 5), style = wx.TE_PROCESS_ENTER)
            self.linetextctrl.SetBackgroundColour(wx.Colour(244, 244, 232))
            self.static = wx.StaticText (self, -1, "GetLine LineNr: %d" % (self.curline + 1), pos = (10, 8))
            self.lines = self.parent.txtDocument.LineFromPosition (self.parent.txtDocument.GetTextLength())
            self.linetextctrl.Bind (wx.EVT_CHAR,  self.OnChar)

            if self.parent.GetLine_oldstylepos != -1:
                self.parent.txtDocument.StartStyling(self.parent.GetLine_oldstylepos, wx.stc.STC_INDIC2_MASK) #clear style
                self.parent.txtDocument.SetStyling(self.parent.GetLine_oldstylelen, 0) #clear style

            endpos = self.parent.txtDocument.GetLineEndPosition (self.curline)
            len = self.parent.txtDocument.LineLength (self.curline)
            begpos = endpos - len
            self.parent.txtDocument.StartStyling(begpos, wx.stc.STC_INDIC2_MASK)
            self.parent.txtDocument.SetStyling(len, wx.stc.STC_INDIC2_MASK)
            self.parent.GetLine_oldstylepos = begpos
            self.parent.GetLine_oldstylepos = begpos
            self.parent.GetLine_oldstylelen = len
            self.parent.txtDocument.MarkerAdd(self.curline, self.parent.GetLine_BOOKMARKNUMBER)
            if not self.PLATFORM_IS_WIN: #on gtk, it needs additional setfocus
                self.linetextctrl.SetFocus()

        def ClearOldStyle(self):
            if self.parent.GetLine_oldstylepos != -1:
                self.parent.txtDocument.StartStyling(self.parent.GetLine_oldstylepos, wx.stc.STC_INDIC2_MASK)
                self.parent.txtDocument.SetStyling(self.parent.GetLine_oldstylelen, 0) #clear style
                self.parent.GetLine_oldstylepos = -1
        
        def OnChar(self, event):
            key = event.GetKeyCode()
            print key
            if key == wx.WXK_ESCAPE:
                self.ClearOldStyle()
                self.parent.txtDocument.MarkerDelete(self.parent.txtDocument.GetCurrentLine(), self.parent.GetLine_BOOKMARKNUMBER)
                self.Destroy()
            if key == wx.WXK_RETURN:
                self.ClearOldStyle()
                curline = self.parent.txtDocument.GetCurrentLine()
                self.parent.txtDocument.MarkerDelete(curline, self.parent.GetLine_BOOKMARKNUMBER)
                self.parent.txtDocument.CmdKeyExecute(wx.stc.STC_CMD_HOMEDISPLAY)
                self.parent.txtDocument.AddText(self.parent.txtDocument.GetLine(self.curline))
                self.parent.txtDocument.CmdKeyExecute(wx.stc.STC_CMD_LINEUP)
                linelen = self.parent.txtDocument.LineLength(curline)
                if linelen >= self.curxposinline:
                    self.parent.txtDocument.GotoPos(self.parent.txtDocument.GetCurrentPos() + self.curxposinline)
                self.Destroy()
            if key in [wx.WXK_DOWN, wx.WXK_UP]:
                if key == wx.WXK_DOWN:
                    self.curline += 1
                    if self.curline > self.lines - 1:
                        self.curline = 0
                else:
                    self.curline -= 1
                    if self.curline < 0:
                        self.curline = self.lines - 1
                self.static.SetLabel ("GetLine LineNr: %d" % (self.curline + 1))
                self.linetextctrl.SetValue (self.parent.txtDocument.GetLine(self.curline).strip())
                endpos = self.parent.txtDocument.GetLineEndPosition (self.curline)
                len = self.parent.txtDocument.LineLength (self.curline)
                begpos = endpos - len
                self.ClearOldStyle()
                self.parent.txtDocument.StartStyling(begpos, wx.stc.STC_INDIC2_MASK)
                self.parent.txtDocument.SetStyling(len, wx.stc.STC_INDIC2_MASK)
                self.parent.GetLine_oldstylepos = begpos
                self.parent.GetLine_oldstylelen = len

            event.Skip()

    def OnGetLine (event):
        DrFrame.txtDocument.MarkerDefine(DrFrame.GetLine_BOOKMARKNUMBER, wx.stc.STC_MARK_MINUS, 'black', 'white')
        DrFrame.GetLine_oldstylepos = -1
        DrFrame.txtDocument.IndicatorSetStyle(2, 0x06)
        dlg = GetLineDialog(DrFrame, -1, "GetLine")
        dlg.ShowModal()
        dlg.ClearOldStyle()
        DrFrame.txtDocument.MarkerDelete(DrFrame.txtDocument.GetCurrentLine(), DrFrame.GetLine_BOOKMARKNUMBER)

    ID_GETLINE = DrFrame.GetNewId()
    DrFrame.GetLine_oldstylepos = -1
    DrFrame.GetLine_oldstylelen = -1
    DrFrame.GetLine_BOOKMARKMASK = 8
    DrFrame.GetLine_BOOKMARKNUMBER = 4

    DrFrame.Bind(wx.EVT_MENU, OnGetLine, id = ID_GETLINE)

    DrFrame.AddPluginShortcutFunction("GetLine", "Get Line", OnGetLine)
    DrFrame.AddPluginPopUpMenuFunction("GetLine", "Get Line", OnGetLine)

    DrFrame.LoadPluginShortcuts('GetLine')
    DrFrame.editmenu.AppendSeparator()
    DrFrame.editmenu.Append(ID_GETLINE, DrFrame.GetPluginMenuLabel('GetLine', 'Get Line', 'Get Line'))
