#   Programmer: Daniel Pozmanter
#   E-mail:     drpython@bluebottle.com
#   Note:       You must reply to the verification e-mail to get through.
#
#   Copyright 2003-2004 Daniel Pozmanter
#
#   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

#Changelog:
#Version 0.0.1:
#   You can now insert into the Prompt.
#   ID Number bugfix (document, path, bookmark pop up menus).

#Plugin
#InsertMenu

import wx
import os, os.path

def OnAbout(DrFrame):
    AboutString = """InsertMenu:

Version: 0.0.1

By Daniel Pozmanter

Released under the GPL.

Credits:
Franz: inspiration:  I wouldn't have written
this plugin if he hadn't thought of making
a useful drscript for adding the current filename/path
to the clipboard."""

    DrFrame.ShowMessage(AboutString, "About")

def Plugin(DrFrame):

    def Insert(target):
        if DrFrame.txtPrompt.GetSTCFocus():
            cpos = DrFrame.txtPrompt.GetCurrentPos()
            editpoint = DrFrame.txtPrompt.GetEditPoint()
            if cpos < editpoint:
                cpos = editpoint
            DrFrame.txtPrompt.InsertText(cpos, target)
            DrFrame.txtPrompt.GotoPos(cpos + len(target))
        else:
            cpos = DrFrame.txtDocument.GetCurrentPos()
            DrFrame.txtDocument.InsertText(cpos, target)
            DrFrame.txtDocument.GotoPos(cpos + len(target))

    def OnInsertBookmark(event):
        i = event.GetId() - DrFrame.ID_INSERTMENU_BOOKMARK_BASE

        Insert(DrFrame.insertmenubookmarks[i])

    def OnInsertCWD(event):
        Insert(os.getcwd())

    def OnInsertDocumentFile(event):
        i = event.GetId() - DrFrame.ID_INSERT_FILENAME_BASE
        fn = DrFrame.txtDocumentArray[i].filename
        if fn:
            Insert(fn)

    def OnInsertDocumentPath(event):
        i = event.GetId() - DrFrame.ID_INSERT_PATH_BASE
        fn = DrFrame.txtDocumentArray[i].filename
        if fn:
            Insert(os.path.split(fn)[0])

    def OnInsertFilename(event):
        fn = DrFrame.txtDocument.filename
        if fn:
            Insert(fn)

    def OnInsertPath(event):
        fn = DrFrame.txtDocument.filename
        if fn:
            Insert(os.path.split(fn)[0])

    def OnViewBookmarkMenu(event):
        bookfile = DrFrame.userdatadirectory + "/bookmarks.dat"
        bookmarksmenu = wx.Menu()
        DrFrame.insertmenubookmarks = []
        if os.path.exists(bookfile):
            try:
                #Read from the file
                f = open(bookfile, 'r')
                folders = [bookmarksmenu]
                folderindex = 0
                menuTitles = []
                menuTitleindex = -1
                lastCount = 1
                bookmarkcount = 0
                #Skip the First Line
                line = f.readline()
                #Initialize
                line = f.readline()
                while line:
                    c = line.count('\t')
                    line = line[c:].rstrip()
                    while lastCount > c:
                        folders[(folderindex - 1)].AppendMenu(DrFrame.ID_INSERTMENU_BOOKMARK_MENU, menuTitles.pop(), folders.pop())
                        folderindex = folderindex - 1
                        menuTitleindex = menuTitleindex - 1
                        lastCount = lastCount - 1
                    if line[0] == '>':
                        folders.append(wx.Menu())
                        menuTitles.append(line[1:])
                        folderindex = folderindex + 1
                        menuTitleindex = menuTitleindex + 1
                        c = c + 1
                    else:
                        DrFrame.insertmenubookmarks.append(line)
                        DrFrame.Bind(wx.EVT_MENU, OnInsertBookmark, id=(DrFrame.ID_INSERTMENU_BOOKMARK_BASE + bookmarkcount))
                        folders[folderindex].Append((DrFrame.ID_INSERTMENU_BOOKMARK_BASE + bookmarkcount), line)
                        bookmarkcount = bookmarkcount + 1
                    lastCount = c
                    line = f.readline()
                f.close()
                #Add any menus not yet added:
                c = 1
                while lastCount > c:
                    folders[(folderindex - 1)].AppendMenu(DrFrame.ID_INSERTMENU_BOOKMARK_MENU, menuTitles.pop(), folders.pop())
                    folderindex = folderindex - 1
                    menuTitleindex = menuTitleindex - 1
                    lastCount = lastCount - 1

                DrFrame.PopupMenu(bookmarksmenu, DrFrame.ScreenToClient(wx.GetMousePosition()))
                bookmarksmenu.Destroy()
            except:
                DrFrame.ShowMessage("Your bookmarks file is a tad messed up.", "Error")

    def OnViewFileMenu(event):
        ViewDocumentMenu(DrFrame.ID_INSERT_FILENAME_BASE, OnInsertDocumentFile)

    def OnViewPathMenu(event):
        ViewDocumentMenu(DrFrame.ID_INSERT_PATH_BASE, OnInsertDocumentPath)

    def ViewDocumentMenu(ID_BASE, BindTarget):
        docMenu = wx.Menu()
        x = 0
        l = len(DrFrame.txtDocumentArray)
        if l > 10:
            y = 0
            yl = 10
            if yl > l:
                yl = l
            a = 0
            docSubMenus = []
            while y < yl:
                docSubMenus.append(wx.Menu())
                docMenu.AppendMenu(ID_BASE+a, str(y+1) + " - " + str(yl), docSubMenus[a])
                while x < yl:
                    if DrFrame.txtDocumentArray[x].filename:
                        docSubMenus[a].Append(ID_BASE+x, os.path.basename(DrFrame.txtDocumentArray[x].filename))
                    else:
                        docSubMenus[a].Append(ID_BASE+x, "Untitled " + str(DrFrame.txtDocumentArray[x].untitlednumber))
                    DrFrame.Bind(wx.EVT_MENU, BindTarget, id=ID_BASE+x)
                    x = x + 1
                if y == l:
                    break
                y = y + 10
                yl = yl + 10
                a = a + 1
                if yl > l:
                    yl = l
        else:
            while x < l:
                if DrFrame.txtDocumentArray[x].filename:
                    docMenu.Append(ID_BASE+x, os.path.basename(DrFrame.txtDocumentArray[x].filename))
                else:
                    docMenu.Append(ID_BASE+x, "Untitled " + str(DrFrame.txtDocumentArray[x].untitlednumber))
                DrFrame.Bind(wx.EVT_MENU, BindTarget, id=ID_BASE+x)
                x = x + 1

        DrFrame.PopupMenu(docMenu, DrFrame.ScreenToClient(wx.GetMousePosition()))
        docMenu.Destroy()


    ID_INSERT_MENU = DrFrame.GetNewId()
    ID_INSERT_FILENAME = DrFrame.GetNewId()
    ID_INSERT_FILENAME_MENU = DrFrame.GetNewId()
    ID_INSERT_PATH = DrFrame.GetNewId()
    ID_INSERT_PATH_MENU = DrFrame.GetNewId()
    ID_INSERT_BOOKMARK = DrFrame.GetNewId()
    ID_INSERT_CWD = DrFrame.GetNewId()

    DrFrame.ID_INSERT_FILENAME_BASE = 1230
    DrFrame.ID_INSERT_PATH_BASE = 1330

    DrFrame.ID_INSERTMENU_BOOKMARK_MENU = 1500
    DrFrame.ID_INSERTMENU_BOOKMARK_BASE = 1530

    DrFrame.LoadPluginShortcuts('InsertMenu')

    filenamemenu = wx.Menu()
    filenamemenu.Append

    insertmenu = wx.Menu()
    insertmenu.Append(ID_INSERT_FILENAME, 'Current Document Filename')
    insertmenu.Append(ID_INSERT_FILENAME_MENU, 'Document Filename >')
    insertmenu.AppendSeparator()
    insertmenu.Append(ID_INSERT_PATH, 'Current Document Path')
    insertmenu.Append(ID_INSERT_PATH_MENU, 'Document Path >')
    insertmenu.AppendSeparator()
    insertmenu.Append(ID_INSERT_BOOKMARK, 'Bookmark >')
    insertmenu.AppendSeparator()
    insertmenu.Append(ID_INSERT_CWD, 'Current Working Directory')

    DrFrame.editmenu.InsertSeparator(9)
    DrFrame.editmenu.InsertMenu(10, ID_INSERT_MENU, "Insert", insertmenu)

    DrFrame.Bind(wx.EVT_MENU, OnInsertFilename, id=ID_INSERT_FILENAME)
    DrFrame.Bind(wx.EVT_MENU, OnViewFileMenu, id=ID_INSERT_FILENAME_MENU)

    DrFrame.Bind(wx.EVT_MENU, OnInsertPath, id=ID_INSERT_PATH)
    DrFrame.Bind(wx.EVT_MENU, OnViewPathMenu, id=ID_INSERT_PATH_MENU)

    DrFrame.Bind(wx.EVT_MENU, OnViewBookmarkMenu, id=ID_INSERT_BOOKMARK)

    DrFrame.Bind(wx.EVT_MENU, OnInsertCWD, id=ID_INSERT_CWD)

    DrFrame.AddPluginFunction("InsertMenu", "Insert Current Document Filename", OnInsertFilename)
    DrFrame.AddPluginFunction("InsertMenu", "Insert Document Filename >", OnViewFileMenu)
    DrFrame.AddPluginFunction("InsertMenu", "Insert Current Document Path", OnInsertPath)
    DrFrame.AddPluginFunction("InsertMenu", "Insert Document Path >", OnViewPathMenu)
    DrFrame.AddPluginFunction("InsertMenu", "Insert Bookmark >", OnViewBookmarkMenu)
    DrFrame.AddPluginFunction("InsertMenu", "Insert Current Working Directory", OnInsertCWD)

