Skip to content

Commit bc8639e

Browse files
committed
0.8.0.45: Fixed unusual state bug that would cause actions that spawned more than one dialog to hide the main editor window when the file picker opens
1 parent bad149e commit bc8639e

5 files changed

Lines changed: 72 additions & 115 deletions

File tree

src/sqlite-ce-edit/fileops.c

Lines changed: 43 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,12 @@ void OpenQueryFile(const wchar_t *path) {
215215
}
216216

217217
void DoOpenQuery(void) {
218-
CE_OPENFILENAME ofn;
219218
wchar_t szFile[MAX_PATH] = L"";
220219
int i;
221220

222-
memset(&ofn, 0, sizeof(ofn));
223-
ofn.lStructSize = sizeof(ofn);
224-
ofn.hwndOwner = g_hwndMain;
225-
ofn.lpstrFile = szFile;
226-
ofn.nMaxFile = MAX_PATH;
227-
ofn.lpstrFilter = L"SQL Files (*.sql)\0*.sql\0All Files (*.*)\0*.*\0";
228-
ofn.lpstrTitle = L"Open Query";
229-
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
230-
ofn.lpstrInitialDir = g_szLastQueryDir;
231-
232-
if (GetOpenFileNameW(&ofn)) {
221+
if (CustomFilePicker(g_hwndMain, szFile, MAX_PATH,
222+
L"Open Query", L"SQL Files (*.sql)\0*.sql\0",
223+
NULL, g_szLastQueryDir, 0)) {
233224
/* Remember directory for next time */
234225
lstrcpyW(g_szLastQueryDir, szFile);
235226
for (i = lstrlenW(g_szLastQueryDir) - 1; i >= 0; i--) {
@@ -241,7 +232,6 @@ void DoOpenQuery(void) {
241232
}
242233

243234
void DoSaveQuery(void) {
244-
CE_OPENFILENAME ofn;
245235
wchar_t szFile[MAX_PATH];
246236
HANDLE hFile;
247237
DWORD dwLen, dwWritten;
@@ -253,17 +243,10 @@ void DoSaveQuery(void) {
253243
lstrcpyW(szFile, g_szQueryPath);
254244
} else {
255245
lstrcpyW(szFile, L"query.sql");
256-
memset(&ofn, 0, sizeof(ofn));
257-
ofn.lStructSize = sizeof(ofn);
258-
ofn.hwndOwner = g_hwndMain;
259-
ofn.lpstrFile = szFile;
260-
ofn.nMaxFile = MAX_PATH;
261-
ofn.lpstrFilter = L"SQL Files (*.sql)\0*.sql\0All Files (*.*)\0*.*\0";
262-
ofn.lpstrDefExt = L"sql";
263-
ofn.lpstrTitle = L"Save Query";
264-
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
265246

266-
if (!GetSaveFileNameW(&ofn)) return;
247+
if (!CustomFilePicker(g_hwndMain, szFile, MAX_PATH,
248+
L"Save Query", L"SQL Files (*.sql)\0*.sql\0",
249+
L"sql", g_szLastQueryDir, 1)) return;
267250
lstrcpyW(g_szQueryPath, szFile);
268251
UpdateTitle();
269252
}
@@ -290,29 +273,21 @@ void DoSaveQuery(void) {
290273
**============================================================================*/
291274

292275
void DoExportResults(void) {
293-
CE_OPENFILENAME ofn;
294-
wchar_t szFile[MAX_PATH] = L"results";
276+
wchar_t szFile[MAX_PATH] = L"results.csv";
295277
HANDLE hFile;
296278
DWORD dwLen, dwWritten;
297279
wchar_t *wbuf, *wp;
298280
char *buf, *bp;
299281
int needQuote, isCSV;
282+
wchar_t *dot;
300283

301-
memset(&ofn, 0, sizeof(ofn));
302-
ofn.lStructSize = sizeof(ofn);
303-
ofn.hwndOwner = g_hwndMain;
304-
ofn.lpstrFile = szFile;
305-
ofn.nMaxFile = MAX_PATH;
306-
ofn.lpstrFilter = L"CSV Files (*.csv)\0*.csv\0Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
307-
ofn.lpstrDefExt = NULL; /* We'll handle extension ourselves */
308-
ofn.lpstrTitle = L"Export Results";
309-
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
310-
ofn.nFilterIndex = 1;
284+
if (!CustomFilePicker(g_hwndMain, szFile, MAX_PATH,
285+
L"Export Results", L"CSV Files (*.csv)\0*.csv\0",
286+
L"csv", NULL, 1)) return;
311287

312-
if (!GetSaveFileNameW(&ofn)) return;
313-
314-
/* Check if CSV (filter 1) or Text (filter 2+) */
315-
isCSV = (ofn.nFilterIndex == 1);
288+
/* Check extension to determine format */
289+
dot = szFile + lstrlenW(szFile) - 4;
290+
isCSV = (dot > szFile && (lstrcmpiW(dot, L".csv") == 0));
316291

317292
/* Append extension if none present */
318293
{
@@ -510,6 +485,7 @@ void DoExportTxt(void) {
510485

511486
static HWND g_hwndPickDlg;
512487
static char g_pickResult[128];
488+
static int g_pickDone;
513489

514490
static LRESULT CALLBACK PickWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
515491
if (msg == WM_COMMAND) {
@@ -536,7 +512,7 @@ static LRESULT CALLBACK PickWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM l
536512
return 0;
537513
}
538514
if (msg == WM_DESTROY) {
539-
PostQuitMessage(0);
515+
g_pickDone = 1;
540516
return 0;
541517
}
542518
return DefWindowProcW(hwnd, msg, wParam, lParam);
@@ -573,16 +549,23 @@ static int PickTable(char *tblName) {
573549
return 0;
574550
}
575551

576-
/* Register window class */
577-
memset(&wc, 0, sizeof(wc));
578-
wc.lpfnWndProc = PickWndProc;
579-
wc.hInstance = g_hInst;
580-
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
581-
wc.lpszClassName = L"PickTableWnd";
582-
RegisterClassW(&wc);
552+
/* Register window class once */
553+
{
554+
static int classRegistered = 0;
555+
if (!classRegistered) {
556+
memset(&wc, 0, sizeof(wc));
557+
wc.lpfnWndProc = PickWndProc;
558+
wc.hInstance = g_hInst;
559+
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
560+
wc.lpszClassName = L"PickTableWnd";
561+
RegisterClassW(&wc);
562+
classRegistered = 1;
563+
}
564+
}
583565

584566
/* Create popup window */
585567
g_pickResult[0] = 0;
568+
g_pickDone = 0;
586569
g_hwndPickDlg = CreateWindowExW(0, L"PickTableWnd", L"Select Table",
587570
WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
588571
50, 50, 180, 160, g_hwndMain, NULL, g_hInst, NULL);
@@ -611,15 +594,15 @@ static int PickTable(char *tblName) {
611594
SetFocus(hwndList);
612595
EnableWindow(g_hwndMain, FALSE);
613596

614-
while (GetMessageW(&msg, NULL, 0, 0)) {
597+
while (!g_pickDone && GetMessageW(&msg, NULL, 0, 0)) {
615598
TranslateMessage(&msg);
616599
DispatchMessageW(&msg);
617600
}
618601

619602
EnableWindow(g_hwndMain, TRUE);
603+
ShowWindow(g_hwndMain, SW_SHOWNORMAL);
620604
SetForegroundWindow(g_hwndMain);
621605
sqlite_free_table(results);
622-
UnregisterClassW(L"PickTableWnd", g_hInst);
623606

624607
if (g_pickResult[0]) {
625608
strcpy(tblName, g_pickResult);
@@ -629,7 +612,6 @@ static int PickTable(char *tblName) {
629612
}
630613

631614
void DoExportTable(void) {
632-
CE_OPENFILENAME ofn;
633615
wchar_t szFile[MAX_PATH];
634616
char tblName[128];
635617
char sql[512];
@@ -640,6 +622,7 @@ void DoExportTable(void) {
640622
char line[4096];
641623
char *lp, *p;
642624
const char *t;
625+
wchar_t *ext;
643626

644627
if (!g_db) return;
645628

@@ -671,28 +654,18 @@ void DoExportTable(void) {
671654

672655
/* Default filename from table name */
673656
MultiByteToWideChar(CP_ACP, 0, tblName, -1, szFile, MAX_PATH);
657+
lstrcatW(szFile, L".csv");
674658

675-
memset(&ofn, 0, sizeof(ofn));
676-
ofn.lStructSize = sizeof(ofn);
677-
ofn.hwndOwner = g_hwndMain;
678-
ofn.lpstrFile = szFile;
679-
ofn.nMaxFile = MAX_PATH;
680-
ofn.lpstrFilter = L"CSV (*.csv)\0*.csv\0SQL INSERT (*.sql)\0*.sql\0SQL CREATE+INSERT (*.sql)\0*.sql\0";
681-
ofn.lpstrTitle = L"Export Table";
682-
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
659+
if (!CustomFilePicker(g_hwndMain, szFile, MAX_PATH,
660+
L"Export Table", L"CSV (*.csv)\0*.csv\0",
661+
L"csv", NULL, 1)) return;
683662

684-
if (!GetSaveFileNameW(&ofn)) return;
685-
686-
/* fmt: 1=CSV, 2=INSERT, 3=CREATE+INSERT */
687-
fmt = ofn.nFilterIndex;
688-
689-
/* Append extension if missing */
690-
{
691-
wchar_t *ext = (fmt == 1) ? L".csv" : L".sql";
692-
int len = lstrlenW(szFile);
693-
int elen = lstrlenW(ext);
694-
if (len < elen || lstrcmpiW(szFile + len - elen, ext) != 0)
695-
lstrcatW(szFile, ext);
663+
/* Determine format from extension: csv=1, sql=2 (INSERT), default csv */
664+
ext = szFile + lstrlenW(szFile) - 4;
665+
if (ext > szFile && lstrcmpiW(ext, L".sql") == 0) {
666+
fmt = 2; /* SQL INSERT */
667+
} else {
668+
fmt = 1; /* CSV */
696669
}
697670

698671
/* Query table data */

src/sqlite-ce-edit/filepicker.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static const wchar_t *g_pickerFilter = NULL;
1919
static const wchar_t *g_pickerDefExt = NULL;
2020
static int g_pickerSaveMode = 0;
2121
static int g_pickerOK = 0;
22+
static int g_pickerDone = 0;
2223
static WNDPROC g_pfnListProc = NULL;
2324

2425
/*============================================================================
@@ -334,6 +335,7 @@ static LRESULT CALLBACK PickerWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
334335
return 0;
335336
case WM_DESTROY:
336337
g_hwndPicker = NULL;
338+
g_pickerDone = 1;
337339
return 0;
338340
}
339341
return DefWindowProcW(hwnd, msg, wParam, lParam);
@@ -358,6 +360,7 @@ int CustomFilePicker(HWND hwndOwner, wchar_t *filePath, int maxPath,
358360
g_pickerDefExt = defExt;
359361
g_pickerSaveMode = saveMode;
360362
g_pickerOK = 0;
363+
g_pickerDone = 0;
361364

362365
/* Set initial directory */
363366
if (initialDir && initialDir[0]) {
@@ -378,12 +381,18 @@ int CustomFilePicker(HWND hwndOwner, wchar_t *filePath, int maxPath,
378381
lstrcpyW(g_pickerResult, fn);
379382
}
380383

381-
/* Register window class */
382-
wc.lpfnWndProc = PickerWndProc;
383-
wc.hInstance = g_hInst;
384-
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
385-
wc.lpszClassName = L"SQLiteCEFilePicker";
386-
RegisterClassW(&wc);
384+
/* Register window class once */
385+
{
386+
static int classRegistered = 0;
387+
if (!classRegistered) {
388+
wc.lpfnWndProc = PickerWndProc;
389+
wc.hInstance = g_hInst;
390+
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
391+
wc.lpszClassName = L"SQLiteCEFilePicker";
392+
RegisterClassW(&wc);
393+
classRegistered = 1;
394+
}
395+
}
387396

388397
/* Create dialog */
389398
GetWindowRect(hwndOwner, &rc);
@@ -427,15 +436,16 @@ int CustomFilePicker(HWND hwndOwner, wchar_t *filePath, int maxPath,
427436

428437
/* Modal loop */
429438
EnableWindow(hwndOwner, FALSE);
430-
while (g_hwndPicker && GetMessageW(&msg, NULL, 0, 0)) {
439+
440+
while (!g_pickerDone && GetMessageW(&msg, NULL, 0, 0)) {
431441
TranslateMessage(&msg);
432442
DispatchMessageW(&msg);
433443
}
444+
434445
EnableWindow(hwndOwner, TRUE);
446+
ShowWindow(hwndOwner, SW_SHOWNORMAL);
435447
SetForegroundWindow(hwndOwner);
436448

437-
UnregisterClassW(L"SQLiteCEFilePicker", g_hInst);
438-
439449
/* Copy result */
440450
if (g_pickerOK && g_pickerResult[0]) {
441451
lstrcpyW(filePath, g_pickerResult);

src/sqlite-ce-edit/globals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ BOOL WINAPI GetSaveFileNameW(CE_OPENFILENAME*);
8282
** Version
8383
**============================================================================*/
8484

85-
#define SQLITECEDIT_VERSION L"0.8.0.32"
85+
#define SQLITECEDIT_VERSION L"0.8.0.45"
8686

8787
/*============================================================================
8888
** Menu IDs

src/sqlite-ce-edit/import.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ static int ParseCSVLine(char *line, char **fields, int maxFields) {
5353
}
5454

5555
void DoImportCSV(void) {
56-
CE_OPENFILENAME ofn;
5756
wchar_t szFile[MAX_PATH] = L"";
5857
wchar_t tblName[64];
5958
wchar_t *wp;
@@ -73,16 +72,9 @@ void DoImportCSV(void) {
7372
return;
7473
}
7574

76-
memset(&ofn, 0, sizeof(ofn));
77-
ofn.lStructSize = sizeof(ofn);
78-
ofn.hwndOwner = g_hwndMain;
79-
ofn.lpstrFile = szFile;
80-
ofn.nMaxFile = MAX_PATH;
81-
ofn.lpstrFilter = L"CSV Files (*.csv)\0*.csv\0All Files (*.*)\0*.*\0";
82-
ofn.lpstrTitle = L"Import CSV";
83-
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
84-
85-
if (!GetOpenFileNameW(&ofn)) return;
75+
if (!CustomFilePicker(g_hwndMain, szFile, MAX_PATH,
76+
L"Import CSV", L"CSV Files (*.csv)\0*.csv\0",
77+
NULL, NULL, 0)) return;
8678

8779
fn = GetFilename(szFile);
8880
wp = tblName;

src/sqlite-ce-edit/schema.c

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ void ExportSelectedDDL(void) {
694694
char sql[256];
695695
char **results = NULL;
696696
int nRows = 0, nCols = 0;
697-
CE_OPENFILENAME ofn;
698697
HANDLE hFile;
699698
DWORD dwWritten;
700699

@@ -738,17 +737,9 @@ void ExportSelectedDDL(void) {
738737
/* Default filename */
739738
wsprintfW(szFile, L"%s.sql", text);
740739

741-
memset(&ofn, 0, sizeof(ofn));
742-
ofn.lStructSize = sizeof(ofn);
743-
ofn.hwndOwner = g_hwndMain;
744-
ofn.lpstrFile = szFile;
745-
ofn.nMaxFile = MAX_PATH;
746-
ofn.lpstrFilter = L"SQL Files (*.sql)\0*.sql\0All Files (*.*)\0*.*\0";
747-
ofn.lpstrDefExt = L"sql";
748-
ofn.lpstrTitle = L"Export DDL";
749-
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
750-
751-
if (GetSaveFileNameW(&ofn)) {
740+
if (CustomFilePicker(g_hwndMain, szFile, MAX_PATH,
741+
L"Export DDL", L"SQL Files (*.sql)\0*.sql\0",
742+
L"sql", NULL, 1)) {
752743
hFile = CreateFileW(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
753744
if (hFile != INVALID_HANDLE_VALUE) {
754745
WriteFile(hFile, results[1], strlen(results[1]), &dwWritten, NULL);
@@ -770,7 +761,6 @@ void ExportAllDDL(void) {
770761
char header[256];
771762
char **results = NULL;
772763
int nRows = 0, nCols = 0, i;
773-
CE_OPENFILENAME ofn;
774764
HANDLE hFile;
775765
DWORD dwWritten;
776766
SYSTEMTIME st;
@@ -789,17 +779,9 @@ void ExportAllDDL(void) {
789779
}
790780
wsprintfW(szFile, L"%s.sql", dbname);
791781

792-
memset(&ofn, 0, sizeof(ofn));
793-
ofn.lStructSize = sizeof(ofn);
794-
ofn.hwndOwner = g_hwndMain;
795-
ofn.lpstrFile = szFile;
796-
ofn.nMaxFile = MAX_PATH;
797-
ofn.lpstrFilter = L"SQL Files (*.sql)\0*.sql\0All Files (*.*)\0*.*\0";
798-
ofn.lpstrDefExt = L"sql";
799-
ofn.lpstrTitle = L"Export All DDL";
800-
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
801-
802-
if (!GetSaveFileNameW(&ofn)) return;
782+
if (!CustomFilePicker(g_hwndMain, szFile, MAX_PATH,
783+
L"Export All DDL", L"SQL Files (*.sql)\0*.sql\0",
784+
L"sql", NULL, 1)) return;
803785

804786
/* Get all DDL ordered: tables first, then views, then triggers, then indexes */
805787
sqlite_get_table(g_db,

0 commit comments

Comments
 (0)