CodeEditor.h

Header with addition method "MouseWheel" (EditorBar class) - Zbigniew Rebacz, 04/05/2014 12:44 PM

Download (16.3 KB)

 
1
#ifndef IDEEDITOR_H
2
#define IDEEDITOR_H
3

    
4
//#include <ide/Common/Common.h>
5
#include <CtrlLib/CtrlLib.h>
6

    
7

    
8
NAMESPACE_UPP
9

    
10
#define  LAYOUTFILE <CodeEditor/CodeEditor.lay>
11
#include <CtrlCore/lay.h>
12

    
13
#define IMAGEVECTOR Vector
14
#define IMAGECLASS  CodeEditorImg
15
#define IMAGEFILE   <CodeEditor/CodeEditor.iml>
16
#include <Draw/iml_header.h>
17

    
18
void FindWildcardMenu(Callback1<int> cb, Point p, bool tablf, Ctrl *owner = NULL);
19

    
20
struct LineInfoRecord {
21
        int    lineno;
22
        String breakpoint;
23
        int    count;
24
        int    error;
25
        int    firstedited;
26
        int    edited;
27

    
28
        LineInfoRecord() { error = 0; edited = 0; }
29
};
30

    
31
typedef Array<LineInfoRecord> LineInfo;
32

    
33
void ClearErrors(LineInfo& li);
34

    
35
struct LineInfoRemRecord : Moveable<LineInfoRemRecord> {
36
        int    firstedited;
37
        int    edited;
38
};
39

    
40
typedef Vector<LineInfoRemRecord> LineInfoRem;
41

    
42
void Renumber(LineInfo& lf);
43
void ClearBreakpoints(LineInfo& lf);
44
void ValidateBreakpoints(LineInfo& lf);
45

    
46
class CodeEditor;
47

    
48
class EditorBar : public FrameLeft<Ctrl> {
49
public:
50
        virtual void Paint(Draw& w);
51
        virtual void MouseMove(Point p, dword flags);
52
        virtual void MouseLeave();
53
        virtual void MouseWheel(Point p, int zdelta, dword keyflags);
54
        virtual void LeftDown(Point p, dword flags);
55
        virtual void LeftDouble(Point p, dword flags);
56
        virtual void RightDown(Point p, dword flags);
57

    
58
private:
59
        struct LnInfo : Moveable<LnInfo> {
60
                int    lineno;
61
                String breakpoint;
62
                int    error;
63
                int    firstedited;
64
                int    edited;
65
                Image  icon;
66
                String annotation;
67

    
68
                LnInfo() { lineno = -1; error = 0; firstedited = 0; edited = 0; }
69
        };
70
        
71
        Vector<LnInfo>   li;
72
        LineInfoRem      li_removed;
73

    
74
        CodeEditor       *editor;
75
        int              ptrline[2];
76
        Image            ptrimg[2];
77
        bool             bingenabled;
78
        bool             hilite_if_endif;
79
        bool             line_numbers;
80
        int              annotations;
81
        bool             ignored_next_edit;
82
        int              next_age;
83
        int              active_annotation;
84

    
85
        String& PointBreak(int& y);
86
        void    sPaintImage(Draw& w, int y, int fy, const Image& img);
87
        void    SyncWidth();
88

    
89
public:
90
        Callback1<int> WhenBreakpoint;
91
        Callback       WhenAnnotationMove;
92
        Callback       WhenAnnotationClick;
93
        Callback       WhenAnnotationRightClick;
94

    
95
        void InsertLines(int i, int count);
96
        void RemoveLines(int i, int count);
97
        void ClearLines();
98

    
99
        void Scroll()                          { Refresh(); }
100

    
101
        void Renumber(int linecount);
102
        void ClearBreakpoints();
103
        void ValidateBreakpoints();
104

    
105
        String  GetBreakpoint(int ln);
106
        void    SetBreakpoint(int ln, const String& s);
107
        void    SetEdited(int ln, int count = 1);
108
        void    ClearEdited();
109
        void    SetError(int ln, int err);
110
        void    ClearErrors(int ln);
111

    
112
        void SetEditor(CodeEditor *e)           { editor = e; }
113

    
114
        LineInfo GetLineInfo() const;
115
        void     SetLineInfo(const LineInfo& li, int total);
116
        LineInfoRem & GetLineInfoRem()                   { return li_removed; }
117
        void     SetLineInfoRem(LineInfoRem pick_ li)    { li_removed = pick(li); }
118
        
119
        void     SetAnnotation(int line, const Image& img, const String& ann);
120
        String   GetAnnotation(int line) const;
121

    
122
        int      GetLineNo(int lineno) const;
123
        int      GetNoLine(int line) const;
124

    
125
        void     SetPtr(int line, const Image& img, int i);
126
        void     HidePtr();
127

    
128
        void     EnableBreakpointing(bool b)     { bingenabled = b; }
129
        void     HiliteIfEndif(bool b)           { hilite_if_endif = b; Refresh(); }
130
        void     LineNumbers(bool b);
131
        void     Annotations(int width);
132
        
133
        bool     IsHiliteIfEndif() const         { return hilite_if_endif; }
134
        
135
        int      GetActiveAnnotationLine() const { return active_annotation; }
136

    
137
        EditorBar();
138
        virtual ~EditorBar();
139
};
140

    
141
struct IdentPos {
142
        int    begin;
143
        int    end;
144
        String ident;
145
};
146

    
147
Array<IdentPos> GetLineIdent(const char *line);
148
Vector<Point>   GetLineString(const wchar *wline, bool& is_begin, bool& is_end);
149

    
150
inline int  CharFilterCIdent(int i)  { return IsAlNum(i) || i == '_' ? i : 0; }
151
inline bool iscidl(int c)            { return iscid(c) || IsLetter(c); }
152
inline bool islbrkt(int c)           { return c == '{' || c == '[' || c == '('; }
153
inline bool isrbrkt(int c)           { return c == '}' || c == ']' || c == ')'; }
154
inline bool isbrkt(int c)            { return islbrkt(c) || isrbrkt(c); }
155

    
156
struct FindReplaceDlg : WithIDEFindReplaceLayout<TopWindow> {
157
        WString itext;
158
        bool    replacing;
159

    
160
        Size    GetAdjustedSize();
161
        virtual bool Key(dword key, int count);
162
        void Setup(bool doreplace);
163
        void Sync();
164
        
165
        typedef FindReplaceDlg CLASSNAME;
166

    
167
        FindReplaceDlg();
168
};
169
        
170
class CodeEditor : public LineEdit {
171
        friend class EditorBar;
172

    
173
public:
174
        virtual bool  Key(dword code, int count);
175
        virtual void  LeftDown(Point p, dword keyflags);
176
        virtual void  LeftDouble(Point p, dword keyflags);
177
        virtual void  MouseMove(Point p, dword keyflags);
178
        virtual Image CursorImage(Point p, dword keyflags);
179
        virtual void  Serialize(Stream& s);
180
        virtual void  MouseLeave();
181

    
182
        void         CheckEdited(bool e = true) { check_edited = e; }
183
        bool         GetCheckEdited()           { return check_edited; }
184

    
185
        enum HighlightType {
186
                HIGHLIGHT_NONE = -1, HIGHLIGHT_CPP = 0, HIGHLIGHT_USC, HIGHLIGHT_JAVA, HIGHLIGHT_T,
187
                HIGHLIGHT_CALC, HIGHLIGHT_LAY, HIGHLIGHT_SCH, HIGHLIGHT_SQL, HIGHLIGHT_CS,
188
                HIGHLIGHT_JAVASCRIPT, HIGHLIGHT_CSS, HIGHLIGHT_JSON,
189
                HIGHLIGHT_COUNT
190
        };
191

    
192
protected:
193
        virtual void HighlightLine(int line, Vector<Highlight>& h, int pos);
194
        virtual void PreInsert(int pos, const WString& s);
195
        virtual void PostInsert(int pos, const WString& s);
196
        virtual void PreRemove(int pos, int size);
197
        virtual void PostRemove(int pos, int size);
198
        virtual void DirtyFrom(int line);
199
        virtual void SelectionChanged();
200

    
201
        virtual void ClearLines();
202
        virtual void InsertLines(int line, int count);
203
        virtual void RemoveLines(int line, int count);
204

    
205
        virtual void NewScrollPos();
206

    
207
        EditorBar   bar;
208
        Vector<int> line2;
209

    
210
        static Vector< Index<String> > keyword;
211
        static Vector< Index<String> > name;
212
        static Index<String> kw_upp;
213
        static int kw_macros, kw_logs, kw_sql_base, kw_sql_func;
214

    
215
        struct Isx : Moveable<Isx> {
216
                int    line;
217
                int    pos;
218

    
219
                friend bool operator==(Isx a, Isx b) { return a.line == b.line && a.pos == b.pos; }
220
                friend bool operator!=(Isx a, Isx b) { return !(a == b); }
221
        };
222

    
223
        struct IfState : Moveable<IfState> {
224
                enum        { IF = '0', ELIF, ELSE, ELSE_ERROR, ENDIF_ERROR };
225
                WString iftext;
226
                short   ifline;
227
                char    state;
228

    
229
                bool operator==(const IfState& b) const {
230
                        return iftext == b.iftext && state == b.state && ifline == b.ifline;
231
                }
232

    
233
                IfState()                         { ifline = state = 0; }
234
        };
235

    
236
        struct SyntaxState {
237
                int         line;
238

    
239
                bool        comment;
240
                bool        linecomment;
241
                bool        string;
242
                bool        linecont;
243
                bool        was_namespace;
244
                char        macro;
245
                enum        { MACRO_OFF, MACRO_CONT, MACRO_END };
246

    
247
                int         cl, bl, pl;
248

    
249
                WithDeepCopy< Vector<int> > brk;
250
                WithDeepCopy< Vector<int> > blk;
251
                WithDeepCopy< Vector<int> > bid;
252
                WithDeepCopy< Vector<Isx> > par;
253
                WithDeepCopy< Vector<IfState> > ifstack;
254

    
255
                int         stmtline;
256
                int         endstmtline;
257
                int         seline;
258
                int         spar;
259
                Color       uvscolor;
260

    
261
                void  DropItem(int type);
262
                bool  Drop(int type);
263
                void  ClearBraces();
264
                void  Clear();
265
                bool  MatchHilite(const SyntaxState& st) const;
266
                void  Grounding(const wchar *ln, const wchar *e);
267
                void  ScanSyntax(const wchar *ln, const wchar *e, int tab_size);
268

    
269
                static Color IfColor(char ifstate);
270

    
271
                SyntaxState()                         { Clear(); }
272
        };
273

    
274
        friend struct SyntaxState;
275

    
276
        SyntaxState scache[4];
277
        SyntaxState rm_ins;
278
        char        rmb;
279
        byte        hilite_bracket;
280
        int         highlight_bracket_pos0;
281
        int         highlight_bracket_pos;
282
        bool        bracket_flash;
283
        int         bracket_start;
284

    
285
        byte    hilite_scope;
286
        byte    hilite_ifdef;
287
        bool    thousands_separator;
288
        bool    indent_spaces : 1;
289
        bool    no_parenthesis_indent : 1;
290
        bool    barline : 1;
291
        int     indent_amount;
292
        double  stat_edit_time;
293
        Time    last_key_time;
294

    
295
        bool    auto_enclose;
296
        bool    mark_lines;
297
        bool    check_edited;
298
        bool    persistent_find_replace;
299

    
300
        FindReplaceDlg findreplace;
301
        
302
        enum {
303
                WILDANY = 16,
304
                WILDONE,
305
                WILDSPACE,
306
                WILDNUMBER,
307
                WILDID,
308
        };
309

    
310
        struct Found {
311
                int     type;
312
                WString text;
313
        };
314

    
315
        Array<Found> foundwild;
316
        WString      foundtext;
317
        bool   foundsel;
318
        bool   found, notfoundfw, notfoundbk;
319
        int    foundpos, foundsize;
320

    
321
        int    iwc;
322

    
323
        int    highlight;
324
        
325
        struct Tip : Ctrl {
326
                Value v;
327
                const Display *d;
328
                
329
                virtual void Paint(Draw& w);
330
                
331
                Tip();
332
        };
333
        
334
        Tip   tip;
335
        int   tippos;
336
        
337
        int   replacei;
338

    
339
        struct HlSt;
340
        
341
        static int  InitUpp(const char **q);
342

    
343
        const wchar *HlString(HlSt& hls, const wchar *p);
344

    
345
        SyntaxState ScanSyntax(int line);
346

    
347
        void   CancelBracketHighlight(int& pos);
348
        void   CheckBracket(int li, int pos, int ppos, int pos0, WString ln, int d, int limit);
349
        void   CheckLeftBracket(int pos);
350
        void   CheckRightBracket(int pos);
351
        void   CheckBrackets();
352
        void   OpenNormalFindReplace(bool replace);
353
        void   FindReplaceAddHistory();
354
        void   FindWildcard();
355
        void   ReplaceWildcard();
356
        void   InsertWildcard(int c);
357
        void   CheckBraces(const WString& text);
358

    
359
        void   SetFound(int fi, int type, const WString& text);
360

    
361
        int     Match(const wchar *f, const wchar *s, int line, bool we, bool icase, int fi = 0);
362
        WString GetWild(int type, int& i);
363
        WString GetReplaceText();
364
        WString GetReplaceText(WString replace, bool wildcards, bool samecase);
365

    
366
        bool   InsertRS(int chr, int count = 1);
367
        void   IndentEnter(int count = 1);
368
        void   SyntaxIndent(const SyntaxState& st, bool ndnt);
369
        void   IndentInsert(int chr);
370

    
371
        void   ForwardWhenBreakpoint(int i);
372

    
373
        Color  BlockColor(int level);
374
        void   Bracket(int pos, HlSt& hls);
375

    
376
        bool   ToggleSimpleComment(int &start_line, int &end_line, bool usestars = true);
377
        void   ToggleLineComments(bool usestars = false);
378
        void   ToggleStarComments();
379
        void   Enclose(const char *c1, const char *c2, int l = -1, int h = -1);
380
        void   Make(Callback1<String&> op);
381
        void   TabsOrSpaces(String& out, bool maketabs);
382
        void   LineEnds(String& out);
383

    
384
        enum {
385
                TIMEID_PERIODIC = Ctrl::TIMEID_COUNT,
386
                TIMEID_COUNT,
387
        };
388

    
389
        void   Periodic();
390

    
391
public:
392
#define HL_COLOR(x, a, b)      x,
393
        enum {
394
#include "hl_color.i"
395
                HL_COUNT
396
        };
397
#undef HL_COLOR
398

    
399
        struct HlStyle {
400
                Color color;
401
                bool  bold;
402
                bool  italic;
403
                bool  underline;
404
        };
405

    
406
private:
407
        HlStyle  hl_style[HL_COUNT];
408

    
409
public:
410
        static int  LoadSyntax(const char *keywords[], const char *names[]);
411

    
412
        struct MouseTip {
413
                int            pos;
414
                Value          value;
415
                const Display *display;
416
                Size           sz;
417
        };
418

    
419
        Callback         WhenSelection;
420
        Gate1<MouseTip&> WhenTip;
421
        Callback         WhenLeftDown;
422
        Callback1<int>   WhenCtrlClick;
423
        Callback         WhenAnnotationMove;
424
        Callback         WhenAnnotationClick;
425
        Callback         WhenAnnotationRightClick;
426

    
427
        FrameTop<Button>    topsbbutton;
428
        FrameTop<Button>    topsbbutton1;
429

    
430
        void   Clear()                    { LineEdit::Clear(); found = notfoundfw = notfoundbk = false; }
431

    
432
        void   Highlight(int h)           { highlight = h; Refresh(); }
433
        int    GetHighlight() const       { return highlight; }
434

    
435
        const HlStyle& GetHlStyle(int i);
436
        void           SetHlStyle(int i, Color c, bool bold = false, bool italic = false, bool underline = false);
437

    
438
        void   CloseFindReplace();
439
        void   FindReplace(bool pick_selection, bool pick_text, bool replace);
440
        bool   FindFrom(int pos, bool back, const wchar *text, bool wholeword, bool ignorecase, bool wildcards, bool block);
441
        bool   Find(bool back, const wchar *text, bool wholeword, bool ignorecase, bool wildcards,
442
                    bool block);
443
        bool   Find(bool back = false, bool blockreplace = false, bool replace = false);
444
        bool   GetStringRange(int cursor, int& b, int &e) const;
445
        bool   GetStringRange(int& b, int &e) const { return GetStringRange(GetCursor(), b, e); }
446
        bool   FindString(bool back);
447
        bool   FindLangString(bool back);
448
        void   Replace();
449
        void   BlockReplace();
450
        int    BlockReplace(WString find, WString replace, bool wholeword, bool ignorecase,
451
                            bool wildcards, bool samecase);
452
        
453
        void   MakeTabsOrSpaces(bool tabs);
454
        void   MakeLineEnds();
455

    
456
        void   CopyWord();
457
        void   SwapChars();
458

    
459
        void   SerializeFind(Stream& s);
460
        bool   IsFindOpen() const                       { return findreplace.IsOpen(); }
461
        void   FindClose()                              { findreplace.Close(); }
462

    
463
        void   Goto();
464

    
465
        void   DoFind();
466
        void   DoFindBack();
467

    
468
        void    FindWord(bool back);
469
        WString GetI();
470
        void    SetI(Ctrl *edit);
471
        void    PutI(WithDropChoice<EditString>& edit);
472

    
473
        void   MoveNextWord(bool sel);
474
        void   MovePrevWord(bool sel);
475
        void   MoveNextBrk(bool sel);
476
        void   MovePrevBrk(bool sel);
477

    
478
        String GetWord(int pos);
479
        String GetWord();
480

    
481
        bool   GetWordPos(int pos, int& l, int& h);
482

    
483
        void   DeleteWord();
484
        void   DeleteWordBack();
485
        void   SetLineSelection(int l, int h);
486
        bool   GetLineSelection(int& l, int& h);
487
        void   TabRight();
488
        void   TabLeft();
489

    
490
        Callback1<int> WhenBreakpoint;
491

    
492
        LineInfo GetLineInfo() const                      { return bar.GetLineInfo(); }
493
        void     SetLineInfo(const LineInfo& lf);
494
        LineInfoRem GetLineInfoRem()                      { return LineInfoRem(bar.GetLineInfoRem(), 0); }
495
        void     SetLineInfoRem(LineInfoRem pick_  lf)    { bar.SetLineInfoRem(LineInfoRem(lf, 0)); }
496
        double   GetStatEditTime() const                  { return stat_edit_time; }
497
        void     Renumber()                               { bar.Renumber(GetLineCount()); }
498
        void     ClearBreakpoints()                       { bar.ClearBreakpoints(); }
499
        void     ValidateBreakpoints()                    { bar.ValidateBreakpoints(); }
500
        int      GetLineNo(int line) const                { return bar.GetLineNo(line); }
501
        int      GetNoLine(int line) const                { return bar.GetNoLine(line); }
502
        void     SetPtr(int line, const Image& img, int i){ bar.SetPtr(line, img, i); }
503
        void     HidePtr()                                { bar.HidePtr(); }
504
        String   GetBreakpoint(int line)                  { return bar.GetBreakpoint(line); }
505
        void     SetBreakpoint(int line, const String& b) { bar.SetBreakpoint(line, b); }
506
        void     SetError(int line, int err)              { bar.SetError(line, err); }
507
        void     ClearErrors(int line = -1)               { bar.ClearErrors(line); }
508
        void     ClearEdited()                            { bar.ClearEdited(); }
509
        int                 GetUndoCount()                           { return undo.GetCount(); }
510
        void     GotoLine(int line);
511
        void     EnableBreakpointing()                    { bar.EnableBreakpointing(true); }
512
        void     DisableBreakpointing()                   { bar.EnableBreakpointing(false); }
513
        void     Renumber2();
514
        int      GetLine2(int i) const;
515

    
516
        void     HiliteScope(byte b)                      { hilite_scope = b; Refresh(); }
517
        void     HiliteBracket(byte b)                    { hilite_bracket = b; Refresh(); }
518
        void     HiliteIfDef(byte b)                      { hilite_ifdef = b; Refresh(); }
519
        void     HiliteIfEndif(bool b)                    { bar.HiliteIfEndif(b); }
520
        void     ThousandsSeparator(bool b)               { thousands_separator = b; Refresh(); }
521
        void     IndentSpaces(bool is)                    { indent_spaces = is; }
522
        void     IndentAmount(int ia)                     { indent_amount = ia; }
523
        void     NoParenthesisIndent(bool b)              { no_parenthesis_indent = b; }
524
        void     LineNumbers(bool b)                      { bar.LineNumbers(b); }
525
        void     MarkLines(bool b)                        { mark_lines = b; }
526
        bool     GetMarkLines()                           { return mark_lines; }
527
        void     AutoEnclose(bool b)                      { auto_enclose = b; }
528
        void     BarLine(bool b)                          { barline = b; }
529
        
530
        void     PersistentFindReplace(bool b = true)     { persistent_find_replace = b; }
531
        bool     IsPersistentFindReplace() const          { return persistent_find_replace; }
532
        
533
        void     Annotations(int width)                   { bar.Annotations(width); }
534
        void     SetAnnotation(int i, const Image& icon, const String& a) { bar.SetAnnotation(i, icon, a); }
535
        String   GetAnnotation(int i) const               { return bar.GetAnnotation(i); }
536
        int      GetActiveAnnotationLine() const          { return bar.GetActiveAnnotationLine(); }
537

    
538
        void     HideBar()                                { bar.Hide(); }
539

    
540
        void     DefaultHlStyles();
541
        void     LoadHlStyles(const char *s);
542
        String   StoreHlStyles();
543
        
544
        void     SyncTip();
545
        void     CloseTip()                               { if(tip.IsOpen()) tip.Close(); tip.d = NULL;  }
546

    
547
        const char *GetHlName(int i);
548
        bool        HasHlFont(int i);
549

    
550
        typedef CodeEditor CLASSNAME;
551

    
552
        CodeEditor();
553
        virtual ~CodeEditor();
554

    
555
        static const Index<String>& CppKeywords();
556
        static void InitKeywords();
557
};
558

    
559
END_UPP_NAMESPACE
560

    
561
#endif