EditorBar.cpp

Contains implementation of "MouseWheel" - Zbigniew Rebacz, 04/05/2014 12:44 PM

Download (11.5 KB)

 
1
#include "CodeEditor.h"
2

    
3
NAMESPACE_UPP
4

    
5
void Renumber(LineInfo& lf)
6
{
7
        LineInfo tf;
8
        int l = 0;
9
        if(lf.GetCount()) {
10
                LineInfoRecord& t = tf.Add();
11
                t.breakpoint = lf[0].breakpoint;
12
                t.lineno = 0;
13
                t.count = lf[0].count;
14
                t.error = lf[0].error;
15
                t.firstedited = lf[0].firstedited;
16
                t.edited = lf[0].edited;
17
                l += t.count;
18
        }
19
        for(int i = 1; i < lf.GetCount(); i++) {
20
                LineInfoRecord& r = lf[i];
21
                if(r.breakpoint.IsEmpty() && r.error == 0 && r.edited == 0 &&
22
                        tf.Top().breakpoint.IsEmpty() && tf.Top().error == 0 && tf.Top().edited == 0)
23
                        tf.Top().count += r.count;
24
                else {
25
                        LineInfoRecord& t = tf.Add();
26
                        t.breakpoint = r.breakpoint;
27
                        t.error = r.error;
28
                        t.firstedited = r.firstedited;
29
                        t.edited = r.edited;
30
                        t.count = r.count;
31
                        t.lineno = l;
32
                }
33
                l += r.count;
34
        }
35
        lf = pick(tf);
36
}
37

    
38
void ClearBreakpoints(LineInfo& lf)
39
{
40
        for(int i = 0; i < lf.GetCount(); i++)
41
                lf[i].breakpoint.Clear();
42
}
43

    
44
void ValidateBreakpoints(LineInfo& lf)
45
{
46
        for(int i = 0; i < lf.GetCount(); i++)
47
                if(lf[i].breakpoint[0] == 0xe)
48
                        lf[i].breakpoint = "1";
49
}
50

    
51
void EditorBar::sPaintImage(Draw& w, int y, int fy, const Image& img)
52
{
53
        w.DrawImage(0, y + (fy - img.GetSize().cy) / 2, img);
54
}
55

    
56
void EditorBar::Paint(Draw& w)
57
{
58
        static Image (*numeri[])() = {
59
                CodeEditorImg::N0, CodeEditorImg::N1, CodeEditorImg::N2, CodeEditorImg::N3, CodeEditorImg::N4,
60
                CodeEditorImg::N5, CodeEditorImg::N6, CodeEditorImg::N7, CodeEditorImg::N8, CodeEditorImg::N9,
61
        };
62
        Size sz = GetSize();
63
        w.DrawRect(0, 0, sz.cx, sz.cy, SColorLtFace);
64
        if(!editor) return;
65
        int fy = editor->GetFontSize().cy;
66
        int hy = fy >> 1;
67
        int y = 0;
68
        int i = editor->GetScrollPos().y;
69
        int cy = GetSize().cy;
70
        bool hi_if = (hilite_if_endif && (editor->highlight == CodeEditor::HIGHLIGHT_CPP
71
                || editor->highlight == CodeEditor::HIGHLIGHT_CS
72
                || editor->highlight == CodeEditor::HIGHLIGHT_JAVA));
73
        Vector<CodeEditor::IfState> previf;
74
        if(hi_if)
75
                previf <<= editor->ScanSyntax(i).ifstack;
76
        int ptri[2];
77
        for(int q = 0; q < 2; q++)
78
                ptri[q] = ptrline[q] >= 0 ? GetLineNo(ptrline[q]) : -1;
79
        while(y < cy) {
80
                String b;
81
                int err = 0;
82
                int edit = 0;
83
                String ann;
84
                Image  icon;
85
                if(i < li.GetCount()) {
86
                        const LnInfo& l = li[i];
87
                        b = l.breakpoint;
88
                        err = l.error;
89
                        edit = l.edited;
90
                        icon = l.icon;
91
                        ann = l.annotation;
92
                }
93
                if(editor->GetCaret().top == y && editor->barline)
94
                        w.DrawRect(0, y, sz.cx, fy, Blend(SColorHighlight(), SColorLtFace(), 200));
95
                if(line_numbers && i < editor->GetLineCount()) {
96
                        String n = AsString(i + 1);
97
                        for(int q = 0; q < 4 && q < n.GetLength(); q++) {
98
                                w.DrawImage(sz.cx - 8 - q * 6,
99
                                        y + (fy - CodeEditorImg::N0().GetSize().cy) / 2,
100
                                        numeri[n[n.GetLength() - 1 - q] - '0'],
101
                                        // CodeEditorImg::Vector[n[n.GetLength() - 1 - q] - '0' + CodeEditorImg::I_N0],
102
                                        Brown);
103
                        }
104
                }
105
                if(hi_if) {
106
                        Vector<CodeEditor::IfState> nextif;
107
                        if(i < li.GetCount())
108
                                nextif <<= editor->ScanSyntax(i + 1).ifstack;
109
                        int pifl = previf.GetCount(), nifl = nextif.GetCount();
110
                        int dif = max(pifl, nifl);
111
                        if(--dif >= 0) {
112
                                char p = (dif < pifl ? previf[dif].state : 0);
113
                                char n = (dif < nifl ? nextif[dif].state : 0);
114
                                int wd = min(2 * (dif + 1), sz.cx);
115
                                int x = sz.cx - wd;
116
                                Color cn = CodeEditor::SyntaxState::IfColor(n);
117
                                if(p == n)
118
                                        w.DrawRect(x, y, 1, fy, cn);
119
                                else {
120
                                        Color cp = CodeEditor::SyntaxState::IfColor(p);
121
                                        w.DrawRect(x, y, 1, hy, cp);
122
                                        w.DrawRect(x, y + hy, wd, 1, Nvl(cn, cp));
123
                                        w.DrawRect(x, y + hy, 1, fy - hy, cn);
124
                                        if(--dif >= 0) {
125
                                                x = sz.cx - min(2 * (dif + 1), sz.cx);
126
                                                if(!p)
127
                                                        w.DrawRect(x, y, 1, hy, CodeEditor::SyntaxState::IfColor(dif < pifl ? previf[dif].state : 0));
128
                                                if(!n)
129
                                                        w.DrawRect(x, y + hy, 1, fy - hy, CodeEditor::SyntaxState::IfColor(dif < nifl ? nextif[dif].state : 0));
130
                                        }
131
                                }
132
                        }
133
                        previf = pick(nextif);
134
                }
135
                if(editor->GetMarkLines()) {
136
                        int width = CodeEditorImg::Breakpoint().GetWidth() >> 1;
137
                        if(edit)
138
                        {
139
                                int age = (int)(log((double)(editor->GetUndoCount() + 1 - edit)) * 30);
140
                                w.DrawRect(0, y, width, fy, Blend(LtBlue, SColorLtFace(), min(220, age)));
141
                        }
142
                        if(err)
143
                                w.DrawRect(width, y, width, fy, err == 1 ? LtRed : (err == 2 ? Color(255, 175, 0) : Green));
144
                }
145

    
146
                if(!b.IsEmpty())
147
                        sPaintImage(w, y, fy, b == "1"   ? CodeEditorImg::Breakpoint() :
148
                                              b == "\xe" ? CodeEditorImg::InvalidBreakpoint() :
149
                                                           CodeEditorImg::CondBreakpoint());
150
                for(int q = 0; q < 2; q++)
151
                        if(ptri[q] == i)
152
                                sPaintImage(w, y, fy, ptrimg[q]);
153

    
154
                if(annotations && !IsNull(icon))
155
                        w.DrawImage(sz.cx - annotations, y + (fy - icon.GetSize().cy) / 2, icon);
156

    
157
                y += fy;
158
                i++;
159
        }
160
}
161

    
162
void EditorBar::MouseMove(Point p, dword flags)
163
{
164
        int pa = active_annotation;
165
        if(p.x > GetSize().cx - annotations)
166
                active_annotation = p.y / editor->GetFont().Info().GetHeight() + editor->GetScrollPos().y;
167
        else
168
                active_annotation = -1;
169
        if(active_annotation >= editor->GetLineCount())
170
                active_annotation = -1;
171
        if(pa != active_annotation)
172
                WhenAnnotationMove();
173
        if(editor)
174
                editor->MouseMove(Point(0, p.y), flags);
175
}
176

    
177
void EditorBar::MouseLeave()
178
{
179
        int pa = active_annotation;
180
        active_annotation = -1;
181
        if(pa != active_annotation)
182
                WhenAnnotationMove();
183
}
184

    
185
void EditorBar::MouseWheel(Point p, int zdelta, dword keyflags)
186
{
187
        if(editor)
188
                editor->MouseWheel(p, zdelta, keyflags);
189
}
190

    
191
void EditorBar::LeftDown(Point p, dword flags)
192
{
193
        if(p.x > GetSize().cx - annotations)
194
                WhenAnnotationClick();
195
        else
196
        if(editor)
197
                editor->LeftDown(Point(0, p.y), flags);
198
}
199

    
200
String& EditorBar::PointBreak(int& y)
201
{
202
        y = minmax(y / editor->GetFont().Info().GetHeight()
203
                + editor->GetScrollPos().y, 0, editor->GetLineCount());
204
        return li.At(y).breakpoint;
205
}
206

    
207
void EditorBar::LeftDouble(Point p, dword flags)
208
{
209
        if(!editor || !bingenabled) return;
210
        String& b = PointBreak(p.y);
211
        if(b.IsEmpty())
212
                b = "1";
213
        else
214
                b.Clear();
215
        WhenBreakpoint(p.y);
216
        Refresh();
217
}
218

    
219
void EditorBar::RightDown(Point p, dword flags)
220
{
221
        if(p.x > GetSize().cx - annotations)
222
                WhenAnnotationRightClick();
223
}
224

    
225
void EditorBar::InsertLines(int i, int count)
226
{
227
        li.InsertN(minmax(i + 1, 0, li.GetCount()), max(count, 0));
228
        if(editor->GetCheckEdited()) {
229
                if(editor->IsUndoOp() && li_removed.GetCount() >= count) {
230
                        for(int t = 0; t < count; t++) {
231
                                li.At(i + t).firstedited = li_removed[li_removed.GetCount() - count + t].firstedited;
232
                                li[i + t].edited = li_removed[li_removed.GetCount() - count + t].edited;
233
                        }
234
                        li_removed.Drop(count);
235
                        SetEdited(i + count, 1);
236
                        ignored_next_edit = true;
237
                }
238
                else {
239
                        if (li[i].firstedited == 0) {
240
                                bool fe = li[i].firstedited;
241
                                li.At(i + count).firstedited = fe;
242
                        }
243
                        SetEdited(i + 1, count);
244
                }
245
        }
246
        Refresh();
247
}
248

    
249
void EditorBar::RemoveLines(int i, int count)
250
{
251
        if(editor->GetCheckEdited() && !editor->IsUndoOp()) {
252
                for(int t = i - 1; t < i + count - 1; t++) {
253
                        LineInfoRemRecord& rm = li_removed.Add();
254
                        rm.firstedited = li[t].firstedited;
255
                        rm.edited = li[t].edited;
256
                }
257
                if(li.At(i + count - 1).firstedited)
258
                        next_age = li[i + count - 1].firstedited;
259
                else
260
                        next_age = editor->GetUndoCount();
261
        }
262
        i = minmax(i, 0, li.GetCount());
263
        li.Remove(i, minmax(count, 0, li.GetCount() - i));
264
        Refresh();
265
}
266

    
267
void EditorBar::ClearLines()
268
{
269
        li.Clear();
270
        li.Shrink();
271
        li_removed.Clear();
272
        li_removed.Shrink();
273
        Refresh();
274
}
275

    
276
LineInfo EditorBar::GetLineInfo() const
277
{
278
        LineInfo lf;
279
        int l = -2;
280
        for(int i = 0; i < li.GetCount(); i++) {
281
                const LnInfo& ln = li[i];
282
                if(!ln.breakpoint.IsEmpty() || ln.error || ln.edited) {
283
                        LineInfoRecord& r = lf.Add();
284
                        r.lineno = ln.lineno;
285
                        r.count = 1;
286
                        r.breakpoint = ln.breakpoint;
287
                        r.error = ln.error;
288
                        r.firstedited = ln.firstedited;
289
                        r.edited = ln.edited;
290
                        l = -2;
291
                }
292
                else
293
                if(ln.lineno != l) {
294
                        LineInfoRecord& r = lf.Add();
295
                        r.lineno = l = ln.lineno;
296
                        r.count = 1;
297
                }
298
                else
299
                        lf.Top().count++;
300
                if(l >= 0) l++;
301
        }
302
        return lf;
303
}
304

    
305
void EditorBar::SetLineInfo(const LineInfo& lf, int total)
306
{
307
        li.Clear();
308
        if(lf.GetCount() == 0) {
309
                for(int i = 0; i < total; i++)
310
                        li.Add().lineno = i;
311
        }
312
        else {
313
                for(int i = 0; i < lf.GetCount() && (total < 0 || li.GetCount() < total); i++) {
314
                        const LineInfoRecord& r = lf[i];
315
                        int l = r.lineno;
316
                        for(int j = r.count; j-- && li.GetCount() < total;) {
317
                                LnInfo& ln = li.Add();
318
                                ln.lineno = l;
319
                                ln.breakpoint = r.breakpoint;
320
                                ln.error = r.error;
321
                                ln.firstedited = r.firstedited;
322
                                ln.edited = r.edited;
323
                                if(l >= 0) l++;
324
                        }
325
                }
326
                while(li.GetCount() < total)
327
                        li.Add().lineno = -1;
328
        }
329
}
330

    
331
void EditorBar::Renumber(int linecount)
332
{
333
        li.SetCount(linecount);
334
        for(int i = 0; i < linecount; i++)
335
                li[i].lineno = i;
336
}
337

    
338
void EditorBar::ClearBreakpoints()
339
{
340
        for(int i = 0; i < li.GetCount(); i++)
341
                li[i].breakpoint.Clear();
342
        Refresh();
343
}
344

    
345
void EditorBar::ValidateBreakpoints()
346
{
347
        for(int i = 0; i < li.GetCount(); i++)
348
                if(li[i].breakpoint[0] == 0xe)
349
                        li[i].breakpoint = "1";
350
        Refresh();
351
}
352

    
353
String EditorBar::GetBreakpoint(int ln)
354
{
355
        return ln < li.GetCount() ? li[ln].breakpoint : Null;
356
}
357

    
358
void EditorBar::SetAnnotation(int line, const Image& img, const String& ann)
359
{
360
        if(line >= 0 && line < li.GetCount()) {
361
                li[line].icon = img;
362
                li[line].annotation = ann;
363
        }
364
}
365

    
366
String EditorBar::GetAnnotation(int line) const
367
{
368
        return line >= 0 && line < li.GetCount() ? li[line].annotation : String();
369
}
370

    
371
void EditorBar::SetBreakpoint(int ln, const String& s)
372
{
373
        li.At(ln).breakpoint = s;
374
        WhenBreakpoint(ln);
375
}
376

    
377
void EditorBar::SetEdited(int ln, int count)
378
{
379
        if(ignored_next_edit) {
380
                ignored_next_edit = false;
381
                return;
382
        }
383
        int age = editor->GetUndoCount() + 1;
384
        bool undo = editor->IsUndoOp();
385
        for(int i = 0; i < count; i++) {
386
                if(undo) {
387
                        if (li.At(ln + i).firstedited >= age - 1) {
388
                                li[ln + i].firstedited = 0;
389
                                li[ln + i].edited = 0;
390
                        }
391
                }
392
                else {
393
                        if(next_age) {
394
                                li[ln + i].firstedited = next_age;
395
                                li[ln + i].edited = age;
396
                                next_age = 0;
397
                        }
398
                        else {
399
                                if(li.At(ln + i).firstedited == 0)
400
                                        li[ln + i].firstedited = age;
401
                                li[ln + i].edited = age;
402
                        }
403
                }
404
        }
405
        Refresh();
406
}
407

    
408
void EditorBar::ClearEdited()
409
{
410
        for(int i = 0; i < li.GetCount(); i++) {
411
                li.At(i).firstedited = 0;
412
                li[i].edited = 0;
413
        }
414
        li_removed.Clear();
415
        li_removed.Shrink();
416
        Refresh();
417
}
418

    
419
void EditorBar::SetError(int ln, int err)
420
{
421
        li.At(ln).error = err;
422
}
423

    
424
void EditorBar::ClearErrors(int line)
425
{
426
        int count;
427
        if(line < 0) {
428
                line = 0;
429
                count = li.GetCount();
430
        }
431
        else
432
        if(line >= li.GetCount())
433
                return;
434
        else
435
                count = line + 1;
436

    
437
        for(int i = line; i < count; i++)
438
                li[i].error = 0;
439
}
440

    
441
int  EditorBar::GetLineNo(int lineno) const {
442
        for(int i = 0; i < li.GetCount(); i++) {
443
                if(lineno <= li[i].lineno)
444
                        return i;
445
        }
446
        return lineno;
447
}
448

    
449
int  EditorBar::GetNoLine(int line) const {
450
        int n = 0;
451
        for(int i = 0; i < li.GetCount(); i++) {
452
                if(li[i].lineno >= 0)
453
                        n = li[i].lineno;
454
                if(i == line) return n;
455
        }
456
        return n;
457
}
458

    
459
void EditorBar::SetPtr(int line, const Image& img, int i)
460
{
461
        ASSERT(i >= 0 && i < 2);
462
        ptrline[i] = line;
463
        ptrimg[i] = img;
464
        Refresh();
465
}
466

    
467
void EditorBar::HidePtr()
468
{
469
        ptrline[0] = ptrline[1] = -1;
470
        Refresh();
471
}
472

    
473
void EditorBar::SyncWidth()
474
{
475
        Width((line_numbers ? 27 : 12) + annotations);
476
        Refresh();
477
}
478

    
479
void EditorBar::LineNumbers(bool b)
480
{
481
        line_numbers = b;
482
        SyncWidth();
483
}
484

    
485
void EditorBar::Annotations(int width)
486
{
487
        annotations = width;
488
        SyncWidth();
489
}
490

    
491
EditorBar::EditorBar()
492
{
493
        LineNumbers(false);
494
        editor = NULL;
495
        bingenabled = true;
496
        hilite_if_endif = true;
497
        line_numbers = false;
498
        annotations = 0;
499
        ignored_next_edit = false;
500
        next_age = 0;
501
        SyncWidth();
502
}
503

    
504
EditorBar::~EditorBar()
505
{
506
}
507

    
508
void ClearErrors(LineInfo& li)
509
{
510
        for(int i = 0; i < li.GetCount(); i++)
511
                li[i].error = 0;
512
}
513

    
514
void SetError(LineInfo& li, int line, int err)
515
{
516
        li.At(line).error = err;
517
}
518

    
519
END_UPP_NAMESPACE