-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
3203 lines (2666 loc) · 110 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
// The code is based on https://github.com/little-brother/sqlite-wlx
#define UNICODE
#define _UNICODE
#define _WIN32_WINNT 0x0600
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <uxtheme.h>
#include <locale.h>
#include <tchar.h>
#include <shlwapi.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include "include/sqlite3.h"
#define LVS_EX_AUTOSIZECOLUMNS 0x10000000
#define WMU_UPDATE_GRID WM_USER + 1
#define WMU_UPDATE_DATA WM_USER + 2
#define WMU_UPDATE_ROW_COUNT 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 WMU_EDIT_CELL WM_USER + 20
#define WMU_UPDATE_COUNTS WM_USER + 22
#define WMU_AUTOCOMPLETE WM_USER + 23
#define WMU_SET_SCROLL_HEIGHT WM_USER + 24
#define WMU_UPDATE_SCROLL_POS WM_USER + 25
#define IDC_MAIN 100
#define IDC_TABLELIST 101
#define IDC_GRID 102
#define IDC_STATUSBAR 103
#define IDC_CELL_EDIT 104
#define IDC_BUTTON 105
#define IDC_HEADER_EDIT 1000
#define IDC_DLG_GRID 2000
#define IDC_DLG_OK 2001
#define IDC_DLG_COLUMNS 2002
#define IDC_DLG_EDIT 2200
#define IDC_DLG_LABEL 2500
#define IDM_COPY_CELL 5000
#define IDM_COPY_ROWS 5001
#define IDM_COPY_COLUMN 5002
#define IDM_FILTER_ROW 5003
#define IDM_DARK_THEME 5004
#define IDM_HIDE_COLUMN 5010
#define IDM_DROP 5015
#define IDM_BLOB 5020
#define IDM_ADD_ROW 5021
#define IDM_DELETE_ROW 5022
#define IDM_SEPARATOR1 5030
#define IDM_SEPARATOR2 5031
#define IDM_SAVE_AS 5032
#define IDM_OPEN_DB 5130
#define IDM_EXIT 5131
#define IDM_RECENT 5140
#define IDM_ABOUT 5165
#define IDM_HOMEPAGE 5166
#define IDM_WIKI 5167
#define IDI_ICON 6000
#define SB_VERSION 0
#define SB_TABLE_COUNT 1
#define SB_VIEW_COUNT 2
#define SB_TYPE 3
#define SB_ROW_COUNT 4
#define SB_CURRENT_CELL 5
#define SB_AUXILIARY 6
#define SPLITTER_WIDTH 5
#define MAX_TEXT_LENGTH 32000
#define MAX_COLUMN_LENGTH 2000
#define MAX_TABLE_LENGTH 2000
#define BLOB_VALUE "(BLOB)"
#define MAX_RECENT_FILES 10
#define APP_NAME TEXT("sqlite-x")
#define APP_VERSION TEXT("1.0.5")
#ifdef __MINGW64__
#define APP_PLATFORM 64
#else
#define APP_PLATFORM 32
#endif
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
static TCHAR iniPath[MAX_PATH] = {0};
struct DLGITEMTEMPLATEEX {
DLGITEMTEMPLATE;
WORD menu;
WORD windowClass;
};
LRESULT CALLBACK cbMainWnd(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND ListLoadW (HWND hParentWnd, TCHAR* fileToLoad, int showFlags);
void __stdcall ListCloseWindow(HWND hWnd);
LRESULT CALLBACK cbNewMain (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK cbNewTableList(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK cbNewDataGrid(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);
BOOL cbNewAutoCompleteEdit(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK cbNewCellEdit(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK cbNewAutoComplete(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK cbNewColumns(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK cbNewAddRowEdit(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK cbDlgAddRow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK cbDlgAddTable(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK cbEnumSetFont (HWND hWnd, LPARAM hFont);
void updateRecentList(HWND hWnd);
void bindValue(sqlite3_stmt* stmt, int i, const char* value8);
void bindFilterValues(HWND hHeader, sqlite3_stmt* stmt);
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 findString(TCHAR* text, TCHAR* word, BOOL isMatchCase, BOOL isWholeWords);
TCHAR* extractUrl(TCHAR* data);
void setClipboardText(const TCHAR* text);
int getFontHeight(HWND hWnd);
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 saveFile(TCHAR* path, const TCHAR* filter, const TCHAR* defExt, HWND hWnd);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc = {0};
wc.lpfnWndProc = cbMainWnd;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.lpszClassName = TEXT("sqlite-x-class");
wc.hbrBackground = (HBRUSH) GetSysColorBrush(COLOR_APPWORKSPACE);
wc.style = CS_DBLCLKS;
RegisterClass(&wc);
HMENU hFileMenu = CreatePopupMenu();
AppendMenu(hFileMenu, MF_STRING, IDM_OPEN_DB, TEXT("Open..."));
AppendMenu(hFileMenu, MF_STRING, IDM_SEPARATOR1, NULL);
AppendMenu(hFileMenu, MF_STRING, IDM_EXIT, TEXT("Exit"));
HMENU hAboutMenu = CreatePopupMenu();
AppendMenu(hAboutMenu, MF_STRING, IDM_ABOUT, TEXT("About"));
AppendMenu(hAboutMenu, MF_STRING, IDM_WIKI, TEXT("Wiki"));
AppendMenu(hAboutMenu, MF_STRING, IDM_HOMEPAGE, TEXT("Homepage"));
HMENU hMainMenu = CreateMenu();
AppendMenu(hMainMenu, MF_STRING | MF_POPUP, (UINT_PTR)hFileMenu, TEXT("Database"));
AppendMenu(hMainMenu, MF_STRING | MF_POPUP, (UINT_PTR)hAboutMenu, TEXT("?"));
ModifyMenu(hMainMenu, 1, MF_BYPOSITION | MFT_RIGHTJUSTIFY, 0, TEXT("?"));
HWND hWnd = CreateWindowEx(0, TEXT("sqlite-x-class"), TEXT("sqlite-x - No database selected"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMainMenu, hInstance, NULL);
if (hWnd == NULL)
return 0;
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);
setlocale(LC_CTYPE, "");
if (getStoredValue(TEXT("dpi-aware"), 0))
SetProcessDPIAware();
TCHAR buf16[MAX_PATH];
GetModuleFileName(0, buf16, MAX_PATH);
PathRemoveFileSpec(buf16);
_sntprintf(iniPath, MAX_PATH, TEXT("%ls/%ls.ini"), buf16, APP_NAME);
int nArgs = 0;
TCHAR** args = CommandLineToArgvW(GetCommandLine(), &nArgs);
if (nArgs > 1) {
TCHAR path16[MAX_PATH + 1] = {0};
_tcsncpy(path16, args[1], MAX_PATH);
if (!ListLoadW (hWnd, path16, 0))
return 0;
}
HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
SetWindowPos(hWnd, 0,
getStoredValue(TEXT("position-x"), 200),
getStoredValue(TEXT("position-y"), 200),
getStoredValue(TEXT("width"), 800),
getStoredValue(TEXT("height"), 600),
SWP_NOZORDER);
ShowWindow(hWnd, getStoredValue(TEXT("show-maximized"), 0) ? SW_MAXIMIZE : nCmdShow);
updateRecentList(hWnd);
if (nArgs < 2 && getStoredValue(TEXT("open-last-db"), 1))
SendMessage(hWnd, WM_COMMAND, MAKEWPARAM(IDM_RECENT, 0), 0);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK cbMainWnd(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CLOSE: {
HWND hMainWnd = GetDlgItem(hWnd, IDC_MAIN);
if (hMainWnd)
ListCloseWindow(hMainWnd);
if (IsWindowVisible(hWnd)) {
WINDOWPLACEMENT wp = {};
wp.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hWnd, &wp);
if (wp.showCmd == SW_SHOWMAXIMIZED) {
setStoredValue(TEXT("show-maximized"), 1);
} else {
RECT rc;
GetWindowRect(hWnd, &rc);
setStoredValue(TEXT("position-x"), rc.left);
setStoredValue(TEXT("position-y"), rc.top);
setStoredValue(TEXT("width"), rc.right - rc.left);
setStoredValue(TEXT("height"), rc.bottom - rc.top);
setStoredValue(TEXT("show-maximized"), 0);
}
}
PostQuitMessage(0);
return 0;
}
break;
case WM_SIZE: {
RECT rc;
GetClientRect(hWnd, &rc);
HWND hMainWnd = GetDlgItem(hWnd, IDC_MAIN);
if (hMainWnd)
SetWindowPos(hMainWnd, 0, 0, 0, rc.right, rc.bottom, SWP_NOMOVE | SWP_NOZORDER);
}
break;
case WM_KEYDOWN: {
if (wParam == VK_ESCAPE && getStoredValue(TEXT("exit-by-escape"), 1)) {
SendMessage(hWnd, WM_DESTROY, 0, 0);
return TRUE;
}
}
break;
case WM_COMMAND: {
WORD cmd = LOWORD(wParam);
if (cmd == IDM_ABOUT) {
OSVERSIONINFO vi = {0};
vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
NTSTATUS (WINAPI *pRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation) = NULL;
HINSTANCE hNTdllDll = LoadLibrary(TEXT("ntdll.dll"));
if (hNTdllDll != NULL) {
pRtlGetVersion = (NTSTATUS (WINAPI *)(PRTL_OSVERSIONINFOW))
GetProcAddress (hNTdllDll, "RtlGetVersion");
if (pRtlGetVersion != NULL)
pRtlGetVersion((PRTL_OSVERSIONINFOW)&vi);
FreeLibrary(hNTdllDll);
}
if (pRtlGetVersion == NULL)
GetVersionEx(&vi);
BOOL isWin64 = FALSE;
#if defined(_WIN64)
isWin64 = TRUE;
#else
isWin64 = IsWow64Process(GetCurrentProcess(), &isWin64) && isWin64;
#endif
TCHAR buf[1024];
_sntprintf(buf, 1023, TEXT("GUI version: %ls\nArchitecture: %ls\nSQLite version: %hs\nBuild date: %ls\n\nOS: %i.%i.%i %ls %ls"),
APP_VERSION,
APP_PLATFORM == 32 ? TEXT("x86") : TEXT("x86-64"),
sqlite3_libversion(),
TEXT(__DATE__),
vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber, vi.szCSDVersion,
isWin64 ? TEXT("x64") : TEXT("x32")
);
MessageBox(hWnd, buf, APP_NAME, MB_OK);
}
if (cmd == IDM_HOMEPAGE)
ShellExecute(0, 0, TEXT("https://github.com/little-brother/sqlite-x"), 0, 0 , SW_SHOW);
if (cmd == IDM_WIKI)
ShellExecute(0, 0, TEXT("https://github.com/little-brother/sqlite-x/wiki"), 0, 0 , SW_SHOW);
if (cmd == IDM_OPEN_DB) {
TCHAR path16[MAX_PATH];
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = path16;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = TEXT("Databases (*.sqlite, *.sqlite3, *.db, *.db3)\0*.sqlite;*.sqlite3;*.db;*.db3\0All\0*.*\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
if (!GetOpenFileName(&ofn))
return 0;
TCHAR ext16[32] = {0};
_tsplitpath(path16, NULL, NULL, NULL, ext16);
if (_tcslen(ext16) == 0)
_tcscat(path16, TEXT(".sqlite"));
ListLoadW (hWnd, path16, 0);
}
if (cmd == IDM_EXIT) {
SendMessage(hWnd, WM_CLOSE, 0, 0);
}
if (cmd >= IDM_RECENT && cmd < IDM_RECENT + MAX_RECENT_FILES) {
TCHAR path16[MAX_PATH + 1];
if (GetMenuString(GetSubMenu(GetMenu(hWnd), 0), cmd, path16, MAX_PATH, MF_BYCOMMAND))
ListLoadW (hWnd, path16, 0);
}
}
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void updateRecentList(HWND hWnd) {
HMENU hMenu = GetSubMenu(GetMenu(hWnd), 0);
for (int i = 0; i < MAX_RECENT_FILES; i++)
DeleteMenu(hMenu, IDM_RECENT + i, MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_SEPARATOR2, MF_BYCOMMAND);
TCHAR* buf16 = calloc(20 * MAX_PATH, sizeof(TCHAR));
if (GetPrivateProfileString(APP_NAME, TEXT("recent"), NULL, buf16, 20 * MAX_PATH, iniPath)) {
int i = 0;
TCHAR* path16 = _tcstok(buf16, TEXT("?"));
while (path16 != NULL && i < MAX_RECENT_FILES) {
if (_taccess(path16, 0) == 0) {
if (i == 0)
InsertMenu(hMenu, IDM_EXIT, MF_BYCOMMAND | MF_STRING, IDM_SEPARATOR2, NULL);
InsertMenu(hMenu, IDM_SEPARATOR2, MF_BYCOMMAND | MF_STRING, IDM_RECENT + i, path16);
i++;
}
path16 = _tcstok(NULL, TEXT("?"));
}
}
free(buf16);
}
HWND ListLoadW (HWND hParentWnd, TCHAR* fileToLoad, int showFlags) {
int len = _tcslen(fileToLoad) + 10;
TCHAR* journal = calloc(len + 1, sizeof(TCHAR));
_sntprintf(journal, len, TEXT("%ls-wal"), fileToLoad);
BOOL hasJournal = _taccess(journal, 0) == 0;
free(journal);
char* fileToLoad8 = utf16to8(fileToLoad);
BOOL isEditable = getStoredValue(TEXT("editable"), 1);
sqlite3 *db = 0;
if (SQLITE_OK != sqlite3_open_v2(fileToLoad8, &db, (isEditable ? (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) : SQLITE_OPEN_READONLY) | SQLITE_OPEN_URI, NULL)) {
MessageBox(hParentWnd, TEXT("Error to open database"), fileToLoad, MB_OK);
free(fileToLoad8);
return NULL;
}
free(fileToLoad8);
HWND hMainWnd = GetDlgItem(hParentWnd, IDC_MAIN);
if (hMainWnd) {
ListCloseWindow(hMainWnd);
DestroyWindow(hMainWnd);
}
BOOL isStandalone = GetParent(hParentWnd) == HWND_DESKTOP;
hMainWnd = CreateWindow(WC_STATIC, APP_NAME, WS_CHILD | (isStandalone ? SS_SUNKEN : 0),
0, 0, 100, 100, hParentWnd, (HMENU)IDC_MAIN, GetModuleHandle(0), NULL);
int cacheSize = getStoredValue(TEXT("cache-size"), 2000);
SetProp(hMainWnd, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hMainWnd, GWLP_WNDPROC, (LONG_PTR)&cbNewMain));
SetProp(hMainWnd, TEXT("FILTERROW"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("CACHE"), calloc(cacheSize, sizeof(TCHAR*)));
SetProp(hMainWnd, TEXT("CACHESIZE"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("DB"), (HANDLE)db);
SetProp(hMainWnd, TEXT("HASJOURNAL"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("ORDERBY"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("CACHEOFFSET"), calloc(1, sizeof(int)));
SetProp(hMainWnd, TEXT("TABLENAME8"), calloc(MAX_TABLE_LENGTH, sizeof(char)));
SetProp(hMainWnd, TEXT("WHERE8"), calloc(MAX_TEXT_LENGTH, sizeof(char)));
SetProp(hMainWnd, TEXT("HASROWID"), 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("SPLITTERPOSITION"), 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)));
SetProp(hMainWnd, TEXT("SPLITTERCOLOR"), calloc(1, sizeof(int)));
*(int*)GetProp(hMainWnd, TEXT("HASJOURNAL")) = hasJournal;
*(int*)GetProp(hMainWnd, TEXT("SPLITTERPOSITION")) = getStoredValue(TEXT("splitter-position"), 200);
*(int*)GetProp(hMainWnd, TEXT("FONTSIZE")) = getStoredValue(TEXT("font-size"), 16);
*(int*)GetProp(hMainWnd, TEXT("CACHESIZE")) = cacheSize;
*(int*)GetProp(hMainWnd, TEXT("CACHEOFFSET")) = -1;
*(int*)GetProp(hMainWnd, TEXT("FILTERROW")) = getStoredValue(TEXT("filter-row"), 0);
*(int*)GetProp(hMainWnd, TEXT("DARKTHEME")) = getStoredValue(TEXT("dark-theme"), 0);
*(int*)GetProp(hMainWnd, TEXT("FILTERALIGN")) = getStoredValue(TEXT("filter-align"), 0);
HWND hStatusWnd = CreateStatusWindow(WS_CHILD | WS_VISIBLE | (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, 110 * z, 180 * z, 225 * z, 420 * z, 500 * z, -1};
SendMessage(hStatusWnd, SB_SETPARTS, 7, (LPARAM)&sizes);
CreateWindow(WC_BUTTON, TEXT("Add table"), WS_CHILD | WS_VISIBLE,
0, 0, 100, 100, hMainWnd, (HMENU)IDC_BUTTON, GetModuleHandle(0), NULL);
HWND hListWnd = CreateWindow(WC_LISTBOX, NULL, WS_CHILD | WS_VISIBLE | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP | WS_HSCROLL,
0, 20, 100, 100, hMainWnd, (HMENU)IDC_TABLELIST, GetModuleHandle(0), NULL);
SetProp(hListWnd, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hListWnd, GWLP_WNDPROC, (LONG_PTR)cbNewTableList));
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"), 1);
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)cbNewDataGrid));
HWND hAutoComplete = CreateWindowEx(WS_EX_TOPMOST, WC_LISTBOX, NULL, WS_POPUP | WS_BORDER | WS_VSCROLL, 0, 0, 170, 200, hMainWnd, (HMENU)0, GetModuleHandle(0), NULL);
SetProp(hAutoComplete, TEXT("CURRENTINPUT"), 0);
SetProp(hAutoComplete, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hAutoComplete, GWLP_WNDPROC, (LONG_PTR)&cbNewAutoComplete));
SetProp(hMainWnd, TEXT("AUTOCOMPLETE"), hAutoComplete);
HWND hHeader = ListView_GetHeader(hGridWnd);
LONG_PTR styles = GetWindowLongPtr(hHeader, GWL_STYLE);
SetWindowLongPtr(hHeader, GWL_STYLE, styles | HDS_FILTERBAR);
SetWindowTheme(hHeader, TEXT(" "), TEXT(" "));
SetProp(hHeader, TEXT("WNDPROC"), (HANDLE)SetWindowLongPtr(hHeader, GWLP_WNDPROC, (LONG_PTR)cbNewHeader));
BOOL isCopyColumn = getStoredValue(TEXT("copy-column"), 0);
HMENU hGridMenu = CreatePopupMenu();
AppendMenu(hGridMenu, MF_STRING, IDM_COPY_CELL, !isCopyColumn ? TEXT("Copy cell\tCtrl+C") : TEXT("Copy cell"));
AppendMenu(hGridMenu, MF_STRING, IDM_COPY_ROWS, !isCopyColumn ? TEXT("Copy row(s)\tShift+C") : TEXT("Copy row(s)"));
AppendMenu(hGridMenu, MF_STRING, IDM_COPY_COLUMN, !isCopyColumn ? TEXT("Copy column\tCtrl+Shift+C") : TEXT("Copy column"));
AppendMenu(hGridMenu, MF_STRING, IDM_SAVE_AS, TEXT("Save as ...\tCtrl+S"));
AppendMenu(hGridMenu, MF_STRING, IDM_SEPARATOR2, NULL);
AppendMenu(hGridMenu, MF_STRING, IDM_HIDE_COLUMN, TEXT("Hide column\tCtrl+H"));
AppendMenu(hGridMenu, MF_STRING, 0, NULL);
AppendMenu(hGridMenu, (*(int*)GetProp(hMainWnd, TEXT("FILTERROW")) != 0 ? MF_CHECKED : 0) | MF_STRING, IDM_FILTER_ROW, TEXT("Filters\tCtrl+F"));
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 hListMenu = CreatePopupMenu();
AppendMenu(hListMenu, MF_STRING, IDM_DROP, TEXT("Delete"));
SetProp(hMainWnd, TEXT("LISTMENU"), hListMenu);
HMENU hBlobMenu = CreatePopupMenu();
AppendMenu(hBlobMenu, MF_STRING, IDM_BLOB, TEXT("Save to file"));
SetProp(hMainWnd, TEXT("BLOBMENU"), hBlobMenu);
sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, getStoredValue(TEXT("hide-service-tables"), 0) ?
"select name, type from sqlite_master where type in ('table', 'view') and instr(name, '__') != 1 and instr(name, '$') != 1 order by type, name" :
"select name, type from sqlite_master where type in ('table', 'view') order by type, name",
-1, &stmt, 0);
while(SQLITE_ROW == sqlite3_step(stmt)) {
TCHAR* name16 = utf8to16((char*)sqlite3_column_text(stmt, 0));
int pos = ListBox_AddString(hListWnd, name16);
free(name16);
BOOL isTable = strcmp((char*)sqlite3_column_text(stmt, 1), "table") == 0;
SendMessage(hListWnd, LB_SETITEMDATA, pos, isTable);
}
sqlite3_finalize(stmt);
if (getStoredValue(TEXT("enable-foreign-keys"), 0))
sqlite3_exec(db, "pragma foreign_keys = on", 0, 0, 0);
TCHAR buf[255];
_sntprintf(buf, 32, TEXT(" %ls"), APP_VERSION);
SendMessage(hStatusWnd, SB_SETTEXT, SB_VERSION, (LPARAM)buf);
SendMessage(hMainWnd, WMU_UPDATE_COUNTS, 0, 0);
SendMessage(hMainWnd, WMU_SET_FONT, 0, 0);
SendMessage(hMainWnd, WMU_SET_THEME, 0, 0);
ListBox_SetCurSel(hListWnd, 0);
SendMessage(hMainWnd, WMU_UPDATE_GRID, 0, 0);
ShowWindow(hMainWnd, SW_SHOW);
SetFocus(hListWnd);
SetWindowText(hParentWnd, fileToLoad);
SendMessage(hParentWnd, WM_SIZE, 0, 0);
TCHAR* paths16 = calloc(20 * MAX_PATH, sizeof(TCHAR));
_tcscat(paths16, fileToLoad);
_tcscat(paths16, TEXT("?"));
TCHAR* buf16 = calloc(20 * MAX_PATH, sizeof(TCHAR));
if (GetPrivateProfileString(APP_NAME, TEXT("recent"), NULL, buf16, 20 * MAX_PATH, iniPath)) {
int i = 0;
TCHAR* path16 = _tcstok(buf16, TEXT("?"));
while (path16 != NULL && i < MAX_RECENT_FILES) {
TCHAR tmp16[MAX_PATH + 1] = {0};
_tcscat(tmp16, path16);
_tcscat(tmp16, TEXT("?"));
if (_tcsstr(paths16, tmp16) == 0 && _taccess(path16, 0) == 0) {
_tcscat(paths16, tmp16);
i++;
}
path16 = _tcstok(NULL, TEXT("?"));
}
}
WritePrivateProfileString(APP_NAME, TEXT("recent"), paths16, iniPath);
free(buf16);
free(paths16);
updateRecentList(hParentWnd);
return hMainWnd;
}
void __stdcall ListCloseWindow(HWND hWnd) {
setStoredValue(TEXT("splitter-position"), *(int*)GetProp(hWnd, TEXT("SPLITTERPOSITION")));
setStoredValue(TEXT("font-size"), *(int*)GetProp(hWnd, TEXT("FONTSIZE")));
setStoredValue(TEXT("filter-row"), *(int*)GetProp(hWnd, TEXT("FILTERROW")));
setStoredValue(TEXT("dark-theme"), *(int*)GetProp(hWnd, TEXT("DARKTHEME")));
SendMessage(hWnd, WMU_RESET_CACHE, 0, 0);
sqlite3* db = (sqlite3*)GetProp(hWnd, TEXT("DB"));
BOOL hasJournal = *(int*)GetProp(hWnd, TEXT("HASJOURNAL"));
int deleteMode = getStoredValue(TEXT("delete-journal"), 0);
TCHAR* dbpath = utf8to16(sqlite3_db_filename(db, 0));
sqlite3_close(db);
if (!hasJournal && deleteMode) {
int len = _tcslen(dbpath) + 10;
TCHAR* journal = calloc(len + 1, sizeof(TCHAR));
_sntprintf(journal, len, TEXT("%ls-wal"), dbpath);
if (_taccess(journal, 0) == 0 && (deleteMode == 2 || (deleteMode == 1 && IDYES == MessageBox(hWnd, TEXT("Delete journal files?"), TEXT("Confirmation"), MB_YESNO | MB_ICONQUESTION)))) {
DeleteFile(journal);
_sntprintf(journal, len, TEXT("%ls-shm"), dbpath);
DeleteFile(journal);
}
free(journal);
}
free(dbpath);
free((int*)GetProp(hWnd, TEXT("HASJOURNAL")));
free((int*)GetProp(hWnd, TEXT("FILTERROW")));
free((int*)GetProp(hWnd, TEXT("DARKTHEME")));
free((TCHAR***)GetProp(hWnd, TEXT("CACHE")));
free((TCHAR***)GetProp(hWnd, TEXT("CACHESIZE")));
free((int*)GetProp(hWnd, TEXT("ORDERBY")));
free((int*)GetProp(hWnd, TEXT("CACHEOFFSET")));
free((char*)GetProp(hWnd, TEXT("TABLENAME8")));
free((char*)GetProp(hWnd, TEXT("WHERE8")));
free((int*)GetProp(hWnd, TEXT("HASROWID")));
free((int*)GetProp(hWnd, TEXT("ROWCOUNT")));
free((int*)GetProp(hWnd, TEXT("TOTALROWCOUNT")));
free((int*)GetProp(hWnd, TEXT("SPLITTERPOSITION")));
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")));
free((int*)GetProp(hWnd, TEXT("SPLITTERCOLOR")));
DeleteFont(GetProp(hWnd, TEXT("FONT")));
DeleteObject(GetProp(hWnd, TEXT("BACKBRUSH")));
DeleteObject(GetProp(hWnd, TEXT("FILTERBACKBRUSH")));
DeleteObject(GetProp(hWnd, TEXT("SPLITTERBRUSH")));
DestroyMenu(GetProp(hWnd, TEXT("GRIDMENU")));
DestroyMenu(GetProp(hWnd, TEXT("LISTMENU")));
DestroyMenu(GetProp(hWnd, TEXT("BLOBMENU")));
RemoveProp(hWnd, TEXT("CACHESIZE"));
RemoveProp(hWnd, TEXT("FILTERROW"));
RemoveProp(hWnd, TEXT("DARKTHEME"));
RemoveProp(hWnd, TEXT("WNDPROC"));
RemoveProp(hWnd, TEXT("DB"));
RemoveProp(hWnd, TEXT("AUTOCOMPLETE"));
RemoveProp(hWnd, TEXT("HASJOURNAL"));
RemoveProp(hWnd, TEXT("ORDERBY"));
RemoveProp(hWnd, TEXT("CACHEOFFSET"));
RemoveProp(hWnd, TEXT("TABLENAME8"));
RemoveProp(hWnd, TEXT("WHERE8"));
RemoveProp(hWnd, TEXT("HASROWID"));
RemoveProp(hWnd, TEXT("ROWCOUNT"));
RemoveProp(hWnd, TEXT("TOTALROWCOUNT"));
RemoveProp(hWnd, TEXT("SPLITTERPOSITION"));
RemoveProp(hWnd, TEXT("CURRENTROWNO"));
RemoveProp(hWnd, TEXT("CURRENTCOLNO"));
RemoveProp(hWnd, TEXT("SEARCHCELLPOS"));
RemoveProp(hWnd, TEXT("FILTERALIGN"));
RemoveProp(hWnd, TEXT("LASTFOCUS"));
RemoveProp(hWnd, TEXT("FONT"));
RemoveProp(hWnd, TEXT("FONTFAMILY"));
RemoveProp(hWnd, TEXT("FONTSIZE"));
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("SPLITTERCOLOR"));
RemoveProp(hWnd, TEXT("BACKBRUSH"));
RemoveProp(hWnd, TEXT("FILTERBACKBRUSH"));
RemoveProp(hWnd, TEXT("SPLITTERBRUSH"));
RemoveProp(hWnd, TEXT("GRIDMENU"));
RemoveProp(hWnd, TEXT("LISTMENU"));
RemoveProp(hWnd, TEXT("BLOBMENU"));
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;
int splitterW = *(int*)GetProp(hWnd, TEXT("SPLITTERPOSITION"));
GetClientRect(hWnd, &rc);
HWND hBtnWnd = GetDlgItem(hWnd, IDC_BUTTON);
HWND hListWnd = GetDlgItem(hWnd, IDC_TABLELIST);
HWND hGridWnd = GetDlgItem(hWnd, IDC_GRID);
int h = 0;
if (getStoredValue(TEXT("editable"), 1) == 1) {
HWND hHeader = ListView_GetHeader(hGridWnd);
BOOL isFilterRow = *(int*)GetProp(hWnd, TEXT("FILTERROW")) != 0;
BOOL hasColumns = Header_GetItemCount(hHeader) > 0;
LONG_PTR styles = 0;
if (!hasColumns) {
styles = GetWindowLongPtr(hHeader, GWL_STYLE);
SetWindowLongPtr(hHeader, GWL_STYLE, isFilterRow ? styles | HDS_FILTERBAR : styles & (~HDS_FILTERBAR));
ListView_AddColumn(hGridWnd, TEXT("Temp"), LVCFMT_LEFT);
}
RECT rcHeader;
Header_GetItemRect(hHeader, 0, &rcHeader);
h = rcHeader.bottom - rcHeader.top;
if (isFilterRow)
h = round(h/2);
if (!hasColumns) {
SetWindowLongPtr(hHeader, GWL_STYLE, styles);
ListView_DeleteColumn(hGridWnd, 0);
}
}
SetWindowPos(hBtnWnd, 0, 0, 0, splitterW, h, SWP_NOMOVE | SWP_NOZORDER);
SetWindowPos(hListWnd, 0, 0, h, splitterW, rc.bottom - statusH - h, SWP_NOZORDER);
SetWindowPos(hGridWnd, 0, splitterW + SPLITTER_WIDTH, 0, rc.right - splitterW - SPLITTER_WIDTH, rc.bottom - statusH, SWP_NOZORDER);
}
break;
case WM_PAINT: {
PAINTSTRUCT ps = {0};
HDC hDC = BeginPaint(hWnd, &ps);
RECT rc;
GetClientRect(hWnd, &rc);
rc.left = *(int*)GetProp(hWnd, TEXT("SPLITTERPOSITION"));
rc.right = rc.left + SPLITTER_WIDTH;
FillRect(hDC, &rc, (HBRUSH)GetProp(hWnd, TEXT("SPLITTERBRUSH")));
EndPaint(hWnd, &ps);
return 0;
}
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, GetProp(hWnd, TEXT("ISMOUSEHOVER")) ? IDC_SIZEWE : IDC_ARROW));
return TRUE;
}
break;
case WM_SETFOCUS: {
SetFocus(GetProp(hWnd, TEXT("LASTFOCUS")));
}
break;
case WM_LBUTTONDOWN: {
int x = GET_X_LPARAM(lParam);
int pos = *(int*)GetProp(hWnd, TEXT("SPLITTERPOSITION"));
if (x >= pos && x <= pos + SPLITTER_WIDTH) {
SetProp(hWnd, TEXT("ISMOUSEDOWN"), (HANDLE)1);
SetCapture(hWnd);
}
return 0;
}
break;
case WM_LBUTTONUP: {
ReleaseCapture();
RemoveProp(hWnd, TEXT("ISMOUSEDOWN"));
}
break;
case WM_MOUSEMOVE: {
DWORD x = GET_X_LPARAM(lParam);
int* pPos = (int*)GetProp(hWnd, TEXT("SPLITTERPOSITION"));
if (!GetProp(hWnd, TEXT("ISMOUSEHOVER")) && *pPos <= x && x <= *pPos + SPLITTER_WIDTH) {
TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT), TME_LEAVE, hWnd, 0};
TrackMouseEvent(&tme);
SetProp(hWnd, TEXT("ISMOUSEHOVER"), (HANDLE)1);
}
if (GetProp(hWnd, TEXT("ISMOUSEDOWN")) && x > 0 && x < 32000) {
*pPos = x;
SendMessage(hWnd, WM_SIZE, 0, 0);
}
}
break;
case WM_MOUSELEAVE: {
SetProp(hWnd, TEXT("ISMOUSEHOVER"), 0);
}
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_CONTEXTMENU: {
if ((HWND)wParam == GetDlgItem(hWnd, IDC_TABLELIST)) {
HWND hListWnd = (HWND)wParam;
POINT p;
GetCursorPos(&p);
ScreenToClient(hListWnd, &p);
int pos = SendMessage(hListWnd, LB_ITEMFROMPOINT, 0, (WPARAM)MAKELPARAM(p.x, p.y));
int currPos = ListBox_GetCurSel(hListWnd);
if (pos != -1 && currPos != pos) {
ListBox_SetCurSel(hListWnd, pos);
SendMessage(hWnd, WM_COMMAND, MAKELPARAM(IDC_TABLELIST, LBN_SELCHANGE), (LPARAM)hListWnd);
}
if (pos != -1 && currPos != -1 && getStoredValue(TEXT("editable"), 1) == 1) {
ClientToScreen(hListWnd, &p);
HMENU hMenu = GetProp(hWnd, TEXT("LISTMENU"));
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_TOPALIGN | TPM_LEFTALIGN, p.x, p.y, 0, hWnd, NULL);
}
}
}
break;
case WM_CTLCOLORLISTBOX: {
SetBkColor((HDC)wParam, *(int*)GetProp(hWnd, TEXT("BACKCOLOR")));
SetTextColor((HDC)wParam, *(int*)GetProp(hWnd, TEXT("TEXTCOLOR")));
return (INT_PTR)(HBRUSH)GetProp(hWnd, TEXT("BACKBRUSH"));
}
break;
case WM_COMMAND: {
WORD cmd = LOWORD(wParam);
if (cmd == IDC_BUTTON) {
struct DLGITEMTEMPLATEEX tpl = {0};
HWND hListWnd = GetDlgItem(hWnd, IDC_TABLELIST);
if (IDOK == DialogBoxIndirectParam ((HINSTANCE)GetModuleHandle(NULL), (LPCDLGTEMPLATE)&tpl, hWnd, &cbDlgAddTable, (LPARAM)hWnd)) {
ListBox_SetCurSel(hListWnd, ListBox_GetCount(hListWnd) - 1);
SendMessage(hWnd, WMU_UPDATE_COUNTS, 0, 0);
SendMessage(hWnd, WM_COMMAND, MAKELPARAM(IDC_TABLELIST, LBN_SELCHANGE), (LPARAM)hListWnd);
}
SetFocus(hListWnd);
}
if (cmd == IDC_TABLELIST && HIWORD(wParam) == LBN_SELCHANGE)
SendMessage(hWnd, WMU_UPDATE_GRID, 0, 0);
if (cmd == IDC_TABLELIST && HIWORD(wParam) == LBN_SETFOCUS)
SetProp(hWnd, TEXT("LASTFOCUS"), (HWND)lParam);
if (cmd == IDM_COPY_CELL || cmd == IDM_COPY_ROWS || cmd == IDM_COPY_COLUMN || cmd == IDM_SAVE_AS) {
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;
}
int cacheSize = *(int*)GetProp(hWnd, TEXT("CACHESIZE"));
int cacheOffset = *(int*)GetProp(hWnd, TEXT("CACHEOFFSET"));
int minRowNo = ListView_GetNextItem(hGridWnd, -1, LVNI_SELECTED);
int maxRowNo = minRowNo;
do {
int rowNo = ListView_GetNextItem(hGridWnd, maxRowNo, LVNI_SELECTED);
if (rowNo == -1)
break;
maxRowNo = rowNo;
} while (maxRowNo <= cacheOffset + cacheSize);
if (rowNo < cacheOffset ||
rowNo >= cacheOffset + cacheSize ||
(cmd == IDM_COPY_COLUMN && selCount == 1 && (0 < cacheOffset || rowCount > cacheSize)) ||
(cmd == IDM_COPY_COLUMN && selCount != 1 && (minRowNo < cacheOffset || maxRowNo >= cacheOffset + cacheSize)) ||
((cmd == IDM_COPY_ROWS || cmd == IDM_SAVE_AS) && selCount > 1 && (minRowNo < cacheOffset || maxRowNo >= cacheOffset + cacheSize))
) {
TCHAR msg[1024];
_sntprintf(msg, 1024, TEXT("The operation is available only for full-cached resultset.\nThis resultset has %i rows and a cache size is %i.\nCheck Wiki to learn how to increase the cache size."), rowCount, cacheSize);
MessageBox(hWnd, msg, 0, 0);
return 0;
}
TCHAR*** cache = (TCHAR***)GetProp(hWnd, TEXT("CACHE"));
TCHAR* delimiter = getStoredString(TEXT("column-delimiter"), TEXT("\t"));
UINT searchNext = cmd == IDM_SAVE_AS && ListView_GetSelectedCount(hGridWnd) < 2 ? LVNI_ALL : LVNI_SELECTED;
int len = 0;
if (cmd == IDM_COPY_CELL)
len = _tcslen(cache[rowNo - cacheOffset][colNo]);
if (cmd == IDM_COPY_ROWS || cmd == IDM_SAVE_AS) {
int rowNo = ListView_GetNextItem(hGridWnd, -1, searchNext);
while (rowNo != -1) {
for (int colNo = 0; colNo < colCount; colNo++) {
if (ListView_GetColumnWidth(hGridWnd, colNo))
len += _tcslen(cache[rowNo - cacheOffset][colNo]) + 1; /* column delimiter */
}
len++; /* \n */
rowNo = ListView_GetNextItem(hGridWnd, rowNo, searchNext);
}
}
if (cmd == IDM_COPY_COLUMN) {
int rowNo = selCount < 2 ? 0 : ListView_GetNextItem(hGridWnd, -1, searchNext);
while (rowNo != -1 && rowNo < rowCount) {
len += _tcslen(cache[rowNo - cacheOffset][colNo]) + 1 /* \n */;
rowNo = selCount < 2 ? rowNo + 1 : ListView_GetNextItem(hGridWnd, rowNo, searchNext);
}
}
TCHAR* buf = calloc(len + 1, sizeof(TCHAR));
if (cmd == IDM_COPY_CELL)
_tcscat(buf, cache[rowNo - cacheOffset][colNo]);
if (cmd == IDM_COPY_ROWS || cmd == IDM_SAVE_AS) {
int pos = 0;
int rowNo = ListView_GetNextItem(hGridWnd, -1, searchNext);
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[rowNo - cacheOffset][colNo]);
_tcsncpy(buf + pos, cache[rowNo - cacheOffset][colNo], len);
buf[pos + len] = delimiter[0];
pos += len + 1;
}
}
buf[pos - (pos > 0)] = TEXT('\n');
rowNo = ListView_GetNextItem(hGridWnd, rowNo, searchNext);
}
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, searchNext);
while (rowNo != -1 && rowNo < rowCount) {
int len = _tcslen(cache[rowNo - cacheOffset][colNo]);
_tcsncpy(buf + pos, cache[rowNo - cacheOffset][colNo], len);
rowNo = selCount < 2 ? rowNo + 1 : ListView_GetNextItem(hGridWnd, rowNo, searchNext);
if (rowNo != -1 && rowNo < rowCount)
buf[pos + len] = TEXT('\n');
pos += len + 1;
}
}
if (cmd != IDM_SAVE_AS) {
setClipboardText(buf);
} else {
TCHAR path16[MAX_PATH] = {0};
TCHAR filter16[] = TEXT("CSV files (*.csv, *.tsv)\0*.csv;*.tsv\0All\0*.*\0");
if (saveFile(path16, filter16, TEXT(""), hWnd)) {
FILE* fp = _tfopen (path16, TEXT("wb"));
if (fp) {
char* delimiter8 = utf16to8(delimiter);
TCHAR colName16[MAX_COLUMN_LENGTH];
for(int colNo = 0; colNo < colCount; colNo++) {
Header_GetItemText(hHeader, colNo, colName16, MAX_COLUMN_LENGTH);
char* colName8 = utf16to8(colName16);
fwrite(colName8, strlen(colName8), 1, fp);
free(colName8);
fwrite(colNo < colCount - 1 ? delimiter8 : "\n", 1, 1, fp);
}
free(delimiter8);