-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathTextSource.cs
More file actions
432 lines (375 loc) · 10 KB
/
TextSource.cs
File metadata and controls
432 lines (375 loc) · 10 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using System.IO;
namespace FastColoredTextBoxNS
{
/// <summary>
/// This class contains the source text (chars and styles).
/// It stores a text lines, the manager of commands, undo/redo stack, styles.
/// </summary>
public class TextSource : IList<Line>, IDisposable
{
readonly protected List<Line> lines = new List<Line>();
protected LinesAccessor linesAccessor;
int lastLineUniqueId;
public CommandManager Manager
{
get;
set;
}
FastColoredTextBox currentTB;
/// <summary>
/// Styles
/// </summary>
public readonly Style[] Styles;
/// <summary>
/// Occurs when line was inserted/added
/// </summary>
public event EventHandler<LineInsertedEventArgs> LineInserted;
/// <summary>
/// Occurs when line was removed
/// </summary>
public event EventHandler<LineRemovedEventArgs> LineRemoved;
/// <summary>
/// Occurs when text was changed
/// </summary>
public event EventHandler<TextChangedEventArgs> TextChanged;
/// <summary>
/// Occurs when text was deleted
/// </summary>
public event EventHandler<TextDeletedEventArgs> TextDeleted;
/// <summary>
/// Occurs when recalc is needed
/// </summary>
public event EventHandler<TextChangedEventArgs> RecalcNeeded;
/// <summary>
/// Occurs when recalc wordwrap is needed
/// </summary>
public event EventHandler<TextChangedEventArgs> RecalcWordWrap;
/// <summary>
/// Occurs before text changing
/// </summary>
public event EventHandler<TextChangingEventArgs> TextChanging;
/// <summary>
/// Occurs after CurrentTB was changed
/// </summary>
public event EventHandler CurrentTBChanged;
/// <summary>
/// Current focused FastColoredTextBox
/// </summary>
public FastColoredTextBox CurrentTB
{
get
{
return currentTB;
}
set
{
if ( currentTB == value )
return;
currentTB = value;
OnCurrentTBChanged();
}
}
public virtual void ClearIsChanged()
{
foreach ( var line in lines )
line.IsChanged = false;
}
public virtual Line CreateLine()
{
return new Line( GenerateUniqueLineId(), currentTB );
}
private void OnCurrentTBChanged()
{
if ( CurrentTBChanged != null )
CurrentTBChanged( this, EventArgs.Empty );
}
/// <summary>
/// Default text style
/// This style is using when no one other TextStyle is not defined in Char.style
/// </summary>
public TextStyle DefaultStyle
{
get;
set;
}
public TextSource( FastColoredTextBox currentTB )
{
this.CurrentTB = currentTB;
linesAccessor = new LinesAccessor( this );
Manager = new CommandManager( this );
if ( Enum.GetUnderlyingType( typeof( StyleIndex ) ) == typeof( UInt32 ) )
Styles = new Style[32];
else
Styles = new Style[16];
InitDefaultStyle();
}
public virtual void InitDefaultStyle()
{
DefaultStyle = new TextStyle( null, null, FontStyle.Regular );
}
public virtual Line this[int i]
{
get
{
if ( ( i < 0 )
|| ( i >= lines.Count ) )
{
return null;
}
return lines[i];
}
set
{
throw new NotImplementedException();
}
}
public virtual bool IsLineLoaded( int iLine )
{
return lines[iLine] != null;
}
/// <summary>
/// Text lines
/// </summary>
public virtual IList<string> GetLines()
{
return linesAccessor;
}
public IEnumerator<Line> GetEnumerator()
{
return lines.GetEnumerator();
}
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return ( lines as IEnumerator );
}
public virtual int BinarySearch( Line item, IComparer<Line> comparer )
{
return lines.BinarySearch( item, comparer );
}
public virtual int GenerateUniqueLineId()
{
return lastLineUniqueId++;
}
public virtual void InsertLine( int index, Line line )
{
lines.Insert( index, line );
OnLineInserted( index );
}
public virtual void OnLineInserted( int index )
{
OnLineInserted( index, 1 );
}
public virtual void OnLineInserted( int index, int count )
{
if ( LineInserted != null )
LineInserted( this, new LineInsertedEventArgs( index, count ) );
}
public virtual void RemoveLine( int index )
{
RemoveLine( index, 1 );
}
public virtual bool IsNeedBuildRemovedLineIds
{
get
{
return LineRemoved != null;
}
}
public virtual void RemoveLine( int index, int count )
{
List<int> removedLineIds = new List<int>();
//
if ( count > 0 )
if ( IsNeedBuildRemovedLineIds )
for ( int i = 0; i < count; i++ )
removedLineIds.Add( this[index + i].UniqueId );
//
lines.RemoveRange( index, count );
OnLineRemoved( index, count, removedLineIds );
}
public virtual void OnLineRemoved( int index, int count, List<int> removedLineIds )
{
if ( count > 0 )
if ( LineRemoved != null )
LineRemoved( this, new LineRemovedEventArgs( index, count, removedLineIds ) );
}
public virtual void OnTextChanged( int fromLine, int toLine )
{
if ( TextChanged != null )
TextChanged( this, new TextChangedEventArgs( Math.Min( fromLine, toLine ), Math.Max( fromLine, toLine ) ) );
}
public virtual void OnTextDeleted( Range DeleteRange )
{
if ( TextDeleted != null )
TextDeleted( this, new TextDeletedEventArgs( DeleteRange ) );
}
public class TextChangedEventArgs : EventArgs
{
public int iFromLine;
public int iToLine;
public TextChangedEventArgs( int iFromLine, int iToLine )
{
this.iFromLine = iFromLine;
this.iToLine = iToLine;
}
}
public class TextDeletedEventArgs : EventArgs
{
public Range DeletedRange;
public TextDeletedEventArgs( Range DeleteRange )
{
this.DeletedRange = DeleteRange;
}
}
public virtual int IndexOf( Line item )
{
return lines.IndexOf( item );
}
public virtual void Insert( int index, Line item )
{
InsertLine( index, item );
}
public virtual void RemoveAt( int index )
{
RemoveLine( index );
}
public virtual void Add( Line item )
{
InsertLine( Count, item );
}
public virtual void Clear()
{
RemoveLine( 0, Count );
}
public virtual bool Contains( Line item )
{
return lines.Contains( item );
}
public virtual void CopyTo( Line[] array, int arrayIndex )
{
lines.CopyTo( array, arrayIndex );
}
/// <summary>
/// Lines count
/// </summary>
public virtual int Count
{
get
{
return lines.Count;
}
}
public virtual bool IsReadOnly
{
get
{
return false;
}
}
public virtual bool Remove( Line item )
{
int i = IndexOf( item );
if ( i >= 0 )
{
RemoveLine( i );
return true;
}
else
return false;
}
public virtual void NeedRecalc( TextChangedEventArgs args )
{
if ( RecalcNeeded != null )
RecalcNeeded( this, args );
}
public virtual void OnRecalcWordWrap( TextChangedEventArgs args )
{
if ( RecalcWordWrap != null )
RecalcWordWrap( this, args );
}
public virtual void OnTextChanging()
{
string temp = null;
OnTextChanging( ref temp );
}
public virtual void OnTextChanging( ref string text )
{
if ( TextChanging != null )
{
var args = new TextChangingEventArgs()
{
InsertingText = text
};
TextChanging( this, args );
text = args.InsertingText;
if ( args.Cancel )
text = string.Empty;
};
}
public virtual int GetLineLength( int i )
{
if ( currentTB.AllowTabs )
{
int length = 0;
var line = lines[i];
for ( int j = 0; j < line.Count; ++j )
{
if ( line[j].c == '\t' )
{
// combine tabs
++length;
bool firstChar = true;
while ( ( firstChar )
|| ( ( j % currentTB.TabLength ) > 0 ) )
{
firstChar = false;
++j;
}
}
else
{
++length;
}
}
return length;
}
return lines[i].Count;
}
public virtual bool LineHasFoldingStartMarker( int LineIndex )
{
if ( ( LineIndex < 0 )
|| ( LineIndex >= lines.Count ) )
{
return false;
}
return !string.IsNullOrEmpty( lines[LineIndex].FoldingStartMarker );
}
public virtual bool LineHasFoldingEndMarker( int LineIndex )
{
if ( ( LineIndex < 0 )
|| ( LineIndex >= lines.Count ) )
{
return false;
}
return !string.IsNullOrEmpty( lines[LineIndex].FoldingEndMarker );
}
public virtual void Dispose()
{
;
}
public virtual void SaveToFile( string fileName, Encoding enc )
{
using ( StreamWriter sw = new StreamWriter( fileName, false, enc ) )
{
for ( int i = 0; i < Count - 1; i++ )
sw.WriteLine( lines[i].Text );
sw.Write( lines[Count - 1].Text );
}
}
}
}