#   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

#Plugin
#Switcheroo

'''
1.1.0:
Updates for 3.10.6
Dialog fixes/tweaks.
Now works in the prompt as well.
Moved html into a text dialog.

For Version 1.0.4:
Changes:
Updated install/uninstall, toolbar for drpython 3.3.0

For Version 1.0.3:
Changes:
Updated Find History to use new drFindTextControl method.

For Version: 1.0.2:
Changes:
Added History Support.
Added Menu Button.

For Version: 1.0.1:
Changes:
Updates for drFindTextCtrl
Add to Search Menu instead of Edit Menu
'''

import wx
import wx.lib.dialogs
from drFindReplaceDialog import drFindTextCtrl

def OnAbout(DrFrame):
    AboutString = '''Version: 1.1.0

By Daniel Pozmanter
Released under the GPL.'''
    DrFrame.ShowMessage(AboutString, "About Switcheroo")

def OnHelp(DrFrame):
    HelpString = '''Switcheroo:

Switcheroo is like replace, except instead of replacing "Text A" with "Text B",
it replaces every instance of "Text A" with "Text B",
and every instance of "Text B" with "Text A".

So using Switcheroo and selecting:

Switch:   Dog
With:     Cat

on the following text:

"The Dog chased the Cat, and the Cat bit back."

Will yield:
"The Cat chased the Dog, and the Dog bit back."
'''

    DrFrame.ShowMessage(HelpString, "Switcheroo Help")

def UnInstall(DrFrame):
    plugindir = DrFrame.prefs.pluginsdirectory
    if os.path.exists(plugindir + "/bitmaps/16/Switcheroo.png"):
        os.remove(plugindir + "/bitmaps/16/Switcheroo.png")
    if os.path.exists(plugindir + "/bitmaps/24/Switcheroo.png"):
        os.remove(plugindir + "/bitmaps/24/Switcheroo.png")

    DrFrame.RemovePluginIcon("Switcheroo")

    return True

class drSwitcherooDialog(wx.Dialog):
    def __init__(self, parent, id, title, stc):
        wx.Dialog.__init__(self, parent, id, title, wx.Point(50, 50), wx.Size(375, 275), wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.THICK_FRAME | wx.RESIZE_BORDER)

        self.stc = stc

        self.ID_SWITCH = 3001
        self.ID_CANCEL = 3002

        self.ID_CHK_REGEX = 1010
        self.ID_CREATERE_A = 1011
        self.ID_CREATERE_B = 1012

        self.ID_BTNA = 98
        self.ID_BTNB = 99

        self.parent = parent

        self.txtA = drFindTextCtrl(self, id, "", wx.DefaultPosition, wx.Size(175, -1), self.OnbtnSwitch)
        self.txtA.SetFocus()
        self.txtB = drFindTextCtrl(self, id, "", wx.DefaultPosition, wx.Size(175, -1), self.OnbtnSwitch)

        self.txtA.SetHistory(parent.SwitcherooHistory)

        self.txtB.SetHistory(parent.SwitcherooHistory)

        self.btnPopUpA = wx.Button(self, self.ID_BTNA, " Menu ")
        self.btnPopUpB = wx.Button(self, self.ID_BTNB, " Menu ")

        self.chkCase = wx.CheckBox(self, id, "&Match Case")
        self.chkWholeWord = wx.CheckBox(self, id, "Match &Whole Word")
        self.chkInSelection = wx.CheckBox(self, id, "In Selection")

        #Prefs
        self.chkCase.SetValue(parent.prefs.findreplacematchcase)
        self.chkWholeWord.SetValue(parent.prefs.findreplacewholeword)
        self.chkInSelection.SetValue(parent.prefs.findreplaceinselection)

        self.btnSwitch = wx.Button(self, self.ID_SWITCH, "&Switch All")
        self.btnSwitch.SetDefault()
        self.btnCancel = wx.Button(self, self.ID_CANCEL, "&Cancel")

        self.theSizer = wx.FlexGridSizer(8, 3, 5, 10)

        #Size Buffer
        self.theSizer.Add(wx.StaticText(self, -1, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, -1, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, -1, "    "), 1, wx.SHAPED)

        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, id, "Switch: "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)
        self.theSizer.Add(self.txtA, 1, wx.SHAPED)
        self.theSizer.Add(self.btnPopUpA, 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, id, "With: "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)
        self.theSizer.Add(self.txtB, 1, wx.SHAPED)
        self.theSizer.Add(self.btnPopUpB, 1, wx.SHAPED)

        #Size Buffer
        self.theSizer.Add(wx.StaticText(self, -1, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, -1, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, -1, "    "), 1, wx.SHAPED)

        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)
        self.theSizer.Add(self.chkCase, 1, wx.SHAPED)
        self.theSizer.Add(self.chkWholeWord, 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)
        self.theSizer.Add(self.chkInSelection, 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)

        #Size Buffer
        self.theSizer.Add(wx.StaticText(self, -1, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, -1, "    "), 1, wx.SHAPED)
        self.theSizer.Add(wx.StaticText(self, -1, "    "), 1, wx.SHAPED)

        self.theSizer.Add(wx.StaticText(self, id, "    "), 1, wx.SHAPED)
        self.theSizer.Add(self.btnSwitch, 1, wx.SHAPED)
        self.theSizer.Add(self.btnCancel, 1, wx.SHAPED)

        self.SetAutoLayout(True)
        self.SetSizer(self.theSizer)
        self.Bind(wx.EVT_BUTTON,  self.OnbtnSwitch, id=self.ID_SWITCH)
        self.Bind(wx.EVT_BUTTON,  self.OnbtnCancel, id=self.ID_CANCEL)
        self.Bind(wx.EVT_BUTTON,  self.OnbtnPopUp, id=self.ID_BTNA)
        self.Bind(wx.EVT_BUTTON,  self.OnbtnPopUp, id=self.ID_BTNB)

        self.textA = ""
        self.textB = ""
        self.flags = wx.FR_DOWN

    def OnbtnPopUp(self, event):
        eid = event.GetId()
        if eid == self.ID_BTNA:
            s = self.txtA.GetPosition()[0]
            x = self.btnPopUpA.GetPosition()[0]
            self.txtA.PopUp((x-s, 0))
        elif eid == self.ID_BTNB:
            s = self.txtB.GetPosition()[0]
            x = self.btnPopUpB.GetPosition()[0]
            self.txtB.PopUp((x-s, 0))

    def OnbtnSwitch(self, event):
        self.Show(0)
        a = self.txtA.GetValue()
        b = self.txtB.GetValue()
        self.txtA.AppendToHistory(self.parent.SwitcherooHistory)
        self.txtB.AppendToHistory(self.parent.SwitcherooHistory)
        if self.chkCase.IsChecked():
            self.flags = self.flags + wx.FR_MATCHCASE
        if self.chkWholeWord.IsChecked():
            self.flags = self.flags + wx.FR_WHOLEWORD
        temp = "DRPYTHONTEMPSWITCHEROO"
        if (len(a) > 0) and (len(b) > 0):
            if self.chkInSelection.GetValue():
                start = self.stc.GetSelectionStart()
                end = self.stc.GetSelectionEnd()

                self.stc.Finder.SetTargetRange(start, end)
                x = self.stc.Finder.ReplaceAll(a, temp, self.flags)
                end = end + ((len(temp) - len(a)) * x)
                self.stc.Finder.SetTargetRange(start, end)
                x = self.stc.Finder.ReplaceAll(b, a, self.flags)
                end = end + ((len(a) - len(b)) * x)
                self.stc.Finder.SetTargetRange(start, end)
                self.stc.Finder.ReplaceAll(temp, b, self.flags)
            else:
                self.stc.Finder.SetTargetRange(0, self.stc.GetTextLength())
                self.stc.Finder.ReplaceAll(a, temp, self.flags)
                self.stc.Finder.SetTargetRange(0, self.stc.GetTextLength())
                self.stc.Finder.ReplaceAll(b, a, self.flags)
                self.stc.Finder.SetTargetRange(0, self.stc.GetTextLength())
                self.stc.Finder.ReplaceAll(temp, b, self.flags)
        self.Close(1)

    def OnbtnCancel(self, event):
        self.Close(1)

def Plugin(DrFrame):
    def OnSwitcheroo(event):
        d = drSwitcherooDialog(DrFrame, -1, "Switcheroo", DrFrame.GetActiveSTC())
        d.Show()

    ID_SWITCHEROO = DrFrame.GetNewId()

    DrFrame.SwitcherooHistory = []

    DrFrame.LoadPluginShortcuts('Switcheroo')
    DrFrame.searchmenu.AppendSeparator()
    DrFrame.searchmenu.Append(ID_SWITCHEROO, "Switcheroo...", " Switch all occurences of A with B, and B with A")

    DrFrame.Bind(wx.EVT_MENU, OnSwitcheroo, id=ID_SWITCHEROO)

    DrFrame.AddPluginFunction("Switcheroo", "Switcheroo", OnSwitcheroo)
