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
#include "CtrlLib.h"

NAMESPACE_UPP

void DocEdit::MouseWheel(Point p, int zdelta, dword keyflags)
{
sb.Wheel(zdelta);
}

void DocEdit::ClearLines()
{
para.Clear();
ASSERT(this->line.GetCount() == para.GetCount());
}

void DocEdit::InsertLines(int line, int count)
{
para.Insert(line, Para(), count);
ASSERT(this->line.GetCount() == para.GetCount());
}

void DocEdit::RemoveLines(int line, int count)
{
para.Remove(line, count);
ASSERT(this->line.GetCount() == para.GetCount());
}

DocEdit::Fmt DocEdit::Format(const WString& text) const
{
FontInfo fi = font.Info();
Fmt fmt;
int tcx = fi['x'] * 4;
fmt.len = text.GetLength();
fmt.text.Alloc(text.GetLength());
memcpy(fmt.text, text, text.GetLength() * sizeof(wchar));
fmt.width.Alloc(text.GetLength());
fmt.line.Add(0);
int *w = fmt.width;
int x = 0;
const wchar *space = NULL;
int spacex = 0;
for(wchar *s = fmt.text; s < fmt.text + fmt.len; s++) {
int cw;
if(*s == '\t')
cw = (x + tcx) / tcx * tcx - x;
else
cw = fi[*s];
*w++ = cw;
if(*s == ' ' || *s == '\t') {
space = s;
spacex = x + cw;
*s = ' ';
}
x += cw;
if(x > cx)
if(space && space <= s) {
space++;
fmt.line.Add(int(space - fmt.text));
space = NULL;
x -= spacex;
}
else {
fmt.line.Add(int(s - fmt.text));
x = cw;
}
}
fmt.fi = fi;
return fmt;
}

int DocEdit::GetHeight(int i) {
Para& p = para[i];
if(p.cx == cx) return p.cy;
Fmt fmt = Format(line[i]);
p.cx = cx;
p.cy = fmt.line.GetCount() * (fmt.fi.GetHeight()) + after;
return p.cy;
}

int DocEdit::GetY(int parai) {
int y = 1;
for(int i = 0; i < parai; i++)
y += GetHeight(i);
return y;
}

void DocEdit::InvalidateLine(int i)
{
para[i].cx = -1;
}

void DocEdit::RefreshLine(int i) {
int q = para[i].cx >= 0 ? para[i].cy : -1;
Refresh(1, GetY(i) - sb, cx, GetHeight(i));
if(q < 0 || q != para[i].cy)
Refresh();
}

int sSum(const int *w, int n)
{
int m = 0;
while(n--)
m += *w++;
return m;
}

void DocEdit::Paint(Draw& w) {
Size sz = GetSize();
Color bg = color[IsShowEnabled() && !IsReadOnly() ? PAPER_NORMAL : PAPER_READONLY];
if(nobg)
bg = Null;
int y = -sb + 1;
int pos = 0;
int sell, selh;
GetSelection(sell, selh);
for(int i = 0; i < para.GetCount() && y < sz.cy; i++) {
int h = GetHeight(i);
if(y + h >= 0) {
WString text = line[i];
Fmt fmt = Format(text);
int p = pos;
for(int i = 0; i < fmt.line.GetCount(); i++) {
int n = fmt.LineEnd(i) - fmt.line[i];
int a = minmax(sell - p, 0, n);
int b = minmax(selh - p, 0, n) - a;
int c = n - a - b;
int *wa = fmt.width + fmt.line[i];
int *wb = fmt.width + fmt.line[i] + a;
int *wc = fmt.width + fmt.line[i] + a + b;
int acx = sSum(wa, a);
int bcx = sSum(wb, b);
int ccx = sSum(wc, c);
w.DrawRect(1, y, acx, fmt.fi.GetHeight(), bg);
w.DrawText(1, y, ~fmt.text + fmt.line[i], font,
IsShowEnabled() ? color[INK_NORMAL] : color[INK_DISABLED], a, wa);
w.DrawRect(1 + acx, y, bcx, fmt.fi.GetHeight(), color[PAPER_SELECTED]);
w.DrawText(1 + acx, y, ~fmt.text + fmt.line[i] + a, font, color[INK_SELECTED], b, wb);
w.DrawRect(1 + acx + bcx, y, ccx, fmt.fi.GetHeight(), bg);
w.DrawText(1 + acx + bcx, y, ~fmt.text + fmt.line[i] + a + b, font, color[INK_NORMAL], c, wc);
p += n;
w.DrawRect(1 + acx + bcx + ccx, y, cx - (acx + bcx + ccx), fmt.fi.GetHeight(),
p >= sell && p < selh ? color[PAPER_SELECTED] : bg);
y += fmt.fi.GetHeight();
}
w.DrawRect(1, y, cx, after, color[PAPER_NORMAL]);
y += after;
}
else
y += h;
pos += line[i].GetLength() + 1;
}
w.DrawRect(0, -sb, sz.cx, 1, bg);
w.DrawRect(0, 0, 1, sz.cy, bg);
w.DrawRect(sz.cx - 1, 0, 1, sz.cy, bg);
if(eofline)
w.DrawRect(1, y++, cx, 1, SColorShadow);
if(y < sz.cy)
w.DrawRect(1, y, cx, sz.cy - y, bg);
DrawTiles(w, DropCaret(), CtrlImg::checkers());
}

void DocEdit::SetSb()
{
Size sz = GetSize();
cx = max(Draw::GetStdFontCy(), sz.cx - 2);
sb.SetPage(GetSize().cy);
sb.SetTotal(GetY(para.GetCount()) + 2);
}

void DocEdit::Layout()
{
SetSb();
Invalidate();
}

Point DocEdit::GetCaret(int pos) {
int i = GetLinePos(pos);
Fmt fmt = Format(line[i]);
int l;
for(l = 0; l < fmt.line.GetCount(); l++)
if(pos < fmt.line[l])
break;
l--;
const int *w = fmt.width + fmt.line[l];
pos -= fmt.line[l];
int x = 0;
while(pos-- > 0)
x += *w++;
return Point(x, GetY(i) + l * fmt.fi.GetHeight());
}

int DocEdit::GetCursorPos(Point p) {
int pos = 0;
for(int i = 0; i < para.GetCount(); i++) {
int h = GetHeight(i);
if(p.y < h) {
WString text = line[i];
Fmt fmt = Format(text);
int x = 0;
int l = p.y / fmt.fi.GetHeight();
if(l < 0)
return pos;
if(l >= fmt.line.GetCount())
return pos + text.GetLength();
const int *w = fmt.width + fmt.line[l];
const int *e = fmt.width + fmt.LineEnd(l);
while(w < e) {
if(p.x < x + *w / 2)
return int(w - fmt.width) + pos;
x += *w++;
}
int p = int(e - fmt.width);
if(p > 0 && text[p - 1] == ' ' && l < fmt.line.GetCount() - 1)
p--;
return p + pos;
}
p.y -= h;
pos += line[i].GetLength() + 1;
}
return GetLength();
}

void DocEdit::PlaceCaret(bool scroll) {
Point cr = GetCaret(cursor);
int fy = font.Info().GetLineHeight();
if(scroll)
if(cursor == total)
sb.End();
else
sb.ScrollInto(cr.y, fy + 2);
SetCaret(cr.x + 1, cr.y - sb, 1, fy);
WhenSel();
}

void DocEdit::PlaceCaret(int newpos, bool select) {
if(newpos > GetLength())
newpos = GetLength();
int z = GetLine(newpos);
if(select) {
if(anchor < 0) {
anchor = cursor;
}
RefreshLines(z, GetLine(cursor));
}
else
if(anchor >= 0) {
RefreshLines(GetLine(cursor), GetLine(anchor));
anchor = -1;
}
cursor = newpos;
PlaceCaret(true);
SelectionChanged();
if(IsSelection())
SetSelectionSource(ClipFmtsText());
}

int DocEdit::GetMousePos(Point p)
{
return GetCursorPos(Point(p.x - 1, p.y + sb - 1));
}

void DocEdit::LeftDown(Point p, dword flags) {
SetWantFocus();
int c = GetMousePos(p);
int l, h;
if(GetSelection(l, h) && c >= l && c < h) {
selclick = true;
return;
}
PlaceCaret(c, flags & K_SHIFT);
SetCapture();
}

void DocEdit::LeftUp(Point p, dword flags)
{
if(!HasCapture() && selclick) {
int c = GetMousePos(p);
PlaceCaret(c, flags & K_SHIFT);
SetWantFocus();
}
selclick = false;
}

void DocEdit::MouseMove(Point p, dword flags) {
if(!HasCapture()) return;
PlaceCaret(GetMousePos(p), true);
}

void DocEdit::LeftDouble(Point, dword)
{
int l, h;
if(GetWordSelection(cursor, l, h))
SetSelection(l, h);
}

void DocEdit::LeftTriple(Point, dword)
{
int q = cursor;
int i = GetLinePos(q);
q = cursor - q;
SetSelection(q, q + GetLineLength(i) + 1);
}

Image DocEdit::CursorImage(Point, dword) {
return Image::IBeam();
}

void DocEdit::GotFocus() {
Refresh();
}

void DocEdit::LostFocus() {
Refresh();
}

void DocEdit::VertMove(int delta, bool select, bool scs) {
int hy = GetY(para.GetCount());
Point p = GetCaret(cursor);
int yy = p.y;
for(;;) {
p.y += delta;
if(p.y > hy) p.y = hy - 1;
if(p.y < 0) p.y = 0;
int q = GetCursorPos(p);
if(q >= 0 && q != cursor && (delta < 0) == (q < cursor) && GetCaret(q).y != yy) {
PlaceCaret(q, select);
break;
}
if(p.y == 0 || p.y >= hy - 1) {
PlaceCaret(delta > 0 ? total : 0, select);
break;
}
delta = sgn(delta) * 4;
}
if(scs)
sb = GetCaret(cursor).y - (yy - sb);
PlaceCaret(true);
}

void DocEdit::HomeEnd(int x, bool select) {
Point p = GetCaret(cursor);
p.x = x;
PlaceCaret(GetCursorPos(p), select);
}

bool DocEdit::Key(dword key, int cnt)
{
NextUndo();
bool h;
int q;
bool select = key & K_SHIFT;
int pgsk = max(8, 6 * GetSize().cy / 8);
switch(key & ~K_SHIFT) {
case K_CTRL_LEFT:
PlaceCaret(GetPrevWord(cursor), select);
break;
case K_CTRL_RIGHT:
PlaceCaret(GetNextWord(cursor), select);
break;
case K_HOME:
HomeEnd(0, select);
break;
case K_END:
HomeEnd(cx, select);
break;
case K_CTRL_HOME:
case K_CTRL_PAGEUP:
PlaceCaret(0, select);
break;
case K_CTRL_END:
case K_CTRL_PAGEDOWN:
PlaceCaret(total, select);
break;
case K_UP:
if(GetCursor() == 0)
return !updownleave;
VertMove(-8, select, false);
return true;
case K_DOWN:
if(GetCursor() == GetLength())
return !updownleave;
VertMove(8, select, false);
return true;
case K_PAGEUP:
VertMove(-pgsk, select, true);
return true;
case K_PAGEDOWN:
VertMove(pgsk, select, true);
return true;
case K_LEFT:
if(cursor)
PlaceCaret(cursor - 1, select);
break;
case K_RIGHT:
if(cursor < total)
PlaceCaret(cursor + 1, select);
break;
default:
if(IsReadOnly()) return MenuBar::Scan(WhenBar, key);
switch(key) {
case K_BACKSPACE:
case K_SHIFT|K_BACKSPACE:
if(RemoveSelection()) break;
if(cursor == 0) return true;
cursor--;
Remove(cursor, 1);
break;
case K_CTRL_BACKSPACE:
if(RemoveSelection()) break;
if(cursor <= 0) return true;
q = cursor - 1;
h = IsLetter(GetChar(q));
while(q > 0 && IsLetter(GetChar(q - 1)) == h) q--;
Remove(q, cursor - q);
SetCursor(q);
break;
case K_DELETE:
if(RemoveSelection()) break;
if(cursor >= total) return true;
if(cursor < total)
Remove(cursor, 1);
break;
case K_CTRL_DELETE:
if(RemoveSelection()) break;
if(cursor >= total) return true;
q = cursor;
h = IsLetter(GetChar(q));
while(IsLetter(GetChar(q)) == h && q < total) q++;
Remove(cursor, q - cursor);
break;
case K_ENTER:
if(!processenter)
return true;
key = '\n';
default:
if(filter && key >= 32 && key < 65535)
key = (*filter)(key);
if(key >= ' ' && key < 65536 || key == '\n' || key == '\t' || key == K_SHIFT_SPACE) {
if(key == K_TAB && !processtab)
return false;
if(key >= 128 && key < 65536 && (charset != CHARSET_UNICODE && charset != CHARSET_UTF8_BOM)
&& FromUnicode((wchar)key, charset) == DEFAULTCHAR)
return true;
RemoveSelection();
Insert(cursor, WString(key == K_SHIFT_SPACE ? ' ' : key, cnt), true);
cursor += cnt;
break;
}
return MenuBar::Scan(WhenBar, key);
}
UpdateAction();
}
PlaceCaret(true);
return true;
}

void DocEdit::Scroll()
{
PlaceCaret(false);
Refresh();
}

void DocEdit::Invalidate()
{
for(int i = 0; i < para.GetCount(); i++)
para[i].cx = -1;
PlaceCaret(false);
}

void DocEdit::RefreshStyle()
{
cursor = 0;
sb = 0;
ClearSelection();
Invalidate();
Layout();
Refresh();
}

void DocEdit::RightDown(Point p, dword w)
{
SetFocus();
int c = GetMousePos(p);
int l, h;
if(!GetSelection(l, h) || c < l || c >= h)
PlaceCaret(c, false);
MenuBar::Execute(WhenBar);
}

DocEdit::DocEdit()
{
updownleave = false;
cx = 0;
filter = NULL;
after = 0;
font = StdFont();
AutoHideSb();
SetFrame(ViewFrame());
AddFrame(sb);
sb.SetLine(8);
sb.WhenScroll = THISBACK(Scroll);
InsertLines(0, 1);
eofline = true;
}

DocEdit::~DocEdit() {}

void DocEdit::DragAndDrop(Point p, PasteClip& d)
{
if(IsReadOnly()) return;
int c = GetMousePos(p);
if(AcceptText(d)) {
NextUndo();
int a = sb;
int sell, selh;
WString txt = GetWString(d);
if(GetSelection(sell, selh)) {
if(c >= sell && c < selh) {
RemoveSelection();
if(IsDragAndDropSource())
d.SetAction(DND_COPY);
c = sell;
}
else
if(d.GetAction() == DND_MOVE && IsDragAndDropSource()) {
if(c > sell)
c -= selh - sell;
RemoveSelection();
d.SetAction(DND_COPY);
}
}
int count = Insert(c, txt);
sb = a;
SetFocus();
SetSelection(c, c + count);
Action();
return;
}
if(!d.IsAccepted()) return;
Point dc = Null;
if(c >= 0) {
Point cr = GetCaret(c);
dc = Point(cr.x + 1, cr.y);
}
if(dc != dropcaret) {
RefreshDropCaret();
dropcaret = dc;
RefreshDropCaret();
}
}

Rect DocEdit::DropCaret()
{
if(IsNull(dropcaret))
return Rect(0, 0, 0, 0);
return RectC(dropcaret.x, dropcaret.y - sb, 1, font.Info().GetLineHeight());
}

void DocEdit::RefreshDropCaret()
{
Refresh(DropCaret());
}

void DocEdit::DragRepeat(Point p)
{
sb = (int)sb + GetDragScroll(this, p, 16).y;
}

void DocEdit::DragLeave()
{
RefreshDropCaret();
dropcaret = Null;
isdrag = false;
Layout();
}

void DocEdit::LeftDrag(Point p, dword flags)
{
int c = GetMousePos(p);
int l, h;
if(!HasCapture() && GetSelection(l, h) && c >= l && c < h) {
WString sample = GetW(l, min(h - l, 3000));
Size ssz = StdSampleSize();
ImageDraw iw(ssz);
iw.DrawRect(ssz, Black());
iw.Alpha().DrawRect(ssz, Black());
DrawTLText(iw.Alpha(), 0, 0, ssz.cx, sample, StdFont(), White());
NextUndo();
if(DoDragAndDrop(ClipFmtsText(), iw) == DND_MOVE) {
RemoveSelection();
Action();
}
}
}

END_UPP_NAMESPACE

Change log

r4349 by cxl on Dec 26, 2011   Diff
*CtrlLib: Fixed D&D in DocEdit
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)
r3159 by cxl on Feb 4, 2011   Diff
.CtrlLib: Added some parenthesis to
DocEdit if to make it clear
r2325 by cxl on Apr 18, 2010   Diff
CtrlLib: EditField ReadOnly Ctrl+C
fix, DocEdit: NoEofLine, Display:
Background fix
All revisions of this file

File info

Size: 12679 bytes, 596 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting