-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunitmain.pas
1884 lines (1529 loc) · 54.2 KB
/
unitmain.pas
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
unit unitmain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, PrintersDlgs, Forms, Controls, Graphics,
Dialogs,
Grids, ComCtrls, Menus,
Printers,
StdCtrls,
LConvEncoding,
LazHelpHTML,
UTF8Process,
LCLIntf, LCLType,
clipbrd, //clipboard
fpspreadsheet, fpsallformats, //export ods /xls
inifiles,csvdocument;
type
TRange = record
Min : integer;
Max : integer;
end;
type
{ TFormMain }
TFormMain = class(TForm)
FindDialog1: TFindDialog;
FontDialog1: TFontDialog;
ImageList1: TImageList;
MainMenu1: TMainMenu;
MenuItem1: TMenuItem;
MenuItem10: TMenuItem;
MenuItem100: TMenuItem;
MenuItem11: TMenuItem;
MenuItem12: TMenuItem;
MenuItem13: TMenuItem;
MenuItem14: TMenuItem;
MenuItem15: TMenuItem;
MenuItem16: TMenuItem;
MenuItemAutoResize: TMenuItem;
MenuItemToggleLabel: TMenuItem;
MenuItem19: TMenuItem;
MenuItem2: TMenuItem;
MenuItem20: TMenuItem;
MenuItem21: TMenuItem;
MenuItem22: TMenuItem;
MenuItem23: TMenuItem;
MenuItem24: TMenuItem;
MenuItem25: TMenuItem;
MenuItem26: TMenuItem;
MenuItem27: TMenuItem;
MenuItem28: TMenuItem;
MenuItem29: TMenuItem;
MenuItem3: TMenuItem;
MenuItem30: TMenuItem;
MenuItem31: TMenuItem;
MenuItem32: TMenuItem;
MenuItem33: TMenuItem;
MenuItem34: TMenuItem;
MenuItem35: TMenuItem;
MenuItem36: TMenuItem;
MenuItem37: TMenuItem;
MenuItem39: TMenuItem;
MenuItem4: TMenuItem;
MenuItemColors: TMenuItem;
MenuItem41: TMenuItem;
MenuItem42: TMenuItem;
MenuItemRestoreDefaults: TMenuItem;
MenuItem46: TMenuItem;
MenuItem48: TMenuItem;
MenuItem49: TMenuItem;
MenuItem5: TMenuItem;
MenuItem50: TMenuItem;
MenuItem51: TMenuItem;
MenuItem52: TMenuItem;
MenuItem53: TMenuItem;
MenuItem54: TMenuItem;
MenuItem55: TMenuItem;
MenuItem56: TMenuItem;
MenuItem57: TMenuItem;
MenuItem58: TMenuItem;
MenuItem59: TMenuItem;
MenuItem6: TMenuItem;
MenuItem60: TMenuItem;
MenuItem61: TMenuItem;
MenuItem62: TMenuItem;
MenuItem63: TMenuItem;
MenuItem64: TMenuItem;
MenuItem65: TMenuItem;
MenuItem66: TMenuItem;
MenuItem67: TMenuItem;
MenuItem68: TMenuItem;
MenuItem69: TMenuItem;
MenuItem7: TMenuItem;
MenuItem70: TMenuItem;
MenuItem71: TMenuItem;
MenuItem72: TMenuItem;
MenuItem73: TMenuItem;
MenuItem74: TMenuItem;
MenuItem75: TMenuItem;
MenuItem76: TMenuItem;
MenuItem77: TMenuItem;
MenuItem78: TMenuItem;
MenuItem79: TMenuItem;
MenuItem8: TMenuItem;
MenuItem80: TMenuItem;
MenuItem81: TMenuItem;
MenuItem82: TMenuItem;
MenuItem83: TMenuItem;
MenuItem84: TMenuItem;
MenuItem85: TMenuItem;
MenuItem86: TMenuItem;
MenuItem87: TMenuItem;
MenuItem88: TMenuItem;
MenuItem89: TMenuItem;
MenuItem9: TMenuItem;
MenuItem90: TMenuItem;
MenuItem91: TMenuItem;
MenuItem92: TMenuItem;
MenuItem93: TMenuItem;
MenuItem94: TMenuItem;
MenuItem95: TMenuItem;
MenuItem96: TMenuItem;
MenuItem97: TMenuItem;
MenuItem98: TMenuItem;
MenuItem99: TMenuItem;
PopupMenu1: TPopupMenu;
PrintDialog1: TPrintDialog;
MenuItemSaveOnExit: TMenuItem;
StatusBar1: TStatusBar;
StringGrid1: TStringGrid;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton10: TToolButton;
ToolButton11: TToolButton;
ToolButton12: TToolButton;
ToolButton13: TToolButton;
ToolButton15: TToolButton;
ToolButton16: TToolButton;
ToolButton17: TToolButton;
ToolButton18: TToolButton;
ToolButton19: TToolButton;
ToolButton2: TToolButton;
ToolButton20: TToolButton;
ToolButton21: TToolButton;
ToolButton22: TToolButton;
ToolButton23: TToolButton;
ToolButton24: TToolButton;
ToolButton25: TToolButton;
ToolButton26: TToolButton;
ToolButton27: TToolButton;
ToolButton28: TToolButton;
ToolButton29: TToolButton;
ToolButton3: TToolButton;
ToolButton30: TToolButton;
ToolButton31: TToolButton;
ToolButton32: TToolButton;
ToolButton33: TToolButton;
ToolButton34: TToolButton;
ToolButton35: TToolButton;
ToolButton36: TToolButton;
ToolButton37: TToolButton;
ToolButton38: TToolButton;
ToolButton4: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
ToolButton7: TToolButton;
ToolButton8: TToolButton;
ToolButton9: TToolButton;
procedure MenuItemSearchOnlineClick(Sender: TObject);
procedure MenuItemFontClick(Sender: TObject);
procedure MenuItemDonateClick(Sender: TObject);
procedure FindDialog1Find(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormDropFiles(Sender: TObject; const FileNames: array of String);
procedure FormShow(Sender: TObject);
procedure MenuItemOpenCSVClick(Sender: TObject);
procedure MenuItemSaveClick(Sender: TObject);
procedure MenuItemSaveAsClick(Sender: TObject);
procedure MenuItemExportClick(Sender: TObject);
procedure MenuItemQuitClick(Sender: TObject);
procedure MenuItemAutoresizeClick(Sender: TObject);
procedure MenuItemToggleLabelClick(Sender: TObject);
procedure MenuItemMoveRightToLeftColClick(Sender: TObject);
procedure MenuItemMoveLeftToRightColClick(Sender: TObject);
procedure MenuItemMoveEndColClick(Sender: TObject);
procedure MenuItemMoveRightColClick(Sender: TObject);
procedure MenuItemMoveLeftColClick(Sender: TObject);
procedure MenuItemMoveStartColClick(Sender: TObject);
procedure MenuItemBottomToTopRowClick(Sender: TObject);
procedure MenuItemTopToBottomRowClick(Sender: TObject);
procedure MenuItemMoveBottomRowClick(Sender: TObject);
procedure MenuItemMoveDownRowClick(Sender: TObject);
procedure MenuItemMoveUpRowClick(Sender: TObject);
procedure MenuItemMoveTopRowClick(Sender: TObject);
procedure MenuItemDuplicateColClick(Sender: TObject);
procedure MenuItemSwapRowsClick(Sender: TObject);
procedure MenuItemAddColClick(Sender: TObject);
procedure MenuItemRemoveColsClick(Sender: TObject);
procedure MenuItemAddRowClick(Sender: TObject);
procedure MenuItemRemoveRowsClick(Sender: TObject);
procedure MenuItemToggleToolBarClick(Sender: TObject);
procedure MenuItemWebpageClick(Sender: TObject);
procedure MenuItemSwapColumnsClick(Sender: TObject);
procedure MenuItemPrintClick(Sender: TObject);
procedure MenuItemTakeSnapshotClick(Sender: TObject);
procedure MenuItemCopyCellClick(Sender: TObject);
procedure MenuItemPasteCellClick(Sender: TObject);
procedure MenuItemSearchClick(Sender: TObject);
procedure MenuItemDuplicateRowClick(Sender: TObject);
procedure MenuItemColorLine1Click(Sender: TObject);
procedure MenuItemColorLine2Click(Sender: TObject);
procedure MenuItemRestoreDefaultsClick(Sender: TObject);
procedure MenuItemColorLabelClick(Sender: TObject);
procedure MenuItemSortColumnClick(Sender: TObject);
procedure MenuItemDonate2Click(Sender: TObject);
procedure MenuItemSaveOnExitClick(Sender: TObject);
procedure MenuItemToggleStatusBarClick(Sender: TObject);
procedure MenuItemAboutClick(Sender: TObject);
procedure MenuItemNewClick(Sender: TObject);
procedure StringGrid1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState);
procedure ToolButtonSortDecreaseClick(Sender: TObject);
private
{ private declarations }
procedure LoadStringGrid(csv:string);
procedure Sg2Csv(sep:char;ff:string);
procedure Sg2Xml(F:string);
procedure PrintGrid(var sGrid : TStringGrid);
procedure Sg2Ods(FilenameODS:string);
procedure Sg2Xls(FilenameXLS:string);
public
{ public declarations }
end;
var
FormMain: TFormMain;
tlista : TStringList;
FileName : string;
//Default values :
sep : char = ',';
line1 : Tcolor = $00eeeeee;
line2 : Tcolor = clwhite;
labelc : Tcolor = clBtnFace;
sgfontcolor : Tcolor = clblack;
sgfontsize : integer = 10;
sgfontname : TFontName = 'Arial';
sgfontstyle : TFontStyles = []; //fsBold,fsItalic,fsUnderline,fsStrikeOut
eid,sid,lid : integer;
filterAutoRecognition : string = 'Automatic recognition';
filterXml : string = 'Xml format';
filterHtml : string = 'Html documents';
filterOds : string = 'Open document spreadsheet';
filterCSV : string = 'Comma-separated file';
filterCSVsemicolon : string = 'Semicolon-separated file';
filterCSVpipe : string = 'Pipe-separated file';
filterCSVasterisk : string = 'Asterisk-separated file';
filterCSVcolon : string = 'Colon-separated file';
filterCSVdollar : string = 'Dollar sign-separated file';
filterCSVtab : string = 'Tab-separated file';
filterXls : string = 'Excel format 8.0';
s_true :boolean = false;
const
conf = 'settings.ini';
version = '1.0';
implementation
{$R *.lfm}
//By TrustFm
procedure TFormMain.Sg2Ods(FilenameODS:string);
const OUTPUT_FORMAT = sfOpenDocument;
var i,a : integer;
MyWorkbook: TsWorkbook;
MyWorksheet: TsWorksheet;
begin
// Create the spreadsheet
MyWorkbook := TsWorkbook.Create;
MyWorksheet := MyWorkbook.AddWorksheet('Worksheet');
for i:=0 to stringgrid1.RowCount -1 do begin
for a:=0 to stringgrid1.ColCount -1 do begin
MyWorksheet.WriteUTF8Text(i,a,stringgrid1.Cells[a,i]);
end;
end;
// Save the spreadsheet to a file
MyWorkbook.WriteToFile(FilenameODS, OUTPUT_FORMAT);
MyWorkbook.Free;
end;
//By TrustFm
procedure TFormMain.Sg2Xls(FilenameXLS:string);
const OUTPUT_FORMAT = sfExcel8;
var i,a : integer;
MyWorkbook: TsWorkbook;
MyWorksheet: TsWorksheet;
begin
// Create the spreadsheet
MyWorkbook := TsWorkbook.Create;
MyWorksheet := MyWorkbook.AddWorksheet('Worksheet');
for i:=0 to stringgrid1.RowCount -1 do begin
for a:=0 to stringgrid1.ColCount -1 do begin
MyWorksheet.WriteUTF8Text(i,a,stringgrid1.Cells[a,i]);
end;
end;
// Save the spreadsheet to a file
MyWorkbook.WriteToFile(FilenameXLS, OUTPUT_FORMAT);
MyWorkbook.Free;
end;
//By TrustFm
procedure GoToURL(URL:string);
var
v: THTMLBrowserHelpViewer;
p: LongInt;
BrowserProcess: TProcessUTF8;
BrowserPath, BrowserParams : string;
begin
v:=THTMLBrowserHelpViewer.Create(nil);
try
v.FindDefaultBrowser(BrowserPath,BrowserParams);
p:=System.Pos('%s', BrowserParams);
System.Delete(BrowserParams,p,2);
System.Insert(URL,BrowserParams,p);
BrowserProcess:=TProcessUTF8.Create(nil);
try
BrowserProcess.CommandLine:=BrowserPath+' '+BrowserParams;
BrowserProcess.Execute;
finally
BrowserProcess.Free;
end;
finally
v.Free;
end;
end;
//main functions
//http://www.swissdelphicenter.ch/torry/showcode.php?id=2149
function FontStyletoStr(St: TFontStyles): string;
var
S: string;
begin
S := '';
if St = [fsbold] then S := 'Bold'
else if St = [fsItalic] then S := 'Italic'
else if St = [fsStrikeOut] then S := 'StrikeOut'
else if St = [fsUnderline] then S := 'UnderLine'
else if St = [fsbold, fsItalic] then S := 'BoldItalic'
else if St = [fsBold, fsStrikeOut] then S := 'BoldStrike'
else if St = [fsBold, fsUnderline] then S := 'BoldUnderLine'
else if St = [fsBold..fsStrikeOut] then S := 'BoldItalicStrike'
else if St = [fsBold..fsUnderLine] then S := 'BoldItalicUnderLine'
else if St = [fsbold..fsItalic, fsStrikeOut] then S := 'BoldItalicStrike'
else if St = [fsBold, fsStrikeOut..fsUnderline] then S := 'BoldStrikeUnderLine'
else if St = [fsItalic, fsStrikeOut] then S := 'ItalicStrike'
else if St = [fsItalic..fsUnderline] then S := 'ItalicUnderLine'
else if St = [fsStrikeOut..fsUnderLine] then S := 'StrikeUnderLine'
else if St = [fsItalic..fsStrikeOut] then S := 'ItalicUnderLineStrike';
FontStyletoStr := S;
end;
(*----------------------------------------------------*)
function StrtoFontStyle(St: string): TFontStyles;
var
S: TfontStyles;
begin
S := [];
St := UpperCase(St);
if St = 'BOLD' then S := [fsBold]
else if St = 'ITALIC' then S := [fsItalic]
else if St = 'STRIKEOUT' then S := [fsStrikeOut]
else if St = 'UNDERLINE' then S := [fsUnderLine]
else if St = 'BOLDITALIC' then S := [fsbold, fsItalic]
else if St = 'BOLDSTRIKE' then S := [fsBold, fsStrikeOut]
else if St = 'BOLDUNDERLINE' then S := [fsBold, fsUnderLine]
else if St = 'BOLDITALICSTRIKE' then S := [fsBold..fsStrikeOut]
else if St = 'BOLDITALICUNDERLINE' then S := [fsBold..fsUnderLine]
else if St = 'BOLDITALICSTRIKE' then S := [fsbold..fsItalic, fsStrikeOut]
else if St = 'BOLDSTRIKEUNDERLINE' then S := [fsBold, fsStrikeOut..fsUnderline]
else if St = 'ITALICSTRIKE' then S := [fsItalic, fsStrikeOut]
else if St = 'ITALICUNDERLINE' then S := [fsItalic..fsUnderline]
else if St = 'STRIKEUNDERLINE' then S := [fsStrikeOut..fsUnderLine]
else if St = 'ITALICUNDERLINESTRIKE' then S := [fsItalic..fsStrikeOut];
StrtoFontStyle := S;
end;
//By TrustFm
function GetSelectedRange(IsColumn:boolean; Grid : TStringGrid):TRange;
var res : TRange;
begin
if IsColumn=false then begin
res.Min := grid.Selection.Top;
res.Max := grid.Selection.Bottom;
end else begin //is column
res.Min := grid.Selection.Left;
res.Max := grid.Selection.Right;
end;
result := res;
end;
//By TrustFm
procedure Swap(IsColumn:boolean; Grid : TStringGrid);
var res : TRange;
begin
res := GetSelectedRange(IsColumn, Grid);
if IsColumn=true then begin
if res.Max-res.Min>=1 then begin
Grid.MoveColRow(IsColumn,res.Max, res.Min);
Grid.MoveColRow(IsColumn,res.Min+1, res.Max);
end else begin
showmessage('Select a range of columns')
end;
end else begin //is row
if res.Max-res.Min>=1 then begin
Grid.MoveColRow(IsColumn,res.Max, res.Min);
Grid.MoveColRow(IsColumn,res.Min+1, res.Max);
end else begin
showmessage('Select a range of rows')
end;
end;
end;
//By TrustFm
procedure Duplicate(IsColumn:boolean; Grid : TStringGrid);
var i : integer;
begin
if IsColumn then begin
Grid.InsertColRow(IsColumn,Grid.Col+1); //add an empty column
for i:=0 to Grid.RowCount-1 do begin
Grid.Cells[Grid.Col+1,i]:=Grid.Cells[Grid.Col,i];
end;
end else begin //is row
Grid.InsertColRow(IsColumn,Grid.Row+1); //add an empty row
for i:=0 to Grid.ColCount-1 do begin
Grid.Cells[i,Grid.Row+1]:=Grid.Cells[i,Grid.Row];
end;
end;
end;
function elvalaszto(const elsosor: string): char; // auto separator
var
sepa : array [0..6] of char;
tmpmax,max,i:integer;
c:char;
begin
max := 0;
tmpmax := 0;
sepa[0] := ',';
sepa[1] := ';';
sepa[2] := '*';
sepa[3] := '|';
sepa[4] := ':';
sepa[5] := '$';
sepa[6] := #9;
for i:=0 to high(sepa) do begin
tmpmax := Length(elsosor) - Length(StringReplace(elsosor, sepa[i], '', [rfReplaceAll, rfIgnoreCase]));
if max < tmpmax then begin
max := tmpmax;
c := sepa[i];
end;
end;
Result := c;
end;
//Modded by TrustFm
procedure Sortgrid(Increment:boolean; Grid : TStringGrid; SortCol:integer);
{A simple exchange sort of grid rows}
var
i,j : integer;
temp:tstringlist;
begin
temp:=tstringlist.create;
with Grid do
if Increment = true then begin
for i := FixedRows to RowCount - 2 do {because last row has no next row}
for j:= i+1 to rowcount-1 do {from next row to end}
if CompareText(Cells[SortCol, i], Cells[SortCol,j]) > 0 then begin
temp.assign(rows[j]);
rows[j].assign(rows[i]);
rows[i].assign(temp);
end;
end else begin //decremental
for i := FixedRows to RowCount - 2 do {because last row has no next row}
for j:= i+1 to rowcount-1 do {from next row to end}
if CompareText(Cells[SortCol, i], Cells[SortCol,j]) < 0 then begin
temp.assign(rows[j]);
rows[j].assign(rows[i]);
rows[i].assign(temp);
end;
end; //for
temp.free;
end;
function htmlcolor(szin: TColor): string;
var
a,b,tmp :string;
begin
tmp := ColorToString(szin);
if tmp[1] = '$' then begin
delete(tmp,1,3);
a := tmp[1] + tmp[2];
b := tmp[5] + tmp[6];
delete(tmp,1,2);
delete(tmp,3,4);
tmp := '#'+b+tmp+a;
end;
if tmp[1]+tmp[2] = 'cl' then begin
tmp := stringreplace(tmp,'cl','',[rfReplaceAll]);
end;
result := strlower(pchar(tmp));
end;
procedure SGridToHtml(SG: TStringGrid; filenamex:string);
var
i, p: integer;
Text,wh: string;
Dest :tstringlist;
begin
Dest := tstringlist.Create;
Dest.Add('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
Dest.Add('<html>');
Dest.Add('<head>');
Dest.Add('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');
Dest.Add('<title>CSVpad v'+version+' export: ' + ExtractFileName(FileName) + '</title>');
Dest.Add('<style type="text/css">');
Dest.Add('body,table{font-family: Verdana, Arial, Helvetica, sans-serif; font-size: ' + inttostr(FormMain.StringGrid1.Font.Size) + 'pt; color: '+htmlcolor(FormMain.StringGrid1.Font.Color)+';}');
Dest.Add('a,a:hover {text-decoration: underline; color: '+htmlcolor(sgfontcolor)+';}');
Dest.Add('.center {margin: auto;text-align: left; background: '+htmlcolor(FormMain.Color)+'; border: 1px solid #eee;}');
Dest.Add('.bg1 {background-color: '+htmlcolor(FormMain.stringgrid1.color)+';}');
Dest.Add('.bg1:hover {background-color: #eee;}');
Dest.Add('.bg2 {background: '+htmlcolor(FormMain.stringgrid1.AlternateColor)+';}');
Dest.Add('.bg2:hover {background-color: #eee;}');
Dest.Add('.fix {background-color: '+htmlcolor(FormMain.stringgrid1.FixedColor)+'}');
Dest.Add('</style>');
Dest.Add('</head>');
Dest.Add('<body>');
Dest.Add('<div style="text-align: center; font-size:'+inttostr(FormMain.StringGrid1.Font.Size + 1) +'pt;"><strong>' + ExtractFileName(FileName) + '</strong></div>');
Dest.Add('<div style="text-align: center;">');
Dest.Add(' <table class="center" cellpadding="1" cellspacing="1">');
for i := 0 to SG.RowCount - 1 do
begin
if odd(i) then begin
Dest.Add(' <tr class="bg2">');
end else begin
if (sg.FixedRows = 1) and (i = 0) then begin
Dest.Add(' <tr class="fix">');
end else begin
Dest.Add(' <tr class="bg1">');
end
end;
for p := 0 to SG.ColCount - 1 do
begin
Text := sg.Cells[p, i];
if Text = '' then wh := '' else wh := ' width: '+inttostr(sg.ColWidths[p]+50)+'px;';
Dest.Add(' <td style="height: 15px;'+wh+'">'+Text+'</td>');
end;
Dest.Add(' </tr>');
end;
Dest.Add(' </table>');
Dest.Add('<br>');
Dest.Add('<br>');
Dest.Add('<div style="text-align: center;">Created using <strong>CSVpad v' + version + '</strong> by: <a href="http://www.trustfm.net/">TrustFm</a>. Based on DMcsvEditor by <a href="http://darhmedia.blogspot.hu/">Darh Media - Tivadar</a></div>');
Dest.Add('</div>');
Dest.Add('</body>');
Dest.Add('</html>');
Dest.Text := (Dest.Text); //UTF8encode not needed
Dest.SaveToFile(filenamex);
Dest.Free;
end;
procedure TFormMain.Sg2Xml(F:string);
var
xml:tstringlist;
i,a:integer;
begin
xml := tstringlist.Create;
{
An Attribute is something that is self-contained, i.e., a color, an ID, a name.
An Element is something that does or could have attributes of its own or contain other elements.
}
xml.Add('<?xml version="1.0" encoding="utf-8" standalone="yes"?>');
xml.Add('<!-- Creator: DMcsvEditor v'+version+' (linux) '+datetostr(now)+' -->');
xml.Add('<grid cols="'+inttostr(stringgrid1.ColCount)+'" rows="'+inttostr(stringgrid1.RowCount)+'">');
for i:=0 to stringgrid1.ColCount - 1 do begin
for a:=0 to stringgrid1.RowCount - 1 do begin
if stringgrid1.Cells[i,a] <> '' then
xml.Add(' <cell col="'+inttostr(i+1)+'" row="'+inttostr(a+1)+'">'+stringgrid1.Cells[i,a]+'</cell>')
else
xml.Add(' <cell col="'+inttostr(i+1)+'" row="'+inttostr(a+1)+'" />');
end;
end;
xml.Add('</grid>');
xml.Text := (xml.Text); //not needed UTF8Encode
xml.SaveToFile(F);
xml.Free;
end;
// CSV fájl feldogozása
procedure TFormMain.LoadStringGrid(csv:string);
var
i,a : integer;
csv1 : TCSVDocument;
begin
csv1 := TCSVDocument.Create;
csv1.Delimiter:= sep;
csv1.LoadFromFile(UTF8ToANSI(csv));
stringgrid1.BeginUpdate;
try
//stringgrid1.Font.CharSet:= 4;
stringgrid1.RowCount := csv1.RowCount;
stringgrid1.ColCount := csv1.ColCount[0];
for i:=0 to csv1.RowCount -1 do begin
for a:=0 to csv1.ColCount[i]-1 do begin
stringgrid1.Cells[a,i] := (StringReplace(csv1.Cells[a,i],#13#10,' ',[rfReplaceAll])); //sysToutf8 not needed
end;
end;
finally
stringgrid1.EndUpdate;
FreeAndNil(csv1);
end;
end;
//Print the grid
procedure TFormMain.PrintGrid(var sGrid : TStringGrid);
var
r,c,x,y: Integer;
begin
Printer.Title := ExtractFileName(FileName);
printer.BeginDoc;
Printer.Canvas.Font.Name := stringgrid1.Font.Name;
Printer.Canvas.Font.Size := stringgrid1.Font.Size;
x:= 10;
//y1 := 50;
for r:=0 to sgrid.RowCount - 1 do begin
if printer.PageHeight <= (x+250) then begin
printer.NewPage;
x := 10;
end;
if r = 0 then begin
if sgrid.FixedRows > 0 then begin
Printer.Canvas.Font.Style := [fsBold];
end;
end else begin
Printer.Canvas.Font.Style := [];
end;
y := 20;
for c := 0 to sgrid.ColCount - 1 do begin
printer.Canvas.TextOut(y,x,sGrid.Cells[c,r]);
y := sgrid.ColWidths[c] * 5+y;
end;
x := x +80;
end;
Printer.EndDoc;
end;
procedure TFormMain.Sg2Csv(sep:char;ff:string);
var i,a : integer;
csv2 : TCSVDocument;
begin
csv2 := TCSVDocument.Create;
csv2.Delimiter:= sep;
csv2.QuoteChar := '"';
for i:=0 to stringgrid1.RowCount -1 do begin
for a:=0 to stringgrid1.ColCount -1 do begin
csv2.Cells[a,i] := stringgrid1.Cells[a,i];
end;
end;
csv2.SaveToFile(ff);
FreeAndNil(csv2);
end;
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
//EVENTS
//on form create
procedure TFormMain.FormCreate(Sender: TObject);
var folder : string;
inif : tinifile;
begin
tlista := tstringlist.Create;
//restore form
folder := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
inif := tinifile.Create(folder +conf);
try
MenuItemSaveOnExit.Checked := inif.ReadBool('Main','SettingsSave',MenuItemSaveOnExit.Checked);
lid := inif.ReadInteger('Main','LID',lid);
sid := inif.ReadInteger('Main','SID',sid);
eid := inif.ReadInteger('Main','EID',eid);
MenuItemAutoResize.Checked := inif.ReadBool('Settings','AutoResize',MenuItemAutoResize.Checked);
MenuItemToggleLabel.Checked := inif.ReadBool('Settings','Label',MenuItemToggleLabel.Checked);
if MenuItemAutoResize.Checked then
MenuItemAutoresizeClick(Sender);
if MenuItemToggleLabel.Checked then
MenuItemToggleLabelClick(Sender);
MenuItem24.Checked := inif.ReadBool('View','ToolBar',MenuItem24.Checked);
toolbar1.Visible := MenuItem24.Checked;
MenuItem4.Checked := inif.ReadBool('View','StatusBar',MenuItem4.Checked);
statusbar1.Visible := MenuItem4.Checked;
stringgrid1.Color:= stringtocolor(inif.ReadString('Settings','Line1',colortostring(line1)));
stringgrid1.AlternateColor:= stringtocolor(inif.ReadString('Settings','Line2',colortostring(line2)));
stringgrid1.Font.Color:= stringtocolor(inif.ReadString('Settings','FontColor',colortostring(sgfontcolor)));
stringgrid1.Font.Size:= strtoint(inif.ReadString('Settings','FontSize',colortostring(sgfontsize)));
stringgrid1.Font.Name:=inif.ReadString('Settings','FontName',sgfontname);
stringgrid1.Font.Style:=StrToFontStyle(inif.ReadString('Settings','FontStyle',FontStyleToStr(sgfontstyle)));
stringgrid1.FixedColor := stringtocolor(inif.ReadString('Settings','labelc',colortostring(labelc)));
stringgrid1.Repaint;
finally
inif.Free;
end;
// INI READ END
end;
procedure TFormMain.FormDestroy(Sender: TObject);
var folder : string;
inif : tinifile;
begin
folder := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
inif := tinifile.Create(folder + conf );
try
if MenuItemSaveOnExit.Checked then begin
inif.WriteBool('Settings','AutoResize',MenuItemAutoResize.Checked);
inif.WriteBool('Settings','Label',MenuItemToggleLabel.Checked);
inif.WriteString('Settings','Line1',ColorToString(stringgrid1.Color));
inif.WriteString('Settings','Line2',ColorToString(stringgrid1.AlternateColor));
inif.WriteString('Settings','FontColor',ColorToString(stringgrid1.Font.Color));
inif.WriteString('Settings','FontSize',inttostr(stringgrid1.Font.Size));
inif.WriteString('Settings','FontName',stringgrid1.Font.Name);
inif.WriteString('Settings','FontStyle',FontStyleToStr(stringgrid1.Font.Style));
inif.WriteString('Settings','labelc',ColorToString(stringgrid1.FixedColor));
inif.WriteBool('View','ToolBar',MenuItem24.Checked);
inif.WriteBool('View','StatusBar',MenuItem4.Checked);
inif.WriteInteger('Main','LID',lid);
inif.WriteInteger('Main','SID',sid);
inif.WriteInteger('Main','EID',eid);
end;
inif.WriteBool('Main','SettingsSave',MenuItemSaveOnExit.Checked);
finally
inif.Free;
end;
tlista.Free;
end;
//On drop files
procedure TFormMain.FormDropFiles(Sender: TObject; const FileNames: array of String);
var
txt1:string;
begin
txt1 := copy(FileNames[0],strlen(pchar(FileNames[0]))-3,strlen(pchar(FileNames[0])));
if (AnsiLowerCase(txt1) <> '.csv') and (AnsiLowerCase(txt1) <> '.tab') and (AnsiLowerCase(txt1) <> '.tsv') and (AnsiLowerCase(txt1) <> '.txt') then begin
application.MessageBox('This is file extension not supported!','Warning',0);
end else begin
Filename := FileNames[0];
tlista.LoadFromFile(FileNames[0]);
if elvalaszto(tlista.Strings[0]) <> '' then begin
sep := elvalaszto(tlista.Strings[0]);
end else begin
sep := ',';
end;
LoadStringGrid(Filename);
Statusbar1.Panels[2].Text := Filename;
if sep = #9 then
Statusbar1.Panels[1].Text := 'Tab'
else
Statusbar1.Panels[1].Text := sep;
end;
end;
//on show form
procedure TFormMain.FormShow(Sender: TObject);
var i:integer;
txt1:string;
parameter : string;
begin
sep := ',';
Statusbar1.Panels[1].Text := sep;
if ParamCount > 0 then begin
for i := 1 to ParamCount do begin
parameter:=parameter+ParamStr(i)+'';
end;
parameter:=''+parameter+'';
if FileExists(parameter) then begin
FileName := parameter;
txt1 := copy(parameter,strlen(pchar(parameter))-3,strlen(pchar(parameter)));
if (AnsiLowerCase(txt1) = '.csv') or
(AnsiLowerCase(txt1) = '.tab') or
(AnsiLowerCase(txt1) = '.tsv') or
(AnsiLowerCase(txt1) = '.txt') then begin
tlista.LoadFromFile(FileName);
if elvalaszto(tlista.Strings[0]) <> '' then begin
sep := elvalaszto(tlista.Strings[0]);
end else begin
sep := ',';
end;
LoadStringGrid(FileName);
Statusbar1.Panels[2].Text := Filename;
if sep = #9 then
Statusbar1.Panels[1].Text := 'Tab'
else
Statusbar1.Panels[1].Text := sep;
end else begin
application.MessageBox('This file extension is not supported!','Warning',0);
end;
end;
end;
end;
procedure TFormMain.StringGrid1Click(Sender: TObject);
begin
s_true := false;
end;
procedure TFormMain.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
begin
Statusbar1.Panels[0].Text := 'X:' + IntToStr(stringgrid1.row+1) +
' Y:' + IntToStr(stringgrid1.Col+1);
with (Sender as TStringGrid) do begin
if s_true then begin
if (arow = stringgrid1.row)and (acol =stringgrid1.col) then begin
Canvas.Brush.Color:= clyellow;
Canvas.FillRect(aRect);
end;
end;
end;
end;
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
//menu : FILE
//new file [clear stringrid]
procedure TFormMain.MenuItemNewClick(Sender: TObject);
var
i: integer;
begin
//sep := ',';
FileName := '';
tlista.Clear;
Statusbar1.Panels[2].Text := '';
//Statusbar1.Panels[1].Text := ',';
for i := 0 to StringGrid1.RowCount - 1 do begin
StringGrid1.Rows[i].Clear();
end;