Subversion Repositories Kolibri OS

Rev

Rev 8741 | Rev 8772 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8728 leency 1
(*
2
    Copyright 2021 Anton Krotov
3
 
4
    This file is part of CEdit.
5
 
6
    CEdit is free software: you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation, either version 3 of the License, or
9
    (at your option) any later version.
10
 
11
    CEdit is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with CEdit. If not, see .
18
*)
19
 
20
MODULE CEdit;
21
 
22
IMPORT
8762 leency 23
    OpenDlg, K := KolibriOS,
8728 leency 24
    U := Utils, Lines, Menu, List,
25
    G := Graph, T := Text, E := Encodings,
26
    CB := Clipboard, Languages,
27
    ChangeLog, Scroll,
8762 leency 28
    RW, Ini, box_lib, Icons;
8728 leency 29
 
30
CONST
8762 leency 31
    header = "CEdit (03-jun-2021)";
8728 leency 32
 
8762 leency 33
    ShellFilter = "";
8728 leency 34
    EditFilter = "SH|ASM|TXT|INC|OB07|C|CPP|H|PAS|PP|LUA|INI";
35
 
36
    fontWidth = K.fontWidth;
37
    fontHeight = K.fontHeight;
38
    scrollWidth = 22;
39
 
40
    btnClose = 1;
41
    btnNew = 20;
42
    btnOpen = 21;
43
    btnSave = 22;
44
    btnSearch = 23;
45
    btnUndo = 24;
46
    btnRedo = 25;
47
    btnUpper = 26;
48
    btnLower = 27;
49
    btnBuild = 28;
50
    btnRun = 29;
51
    btnUp = 30;
52
    btnDown = 31;
53
    btnLeft = 32;
54
    btnRight = 33;
55
    btnYes = 40;
56
    btnNo = 41;
57
    btnFindEdit = 50;
58
    btnReplaceEdit = 51;
59
    btnGotoEdit = 52;
60
    btnFindNext = 60;
61
    btnReplace = 61;
62
    btnReplaceAll = 62;
63
    btnGoto = 63;
64
    btnCloseFind = 64;
65
 
66
    btnFile = 70;
67
    btnEdit = 71;
68
    btnEncoding = 72;
69
    btnView = 73;
70
    btnSyntax = 74;
71
    btnProgram = 75;
72
    btnTools = 76;
73
 
74
    MainMenuHeight = fontHeight + 7;
75
 
76
    btnHeight = 25;
77
    btnWidth = 75;
78
    btnTop = MainMenuHeight + 3;
79
    toolBtnSize = 24;
80
    toolbarDelim = 7;
81
 
82
    TOP = btnTop + toolBtnSize + 7;
83
    RIGHT = scrollWidth - 2;
84
    BOTTOM = scrollWidth + 25;
85
 
8762 leency 86
    minWinWidth = 635; minWinHeight = 542;
8728 leency 87
 
88
    SEARCH_PADDING = 10;
89
    searchLeft = 10;
90
    EditBox_Width = 180;
91
    EDITBOX_MAXCHARS = 500;
92
 
93
    menuFileX = searchLeft;
94
    menuEditX = menuFileX + 4*fontWidth + 2 + 7;
95
    menuEncodingX = menuEditX + 4*fontWidth + 2 + 7;
96
    menuViewX = menuEncodingX + 8*fontWidth + 2 + 7;
97
    menuSyntaxX = menuViewX + 4*fontWidth + 2 + 7;
98
    menuProgramX = menuSyntaxX + 6*fontWidth + 2 + 7;
99
    menuToolsX = menuProgramX + 7*fontWidth + 2 + 7;
100
 
101
    menuCut = 1;
102
    menuCopy = 2;
103
    menuPaste = 3;
104
    menuDelete = 4;
105
    menuSelectAll = 5;
106
 
107
    menuUndo = 6;
108
    menuRedo = 7;
109
 
110
    menuSearch = 8;
111
    menuGoto = 9;
112
 
113
    menuNew = 10;
114
    menuOpen = 11;
115
    menuSave = 12;
116
    menuSaveAs = 13;
117
    menuFolder = 14;
118
    menuExit = 15;
119
 
120
    menuNumbers = 20;
121
    menuColors = 1000;
122
    menuMaxColors = menuColors + Ini.MAX_SECTIONS - 1;
123
 
124
    menuNone = 30;
125
    menuC = 31;
126
    menuFasm = 32;
127
    menuIni = 33;
128
    menuLua = 34;
129
    menuOberon = 35;
130
    menuPascal = 36;
131
 
132
    menuBuild = 50;
133
    menuBuildScript = 51;
134
    menuRun = 52;
135
    menuRunScript = 53;
136
    menuDebug = 54;
137
    menuDebugScript = 55;
138
 
139
    menuUTF8BOM = 60;
140
    menuUTF8 = 61;
141
    menuCP866 = 62;
142
    menuWin1251 = 63;
143
 
144
    menuPipet = 70;
145
    menuSysFunc = 71;
146
 
8762 leency 147
 
8728 leency 148
VAR
149
    canvas: G.tCanvas;
150
    font: G.tFont;
151
    text: T.tText;
152
    winWidth, winHeight: INTEGER;
153
    shift: SET;
154
    AppPath, runScript, buildScript, debugScript: RW.tFileName;
155
    OD: OpenDlg.Dialog;
156
    confirm, notFound, search, modified: BOOLEAN;
157
 
158
    leftButton, VScroll, HScroll: BOOLEAN;
159
    vScroll, hScroll: Scroll.tScroll;
160
    LEFT: INTEGER;
161
 
162
    FindEdit, ReplaceEdit, GotoEdit: box_lib.edit_box;
163
    CS, WH, BKW: box_lib.checkbox;
164
 
165
    new_searchText, searchText, replaceText, gotoText: T.tString;
166
    cs, whole: BOOLEAN;
167
 
168
    replaced: INTEGER;
169
 
170
    mainTID: INTEGER;
171
 
172
    context, menuFile, menuEdit, menuEncoding, menuView, menuSyntax, menuProgram, menuTools: Menu.tMenu;
173
 
174
    icons: INTEGER;
175
    grayIcons: INTEGER;
176
 
177
    IPC: ARRAY 64 OF INTEGER;
178
 
179
 
180
PROCEDURE WritePos (y: INTEGER);
181
VAR
182
    s1, s2: ARRAY 32 OF WCHAR;
183
    line, col: INTEGER;
184
BEGIN
185
    T.getPos(text, col, line);
186
    U.int2str(line, s1);
187
    U.int2str(col, s2);
188
    U.append(s1, ": ");
189
    U.append(s1, s2);
8762 leency 190
    K.DrawText(LEFT, y, K.textColor, s1)
8728 leency 191
END WritePos;
192
 
193
 
194
PROCEDURE EditBox_Focus (edit: box_lib.edit_box): BOOLEAN;
195
    RETURN 1 IN BITS(edit.flags)
196
END EditBox_Focus;
197
 
198
 
199
PROCEDURE EditBox_SetFocus (edit: box_lib.edit_box; value: BOOLEAN);
200
BEGIN
201
    IF value THEN
202
        edit.flags := ORD(BITS(edit.flags) + {1})
203
    ELSE
204
        edit.flags := ORD(BITS(edit.flags) - {1})
205
    END;
206
    IF search THEN
207
        box_lib.edit_box_draw(edit)
208
    END
209
END EditBox_SetFocus;
210
 
211
 
212
PROCEDURE Rect (left, top, right, bottom, color: INTEGER);
213
BEGIN
214
    K.DrawLine(left, top, right, top, color);
215
    K.DrawLine(left, bottom, right, bottom, color);
216
    K.DrawLine(left, top, left, bottom, color);
217
    K.DrawLine(right, top, right, bottom, color);
218
END Rect;
219
 
220
 
221
PROCEDURE Message (s: ARRAY OF WCHAR);
222
CONST
223
    minWidth = 30;
224
    height = 40;
225
    borderColor = 808080H;
226
VAR
227
    top, left, right, bottom, x, y, width: INTEGER;
228
BEGIN
229
    width := minWidth + LENGTH(s)*fontWidth;
230
    left := (canvas.width - width) DIV 2 + LEFT;
231
    top := (canvas.height - height) DIV 2 + TOP;
232
    bottom := top + height - 1;
233
    right := left + width - 1;
234
    x := minWidth DIV 2 + left;
235
    y := (height - fontHeight) DIV 2 + top;
8762 leency 236
    K.DrawRect(left, top, width, height, K.winColor);
8728 leency 237
    Rect(left, top, right, bottom, borderColor);
8762 leency 238
    K.DrawText(x, y, K.textColor, s);
8728 leency 239
END Message;
240
 
241
 
242
PROCEDURE NotFound;
243
BEGIN
244
    IF notFound THEN
245
        Message("not found");
246
        notFound := FALSE;
247
        EditBox_SetFocus(FindEdit, TRUE);
248
        EditBox_SetFocus(ReplaceEdit, FALSE);
249
        EditBox_SetFocus(GotoEdit, FALSE)
250
    END
251
END NotFound;
252
 
253
 
254
PROCEDURE Replaced;
255
VAR
256
    s, s1: ARRAY 32 OF WCHAR;
257
BEGIN
258
    IF replaced # 0 THEN
259
        s := "replaced: ";
260
        U.int2str(replaced, s1);
261
        U.append(s, s1);
262
        Message(s);
263
        replaced := 0;
264
        EditBox_SetFocus(FindEdit, TRUE);
265
        EditBox_SetFocus(ReplaceEdit, FALSE);
266
        EditBox_SetFocus(GotoEdit, FALSE)
267
    END
268
END Replaced;
269
 
270
 
271
PROCEDURE toolbarIcons;
272
CONST
273
    iconPad = (toolBtnSize - 16) DIV 2;
274
VAR
275
    x, color: INTEGER;
276
BEGIN
277
    x := searchLeft + (toolBtnSize + 5)*2;
278
    IF text.modified THEN
8762 leency 279
        Icons.draw(icons, 5, x + iconPad, btnTop + iconPad)
8728 leency 280
    ELSE
8762 leency 281
        Icons.draw(grayIcons, 5, x + iconPad, btnTop + iconPad)
8728 leency 282
    END;
283
 
284
    IF text.edition # NIL THEN
285
        x := searchLeft + (toolBtnSize + 5)*4 + toolbarDelim*2;
286
        IF ChangeLog.isFirstGuard(text.edition) THEN
8762 leency 287
            Icons.draw(grayIcons, 37, x + iconPad, btnTop + iconPad)
8728 leency 288
        ELSE
8762 leency 289
            Icons.draw(icons, 37, x + iconPad, btnTop + iconPad)
8728 leency 290
        END;
291
 
292
        x := searchLeft + (toolBtnSize + 5)*5 + toolbarDelim*2;
293
        IF ChangeLog.isLastGuard(text.edition) THEN
8762 leency 294
            Icons.draw(grayIcons, 36, x + iconPad, btnTop + iconPad)
8728 leency 295
        ELSE
8762 leency 296
            Icons.draw(icons, 36, x + iconPad, btnTop + iconPad)
8728 leency 297
        END
298
    END;
299
 
300
    IF T.selected(text) THEN
8762 leency 301
        color := K.caseColor
8728 leency 302
    ELSE
8762 leency 303
        color := K.disCaseColor
8728 leency 304
    END;
305
 
306
    x := searchLeft + (toolBtnSize + 5)*6 + toolbarDelim*3;
8762 leency 307
    K.DrawRect(x, btnTop, toolBtnSize, toolBtnSize, K.toolbarColor);
8728 leency 308
    K.DrawText69(x + (toolBtnSize - 12) DIV 2, btnTop + (toolBtnSize - 9) DIV 2 + 2, color, "AB");
309
    INC(x, toolBtnSize + 5);
8762 leency 310
    K.DrawRect(x, btnTop, toolBtnSize, toolBtnSize, K.toolbarColor);
8728 leency 311
    K.DrawText69(x + (toolBtnSize - 12) DIV 2, btnTop + (toolBtnSize - 9) DIV 2 + 2, color, "ab");
312
 
313
    x := searchLeft + (toolBtnSize + 5)*8 + toolbarDelim*4;
314
    IF buildScript # "" THEN
8762 leency 315
        Icons.draw(icons, 54, x + iconPad, btnTop + iconPad)
8728 leency 316
    ELSE
8762 leency 317
        Icons.draw(grayIcons, 54, x + iconPad, btnTop + iconPad)
8728 leency 318
    END;
319
    INC(x, toolBtnSize + 5);
320
    IF runScript # "" THEN
8762 leency 321
        Icons.draw(icons, 53, x + iconPad, btnTop + iconPad)
8728 leency 322
    ELSE
8762 leency 323
        Icons.draw(grayIcons, 53, x + iconPad, btnTop + iconPad)
8728 leency 324
    END
325
END toolbarIcons;
326
 
327
 
328
PROCEDURE WriteModified (x, y: INTEGER);
329
BEGIN
330
    modified := text.modified;
8762 leency 331
    K.DrawRect(x, TOP + canvas.height + scrollWidth - 1, 9*fontWidth, BOTTOM - scrollWidth + 1, K.winColor);
8728 leency 332
    IF modified THEN
8762 leency 333
        K.DrawText866(x, y, K.textColor, "modified")
8728 leency 334
    END
335
END WriteModified;
336
 
337
 
338
PROCEDURE repaint;
339
VAR
340
    width, height, scrollX, scrollY, y: INTEGER;
341
BEGIN
342
    IF text # NIL THEN
343
        IF confirm THEN
344
            K.DeleteButton(btnYes);
345
            K.DeleteButton(btnNo);
346
            confirm := FALSE
347
        END;
348
        T.draw(text);
349
        K.ClientSize(width, height);
350
        y := height - (BOTTOM - scrollWidth) + (BOTTOM - scrollWidth - 16) DIV 2;
8762 leency 351
        K.DrawRect(LEFT, TOP + canvas.height + scrollWidth - 1, 16*fontWidth, BOTTOM - scrollWidth + 1, K.winColor);
8728 leency 352
        WritePos(y);
353
 
354
        IF modified # text.modified THEN
355
            WriteModified(width - 9*fontWidth, y)
356
        END;
357
 
358
        T.getScroll(text, scrollX, scrollY);
359
        vScroll.value := scrollY; vScroll.maxVal := text.count - 1;
360
        hScroll.value := scrollX; hScroll.maxVal := Lines.maxLength;
361
        Scroll.draw(vScroll, LEFT + canvas.width - 1, TOP + scrollWidth - 1);
362
        Scroll.draw(hScroll, LEFT + scrollWidth, TOP + canvas.height - 1);
363
 
364
        G.DrawCanvas(canvas, LEFT, TOP);
365
        NotFound;
366
        Replaced;
367
        toolbarIcons
368
    END
369
END repaint;
370
 
371
 
372
PROCEDURE resize;
373
VAR
374
    cliWidth, cliHeight: INTEGER;
375
BEGIN
376
    K.WinSize(winWidth, winHeight);
377
    IF winWidth < minWinWidth THEN
378
        winWidth := minWinWidth
379
    END;
380
    IF winHeight < minWinHeight THEN
381
        winHeight := minWinHeight
382
    END;
383
    K.SetWinSize(winWidth, winHeight);
384
    K.WinSize(winWidth, winHeight);
385
    K.ClientSize(cliWidth, cliHeight);
386
    G.destroy(canvas);
387
    canvas := G.CreateCanvas(cliWidth - (LEFT + RIGHT + 1), cliHeight - (TOP + BOTTOM));
388
    G.SetFont(canvas, font);
389
    T.setCanvas(canvas);
390
    T.resize(canvas.width, canvas.height);
391
    Scroll.resize(vScroll, vScroll.xSize, canvas.height - scrollWidth*2 + 1);
392
    Scroll.resize(hScroll, canvas.width - scrollWidth*2, hScroll.ySize);
393
END resize;
394
 
395
 
396
PROCEDURE SearchPanel (left, top: INTEGER);
397
VAR
398
    y, right, bottom, color: INTEGER;
399
BEGIN
400
    right := left + EditBox_Width + SEARCH_PADDING*2;
401
    bottom := top + 395 + btnHeight + SEARCH_PADDING;
8762 leency 402
    color := K.borderColor;
8728 leency 403
    Rect(left, top, right, bottom, color);
404
    K.CreateButton(btnCloseFind, right - 20, top, 20, 20, 0EF999FH, "");
405
    K.DrawLine(right - 14, top + 5, right - 5, top + 14, 0FFFFFFH);
406
    K.DrawLine(right - 15, top + 5, right - 5, top + 15, 0FFFFFFH);
407
    K.DrawLine(right - 15, top + 6, right - 6, top + 15, 0FFFFFFH);
408
    K.DrawLine(right - 15, top + 14, right - 6, top + 5, 0FFFFFFH);
409
    K.DrawLine(right - 15, top + 15, right - 5, top + 5, 0FFFFFFH);
410
    K.DrawLine(right - 14, top + 15, right - 5, top + 6, 0FFFFFFH);
411
 
412
    INC(top, 15);
413
    INC(left, SEARCH_PADDING);
8762 leency 414
    K.DrawText866(left, top, K.textColor, "find");
415
    K.DrawText866(left, top + 55, K.textColor, "replace with");
8728 leency 416
    K.CreateButton(btnFindEdit + ORD({30}), left, top + 20, EditBox_Width, fontHeight + 5, 0, "");
417
    K.CreateButton(btnReplaceEdit + ORD({30}), left, top + 75, EditBox_Width, fontHeight + 5, 0, "");
8762 leency 418
    K.DrawText866(left, top + 330, K.textColor, "go to line");
8728 leency 419
    K.CreateButton(btnGotoEdit + ORD({30}), left, top + 350, EditBox_Width, fontHeight + 5, 0, "");
420
    BKW.top_s := BKW.top_s MOD 65536 + (top + 110) * 65536;
421
    CS.top_s := CS.top_s MOD 65536 + (top + 140) * 65536;
422
    WH.top_s := WH.top_s MOD 65536 + (top + 170) * 65536;
423
    BKW.left_s := BKW.left_s MOD 65536 + left * 65536;
424
    CS.left_s := CS.left_s MOD 65536 + left * 65536;
425
    WH.left_s := WH.left_s MOD 65536 + left * 65536;
426
    FindEdit.top := top + 20;
427
    ReplaceEdit.top := top + 75;
428
    GotoEdit.top := top + 350;
429
    FindEdit.left := left;
430
    ReplaceEdit.left := left;
431
    GotoEdit.left := left;
432
    box_lib.edit_box_draw(FindEdit);
433
    box_lib.edit_box_draw(ReplaceEdit);
434
    box_lib.edit_box_draw(GotoEdit);
8762 leency 435
    box_lib.check_box_draw2(BKW); K.DrawText866(left + 20, top + 110, K.textColor, "backward");
436
    box_lib.check_box_draw2(CS); K.DrawText866(left + 20, top + 140, K.textColor, "match case");
437
    box_lib.check_box_draw2(WH); K.DrawText866(left + 20, top + 170, K.textColor, "whole word");
8728 leency 438
    y := top + 200;
8762 leency 439
    K.CreateButton(btnFindNext, left, y, btnWidth, btnHeight, K.btnColor, "next"); INC(y, btnHeight + 10);
440
    K.CreateButton(btnReplace, left, y, btnWidth, btnHeight, K.btnColor, "replace"); INC(y, btnHeight + 10);
441
    K.CreateButton(btnReplaceAll, left, y, btnWidth + 5*fontWidth - 2, btnHeight, K.btnColor, "replace all");
442
    K.CreateButton(btnGoto, left, top + 380, btnWidth, btnHeight, K.btnColor, "go");
8728 leency 443
END SearchPanel;
444
 
445
 
446
PROCEDURE draw_window;
447
CONST
448
    iconPad = (toolBtnSize - 16) DIV 2;
449
VAR
450
    width, height, x, y: INTEGER;
451
 
452
 
453
    PROCEDURE drawToolbarBtn (id, x: INTEGER);
454
    BEGIN
8762 leency 455
        K.DrawRect(x, btnTop, toolBtnSize, toolBtnSize, K.toolbarColor);
456
        K.DrawLine(x, btnTop + toolBtnSize, x + toolBtnSize, btnTop + toolBtnSize, K.shadowColor);
457
        K.DrawLine(x + toolBtnSize, btnTop, x + toolBtnSize, btnTop + toolBtnSize, K.shadowColor);
458
        K.CreateButton(id + ORD({30}), x, btnTop, toolBtnSize, toolBtnSize, K.btnColor, "");
8728 leency 459
    END drawToolbarBtn;
460
 
461
 
462
    PROCEDURE drawMainMenu (menu: Menu.tMenu; x: INTEGER; btn: INTEGER; caption: ARRAY OF WCHAR);
463
    VAR
464
        menuColor, n: INTEGER;
465
    BEGIN
466
        IF menu.tid # 0 THEN
8762 leency 467
            menuColor := K.selMenuColor
8728 leency 468
        ELSE
8762 leency 469
            menuColor := K.winColor
8728 leency 470
        END;
471
        n := LENGTH(caption);
472
        K.DrawRect(x, 0, n*fontWidth + 2, MainMenuHeight, menuColor);
8762 leency 473
        K.CreateButton(btn + ORD({30}), x, 0, n*fontWidth + 2, MainMenuHeight, K.btnColor, "");
474
        K.DrawText(x + 1, (MainMenuHeight - fontHeight) DIV 2 + 1, K.textColor, caption)
8728 leency 475
    END drawMainMenu;
476
 
477
 
478
BEGIN
479
    K.BeginDraw;
8762 leency 480
    K.CreateWindow(50 + K.GetTickCount() MOD 128, 50 + K.GetTickCount() MOD 128, winWidth, winHeight, K.winColor, 73H, 0, 0, header);
8728 leency 481
    IF (text # NIL) & ~K.RolledUp() THEN
482
        confirm := FALSE;
483
        K.ClientSize(width, height);
484
 
8762 leency 485
        K.DrawRect(0, 0, width, TOP, K.winColor);
486
        K.DrawRect(0, 0, LEFT, height, K.winColor);
487
        K.DrawRect(LEFT + canvas.width - 1, TOP + canvas.height - 1, scrollWidth, scrollWidth, K.winColor);
8728 leency 488
 
489
        drawMainMenu(menuFile, menuFileX, btnFile, "file");
490
        drawMainMenu(menuEdit, menuEditX, btnEdit, "edit");
491
        drawMainMenu(menuEncoding, menuEncodingX, btnEncoding, "encoding");
492
        drawMainMenu(menuView, menuViewX, btnView, "view");
493
        drawMainMenu(menuSyntax, menuSyntaxX, btnSyntax, "syntax");
494
        drawMainMenu(menuProgram, menuProgramX, btnProgram, "program");
495
        drawMainMenu(menuTools, menuToolsX, btnTools, "tools");
496
 
497
        x := searchLeft;
498
 
499
        drawToolbarBtn(btnNew, x);
8762 leency 500
        Icons.draw(icons, 2, x + iconPad, btnTop + iconPad);
8728 leency 501
        INC(x, toolBtnSize + 5);
502
 
503
        drawToolbarBtn(btnOpen, x);
8762 leency 504
        Icons.draw(icons, 0, x + iconPad, btnTop + iconPad);
8728 leency 505
        INC(x, toolBtnSize + 5);
506
 
507
        drawToolbarBtn(btnSave, x);
508
        INC(x, toolBtnSize + 5 + toolbarDelim);
509
 
510
        drawToolbarBtn(btnSearch, x);
8762 leency 511
        Icons.draw(icons, 49, x + iconPad, btnTop + iconPad);
8728 leency 512
        INC(x, toolBtnSize + 5 + toolbarDelim);
513
 
514
        drawToolbarBtn(btnUndo, x);
515
        INC(x, toolBtnSize + 5);
516
 
517
        drawToolbarBtn(btnRedo, x);
518
        INC(x, toolBtnSize + 5 + toolbarDelim);
519
 
520
        drawToolbarBtn(btnUpper, x);
521
        INC(x, toolBtnSize + 5);
522
 
523
        drawToolbarBtn(btnLower, x);
524
        INC(x, toolBtnSize + 5 + toolbarDelim);
525
 
526
        drawToolbarBtn(btnBuild, x);
8762 leency 527
        Icons.draw(icons, 54, x + iconPad, btnTop + iconPad);
8728 leency 528
        INC(x, toolBtnSize + 5);
529
 
530
        drawToolbarBtn(btnRun, x);
8762 leency 531
        Icons.draw(icons, 53, x + iconPad, btnTop + iconPad);
8728 leency 532
        INC(x, toolBtnSize + 5);
533
 
8762 leency 534
        K.CreateButton(btnUp, LEFT + canvas.width - 1, TOP, scrollWidth - 1, scrollWidth, K.btnColor, 0X);
535
        K.DrawText69(LEFT + canvas.width - 1 + (scrollWidth - 6) DIV 2, TOP + (scrollWidth - 9) DIV 2, K.btnTextColor, 18X);
536
        K.CreateButton(btnDown, LEFT + canvas.width - 1, TOP + canvas.height - scrollWidth - 1, scrollWidth - 1, scrollWidth, K.btnColor, 0X);
537
        K.DrawText69(LEFT + canvas.width - 1 + (scrollWidth - 6) DIV 2, TOP + canvas.height - scrollWidth + (scrollWidth - 9) DIV 2, K.btnTextColor, 19X);
8728 leency 538
 
8762 leency 539
        K.CreateButton(btnLeft, LEFT, TOP + canvas.height - 1, scrollWidth, scrollWidth - 1, K.btnColor, 0X);
540
        K.DrawText69(LEFT + (scrollWidth - 6) DIV 2, TOP + canvas.height - 1 + (scrollWidth - 9) DIV 2 + 1, K.btnTextColor, 1BX);
541
        K.CreateButton(btnRight, LEFT + canvas.width - scrollWidth - 1, TOP + canvas.height - 1, scrollWidth, scrollWidth - 1, K.btnColor, 0X);
542
        K.DrawText69(LEFT + canvas.width - scrollWidth - 1 + (scrollWidth - 6) DIV 2, TOP + canvas.height - 1 + (scrollWidth - 9) DIV 2 + 1, K.btnTextColor, 1AX);
8728 leency 543
 
8762 leency 544
        K.PutPixel(LEFT + canvas.width + scrollWidth - 2, TOP, K.winColor);
545
        K.PutPixel(LEFT, TOP + canvas.height + scrollWidth - 2, K.winColor);
546
 
8728 leency 547
        y := (btnHeight - fontHeight) DIV 2 + btnTop;
548
        CASE text.enc OF
8762 leency 549
        |E.UTF8:    K.DrawText866(width -  6*fontWidth, y, K.textColor, "UTF-8")
550
        |E.UTF8BOM: K.DrawText866(width - 10*fontWidth, y, K.textColor, "UTF-8-BOM")
551
        |E.CP866:   K.DrawText866(width -  6*fontWidth, y, K.textColor, "CP866")
552
        |E.W1251:   K.DrawText866(width - 13*fontWidth, y, K.textColor, "Windows-1251")
8728 leency 553
        END;
554
        IF search THEN
555
            SearchPanel(searchLeft, TOP)
556
        END;
557
 
558
        y := height - (BOTTOM - scrollWidth) + (BOTTOM - scrollWidth - 16) DIV 2;
8762 leency 559
        K.DrawRect(LEFT + 16*fontWidth, TOP + canvas.height + scrollWidth - 1, width - LEFT - 25*fontWidth, BOTTOM - scrollWidth + 1, K.winColor);
560
        K.DrawText866(LEFT + 16*fontWidth, y, K.textColor, text.fileName);
8728 leency 561
        WriteModified(width - 9*fontWidth, y);
562
        repaint
563
    END;
564
    K.EndDraw
565
END draw_window;
566
 
567
 
568
PROCEDURE mouse (VAR x, y: INTEGER);
569
VAR
570
    mouseX, mouseY,
571
    cliX, cliY,
572
    winX, winY: INTEGER;
573
BEGIN
574
    K.MousePos(mouseX, mouseY);
575
    K.WinPos(winX, winY);
576
    K.ClientPos(cliX, cliY);
577
    x := mouseX - winX - cliX - LEFT;
578
    y := mouseY - winY - cliY - TOP;
579
END mouse;
580
 
581
 
582
PROCEDURE getKBState;
583
VAR
584
    kbState: SET;
585
BEGIN
586
    kbState := K.GetControlKeys();
587
    IF {0, 1} * kbState # {} THEN
588
        INCL(shift, T.SHIFT)
589
    ELSE
590
        EXCL(shift, T.SHIFT)
591
    END;
592
 
593
    IF {2, 3} * kbState # {} THEN
594
        INCL(shift, T.CTRL)
595
    ELSE
596
        EXCL(shift, T.CTRL)
597
    END
598
END getKBState;
599
 
600
 
601
PROCEDURE OpenFile (VAR FileName: RW.tFileName; filter: ARRAY OF CHAR);
602
BEGIN
603
    OpenDlg.SetFilter(OD, filter);
604
    OpenDlg.Show(OD, 500, 400);
605
    WHILE OD.status = 2 DO
606
        K.Pause(30)
607
    END;
608
    IF OD.status = 1 THEN
609
        COPY(OD.FilePath, FileName)
610
    ELSE
611
        FileName := ""
612
    END
613
END OpenFile;
614
 
615
 
616
PROCEDURE error (s: RW.tFileName);
617
BEGIN
618
    K.Run("/rd/1/@notify", s)
619
END error;
620
 
621
 
622
PROCEDURE saveAs;
623
VAR
624
    fileName: RW.tFileName;
625
    ext: ARRAY 8 OF CHAR;
626
BEGIN
627
    OD._type := OpenDlg.tsave;
628
    U.getFileName(text.fileName, OD.FileName, U.SLASH);
629
    IF OD.FileName = "" THEN
630
        OD.FileName := "NewFile.";
631
        CASE text.lang OF
632
        |Languages.langNone:   ext := "txt"
633
        |Languages.langC:      ext := "c"
634
        |Languages.langFasm:   ext := "asm"
635
        |Languages.langIni:    ext := "ini"
636
        |Languages.langLua:    ext := "lua"
637
        |Languages.langOberon: ext := "ob07"
638
        |Languages.langPascal: ext := "pas"
639
        END;
640
        U.append8(OD.FileName, ext)
641
    END;
642
    OpenFile(fileName, EditFilter);
643
    IF fileName # "" THEN
644
        IF T.save(text, fileName, text.enc, RW.EOL_CRLF) THEN
645
            T.setName(text, fileName)
646
        ELSE
647
            error("'cedit: error saving file' -E")
648
        END
649
    END
650
END saveAs;
651
 
652
 
653
PROCEDURE save;
654
BEGIN
655
    IF text.modified THEN
656
        IF text.fileName # "" THEN
657
            IF ~T.save(text, text.fileName, text.enc, RW.EOL_CRLF) THEN
658
                error("'cedit: error saving file' -E")
659
            END
660
        ELSE
661
            saveAs
662
        END
663
    END
664
END save;
665
 
666
 
667
PROCEDURE SelfRun (file: ARRAY OF CHAR);
668
BEGIN
669
    K.Run(AppPath, file)
670
END SelfRun;
671
 
672
 
673
PROCEDURE open;
674
VAR
675
    fileName: RW.tFileName;
676
BEGIN
677
    OD._type := OpenDlg.topen;
678
    OpenFile(fileName, EditFilter);
679
    IF fileName # "" THEN
680
        SelfRun(fileName)
681
    END
682
END open;
683
 
684
 
685
PROCEDURE Confirm;
686
CONST
687
    width  = btnWidth*2 + 30;
688
    height = btnHeight*2 + 20;
689
    lineColor = 808080H;
690
VAR
691
    left, top, right, bottom: INTEGER;
692
BEGIN
693
    draw_window;
694
    confirm := TRUE;
695
    left := (canvas.width - width) DIV 2 + LEFT;
696
    top := (canvas.height - height) DIV 2 + TOP;
697
    right := left + width - 1;
698
    bottom := top + height - 1;
8762 leency 699
    K.DrawRect(left, top, width, height, K.winColor);
8728 leency 700
    Rect(left, top, right, bottom, lineColor);
8762 leency 701
    K.DrawText866(left + (width - 10*fontWidth) DIV 2, top + 10, K.textColor, "save file?");
702
    K.CreateButton(btnYes, left + 10, top + 35, btnWidth, btnHeight, K.btnColor, "yes");
703
    K.CreateButton(btnNo, left + 20 + btnWidth, top + 35, btnWidth, btnHeight, K.btnColor, "no");
8728 leency 704
END Confirm;
705
 
706
 
707
PROCEDURE createEdit (left, top: INTEGER): box_lib.edit_box;
708
VAR
709
    edit, EditBox0: box_lib.edit_box;
710
BEGIN
711
    NEW(EditBox0);
712
    EditBox0.text := K.malloc(EDITBOX_MAXCHARS + 2);
713
    ASSERT(EditBox0.text # 0);
714
    edit := box_lib.kolibri_new_edit_box(left, top, EditBox_Width, EDITBOX_MAXCHARS, EditBox0);
715
    edit.flags := 4002H;
716
    edit.text_color := 30000000H;
717
    EditBox_SetFocus(edit, FALSE)
718
    RETURN edit
719
END createEdit;
720
 
721
 
722
PROCEDURE createSearchForm;
723
BEGIN
724
    FindEdit := createEdit(searchLeft, TOP + 20);
725
    ReplaceEdit := createEdit(searchLeft, TOP + 20 + 55);
726
    GotoEdit := createEdit(searchLeft, TOP + 20 + 330);
727
    GotoEdit.flags := ORD(BITS(GotoEdit.flags) + BITS(8000H));
728
    BKW := box_lib.kolibri_new_check_box(searchLeft, TOP + 90 + 20, 16, 16, "", 8*fontWidth + 4);
729
    CS := box_lib.kolibri_new_check_box(searchLeft, TOP + 120 + 20, 16, 16, "", 10*fontWidth + 4);
730
    WH := box_lib.kolibri_new_check_box(searchLeft, TOP + 150 + 20, 16, 16, "", 10*fontWidth + 4);
731
END createSearchForm;
732
 
733
 
734
PROCEDURE EditBox_GetValue (edit: box_lib.edit_box; VAR s: ARRAY OF WCHAR);
735
VAR
736
    str: ARRAY EDITBOX_MAXCHARS + 1 OF CHAR;
737
    i: INTEGER;
738
BEGIN
739
    box_lib.edit_box_get_value(edit, str);
740
    i := 0;
741
    WHILE str[i] # 0X DO
742
        s[i] := WCHR(E.cp866[ORD(str[i])]);
743
        INC(i)
744
    END;
745
    s[i] := 0X
746
END EditBox_GetValue;
747
 
748
 
749
PROCEDURE Search;
750
BEGIN
751
    search := ~search;
752
    IF search THEN
753
        LEFT := searchLeft + EditBox_Width + SEARCH_PADDING*3;
754
        IF T.search(text, searchText, cs, whole) THEN END
755
    ELSE
756
        LEFT := searchLeft;
757
        IF T.search(text, "", FALSE, FALSE) THEN END
758
    END;
759
    EditBox_SetFocus(FindEdit, search);
760
    EditBox_SetFocus(ReplaceEdit, FALSE);
761
    EditBox_SetFocus(GotoEdit, FALSE);
762
    resize;
763
    draw_window
764
END Search;
765
 
766
 
767
PROCEDURE click (x, y: INTEGER): INTEGER;
768
VAR
769
    scrollX, scrollY: INTEGER;
770
BEGIN
771
    IF (0 <= x) & (x < canvas.width) & (0 <= y) & (y < canvas.height) THEN
772
        leftButton := TRUE;
773
        EditBox_SetFocus(FindEdit, FALSE);
774
        EditBox_SetFocus(ReplaceEdit, FALSE);
775
        EditBox_SetFocus(GotoEdit, FALSE);
776
        IF ~(T.SHIFT IN shift) THEN
777
            T.resetSelect(text)
778
        END;
779
        T.mouse(text, x, y);
780
        repaint
781
    ELSIF (canvas.width < x) & (x < canvas.width + scrollWidth) & (scrollWidth < y) & (y < canvas.height - scrollWidth) THEN
782
        VScroll := TRUE;
783
        DEC(x, canvas.width);
784
        DEC(y, scrollWidth);
785
        Scroll.mouse(vScroll, x, y);
786
        T.getScroll(text, scrollX, scrollY);
787
        T.scroll(text, 0, vScroll.value - scrollY);
788
        repaint
789
    ELSIF (scrollWidth < x) & (x < canvas.width - scrollWidth) & (canvas.height < y) & (y < canvas.height + scrollWidth) THEN
790
        HScroll := TRUE;
791
        DEC(x, scrollWidth);
792
        DEC(y, canvas.height);
793
        Scroll.mouse(hScroll, x, y);
794
        T.getScroll(text, scrollX, scrollY);
795
        T.scroll(text, hScroll.value - scrollX, 0);
796
        repaint
797
    END
798
    RETURN K.GetTickCount()
799
END click;
800
 
801
 
802
PROCEDURE LeftButtonUp;
803
BEGIN
804
    leftButton := FALSE;
805
    VScroll := FALSE;
806
    HScroll := FALSE;
807
    Scroll.MouseUp(vScroll);
808
    Scroll.MouseUp(hScroll);
809
END LeftButtonUp;
810
 
811
 
812
PROCEDURE close;
813
BEGIN
814
    IF text.modified THEN
815
        Confirm
816
    ELSE
817
        K.Exit
818
    END
819
END close;
820
 
821
 
822
PROCEDURE MenuItemClick (menu: Menu.tMenu; id: INTEGER);
823
BEGIN
824
    K.SendIPC(mainTID, id)
825
END MenuItemClick;
826
 
827
 
828
PROCEDURE goto;
829
VAR
830
    gotoVal: INTEGER;
831
BEGIN
832
    EditBox_GetValue(GotoEdit, gotoText);
833
    IF U.str2int(gotoText, gotoVal) & T.goto(text, gotoVal) THEN END
834
END goto;
835
 
836
 
837
PROCEDURE Script (script: ARRAY OF CHAR);
838
BEGIN
839
    IF script # "" THEN
840
        K.Run("/rd/1/@open", script)
841
    END
842
END Script;
843
 
844
 
845
PROCEDURE receiveIPC;
846
BEGIN
847
    IF IPC[0] # Menu.lastTID THEN
848
        IPC[2] := 0
849
    END;
850
    CASE IPC[2] OF
851
    |0:
852
    |menuCut:       T.key(text, ORD("X"), {T.CTRL})
853
    |menuCopy:      T.key(text, ORD("C"), {T.CTRL})
854
    |menuPaste:     T.key(text, ORD("V"), {T.CTRL})
855
    |menuDelete:    T.key(text, 46, {})
856
    |menuSelectAll: T.key(text, ORD("A"), {T.CTRL})
857
 
858
    |menuNew:
859
        SelfRun("")
860
    |menuOpen:
861
        open
862
    |menuSave:
863
        save;
864
        repaint
865
    |menuSaveAs:
866
        saveAs;
867
        repaint
868
    |menuFolder:
869
        K.Run("/rd/1/File Managers/Eolite", text.fileName)
870
    |menuExit:
871
        close
872
    |menuUndo:
873
        T.undo(text);
874
        repaint
875
    |menuRedo:
876
        T.redo(text);
877
        repaint
878
    |menuSearch:
879
        IF ~search THEN
880
            Search
881
        END;
882
        EditBox_SetFocus(FindEdit, TRUE);
883
        EditBox_SetFocus(ReplaceEdit, FALSE);
884
        EditBox_SetFocus(GotoEdit, FALSE)
885
    |menuGoto:
886
        IF ~search THEN
887
            Search
888
        END;
889
        EditBox_SetFocus(GotoEdit, TRUE);
890
        EditBox_SetFocus(FindEdit, FALSE);
891
        EditBox_SetFocus(ReplaceEdit, FALSE)
892
    |menuNumbers:
893
        T.toggleNumbers(text)
894
    |menuNone:
895
        T.setLang(text, Languages.langNone)
896
    |menuC:
897
        T.setLang(text, Languages.langC)
898
    |menuFasm:
899
        T.setLang(text, Languages.langFasm)
900
    |menuIni:
901
        T.setLang(text, Languages.langIni)
902
    |menuLua:
903
        T.setLang(text, Languages.langLua)
904
    |menuOberon:
905
        T.setLang(text, Languages.langOberon)
906
    |menuPascal:
907
        T.setLang(text, Languages.langPascal)
908
    |menuBuild:
909
        Script(buildScript)
910
    |menuBuildScript:
911
        OpenFile(buildScript, ShellFilter)
912
    |menuRun:
913
        Script(runScript)
914
    |menuRunScript:
915
        OpenFile(runScript, ShellFilter)
916
    |menuDebug:
917
        Script(debugScript)
918
    |menuDebugScript:
919
        OpenFile(debugScript, ShellFilter)
920
    |menuUTF8BOM:
921
        text.enc := E.UTF8BOM
922
    |menuUTF8:
923
        text.enc := E.UTF8
924
    |menuCP866:
925
        text.enc := E.CP866
926
    |menuWin1251:
927
        text.enc := E.W1251
928
    |menuPipet:
929
        K.Run("/rd/1/develop/pipet", "")
930
    |menuSysFunc:
931
        K.Run("/rd/1/docpack", "f")
932
    |menuColors..menuMaxColors:
933
        Ini.selectSection(IPC[2] - menuColors)
934
    END;
935
    IPC[0] := 0;
936
    IPC[1] := 0
937
END receiveIPC;
938
 
939
 
940
PROCEDURE MenuKeyDown (menu: Menu.tMenu; key: INTEGER): BOOLEAN;
941
VAR
942
    menuItem: INTEGER;
943
BEGIN
944
    menuItem := -1;
945
    getKBState;
946
    IF (T.CTRL IN shift) THEN
947
        CASE key DIV 65536 OF
948
        |21: menuItem := menuRedo
949
        |30: menuItem := menuSelectAll
950
        |33: menuItem := menuSearch
951
        |34: menuItem := menuGoto
952
        |44: menuItem := menuUndo
953
        |45: menuItem := menuCut
954
        |46: menuItem := menuCopy
955
        |47: menuItem := menuPaste
956
        |24: menuItem := menuOpen
957
        |31: menuItem := menuSave
958
        |49: menuItem := menuNew
959
        |67: menuItem := menuBuild
960
        ELSE
961
        END
962
    ELSE
963
        IF key DIV 65536 = 83 THEN
964
            menuItem := menuDelete
965
        ELSIF key DIV 65536 = 67 THEN
966
            menuItem := menuRun
967
        END
968
    END;
969
    IF menuItem # -1 THEN
970
        IF Menu.isEnabled(menu, menuItem) THEN
971
            MenuItemClick(menu, menuItem)
972
        ELSE
973
            menuItem := -1
974
        END
975
    END
976
    RETURN menuItem # -1
977
END MenuKeyDown;
978
 
979
 
980
PROCEDURE CreateContextMenu (): Menu.tMenu;
981
VAR
982
    menu: List.tList;
983
BEGIN
984
    menu := List.create(NIL);
985
    Menu.AddMenuItem(menu, menuUndo,      "undo         ctrl-Z");
986
    Menu.AddMenuItem(menu, menuRedo,      "redo         ctrl-Y");
987
    Menu.delimiter(menu);
988
    Menu.AddMenuItem(menu, menuCut,       "cut          ctrl-X");
989
    Menu.AddMenuItem(menu, menuCopy,      "copy         ctrl-C");
990
    Menu.AddMenuItem(menu, menuPaste,     "paste        ctrl-V");
991
    Menu.AddMenuItem(menu, menuDelete,    "delete");
992
    Menu.delimiter(menu);
993
    Menu.AddMenuItem(menu, menuSelectAll, "select all   ctrl-A");
994
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
995
END CreateContextMenu;
996
 
997
 
998
PROCEDURE CreateMenuFile (): Menu.tMenu;
999
VAR
1000
    menu: List.tList;
1001
BEGIN
1002
    menu := List.create(NIL);
1003
    Menu.AddMenuItem(menu, menuNew,    "new       ctrl-N");
1004
    Menu.AddMenuItem(menu, menuOpen,   "open      ctrl-O");
1005
    Menu.AddMenuItem(menu, menuSave,   "save      ctrl-S");
1006
    Menu.AddMenuItem(menu, menuSaveAs, "save as");
1007
    Menu.AddMenuItem(menu, menuFolder, "folder");
1008
    Menu.delimiter(menu);
1009
    Menu.AddMenuItem(menu, menuExit,   "exit");
1010
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1011
END CreateMenuFile;
1012
 
1013
 
1014
PROCEDURE CreateMenuEdit (): Menu.tMenu;
1015
VAR
1016
    menu: List.tList;
1017
BEGIN
1018
    menu := List.create(NIL);
1019
    Menu.AddMenuItem(menu, menuUndo,      "undo          ctrl-Z");
1020
    Menu.AddMenuItem(menu, menuRedo,      "redo          ctrl-Y");
1021
    Menu.delimiter(menu);
1022
    Menu.AddMenuItem(menu, menuSearch,    "search        ctrl-F");
1023
    Menu.AddMenuItem(menu, menuGoto,      "go to line    ctrl-G");
1024
    Menu.delimiter(menu);
1025
    Menu.AddMenuItem(menu, menuCut,       "cut           ctrl-X");
1026
    Menu.AddMenuItem(menu, menuCopy,      "copy          ctrl-C");
1027
    Menu.AddMenuItem(menu, menuPaste,     "paste         ctrl-V");
1028
    Menu.AddMenuItem(menu, menuDelete,    "delete");
1029
    Menu.delimiter(menu);
1030
    Menu.AddMenuItem(menu, menuSelectAll, "select all    ctrl-A");
1031
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1032
END CreateMenuEdit;
1033
 
1034
 
1035
PROCEDURE CreateMenuEncoding (): Menu.tMenu;
1036
VAR
1037
    menu: List.tList;
1038
BEGIN
1039
    menu := List.create(NIL);
1040
    Menu.AddMenuItem(menu, menuUTF8BOM, "UTF-8-BOM");
1041
    Menu.AddMenuItem(menu, menuUTF8,    "UTF-8");
1042
    Menu.AddMenuItem(menu, menuCP866,   "CP866");
1043
    Menu.AddMenuItem(menu, menuWin1251, "Windows-1251");
1044
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1045
END CreateMenuEncoding;
1046
 
1047
 
1048
PROCEDURE CreateMenuView (): Menu.tMenu;
1049
VAR
1050
    menu: List.tList;
1051
    colors: Ini.tSection;
1052
    idx: INTEGER;
1053
BEGIN
1054
    menu := List.create(NIL);
8762 leency 1055
    Menu.AddMenuItem(menu, menuNumbers, "line numbers");
8728 leency 1056
    Menu.delimiter(menu);
1057
 
1058
    colors := Ini.sections.first(Ini.tSection);
1059
    idx := menuColors;
1060
    WHILE colors # NIL DO
1061
        Menu.AddMenuItem(menu, idx, colors.name);
1062
        INC(idx);
1063
        colors := colors.next(Ini.tSection)
1064
    END;
1065
 
1066
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1067
END CreateMenuView;
1068
 
1069
 
1070
PROCEDURE CreateMenuSyntax (): Menu.tMenu;
1071
VAR
1072
    menu: List.tList;
1073
BEGIN
1074
    menu := List.create(NIL);
1075
    Menu.AddMenuItem(menu, menuC,      "C");
1076
    Menu.AddMenuItem(menu, menuFasm,   "Fasm");
1077
    Menu.AddMenuItem(menu, menuIni,    "Ini");
1078
    Menu.AddMenuItem(menu, menuLua,    "Lua");
1079
    Menu.AddMenuItem(menu, menuOberon, "Oberon");
1080
    Menu.AddMenuItem(menu, menuPascal, "Pascal");
1081
    Menu.delimiter(menu);
1082
    Menu.AddMenuItem(menu, menuNone,   "none");
1083
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1084
END CreateMenuSyntax;
1085
 
1086
 
1087
PROCEDURE CreateMenuProgram (): Menu.tMenu;
1088
VAR
1089
    menu: List.tList;
1090
BEGIN
1091
    menu := List.create(NIL);
1092
    Menu.AddMenuItem(menu, menuBuild,       "build    ctrl+F9");
1093
    Menu.AddMenuItem(menu, menuBuildScript, "script");
1094
    Menu.delimiter(menu);
1095
    Menu.AddMenuItem(menu, menuRun,         "run           F9");
1096
    Menu.AddMenuItem(menu, menuRunScript,   "script");
1097
    Menu.delimiter(menu);
1098
    Menu.AddMenuItem(menu, menuDebug,       "debug");
1099
    Menu.AddMenuItem(menu, menuDebugScript, "script");
1100
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1101
END CreateMenuProgram;
1102
 
1103
 
1104
PROCEDURE CreateMenuTools (): Menu.tMenu;
1105
VAR
1106
    menu: List.tList;
1107
BEGIN
1108
    menu := List.create(NIL);
1109
    Menu.AddMenuItem(menu, menuPipet,   "pipet");
1110
    Menu.AddMenuItem(menu, menuSysFunc, "system functions");
1111
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1112
END CreateMenuTools;
1113
 
1114
 
1115
PROCEDURE CanvasToScreen (VAR x, y: INTEGER);
1116
VAR
1117
    cliX, cliY,
1118
    winX, winY: INTEGER;
1119
BEGIN
1120
    K.WinPos(winX, winY);
1121
    K.ClientPos(cliX, cliY);
1122
    x := x + winX + cliX + LEFT;
1123
    y := y + winY + cliY + TOP;
1124
END CanvasToScreen;
1125
 
1126
 
1127
PROCEDURE ShowMenu (menu: Menu.tMenu);
1128
VAR
1129
    winX, winY, cliX, cliY, x, y, i: INTEGER;
1130
    selected: BOOLEAN;
1131
BEGIN
1132
    IF menu # context THEN
1133
        K.WinPos(winX, winY);
1134
        K.ClientPos(cliX, cliY);
1135
        x := winX + cliX;
1136
        y := MainMenuHeight + winY + cliY
1137
    ELSE
1138
        mouse(x, y);
1139
        IF ~((0 <= x) & (x < canvas.width) & (0 <= y) & (y < canvas.height)) THEN
1140
            menu := NIL
1141
        END
1142
    END;
1143
 
1144
    IF menu = menuFile THEN
1145
        Menu.setEnabled(menu, menuSave, text.modified);
1146
        Menu.setEnabled(menu, menuFolder, text.fileName # "");
1147
        INC(x, menuFileX)
1148
    ELSIF (menu = menuEdit) OR (menu = context) THEN
1149
        Menu.setEnabled(menu, menuUndo, ~ChangeLog.isFirstGuard(text.edition));
1150
        Menu.setEnabled(menu, menuRedo, ~ChangeLog.isLastGuard(text.edition));
1151
        selected := T.selected(text);
1152
        Menu.setEnabled(menu, menuCut, selected);
1153
        Menu.setEnabled(menu, menuCopy, selected);
1154
        Menu.setEnabled(menu, menuDelete, selected);
1155
        Menu.setEnabled(menu, menuPaste, CB.available());
1156
        IF menu = menuEdit THEN
1157
            INC(x, menuEditX)
1158
        ELSE
1159
            IF y + menu.height >= canvas.height THEN
1160
                DEC(y, menu.height)
1161
            END;
1162
            IF x + menu.width >= canvas.width THEN
1163
                DEC(x, menu.width)
1164
            END;
1165
            CanvasToScreen(x, y)
1166
        END
1167
    ELSIF menu = menuEncoding THEN
1168
        Menu.setCheck(menu, menuUTF8BOM, ORD(text.enc = E.UTF8BOM)*2);
1169
        Menu.setCheck(menu, menuUTF8,    ORD(text.enc = E.UTF8)*2);
1170
        Menu.setCheck(menu, menuCP866,   ORD(text.enc = E.CP866)*2);
1171
        Menu.setCheck(menu, menuWin1251, ORD(text.enc = E.W1251)*2);
1172
        INC(x, menuEncodingX)
1173
    ELSIF menu = menuView THEN
1174
        Menu.setCheck(menu, menuNumbers, ORD(text.numbers));
1175
        FOR i := 0 TO Ini.sections.count - 1 DO
1176
            Menu.setCheck(menu, menuColors + i, ORD(Ini.curSectionNum = i)*2)
1177
        END;
1178
        INC(x, menuViewX)
1179
    ELSIF menu = menuSyntax THEN
1180
        Menu.setCheck(menu, menuNone,   ORD(text.lang = Languages.langNone)*2);
1181
        Menu.setCheck(menu, menuC,      ORD(text.lang = Languages.langC)*2);
1182
        Menu.setCheck(menu, menuFasm,   ORD(text.lang = Languages.langFasm)*2);
1183
        Menu.setCheck(menu, menuIni,    ORD(text.lang = Languages.langIni)*2);
1184
        Menu.setCheck(menu, menuLua,    ORD(text.lang = Languages.langLua)*2);
1185
        Menu.setCheck(menu, menuOberon, ORD(text.lang = Languages.langOberon)*2);
1186
        Menu.setCheck(menu, menuPascal, ORD(text.lang = Languages.langPascal)*2);
1187
        INC(x, menuSyntaxX)
1188
    ELSIF menu = menuProgram THEN
1189
        Menu.setEnabled(menu, menuBuild, buildScript # "");
1190
        Menu.setEnabled(menu, menuDebug, debugScript # "");
1191
        Menu.setEnabled(menu, menuRun, runScript # "");
1192
        INC(x, menuProgramX)
1193
    ELSIF menu = menuTools THEN
1194
        INC(x, menuToolsX)
1195
    END;
1196
 
1197
    IF menu # NIL THEN
1198
        IF Menu.opened(menu) THEN
1199
            Menu.close(menu)
1200
        END;
1201
        Menu.open(menu, x, y)
1202
    END
1203
END ShowMenu;
1204
 
1205
 
1206
PROCEDURE main;
1207
VAR
1208
    width, height, x, y, scrollX, scrollY: INTEGER;
1209
    key: INTEGER;
1210
    msState: SET;
1211
    scroll: INTEGER;
1212
    err: INTEGER;
1213
    fileName, filePath: RW.tFileName;
8762 leency 1214
    cliWidth, cliHeight: INTEGER;
8728 leency 1215
    resized: BOOLEAN;
1216
    firstClickX, firstClickY, time: INTEGER;
1217
BEGIN
8762 leency 1218
    K.GetSystemColors;
1219
    Icons.get(icons, grayIcons);
8728 leency 1220
    modified := FALSE;
1221
    mainTID := K.ThreadID();
1222
    K.SetIPC(IPC);
1223
    U.ptr2str(K.GetName(), AppPath);
1224
    Ini.load(AppPath);
1225
    K.SetEventsMask({0, 1, 2, 5, 6, 31});
1226
    LeftButtonUp;
1227
    resized := FALSE;
1228
    shift := {};
1229
    winWidth := minWinWidth;
1230
    winHeight := minWinHeight;
8762 leency 1231
    cliWidth := winWidth;
1232
    cliHeight := winHeight;
8728 leency 1233
    LEFT := 10;
1234
    canvas := G.CreateCanvas(winWidth - (LEFT + RIGHT + 10), winHeight - (TOP + BOTTOM + 4) - K.SkinHeight());
1235
    font := G.CreateFont(1, "", {});
1236
    G.SetFont(canvas, font);
1237
    T.init(NIL);
1238
    T.setCanvas(canvas);
1239
    U.ptr2str(K.GetCommandLine(), fileName);
1240
    context := CreateContextMenu();
1241
    menuFile := CreateMenuFile();
1242
    menuEdit := CreateMenuEdit();
1243
    menuEncoding := CreateMenuEncoding();
1244
    menuView := CreateMenuView();
1245
    menuSyntax := CreateMenuSyntax();
1246
    menuProgram := CreateMenuProgram();
1247
    menuTools := CreateMenuTools();
1248
    IF fileName = "" THEN
1249
        text := T.New();
1250
        filePath := "/rd/1"
1251
    ELSE
1252
        text := T.open(fileName, err);
1253
        IF text = NIL THEN
1254
            error("'cedit: error opening file' -E");
1255
            K.Exit
1256
        ELSE
1257
            U.getPath(fileName, filePath)
1258
        END
1259
    END;
1260
    OD := OpenDlg.Create(draw_window, OpenDlg.topen, filePath, "");
1261
 
8762 leency 1262
    vScroll := Scroll.create(scrollWidth, canvas.height - scrollWidth*2 + 1);
1263
    hScroll := Scroll.create(canvas.width - scrollWidth*2, scrollWidth);
8728 leency 1264
    T.resize(canvas.width, canvas.height);
1265
    T.SetPos(text, 0, 0);
1266
    confirm := FALSE;
1267
    notFound := FALSE;
1268
    search := FALSE;
1269
    createSearchForm;
1270
    new_searchText := "";
1271
    searchText := "";
1272
    cs := FALSE;
1273
    whole := FALSE;
1274
    replaced := 0;
1275
    draw_window;
1276
    repaint;
1277
    buildScript := "";
1278
    runScript := "";
1279
    debugScript := "";
1280
    WHILE TRUE DO
1281
        CASE K.WaitForEvent() OF
1282
        |1:
8762 leency 1283
            K.GetSystemColors;
1284
            Icons.get(icons, grayIcons);
8728 leency 1285
            IF ~K.RolledUp() THEN
8762 leency 1286
                K.ClientSize(width, height);
1287
                IF (width # cliWidth) OR (height # cliHeight) THEN
1288
                    cliWidth := width;
1289
                    cliHeight := height;
8728 leency 1290
                    resize;
1291
                    resized := TRUE
1292
                END;
1293
                K.SetEventsMask({0, 1, 2, 5, 6, 31})
1294
            ELSE
1295
                K.SetEventsMask({0, 30, 31})
1296
            END;
1297
            draw_window
1298
        |2:
1299
            key := K.GetKey();
1300
            getKBState;
1301
            IF key DIV 65536 = 61 THEN (* F3 *)
1302
                key := -1;
1303
                IF search & (searchText # "") THEN
1304
                    notFound := ~T.findNext(text, box_lib.check_box_get_value(BKW))
1305
                END
1306
            ELSIF key DIV 65536 = 67 THEN (* F9 *)
1307
                key := -1;
1308
                IF T.CTRL IN shift THEN
1309
                    Script(buildScript)
1310
                ELSE
1311
                    Script(runScript)
1312
                END
1313
            ELSIF (key DIV 65536 = 55) & (key DIV 256 MOD 256 = 52) THEN
1314
                key := -1 (* PrtScn *)
1315
            ELSIF (T.CTRL IN shift) & (key DIV 65536 = 33) THEN
1316
                key := -1;
1317
                IF ~search THEN
1318
                    Search
1319
                END;
1320
                EditBox_SetFocus(FindEdit, TRUE);
1321
                EditBox_SetFocus(ReplaceEdit, FALSE);
1322
                EditBox_SetFocus(GotoEdit, FALSE);
1323
            ELSIF (T.CTRL IN shift) & (key DIV 65536 = 34) THEN
1324
                key := -1;
1325
                IF ~search THEN
1326
                    Search
1327
                END;
1328
                EditBox_SetFocus(GotoEdit, TRUE);
1329
                EditBox_SetFocus(FindEdit, FALSE);
1330
                EditBox_SetFocus(ReplaceEdit, FALSE)
1331
            END;
1332
            IF (key # -1) & EditBox_Focus(FindEdit) THEN
1333
                box_lib.edit_box_key(FindEdit, key);
1334
                EditBox_GetValue(FindEdit, new_searchText);
1335
                IF new_searchText # searchText THEN
1336
                    searchText := new_searchText;
1337
                    notFound := ~T.search(text, searchText, cs, whole)
1338
                END
1339
            ELSIF (key # -1) & EditBox_Focus(ReplaceEdit) THEN
1340
                box_lib.edit_box_key(ReplaceEdit, key);
1341
                EditBox_GetValue(ReplaceEdit, replaceText)
1342
            ELSIF (key # -1) & EditBox_Focus(GotoEdit) THEN
1343
                IF (key DIV 256) MOD 256 = 13 THEN
1344
                    goto
1345
                ELSE
1346
                    box_lib.edit_box_key(GotoEdit, key)
1347
                END
1348
            ELSIF key # -1 THEN
1349
                CASE key DIV 65536 OF
1350
                |73: key := 33
1351
                |81: key := 34
1352
                |71: key := 36
1353
                |79: key := 35
1354
                |72: key := 38
1355
                |80: key := 40
1356
                |75: key := 37
1357
                |77: key := 39
1358
                |82: key := -1 (* insert *)
1359
                |83: key := 46
1360
                |59, 60, 62..66, 68, 87, 88: key := -1 (* F1, F2, F4..F8, F10, F11, F12 *)
1361
                ELSE
1362
                    IF (T.CTRL IN shift) THEN
1363
                        CASE key DIV 65536 OF
1364
                        |21: T.redo(text);
1365
                             key := -1
1366
                        |22: key := ORD("U")
1367
                        |24: key := -1;
1368
                             open
1369
                        |30: key := ORD("A")
1370
                        |31: key := -1;
1371
                             save
1372
                        |38: key := ORD("L")
1373
                        |44: T.undo(text);
1374
                             key := -1
1375
                        |45: key := ORD("X")
1376
                        |46: key := ORD("C")
1377
                        |47: key := ORD("V")
1378
                        |49: key := -1;
1379
                             SelfRun("")
1380
                        ELSE
1381
                            key := -1
1382
                        END
1383
                    ELSE
1384
                        T.input(text, E.cp866[key DIV 256 MOD 256]);
1385
                        key := -1
1386
                    END
1387
                END;
1388
                IF key # -1 THEN
1389
                    T.key(text, key, shift)
1390
                END
1391
            END;
1392
            repaint
1393
        |3:
1394
            CASE K.ButtonCode() OF
1395
            |0:
1396
 
1397
            |btnFile:
1398
                ShowMenu(menuFile)
1399
            |btnEdit:
1400
                ShowMenu(menuEdit)
1401
            |btnEncoding:
1402
                ShowMenu(menuEncoding)
1403
            |btnView:
1404
                ShowMenu(menuView)
1405
            |btnSyntax:
1406
                ShowMenu(menuSyntax)
1407
            |btnProgram:
1408
                ShowMenu(menuProgram)
1409
            |btnTools:
1410
                ShowMenu(menuTools)
1411
            |btnNo:
1412
                K.Exit
1413
            |btnYes:
1414
                save;
1415
                IF ~text.modified THEN
1416
                    K.Exit
1417
                END;
1418
                repaint
1419
            |btnClose:
1420
                close
1421
            |btnNew:
1422
                SelfRun("")
1423
            |btnOpen:
1424
                open
1425
            |btnSave:
1426
                save;
1427
                repaint
1428
            |btnSearch:
1429
                IF ~search THEN
1430
                    Search
1431
                END
1432
            |btnCloseFind:
1433
                Search
1434
            |btnUndo:
1435
                T.undo(text);
1436
                repaint
1437
            |btnRedo:
1438
                T.redo(text);
1439
                repaint
1440
            |btnUpper:
1441
                T.chCase(text, TRUE);
1442
                repaint
1443
            |btnLower:
1444
                T.chCase(text, FALSE);
1445
                repaint
1446
            |btnBuild:
1447
                Script(buildScript)
1448
            |btnRun:
1449
                Script(runScript)
1450
            |btnUp:
1451
                T.scroll(text, 0, -1);
1452
                repaint
1453
            |btnDown:
1454
                T.scroll(text, 0, 1);
1455
                repaint
1456
            |btnLeft:
1457
                T.scroll(text, -1, 0);
1458
                repaint
1459
            |btnRight:
1460
                T.scroll(text, 1, 0);
1461
                repaint
1462
            |btnFindEdit:
1463
                EditBox_SetFocus(FindEdit, TRUE);
1464
                EditBox_SetFocus(ReplaceEdit, FALSE);
1465
                EditBox_SetFocus(GotoEdit, FALSE)
1466
            |btnReplaceEdit:
1467
                EditBox_SetFocus(ReplaceEdit, TRUE);
1468
                EditBox_SetFocus(FindEdit, FALSE);
1469
                EditBox_SetFocus(GotoEdit, FALSE)
1470
            |btnGotoEdit:
1471
                EditBox_SetFocus(GotoEdit, TRUE);
1472
                EditBox_SetFocus(FindEdit, FALSE);
1473
                EditBox_SetFocus(ReplaceEdit, FALSE)
1474
            |btnFindNext:
1475
                IF searchText # "" THEN
1476
                    notFound := ~T.findNext(text, box_lib.check_box_get_value(BKW));
1477
                    repaint
1478
                END
1479
            |btnReplace:
1480
                T.replace(text, replaceText, LENGTH(searchText));
1481
                repaint
1482
            |btnReplaceAll:
1483
                notFound := ~T.search(text, searchText, cs, whole);
1484
                IF ~notFound THEN
1485
                    replaced := T.replaceAll(text, replaceText, LENGTH(searchText));
1486
                END;
1487
                repaint
1488
            |btnGoto:
1489
                goto;
1490
                repaint
1491
            END
1492
        |6:
1493
            Menu.close(menuFile);
1494
            Menu.close(menuEdit);
1495
            Menu.close(menuEncoding);
1496
            Menu.close(menuView);
1497
            Menu.close(menuSyntax);
1498
            Menu.close(menuProgram);
1499
            Menu.close(menuTools);
1500
            Menu.close(context);
1501
            IF ~resized THEN
1502
                getKBState;
1503
                msState := K.MouseState();
1504
                IF ~(0 IN msState) OR (16 IN msState) THEN
1505
                    LeftButtonUp
1506
                END;
1507
                scroll := K.Scroll();
1508
                IF scroll # 0 THEN
1509
                    T.scroll(text, 0, scroll*3);
1510
                    repaint
1511
                END;
1512
                IF leftButton THEN
1513
                    IF K.GetTickCount() - time > 9 THEN
1514
                        mouse(x, y);
1515
                        T.mouse(text, x, y);
1516
                        repaint
1517
                    END
1518
                END;
1519
                IF VScroll THEN
1520
                    mouse(x, y);
1521
                    Scroll.mouse(vScroll, x, y - scrollWidth);
1522
                    T.getScroll(text, scrollX, scrollY);
1523
                    T.scroll(text, 0, vScroll.value - scrollY);
1524
                    repaint
1525
                END;
1526
                IF HScroll THEN
1527
                    mouse(x, y);
1528
                    Scroll.mouse(hScroll, x - scrollWidth, y);
1529
                    T.getScroll(text, scrollX, scrollY);
1530
                    T.scroll(text, hScroll.value - scrollX, 0);
1531
                    repaint
1532
                END;
1533
                IF (8 IN msState) & ~(24 IN msState) THEN
1534
                    mouse(firstClickX, firstClickY);
1535
                    time := click(firstClickX, firstClickY)
1536
                END;
1537
                IF 9 IN msState THEN
1538
                    ShowMenu(context)
1539
                END;
1540
                IF 24 IN msState THEN
1541
                    mouse(x, y);
1542
                    IF (ABS(x - firstClickX) < 5) & (ABS(y - firstClickY) < 5) THEN
1543
                        VScroll := FALSE;
1544
                        HScroll := FALSE;
1545
                        IF (0 <= x) & (x < canvas.width) & (0 <= y) & (y < canvas.height) THEN
1546
                            leftButton := FALSE;
1547
                            T.selectWord(text);
1548
                            repaint
1549
                        END
1550
                    ELSE
1551
                        firstClickX := x;
1552
                        firstClickY := y;
1553
                        time := click(firstClickX, firstClickY)
1554
                    END
1555
                END
1556
            END;
1557
            IF search THEN
1558
                IF EditBox_Focus(FindEdit) THEN
1559
                    box_lib.edit_box_mouse(FindEdit)
1560
                END;
1561
                IF EditBox_Focus(ReplaceEdit) THEN
1562
                    box_lib.edit_box_mouse(ReplaceEdit)
1563
                END;
1564
                IF EditBox_Focus(GotoEdit) THEN
1565
                    box_lib.edit_box_mouse(GotoEdit)
1566
                END;
1567
                box_lib.check_box_mouse2(CS);
1568
                box_lib.check_box_mouse2(WH);
1569
                box_lib.check_box_mouse2(BKW);
1570
                IF box_lib.check_box_get_value(CS) # cs THEN
1571
                    cs := ~cs;
1572
                    notFound := ~T.search(text, searchText, cs, whole);
1573
                    repaint
1574
                END;
1575
                IF box_lib.check_box_get_value(WH) # whole THEN
1576
                    whole := ~whole;
1577
                    notFound := ~T.search(text, searchText, cs, whole);
1578
                    repaint
1579
                END
1580
            END;
1581
            resized := FALSE
1582
        |7: receiveIPC
1583
        ELSE
1584
        END
1585
    END
1586
END main;
1587
 
1588
 
1589
BEGIN
1590
    main
1591
END CEdit.