@@ -215,21 +215,12 @@ void OpenQueryFile(const wchar_t *path) {
215215}
216216
217217void 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
243234void 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
292275void 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
511486static HWND g_hwndPickDlg ;
512487static char g_pickResult [128 ];
488+ static int g_pickDone ;
513489
514490static 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
631614void 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 */
0 commit comments