44using AppKit ;
55using Nikse . SubtitleEdit . Core ;
66using System . Collections . Generic ;
7+ using Nikse . SubtitleEdit . Core . SpellCheck ;
8+ using System . Text ;
9+ using Nikse . SubtitleEdit . UILogic ;
10+ using System . Linq ;
711
812namespace Edit
913{
1014 public partial class SpellCheck : NSWindow
1115 {
16+
17+ private List < string > _suggestions = new List < string > ( ) ;
18+
1219 public SpellCheck ( IntPtr handle )
1320 : base ( handle )
1421 {
@@ -20,6 +27,15 @@ public SpellCheck(NSCoder coder)
2027 {
2128 }
2229
30+ private void InitializeTable ( NSTableView table )
31+ {
32+ var columns = table . TableColumns ( ) ;
33+ columns [ 0 ] . SetIdentifier ( StringListTableDelegate . CellIdentifiers [ 0 ] ) ;
34+ columns [ 0 ] . MinWidth = 50 ;
35+ columns [ 0 ] . MaxWidth = 20000 ;
36+ columns [ 0 ] . Width = 2060 ;
37+ }
38+
2339 public override void AwakeFromNib ( )
2440 {
2541 base . AwakeFromNib ( ) ;
@@ -34,33 +50,125 @@ public override void AwakeFromNib()
3450
3551 ( WindowController as SpellCheckController ) . InitializeSpellCheck ( ) ;
3652
37- _buttonAbort . Activated += ( object sender , EventArgs e ) =>
38- {
39- ( WindowController as SpellCheckController ) . Abort ( ) ;
40- Close ( ) ;
41- } ;
53+ _buttonAbort . Activated += ( object sender , EventArgs e ) =>
54+ {
55+ ( WindowController as SpellCheckController ) . Abort ( ) ;
56+ Close ( ) ;
57+ } ;
58+
59+ _buttonSkipAll . Activated += ( object sender , EventArgs e ) =>
60+ {
61+ ( WindowController as SpellCheckController ) . SkipAll ( ) ;
62+ } ;
63+
64+ _buttonSkipOne . Activated += ( object sender , EventArgs e ) =>
65+ {
66+ ( WindowController as SpellCheckController ) . SkipOne ( ) ;
67+ } ;
68+
69+ _buttonChange . Activated += ( object sender , EventArgs e ) =>
70+ {
71+ ( WindowController as SpellCheckController ) . ChangeWord ( _textWordNotFound . StringValue ) ;
72+ } ;
73+
74+ _buttonChangeAll . Activated += ( object sender , EventArgs e ) =>
75+ {
76+ ( WindowController as SpellCheckController ) . ChangeWordAll ( _textWordNotFound . StringValue ) ;
77+ } ;
4278
43- _buttonSkipAll . Activated += ( object sender , EventArgs e ) =>
79+ _buttonAddToNames . Activated += ( object sender , EventArgs e ) =>
80+ {
81+ ( WindowController as SpellCheckController ) . AddToNames ( _textWordNotFound . StringValue ) ;
82+ } ;
83+
84+ _buttonAddToUserDictionary . Activated += ( object sender , EventArgs e ) =>
85+ {
86+ ( WindowController as SpellCheckController ) . AddToUserDictionary ( _textWordNotFound . StringValue ) ;
87+ } ;
88+
89+ _buttonGoogleIt . Activated += ( object sender , EventArgs e ) =>
90+ {
91+ System . Diagnostics . Process . Start ( "https://www.google.com/search?q=" + Utilities . UrlEncode ( _textWordNotFound . StringValue ) ) ;
92+ } ;
93+
94+ _buttonUseSuggestion . Activated += ( object sender , EventArgs e ) =>
95+ {
96+ int idx = ( int ) _tableSuggestions . SelectedRow ;
97+ if ( _suggestions . Count > 0 && idx >= 0 )
4498 {
45- ( WindowController as SpellCheckController ) . SkipAll ( ) ;
46- } ;
99+ ( WindowController as SpellCheckController ) . ChangeWord ( _suggestions [ idx ] ) ;
100+ }
101+ } ;
47102
48- _buttonSkipOne . Activated += ( object sender , EventArgs e ) =>
103+ _buttonUseSuggestionAlways . Activated += ( object sender , EventArgs e ) =>
104+ {
105+ int idx = ( int ) _tableSuggestions . SelectedRow ;
106+ if ( _suggestions . Count > 0 && idx >= 0 )
49107 {
50- ( WindowController as SpellCheckController ) . SkipOne ( ) ;
51- _textWordNotFound . StringValue = string . Empty ;
52- } ;
108+ ( WindowController as SpellCheckController ) . ChangeWordAll ( _suggestions [ idx ] ) ;
109+ }
110+ } ;
53111
112+ _popUpLanguages . Activated += ( object sender , EventArgs e ) =>
113+ {
114+ ( WindowController as SpellCheckController ) . LoadDictionaries ( _popUpLanguages . SelectedItem . Title ) ;
115+ } ;
116+
117+ _checkAutoFixNames . Activated += ( object sender , EventArgs e ) =>
118+ {
119+ ( WindowController as SpellCheckController ) . SetAutoFixState ( _checkAutoFixNames . State == NSCellStateValue . On ) ;
120+ } ;
54121
122+ InitializeTable ( _tableSuggestions ) ;
55123 }
56124
57125 public void InitializeLanguages ( List < string > list )
58126 {
59-
127+ _popUpLanguages . RemoveAllItems ( ) ;
128+ foreach ( var language in list )
129+ {
130+ _popUpLanguages . AddItem ( language ) ;
131+ }
132+ }
133+
134+ public void ShowSuggestions ( List < string > suggestions )
135+ {
136+ _suggestions = suggestions ;
137+ var ds = new StringListTableDataSource ( suggestions ) ;
138+ _tableSuggestions . DataSource = ds ;
139+ _tableSuggestions . Delegate = new StringListTableDelegate ( ds , null ) ;
140+ if ( suggestions . Count > 0 )
141+ {
142+ _tableSuggestions . SelectRow ( 0 , false ) ;
143+ }
60144 }
61145
62146 public void ShowUnknownWord ( SpellCheckWord currentSpellCheckWord , Paragraph currentParagraph )
63147 {
148+ string text = HtmlUtil . RemoveHtmlTags ( currentParagraph . Text , true ) ;
149+ _textViewFullText . Editable = true ;
150+ _textViewFullText . TextStorage . SetString ( new NSAttributedString ( "" ) ) ;
151+ _textViewFullText . InsertText ( new NSString ( text ) ) ;
152+ int idx = text . IndexOf ( currentSpellCheckWord . Text ) ;
153+ while ( idx >= 0 )
154+ {
155+ bool startOk = idx == 0 || text . Substring ( idx - 1 , 1 ) . ToLower ( ) == text . Substring ( idx - 1 , 1 ) . ToUpper ( ) ;
156+ if ( startOk )
157+ {
158+ int endIdx = idx + currentSpellCheckWord . Text . Length ;
159+ bool endOk = endIdx >= text . Length || text . Substring ( endIdx , 1 ) . ToLower ( ) == text . Substring ( endIdx , 1 ) . ToUpper ( ) ;
160+ if ( endOk )
161+ {
162+ _textViewFullText . SetTextColor ( NSColor . Red , new NSRange ( idx , currentSpellCheckWord . Text . Length ) ) ;
163+ }
164+ }
165+ if ( idx < text . Length - 1 )
166+ {
167+ idx = text . IndexOf ( currentSpellCheckWord . Text , idx + 1 ) ;
168+ }
169+ }
170+ _textViewFullText . Editable = false ;
171+
64172 _textWordNotFound . StringValue = currentSpellCheckWord . Text ;
65173 }
66174
@@ -70,5 +178,6 @@ public void ShowProgress(int currentParagraphIndex, Subtitle _subtitle)
70178 _progressBar . DoubleValue = currentParagraphIndex ;
71179 Title = Configuration . Settings . Language . SpellCheck . Title + " - " + ( currentParagraphIndex + 1 ) + " / " + _subtitle . Paragraphs . Count ;
72180 }
181+
73182 }
74183}
0 commit comments