<scriptlet>
<implements type="Automation" id="dispatcher">
	<property name="PluginEvent">
		<get/>
	</property>
	<property name="PluginDescription">
		<get/>
	</property>
	<property name="PluginFileFilters">
		<get/>
	</property>
	<property name="PluginIsAutomatic">
		<get/>
	</property>
	<method name="UnpackFile"/>
	<method name="PackFile"/>
	<method name="IsFolder"/>
	<method name="UnpackFolder"/>
	<method name="PackFolder"/>
	<method name="ShowSettingsDialog"/>
</implements>

<script language="VBS">

'/////////////////////////////////////////////////////////////////////////////
'    This is a plugin for WinMerge.
'    It will display the text content of MS Word files.
'    Copyright (C) 2008-2014 Takashi Sawanaka
'
'    This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
'

Option Explicit

Const RegKeyPath = "HKCU\Software\Thingamahoochie\WinMerge\Plugins\CompareMSWordFiles.sct\"
Dim MsgCannotGetMacros
MsgCannotGetMacros = "Cannot get Macros." & vbCrLf & _
	"   To allow WinMerge to compare macros, use MS Office to alter the settings in the Macro Security for the current application." & vbCrLf & _
	"   The Trust access to Visual Basic Project feature should be turned on to use this feature in WinMerge." & vbCrLf

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim wsh: Set wsh = CreateObject("WScript.Shell")

Function isAccessibleVBAProject(doc)
	Dim count
	count = -1
	On Error Resume Next
	count = doc.VBProject.VBComponents.Count
	isAccessibleVBAProject = (count > 0)
End Function

Function regRead(Key, DefaultValue)
	regRead = DefaultValue
	On Error Resume Next
	regRead = wsh.RegRead(Key)
End Function

Function writeObjectProperties(fo, items)
	On Error Resume Next
	Dim o
	For Each o In items
		fo.WriteLine o.Name & ": " & o.Value
	Next
End Function

Function writeBookmarks(fo, items)
	On Error Resume Next
	Dim o
	For Each o In items
		fo.WriteLine o.Name & ": " & o.Start
	Next
End Function

Sub ungroupShapes(doc)
    On Error Resume Next
    Dim cnt, shp
    Do
        cnt = doc.Shapes.Count
        For Each shp In doc.Shapes
            shp.ungroup
        Next
    Loop While cnt <> doc.Shapes.Count
End Sub

Function writeTextsInShapes(fo, doc)
	Dim shp
	On Error Resume Next
	For Each shp In doc.Shapes
		fo.WriteLine shp.Name & ": " & shp.TextFrame.TextRange.Text
	Next
End Function

Function getModuleExtension(cmp)
	Select Case cmp.Type
	Case 2
		getModuleExtension = ".cls"
	Case 3
		getModuleExtension = ".frm"
	Case Else
		getModuleExtension = ".bas"
	End Select
End Function

Function get_PluginEvent()
	get_PluginEvent = "FILE_FOLDER_PACK_UNPACK"
End Function

Function get_PluginDescription()
	get_PluginDescription = "Display the text content of MS Word files"
End Function

Function get_PluginFileFilters()
	get_PluginFileFilters = "\.doc(\..*)?$;\.dot(\..*)?$;\.docx(\..*)?$;\.docm(\..*)?$;\.dotx(\..*)?$;\.dotm(\..*)?$"
End Function

Function get_PluginIsAutomatic()
	get_PluginIsAutomatic = True
End Function

Function UnpackFile(fileSrc, fileDst, pbChanged, pSubcode)
	Dim fo
	Dim wd 
	Dim doc
	Dim cmp

	Set fo = fso.CreateTextFile(fileDst, True, True)

	Set wd = CreateObject("Word.Application")
	wd.DisplayAlerts = False

	Set doc = wd.Documents.Open(fileSrc)

	On Error Resume Next

	If regRead(RegKeyPath & "CompareDocumentProperties", False) Then
		fo.WriteLine "[Document Properties]"
		writeObjectProperties fo, doc.BuiltinDocumentProperties
		fo.WriteLine ""
	End If

	If regRead(RegKeyPath & "CompareBookmarks", True) Then
		fo.WriteLine "[Bookmarks]"
		writeBookmarks fo, doc.Bookmarks
		fo.WriteLine ""
	End If

	If regRead(RegKeyPath & "CompareTextContents", True) Then
		fo.WriteLine "[Text Contents]"
		fo.Write doc.Content.Text
		fo.WriteLine ""
	End If

	If regRead(RegKeyPath & "CompareTextsInShapes", True) Then
		fo.WriteLine "[Shapes]"
		ungroupShapes doc
		writeTextsInShapes fo, doc
		fo.WriteLine ""
	End If

	If regRead(RegKeyPath & "CompareVBAMacros", True) Then
		If Not isAccessibleVBAProject(doc) Then
			fo.WriteLine MsgCannotGetMacros
		End If
		For Each cmp In doc.VBProject.VBComponents
			fo.WriteLine "[CodeModule." & cmp.Name & "]"
			If cmp.CodeModule.CountOfLines > 0 Then
				fo.WriteLine cmp.CodeModule.Lines(1, cmp.CodeModule.CountOfLines)
			End If
			fo.WriteLine ""
		Next
	End If

	doc.Saved = True
	doc.Close
	Set doc = Nothing
	wd.Quit
	Set wd = Nothing

	fo.Close
	Set fo = Nothing

	pbChanged = True
	pSubcode = 0
	UnpackFile = True

End Function

Function PackFile(fileSrc, fileDst, pbChanged, pSubcode)
	PackFile = False
End Function

Function IsFolder(file)
	IsFolder = regRead(RegKeyPath & "UnpackToFolder", False)
End Function

Function UnpackFolder(fileSrc, folderDst, pbChanged, pSubcode)
	Dim fo
	Dim wd
	Dim doc
	Dim cmp

	If Not fso.FolderExists(folderDst) Then fso.CreateFolder folderDst

	Set wd = CreateObject("Word.Application")
	wd.DisplayAlerts = False

	Set doc = wd.Documents.Open(fileSrc)

	On Error Resume Next

	If regRead(RegKeyPath & "CompareDocumentProperties", False) Then
		Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "(0)DocumentProperties.txt"), True, True)
		writeObjectProperties fo, doc.BuiltinDocumentProperties
		fo.Close
	End If

	If regRead(RegKeyPath & "CompareBookmarks", True) Then
		Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "(0)Bookmarks.txt"), True, True)
		writeBookmarks fo, doc.Bookmarks
		fo.Close
	End If

	If regRead(RegKeyPath & "CompareTextContents", True) Then
		Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "Document.txt"), True, True)
		fo.Write doc.Content.Text
		fo.Close
	End If

	If regRead(RegKeyPath & "CompareTextsInShapes", True) Then
		Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "Shapes.txt"), True, True)
		ungroupShapes doc
		writeTextsInShapes fo, doc
		fo.Close
	End If

	If regRead(RegKeyPath & "CompareDocumentsAsHTML", True) Then
		doc.SaveAs fso.BuildPath(folderDst, "Document.htm"), 10 ' wdFormatFilteredHTML
	End If
	If regRead(RegKeyPath & "CompareVBAMacros", True) Then
		If Not isAccessibleVBAProject(doc) Then
			Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "CannotGetMacros.bas"), True, True)
			fo.WriteLine MsgCannotGetMacros
			fo.Close
		End If
		
		For Each cmp In doc.VBProject.VBComponents
			cmp.Export fso.BuildPath(folderDst, cmp.Name & getModuleExtension(cmp))
		Next
	End If

	doc.Close
	Set doc = Nothing
	wd.Quit
	Set wd = Nothing
	Set fo = Nothing

	pbChanged = True
	pSubcode = 0
	UnpackFolder = True
End Function

Function PackFolder(fileSrc, folderDst, pbChanged, pSubcode)
	PackFolder = False
End Function

Function ShowSettingsDialog()
	Dim tname: tname = fso.BuildPath(fso.GetSpecialFolder(2), fso.GetTempName() & ".hta")
	Dim tfile: Set tfile = fso.CreateTextFile(tname)
	tfile.Write getResource("dialog1")
	tfile.Close
	Run wsh, "mshta.exe """ & tname & """"
	fso.DeleteFile tname 
End Function

Sub Run(sh, cmd)
	sh.Run cmd, 1, True
End Sub

</script>

<resource id="dialog1">
<![CDATA[
<html>
  <head>
    <title>CompareMSWordFiles.sct WinMerge Plugin Options</title>
    <meta content="text/html" charset="Shift_JIS">
    <style>
      body { background-color: lightgray; }
      ul { list-style:none; }
    </style>
    <script type="text/javascript">
      var REGKEY_PATH = "HKCU\\Software\\Thingamahoochie\\WinMerge\\Plugins\\CompareMSWordFiles.sct\\";

      function regRead(key, defaultValue) {
        try {
          return (new ActiveXObject("WScript.Shell")).RegRead(key);
        } catch (e) {
          return defaultValue;
        }
      }

      function regWrite(key, value, type) {
        (new ActiveXObject("WScript.Shell")).RegWrite(key, value, type);
      }

      function onload() {

        var w = 600, h = 400;
        window.resizeTo(w, h);
        window.moveTo((screen.width - w) / 2, (screen.height - h) / 2);

        cboLanguage.selectedIndex = navigator.browserLanguage.substr(0, 2) === 'ja' ? 1 : 0;
        setLanguage(navigator.browserLanguage);
        chkUnpackToFolder.checked = regRead(REGKEY_PATH + "UnpackToFolder", false);
        chkCompareDocumentProperties.checked = regRead(REGKEY_PATH + "CompareDocumentProperties", false);
        chkCompareBookmarks.checked = regRead(REGKEY_PATH + "CompareBookmarks", true);
        chkCompareTextContents.checked = regRead(REGKEY_PATH + "CompareTextContents", true);
        chkCompareDocumentsAsHTML.checked = regRead(REGKEY_PATH + "CompareDocumentsAsHTML", true);
        chkCompareTextsInShapes.checked = regRead(REGKEY_PATH + "CompareTextsInShapes", true);
        chkCompareVBAMacros.checked = regRead(REGKEY_PATH + "CompareVBAMacros", true);
        chkUnpackToFolder_onclick();
        chkCompareDocumentsAsHTML_onclick();
      }

      function setLanguage(lang) {
        var div = document.getElementById("language") || document.createElement('div')
        div.id = "language";
        var html = '&shy;<style>';
        if (lang.substr(0, 2).toLowerCase() === 'ja') {
          html += '.en { display: none } .ja { display: inline }';
        } else {
          html += '.ja { display: none } .en { display: inline }';
        }
        div.innerHTML = html;
        if (!document.getElementById("language"))
          document.body.appendChild(div);
      }

      function cboLanguage_onclick() {
        setLanguage(cboLanguage.options[cboLanguage.selectedIndex].text);
      }

      function chkUnpackToFolder_onclick() {
        if (!chkUnpackToFolder.checked)
          chkCompareDocumentsAsHTML.checked = false;
      }

      function chkCompareDocumentsAsHTML_onclick() {
        if (chkCompareDocumentsAsHTML.checked)
          chkUnpackToFolder.checked = true;
      }

      function btnOk_onclick() {
        regWrite(REGKEY_PATH + "UnpackToFolder", chkUnpackToFolder.checked, "REG_DWORD");
        regWrite(REGKEY_PATH + "CompareDocumentProperties", chkCompareDocumentProperties.checked, "REG_DWORD");
        regWrite(REGKEY_PATH + "CompareBookmarks", chkCompareBookmarks.checked, "REG_DWORD");
        regWrite(REGKEY_PATH + "CompareTextContents", chkCompareTextContents.checked, "REG_DWORD");
        regWrite(REGKEY_PATH + "CompareDocumentsAsHTML", chkCompareDocumentsAsHTML.checked, "REG_DWORD");
        regWrite(REGKEY_PATH + "CompareTextsInShapes", chkCompareTextsInShapes.checked, "REG_DWORD");
        regWrite(REGKEY_PATH + "CompareVBAMacros", chkCompareVBAMacros.checked, "REG_DWORD"); window.close();
      }

      function btnCancel_onclick() {
        window.close();
      }

    </script>
  </head>
  <body onload="onload();">
    <div>
      <ul>
        <li>
          <label class="en">Language: </label>
          <label class="ja">言語: </label>
          <select id="cboLanguage" onclick="cboLanguage_onclick();" >
            <option value="English">English</option>
            <option value="Japanese">Japanese</option>
          </select>
        </li>
      </ul>
      <ul>
        <li>
          <input id="chkUnpackToFolder" type="checkbox" onclick="chkUnpackToFolder_onclick();"/>
          <label class="en">Extract document data to multiple files </label>
          <label class="ja">ドキュメントの情報を複数ファイルに展開する </label>
        </li>
        <li>
          <input id="chkCompareDocumentProperties" type="checkbox" />
          <label class="en">Compare document properties </label>
          <label class="ja">ドキュメントプロパティを比較する </label>
        </li>
        <li>
          <input id="chkCompareBookmarks" type="checkbox" />
          <label class="en">Compare bookmarks </label>
          <label class="ja">ブックマークの定義を比較する </label>
        </li>
        <li>
          <input id="chkCompareTextContents" type="checkbox" />
          <label class="en">Compare text contents of documents </label>
          <label class="ja">ドキュメント内のテキスト部を比較する </label>
        </li>
        <li>
          <input id="chkCompareDocumentsAsHTML" type="checkbox" onclick="chkCompareDocumentsAsHTML_onclick();"/>
          <label class="en">Compare documents as HTML file (very slow)</label>
          <label class="ja">ドキュメントをHTMLファイル化して比較する (処理時間増大)</label>
        </li>
        <li>
          <input id="chkCompareTextsInShapes" type="checkbox" />
          <label class="en">Compare texts in shapes </label>
          <label class="ja">図形内のテキストを比較する </label>
        </li>
        <li>
          <input id="chkCompareVBAMacros" type="checkbox" />
          <label class="en">Compare VBA macros </label>
          <label class="ja">VBA マクロを比較する </label>
        </li>
      </ul>
      <ul>
        <li>
          <input class="en" type="button" onclick="btnOk_onclick();" value="OK" />
          <input class="en" type="button" onclick="btnCancel_onclick();" value="Cancel" />
          <input class="ja" type="button" onclick="btnOk_onclick();" value="OK" />
          <input class="ja" type="button" onclick="btnCancel_onclick();" value="キャンセル" />
        </li>
      </ul>
    </div>
  </body>
</html>
]]>
</resource>

</scriptlet>
