-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThisAddIn.cs
2322 lines (2199 loc) · 102 KB
/
ThisAddIn.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AUTHOR'S MEMO AND MIT LICENSE STATEMENT
// ///////////////////////////////////////
//
// Author: Haro Mherian, Ph.D. Mathematics, Computer Sciences, Linguistics, etc.
// This software was originally created in 2008. Being the only linguistically complete Armenian spell-checker
// tool (in both Classical and Reformed Orthographies), it was commercially available under "HySpell Armenian
// Spell-Checker" name until June 2017. It was then made open source in order to promote software development
// in the direction and advancement of the Armenian language.
// For further information, as well as, further linguistic tools, please contact: www.hyspell.com
//
// Copyright (c) 2017 hyspell.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice, along with author's memo, and permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Collections.Generic;
using System.Collections;
using System.Globalization;
using NHunspell;
namespace HySpell
{
enum CapState
{
enNone = 0,
enFirstCap = 1,
enAllCaps = 2,
}
public class HySpellSettings
{
private bool bMixModeView = true; // use mixed mode view by default
private int nOrthographyType = 0; // classical by default
public bool MixModeView
{
get { return bMixModeView; }
set { bMixModeView = value; }
}
public int OrthographyType
{
get { return nOrthographyType; }
set { nOrthographyType = value; }
}
}
public class ReferenceDictionary
{
private int _LinkIndex;
private string _LinkLabel = "";
private string _LinkURL = "";
private string _LinkDesc = "";
public int LinkIndex
{
get { return _LinkIndex; }
set { _LinkIndex = value; }
}
public string LinkLabel
{
get { return _LinkLabel; }
set { _LinkLabel = value; }
}
public string LinkURL
{
get { return _LinkURL; }
set { _LinkURL = value; }
}
public string LinkDescription
{
get { return _LinkDesc; }
set { _LinkDesc = value; }
}
public ReferenceDictionary() { }
public ReferenceDictionary(int nLinkIndex, string sLinkLabel, string sLinkURL)
{
_LinkIndex = nLinkIndex;
_LinkLabel = sLinkLabel;
_LinkURL = sLinkURL;
}
public ReferenceDictionary(int nLinkIndex, string sLinkLabel, string sLinkURL, string sLinkDesc)
{
_LinkIndex = nLinkIndex;
_LinkLabel = sLinkLabel;
_LinkURL = sLinkURL;
_LinkDesc = sLinkDesc;
}
}
public class ReferenceDictionaries : ICollection
{
public string CollectionName;
private ArrayList rDictArray = new ArrayList();
public ReferenceDictionary this[int index]
{
get { return (ReferenceDictionary)rDictArray[index]; }
}
public void CopyTo(Array a, int index)
{
rDictArray.CopyTo(a, index);
}
public int Count
{
get { return rDictArray.Count; }
}
public object SyncRoot
{
get { return this; }
}
public bool IsSynchronized
{
get { return false; }
}
public IEnumerator GetEnumerator()
{
return rDictArray.GetEnumerator();
}
public void Add(ReferenceDictionary newDict)
{
rDictArray.Add(newDict);
}
}
public partial class ThisAddIn
{
private string sTaskPaneName = "HySpell";
private Word.Application myApplication;
private int m_nLangId;
private Office.CommandBar commandBar;
private Office.CommandBarButton[] hsMenuControls;
private Office.CommandBarButton[] hsSubMenuControls;
private Office.CommandBarPopup hsAutoCorrectPopup;
//private Office.CommandBarPopup hsSynonymsPopup;
private Office.CommandBarButton hsMenuControlLookup;
//private Office.CommandBarButton hsMenuControlSyn;
//private Office.CommandBarButton hsMenuControlName;
//private Word.Template customTemplate;
private HSTaskPane oHSTaskPaneControl;
private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
private int nSelWordEncoding = 2; // default is unicode
private int nCurrentWordStart = 0;
private int nCurrentWordEnd = 0;
//Word.Style hs_SEStyle = null;
//
bool bShowErrors = true;
Word.Paragraphs oPars = null;
Word.Shapes oShapes = null;
Word.Words words = null;
Word.Range CurRange = null;
Word.Range SelWordRange = null;
string[] ArWords;
string sParText = "";
string sSCIIOutput = "";
string sUniInput = "";
string sUserSuggWord = "";
ArrayList arrSuggestList = null;
ArrayList arrPossibleWords = null;
bool bTerminateOrthoConvert = false;
CapState enCapState = CapState.enNone;
//string sDocContents = "";
bool bStart = true;
bool bCheckNormalText = true;
bool bCursorInShape = false;
bool bRestartPar = false;
bool bHasPrevTextBoxLink = false;
int nHit = 0;
int nCursorPos = 0;
int nParIndex = 1;
int nShapeIndex = 0;
int nParCount = 0;
int nParOffset = 0;
int nIParOffset = 0;
int nWordLen = 1;
int nRetFlag = -1;
int nStartPos = 0;
int j = 0;
private HySpellSettings m_oSettings;
private bool bInitSpellCheckCall = true;
private string sCustomDictPath = "";
private HySpellEncoder.Wrapper m_oEncoder = null;
private Hunspell m_oHunspell = null;
private Hunspell m_oHunspell1 = null;
private Hunspell m_oHunspell2 = null;
private bool m_bFromClassic = true;
public Dictionary<string, string> m_LexMapDic = null;
private Dictionary<string, string> m_FlexMapDic = null;
public HySpellSettings ProgramSettings
{
get { return m_oSettings; }
set { m_oSettings = value; }
}
public HySpellEncoder.Wrapper HSEncoder
{
get { return m_oEncoder; }
set { m_oEncoder = value; }
}
public Hunspell HSWrapper
{
get { return m_oHunspell; }
set { m_oHunspell = value; }
}
public string CustomDictPath
{
get { return sCustomDictPath; }
set { sCustomDictPath = value; }
}
public string CurrentWordOutPut
{
get { return sSCIIOutput; }
set { sSCIIOutput = value; }
}
public string CurrentWord
{
get { return sUniInput; }
set { sUniInput = value; }
}
public int CurrentWordLength
{
get { return nWordLen; }
set { nWordLen = value; }
}
public string UserSuggestedWord
{
get { return sUserSuggWord; }
set { sUserSuggWord = value; }
}
public ArrayList SuggestList
{
get { return arrSuggestList; }
set { arrSuggestList = value; }
}
public ArrayList PossibleWords
{
get { return arrPossibleWords; }
set { arrPossibleWords = value; }
}
public bool TerminateOrthoConvert
{
get { return bTerminateOrthoConvert; }
set { bTerminateOrthoConvert = value; }
}
public Word.Range CurrentSelectedRange
{
get { return SelWordRange; }
set { SelWordRange = value; }
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
m_oSettings = new HySpellSettings();
myApplication = this.Application;
m_nLangId = (int)myApplication.Language;
myApplication.WindowBeforeRightClick += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(myApplication_WindowBeforeRightClick);
myApplication.WindowSelectionChange += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler(myApplication_WindowSelectionChange);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
UnInitializeWrapper();
// remove all task-panes
RemoveTaskPanes();
// remove all HySpell custom menu items
RemoveExistingMenuItem("hs_Spelling");
RemoveExistingMenuItem("Text");
RemoveExistingMenuItem("Lists");
RemoveExistingMenuItem("Table Text");
RemoveExistingMenuItem("Table Lists");
RemoveExistingMenuItem("Hyperlink Context Menu");
RemoveExistingMenuItem("Headings");
RemoveExistingMenuItem("Footnotes");
}
static public void SerializeToXML(HySpellSettings oSettings, string sPath)
{
XmlSerializer serializer = new XmlSerializer(typeof(HySpellSettings));
TextWriter textWriter = new StreamWriter(sPath);
serializer.Serialize(textWriter, oSettings);
textWriter.Close();
}
static public HySpellSettings DeserializeFromXML(HySpellSettings oDefaultSettings, string sPath)
{
if (!File.Exists(sPath))
SerializeToXML(oDefaultSettings, sPath);
XmlSerializer deserializer = new XmlSerializer(typeof(HySpellSettings));
TextReader textReader = new StreamReader(sPath);
HySpellSettings oSettings = (HySpellSettings)deserializer.Deserialize(textReader);
textReader.Close();
return oSettings;
}
public void SerializeDictionaryCollection(string sPath)
{
ReferenceDictionaries refDicts = new ReferenceDictionaries();
refDicts.CollectionName = "ReferenceDictionaries";
ReferenceDictionary dic0 = new ReferenceDictionary(0, "Մալխասեանց (1944)", "http://www.nayiri.com/imagedDictionaryBrowser.jsp?dictionaryId=6&query=");
refDicts.Add(dic0);
ReferenceDictionary dic1 = new ReferenceDictionary(1, "Աղայան (1976)", "http://www.nayiri.com/imagedDictionaryBrowser.jsp?dictionaryId=24&query=");
refDicts.Add(dic1);
ReferenceDictionary dic2 = new ReferenceDictionary(2, "Սուքիասեան (1967)", "http://www.nayiri.com/imagedDictionaryBrowser.jsp?dictionaryId=25&query=");
refDicts.Add(dic2);
ReferenceDictionary dic3 = new ReferenceDictionary(3, "Խաչատուրեան (1992)", "http://www.nayiri.com/imagedDictionaryBrowser.jsp?dictionaryId=8&query=");
refDicts.Add(dic3);
ReferenceDictionary dic4 = new ReferenceDictionary(4, "Գույումճեան (1981)", "http://www.nayiri.com/imagedDictionaryBrowser.jsp?dictionaryId=3&query=");
refDicts.Add(dic4);
ReferenceDictionary dic5 = new ReferenceDictionary(5, "Աճառեան (1926)", "http://www.nayiri.com/imagedDictionaryBrowser.jsp?dictionaryId=7&query=");
refDicts.Add(dic5);
XmlSerializer xmlSer = new XmlSerializer(typeof(ReferenceDictionaries));
TextWriter writer = new StreamWriter(sPath);
xmlSer.Serialize(writer, refDicts);
}
public void DeSerializeDictionaryCollection(ref ReferenceDictionaries oRefDicts, string sPath)
{
if (!File.Exists(sPath))
SerializeDictionaryCollection(sPath);
XmlSerializer deserializer = new XmlSerializer(typeof(ReferenceDictionaries));
TextReader textReader = new StreamReader(sPath);
oRefDicts = (ReferenceDictionaries)deserializer.Deserialize(textReader);
textReader.Close();
}
public void SetProgramSettings()
{
//string sPath = Directory.GetParent(this.Application.Path).ToString();
string sPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string sXMLPath = sPath + @"\hyspell\HyspellSettings.xml";
SerializeToXML(m_oSettings, sXMLPath);
}
public void GetProgramSettings()
{
//string sPath = Directory.GetParent(this.Application.Path).ToString();
string sPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string sXMLPath = sPath + @"\hyspell\HyspellSettings.xml";
m_oSettings = DeserializeFromXML(m_oSettings, sXMLPath);
}
public string PutAccent(string sWord, string sAccent)
{
if (sAccent.Length > 0)
{
char[] IsArmenianUVowel = {/* UNICODE chars */
'\u0531', // ARMENIAN CAPITAL LETTER AYB
'\u0561', // ARMENIAN SMALL LETTER AYB
'\u0535', // ARMENIAN CAPITAL LETTER YECH
'\u0565', // ARMENIAN SMALL LETTER YECH
'\u0537', // ARMENIAN CAPITAL LETTER E
'\u0567', // ARMENIAN SMALL LETTER E
'\u053B', // ARMENIAN CAPITAL LETTER INI
'\u056B', // ARMENIAN SMALL LETTER INI
'\u0548', // ARMENIAN CAPITAL LETTER VO
'\u0578', // ARMENIAN SMALL LETTER VO
'\u0555', // ARMENIAN CAPITAL LETTER O
'\u0585', // ARMENIAN SMALL LETTER O
};
int nIndex = sWord.LastIndexOfAny(IsArmenianUVowel);
if (nIndex > 0)
sWord = sWord.Insert(nIndex + 1, sAccent);
}
return sWord;
}
public string DecodeAccent(string sSCIIAccent)
{
string sTemp = sSCIIAccent;
switch (sSCIIAccent.ToCharArray()[0])
{
case '\u00B1':
sTemp = '\u055E'.ToString();
break;
case '\u00B0':
sTemp = '\u055B'.ToString();
break;
case '\u00FE':
sTemp = '\u055A'.ToString();
break;
case '\u00AF':
sTemp = '\u055C'.ToString();
break;
}
return sTemp;
}
public string EncodeAccent(string sUNIAccent)
{
string sTemp = sUNIAccent;
switch (sUNIAccent.ToCharArray()[0])
{
case '\u055E':
sTemp = '\u00B1'.ToString();
break;
case '\u055B':
sTemp = '\u00B0'.ToString();
break;
case '\u055A':
sTemp = '\u00FE'.ToString();
break;
case '\u055C':
sTemp = '\u00AF'.ToString();
break;
}
return sTemp;
}
public string EncodeWithAccents(string sWord, HySpellEncoder.Wrapper oEncoder)
{
string sTemp = "";
string sStart = "";
string sAccent = "";
string sEnd = "";
int nIndex = sWord.IndexOfAny(Globals.ThisAddIn.IsArmenianApostrophe);
if (nIndex != -1)
{
sStart = sWord.Substring(0, nIndex);
sAccent = sWord.Substring(nIndex, 1);
sEnd = sWord.Substring(nIndex + 1, sWord.Length - nIndex - 1);
// encode the beginning of the word
ArrayList arrEncoded1 = new ArrayList();
int nRet = oEncoder.Encode(sStart, arrEncoded1);
if (arrEncoded1.Count != 0)
sTemp = arrEncoded1[0].ToString();
// encode the apostrophe character
sTemp += EncodeAccent(sAccent);
}
else
sEnd = sWord;
nIndex = sEnd.IndexOfAny(Globals.ThisAddIn.IsArmenianAccent);
if (nIndex != -1)
{
string sStart1 = sEnd.Substring(0, nIndex);
string sAccent1 = sEnd.Substring(nIndex, 1);
string sEnd1 = sEnd.Substring(nIndex + 1, sEnd.Length - nIndex - 1);
// encode the beginning of the word
ArrayList arrEncoded3 = new ArrayList();
int nRet = oEncoder.Encode(sStart1, arrEncoded3);
if (arrEncoded3.Count != 0)
sTemp += arrEncoded3[0].ToString();
// encode the accent character
sTemp += EncodeAccent(sAccent1);
// encode the end of the word
ArrayList arrEncoded2 = new ArrayList();
nRet = oEncoder.Encode(sEnd1, arrEncoded2);
if (arrEncoded2.Count != 0)
sTemp += arrEncoded2[0].ToString();
}
else
{
ArrayList arrEncoded3 = new ArrayList();
int nRet = oEncoder.Encode(sEnd, arrEncoded3);
if (arrEncoded3.Count != 0)
sTemp += arrEncoded3[0].ToString();
}
return sTemp;
}
public string EncodeWithAccents(string sWord)
{
string sTemp = "";
string sStart = "";
string sAccent = "";
string sEnd = "";
int nIndex = sWord.IndexOfAny(Globals.ThisAddIn.IsArmenianApostrophe);
if (nIndex != -1)
{
sStart = sWord.Substring(0, nIndex);
sAccent = sWord.Substring(nIndex, 1);
sEnd = sWord.Substring(nIndex + 1, sWord.Length - nIndex - 1);
// encode the beginning of the word
ArrayList arrEncoded1 = new ArrayList();
int nRetFlag = m_oEncoder.Encode(sStart, arrEncoded1);
if (arrEncoded1.Count != 0)
sTemp = arrEncoded1[0].ToString();
// encode the apostrophe character
sTemp += EncodeAccent(sAccent);
}
else
sEnd = sWord;
nIndex = sEnd.IndexOfAny(Globals.ThisAddIn.IsArmenianAccent);
if (nIndex != -1)
{
string sStart1 = sEnd.Substring(0, nIndex);
string sAccent1 = sEnd.Substring(nIndex, 1);
string sEnd1 = sEnd.Substring(nIndex + 1, sEnd.Length - nIndex - 1);
// encode the beginning of the word
ArrayList arrEncoded3 = new ArrayList();
nRetFlag = m_oEncoder.Encode(sStart1, arrEncoded3);
if (arrEncoded3.Count != 0)
sTemp += arrEncoded3[0].ToString();
// encode the accent character
sTemp += EncodeAccent(sAccent1);
// encode the end of the word
ArrayList arrEncoded2 = new ArrayList();
nRetFlag = m_oEncoder.Encode(sEnd1, arrEncoded2);
if (arrEncoded2.Count != 0)
sTemp += arrEncoded2[0].ToString();
}
else
{
ArrayList arrEncoded3 = new ArrayList();
nRetFlag = m_oEncoder.Encode(sEnd, arrEncoded3);
if (arrEncoded3.Count != 0)
sTemp += arrEncoded3[0].ToString();
}
return sTemp;
}
public bool IsWordArmenian(Word.Selection selWord, ref ArrayList arrEncoded, ref string sApostrophe, ref string sAccent, ref Word.Range rgWord)
{
rgWord = selWord.Range;
Word.Range rgWhole = selWord.Range;
rgWhole.Start = rgWhole.Words.First.Start;
rgWhole.End = rgWhole.Words.First.End;
while (rgWhole.Start > 0 && !rgWhole.Text.StartsWith("\r") && !rgWhole.Text.StartsWith(" ") && !rgWhole.Text.StartsWith("\t"))
{
int nOldStartPos = rgWhole.Start;
rgWhole.Start = rgWhole.Words.First.Start - 1;
// break loop if trapped inside infinite loop
if (rgWhole.Start >= nOldStartPos)
break;
}
rgWhole.End = rgWhole.Words.Last.End;
while (rgWhole.Words.Last.Text != null && !rgWhole.Words.Last.Text.EndsWith("\r")
&& !rgWhole.Words.Last.Text.EndsWith(" ") && !rgWhole.Words.Last.Text.EndsWith("\t"))
{
int nOldEndPos = rgWhole.End;
rgWhole.End = rgWhole.Words.Last.End + 1;
// break loop if trapped inside infinite loop
if (rgWhole.End <= nOldEndPos)
break;
}
rgWhole.End = rgWhole.Words.Last.End;
char[] cGet = new char[] { '\n', '\r', '\t', '\f', '\v', '\a',
' ', ',', '.', ';', ':', '\"', '\'', '?', '&', '–', '_',
'|', '/', '<', '>', '[', ']', '{', '}', '(', ')',
'«', /*'»',*/'…', '‘', '’', '‚', '“', '”', '„', '£',
'¤', '¥', '¦', '§', /*'¨',*/ '©', 'ª', '®', '¬',
'\u055D', '\u0589'};
string sTemp = rgWhole.Text.Trim(cGet);
int nIndex = sTemp.IndexOfAny(Globals.ThisAddIn.IsArmenianApostrophe);
if (nIndex != -1)
{
sApostrophe = sTemp.Substring(nIndex, 1);
sTemp = sTemp.Remove(nIndex, 1);
}
nIndex = sTemp.IndexOfAny(Globals.ThisAddIn.IsArmenianAccent);
if (nIndex != -1)
{
string sAcc = sTemp.Substring(nIndex, 1);
if (sAcc.IndexOfAny(Globals.ThisAddIn.IsArmenianHyphen) == -1)
sAccent = sAcc;
sTemp = sTemp.Remove(nIndex, 1);
}
if (sTemp.Length > 0)
{
rgWord.Start = rgWhole.Start + rgWhole.Text.IndexOf(sTemp[0]);
rgWord.End = rgWhole.Start + rgWhole.Text.LastIndexOf(sTemp[sTemp.Length - 1]) + 1;
}
nSelWordEncoding = m_oEncoder.Encode_U(sTemp, arrEncoded);
if (sAccent.Length > 0 && nSelWordEncoding == 1)
sAccent = DecodeAccent(sAccent);
return !(nSelWordEncoding == 0);
}
void myApplication_WindowBeforeRightClick(Microsoft.Office.Interop.Word.Selection Sel, ref bool Cancel)
{
// only if the selected word is armenian make the context menu changes, otherwise do not show HySpell menus
string sWord = Sel.Words.First.Text;
int nIndex = -1;
if (sWord != null)
nIndex = sWord.IndexOfAny(Globals.ThisAddIn.IsArmenianCharOrAccent);
if (nIndex != -1 && m_oHunspell == null)
InitializeWrapper();
ArrayList arrEncoded = new ArrayList();
string sApostrophe = "";
string sAccent = "";
Word.Range rgWord = null;
if (nIndex != -1 && IsWordArmenian(Sel, ref arrEncoded, ref sApostrophe, ref sAccent, ref rgWord))
{
// based on spelling correctness show the HySpell context menus
rgWord.SpellingChecked = false;
string sSCIIOutput = arrEncoded[0].ToString();
bool bLegal = m_oHunspell.Spell(sSCIIOutput);
if (!bLegal)
{
ArrayList arrSuggests = new ArrayList();
List<string> suggestions = m_oHunspell.Suggest(sSCIIOutput);
if (suggestions.Count > 0)
{
foreach (string suggestion in suggestions)
{
string sWrd = suggestion.Insert(1, sApostrophe);
arrSuggests.Add(PutAccent(sWrd, sAccent));
}
}
nCurrentWordStart = rgWord.Start;
nCurrentWordEnd = rgWord.End;
// construct hs_Spelling context menu based on suggestions
AddContextMenu("hs_Spelling", arrSuggests);
Cancel = true;
}
else
{
Sel.Words.First.SpellingChecked = true;
nCurrentWordStart = rgWord.Start;
nCurrentWordEnd = rgWord.End;
//string s;
//s = Sel.Words.First.Text;
//int nLinks = Sel.Words.First.Hyperlinks.Count; // ? > 0
//int nTables = Sel.Words.First.Tables.Count; // ? > 0
//object oListStyle = Sel.Words.First.ListStyle; // ? == null
// construct context menu in the other cases with synonyms sub-menu
AddMainMenuItems("Text");
AddMainMenuItems("Lists");
AddMainMenuItems("Table Text");
AddMainMenuItems("Table Lists");
AddMainMenuItems("Hyperlink Context Menu");
AddMainMenuItems("Headings");
AddMainMenuItems("Footnotes");
}
}
else
{
RemoveExistingMenuItem("Text");
RemoveExistingMenuItem("Lists");
RemoveExistingMenuItem("Table Text");
RemoveExistingMenuItem("Table Lists");
RemoveExistingMenuItem("Hyperlink Context Menu");
RemoveExistingMenuItem("Headings");
RemoveExistingMenuItem("Footnotes");
}
}
public bool ByPassAppWinSelectChange = false;
void myApplication_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel)
{
bool bTaskPaneIsVisible = false;
Word.Document doc = this.Application.ActiveDocument;
HSTaskPane cCurrentPane = null;
foreach (Microsoft.Office.Tools.CustomTaskPane cPane in this.CustomTaskPanes)
{
Word.Window cpWindow = (Word.Window)cPane.Window;
if (cPane.Title == sTaskPaneName && cpWindow == doc.ActiveWindow && cPane.Visible)
{
bTaskPaneIsVisible = true;
cCurrentPane = (cPane.Control as HSTaskPane);
}
}
if (!bTaskPaneIsVisible) return;
string sWord = Sel.Words.First.Text;
int nIndex = sWord.IndexOfAny(Globals.ThisAddIn.IsArmenianCharOrAccent);
if (nIndex != -1 && m_oHunspell == null)
InitializeWrapper();
ArrayList arrEncoded = new ArrayList();
string sApostrophe = "";
string sAccent = "";
Word.Range rgWord = null;
if (nIndex != -1 && IsWordArmenian(Sel, ref arrEncoded, ref sApostrophe, ref sAccent, ref rgWord))
{
// based on spelling correctness show the HySpell context menus
((TextBox)cCurrentPane.Controls["tabControl"].Controls[0].Controls["txtMisspellIndicator"]).Text = rgWord.Text;
//rgWord.LanguageID = Microsoft.Office.Interop.Word.WdLanguageID.wdArmenian;
rgWord.SpellingChecked = false;
string sSCIIOutput = arrEncoded[0].ToString();
bool bLegal = m_oHunspell.Spell(sSCIIOutput);
if (!bLegal)
{
((TextBox)cCurrentPane.Controls["tabControl"].Controls[0].Controls["txtMisspellIndicator"]).ForeColor = System.Drawing.Color.White;
((TextBox)cCurrentPane.Controls["tabControl"].Controls[0].Controls["txtMisspellIndicator"]).BackColor = System.Drawing.Color.Red;
}
else
{
Sel.Words.First.SpellingChecked = true;
((TextBox)cCurrentPane.Controls["tabControl"].Controls[0].Controls["txtMisspellIndicator"]).ForeColor = System.Drawing.Color.Black;
((TextBox)cCurrentPane.Controls["tabControl"].Controls[0].Controls["txtMisspellIndicator"]).BackColor = System.Drawing.Color.White;
}
}
else
{
((TextBox)cCurrentPane.Controls["tabControl"].Controls[0].Controls["txtMisspellIndicator"]).Text = sWord;
((TextBox)cCurrentPane.Controls["tabControl"].Controls[0].Controls["txtMisspellIndicator"]).ForeColor = System.Drawing.Color.Black;
((TextBox)cCurrentPane.Controls["tabControl"].Controls[0].Controls["txtMisspellIndicator"]).BackColor = System.Drawing.Color.White;
}
if (cCurrentPane.Controls["tabControl"].Controls["tabPage1"] != null && !bInitSpellCheckCall && !ByPassAppWinSelectChange)
(myCustomTaskPane.Control as HSTaskPane).SetSpellingState("Ստուգել", false);
bInitSpellCheckCall = false;
ByPassAppWinSelectChange = false;
}
void MenuControl_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
string[] sTemps = Ctrl.Tag.Replace("hs_MenuItem", "").Split(':');
int nTagNum = Convert.ToInt32(sTemps[0]);
if (nTagNum < 20)
{
// correct the current misspelled word with the selected suggest word
string sSuggest = Ctrl.Caption;
Object oRS = nCurrentWordStart;
Object oRE = nCurrentWordEnd;
Word.Range rgCurrWord = myApplication.ActiveDocument.Range(ref oRS, ref oRE);
// check case for non-unicode text
if (nSelWordEncoding < 2)
sSuggest = EncodeWithAccents(sSuggest);
rgCurrWord.Text = sSuggest;
}
else
{
switch (nTagNum)
{
case 21: // Ignore
break;
case 22: // Ignore All
if (m_oEncoder != null && m_oHunspell != null)
{
Object oRS = nCurrentWordStart;
Object oRE = nCurrentWordEnd;
Word.Range rgCurrWord = myApplication.ActiveDocument.Range(ref oRS, ref oRE);
ArrayList arrEncoded = new ArrayList();
int nRetFlag = m_oEncoder.Encode(rgCurrWord.Text, arrEncoded);
if (arrEncoded.Count != 0)
m_oHunspell.Add(arrEncoded[0].ToString());
}
break;
case 23: // Add to Dictionary
if (m_oHunspell != null)
{
frmAddNewWord dlg = new frmAddNewWord();
if (Globals.ThisAddIn.ProgramSettings.OrthographyType == 1)
dlg.IsClassicOrthography = false;
Object oRS = nCurrentWordStart;
Object oRE = nCurrentWordEnd;
Word.Range rgCurrWord = myApplication.ActiveDocument.Range(ref oRS, ref oRE);
dlg.HSWrapper = m_oHunspell;
dlg.NewWord = rgCurrWord.Text;
if (dlg.ShowDialog() == DialogResult.OK)
{
string sNewWord = dlg.NewWord;
string sExample = dlg.AffixExample;
if (sExample.Length > 0)
{
m_oHunspell.AddWithAffix(sNewWord, sExample);
AddCustomWord(sNewWord, sExample);
}
else
{
m_oHunspell.Add(sNewWord);
AddCustomWord(sNewWord);
}
}
}
break;
case 24: // Look Up
{
Object oRS = nCurrentWordStart;
Object oRE = nCurrentWordEnd;
Word.Range rgCurrWord = myApplication.ActiveDocument.Range(ref oRS, ref oRE);
string sWord = rgCurrWord.Text;
ShowTaskPane(sWord);
}
break;
default:
break;
}
}
CancelDefault = true;
}
private void AddCustomWord(string word, string sample = "")
{
string newWord = word;
if (sample != "")
newWord += "|" + sample;
try
{
if (!File.Exists(sCustomDictPath))
{
using (StreamWriter sw = File.CreateText(sCustomDictPath)){}
}
using (StreamWriter sw = File.AppendText(sCustomDictPath))
{
sw.WriteLine(newWord);
}
}
catch {}
}
public void ShowTaskPane(string sWord)
{
bool bTaskPaneIsOpen = false;
Word.Document doc = this.Application.ActiveDocument;
foreach (Microsoft.Office.Tools.CustomTaskPane cPane in this.CustomTaskPanes)
{
Word.Window cpWindow = (Word.Window)cPane.Window;
if (cPane.Title == sTaskPaneName && cpWindow == doc.ActiveWindow)
{
cPane.Visible = true;
bTaskPaneIsOpen = true;
}
}
if (!bTaskPaneIsOpen)
{
if (m_oHunspell == null)
InitializeWrapper();
oHSTaskPaneControl = new HSTaskPane(m_oHunspell);
myCustomTaskPane = this.CustomTaskPanes.Add(oHSTaskPaneControl, sTaskPaneName, doc.ActiveWindow);
myCustomTaskPane.Visible = true;
(myCustomTaskPane.Control.Controls[0] as TabControl).TabPages.RemoveAt(1);
}
(myCustomTaskPane.Control.Controls[0] as TabControl).SelectTab(0);
if (sWord.Length > 0)
{
foreach (Microsoft.Office.Tools.CustomTaskPane cPane in this.CustomTaskPanes)
{
Word.Window cpWindow = (Word.Window)cPane.Window;
if (cPane.Title == sTaskPaneName && cpWindow == doc.ActiveWindow)
{
(cPane.Control as HSTaskPane).FindMeaning(sWord, true);
break;
}
}
}
}
public void StartSpellingViaTaskPane()
{
bool bTaskPaneIsOpen = false;
Word.Document doc = this.Application.ActiveDocument;
foreach (Microsoft.Office.Tools.CustomTaskPane cPane in this.CustomTaskPanes)
{
Word.Window cpWindow = (Word.Window)cPane.Window;
if (cPane.Title == sTaskPaneName && cpWindow == doc.ActiveWindow)
{
cPane.Visible = true;
bTaskPaneIsOpen = true;
}
}
if (!bTaskPaneIsOpen)
{
oHSTaskPaneControl = new HSTaskPane(m_oHunspell);
myCustomTaskPane = this.CustomTaskPanes.Add(oHSTaskPaneControl, sTaskPaneName, doc.ActiveWindow);
myCustomTaskPane.Visible = true;
}
foreach (Microsoft.Office.Tools.CustomTaskPane cPane in this.CustomTaskPanes)
{
Word.Window cpWindow = (Word.Window)cPane.Window;
if (cPane.Title == sTaskPaneName && cpWindow == doc.ActiveWindow)
{
(cPane.Control as HSTaskPane).StartSpellChecking();
break;
}
}
}
public void RemoveTaskPanes()
{
if (this.Application.Documents.Count > 0)
{
for (int i = this.CustomTaskPanes.Count; i > 0; i--)
{
Microsoft.Office.Tools.CustomTaskPane cPane = this.CustomTaskPanes[i - 1];
if (cPane.Title == sTaskPaneName)
this.CustomTaskPanes.Remove(cPane);
}
}
}
void AutoCorrectMenuControl_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
// add entries to the autocorrect list
string sAutoCorrectWord = Ctrl.Caption;
Object oRS = nCurrentWordStart;
Object oRE = nCurrentWordEnd;
Word.Range rgCurrWord = myApplication.ActiveDocument.Range(ref oRS, ref oRE);
this.Application.AutoCorrect.Entries.Add(rgCurrWord.Text, sAutoCorrectWord);
CancelDefault = true;
}
private void AddMenu_Click(object sender, EventArgs e)
{
// loop through all words spell checking each word and displaying red underlines wherever misspelled
//
// if (hs_SEStyle == null)
// {
// object type = Word.WdStyleType.wdStyleTypeCharacter;
// hs_SEStyle = myApplication.ActiveDocument.Styles.Add("hs_SpellingErrorStyle", ref type);
// hs_SEStyle.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineWavy;
// hs_SEStyle.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorRed;
// hs_SEStyle.Hidden = true;
// }
// else
// hs_SEStyle.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineWavy;
// initialize variables
bShowErrors = true;
bStart = true;
bCheckNormalText = true;
bCursorInShape = false;
bRestartPar = false;
bHasPrevTextBoxLink = false;
nHit = 0;
nCursorPos = 0;
nParIndex = 1;
nShapeIndex = 0;
nParCount = 0;
nParOffset = 0;
nIParOffset = 0;
nWordLen = 1;
nRetFlag = -1;
nStartPos = 0;
j = 0;
//
nCursorPos = myApplication.Selection.Start;
// get all shapes in the document
oShapes = myApplication.ActiveDocument.Shapes;
Word.ShapeRange oShpRng = myApplication.Selection.ShapeRange;
if (oShpRng.Count > 0)
{
object obj = 1;
Word.Shape shp = oShpRng.get_Item(ref obj);
bCheckNormalText = false;
bCursorInShape = true;
for (int i = 1; i <= oShapes.Count; i++)
{
obj = i;
nShapeIndex = i;
Word.Shape oShp = oShapes.get_Item(ref obj);
if (oShp.Name == shp.Name)
break;
}
}
RestartParagraph();
}
private void RestartParagraph()
{
bHasPrevTextBoxLink = false;
if (bCheckNormalText)
{
// get all paragraphs of normal text area (including field regions)
oPars = myApplication.ActiveDocument.Paragraphs;
nParCount = oPars.Count;
Word.Paragraph CurPar = oPars[nParIndex];
int nCurPos = Math.Max(CurPar.Range.Start, nCursorPos);
// start with the range of the whole active document
Object oRS = null, oRE = null;