My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
#include <CtrlLib/CtrlLib.h>

NAMESPACE_UPP

TextCtrl::TextCtrl()
{
Unicode();
undosteps = 10;
Clear();
undoserial = 0;
incundoserial = false;
undo_op = false;
WhenBar = THISBACK(StdBar);
charset = CHARSET_UNICODE;
color[INK_NORMAL] = SColorText;
color[INK_DISABLED] = SColorDisabled;
color[INK_SELECTED] = SColorHighlightText;
color[PAPER_NORMAL] = SColorPaper;
color[PAPER_READONLY] = SColorFace;
color[PAPER_SELECTED] = SColorHighlight;
processtab = true;
processenter = true;
nobg = false;
}

TextCtrl::~TextCtrl() {}

void TextCtrl::MiddleDown(Point p, dword flags)
{
if(IsReadOnly())
return;
if(AcceptText(Selection())) {
WString w = GetWString(Selection());
selclick = false;
LeftDown(p, flags);
Paste(w);
Action();
}
}

void TextCtrl::CancelMode()
{
selclick = false;
dropcaret = Null;
isdrag = false;
}

void TextCtrl::Clear()
{
cline = cpos = 0;
total = 0;
line.Clear();
line.Shrink();
ClearLines();
line.Add();
InsertLines(0, 1);
DirtyFrom(0);
anchor = -1;
cursor = 0;
SelectionChanged();
Refresh();
undo.Clear();
redo.Clear();
ClearDirty();
SetSb();
PlaceCaret(0);
}

void TextCtrl::DirtyFrom(int line) {}
void TextCtrl::SelectionChanged() {}
void TextCtrl::ClearLines() {}
void TextCtrl::InsertLines(int line, int count) {}
void TextCtrl::RemoveLines(int line, int count) {}
void TextCtrl::PreInsert(int pos, const WString& text) {}
void TextCtrl::PostInsert(int pos, const WString& text) {}
void TextCtrl::PreRemove(int pos, int size) {}
void TextCtrl::PostRemove(int pos, int size) {}
void TextCtrl::RefreshLine(int i) {}
void TextCtrl::InvalidateLine(int i) {}
void TextCtrl::SetSb() {}
void TextCtrl::PlaceCaret(int newcursor, bool sel) {}

void TextCtrl::CachePos(int pos) {
int p = pos;
cline = GetLinePos(p);
cpos = pos - p;
}

void TextCtrl::Load(Stream& s, byte charset) {
Clear();
line.Clear();
ClearLines();
String ln;
total = 0;
SetCharset(charset);
if(charset == CHARSET_UTF8_BOM && s.GetLeft() >= 3) {
int64 pos = s.GetPos();
byte h[3];
if(!(s.Get(h, 3) == 3 && h[0] == 0xEF && h[1] == 0xBB && h[2] == 0xBF))
s.Seek(pos);
charset = CHARSET_UTF8;
}
while(!s.IsEof()) {
int c = s.Get();
if(c == '\n') {
WString w = ToUnicode(ln, charset);
line.Add(w);
total += w.GetLength() + 1;
ln.Clear();
ln.Reserve(1000);
}
if(c >= ' ' || c == '\t')
ln.Cat(c);
}
ln.Shrink();
WString w = ToUnicode(ln, charset);
line.Add(w);
total += w.GetLength();
InsertLines(0, line.GetCount());
Update();
SetSb();
PlaceCaret(0);
}

void TextCtrl::Save(Stream& s, byte charset, bool crlf) const {
if(charset == CHARSET_UTF8_BOM) {
static byte bom[] = { 0xEF, 0xBB, 0xBF };
s.Put(bom, 3);
charset = CHARSET_UTF8;
}
charset = ResolveCharset(charset);
for(int i = 0; i < line.GetCount(); i++) {
if(i)
if(crlf)
s.PutCrLf();
else
s.PutEol();
String txt = charset == CHARSET_UTF8 ? line[i].text
: FromUnicode(line[i], charset);
const char *e = txt.End();
for(const char *w = txt; w != e; w++)
s.Put(*w == DEFAULTCHAR ? '?' : *w);
}
}

void TextCtrl::Set(const String& s, byte charset) {
StringStream ss(s);
Load(ss, charset);
}

String TextCtrl::Get(byte charset) const
{
StringStream ss;
Save(ss, charset);
return ss;
}

int TextCtrl::GetInvalidCharPos(byte charset) const
{
int q = 0;
if(charset != CHARSET_UTF8 && charset != CHARSET_UTF8_BOM)
for(int i = 0; i < line.GetCount(); i++) {
WString txt = line[i];
WString ctxt = ToUnicode(FromUnicode(txt, charset), charset);
for(int w = 0; w < txt.GetLength(); w++)
if(txt[w] != ctxt[w])
return q + w;
q += txt.GetLength() + 1;
}
return -1;
}

void TextCtrl::ClearDirty()
{
dirty = 0;
ClearModify();
WhenState();
}

TextCtrl::UndoData TextCtrl::PickUndoData()
{
UndoData data;
data.undo = undo;
data.redo = redo;
data.undoserial = undoserial;
return data;
}

void TextCtrl::SetPickUndoData(pick_ TextCtrl::UndoData& data)
{
undo = data.undo;
redo = data.redo;
undoserial = data.undoserial;
incundoserial = true;
}

void TextCtrl::Set(const WString& s)
{
Clear();
Insert0(0, s);
}

void TextCtrl::SetData(const Value& v)
{
Set((WString)v);
}

Value TextCtrl::GetData() const
{
return GetW();
}

String TextCtrl::GetEncodedLine(int i, byte charset) const
{
charset = ResolveCharset(charset);
if(charset == CHARSET_UTF8 && charset != CHARSET_UTF8_BOM)
return line[i].text;
return FromUnicode(FromUtf8(line[i].text), charset);
}

int TextCtrl::GetLinePos(int& pos) const {
if(pos < cpos && cpos - pos < pos) {
int i = cline;
int ps = cpos;
for(;;) {
ps -= line[--i].GetLength() + 1;
if(ps <= pos) {
pos = pos - ps;
return i;
}
}
}
else {
int i = 0;
if(pos >= cpos) {
pos -= cpos;
i = cline;
}
for(;;) {
int n = line[i].GetLength() + 1;
if(pos < n) return i;
pos -= n;
i++;
if(i >= line.GetCount()) {
pos = line.Top().GetLength();
return line.GetCount() - 1;
}
}
}
}

int TextCtrl::GetPos(int ln, int lpos) const {
ln = minmax(ln, 0, line.GetCount() - 1);
int i, pos;
if(ln < cline && cline - ln < ln) {
pos = cpos;
i = cline;
while(i > ln)
pos -= line[--i].GetLength() + 1;
}
else {
if(ln >= cline) {
pos = cpos;
i = cline;
}
else {
pos = 0;
i = 0;
}
while(i < ln)
pos += line[i++].GetLength() + 1;
}
return pos + min(line[ln].GetLength(), lpos);
}

WString TextCtrl::GetW(int pos, int size) const
{
int i = GetLinePos(pos);
WString r;
for(;;) {
if(i >= line.GetCount()) break;
WString ln = line[i++];
int sz = min(ln.GetLength() - pos, size);
r.Cat(ln.Mid(pos, sz));
size -= sz;
if(size == 0) break;
#ifdef PLATFORM_WIN32
r.Cat('\r');
#endif
r.Cat('\n');
size--;
if(size == 0) break;
pos = 0;
}
return r;
}

String TextCtrl::Get(int pos, int size, byte charset) const
{
return FromUnicode(GetW(pos, size), charset);
}

int TextCtrl::GetChar(int pos) const {
int i = GetLinePos(pos);
WString ln = line[i];
int c = ln.GetLength() == pos ? '\n' : ln[pos];
return c;
}

int TextCtrl::Insert0(int pos, const WString& txt) {
int inspos = pos;
PreInsert(inspos, txt);
if(pos < cpos)
cpos = cline = 0;
int i = GetLinePos(pos);
DirtyFrom(i);
int size = 0;
WString ln;
Vector<Ln> iln;
for(const wchar *s = txt; s < txt.End(); s++)
if(*s >= ' ' || *s == '\t') {
ln.Cat(*s);
size++;
}
else
if(*s == '\n') {
iln.Add(ln);
size++;
ln.Clear();
}

WString l = line[i];
if(iln.GetCount()) {
iln[0] = l.Mid(0, pos) + WString(iln[0]);
ln.Cat(l.Mid(pos));
line[i] = ln;
InvalidateLine(i);
line.Insert(i, iln);
InsertLines(i, iln.GetCount());
Refresh();
}
else {
line[i] = l.Mid(0, pos) + ln + l.Mid(pos);
InvalidateLine(i);
RefreshLine(i);
}
total += size;
SetSb();
Update();
PostInsert(inspos, txt);
return size;
}

void TextCtrl::Remove0(int pos, int size) {
int rmpos = pos, rmsize = size;
PreRemove(rmpos, rmsize);
total -= size;
if(pos < cpos)
cpos = cline = 0;
int i = GetLinePos(pos);
DirtyFrom(i);
WString ln = line[i];
int sz = min(ln.GetLength() - pos, size);
ln.Remove(pos, sz);
size -= sz;
line[i] = ln;
if(size == 0) {
InvalidateLine(i);
RefreshLine(i);
}
else {
size--;
int j = i + 1;
for(;;) {
int sz = line[j].GetLength() + 1;
if(sz > size) break;
j++;
size -= sz;
}
WString p1 = line[i];
WString p2 = line[j];
p1.Insert(p1.GetLength(), p2.Mid(size, p2.GetLength() - size));
line[i] = p1;
line.Remove(i + 1, j - i);
RemoveLines(i + 1, j - i);
InvalidateLine(i);
Refresh();
}
Update();
PostRemove(rmpos, rmsize);
SetSb();
}

void TextCtrl::Undodo()
{
while(undo.GetCount() > undosteps)
undo.DropHead();
redo.Clear();
}

void TextCtrl::NextUndo()
{
undoserial += incundoserial;
incundoserial = false;
}

void TextCtrl::IncDirty() {
dirty++;
if(dirty == 0 || dirty == 1)
{
if(dirty)
SetModify();
else
ClearModify();
WhenState();
}
}

void TextCtrl::DecDirty() {
dirty--;
if(dirty == 0 || dirty == -1)
{
if(dirty)
SetModify();
else
ClearModify();
WhenState();
}
}

int TextCtrl::InsertU(int pos, const WString& txt, bool typing) {
int sz = Insert0(pos, txt);
if(undosteps) {
if(undo.GetCount() > 1 && typing && *txt != '\n' && IsDirty()) {
UndoRec& u = undo.Tail();
if(u.typing && u.pos + u.size == pos) {
u.size += txt.GetLength();
return sz;
}
}
UndoRec& u = undo.AddTail();
incundoserial = true;
IncDirty();
u.serial = undoserial;
u.pos = pos;
u.size = sz;
u.typing = typing;
}
return sz;
}

void TextCtrl::RemoveU(int pos, int size) {
if(size + pos > total)
size = total - pos;
if(size <= 0) return;
if(undosteps) {
UndoRec& u = undo.AddTail();
incundoserial = true;
IncDirty();
u.serial = undoserial;
u.pos = pos;
u.size = 0;
u.text = Get(pos, size, CHARSET_UTF8);
u.typing = false;
}
Remove0(pos, size);
}

int TextCtrl::Insert(int pos, const WString& _txt, bool typing) {
WString txt = _txt;
if(charset != CHARSET_UNICODE && charset != CHARSET_UTF8_BOM)
for(int i = 0; i < txt.GetCount(); i++)
if(FromUnicode(txt[i], charset) == DEFAULTCHAR)
txt.Set(i, '?');
int sz = InsertU(pos, txt, typing);
Undodo();
return sz;
}

int TextCtrl::Insert(int pos, const String& txt, byte charset)
{
return Insert(pos, ToUnicode(txt, charset), false);
}

void TextCtrl::Remove(int pos, int size) {
RemoveU(pos, size);
Undodo();
}

void TextCtrl::Undo() {
if(undo.IsEmpty()) return;
undo_op = true;
int nc = 0;
int s = undo.Tail().serial;
while(undo.GetCount()) {
const UndoRec& u = undo.Tail();
if(u.serial != s)
break;
UndoRec& r = redo.AddTail();
r.serial = s;
r.typing = false;
nc = r.pos = u.pos;
CachePos(r.pos);
if(u.size) {
r.size = 0;
r.text = Get(u.pos, u.size, CHARSET_UTF8);
Remove0(u.pos, u.size);
}
else {
WString text = FromUtf8(u.text);
r.size = Insert0(u.pos, text);
nc += r.size;
}
undo.DropTail();
DecDirty();
}
ClearSelection();
PlaceCaret(nc, false);
Action();
undo_op = false;
}

void TextCtrl::Redo() {
if(!redo.GetCount()) return;
NextUndo();
int s = redo.Tail().serial;
int nc = 0;
while(redo.GetCount()) {
const UndoRec& r = redo.Tail();
if(r.serial != s)
break;
nc = r.pos + r.size;
CachePos(r.pos);
if(r.size)
RemoveU(r.pos, r.size);
else
nc += InsertU(r.pos, FromUtf8(r.text));
redo.DropTail();
IncDirty();
}
ClearSelection();
PlaceCaret(nc, false);
Action();
}

void TextCtrl::ClearSelection() {
anchor = -1;
Refresh();
WhenSel();
}

void TextCtrl::SetSelection(int l, int h) {
if(l != h) {
PlaceCaret(minmax(l, 0, total), false);
PlaceCaret(minmax(h, 0, total), true);
}
else
SetCursor(l);
}

bool TextCtrl::GetSelection(int& l, int& h) const {
if(anchor < 0) {
l = h = cursor;
return false;
}
else {
l = min(anchor, cursor);
h = max(anchor, cursor);
return true;
}
}

String TextCtrl::GetSelection(byte charset) const {
int l, h;
if(GetSelection(l, h))
return Get(l, h - l, charset);
return String();
}

WString TextCtrl::GetSelectionW() const {
int l, h;
if(GetSelection(l, h))
return GetW(l, h - l);
return WString();
}

bool TextCtrl::RemoveSelection() {
int l, h;
if(anchor < 0) return false;
GetSelection(l, h);
Remove(l, h - l);
anchor = -1;
Refresh();
PlaceCaret(l);
Action();
return true;
}

void TextCtrl::RefreshLines(int l1, int l2) {
int h = max(l1, l2);
for(int i = min(l1, l2); i <= h; i++)
RefreshLine(i);
}

void TextCtrl::Cut() {
if(!IsReadOnly() && IsSelection()) {
Copy();
RemoveSelection();
}
}

void TextCtrl::Copy() {
int l, h;
if(!GetSelection(l, h)) {
int i = GetLine(cursor);
l = GetPos(i);
h = l + line[i].GetLength() + 1;
}
WString txt = GetW(l, h - l);
ClearClipboard();
AppendClipboardUnicodeText(txt);
AppendClipboardText(txt.ToString());
}

void TextCtrl::SelectAll() {
SetSelection();
}

int TextCtrl::Paste(const WString& text) {
if(IsReadOnly()) return 0;
RemoveSelection();
int n = Insert(cursor, text);
PlaceCaret(cursor + n);
Refresh();
return n;
}

void TextCtrl::Paste() {
WString w = ReadClipboardUnicodeText();
if(w.IsEmpty())
w = ReadClipboardText().ToWString();
Paste(w);
Action();
}

void TextCtrl::StdBar(Bar& menu) {
NextUndo();
if(undosteps) {
menu.Add(undo.GetCount() && IsEditable(), t_("Undo"), THISBACK(Undo))
.Key(K_ALT_BACKSPACE)
.Key(K_CTRL_Z);
menu.Add(redo.GetCount() && IsEditable(), t_("Redo"), THISBACK(Redo))
.Key(K_SHIFT|K_ALT_BACKSPACE)
.Key(K_SHIFT_CTRL_Z);
menu.Separator();
}
menu.Add(IsEditable() && IsSelection(),
t_("Cut"), CtrlImg::cut(), THISBACK(Cut))
.Key(K_SHIFT_DELETE)
.Key(K_CTRL_X);
menu.Add(IsSelection(),
t_("Copy"), CtrlImg::copy(), THISBACK(Copy))
.Key(K_CTRL_INSERT)
.Key(K_CTRL_C);
menu.Add(IsEditable() && IsClipboardAvailableText(),
t_("Paste"), CtrlImg::paste(), THISBACK(DoPaste))
.Key(K_SHIFT_INSERT)
.Key(K_CTRL_V);
menu.Add(IsEditable() && IsSelection(),
t_("Erase"), CtrlImg::remove(), THISBACK(DoRemoveSelection))
.Key(K_DELETE);
menu.Separator();
menu.Add(GetLength(),
t_("Select all"), THISBACK(SelectAll))
.Key(K_CTRL_A);
}

String TextCtrl::GetSelectionData(const String& fmt) const
{
return GetTextClip(GetSelectionW(), fmt);
}

END_UPP_NAMESPACE

Change log

r4246 by cxl on Dec 4, 2011   Diff
*CtrlLib: Fixed with TextCtrl and BOM
(char vs byte) (thanks Sender Ghost!)
Go to: 
Project members, sign in to write a code review

Older revisions

r4169 by cxl on Nov 13, 2011   Diff
CtrlLib, ide: Support for UTF8-BOM
encoding (RM #129)
r3522 by cxl on Jun 13, 2011   Diff
.developing rainbow
r2387 by cxl on May 12, 2010   Diff
*CtrlLib: DocEdit/LineEdit readonly ->
undo redo disabled
All revisions of this file

File info

Size: 13377 bytes, 683 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting