Subversion Repositories Kolibri OS

Rev

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