Skip to content

Commit

Permalink
Merged PR 155: V3.0.23 Release 10
Browse files Browse the repository at this point in the history
* Allow import of 1D chemical object (no atoms only labels).
* Fix conversion of 1D chemical object (no atoms only labels).
* Force ChemDoodle Web 8.0.0 if IE browser version is less than 10.
* Fix memory leak in ChemDoodle Web 8.0.0 Editor.

Related work items: #389
  • Loading branch information
MikeWilliams-UK committed Nov 13, 2018
2 parents ebf283b + b0a525b commit 7c9fa0a
Show file tree
Hide file tree
Showing 52 changed files with 479 additions and 248 deletions.
3 changes: 3 additions & 0 deletions src/.nuget/NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<activePackageSource>
<add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
</activePackageSource>
</configuration>
162 changes: 125 additions & 37 deletions src/Chem4Word.V3/Chem4WordV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public partial class Chem4WordV3

public bool ChemistryAllowed = false;
public string ChemistryProhibitedReason = "";
private string _lastContentControlAdded = "";

private bool ChemistrySelected = false;
private bool _chemistrySelected = false;
private bool _markAsChemistryHandled = false;
private int _rightClickEvents = 0;

Expand Down Expand Up @@ -337,8 +338,28 @@ public void LoadOptions()
string betaValue = Globals.Chem4WordV3.ThisVersion.Root?.Element("IsBeta")?.Value;
bool isBeta = betaValue != null && bool.Parse(betaValue);

// Re-Initiallize Telemetry with granted permissions
// Re-Initialize Telemetry with granted permissions
Telemetry = new TelemetryWriter(isBeta || SystemOptions.TelemetryEnabled);

try
{
if (SystemOptions.SelectedEditorPlugIn.Equals(Constants.DefaultEditorPlugIn800))
{
var browser = new WebBrowser().Version;
if (browser.Major < Constants.ChemDoodleWeb800MinimumBrowserVersion)
{
SystemOptions.SelectedEditorPlugIn = Constants.DefaultEditorPlugIn702;
string temp = JsonConvert.SerializeObject(SystemOptions, Formatting.Indented);
Globals.Chem4WordV3.Telemetry.Write(module, "Information", "IE 10+ not detected; Switching to ChemDoodle Web 7.0.2");
File.WriteAllText(optionsFile, temp);
}
}
}
catch
{
//
}

}
catch (Exception ex)
{
Expand Down Expand Up @@ -718,13 +739,45 @@ public void EnableDocumentEvents(Word.Document doc)
// Remember to add corresponding code in DisableDocumentEvents()

// ContentControlOnEnter Event Handler
wdoc.ContentControlOnEnter += OnContentControlOnEnter;
try
{
wdoc.ContentControlOnEnter -= OnContentControlOnEnter;
wdoc.ContentControlOnEnter += OnContentControlOnEnter;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
// ContentControlOnExit Event Handler
wdoc.ContentControlOnExit += OnContentControlOnExit;
try
{
wdoc.ContentControlOnExit -= OnContentControlOnExit;
wdoc.ContentControlOnExit += OnContentControlOnExit;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
// ContentControlBeforeDelete Event Handler
wdoc.ContentControlBeforeDelete += OnContentControlBeforeDelete;
try
{
wdoc.ContentControlBeforeDelete -= OnContentControlBeforeDelete;
wdoc.ContentControlBeforeDelete += OnContentControlBeforeDelete;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
// ContentControlAfterAdd Event Handler
wdoc.ContentControlAfterAdd += OnContentControlAfterAdd;
try
{
wdoc.ContentControlAfterAdd -= OnContentControlAfterAdd;
wdoc.ContentControlAfterAdd += OnContentControlAfterAdd;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}

EventsEnabled = true;
}
Expand All @@ -747,13 +800,41 @@ public void DisableDocumentEvents(Word.Document doc)
// Remember to add corresponding code in EnableDocumentEvents()

// ContentControlOnEnter Event Handler
wdoc.ContentControlOnEnter -= OnContentControlOnEnter;
try
{
wdoc.ContentControlOnEnter -= OnContentControlOnEnter;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
// ContentControlOnExit Event Handler
wdoc.ContentControlOnExit -= OnContentControlOnExit;
try
{
wdoc.ContentControlOnExit -= OnContentControlOnExit;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
// ContentControlBeforeDelete Event Handler
wdoc.ContentControlBeforeDelete -= OnContentControlBeforeDelete;
try
{
wdoc.ContentControlBeforeDelete -= OnContentControlBeforeDelete;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
// ContentControlAfterAdd Event Handler
wdoc.ContentControlAfterAdd -= OnContentControlAfterAdd;
try
{
wdoc.ContentControlAfterAdd -= OnContentControlAfterAdd;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}

private void SetButtonStates(ButtonState state)
Expand Down Expand Up @@ -885,7 +966,7 @@ public void SelectChemistry(Word.Selection sel)
SetButtonStates(ButtonState.CanInsert);
}

ChemistrySelected = chemistrySelected;
_chemistrySelected = chemistrySelected;
}
catch (Exception e)
{
Expand Down Expand Up @@ -2124,7 +2205,7 @@ private void OnWindowBeforeDoubleClick(Word.Selection sel, ref bool cancel)
Debug.WriteLine($"{module.Replace("()", $"({sel.Document.Name})")}");
Debug.WriteLine(" Selection: from " + sel.Range.Start + " to " + sel.Range.End);

if (EventsEnabled && ChemistrySelected)
if (EventsEnabled && _chemistrySelected)
{
CustomRibbon.PerformEdit();
}
Expand Down Expand Up @@ -2195,39 +2276,46 @@ private void OnContentControlAfterAdd(Word.ContentControl NewContentControl, boo
LoadOptions();
}

if (!InUndoRedo && !string.IsNullOrEmpty(NewContentControl?.Tag))
string ccId = NewContentControl.ID;
string ccTag = NewContentControl.Tag;
if (!InUndoRedo && !string.IsNullOrEmpty(ccTag))
{
string message = $"ContentControl {NewContentControl?.ID} added; Looking for structure {NewContentControl?.Tag}";
Debug.WriteLine(" " + message);
Telemetry.Write(module, "Information", message);

Word.Document doc = NewContentControl.Application.ActiveDocument;
Word.Application app = Globals.Chem4WordV3.Application;
CustomXMLPart cxml = CustomXmlPartHelper.GetCustomXmlPart(NewContentControl?.Tag, app.ActiveDocument);
if (cxml != null)
{
Telemetry.Write(module, "Information", "Found copy of " + NewContentControl?.Tag + " in this document.");
}
else
if (!ccId.Equals(_lastContentControlAdded))
{
if (doc.Application.Documents.Count > 1)
string message = $"ContentControl {ccId} added; Looking for structure {ccTag}";
Debug.WriteLine(" " + message);
Telemetry.Write(module, "Information", message);

Word.Document doc = NewContentControl.Application.ActiveDocument;
Word.Application app = Globals.Chem4WordV3.Application;
CustomXMLPart cxml = CustomXmlPartHelper.GetCustomXmlPart(ccTag, app.ActiveDocument);
if (cxml != null)
{
Telemetry.Write(module, "Information", "Found copy of " + ccTag + " in this document.");
}
else
{
Word.Application app1 = Globals.Chem4WordV3.Application;
cxml = CustomXmlPartHelper.FindCustomXmlPart(NewContentControl?.Tag, app1.ActiveDocument);
if (cxml != null)
if (doc.Application.Documents.Count > 1)
{
Telemetry.Write(module, "Information", "Found copy of " + NewContentControl?.Tag + " in other document, adding it into this.");
Word.Application app1 = Globals.Chem4WordV3.Application;
cxml = CustomXmlPartHelper.FindCustomXmlPart(ccTag, app1.ActiveDocument);
if (cxml != null)
{
Telemetry.Write(module, "Information", "Found copy of " + ccTag + " in other document, adding it into this.");

// Generate new molecule Guid and apply it
string newGuid = Guid.NewGuid().ToString("N");
NewContentControl.Tag = newGuid;
// Generate new molecule Guid and apply it
string newGuid = Guid.NewGuid().ToString("N");
NewContentControl.Tag = newGuid;

CMLConverter cmlConverter = new CMLConverter();
Model.Model model = cmlConverter.Import(cxml.XML);
model.CustomXmlPartGuid = newGuid;
doc.CustomXMLParts.Add(cmlConverter.Export(model));
CMLConverter cmlConverter = new CMLConverter();
Model.Model model = cmlConverter.Import(cxml.XML);
model.CustomXmlPartGuid = newGuid;
doc.CustomXMLParts.Add(cmlConverter.Export(model));
}
}
}

_lastContentControlAdded = ccId;
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/Chem4Word.V3/Data/Chem4Word-Versions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
<!-- This file must be uploaded to https://www.chem4word.co.uk/files3/ folder as Chem4Word-Versions.xml -->
<ChangeLog>
<Id>f3c4f4db-2fff-46db-b14a-feb8e09f7742</Id>
<Version>
<Number>3.0.23 Release 10</Number>
<IsBeta>false</IsBeta>
<Released>13-Nov-2018</Released>
<Changes>
<Change>Allow import of 1D chemical object (no atoms only labels)</Change>
<Change>Fix conversion of 1D chemical object (no atoms only labels)</Change>
<Change>Force ChemDoodle Web 8.0.0 if IE browser version is less than 10.</Change>
<Change>Fix memory leak in ChemDoodle Web 8.0.0 Editor.</Change>
<Change>Please download Chem4Word-Setup.exe from https://www.chem4word.co.uk/download/ if you encounter any difficulties updating.</Change>
</Changes>
<Url>https://www.chem4word.co.uk/files3/Chem4Word-Setup.3.0.23.Release.10.msi</Url>
</Version>
<Version>
<Number>3.0.22 Release 9</Number>
<IsBeta>false</IsBeta>
Expand All @@ -11,7 +24,6 @@
<Change>Changes to UTC system time detection.</Change>
<Change>Allow structure with No atoms</Change>
<Change>Set Default editor to ChemDoodle Web 7.0.2 for new users if Windows 7 is detected</Change>
<Change>Please download Chem4Word-Setup.exe from https://www.chem4word.co.uk/download/ if you encounter any difficulties updating.</Change>
</Changes>
<Url>https://www.chem4word.co.uk/files3/Chem4Word-Setup.3.0.22.Release.9.msi</Url>
</Version>
Expand Down
4 changes: 2 additions & 2 deletions src/Chem4Word.V3/Data/This-Version.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Version>
<Number>3.0.22 Release 9</Number>
<Number>3.0.23 Release 10</Number>
<IsBeta>false</IsBeta>
<Released>31-Oct-2018</Released>
<Released>13-Nov-2018</Released>
</Version>
4 changes: 4 additions & 0 deletions src/Chem4Word.V3/Data/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ <h1>Chemistry for Word Add-In V3</h1>
<td>Setup Bootstrapper</td>
<td><a href="/files3/Chem4Word-Setup.exe">Chem4Word-Setup</a></td>
</tr>
<tr>
<td>Version 3.0.23 - Release 10</td>
<td><a href="/files3/Chem4Word-Setup.3.0.23.Release.10.msi">Chem4Word-Setup 3.0.23 Release 10</a></td>
</tr>
<tr>
<td>Version 3.0.22 - Release 9</td>
<td><a href="/files3/Chem4Word-Setup.3.0.22.Release.9.msi">Chem4Word-Setup 3.0.22 Release 9</a></td>
Expand Down
2 changes: 1 addition & 1 deletion src/Chem4Word.V3/Database/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,4 +706,4 @@ public class UserTagDTO
public string Text { get; set; }
public long Lock { get; set; }
}
}
}
76 changes: 53 additions & 23 deletions src/Chem4Word.V3/Helpers/Upgrader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Chem4Word.Model;
using Chem4Word.Model.Converters.CML;
using Word = Microsoft.Office.Interop.Word;

Expand Down Expand Up @@ -181,29 +182,56 @@ public static void DoUpgrade(Word.Document doc)
{
if (cc.ID.Equals(cci.Id))
{
if (cci.Type.Equals("2D"))
{
cc.LockContents = false;
cc.Title = Constants.ContentControlTitle;
cc.Tag = target.Model.CustomXmlPartGuid;
cc.LockContents = true;
int start;
bool isFormula;
string source;
string text;

// ToDo: Regenerate converted 2D structures
}
else
switch (cci.Type)
{
cc.LockContents = false;
cc.Range.Delete();
int start = cc.Range.Start;
cc.Delete();

doc.Application.Selection.SetRange(start - 1, start - 1);
bool isFormula = false;
string source;
string text = ChemistryHelper.GetInlineText(target.Model, cci.Type, ref isFormula, out source);
Word.ContentControl ccn = doc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref _missing);
ChemistryHelper.Insert1D(ccn, text, isFormula, $"{cci.Type}:{target.Model.CustomXmlPartGuid}");
ccn.LockContents = true;
case "2D":
cc.LockContents = false;
cc.Title = Constants.ContentControlTitle;
cc.Tag = target.Model.CustomXmlPartGuid;
cc.LockContents = true;

// ToDo: Regenerate converted 2D structures
break;

case "new":
cc.LockContents = false;
cc.Range.Delete();
start = cc.Range.Start;
cc.Delete();

doc.Application.Selection.SetRange(start - 1, start - 1);
var model = new Model.Model();
var molecule = new Molecule();
molecule.ChemicalNames.Add(new ChemicalName { Id = "m1.n1", Name = cci.Text, DictRef = Constants.Chem4WordUserSynonym});
model.Molecules.Add(molecule);
model.CustomXmlPartGuid = Guid.NewGuid().ToString("N");

var cmlConvertor = new CMLConverter();
doc.CustomXMLParts.Add(cmlConvertor.Export(model));

Word.ContentControl ccn = doc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref _missing);
ChemistryHelper.Insert1D(ccn, cci.Text, false, $"m1.n1:{model.CustomXmlPartGuid}");
ccn.LockContents = true;
break;

default:
cc.LockContents = false;
cc.Range.Delete();
start = cc.Range.Start;
cc.Delete();

doc.Application.Selection.SetRange(start - 1, start - 1);
isFormula = false;
text = ChemistryHelper.GetInlineText(target.Model, cci.Type, ref isFormula, out source);
Word.ContentControl ccr = doc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref _missing);
ChemistryHelper.Insert1D(ccr, text, isFormula, $"{cci.Type}:{target.Model.CustomXmlPartGuid}");
ccr.LockContents = true;
break;
}
}
}
Expand Down Expand Up @@ -373,8 +401,9 @@ private static List<UpgradeTarget> CollectData(Word.Document doc)
}
else
{
// Default to overall concise formula
cci.Type = "c0";
// Default to flagging that new 1D molecule is to be created
cci.Type = "new";
cci.Text = dv;

#region Find new style 1D code

Expand Down Expand Up @@ -446,5 +475,6 @@ public class ContentControlInfo
public int Index { get; set; }
public int Location { get; set; }
public string Type { get; set; }
public string Text { get; set; }
}
}
Loading

0 comments on commit 7c9fa0a

Please sign in to comment.