1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/gatemain.h"
#include "gate/results.h"
#include "gate/applications.hpp"
#include "gate/maps.hpp"
#include "gate/wrappers.hpp"
#include "gate/strings.hpp"
#include "gate/streams.hpp"
#include "gate/ui/gateui.hpp"
#include "gate/ui/forms.hpp"
#include "gate/ui/textboxes.hpp"
#include "gate/ui/menus.hpp"
#include "gate/ui/dialogs.hpp"
#include "gate/ui/timers.hpp"
#include "gate/ui/buttons.hpp"
#include "gate/ui/labels.hpp"
#include "gate/encode/texts.hpp"
#include "gate/files.hpp"
#include "gate/encode/md5hash.hpp"
#if defined(GATE_SYS_WINCE) || defined(GATE_SYS_WIN16)
#define VTXTEDIT_COMPACT 1
#else
//#define VTXTEDIT_COMPACT 1
#endif
#if !defined(VTXTEDIT_COMPACT)
#include "gate/ui/toolbars.hpp"
#include "gate/ui/statusbars.hpp"
#include "gate/graphics/pixmapimages.hpp"
#include "gate/icons/document_new.xpm"
#include "gate/icons/document_open.xpm"
#include "gate/icons/floppydisk.xpm"
#include "gate/icons/cut.xpm"
#include "gate/icons/copy.xpm"
#include "gate/icons/paste.xpm"
#include "gate/icons/cancel.xpm"
#endif
#if defined(GATE_COMPILER_MSVC) && defined(GATE_LEAK_DETECTION) && defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE) && (GATE_ARCH != GATE_ARCH_ARM32) && (GATE_ARCH != GATE_ARCH_ARM64)
# include <vld.h>
#endif
namespace gate
{
namespace apps
{
class GATE_API_LOCAL VTxtEdit : public App
{
private: // ui controls:
Optional<ui::Host> uihost;
ui::Form form;
ui::Layout formLayout;
#if !defined(VTXTEDIT_COMPACT)
ui::Toolbar tb;
ui::Statusbar sb;
#endif
ui::Textbox txt;<--- Shadowed declaration
ui::Timer timer;
ui::Font font;
ui::Menu mnuFile;
ui::Menu mnuEdit;
ui::Menu mnuFormat;
ui::Menu mnuFormatLineEnding;
ui::Menu mnuFormatEncoding;
ui::Menu mnuView;
ui::Menu mnuViewZoom;
ui::Menu mnuHelp;
ui::Menu mnu;
ui::FilePickerDialog dlgFile;
ui::Form formSearch;
ui::Layout formSearchLayout;
ui::Label lblSearch;
ui::Textbox txtSearch;
ui::Button btnSearchNext;
ui::Label lblReplace;
ui::Textbox txtReplace;
ui::Button btnReplace;
ui::Button btnReplaceAll;
private: // states:
uint32_t oldLinePos;
uint32_t oldColumnPos;
String loadedFileName;
String loadedHash;
static gate_uint16_t const MnuKey_FileNew = 0x01;
static gate_uint16_t const MnuKey_FileOpen = 0x02;
static gate_uint16_t const MnuKey_FileSave = 0x03;
static gate_uint16_t const MnuKey_FileSaveAs = 0x04;
static gate_uint16_t const MnuKey_FilePrint = 0x05;
static gate_uint16_t const MnuKey_FileExit = 0x06;
static gate_uint16_t const MnuKey_EditUndo = 0x11;
static gate_uint16_t const MnuKey_EditCut = 0x12;
static gate_uint16_t const MnuKey_EditCopy = 0x13;
static gate_uint16_t const MnuKey_EditPaste = 0x14;
static gate_uint16_t const MnuKey_EditDelete = 0x15;
static gate_uint16_t const MnuKey_EditSearch = 0x16;
static gate_uint16_t const MnuKey_EditReplace = 0x17;
static gate_uint16_t const MnuKey_EditSelectAll = 0x18;
static gate_uint16_t const MnuKey_FormatLineEndingDefault = 0x21;
static gate_uint16_t const MnuKey_FormatLineEndingWindows = 0x22;
static gate_uint16_t const MnuKey_FormatLineEndingUnix = 0x23;
static gate_uint16_t const MnuKey_FormatEncodingUTF8 = 0x25;
static gate_uint16_t const MnuKey_FormatEncodingANSI = 0x26;
static gate_uint16_t const MnuKey_FormatEncodingUTF8BOM = 0x27;
static gate_uint16_t const MnuKey_FormatEncodingUTF16LE = 0x28;
static gate_uint16_t const MnuKey_FormatEncodingUTF16BE = 0x29;
static gate_uint16_t const MnuKey_FormatEncodingUTF32LE = 0x2a;
static gate_uint16_t const MnuKey_FormatEncodingUTF32BE = 0x2b;
static gate_uint16_t const MnuKey_ViewFont = 0x31;
static gate_uint16_t const MnuKey_ViewZoomIncrease = 0x32;
static gate_uint16_t const MnuKey_ViewZoomDecrease = 0x33;
static gate_uint16_t const MnuKey_HelpInfo = 0x41;
bool isModified()
{
String data = this->txt.getText();
String hash = enc::Md5Hash::compute(data);
return this->loadedHash != hash;
}
void newFile()
{
String data;
this->txt.setText(data);
this->loadedHash = enc::Md5Hash::compute(data);
this->loadedFileName = String();
}
bool loadFile(String const& fileName)
{
bool ret = false;
try
{
FileStream fs(fileName);
StringStream txt;<--- Shadow variable
Stream::transfer(fs, txt);
String data = txt.toView();
enc::Text::BomTypeEnum bomtype;
size_t bomlen = enc::Text::detectBomType(data.c_str(), data.length(), bomtype);
switch (bomtype)
{
case enc::Text::BomType_Utf8:
{
String native_data = data.substr(bomlen);
data = native_data;
break;
}
case enc::Text::BomType_Utf16BE:
{
String native_data = data.substr(bomlen);
LocalMemStream utf16_stream(native_data.c_str(), native_data.length());
StringBuilder builder;
enc::Text::loadUtf16BE(utf16_stream, builder);
data = builder.toString();
break;
}
case enc::Text::BomType_Utf16LE:
{
String native_data = data.substr(bomlen);
LocalMemStream utf16_stream(native_data.c_str(), native_data.length());
StringBuilder builder;
enc::Text::loadUtf16LE(utf16_stream, builder);
data = builder.toString();
break;
}
case enc::Text::BomType_Utf32BE:
{
String native_data = data.substr(bomlen);
LocalMemStream utf32_stream(native_data.c_str(), native_data.length());
StringBuilder builder;
enc::Text::loadUtf32BE(utf32_stream, builder);
data = builder.toString();
break;
}
case enc::Text::BomType_Utf32LE:
{
String native_data = data.substr(bomlen);
LocalMemStream utf32_stream(native_data.c_str(), native_data.length());
StringBuilder builder;
enc::Text::loadUtf32LE(utf32_stream, builder);
data = builder.toString();
break;
}
default:
{
String native_data = data;
LocalMemStream ansi_stream(native_data.c_str(), native_data.length());
StringBuilder builder;
enc::Text::loadAnsi(ansi_stream, builder);
data = builder.toString();
break;
}
}
this->txt.setText(data);
this->loadedHash = enc::Md5Hash::compute(data);
this->loadedFileName = fileName;
ret = true;
}
catch (Throwable const& xcpt)
{
ui::MsgBox msgbox;
StringBuilder info;
info << "Loading file" << strings::NewLine
<< fileName << strings::NewLine
<< "failed." << strings::NewLine
<< strings::NewLine
<< xcpt.getMessage() << strings::NewLine
<< xcpt.getSource();
msgbox.show(this->form, info.toString(), "Operation failed", ui::MsgBox::Type_Error | ui::MsgBox::Type_OkOnly);
}
return ret;
}
bool saveFile(String const& fileName)
{
bool ret = false;
String data = this->txt.getText();
uint32_t lineFormat = this->getLineFormatSelection();
uint32_t charFormat = this->getEncodingFormatSelection();
try
{
{
FileStream fs(fileName, Stream::Open_Write);
if (!data.empty())
{
static char const CR = '\r';
static char const LF = '\n';
String saveData;
if (lineFormat == MnuKey_FormatLineEndingWindows)
{
StringBuilder builder(data.size());
char const* ptr = data.c_str();
size_t len = data.size();
while (len-- != 0)
{
if (*ptr == CR)
{
// ignore CR
}
else if (*ptr == LF)
{
// trigger CR+LF
builder.append(&CR, 1);
builder.append(&LF, 1);
}
else
{
builder.append(ptr, 1);
}
}
saveData = builder.toString();
}
else if (lineFormat == MnuKey_FormatLineEndingUnix)
{
StringBuilder builder(data.size());
char const* ptr = data.c_str();
size_t len = data.size();
while (len-- != 0)
{
if (*ptr != CR)
{
// skip all CR and save everything else (including LF)
builder.append(ptr, 1);
}
}
saveData = builder.toString();
}
else
{
/* saveData is original text-box data */
saveData = data;
}
if (charFormat == MnuKey_FormatEncodingUTF8)
{
enc::Text::saveUtf8(saveData, fs, false);
}
else if (charFormat == MnuKey_FormatEncodingANSI)
{
enc::Text::saveAnsi(saveData, fs);
}
else if (charFormat == MnuKey_FormatEncodingUTF8BOM)
{
enc::Text::saveUtf8(saveData, fs, true);
}
else if (charFormat == MnuKey_FormatEncodingUTF16LE)
{
enc::Text::saveUtf16LE(saveData, fs, true);
}
else if (charFormat == MnuKey_FormatEncodingUTF16BE)
{
enc::Text::saveUtf16BE(saveData, fs, true);
}
else if (charFormat == MnuKey_FormatEncodingUTF32LE)
{
enc::Text::saveUtf32LE(saveData, fs, true);
}
else if (charFormat == MnuKey_FormatEncodingUTF32BE)
{
enc::Text::saveUtf32BE(saveData, fs, true);
}
else
{
// everything else is treated as UTF-8 (without BOM)
fs.writeBlock(saveData.c_str(), saveData.length());
}
}
fs.flush();
}
this->loadedFileName = fileName;
this->loadedHash = enc::Md5Hash::compute(data);
ret = true;
}
catch (Throwable const& xcpt)
{
ui::MsgBox msgbox;
StringBuilder info;
info << "Saving to file" << strings::NewLine
<< fileName << strings::NewLine
<< "failed." << strings::NewLine
<< strings::NewLine
<< xcpt.getMessage() << strings::NewLine
<< xcpt.getSource();
msgbox.show(this->form, info.toString(), "Operation failed", ui::MsgBox::Type_Error | ui::MsgBox::Type_OkOnly);
}
return ret;
}
void fileNewInternal()
{
this->stopEditMode();
this->newFile();
this->txt.setText(String());
this->startEditMode();
}
void fileNew()
{
if (this->checkSaveChanges())
{
this->fileNewInternal();
}
}
bool fileOpen()
{
bool ret = false;
do
{
if (!this->checkSaveChanges())
{
break;
}
this->stopEditMode();
String fileName;
try
{
if (!dlgFile.showOpenDialog(this->form))
{
break;
}
fileName = dlgFile.getFileName();
ret = this->loadFile(fileName);
}
catch (Throwable const& xcpt)
{
(void)xcpt;
ui::MsgBox msg;
msg.show(this->form, "Exception", "Failed to select file");
}
} while (0);
this->startEditMode();
return ret;
}
bool fileSave()
{
bool ret = false;
if (this->loadedFileName.empty())
{
ret = this->fileSaveAs();
}
else
{
this->stopEditMode();
ret = this->saveFile(this->loadedFileName);
this->startEditMode();
}
return ret;
}
bool fileSaveAs()
{
bool ret = false;
this->stopEditMode();
do
{
String fileName;
try
{
if (!dlgFile.showSaveDialog(this->form))
{
break;
}
fileName = dlgFile.getFileName();
ret = this->saveFile(fileName);
}
catch (Throwable const& xcpt)
{
(void)xcpt;
ui::MsgBox msg;
msg.show(this->form, "Exception", "Failed to select file");
}
} while (0);
this->startEditMode();
return ret;
}
void filePrint()
{
}
bool checkSaveChanges()
{
// return "false" to cancel further operations
bool ret = false;
if (!this->isModified())
{
// nothing to save, just continue
ret = true;
}
else
{
ui::MsgBox msgbox;
StringBuilder builder;
builder << "There are unsaved changes in your textbox." << strings::NewLine
<< strings::NewLine;
if (this->loadedFileName.empty())
{
builder << "Do you want to save your input?";
}
else
{
builder << "Do you want to save all changes to: " << strings::NewLine
<< this->loadedFileName << " ?";
}
String msg = builder.toString();
ui::MsgBox::ChoiceEnum choice = msgbox.show(this->form, msg, "Save changes?", ui::MsgBox::Type_YesNoCancel);
if (choice == ui::MsgBox::Choice_Yes)
{
this->fileSave();
}
else if (choice == ui::MsgBox::Choice_No)
{
// discard changes, continue
ret = true;
}
}
return ret;
}
void fileExit()
{
if (this->checkSaveChanges())
{
if (this->timer.isCreated())
{
this->timer.destroy();
}
if (this->formSearch.isCreated())
{
this->formSearch.destroy();
}
if (this->form.isCreated())
{
this->form.destroy();
}
uihost->quit();
}
}
void editUndo()
{
this->txt.undo();
}
void editCut()
{
String text = this->txt.getSelectionText();
this->uihost->setClipboardText(text);
this->txt.replaceSelection(String());
}
void editCopy()
{
String text = this->txt.getSelectionText();
this->uihost->setClipboardText(text);
}
void editPaste()
{
String clipboardText;
if (this->uihost->getClipboardText(clipboardText))
{
this->txt.replaceSelection(clipboardText);
}
}
void editDelete()
{
this->txt.replaceSelection(String());
}
void editSearch(bool showReplace = false)
{
static String const textSearch = String::createStatic("Search Text");
static String const textReplace = String::createStatic("Replace Text");
if (!this->formSearch.isCreated())
{
uint32_t ctrlHeight = this->uihost->getControlHeight();
uint32_t charWidth = this->uihost->getLineHeight() * 2 / 3;
uint32_t border = ctrlHeight / 2;
ui::Position parentPos = this->form.getPosition();
int32_t searchHeight = ctrlHeight * 10;
int32_t searchWidth = ctrlHeight * 32;
ui::Position searchPos(parentPos.getCenterX() - searchWidth / 2,
parentPos.getCenterY() - searchHeight / 2,
searchWidth, searchHeight);
this->formSearchLayout.clear();
this->formSearchLayout.setBorder(border);
this->formSearchLayout.setColumnSpace(border);
this->formSearchLayout.setRowSpace(border);
this->formSearch.create(this->form, &searchPos, textSearch,
ui::Form::Flag_Enabled | ui::Form::Flag_Visible | ui::Form::Flag_Resizable);
this->formSearch.CloseEvent += ui::Form::CloseEventHandler(this, &VTxtEdit::Search_Closed);
this->lblSearch.create(this->formSearch, ui::Position(), "Find Token:");
this->txtSearch.create(this->formSearch, ui::Position());
this->btnSearchNext.create(this->formSearch, ui::Position(), "Find Next");
this->lblReplace.create(this->formSearch, ui::Position(), "Replace with:");
this->txtReplace.create(this->formSearch, ui::Position());
this->btnReplace.create(this->formSearch, ui::Position(), "Replace Next");
this->btnReplaceAll.create(this->formSearch, ui::Position(), "Replace All");
uint32_t col1 = this->formSearchLayout.addColumn(ui::Layout::Type_Pixel, real32_t(charWidth * 12));
uint32_t col2 = this->formSearchLayout.addColumn();
uint32_t col3 = this->formSearchLayout.addColumn(ui::Layout::Type_Pixel, real32_t(charWidth * 12));
uint32_t row1 = this->formSearchLayout.addRow(ui::Layout::Type_UnitScale, 1.0f);
uint32_t row2 = this->formSearchLayout.addRow(ui::Layout::Type_UnitScale, 1.0f);
uint32_t row3 = this->formSearchLayout.addRow(ui::Layout::Type_UnitScale, 1.0f);
uint32_t row4 = this->formSearchLayout.addRow(ui::Layout::Type_UnitScale, 1.0f);<--- Variable 'row4' is assigned a value that is never used.
this->formSearchLayout.setControl(this->lblSearch, row1, col1);
this->formSearchLayout.setControl(this->txtSearch, row1, col2);
this->formSearchLayout.setControl(this->btnSearchNext, row1, col3);
this->formSearchLayout.setControl(this->lblReplace, row2, col1);
this->formSearchLayout.setControl(this->txtReplace, row2, col2);
this->formSearchLayout.setControl(this->btnReplace, row2, col3);
this->formSearchLayout.setControl(this->btnReplaceAll, row3, col3);
this->formSearch.setLayout(&this->formSearchLayout);
}
this->lblReplace.setVisible(showReplace);
this->txtReplace.setVisible(showReplace);
this->btnReplace.setVisible(showReplace);
this->btnReplaceAll.setVisible(showReplace);
this->formSearch.setText(showReplace ? textReplace : textSearch);
this->formSearch.setVisible(true);
}
void editReplace()
{
this->editSearch(true);
}
void editSelectAll()
{
this->txt.setFocus();
uint32_t len = this->txt.getTextLength();
this->txt.setSelection(0, len);
}
void updateMenuBar()
{
this->form.setMenu(&this->mnu);
}
void formatLineDefault()
{
for (size_t ndx = 0; ndx != this->mnuFormatLineEnding.count; ++ndx)
{
this->mnuFormatLineEnding.entries[ndx].flags = 0;
}
this->mnuFormatLineEnding.entries[0].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
void formatLineWindows()
{
for (size_t ndx = 0; ndx != this->mnuFormatLineEnding.count; ++ndx)
{
this->mnuFormatLineEnding.entries[ndx].flags = 0;
}
this->mnuFormatLineEnding.entries[1].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
void formatLineUnix()
{
for (size_t ndx = 0; ndx != this->mnuFormatLineEnding.count; ++ndx)
{
this->mnuFormatLineEnding.entries[ndx].flags = 0;
}
this->mnuFormatLineEnding.entries[2].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
uint32_t getLineFormatSelection()
{
for (size_t ndx = 0; ndx != this->mnuFormatLineEnding.length(); ++ndx)
{
if (GATE_FLAG_ENABLED(this->mnuFormatLineEnding.entries[ndx].flags, GATE_UI_MENU_FLAG_CHECKED))
{
return this->mnuFormatLineEnding.entries[ndx].id;
}
}
return MnuKey_FormatLineEndingDefault;
}
uint32_t getEncodingFormatSelection()
{
for (size_t ndx = 0; ndx != this->mnuFormatEncoding.length(); ++ndx)
{
if (GATE_FLAG_ENABLED(this->mnuFormatEncoding.entries[ndx].flags, GATE_UI_MENU_FLAG_CHECKED))
{
return this->mnuFormatEncoding.entries[ndx].id;
}
}
return MnuKey_FormatEncodingUTF8;
}
void formatEncodeAnsi()
{
for (size_t ndx = 0; ndx != this->mnuFormatEncoding.count; ++ndx)
{
this->mnuFormatEncoding.entries[ndx].flags = 0;
}
this->mnuFormatEncoding.entries[0].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
void formatEncodeUtf8()
{
for (size_t ndx = 0; ndx != this->mnuFormatEncoding.count; ++ndx)
{
this->mnuFormatEncoding.entries[ndx].flags = 0;
}
this->mnuFormatEncoding.entries[1].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
void formatEncodeUtf8Bom()
{
for (size_t ndx = 0; ndx != this->mnuFormatEncoding.count; ++ndx)
{
this->mnuFormatEncoding.entries[ndx].flags = 0;
}
this->mnuFormatEncoding.entries[2].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
void formatEncodeUtf16L()
{
for (size_t ndx = 0; ndx != this->mnuFormatEncoding.count; ++ndx)
{
this->mnuFormatEncoding.entries[ndx].flags = 0;
}
this->mnuFormatEncoding.entries[3].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
void formatEncodeUtf16B()
{
for (size_t ndx = 0; ndx != this->mnuFormatEncoding.count; ++ndx)
{
this->mnuFormatEncoding.entries[ndx].flags = 0;
}
this->mnuFormatEncoding.entries[4].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
void formatEncodeUtf32L()
{
for (size_t ndx = 0; ndx != this->mnuFormatEncoding.count; ++ndx)
{
this->mnuFormatEncoding.entries[ndx].flags = 0;
}
this->mnuFormatEncoding.entries[5].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
void formatEncodeUtf32B()
{
for (size_t ndx = 0; ndx != this->mnuFormatEncoding.count; ++ndx)
{
this->mnuFormatEncoding.entries[ndx].flags = 0;
}
this->mnuFormatEncoding.entries[6].flags = GATE_UI_MENU_FLAG_CHECKED;
this->updateMenuBar();
}
void viewFont()
{
}
void viewZoomInc()
{
if (font.size < 64)
{
this->font.size += 2;
}
this->txt.setFont(font);
}
void viewZoomDec()
{
if (font.size > 9)
{
this->font.size -= 2;
}
this->txt.setFont(font);
}
void helpInfo()
{
ui::MsgBox msgbox;
static String const infoText(
"GATE Text Editor" GATE_STR_NEWLINE
GATE_STR_NEWLINE
"This program is part of the VAST GATE project. " GATE_STR_NEWLINE
"It is provided \"AS IS\" and is published under the terms of the BSD License." GATE_STR_NEWLINE
GATE_STR_NEWLINE
"(C) 2025 www.OpenGATE.at" GATE_STR_NEWLINE
"Author: stefan@opengate.at"
);
static String const infoTitle(
"GATE Text Editor"
);
msgbox.show(this->form, infoText, infoTitle, ui::MsgBox::Type_OkOnly | ui::MsgBox::Type_Info);
}
void updatePositionInStatusbar()
{
#if !defined(VTXTEDIT_COMPACT)
uint32_t line = this->txt.getCurrentLine() + 1;
uint32_t col = this->txt.getCurrentColumn() + 1;
if ((line != this->oldLinePos) || (col != this->oldColumnPos))
{
this->oldLinePos = line;
this->oldColumnPos = col;
StringStream strm;
strm << "Line: " << line << ", Column: " << col;
this->sb.setText(strm.toString());
}
#endif
}
void startEditMode()
{
this->timer.start(200);
}
void stopEditMode()
{
if (this->timer.isStarted())
{
this->timer.stop();
}
}
void handleMenuAction(gate_uint16_t menuid)
{
if (menuid == MnuKey_FileNew) { this->fileNew(); }
else if (menuid == MnuKey_FileOpen) { this->fileOpen(); }
else if (menuid == MnuKey_FileSave) { this->fileSave(); }
else if (menuid == MnuKey_FileSaveAs) { this->fileSaveAs(); }
else if (menuid == MnuKey_FilePrint) { this->filePrint(); }
else if (menuid == MnuKey_FileExit) { this->fileExit(); }
else if (menuid == MnuKey_EditUndo) { this->editUndo(); }
else if (menuid == MnuKey_EditCut) { this->editCut(); }
else if (menuid == MnuKey_EditCopy) { this->editCopy(); }
else if (menuid == MnuKey_EditPaste) { this->editPaste(); }
else if (menuid == MnuKey_EditDelete) { this->editDelete(); }
else if (menuid == MnuKey_EditSearch) { this->editSearch(); }
else if (menuid == MnuKey_EditReplace) { this->editReplace(); }
else if (menuid == MnuKey_EditSelectAll) { this->editSelectAll(); }
else if (menuid == MnuKey_FormatLineEndingDefault) { this->formatLineDefault(); }
else if (menuid == MnuKey_FormatLineEndingWindows) { this->formatLineWindows(); }
else if (menuid == MnuKey_FormatLineEndingUnix) { this->formatLineUnix(); }
else if (menuid == MnuKey_FormatEncodingUTF8) { this->formatEncodeUtf8(); }
else if (menuid == MnuKey_FormatEncodingANSI) { this->formatEncodeAnsi(); }
else if (menuid == MnuKey_FormatEncodingUTF8BOM) { this->formatEncodeUtf8Bom(); }
else if (menuid == MnuKey_FormatEncodingUTF16LE) { this->formatEncodeUtf16L(); }
else if (menuid == MnuKey_FormatEncodingUTF16BE) { this->formatEncodeUtf16B(); }
else if (menuid == MnuKey_FormatEncodingUTF32LE) { this->formatEncodeUtf32L(); }
else if (menuid == MnuKey_FormatEncodingUTF32BE) { this->formatEncodeUtf32B(); }
else if (menuid == MnuKey_ViewFont) { this->viewFont(); }
else if (menuid == MnuKey_ViewZoomIncrease) { this->viewZoomInc(); }
else if (menuid == MnuKey_ViewZoomDecrease) { this->viewZoomDec(); }
else if (menuid == MnuKey_HelpInfo) { this->helpInfo(); }
}
void Form_Menu(ui::Form* sender, ui::MenuArg* arg)
{
this->handleMenuAction(arg->MenuEntry.id);
}
#if !defined(VTXTEDIT_COMPACT)
void Toolbar_Click(ui::Toolbar* sender, ui::Toolbar::ToolbarEventArg* arg)
{
gate_uint16_t menuId = (gate_uint16_t)(gate_uintptr_t)arg->button_param;
this->handleMenuAction(menuId);
}
#endif
void Form_Closed(ui::Form* sender, ui::EventArg* arg)
{
this->fileExit();
}
void Search_Closed(ui::Form* sender, ui::EventArg* arg)
{
this->formSearch.destroy();
}
void Timer_Interval(ui::Timer* sender, ui::Timer::TimerArg* arg)
{
this->updatePositionInStatusbar();
}
void searchNextText(String const& find, bool backwards = false)
{
uint32_t lineCount = this->txt.getLineCount();<--- Variable 'lineCount' is assigned a value that is never used.
uint32_t col = this->txt.getCurrentColumn();<--- Variable 'col' is assigned a value that is never used.
uint32_t line = this->txt.getCurrentLine();<--- Variable 'line' is assigned a value that is never used.
String text = this->txt.getText();
}
void SearchNext_Click(ui::Button* sender, ui::EventArg* arg)
{
String searchToken = this->txtSearch.getText();
gate_uint32_t findFlags = 0;
bool matchFound = this->txtSearch.findNext(searchToken, findFlags);
if (!matchFound)
{
ui::MsgBox msgbox;
msgbox.show(this->form, "Search token not found", "Find next token",
ui::MsgBox::Type_OkOnly | ui::MsgBox::Type_Info);
}
}
void Replace_Click(ui::Button* sender, ui::EventArg* arg)
{
}
void ReplaceAll_Click(ui::Button* sender, ui::EventArg* arg)
{
}
public:
VTxtEdit()
{
this->oldLinePos = (uint32_t)-1;
this->oldColumnPos = (uint32_t)-1;
static String const strExtTxt = String::createStatic("*.txt", 5);
static String const strExtAll = String::createStatic("*.*", 3);
static String const strDescrTxt = String::createStatic("Text Files (*.txt)");
static String const strDescrAll = String::createStatic("All Files (*.*)");
try
{
dlgFile.addFilter(strExtTxt, strDescrTxt);
dlgFile.addFilter(strExtAll, strDescrAll);
mnuFile.add("New", MnuKey_FileNew);
mnuFile.add("Open", MnuKey_FileOpen);
mnuFile.add("Save", MnuKey_FileSave);
mnuFile.add("Save As...", MnuKey_FileSaveAs);
mnuFile.addSeparator();
mnuFile.add("Print", MnuKey_FilePrint);
mnuFile.addSeparator();
mnuFile.add("Exit", MnuKey_FileExit);
mnuEdit.add("Undo", MnuKey_EditUndo);
mnuEdit.addSeparator();
mnuEdit.add("Cut", MnuKey_EditCut);
mnuEdit.add("Copy", MnuKey_EditCopy);
mnuEdit.add("Paste", MnuKey_EditPaste);
mnuEdit.add("Delete", MnuKey_EditDelete);
mnuEdit.addSeparator();
mnuEdit.add("Search...", MnuKey_EditSearch);
mnuEdit.add("Replace...", MnuKey_EditReplace);
mnuEdit.addSeparator();
mnuEdit.add("Select all", MnuKey_EditSelectAll);
mnuFormatLineEnding.add("System Default", MnuKey_FormatLineEndingDefault);
mnuFormatLineEnding.add("Windows (CR LF)", MnuKey_FormatLineEndingWindows);
mnuFormatLineEnding.add("Unix (LF)", MnuKey_FormatLineEndingUnix);
mnuFormatEncoding.add("Windows ANSI", MnuKey_FormatEncodingANSI);
mnuFormatEncoding.add("UTF-8 (without BOM)", MnuKey_FormatEncodingUTF8);
mnuFormatEncoding.add("UTF-8 (with BOM)", MnuKey_FormatEncodingUTF8BOM);
mnuFormatEncoding.add("UTF-16 (Little Endian)", MnuKey_FormatEncodingUTF16LE);
mnuFormatEncoding.add("UTF-16 (Big Endian)", MnuKey_FormatEncodingUTF16BE);
mnuFormatEncoding.add("UTF-32 (Little Endian)", MnuKey_FormatEncodingUTF32LE);
mnuFormatEncoding.add("UTF-32 (Bit Endian)", MnuKey_FormatEncodingUTF32BE);
mnuFormat.add("Line Ending", 0, 0, &mnuFormatLineEnding);
mnuFormat.add("Encoding", 0, 0, &mnuFormatEncoding);
mnuViewZoom.add("Increase Font Size", MnuKey_ViewZoomIncrease);
mnuViewZoom.add("Decrease Font Size", MnuKey_ViewZoomDecrease);
mnuView.add("Font", MnuKey_ViewFont);
mnuView.add("Zoom", 0, 0, &mnuViewZoom);
mnuHelp.add("Info", MnuKey_HelpInfo);
mnu.add("File", 0, 0, &mnuFile);
mnu.add("Edit", 0, 0, &mnuEdit);
mnu.add("Format", 0, 0, &mnuFormat);
mnu.add("View", 0, 0, &mnuView);
mnu.add("Help", 0, 0, &mnuHelp);
this->form.CloseEvent += ui::Form::CloseEventHandler(this, &VTxtEdit::Form_Closed);
this->form.MenuEvent += ui::Form::MenuEventHandler(this, &VTxtEdit::Form_Menu);
this->timer.IntervalEvent += ui::Timer::IntervalEventHandler(this, &VTxtEdit::Timer_Interval);
this->btnSearchNext.ClickEvent += ui::Button::ClickEventHandler(this, &VTxtEdit::SearchNext_Click);
this->btnReplace.ClickEvent += ui::Button::ClickEventHandler(this, &VTxtEdit::Replace_Click);
this->btnReplaceAll.ClickEvent += ui::Button::ClickEventHandler(this, &VTxtEdit::ReplaceAll_Click);
#if !defined(VTXTEDIT_COMPACT)
this->tb.ClickEvent += ui::Toolbar::ClickEventHandler(this, &VTxtEdit::Toolbar_Click);
#endif
}
catch (Exception const& xcpt)
{
(void)xcpt;
throw;
}
}
~VTxtEdit() noexcept
{
}
#if !defined(VTXTEDIT_COMPACT)
ui::Toolbar::button_t addStockToolButton(ui::Icon::StockIdEnum stockIconId, void* userparam)
{
ui::Icon icon(*this->uihost, stockIconId, true);
return this->tb.add(icon, userparam);
}
ui::Toolbar::button_t addXpmToolButton(char const* const* xpm, void* userparam)
{
ui::RasterImage image;
graph::PixmapImages::parse(image, xpm);
return this->tb.add(image, userparam);
}
void loadButtonImages()
{
this->tb.addSeparator();
//this->addStockToolButton(ui::Icon::StockId_NewFile, (void*)(gate_uintptr_t)MnuKey_FileNew);
//this->addStockToolButton(ui::Icon::StockId_OpenFile, (void*)(gate_uintptr_t)MnuKey_FileOpen);
//this->addStockToolButton(ui::Icon::StockId_SaveFile, (void*)(gate_uintptr_t)MnuKey_FileSave);
this->addXpmToolButton(gate_icon_document_new_xpm, (void*)(gate_uintptr_t)MnuKey_FileNew);
this->addXpmToolButton(gate_icon_document_open_xpm, (void*)(gate_uintptr_t)MnuKey_FileOpen);
this->addXpmToolButton(gate_icon_floppydisk_xpm, (void*)(gate_uintptr_t)MnuKey_FileSave);
this->tb.addSeparator();
//this->addStockToolButton(ui::Icon::StockId_Cut, (void*)(gate_uintptr_t)MnuKey_EditCut);
//this->addStockToolButton(ui::Icon::StockId_Copy, (void*)(gate_uintptr_t)MnuKey_EditCopy);
//this->addStockToolButton(ui::Icon::StockId_Paste, (void*)(gate_uintptr_t)MnuKey_EditPaste);
//this->addStockToolButton(ui::Icon::StockId_Delete, (void*)(gate_uintptr_t)MnuKey_EditDelete);
this->addXpmToolButton(gate_icon_cut_xpm, (void*)(gate_uintptr_t)MnuKey_EditCut);
this->addXpmToolButton(gate_icon_copy_xpm, (void*)(gate_uintptr_t)MnuKey_EditCopy);
this->addXpmToolButton(gate_icon_paste_xpm, (void*)(gate_uintptr_t)MnuKey_EditPaste);
this->addXpmToolButton(gate_icon_cancel_xpm, (void*)(gate_uintptr_t)MnuKey_EditDelete);
this->tb.addSeparator();
}
#endif
void initControls()
{
this->font = this->uihost->getDefaultFont(ui::Host::FontType_Monospace);
real32_t fontPtSize = this->uihost->getPointsOfPixels(this->font.size);
if (fontPtSize < 10.0f)
{
this->font.size = this->uihost->getPixelsOfPoints(10.0f);
}
this->txt.create(this->form, ui::Position(0, 0, 64, 64), "",
ui::Textbox::Flag_Enabled | ui::Textbox::Flag_Visible |
ui::Textbox::Flag_Multiline | ui::Textbox::Flag_HScroll | ui::Textbox::Flag_VScroll
);
this->txt.setFont(font);
#if !defined(VTXTEDIT_COMPACT)
this->tb.create(this->form, ui::Position(0, 0, 64, 64));
this->loadButtonImages();
this->sb.create(this->form, ui::Position(0, 64, 64, 64), "Editor");
#endif
this->timer.create(*this->uihost);
}
void showException(Exception const& xcpt, String const& title)
{
StringBuilder text;
text << String::createStatic(xcpt.getMessage()) << strings::NewLine;
text << String::createStatic(xcpt.getSource()) << strings::NewLine;
if (xcpt.getErrorCode() != 0)
{
text << "Status code: " << xcpt.getErrorCode() << strings::NewLine;
}
text << "Result code: " << (int32_t)xcpt.getResult();
String msgText = text.toString();
ui::MsgBox msg;
msg.show(this->form, msgText, title, ui::MsgBox::Type_Error | ui::MsgBox::Type_OkOnly);
}
virtual void onInit()
{
static String const initErrorTitle = String::createStatic("Control initialization failed");
try
{
this->uihost.emplace<uintptr_t const>(this->getHandle());
}
catch (Exception const& xcpt)
{
this->showException(xcpt, initErrorTitle);
}
try
{
uint32_t lineheight = this->uihost->getLineHeight();<--- Variable 'lineheight' is assigned a value that is never used.
uint32_t controlHeight = this->uihost->getControlHeight();<--- Variable 'controlHeight' is assigned a value that is never used.
formLayout.clear();
uint32_t colMain = formLayout.addColumn();
#if !defined(VTXTEDIT_COMPACT)
// toolbar
uint32_t rowTb = formLayout.addRow(ui::Layout::Type_UnitScale, 1.5f);
formLayout.setControl(this->tb, rowTb, colMain);
#endif
// text area
uint32_t rowText = formLayout.addRow();
formLayout.setControl(this->txt, rowText, colMain);
#if !defined(VTXTEDIT_COMPACT)
// status bar
uint32_t rowStatus = formLayout.addRow(ui::Layout::Type_UnitScale, 1.0f);
formLayout.setControl(this->sb, rowStatus, colMain);
#endif
this->form.create(*this->uihost, NULL, "Visual Text Editor",
ui::Form::Flag_Enabled | ui::Form::Flag_Resizable | ui::Form::Flag_Minimizable | ui::Form::Flag_Maximizable);
this->form.setMenu(&this->mnu);
//this->form.setLayout(&this->formLayout);
}
catch (Exception const& xcpt)
{
this->showException(xcpt, initErrorTitle);
}
try
{
this->initControls();
}
catch (Exception const& xcpt)
{
this->showException(xcpt, initErrorTitle);
}
try
{
this->form.setLayout(&this->formLayout);
this->form.setVisible(true);
this->formatLineDefault();
this->formatEncodeUtf8();
this->fileNewInternal();
}
catch (Exception const& xcpt)
{
this->showException(xcpt, initErrorTitle);
}
}
virtual void run()
{
this->uihost->run();
}
};
} // end of namespace apps
} // end of namespace gate
int gate_main(char const* program, char const* const* arguments, gate_size_t argcount, gate_uintptr_t apphandle)
{
gate::apps::VTxtEdit vtxtedit;
return gate::App::runApp(vtxtedit, program, arguments, argcount, apphandle);
}
|