-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
2053 lines (1716 loc) · 70.5 KB
/
main.c
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
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <uxtheme.h>
#include <locale.h>
#include <tchar.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#define LVS_EX_AUTOSIZECOLUMNS 0x10000000
#define WMU_UPDATE_CACHE WM_USER + 1
#define WMU_UPDATE_GRID WM_USER + 2
#define WMU_UPDATE_RESULTSET WM_USER + 3
#define WMU_UPDATE_FILTER_SIZE WM_USER + 4
#define WMU_SET_HEADER_FILTERS WM_USER + 5
#define WMU_AUTO_COLUMN_SIZE WM_USER + 6
#define WMU_SET_CURRENT_CELL WM_USER + 7
#define WMU_RESET_CACHE WM_USER + 8
#define WMU_SET_FONT WM_USER + 9
#define WMU_SET_THEME WM_USER + 10
#define WMU_HIDE_COLUMN WM_USER + 11
#define WMU_SHOW_COLUMNS WM_USER + 12
#define WMU_SORT_COLUMN WM_USER + 13
#define WMU_HOT_KEYS WM_USER + 14
#define WMU_HOT_CHARS WM_USER + 15
#define IDC_MAIN 100
#define IDC_GRID 101
#define IDC_STATUSBAR 102
#define IDC_HEADER_EDIT 1000
#define IDM_COPY_CELL 5000
#define IDM_COPY_ROWS 5001
#define IDM_COPY_COLUMN 5002
#define IDM_FILTER_ROW 5003
#define IDM_HEADER_ROW 5004
#define IDM_DARK_THEME 5005
#define IDM_HIDE_COLUMN 5008
#define IDM_ANSI 5010
#define IDM_UTF8 5011
#define IDM_UTF16LE 5012
#define IDM_UTF16BE 5013
#define IDM_COMMA 5020
#define IDM_SEMICOLON 5021
#define IDM_VBAR 5022
#define IDM_TAB 5023
#define IDM_COLON 5024
#define IDM_DEFAULT 5030
#define IDM_NO_PARSE 5031
#define IDM_NO_SHOW 5032
#define SB_VERSION 0
#define SB_CODEPAGE 1
#define SB_DELIMITER 2
#define SB_COMMENTS 3
#define SB_ROW_COUNT 4
#define SB_CURRENT_CELL 5
#define SB_AUXILIARY 6
#define MAX_COLUMN_COUNT 128
#define MAX_COLUMN_LENGTH 2000
#define MAX_FILTER_LENGTH 2000
#define DELIMITERS TEXT(",;|\t:")
#define APP_NAME TEXT("csvtab")
#define APP_VERSION TEXT("1.0.7")
#define CP_UTF16LE 1200
#define CP_UTF16BE 1201
#define LCS_FINDFIRST 1
#define LCS_MATCHCASE 2
#define LCS_WHOLEWORDS 4
#define LCS_BACKWARDS 8
typedef struct {
int size;
DWORD PluginInterfaceVersionLow;
DWORD PluginInterfaceVersionHi;
char DefaultIniName[MAX_PATH];
} ListDefaultParamStruct;
static TCHAR iniPath[MAX_PATH] = {0};
void __stdcall ListCloseWindow(HWND hWnd);
LRESULT CALLBACK cbNewMain (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK cbHotKey(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK cbNewHeader(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK cbNewFilterEdit (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
HWND getMainWindow(HWND hWnd);
void setStoredValue(TCHAR* name, int value);
int getStoredValue(TCHAR* name, int defValue);
TCHAR* getStoredString(TCHAR* name, TCHAR* defValue);
int CALLBACK cbEnumTabStopChildren (HWND hWnd, LPARAM lParam);
TCHAR* utf8to16(const char* in);
char* utf16to8(const TCHAR* in);
int detectCodePage(const unsigned char *data, int len);
TCHAR detectDelimiter(const TCHAR *data, BOOL skipComments);
void setClipboardText(const TCHAR* text);
BOOL isEOL(TCHAR c);
BOOL isNumber(TCHAR* val);
BOOL isUtf8(const char * string);
int findString(TCHAR* text, TCHAR* word, BOOL isMatchCase, BOOL isWholeWords);
BOOL hasString (const TCHAR* str, const TCHAR* sub, BOOL isCaseSensitive);
TCHAR* extractUrl(TCHAR* data);
void mergeSort(int indexes[], void* data, int l, int r, BOOL isBackward, BOOL isNums);
int ListView_AddColumn(HWND hListWnd, TCHAR* colName, int fmt);
int Header_GetItemText(HWND hWnd, int i, TCHAR* pszText, int cchTextMax);
void Menu_SetItemState(HMENU hMenu, UINT wID, UINT fState);
BOOL APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
if (ul_reason_for_call == DLL_PROCESS_ATTACH && iniPath[0] == 0) {
TCHAR path[MAX_PATH + 1] = {0};
GetModuleFileName(hModule, path, MAX_PATH);
TCHAR* dot = _tcsrchr(path, TEXT('.'));
_tcsncpy(dot, TEXT(".ini"), 5);
if (_taccess(path, 0) == 0)
_tcscpy(iniPath, path);
}
return TRUE;
}
void __stdcall ListGetDetectString(char* DetectString, int maxlen) {
TCHAR* detectString16 = getStoredString(TEXT("detect-string"), TEXT("MULTIMEDIA & (ext=\"CSV\" | ext=\"TAB\" | ext=\"TSV\")"));
char* detectString8 = utf16to8(detectString16);
snprintf(DetectString, maxlen, detectString8);
free(detectString16);
free(detectString8);
}
void __stdcall ListSetDefaultParams(ListDefaultParamStruct* dps) {
if (iniPath[0] == 0) {
DWORD size = MultiByteToWideChar(CP_ACP, 0, dps->DefaultIniName, -1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, dps->DefaultIniName, -1, iniPath, size);
}
}
int __stdcall ListSearchTextW(HWND hWnd, TCHAR* searchString, int searchParameter) {
HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
HWND hStatusWnd = GetDlgItem(hWnd, IDC_STATUSBAR);
TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
int* resultset = (int*)GetProp(hWnd, TEXT("RESULTSET"));
int rowCount = *(int*)GetProp(hWnd, TEXT("ROWCOUNT"));
int colCount = Header_GetItemCount(ListView_GetHeader(hGridWnd));
if (!resultset || rowCount == 0)
return 0;
BOOL isFindFirst = searchParameter & LCS_FINDFIRST;
BOOL isBackward = searchParameter & LCS_BACKWARDS;
BOOL isMatchCase = searchParameter & LCS_MATCHCASE;
BOOL isWholeWords = searchParameter & LCS_WHOLEWORDS;
if (isFindFirst) {
*(int*)GetProp(hWnd, TEXT("CURRENTCOLNO")) = 0;
*(int*)GetProp(hWnd, TEXT("SEARCHCELLPOS")) = 0;
*(int*)GetProp(hWnd, TEXT("CURRENTROWNO")) = isBackward ? rowCount - 1 : 0;
}
int rowNo = *(int*)GetProp(hWnd, TEXT("CURRENTROWNO"));
int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
int *pStartPos = (int*)GetProp(hWnd, TEXT("SEARCHCELLPOS"));
rowNo = rowNo == -1 || rowNo >= rowCount ? 0 : rowNo;
colNo = colNo == -1 || colNo >= colCount ? 0 : colNo;
int pos = -1;
do {
for (; (pos == -1) && colNo < colCount; colNo++) {
pos = findString(cache[resultset[rowNo]][colNo] + *pStartPos, searchString, isMatchCase, isWholeWords);
if (pos != -1)
pos += *pStartPos;
*pStartPos = pos == -1 ? 0 : pos + _tcslen(searchString);
}
colNo = pos != -1 ? colNo - 1 : 0;
rowNo += pos != -1 ? 0 : isBackward ? -1 : 1;
} while ((pos == -1) && (isBackward ? rowNo > 0 : rowNo < rowCount));
ListView_SetItemState(hGridWnd, -1, 0, LVIS_SELECTED | LVIS_FOCUSED);
TCHAR buf[256] = {0};
if (pos != -1) {
ListView_EnsureVisible(hGridWnd, rowNo, FALSE);
ListView_SetItemState(hGridWnd, rowNo, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
TCHAR* val = cache[resultset[rowNo]][colNo];
int len = _tcslen(searchString);
_sntprintf(buf, 255, TEXT("%ls%.*ls%ls"),
pos > 0 ? TEXT("...") : TEXT(""),
len + pos + 10, val + pos,
_tcslen(val + pos + len) > 10 ? TEXT("...") : TEXT(""));
SendMessage(hWnd, WMU_SET_CURRENT_CELL, rowNo, colNo);
} else {
MessageBox(hWnd, searchString, TEXT("Not found:"), MB_OK);
}
SendMessage(hStatusWnd, SB_SETTEXT, SB_AUXILIARY, (LPARAM)buf);
SetFocus(hGridWnd);
return 0;
}
int __stdcall ListSearchText(HWND hWnd, char* searchString, int searchParameter) {
DWORD len = MultiByteToWideChar(CP_ACP, 0, searchString, -1, NULL, 0);
TCHAR* searchString16 = (TCHAR*)calloc (len, sizeof (TCHAR));
MultiByteToWideChar(CP_ACP, 0, searchString, -1, searchString16, len);
int rc = ListSearchTextW(hWnd, searchString16, searchParameter);
free(searchString16);
return rc;
}
HWND APIENTRY ListLoadW (HWND hListerWnd, TCHAR* fileToLoad, int showFlags) {
int size = _tcslen(fileToLoad);
TCHAR* filepath = calloc(size + 1, sizeof(TCHAR));
_tcsncpy(filepath, fileToLoad, size);
int maxFileSize = getStoredValue(TEXT("max-file-size"), 10000000);
struct _stat st = {0};
if (_tstat(filepath, &st) != 0 || st.st_size == 0 || maxFileSize > 0 && st.st_size > maxFileSize)
return 0;
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);
setlocale(LC_CTYPE, "");
BOOL isStandalone = GetParent(hListerWnd) == HWND_DESKTOP;
HWND hMainWnd = CreateWindow(WC_STATIC, APP_NAME, WS_CHILD | (isStandalone ? SS_SUNKEN : 0),
0, 0, 100, 100, hListerWnd, (HMENU)IDC_MAIN, GetModuleHandle(0), NULL);
SetProp(hMainWnd, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hMainWnd, GWLP_WNDPROC, (LONG_PTR)&cbNewMain));
SetProp(hMainWnd, TEXT("FILEPATH"), filepath);
SetProp(hMainWnd, TEXT("FILESIZE"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("DELIMITER"), calloc(1, sizeof(TCHAR)));
SetProp(hMainWnd, TEXT("CODEPAGE"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("FILTERROW"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("HEADERROW"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("SKIPCOMMENTS"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("CACHE"), 0);
SetProp(hMainWnd, TEXT("RESULTSET"), 0);
SetProp(hMainWnd, TEXT("ORDERBY"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("COLCOUNT"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("ROWCOUNT"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("TOTALROWCOUNT"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("CURRENTROWNO"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("CURRENTCOLNO"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("SEARCHCELLPOS"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("FONT"), 0);
SetProp(hMainWnd, TEXT("FONTFAMILY"), getStoredString(TEXT("font"), TEXT("Arial")));
SetProp(hMainWnd, TEXT("FONTSIZE"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("FILTERALIGN"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("DARKTHEME"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("TEXTCOLOR"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("BACKCOLOR"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("BACKCOLOR2"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("FILTERTEXTCOLOR"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("FILTERBACKCOLOR"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("CURRENTCELLCOLOR"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("SELECTIONTEXTCOLOR"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("SELECTIONBACKCOLOR"), calloc(1, sizeof(int)));
*(int*)GetProp(hMainWnd, TEXT("FILESIZE")) = st.st_size;
*(int*)GetProp(hMainWnd, TEXT("CODEPAGE")) = -1;
*(int*)GetProp(hMainWnd, TEXT("FONTSIZE")) = getStoredValue(TEXT("font-size"), 16);
*(int*)GetProp(hMainWnd, TEXT("FILTERROW")) = getStoredValue(TEXT("filter-row"), 1);
*(int*)GetProp(hMainWnd, TEXT("HEADERROW")) = getStoredValue(TEXT("header-row"), 1);
*(int*)GetProp(hMainWnd, TEXT("DARKTHEME")) = getStoredValue(TEXT("dark-theme"), 0);
*(int*)GetProp(hMainWnd, TEXT("SKIPCOMMENTS")) = getStoredValue(TEXT("skip-comments"), 0);
*(int*)GetProp(hMainWnd, TEXT("FILTERALIGN")) = getStoredValue(TEXT("filter-align"), 0);
HWND hStatusWnd = CreateStatusWindow(WS_CHILD | WS_VISIBLE | SBT_TOOLTIPS | (isStandalone ? SBARS_SIZEGRIP : 0), NULL, hMainWnd, IDC_STATUSBAR);
HDC hDC = GetDC(hMainWnd);
float z = GetDeviceCaps(hDC, LOGPIXELSX) / 96.0; // 96 = 100%, 120 = 125%, 144 = 150%
ReleaseDC(hMainWnd, hDC);
int sizes[7] = {35 * z, 95 * z, 125 * z, 235 * z, 355 * z, 435 * z, -1};
SendMessage(hStatusWnd, SB_SETPARTS, 7, (LPARAM)&sizes);
TCHAR buf[32];
_sntprintf(buf, 32, TEXT(" %ls"), APP_VERSION);
SendMessage(hStatusWnd, SB_SETTEXT, SB_VERSION, (LPARAM)buf);
SendMessage(hStatusWnd, SB_SETTIPTEXT, SB_COMMENTS, (LPARAM)TEXT("How to process lines starting with #. Check Wiki to get details."));
HWND hGridWnd = CreateWindow(WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SHOWSELALWAYS | LVS_OWNERDATA | WS_TABSTOP,
205, 0, 100, 100, hMainWnd, (HMENU)IDC_GRID, GetModuleHandle(0), NULL);
int noLines = getStoredValue(TEXT("disable-grid-lines"), 0);
ListView_SetExtendedListViewStyle(hGridWnd, LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER | (noLines ? 0 : LVS_EX_GRIDLINES) | LVS_EX_LABELTIP | LVS_EX_HEADERDRAGDROP);
SetProp(hGridWnd, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hGridWnd, GWLP_WNDPROC, (LONG_PTR)cbHotKey));
HWND hHeader = ListView_GetHeader(hGridWnd);
SetWindowTheme(hHeader, TEXT(" "), TEXT(" "));
SetProp(hHeader, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hHeader, GWLP_WNDPROC, (LONG_PTR)cbNewHeader));
HMENU hGridMenu = CreatePopupMenu();
AppendMenu(hGridMenu, MF_STRING, IDM_COPY_CELL, TEXT("Copy cell"));
AppendMenu(hGridMenu, MF_STRING, IDM_COPY_ROWS, TEXT("Copy row(s)"));
AppendMenu(hGridMenu, MF_STRING, IDM_COPY_COLUMN, TEXT("Copy column"));
AppendMenu(hGridMenu, MF_STRING, 0, NULL);
AppendMenu(hGridMenu, MF_STRING, IDM_HIDE_COLUMN, TEXT("Hide column"));
AppendMenu(hGridMenu, MF_STRING, 0, NULL);
AppendMenu(hGridMenu, (*(int*)GetProp(hMainWnd, TEXT("FILTERROW")) != 0 ? MF_CHECKED : 0) | MF_STRING, IDM_FILTER_ROW, TEXT("Filters"));
AppendMenu(hGridMenu, (*(int*)GetProp(hMainWnd, TEXT("HEADERROW")) != 0 ? MF_CHECKED : 0) | MF_STRING, IDM_HEADER_ROW, TEXT("Header row"));
AppendMenu(hGridMenu, (*(int*)GetProp(hMainWnd, TEXT("DARKTHEME")) != 0 ? MF_CHECKED : 0) | MF_STRING, IDM_DARK_THEME, TEXT("Dark theme"));
SetProp(hMainWnd, TEXT("GRIDMENU"), hGridMenu);
HMENU hCodepageMenu = CreatePopupMenu();
AppendMenu(hCodepageMenu, MF_STRING, IDM_ANSI, TEXT("ANSI"));
AppendMenu(hCodepageMenu, MF_STRING, IDM_UTF8, TEXT("UTF-8"));
AppendMenu(hCodepageMenu, MF_STRING, IDM_UTF16LE, TEXT("UTF-16LE"));
AppendMenu(hCodepageMenu, MF_STRING, IDM_UTF16BE, TEXT("UTF-16BE"));
SetProp(hMainWnd, TEXT("CODEPAGEMENU"), hCodepageMenu);
HMENU hDelimiterMenu = CreatePopupMenu();
AppendMenu(hDelimiterMenu, MF_STRING, IDM_COMMA, TEXT(","));
AppendMenu(hDelimiterMenu, MF_STRING, IDM_SEMICOLON, TEXT(";"));
AppendMenu(hDelimiterMenu, MF_STRING, IDM_VBAR, TEXT("|"));
AppendMenu(hDelimiterMenu, MF_STRING, IDM_TAB, TEXT("TAB"));
AppendMenu(hDelimiterMenu, MF_STRING, IDM_COLON, TEXT(":"));
SetProp(hMainWnd, TEXT("DELIMITERMENU"), hDelimiterMenu);
HMENU hCommentMenu = CreatePopupMenu();
AppendMenu(hCommentMenu, MF_STRING, IDM_DEFAULT, TEXT("#0 - Parse as usual"));
AppendMenu(hCommentMenu, MF_STRING, IDM_NO_PARSE, TEXT("#1 - Don't parse"));
AppendMenu(hCommentMenu, MF_STRING, IDM_NO_SHOW, TEXT("#2 - Ignore (hide)"));
SetProp(hMainWnd, TEXT("COMMENTMENU"), hCommentMenu);
SendMessage(hMainWnd, WMU_SET_FONT, 0, 0);
SendMessage(hMainWnd, WMU_SET_THEME, 0, 0);
SendMessage(hMainWnd, WMU_UPDATE_CACHE, 0, 0);
ShowWindow(hMainWnd, SW_SHOW);
SetFocus(hGridWnd);
return hMainWnd;
}
HWND APIENTRY ListLoad (HWND hListerWnd, char* fileToLoad, int showFlags) {
DWORD size = MultiByteToWideChar(CP_ACP, 0, fileToLoad, -1, NULL, 0);
TCHAR* fileToLoadW = (TCHAR*)calloc (size, sizeof (TCHAR));
MultiByteToWideChar(CP_ACP, 0, fileToLoad, -1, fileToLoadW, size);
HWND hWnd = ListLoadW(hListerWnd, fileToLoadW, showFlags);
free(fileToLoadW);
return hWnd;
}
void __stdcall ListCloseWindow(HWND hWnd) {
setStoredValue(TEXT("font-size"), *(int*)GetProp(hWnd, TEXT("FONTSIZE")));
setStoredValue(TEXT("filter-row"), *(int*)GetProp(hWnd, TEXT("FILTERROW")));
setStoredValue(TEXT("header-row"), *(int*)GetProp(hWnd, TEXT("HEADERROW")));
setStoredValue(TEXT("dark-theme"), *(int*)GetProp(hWnd, TEXT("DARKTHEME")));
setStoredValue(TEXT("skip-comments"), *(int*)GetProp(hWnd, TEXT("SKIPCOMMENTS")));
SendMessage(hWnd, WMU_RESET_CACHE, 0, 0);
free((TCHAR*)GetProp(hWnd, TEXT("FILEPATH")));
free((int*)GetProp(hWnd, TEXT("FILESIZE")));
free((TCHAR*)GetProp(hWnd, TEXT("DELIMITER")));
free((int*)GetProp(hWnd, TEXT("CODEPAGE")));
free((int*)GetProp(hWnd, TEXT("FILTERROW")));
free((int*)GetProp(hWnd, TEXT("HEADERROW")));
free((int*)GetProp(hWnd, TEXT("DARKTHEME")));
free((int*)GetProp(hWnd, TEXT("ORDERBY")));
free((int*)GetProp(hWnd, TEXT("COLCOUNT")));
free((int*)GetProp(hWnd, TEXT("ROWCOUNT")));
free((int*)GetProp(hWnd, TEXT("TOTALROWCOUNT")));
free((int*)GetProp(hWnd, TEXT("FONTSIZE")));
free((int*)GetProp(hWnd, TEXT("CURRENTROWNO")));
free((int*)GetProp(hWnd, TEXT("CURRENTCOLNO")));
free((int*)GetProp(hWnd, TEXT("SEARCHCELLPOS")));
free((TCHAR*)GetProp(hWnd, TEXT("FONTFAMILY")));
free((int*)GetProp(hWnd, TEXT("FILTERALIGN")));
free((int*)GetProp(hWnd, TEXT("TEXTCOLOR")));
free((int*)GetProp(hWnd, TEXT("BACKCOLOR")));
free((int*)GetProp(hWnd, TEXT("BACKCOLOR2")));
free((int*)GetProp(hWnd, TEXT("FILTERTEXTCOLOR")));
free((int*)GetProp(hWnd, TEXT("FILTERBACKCOLOR")));
free((int*)GetProp(hWnd, TEXT("CURRENTCELLCOLOR")));
free((int*)GetProp(hWnd, TEXT("SELECTIONTEXTCOLOR")));
free((int*)GetProp(hWnd, TEXT("SELECTIONBACKCOLOR")));
DeleteFont(GetProp(hWnd, TEXT("FONT")));
DeleteObject(GetProp(hWnd, TEXT("BACKBRUSH")));
DeleteObject(GetProp(hWnd, TEXT("FILTERBACKBRUSH")));
DestroyMenu(GetProp(hWnd, TEXT("GRIDMENU")));
DestroyMenu(GetProp(hWnd, TEXT("CODEPAGEMENU")));
DestroyMenu(GetProp(hWnd, TEXT("DELIMITERMENU")));
DestroyMenu(GetProp(hWnd, TEXT("COMMENTMENU")));
RemoveProp(hWnd, TEXT("WNDPROC"));
RemoveProp(hWnd, TEXT("CACHE"));
RemoveProp(hWnd, TEXT("RESULTSET"));
RemoveProp(hWnd, TEXT("FILEPATH"));
RemoveProp(hWnd, TEXT("FILESIZE"));
RemoveProp(hWnd, TEXT("DELIMITER"));
RemoveProp(hWnd, TEXT("CODEPAGE"));
RemoveProp(hWnd, TEXT("FILTERROW"));
RemoveProp(hWnd, TEXT("HEADERROW"));
RemoveProp(hWnd, TEXT("DARKTHEME"));
RemoveProp(hWnd, TEXT("ORDERBY"));
RemoveProp(hWnd, TEXT("COLCOUNT"));
RemoveProp(hWnd, TEXT("ROWCOUNT"));
RemoveProp(hWnd, TEXT("TOTALROWCOUNT"));
RemoveProp(hWnd, TEXT("CURRENTROWNO"));
RemoveProp(hWnd, TEXT("CURRENTCOLNO"));
RemoveProp(hWnd, TEXT("SEARCHCELLPOS"));
RemoveProp(hWnd, TEXT("FILTERALIGN"));
RemoveProp(hWnd, TEXT("LASTFOCUS"));
RemoveProp(hWnd, TEXT("TEXTCOLOR"));
RemoveProp(hWnd, TEXT("BACKCOLOR"));
RemoveProp(hWnd, TEXT("BACKCOLOR2"));
RemoveProp(hWnd, TEXT("FILTERTEXTCOLOR"));
RemoveProp(hWnd, TEXT("FILTERBACKCOLOR"));
RemoveProp(hWnd, TEXT("CURRENTCELLCOLOR"));
RemoveProp(hWnd, TEXT("SELECTIONTEXTCOLOR"));
RemoveProp(hWnd, TEXT("SELECTIONBACKCOLOR"));
RemoveProp(hWnd, TEXT("BACKBRUSH"));
RemoveProp(hWnd, TEXT("FILTERBACKBRUSH"));
RemoveProp(hWnd, TEXT("FONT"));
RemoveProp(hWnd, TEXT("FONTFAMILY"));
RemoveProp(hWnd, TEXT("FONTSIZE"));
RemoveProp(hWnd, TEXT("GRIDMENU"));
RemoveProp(hWnd, TEXT("CODEPAGEMENU"));
RemoveProp(hWnd, TEXT("DELIMITERMENU"));
RemoveProp(hWnd, TEXT("COMMENTMENU"));
DestroyWindow(hWnd);
}
LRESULT CALLBACK cbNewMain(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_SIZE: {
HWND hStatusWnd = GetDlgItem(hWnd, IDC_STATUSBAR);
SendMessage(hStatusWnd, WM_SIZE, 0, 0);
RECT rc;
GetClientRect(hStatusWnd, &rc);
int statusH = rc.bottom;
GetClientRect(hWnd, &rc);
HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
SetWindowPos(hGridWnd, 0, 0, 0, rc.right, rc.bottom - statusH, SWP_NOZORDER);
}
break;
// https://groups.google.com/g/comp.os.ms-windows.programmer.win32/c/1XhCKATRXws
case WM_NCHITTEST: {
return 1;
}
break;
case WM_SETCURSOR: {
SetCursor(LoadCursor(0, IDC_ARROW));
return TRUE;
}
break;
case WM_SETFOCUS: {
SetFocus(GetProp(hWnd, TEXT("LASTFOCUS")));
}
break;
case WM_MOUSEWHEEL: {
if (LOWORD(wParam) == MK_CONTROL) {
SendMessage(hWnd, WMU_SET_FONT, GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? 1: -1, 0);
return 1;
}
}
break;
case WM_KEYDOWN: {
if (SendMessage(hWnd, WMU_HOT_KEYS, wParam, lParam))
return 0;
}
break;
case WM_COMMAND: {
WORD cmd = LOWORD(wParam);
if (cmd == IDM_COPY_CELL || cmd == IDM_COPY_ROWS || cmd == IDM_COPY_COLUMN) {
HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
HWND hHeader = ListView_GetHeader(hGridWnd);
int rowNo = *(int*)GetProp(hWnd, TEXT("CURRENTROWNO"));
int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
int colCount = Header_GetItemCount(hHeader);
int rowCount = *(int*)GetProp(hWnd, TEXT("ROWCOUNT"));
int selCount = ListView_GetSelectedCount(hGridWnd);
if (rowNo == -1 ||
rowNo >= rowCount ||
colCount == 0 ||
cmd == IDM_COPY_CELL && colNo == -1 ||
cmd == IDM_COPY_CELL && colNo >= colCount ||
cmd == IDM_COPY_COLUMN && colNo == -1 ||
cmd == IDM_COPY_COLUMN && colNo >= colCount ||
cmd == IDM_COPY_ROWS && selCount == 0) {
setClipboardText(TEXT(""));
return 0;
}
TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
int* resultset = (int*)GetProp(hWnd, TEXT("RESULTSET"));
if (!resultset)
return 0;
TCHAR* delimiter = getStoredString(TEXT("column-delimiter"), TEXT("\t"));
int len = 0;
if (cmd == IDM_COPY_CELL)
len = _tcslen(cache[resultset[rowNo]][colNo]);
if (cmd == IDM_COPY_ROWS) {
int rowNo = ListView_GetNextItem(hGridWnd, -1, LVNI_SELECTED);
while (rowNo != -1) {
for (int colNo = 0; colNo < colCount; colNo++) {
if (ListView_GetColumnWidth(hGridWnd, colNo))
len += _tcslen(cache[resultset[rowNo]][colNo]) + 1; /* column delimiter */
}
len++; /* \n */
rowNo = ListView_GetNextItem(hGridWnd, rowNo, LVNI_SELECTED);
}
}
if (cmd == IDM_COPY_COLUMN) {
int rowNo = selCount < 2 ? 0 : ListView_GetNextItem(hGridWnd, -1, LVNI_SELECTED);
while (rowNo != -1 && rowNo < rowCount) {
len += _tcslen(cache[resultset[rowNo]][colNo]) + 1 /* \n */;
rowNo = selCount < 2 ? rowNo + 1 : ListView_GetNextItem(hGridWnd, rowNo, LVNI_SELECTED);
}
}
TCHAR* buf = calloc(len + 1, sizeof(TCHAR));
if (cmd == IDM_COPY_CELL)
_tcscat(buf, cache[resultset[rowNo]][colNo]);
if (cmd == IDM_COPY_ROWS) {
int pos = 0;
int rowNo = ListView_GetNextItem(hGridWnd, -1, LVNI_SELECTED);
int* colOrder = calloc(colCount, sizeof(int));
Header_GetOrderArray(hHeader, colCount, colOrder);
while (rowNo != -1) {
for (int idx = 0; idx < colCount; idx++) {
int colNo = colOrder[idx];
if (ListView_GetColumnWidth(hGridWnd, colNo)) {
int len = _tcslen(cache[resultset[rowNo]][colNo]);
_tcsncpy(buf + pos, cache[resultset[rowNo]][colNo], len);
buf[pos + len] = delimiter[0];
pos += len + 1;
}
}
buf[pos - (pos > 0)] = TEXT('\n');
rowNo = ListView_GetNextItem(hGridWnd, rowNo, LVNI_SELECTED);
}
buf[pos - 1] = 0; // remove last \n
free(colOrder);
}
if (cmd == IDM_COPY_COLUMN) {
int pos = 0;
int rowNo = selCount < 2 ? 0 : ListView_GetNextItem(hGridWnd, -1, LVNI_SELECTED);
while (rowNo != -1 && rowNo < rowCount) {
int len = _tcslen(cache[resultset[rowNo]][colNo]);
_tcsncpy(buf + pos, cache[resultset[rowNo]][colNo], len);
rowNo = selCount < 2 ? rowNo + 1 : ListView_GetNextItem(hGridWnd, rowNo, LVNI_SELECTED);
if (rowNo != -1 && rowNo < rowCount)
buf[pos + len] = TEXT('\n');
pos += len + 1;
}
}
setClipboardText(buf);
free(buf);
free(delimiter);
}
if (cmd == IDM_HIDE_COLUMN) {
int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
SendMessage(hWnd, WMU_HIDE_COLUMN, colNo, 0);
}
if (cmd == IDM_FILTER_ROW || cmd == IDM_HEADER_ROW || cmd == IDM_DARK_THEME) {
HMENU hMenu = (HMENU)GetProp(hWnd, TEXT("GRIDMENU"));
int* pOpt = (int*)GetProp(hWnd, cmd == IDM_FILTER_ROW ? TEXT("FILTERROW") : cmd == IDM_HEADER_ROW ? TEXT("HEADERROW" : TEXT("DARKTHEME")));
*pOpt = (*pOpt + 1) % 2;
Menu_SetItemState(hMenu, cmd, *pOpt ? MFS_CHECKED : 0);
UINT msg = cmd == IDM_FILTER_ROW ? WMU_SET_HEADER_FILTERS : cmd == IDM_HEADER_ROW ? WMU_UPDATE_GRID : WMU_SET_THEME;
SendMessage(hWnd, msg, 0, 0);
}
if (cmd == IDM_ANSI || cmd == IDM_UTF8 || cmd == IDM_UTF16LE || cmd == IDM_UTF16BE) {
int* pCodepage = (int*)GetProp(hWnd, TEXT("CODEPAGE"));
*pCodepage = cmd == IDM_ANSI ? CP_ACP : cmd == IDM_UTF8 ? CP_UTF8 : cmd == IDM_UTF16LE ? CP_UTF16LE : CP_UTF16BE;
SendMessage(hWnd, WMU_UPDATE_CACHE, 0, 0);
}
if (cmd == IDM_COMMA || cmd == IDM_SEMICOLON || cmd == IDM_VBAR || cmd == IDM_TAB || cmd == IDM_COLON) {
TCHAR* pDelimiter = (TCHAR*)GetProp(hWnd, TEXT("DELIMITER"));
*pDelimiter = cmd == IDM_COMMA ? TEXT(',') :
cmd == IDM_SEMICOLON ? TEXT(';') :
cmd == IDM_TAB ? TEXT('\t') :
cmd == IDM_VBAR ? TEXT('|') :
cmd == IDM_COLON ? TEXT(':') :
0;
SendMessage(hWnd, WMU_UPDATE_CACHE, 0, 0);
}
if (cmd == IDM_DEFAULT || cmd == IDM_NO_PARSE || cmd == IDM_NO_SHOW) {
int* pSkipComments = (int*)GetProp(hWnd, TEXT("SKIPCOMMENTS"));
*pSkipComments = cmd == IDM_DEFAULT ? 0 : cmd == IDM_NO_PARSE ? 1 : 2;
SendMessage(hWnd, WMU_UPDATE_CACHE, 0, 0);
}
}
break;
case WM_NOTIFY : {
NMHDR* pHdr = (LPNMHDR)lParam;
if (pHdr->idFrom == IDC_GRID && pHdr->code == LVN_GETDISPINFO) {
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)lParam;
LV_ITEM* pItem = &(pDispInfo)->item;
TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
int* resultset = (int*)GetProp(hWnd, TEXT("RESULTSET"));
if(resultset && pItem->mask & LVIF_TEXT) {
int rowNo = resultset[pItem->iItem];
pItem->pszText = cache[rowNo][pItem->iSubItem];
}
}
if (pHdr->idFrom == IDC_GRID && pHdr->code == LVN_COLUMNCLICK) {
NMLISTVIEW* lv = (NMLISTVIEW*)lParam;
return SendMessage(hWnd, HIWORD(GetKeyState(VK_CONTROL)) ? WMU_HIDE_COLUMN : WMU_SORT_COLUMN, lv->iSubItem, 0);
}
if (pHdr->idFrom == IDC_GRID && (pHdr->code == (DWORD)NM_CLICK || pHdr->code == (DWORD)NM_RCLICK)) {
NMITEMACTIVATE* ia = (LPNMITEMACTIVATE) lParam;
SendMessage(hWnd, WMU_SET_CURRENT_CELL, ia->iItem, ia->iSubItem);
}
if (pHdr->idFrom == IDC_GRID && pHdr->code == (DWORD)NM_CLICK && HIWORD(GetKeyState(VK_MENU))) {
NMITEMACTIVATE* ia = (LPNMITEMACTIVATE) lParam;
TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
int* resultset = (int*)GetProp(hWnd, TEXT("RESULTSET"));
TCHAR* url = extractUrl(cache[resultset[ia->iItem]][ia->iSubItem]);
ShellExecute(0, TEXT("open"), url, 0, 0 , SW_SHOW);
free(url);
}
if (pHdr->idFrom == IDC_GRID && pHdr->code == (DWORD)NM_RCLICK) {
POINT p;
GetCursorPos(&p);
TrackPopupMenu(GetProp(hWnd, TEXT("GRIDMENU")), TPM_RIGHTBUTTON | TPM_TOPALIGN | TPM_LEFTALIGN, p.x, p.y, 0, hWnd, NULL);
}
if (pHdr->idFrom == IDC_GRID && pHdr->code == (DWORD)LVN_ITEMCHANGED) {
NMLISTVIEW* lv = (NMLISTVIEW*)lParam;
if (lv->uOldState != lv->uNewState && (lv->uNewState & LVIS_SELECTED))
SendMessage(hWnd, WMU_SET_CURRENT_CELL, lv->iItem, *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO")));
}
if (pHdr->idFrom == IDC_GRID && pHdr->code == (DWORD)LVN_KEYDOWN) {
NMLVKEYDOWN* kd = (LPNMLVKEYDOWN) lParam;
if (kd->wVKey == 0x43) { // C
BOOL isCtrl = HIWORD(GetKeyState(VK_CONTROL));
BOOL isShift = HIWORD(GetKeyState(VK_SHIFT));
BOOL isCopyColumn = getStoredValue(TEXT("copy-column"), 0) && ListView_GetSelectedCount(pHdr->hwndFrom) > 1;
if (!isCtrl && !isShift)
return FALSE;
int action = !isShift && !isCopyColumn ? IDM_COPY_CELL : isCtrl || isCopyColumn ? IDM_COPY_COLUMN : IDM_COPY_ROWS;
SendMessage(hWnd, WM_COMMAND, action, 0);
return TRUE;
}
if (kd->wVKey == 0x41 && HIWORD(GetKeyState(VK_CONTROL))) { // Ctrl + A
HWND hGridWnd = pHdr->hwndFrom;
SendMessage(hGridWnd, WM_SETREDRAW, FALSE, 0);
int rowNo = *(int*)GetProp(hWnd, TEXT("CURRENTROWNO"));
int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
ListView_SetItemState(hGridWnd, -1, LVIS_SELECTED, LVIS_SELECTED | LVIS_FOCUSED);
SendMessage(hWnd, WMU_SET_CURRENT_CELL, rowNo, colNo);
SendMessage(hGridWnd, WM_SETREDRAW, TRUE, 0);
InvalidateRect(hGridWnd, NULL, TRUE);
}
if (kd->wVKey == 0x20 && HIWORD(GetKeyState(VK_CONTROL))) { // Ctrl + Space
SendMessage(hWnd, WMU_SHOW_COLUMNS, 0, 0);
return TRUE;
}
if (kd->wVKey == VK_LEFT || kd->wVKey == VK_RIGHT) {
HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
HWND hHeader = ListView_GetHeader(hGridWnd);
int colCount = Header_GetItemCount(ListView_GetHeader(pHdr->hwndFrom));
int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
int* colOrder = calloc(colCount, sizeof(int));
Header_GetOrderArray(hHeader, colCount, colOrder);
int dir = kd->wVKey == VK_RIGHT ? 1 : -1;
int idx = 0;
for (idx; colOrder[idx] != colNo; idx++);
do {
idx = (colCount + idx + dir) % colCount;
} while (ListView_GetColumnWidth(hGridWnd, colOrder[idx]) == 0);
colNo = colOrder[idx];
free(colOrder);
SendMessage(hWnd, WMU_SET_CURRENT_CELL, *(int*)GetProp(hWnd, TEXT("CURRENTROWNO")), colNo);
return TRUE;
}
}
if ((pHdr->code == HDN_ITEMCHANGED || pHdr->code == HDN_ENDDRAG) && pHdr->hwndFrom == ListView_GetHeader(GetDlgItem(hWnd, IDC_GRID)))
PostMessage(hWnd, WMU_UPDATE_FILTER_SIZE, 0, 0);
if (pHdr->idFrom == IDC_STATUSBAR && (pHdr->code == NM_CLICK || pHdr->code == NM_RCLICK)) {
NMMOUSE* pm = (NMMOUSE*)lParam;
int id = pm->dwItemSpec;
if (id != SB_CODEPAGE && id != SB_DELIMITER && id != SB_COMMENTS)
return 0;
RECT rc, rc2;
GetWindowRect(pHdr->hwndFrom, &rc);
SendMessage(pHdr->hwndFrom, SB_GETRECT, id, (LPARAM)&rc2);
HMENU hMenu = GetProp(hWnd, id == SB_CODEPAGE ? TEXT("CODEPAGEMENU") : id == SB_DELIMITER ? TEXT("DELIMITERMENU") : TEXT("COMMENTMENU"));
if (id == SB_CODEPAGE) {
int codepage = *(int*)GetProp(hWnd, TEXT("CODEPAGE"));
Menu_SetItemState(hMenu, IDM_ANSI, codepage == CP_ACP ? MFS_CHECKED : 0);
Menu_SetItemState(hMenu, IDM_UTF8, codepage == CP_UTF8 ? MFS_CHECKED : 0);
Menu_SetItemState(hMenu, IDM_UTF16LE, codepage == CP_UTF16LE ? MFS_CHECKED : 0);
Menu_SetItemState(hMenu, IDM_UTF16BE, codepage == CP_UTF16BE ? MFS_CHECKED : 0);
}
if (id == SB_DELIMITER) {
TCHAR delimiter = *(TCHAR*)GetProp(hWnd, TEXT("DELIMITER"));
Menu_SetItemState(hMenu, IDM_COMMA, delimiter == TEXT(',') ? MFS_CHECKED : 0);
Menu_SetItemState(hMenu, IDM_SEMICOLON, delimiter == TEXT(';') ? MFS_CHECKED : 0);
Menu_SetItemState(hMenu, IDM_VBAR, delimiter == TEXT('|') ? MFS_CHECKED : 0);
Menu_SetItemState(hMenu, IDM_TAB, delimiter == TEXT('\t') ? MFS_CHECKED : 0);
Menu_SetItemState(hMenu, IDM_COLON, delimiter == TEXT(':') ? MFS_CHECKED : 0);
}
if (id == SB_COMMENTS) {
int skipComments = *(int*)GetProp(hWnd, TEXT("SKIPCOMMENTS"));
Menu_SetItemState(hMenu, IDM_DEFAULT, skipComments == 0 ? MFS_CHECKED : 0);
Menu_SetItemState(hMenu, IDM_NO_PARSE, skipComments == 1 ? MFS_CHECKED : 0);
Menu_SetItemState(hMenu, IDM_NO_SHOW, skipComments == 2 ? MFS_CHECKED : 0);
}
POINT p = {rc.left + rc2.left, rc.top};
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_BOTTOMALIGN | TPM_LEFTALIGN, p.x, p.y, 0, hWnd, NULL);
}
if (pHdr->code == (UINT)NM_SETFOCUS)
SetProp(hWnd, TEXT("LASTFOCUS"), pHdr->hwndFrom);
if (pHdr->idFrom == IDC_GRID && pHdr->code == (UINT)NM_CUSTOMDRAW) {
int result = CDRF_DODEFAULT;
NMLVCUSTOMDRAW* pCustomDraw = (LPNMLVCUSTOMDRAW)lParam;
if (pCustomDraw->nmcd.dwDrawStage == CDDS_PREPAINT)
result = CDRF_NOTIFYITEMDRAW;
if (pCustomDraw->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) {
if (ListView_GetItemState(pHdr->hwndFrom, pCustomDraw->nmcd.dwItemSpec, LVIS_SELECTED)) {
pCustomDraw->nmcd.uItemState &= ~CDIS_SELECTED;
result = CDRF_NOTIFYSUBITEMDRAW;
} else {
pCustomDraw->clrTextBk = *(int*)GetProp(hWnd, pCustomDraw->nmcd.dwItemSpec % 2 == 0 ? TEXT("BACKCOLOR") : TEXT("BACKCOLOR2"));
}
}
if (pCustomDraw->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) {
int rowNo = *(int*)GetProp(hWnd, TEXT("CURRENTROWNO"));
int colNo = *(int*)GetProp(hWnd, TEXT("CURRENTCOLNO"));
BOOL isCurrCell = (pCustomDraw->nmcd.dwItemSpec == (DWORD)rowNo) && (pCustomDraw->iSubItem == colNo);
pCustomDraw->clrText = *(int*)GetProp(hWnd, TEXT("SELECTIONTEXTCOLOR"));
pCustomDraw->clrTextBk = *(int*)GetProp(hWnd, isCurrCell ? TEXT("CURRENTCELLCOLOR") : TEXT("SELECTIONBACKCOLOR"));
}
return result;
}
}
break;
// wParam = colNo
case WMU_HIDE_COLUMN: {
HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
HWND hHeader = ListView_GetHeader(hGridWnd);
int colNo = (int)wParam;
HWND hEdit = GetDlgItem(hHeader, IDC_HEADER_EDIT + colNo);
SetWindowLongPtr(hEdit, GWLP_USERDATA, (LONG_PTR)ListView_GetColumnWidth(hGridWnd, colNo));
ListView_SetColumnWidth(hGridWnd, colNo, 0);
InvalidateRect(hHeader, NULL, TRUE);
}
break;
case WMU_SHOW_COLUMNS: {
HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
HWND hHeader = ListView_GetHeader(hGridWnd);
int colCount = Header_GetItemCount(ListView_GetHeader(hGridWnd));
for (int colNo = 0; colNo < colCount; colNo++) {
if (ListView_GetColumnWidth(hGridWnd, colNo) == 0) {
HWND hEdit = GetDlgItem(hHeader, IDC_HEADER_EDIT + colNo);
ListView_SetColumnWidth(hGridWnd, colNo, (int)GetWindowLongPtr(hEdit, GWLP_USERDATA));
}
}
InvalidateRect(hGridWnd, NULL, TRUE);
}
break;
// wParam = colNo
case WMU_SORT_COLUMN: {
int colNo = (int)wParam + 1;
if (colNo <= 0)
return FALSE;
int* pOrderBy = (int*)GetProp(hWnd, TEXT("ORDERBY"));
int orderBy = *pOrderBy;
*pOrderBy = colNo == orderBy || colNo == -orderBy ? -orderBy : colNo;
SendMessage(hWnd, WMU_UPDATE_RESULTSET, 0, 0);
}
break;
case WMU_UPDATE_CACHE: {
TCHAR* filepath = (TCHAR*)GetProp(hWnd, TEXT("FILEPATH"));
int filesize = *(int*)GetProp(hWnd, TEXT("FILESIZE"));
TCHAR delimiter = *(TCHAR*)GetProp(hWnd, TEXT("DELIMITER"));
int codepage = *(int*)GetProp(hWnd, TEXT("CODEPAGE"));
int skipComments = *(int*)GetProp(hWnd, TEXT("SKIPCOMMENTS"));
SendMessage(hWnd, WMU_RESET_CACHE, 0, 0);
char* rawdata = calloc(filesize + 2, sizeof(char)); // + 2!
FILE *f = _tfopen(filepath, TEXT("rb"));
fread(rawdata, sizeof(char), filesize, f);
fclose(f);
rawdata[filesize] = 0;
rawdata[filesize + 1] = 0;
int leadZeros = 0;
for (int i = 0; leadZeros == i && i < filesize; i++)
leadZeros += rawdata[i] == 0;
if (leadZeros == filesize) {
PostMessage(GetParent(hWnd), WM_KEYDOWN, 0x33, 0x20001); // Switch to Hex-mode
// A special TC-command doesn't work under TC x32.
// https://flint-inc.ru/tcinfo/all_cmd.ru.htm#Misc
// PostMessage(GetAncestor(hWnd, GA_ROOT), WM_USER + 51, 4005, 0);
keybd_event(VK_TAB, VK_TAB, KEYEVENTF_EXTENDEDKEY, 0);
return FALSE;
}
if (codepage == -1)
codepage = detectCodePage(rawdata, filesize);
// Fix unexpected zeros
if (codepage == CP_UTF16BE || codepage == CP_UTF16LE) {
for (int i = 0; i < filesize / 2; i++) {
if (rawdata[2 * i] == 0 && rawdata[2 * i + 1] == 0)
rawdata[2 * i + codepage == CP_UTF16LE] = ' ';
}
}
if (codepage == CP_UTF8 || codepage == CP_ACP) {
for (int i = 0; i < filesize; i++) {
if (rawdata[i] == 0)
rawdata[i] = ' ';
}
}
// end fix
TCHAR* data = 0;
if (codepage == CP_UTF16BE) {
for (int i = 0; i < filesize/2; i++) {
int c = rawdata[2 * i];
rawdata[2 * i] = rawdata[2 * i + 1];
rawdata[2 * i + 1] = c;
}
}
if (codepage == CP_UTF16LE || codepage == CP_UTF16BE)
data = (TCHAR*)(rawdata + leadZeros);
if (codepage == CP_UTF8) {
data = utf8to16(rawdata + leadZeros);
free(rawdata);
}
if (codepage == CP_ACP) {
DWORD len = MultiByteToWideChar(CP_ACP, 0, rawdata, -1, NULL, 0);
data = (TCHAR*)calloc (len, sizeof (TCHAR));
if (!MultiByteToWideChar(CP_ACP, 0, rawdata + leadZeros, -1, data, len))
codepage = -1;
free(rawdata);
}
if (codepage == -1) {
MessageBox(hWnd, TEXT("Can't detect codepage"), NULL, MB_OK);
return 0;
}
if (!delimiter) {
TCHAR* str = getStoredString(TEXT("default-column-delimiter"), TEXT(""));
delimiter = _tcslen(str) ? str[0] : detectDelimiter(data, skipComments);
free(str);
}
int colCount = 1;
int rowNo = -1;
int len = _tcslen(data);
int cacheSize = len / 100 + 1;
TCHAR*** cache = calloc(cacheSize, sizeof(TCHAR**));
BOOL isTrim = getStoredValue(TEXT("trim-values"), 1);
// Two step parsing: 0 - count columns, 1 - fill cache
for (int stepNo = 0; stepNo < 2; stepNo++) {
rowNo = -1;
int start = 0;
for(int pos = 0; pos < len; pos++) {
rowNo++;
if (stepNo == 1) {
if (rowNo >= cacheSize) {
cacheSize += 100;
cache = realloc(cache, cacheSize * sizeof(TCHAR**));
}
cache[rowNo] = (TCHAR**)calloc (colCount, sizeof (TCHAR*));
}
int colNo = 0;
start = pos;
for (; pos < len && colNo < MAX_COLUMN_COUNT; pos++) {
TCHAR c = data[pos];
while (start == pos && (stepNo == 0 && skipComments == 1 || skipComments == 2) && c == TEXT('#')) {
while (data[pos] && !isEOL(data[pos]))
pos++;
while (pos < len && isEOL(data[pos]))
pos++;
c = data[pos];
start = pos;
}
if (c == delimiter && !(skipComments && data[start] == TEXT('#')) || isEOL(c) || pos >= len - 1) {
int vLen = pos - start + (pos >= len - 1);
int qPos = -1;
for (int i = 0; i < vLen; i++) {
TCHAR c = data[start + i];
if (c != TEXT(' ') && c != TEXT('\t')) {
qPos = c == TEXT('"') ? i : -1;
break;
}
}
if (qPos != -1) {
int qCount = 0;
for (int i = qPos; i <= vLen; i++)
qCount += data[start + i] == TEXT('"');
if (pos < len && qCount % 2)
continue;
while(vLen > 0 && data[start + vLen] != TEXT('"'))
vLen--;
vLen -= qPos + 1;
}
if (stepNo == 1) {
if (isTrim) {
int l = 0, r = 0;
if (qPos == -1) {
for (int i = 0; i < vLen && (data[start + i] == TEXT(' ') || data[start + i] == TEXT('\t')); i++)
l++;
}
for (int i = vLen - 1; i > 0 && (data[start + i] == TEXT(' ') || data[start + i] == TEXT('\t')); i--)
r++;
start += l;