Subversion Repositories Kolibri OS

Rev

Rev 8848 | Rev 9010 | 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
8859 leency 31
    header = "CEdit (15-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;
8859 leency 81
    iconPad = (toolBtnSize - Icons.SIZE) DIV 2;
8728 leency 82
 
83
    TOP = btnTop + toolBtnSize + 7;
84
    RIGHT = scrollWidth - 2;
8859 leency 85
    BOTTOM = scrollWidth + 18;
8728 leency 86
 
8762 leency 87
    minWinWidth = 635; minWinHeight = 542;
8728 leency 88
 
89
    SEARCH_PADDING = 10;
8859 leency 90
    searchLeft = 0;
8728 leency 91
    EditBox_Width = 180;
92
    EDITBOX_MAXCHARS = 500;
93
 
94
    menuFileX = searchLeft;
95
    menuEditX = menuFileX + 4*fontWidth + 2 + 7;
96
    menuEncodingX = menuEditX + 4*fontWidth + 2 + 7;
97
    menuViewX = menuEncodingX + 8*fontWidth + 2 + 7;
98
    menuSyntaxX = menuViewX + 4*fontWidth + 2 + 7;
99
    menuProgramX = menuSyntaxX + 6*fontWidth + 2 + 7;
100
    menuToolsX = menuProgramX + 7*fontWidth + 2 + 7;
101
 
102
    menuCut = 1;
103
    menuCopy = 2;
104
    menuPaste = 3;
105
    menuDelete = 4;
106
    menuSelectAll = 5;
107
 
108
    menuUndo = 6;
109
    menuRedo = 7;
110
 
111
    menuSearch = 8;
112
    menuGoto = 9;
113
 
114
    menuNew = 10;
115
    menuOpen = 11;
116
    menuSave = 12;
117
    menuSaveAs = 13;
118
    menuFolder = 14;
119
    menuExit = 15;
120
 
121
    menuNumbers = 20;
8772 leency 122
    menuFontSize = 21;
8728 leency 123
    menuColors = 1000;
124
    menuMaxColors = menuColors + Ini.MAX_SECTIONS - 1;
125
 
126
    menuNone = 30;
127
    menuC = 31;
128
    menuFasm = 32;
129
    menuIni = 33;
130
    menuLua = 34;
131
    menuOberon = 35;
132
    menuPascal = 36;
133
 
134
    menuBuild = 50;
135
    menuBuildScript = 51;
136
    menuRun = 52;
137
    menuRunScript = 53;
138
    menuDebug = 54;
139
    menuDebugScript = 55;
140
 
141
    menuUTF8BOM = 60;
142
    menuUTF8 = 61;
143
    menuCP866 = 62;
144
    menuWin1251 = 63;
145
 
146
    menuPipet = 70;
147
    menuSysFunc = 71;
148
 
8762 leency 149
 
8728 leency 150
VAR
151
    canvas: G.tCanvas;
8772 leency 152
    font, font1, font2: G.tFont;
8728 leency 153
    text: T.tText;
154
    winWidth, winHeight: INTEGER;
155
    shift: SET;
156
    AppPath, runScript, buildScript, debugScript: RW.tFileName;
157
    OD: OpenDlg.Dialog;
158
    confirm, notFound, search, modified: BOOLEAN;
159
 
160
    leftButton, VScroll, HScroll: BOOLEAN;
161
    vScroll, hScroll: Scroll.tScroll;
162
    LEFT: INTEGER;
163
 
164
    FindEdit, ReplaceEdit, GotoEdit: box_lib.edit_box;
165
    CS, WH, BKW: box_lib.checkbox;
166
 
167
    new_searchText, searchText, replaceText, gotoText: T.tString;
168
    cs, whole: BOOLEAN;
169
 
170
    replaced: INTEGER;
171
 
172
    mainTID: INTEGER;
173
 
174
    context, menuFile, menuEdit, menuEncoding, menuView, menuSyntax, menuProgram, menuTools: Menu.tMenu;
175
 
176
    icons: INTEGER;
177
    grayIcons: INTEGER;
178
 
179
    IPC: ARRAY 64 OF INTEGER;
180
 
181
 
182
PROCEDURE WritePos (y: INTEGER);
183
VAR
184
    s1, s2: ARRAY 32 OF WCHAR;
185
    line, col: INTEGER;
186
BEGIN
187
    T.getPos(text, col, line);
188
    U.int2str(line, s1);
189
    U.int2str(col, s2);
190
    U.append(s1, ": ");
191
    U.append(s1, s2);
8762 leency 192
    K.DrawText(LEFT, y, K.textColor, s1)
8728 leency 193
END WritePos;
194
 
195
 
196
PROCEDURE EditBox_Focus (edit: box_lib.edit_box): BOOLEAN;
197
    RETURN 1 IN BITS(edit.flags)
198
END EditBox_Focus;
199
 
200
 
201
PROCEDURE EditBox_SetFocus (edit: box_lib.edit_box; value: BOOLEAN);
202
BEGIN
203
    IF value THEN
204
        edit.flags := ORD(BITS(edit.flags) + {1})
205
    ELSE
206
        edit.flags := ORD(BITS(edit.flags) - {1})
207
    END;
208
    IF search THEN
209
        box_lib.edit_box_draw(edit)
210
    END
211
END EditBox_SetFocus;
212
 
213
 
214
PROCEDURE Rect (left, top, right, bottom, color: INTEGER);
215
BEGIN
216
    K.DrawLine(left, top, right, top, color);
217
    K.DrawLine(left, bottom, right, bottom, color);
218
    K.DrawLine(left, top, left, bottom, color);
219
    K.DrawLine(right, top, right, bottom, color);
220
END Rect;
221
 
222
 
223
PROCEDURE Message (s: ARRAY OF WCHAR);
224
CONST
225
    minWidth = 30;
226
    height = 40;
227
    borderColor = 808080H;
228
VAR
229
    top, left, right, bottom, x, y, width: INTEGER;
230
BEGIN
231
    width := minWidth + LENGTH(s)*fontWidth;
232
    left := (canvas.width - width) DIV 2 + LEFT;
233
    top := (canvas.height - height) DIV 2 + TOP;
234
    bottom := top + height - 1;
235
    right := left + width - 1;
236
    x := minWidth DIV 2 + left;
237
    y := (height - fontHeight) DIV 2 + top;
8762 leency 238
    K.DrawRect(left, top, width, height, K.winColor);
8728 leency 239
    Rect(left, top, right, bottom, borderColor);
8762 leency 240
    K.DrawText(x, y, K.textColor, s);
8728 leency 241
END Message;
242
 
243
 
244
PROCEDURE NotFound;
245
BEGIN
246
    IF notFound THEN
247
        Message("not found");
248
        notFound := FALSE;
249
        EditBox_SetFocus(FindEdit, TRUE);
250
        EditBox_SetFocus(ReplaceEdit, FALSE);
251
        EditBox_SetFocus(GotoEdit, FALSE)
252
    END
253
END NotFound;
254
 
255
 
256
PROCEDURE Replaced;
257
VAR
258
    s, s1: ARRAY 32 OF WCHAR;
259
BEGIN
260
    IF replaced # 0 THEN
261
        s := "replaced: ";
262
        U.int2str(replaced, s1);
263
        U.append(s, s1);
264
        Message(s);
265
        replaced := 0;
266
        EditBox_SetFocus(FindEdit, TRUE);
267
        EditBox_SetFocus(ReplaceEdit, FALSE);
268
        EditBox_SetFocus(GotoEdit, FALSE)
269
    END
270
END Replaced;
271
 
272
 
273
PROCEDURE toolbarIcons;
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;
8859 leency 331
    K.DrawRect(x, TOP + canvas.height + scrollWidth - 1, 8*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
8859 leency 355
            WriteModified(width - 8*fontWidth, y)
8728 leency 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
VAR
448
    width, height, x, y: INTEGER;
449
 
450
 
451
    PROCEDURE drawToolbarBtn (id, x: INTEGER);
452
    BEGIN
8762 leency 453
        K.DrawRect(x, btnTop, toolBtnSize, toolBtnSize, K.toolbarColor);
454
        K.DrawLine(x, btnTop + toolBtnSize, x + toolBtnSize, btnTop + toolBtnSize, K.shadowColor);
455
        K.DrawLine(x + toolBtnSize, btnTop, x + toolBtnSize, btnTop + toolBtnSize, K.shadowColor);
456
        K.CreateButton(id + ORD({30}), x, btnTop, toolBtnSize, toolBtnSize, K.btnColor, "");
8728 leency 457
    END drawToolbarBtn;
458
 
459
 
460
    PROCEDURE drawMainMenu (menu: Menu.tMenu; x: INTEGER; btn: INTEGER; caption: ARRAY OF WCHAR);
461
    VAR
8772 leency 462
        menuColor, textColor, n: INTEGER;
8728 leency 463
    BEGIN
464
        IF menu.tid # 0 THEN
8772 leency 465
            menuColor := K.textColor;
466
            textColor := K.winColor
8728 leency 467
        ELSE
8772 leency 468
            menuColor := K.winColor;
469
            textColor := K.textColor
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, "");
8772 leency 474
        K.DrawText(x + 1, (MainMenuHeight - fontHeight) DIV 2 + 1, textColor, caption)
8728 leency 475
    END drawMainMenu;
476
 
477
 
478
BEGIN
479
    K.BeginDraw;
8772 leency 480
    K.CreateWindow(30 + K.GetTickCount() MOD 128, 30 + 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);
527
        INC(x, toolBtnSize + 5);
528
 
529
        drawToolbarBtn(btnRun, x);
530
        INC(x, toolBtnSize + 5);
531
 
8859 leency 532
        toolbarIcons;
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
8859 leency 549
        |E.UTF8:    K.DrawText866(width -  5*fontWidth, y, K.textColor, "UTF-8")
550
        |E.UTF8BOM: K.DrawText866(width -  9*fontWidth, y, K.textColor, "UTF-8-BOM")
551
        |E.CP866:   K.DrawText866(width -  5*fontWidth, y, K.textColor, "CP866")
552
        |E.W1251:   K.DrawText866(width - 12*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;
8859 leency 559
        K.DrawRect(LEFT + 16*fontWidth, TOP + canvas.height + scrollWidth - 1, width - LEFT - 24*fontWidth, BOTTOM - scrollWidth + 1, K.winColor);
8762 leency 560
        K.DrawText866(LEFT + 16*fontWidth, y, K.textColor, text.fileName);
8859 leency 561
        WriteModified(width - 8*fontWidth, y);
8728 leency 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
8859 leency 753
        LEFT := searchLeft + EditBox_Width + SEARCH_PADDING*2 + 5;
8728 leency 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)
8772 leency 894
    |menuFontSize:
895
        IF font = font1 THEN
896
            font := font2
897
        ELSE
898
            font := font1
899
        END;
900
        resize
8728 leency 901
    |menuNone:
902
        T.setLang(text, Languages.langNone)
903
    |menuC:
904
        T.setLang(text, Languages.langC)
905
    |menuFasm:
906
        T.setLang(text, Languages.langFasm)
907
    |menuIni:
908
        T.setLang(text, Languages.langIni)
909
    |menuLua:
910
        T.setLang(text, Languages.langLua)
911
    |menuOberon:
912
        T.setLang(text, Languages.langOberon)
913
    |menuPascal:
914
        T.setLang(text, Languages.langPascal)
915
    |menuBuild:
916
        Script(buildScript)
917
    |menuBuildScript:
918
        OpenFile(buildScript, ShellFilter)
919
    |menuRun:
920
        Script(runScript)
921
    |menuRunScript:
922
        OpenFile(runScript, ShellFilter)
923
    |menuDebug:
924
        Script(debugScript)
925
    |menuDebugScript:
926
        OpenFile(debugScript, ShellFilter)
927
    |menuUTF8BOM:
928
        text.enc := E.UTF8BOM
929
    |menuUTF8:
930
        text.enc := E.UTF8
931
    |menuCP866:
932
        text.enc := E.CP866
933
    |menuWin1251:
934
        text.enc := E.W1251
935
    |menuPipet:
936
        K.Run("/rd/1/develop/pipet", "")
937
    |menuSysFunc:
938
        K.Run("/rd/1/docpack", "f")
939
    |menuColors..menuMaxColors:
940
        Ini.selectSection(IPC[2] - menuColors)
941
    END;
942
    IPC[0] := 0;
943
    IPC[1] := 0
944
END receiveIPC;
945
 
946
 
947
PROCEDURE MenuKeyDown (menu: Menu.tMenu; key: INTEGER): BOOLEAN;
948
VAR
949
    menuItem: INTEGER;
950
BEGIN
951
    menuItem := -1;
952
    getKBState;
953
    IF (T.CTRL IN shift) THEN
954
        CASE key DIV 65536 OF
955
        |21: menuItem := menuRedo
956
        |30: menuItem := menuSelectAll
957
        |33: menuItem := menuSearch
958
        |34: menuItem := menuGoto
959
        |44: menuItem := menuUndo
960
        |45: menuItem := menuCut
961
        |46: menuItem := menuCopy
962
        |47: menuItem := menuPaste
963
        |24: menuItem := menuOpen
964
        |31: menuItem := menuSave
965
        |49: menuItem := menuNew
966
        |67: menuItem := menuBuild
967
        ELSE
968
        END
969
    ELSE
970
        IF key DIV 65536 = 83 THEN
971
            menuItem := menuDelete
972
        ELSIF key DIV 65536 = 67 THEN
973
            menuItem := menuRun
974
        END
975
    END;
976
    IF menuItem # -1 THEN
977
        IF Menu.isEnabled(menu, menuItem) THEN
978
            MenuItemClick(menu, menuItem)
979
        ELSE
980
            menuItem := -1
981
        END
982
    END
983
    RETURN menuItem # -1
984
END MenuKeyDown;
985
 
986
 
987
PROCEDURE CreateContextMenu (): Menu.tMenu;
988
VAR
989
    menu: List.tList;
990
BEGIN
991
    menu := List.create(NIL);
992
    Menu.AddMenuItem(menu, menuUndo,      "undo         ctrl-Z");
993
    Menu.AddMenuItem(menu, menuRedo,      "redo         ctrl-Y");
994
    Menu.delimiter(menu);
995
    Menu.AddMenuItem(menu, menuCut,       "cut          ctrl-X");
996
    Menu.AddMenuItem(menu, menuCopy,      "copy         ctrl-C");
997
    Menu.AddMenuItem(menu, menuPaste,     "paste        ctrl-V");
998
    Menu.AddMenuItem(menu, menuDelete,    "delete");
999
    Menu.delimiter(menu);
1000
    Menu.AddMenuItem(menu, menuSelectAll, "select all   ctrl-A");
1001
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1002
END CreateContextMenu;
1003
 
1004
 
1005
PROCEDURE CreateMenuFile (): Menu.tMenu;
1006
VAR
1007
    menu: List.tList;
1008
BEGIN
1009
    menu := List.create(NIL);
1010
    Menu.AddMenuItem(menu, menuNew,    "new       ctrl-N");
1011
    Menu.AddMenuItem(menu, menuOpen,   "open      ctrl-O");
1012
    Menu.AddMenuItem(menu, menuSave,   "save      ctrl-S");
1013
    Menu.AddMenuItem(menu, menuSaveAs, "save as");
1014
    Menu.AddMenuItem(menu, menuFolder, "folder");
1015
    Menu.delimiter(menu);
1016
    Menu.AddMenuItem(menu, menuExit,   "exit");
1017
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1018
END CreateMenuFile;
1019
 
1020
 
1021
PROCEDURE CreateMenuEdit (): Menu.tMenu;
1022
VAR
1023
    menu: List.tList;
1024
BEGIN
1025
    menu := List.create(NIL);
1026
    Menu.AddMenuItem(menu, menuUndo,      "undo          ctrl-Z");
1027
    Menu.AddMenuItem(menu, menuRedo,      "redo          ctrl-Y");
1028
    Menu.delimiter(menu);
1029
    Menu.AddMenuItem(menu, menuSearch,    "search        ctrl-F");
1030
    Menu.AddMenuItem(menu, menuGoto,      "go to line    ctrl-G");
1031
    Menu.delimiter(menu);
1032
    Menu.AddMenuItem(menu, menuCut,       "cut           ctrl-X");
1033
    Menu.AddMenuItem(menu, menuCopy,      "copy          ctrl-C");
1034
    Menu.AddMenuItem(menu, menuPaste,     "paste         ctrl-V");
1035
    Menu.AddMenuItem(menu, menuDelete,    "delete");
1036
    Menu.delimiter(menu);
1037
    Menu.AddMenuItem(menu, menuSelectAll, "select all    ctrl-A");
1038
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1039
END CreateMenuEdit;
1040
 
1041
 
1042
PROCEDURE CreateMenuEncoding (): Menu.tMenu;
1043
VAR
1044
    menu: List.tList;
1045
BEGIN
1046
    menu := List.create(NIL);
1047
    Menu.AddMenuItem(menu, menuUTF8BOM, "UTF-8-BOM");
1048
    Menu.AddMenuItem(menu, menuUTF8,    "UTF-8");
1049
    Menu.AddMenuItem(menu, menuCP866,   "CP866");
1050
    Menu.AddMenuItem(menu, menuWin1251, "Windows-1251");
1051
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1052
END CreateMenuEncoding;
1053
 
1054
 
1055
PROCEDURE CreateMenuView (): Menu.tMenu;
1056
VAR
1057
    menu: List.tList;
1058
    colors: Ini.tSection;
1059
    idx: INTEGER;
1060
BEGIN
1061
    menu := List.create(NIL);
8762 leency 1062
    Menu.AddMenuItem(menu, menuNumbers, "line numbers");
8772 leency 1063
    Menu.AddMenuItem(menu, menuFontSize, "x2");
8728 leency 1064
    Menu.delimiter(menu);
1065
 
1066
    colors := Ini.sections.first(Ini.tSection);
1067
    idx := menuColors;
1068
    WHILE colors # NIL DO
1069
        Menu.AddMenuItem(menu, idx, colors.name);
1070
        INC(idx);
1071
        colors := colors.next(Ini.tSection)
1072
    END;
1073
 
1074
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1075
END CreateMenuView;
1076
 
1077
 
1078
PROCEDURE CreateMenuSyntax (): Menu.tMenu;
1079
VAR
1080
    menu: List.tList;
1081
BEGIN
1082
    menu := List.create(NIL);
1083
    Menu.AddMenuItem(menu, menuC,      "C");
1084
    Menu.AddMenuItem(menu, menuFasm,   "Fasm");
1085
    Menu.AddMenuItem(menu, menuIni,    "Ini");
1086
    Menu.AddMenuItem(menu, menuLua,    "Lua");
1087
    Menu.AddMenuItem(menu, menuOberon, "Oberon");
1088
    Menu.AddMenuItem(menu, menuPascal, "Pascal");
1089
    Menu.delimiter(menu);
1090
    Menu.AddMenuItem(menu, menuNone,   "none");
1091
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1092
END CreateMenuSyntax;
1093
 
1094
 
1095
PROCEDURE CreateMenuProgram (): Menu.tMenu;
1096
VAR
1097
    menu: List.tList;
1098
BEGIN
1099
    menu := List.create(NIL);
1100
    Menu.AddMenuItem(menu, menuBuild,       "build    ctrl+F9");
1101
    Menu.AddMenuItem(menu, menuBuildScript, "script");
1102
    Menu.delimiter(menu);
1103
    Menu.AddMenuItem(menu, menuRun,         "run           F9");
1104
    Menu.AddMenuItem(menu, menuRunScript,   "script");
1105
    Menu.delimiter(menu);
1106
    Menu.AddMenuItem(menu, menuDebug,       "debug");
1107
    Menu.AddMenuItem(menu, menuDebugScript, "script");
1108
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1109
END CreateMenuProgram;
1110
 
1111
 
1112
PROCEDURE CreateMenuTools (): Menu.tMenu;
1113
VAR
1114
    menu: List.tList;
1115
BEGIN
1116
    menu := List.create(NIL);
1117
    Menu.AddMenuItem(menu, menuPipet,   "pipet");
1118
    Menu.AddMenuItem(menu, menuSysFunc, "system functions");
1119
    RETURN Menu.create(menu, MenuItemClick, MenuKeyDown)
1120
END CreateMenuTools;
1121
 
1122
 
1123
PROCEDURE CanvasToScreen (VAR x, y: INTEGER);
1124
VAR
1125
    cliX, cliY,
1126
    winX, winY: INTEGER;
1127
BEGIN
1128
    K.WinPos(winX, winY);
1129
    K.ClientPos(cliX, cliY);
1130
    x := x + winX + cliX + LEFT;
1131
    y := y + winY + cliY + TOP;
1132
END CanvasToScreen;
1133
 
1134
 
1135
PROCEDURE ShowMenu (menu: Menu.tMenu);
1136
VAR
1137
    winX, winY, cliX, cliY, x, y, i: INTEGER;
1138
    selected: BOOLEAN;
1139
BEGIN
1140
    IF menu # context THEN
1141
        K.WinPos(winX, winY);
1142
        K.ClientPos(cliX, cliY);
1143
        x := winX + cliX;
1144
        y := MainMenuHeight + winY + cliY
1145
    ELSE
1146
        mouse(x, y);
1147
        IF ~((0 <= x) & (x < canvas.width) & (0 <= y) & (y < canvas.height)) THEN
1148
            menu := NIL
1149
        END
1150
    END;
1151
 
1152
    IF menu = menuFile THEN
1153
        Menu.setEnabled(menu, menuSave, text.modified);
1154
        Menu.setEnabled(menu, menuFolder, text.fileName # "");
1155
        INC(x, menuFileX)
1156
    ELSIF (menu = menuEdit) OR (menu = context) THEN
1157
        Menu.setEnabled(menu, menuUndo, ~ChangeLog.isFirstGuard(text.edition));
1158
        Menu.setEnabled(menu, menuRedo, ~ChangeLog.isLastGuard(text.edition));
1159
        selected := T.selected(text);
1160
        Menu.setEnabled(menu, menuCut, selected);
1161
        Menu.setEnabled(menu, menuCopy, selected);
1162
        Menu.setEnabled(menu, menuDelete, selected);
1163
        Menu.setEnabled(menu, menuPaste, CB.available());
1164
        IF menu = menuEdit THEN
1165
            INC(x, menuEditX)
1166
        ELSE
1167
            IF y + menu.height >= canvas.height THEN
1168
                DEC(y, menu.height)
1169
            END;
1170
            IF x + menu.width >= canvas.width THEN
1171
                DEC(x, menu.width)
1172
            END;
1173
            CanvasToScreen(x, y)
1174
        END
1175
    ELSIF menu = menuEncoding THEN
1176
        Menu.setCheck(menu, menuUTF8BOM, ORD(text.enc = E.UTF8BOM)*2);
1177
        Menu.setCheck(menu, menuUTF8,    ORD(text.enc = E.UTF8)*2);
1178
        Menu.setCheck(menu, menuCP866,   ORD(text.enc = E.CP866)*2);
1179
        Menu.setCheck(menu, menuWin1251, ORD(text.enc = E.W1251)*2);
1180
        INC(x, menuEncodingX)
1181
    ELSIF menu = menuView THEN
8772 leency 1182
        Menu.setCheck(menu, menuNumbers,  ORD(text.numbers));
1183
        Menu.setCheck(menu, menuFontSize, ORD(font = font2));
8728 leency 1184
        FOR i := 0 TO Ini.sections.count - 1 DO
1185
            Menu.setCheck(menu, menuColors + i, ORD(Ini.curSectionNum = i)*2)
1186
        END;
1187
        INC(x, menuViewX)
1188
    ELSIF menu = menuSyntax THEN
1189
        Menu.setCheck(menu, menuNone,   ORD(text.lang = Languages.langNone)*2);
1190
        Menu.setCheck(menu, menuC,      ORD(text.lang = Languages.langC)*2);
1191
        Menu.setCheck(menu, menuFasm,   ORD(text.lang = Languages.langFasm)*2);
1192
        Menu.setCheck(menu, menuIni,    ORD(text.lang = Languages.langIni)*2);
1193
        Menu.setCheck(menu, menuLua,    ORD(text.lang = Languages.langLua)*2);
1194
        Menu.setCheck(menu, menuOberon, ORD(text.lang = Languages.langOberon)*2);
1195
        Menu.setCheck(menu, menuPascal, ORD(text.lang = Languages.langPascal)*2);
1196
        INC(x, menuSyntaxX)
1197
    ELSIF menu = menuProgram THEN
1198
        Menu.setEnabled(menu, menuBuild, buildScript # "");
1199
        Menu.setEnabled(menu, menuDebug, debugScript # "");
1200
        Menu.setEnabled(menu, menuRun, runScript # "");
1201
        INC(x, menuProgramX)
1202
    ELSIF menu = menuTools THEN
1203
        INC(x, menuToolsX)
1204
    END;
1205
 
1206
    IF menu # NIL THEN
1207
        IF Menu.opened(menu) THEN
1208
            Menu.close(menu)
1209
        END;
1210
        Menu.open(menu, x, y)
1211
    END
1212
END ShowMenu;
1213
 
1214
 
1215
PROCEDURE main;
1216
VAR
8772 leency 1217
    width, height, x, y, scrollX, scrollY, i: INTEGER;
8728 leency 1218
    key: INTEGER;
1219
    msState: SET;
1220
    scroll: INTEGER;
1221
    err: INTEGER;
1222
    fileName, filePath: RW.tFileName;
8762 leency 1223
    cliWidth, cliHeight: INTEGER;
8728 leency 1224
    resized: BOOLEAN;
1225
    firstClickX, firstClickY, time: INTEGER;
1226
BEGIN
8762 leency 1227
    K.GetSystemColors;
1228
    Icons.get(icons, grayIcons);
8728 leency 1229
    modified := FALSE;
1230
    mainTID := K.ThreadID();
1231
    K.SetIPC(IPC);
1232
    U.ptr2str(K.GetName(), AppPath);
1233
    Ini.load(AppPath);
1234
    K.SetEventsMask({0, 1, 2, 5, 6, 31});
1235
    LeftButtonUp;
1236
    resized := FALSE;
1237
    shift := {};
8772 leency 1238
    K.ScreenSize(winWidth, winHeight);
1239
    winWidth := (winWidth * 80) DIV 100 - (128 + 30);
1240
    winHeight := winHeight - (128 + 30);
1241
    winWidth := MAX(winWidth, minWinWidth);
1242
    winHeight := MAX(winHeight, minWinHeight);
8762 leency 1243
    cliWidth := winWidth;
1244
    cliHeight := winHeight;
8859 leency 1245
    LEFT := searchLeft;
8728 leency 1246
    canvas := G.CreateCanvas(winWidth - (LEFT + RIGHT + 10), winHeight - (TOP + BOTTOM + 4) - K.SkinHeight());
8772 leency 1247
    font1 := G.CreateFont(1, "", {});
1248
    font2 := G.CreateFont(2, "", {});
1249
    font := font1;
8728 leency 1250
    G.SetFont(canvas, font);
1251
    T.init(NIL);
1252
    T.setCanvas(canvas);
1253
    U.ptr2str(K.GetCommandLine(), fileName);
1254
    context := CreateContextMenu();
1255
    menuFile := CreateMenuFile();
1256
    menuEdit := CreateMenuEdit();
1257
    menuEncoding := CreateMenuEncoding();
1258
    menuView := CreateMenuView();
1259
    menuSyntax := CreateMenuSyntax();
1260
    menuProgram := CreateMenuProgram();
1261
    menuTools := CreateMenuTools();
1262
    IF fileName = "" THEN
1263
        text := T.New();
1264
        filePath := "/rd/1"
1265
    ELSE
1266
        text := T.open(fileName, err);
1267
        IF text = NIL THEN
1268
            error("'cedit: error opening file' -E");
1269
            K.Exit
1270
        ELSE
1271
            U.getPath(fileName, filePath)
1272
        END
1273
    END;
1274
    OD := OpenDlg.Create(draw_window, OpenDlg.topen, filePath, "");
1275
 
8762 leency 1276
    vScroll := Scroll.create(scrollWidth, canvas.height - scrollWidth*2 + 1);
1277
    hScroll := Scroll.create(canvas.width - scrollWidth*2, scrollWidth);
8728 leency 1278
    T.resize(canvas.width, canvas.height);
1279
    T.SetPos(text, 0, 0);
1280
    confirm := FALSE;
1281
    notFound := FALSE;
1282
    search := FALSE;
1283
    createSearchForm;
1284
    new_searchText := "";
1285
    searchText := "";
1286
    cs := FALSE;
1287
    whole := FALSE;
1288
    replaced := 0;
1289
    draw_window;
1290
    repaint;
1291
    buildScript := "";
1292
    runScript := "";
1293
    debugScript := "";
1294
    WHILE TRUE DO
1295
        CASE K.WaitForEvent() OF
1296
        |1:
8772 leency 1297
            FOR i := 0 TO 1 DO
1298
                K.GetSystemColors;
1299
                Icons.get(icons, grayIcons);
1300
                IF ~K.RolledUp() THEN
1301
                    K.ClientSize(width, height);
1302
                    IF (width # cliWidth) OR (height # cliHeight) THEN
1303
                        cliWidth := width;
1304
                        cliHeight := height;
1305
                        resize;
1306
                        resized := TRUE;
1307
                    END;
1308
                    K.SetEventsMask({0, 1, 2, 5, 6, 31})
1309
                ELSE
1310
                    K.SetEventsMask({0, 30, 31})
8728 leency 1311
                END;
8772 leency 1312
                draw_window
1313
            END
1314
 
8728 leency 1315
        |2:
1316
            key := K.GetKey();
1317
            getKBState;
8848 leency 1318
            IF confirm THEN
1319
                IF key DIV 65536 = 28 THEN (* Enter *)
1320
                    save;
1321
                    IF ~text.modified THEN
1322
                        K.Exit
1323
                    END;
1324
                    repaint
1325
                END;
1326
                key := -1
1327
            END;
8728 leency 1328
            IF key DIV 65536 = 61 THEN (* F3 *)
1329
                key := -1;
1330
                IF search & (searchText # "") THEN
8772 leency 1331
                    IF T.SHIFT IN shift THEN
1332
                        notFound := ~T.findNext(text, ~box_lib.check_box_get_value(BKW))
1333
                    ELSE
1334
                        notFound := ~T.findNext(text, box_lib.check_box_get_value(BKW))
1335
                    END
8728 leency 1336
                END
1337
            ELSIF key DIV 65536 = 67 THEN (* F9 *)
1338
                key := -1;
1339
                IF T.CTRL IN shift THEN
1340
                    Script(buildScript)
1341
                ELSE
1342
                    Script(runScript)
1343
                END
8848 leency 1344
            ELSIF key DIV 65536 = 1 THEN (* Esc *)
1345
                key := -1;
8772 leency 1346
                IF search THEN
1347
                    Search
1348
                END
8728 leency 1349
            ELSIF (key DIV 65536 = 55) & (key DIV 256 MOD 256 = 52) THEN
1350
                key := -1 (* PrtScn *)
1351
            ELSIF (T.CTRL IN shift) & (key DIV 65536 = 33) THEN
1352
                key := -1;
1353
                IF ~search THEN
1354
                    Search
1355
                END;
1356
                EditBox_SetFocus(FindEdit, TRUE);
1357
                EditBox_SetFocus(ReplaceEdit, FALSE);
1358
                EditBox_SetFocus(GotoEdit, FALSE);
1359
            ELSIF (T.CTRL IN shift) & (key DIV 65536 = 34) THEN
1360
                key := -1;
1361
                IF ~search THEN
1362
                    Search
1363
                END;
1364
                EditBox_SetFocus(GotoEdit, TRUE);
1365
                EditBox_SetFocus(FindEdit, FALSE);
1366
                EditBox_SetFocus(ReplaceEdit, FALSE)
1367
            END;
1368
            IF (key # -1) & EditBox_Focus(FindEdit) THEN
1369
                box_lib.edit_box_key(FindEdit, key);
1370
                EditBox_GetValue(FindEdit, new_searchText);
1371
                IF new_searchText # searchText THEN
1372
                    searchText := new_searchText;
1373
                    notFound := ~T.search(text, searchText, cs, whole)
1374
                END
1375
            ELSIF (key # -1) & EditBox_Focus(ReplaceEdit) THEN
1376
                box_lib.edit_box_key(ReplaceEdit, key);
1377
                EditBox_GetValue(ReplaceEdit, replaceText)
1378
            ELSIF (key # -1) & EditBox_Focus(GotoEdit) THEN
1379
                IF (key DIV 256) MOD 256 = 13 THEN
1380
                    goto
1381
                ELSE
1382
                    box_lib.edit_box_key(GotoEdit, key)
1383
                END
1384
            ELSIF key # -1 THEN
1385
                CASE key DIV 65536 OF
1386
                |73: key := 33
1387
                |81: key := 34
1388
                |71: key := 36
1389
                |79: key := 35
1390
                |72: key := 38
1391
                |80: key := 40
1392
                |75: key := 37
1393
                |77: key := 39
1394
                |82: key := -1 (* insert *)
1395
                |83: key := 46
1396
                |59, 60, 62..66, 68, 87, 88: key := -1 (* F1, F2, F4..F8, F10, F11, F12 *)
1397
                ELSE
1398
                    IF (T.CTRL IN shift) THEN
1399
                        CASE key DIV 65536 OF
1400
                        |21: T.redo(text);
1401
                             key := -1
1402
                        |22: key := ORD("U")
1403
                        |24: key := -1;
1404
                             open
1405
                        |30: key := ORD("A")
1406
                        |31: key := -1;
1407
                             save
1408
                        |38: key := ORD("L")
1409
                        |44: T.undo(text);
1410
                             key := -1
1411
                        |45: key := ORD("X")
1412
                        |46: key := ORD("C")
1413
                        |47: key := ORD("V")
1414
                        |49: key := -1;
1415
                             SelfRun("")
1416
                        ELSE
1417
                            key := -1
1418
                        END
1419
                    ELSE
1420
                        T.input(text, E.cp866[key DIV 256 MOD 256]);
1421
                        key := -1
1422
                    END
1423
                END;
1424
                IF key # -1 THEN
1425
                    T.key(text, key, shift)
1426
                END
1427
            END;
1428
            repaint
1429
        |3:
1430
            CASE K.ButtonCode() OF
1431
            |0:
1432
 
1433
            |btnFile:
1434
                ShowMenu(menuFile)
1435
            |btnEdit:
1436
                ShowMenu(menuEdit)
1437
            |btnEncoding:
1438
                ShowMenu(menuEncoding)
1439
            |btnView:
1440
                ShowMenu(menuView)
1441
            |btnSyntax:
1442
                ShowMenu(menuSyntax)
1443
            |btnProgram:
1444
                ShowMenu(menuProgram)
1445
            |btnTools:
1446
                ShowMenu(menuTools)
1447
            |btnNo:
1448
                K.Exit
1449
            |btnYes:
1450
                save;
1451
                IF ~text.modified THEN
1452
                    K.Exit
1453
                END;
1454
                repaint
1455
            |btnClose:
1456
                close
1457
            |btnNew:
1458
                SelfRun("")
1459
            |btnOpen:
1460
                open
1461
            |btnSave:
1462
                save;
1463
                repaint
1464
            |btnSearch:
1465
                IF ~search THEN
1466
                    Search
1467
                END
1468
            |btnCloseFind:
1469
                Search
1470
            |btnUndo:
1471
                T.undo(text);
1472
                repaint
1473
            |btnRedo:
1474
                T.redo(text);
1475
                repaint
1476
            |btnUpper:
1477
                T.chCase(text, TRUE);
1478
                repaint
1479
            |btnLower:
1480
                T.chCase(text, FALSE);
1481
                repaint
1482
            |btnBuild:
1483
                Script(buildScript)
1484
            |btnRun:
1485
                Script(runScript)
1486
            |btnUp:
1487
                T.scroll(text, 0, -1);
1488
                repaint
1489
            |btnDown:
1490
                T.scroll(text, 0, 1);
1491
                repaint
1492
            |btnLeft:
1493
                T.scroll(text, -1, 0);
1494
                repaint
1495
            |btnRight:
1496
                T.scroll(text, 1, 0);
1497
                repaint
1498
            |btnFindEdit:
1499
                EditBox_SetFocus(FindEdit, TRUE);
1500
                EditBox_SetFocus(ReplaceEdit, FALSE);
1501
                EditBox_SetFocus(GotoEdit, FALSE)
1502
            |btnReplaceEdit:
1503
                EditBox_SetFocus(ReplaceEdit, TRUE);
1504
                EditBox_SetFocus(FindEdit, FALSE);
1505
                EditBox_SetFocus(GotoEdit, FALSE)
1506
            |btnGotoEdit:
1507
                EditBox_SetFocus(GotoEdit, TRUE);
1508
                EditBox_SetFocus(FindEdit, FALSE);
1509
                EditBox_SetFocus(ReplaceEdit, FALSE)
1510
            |btnFindNext:
1511
                IF searchText # "" THEN
1512
                    notFound := ~T.findNext(text, box_lib.check_box_get_value(BKW));
1513
                    repaint
1514
                END
1515
            |btnReplace:
1516
                T.replace(text, replaceText, LENGTH(searchText));
1517
                repaint
1518
            |btnReplaceAll:
1519
                notFound := ~T.search(text, searchText, cs, whole);
1520
                IF ~notFound THEN
1521
                    replaced := T.replaceAll(text, replaceText, LENGTH(searchText));
1522
                END;
1523
                repaint
1524
            |btnGoto:
1525
                goto;
1526
                repaint
1527
            END
1528
        |6:
1529
            Menu.close(menuFile);
1530
            Menu.close(menuEdit);
1531
            Menu.close(menuEncoding);
1532
            Menu.close(menuView);
1533
            Menu.close(menuSyntax);
1534
            Menu.close(menuProgram);
1535
            Menu.close(menuTools);
1536
            Menu.close(context);
1537
            IF ~resized THEN
1538
                getKBState;
1539
                msState := K.MouseState();
1540
                IF ~(0 IN msState) OR (16 IN msState) THEN
1541
                    LeftButtonUp
1542
                END;
1543
                scroll := K.Scroll();
1544
                IF scroll # 0 THEN
1545
                    T.scroll(text, 0, scroll*3);
1546
                    repaint
1547
                END;
1548
                IF leftButton THEN
1549
                    IF K.GetTickCount() - time > 9 THEN
1550
                        mouse(x, y);
1551
                        T.mouse(text, x, y);
1552
                        repaint
1553
                    END
1554
                END;
1555
                IF VScroll THEN
1556
                    mouse(x, y);
1557
                    Scroll.mouse(vScroll, x, y - scrollWidth);
1558
                    T.getScroll(text, scrollX, scrollY);
1559
                    T.scroll(text, 0, vScroll.value - scrollY);
1560
                    repaint
1561
                END;
1562
                IF HScroll THEN
1563
                    mouse(x, y);
1564
                    Scroll.mouse(hScroll, x - scrollWidth, y);
1565
                    T.getScroll(text, scrollX, scrollY);
1566
                    T.scroll(text, hScroll.value - scrollX, 0);
1567
                    repaint
1568
                END;
1569
                IF (8 IN msState) & ~(24 IN msState) THEN
1570
                    mouse(firstClickX, firstClickY);
1571
                    time := click(firstClickX, firstClickY)
1572
                END;
1573
                IF 9 IN msState THEN
1574
                    ShowMenu(context)
1575
                END;
1576
                IF 24 IN msState THEN
1577
                    mouse(x, y);
1578
                    IF (ABS(x - firstClickX) < 5) & (ABS(y - firstClickY) < 5) THEN
1579
                        VScroll := FALSE;
1580
                        HScroll := FALSE;
1581
                        IF (0 <= x) & (x < canvas.width) & (0 <= y) & (y < canvas.height) THEN
1582
                            leftButton := FALSE;
1583
                            T.selectWord(text);
1584
                            repaint
1585
                        END
1586
                    ELSE
1587
                        firstClickX := x;
1588
                        firstClickY := y;
1589
                        time := click(firstClickX, firstClickY)
1590
                    END
1591
                END
1592
            END;
1593
            IF search THEN
1594
                IF EditBox_Focus(FindEdit) THEN
1595
                    box_lib.edit_box_mouse(FindEdit)
1596
                END;
1597
                IF EditBox_Focus(ReplaceEdit) THEN
1598
                    box_lib.edit_box_mouse(ReplaceEdit)
1599
                END;
1600
                IF EditBox_Focus(GotoEdit) THEN
1601
                    box_lib.edit_box_mouse(GotoEdit)
1602
                END;
1603
                box_lib.check_box_mouse2(CS);
1604
                box_lib.check_box_mouse2(WH);
1605
                box_lib.check_box_mouse2(BKW);
1606
                IF box_lib.check_box_get_value(CS) # cs THEN
1607
                    cs := ~cs;
1608
                    notFound := ~T.search(text, searchText, cs, whole);
1609
                    repaint
1610
                END;
1611
                IF box_lib.check_box_get_value(WH) # whole THEN
1612
                    whole := ~whole;
1613
                    notFound := ~T.search(text, searchText, cs, whole);
1614
                    repaint
1615
                END
1616
            END;
1617
            resized := FALSE
1618
        |7: receiveIPC
1619
        ELSE
1620
        END
1621
    END
1622
END main;
1623
 
1624
 
1625
BEGIN
1626
    main
1627
END CEdit.