Skip to content

Commit 3551d86

Browse files
committed
Dynamic context menu depending on current view. Result export now supports plaintext alongside CSV (untested)
1 parent 3c4735a commit 3551d86

4 files changed

Lines changed: 100 additions & 0 deletions

File tree

src/sqlite-ce-edit/editor.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,56 @@ void SyncLineNumScroll(void) {
113113
UpdateLineNumbers();
114114
}
115115

116+
static int g_lastMenuMode = 0; /* Start with Query menu (mode 0) */
117+
118+
static void UpdateContextMenu(int mode) {
119+
HMENU hCBMenu, hCtx;
120+
121+
/* Only rebuild if mode changed */
122+
if (mode == g_lastMenuMode) return;
123+
124+
/* Get the actual menu from CommandBar */
125+
hCBMenu = CommandBar_GetMenu(g_hwndCB, 0);
126+
if (!hCBMenu) return;
127+
128+
/* Remove old context menu at position 1 */
129+
RemoveMenu(hCBMenu, 1, MF_BYPOSITION);
130+
131+
/* Create and insert new context menu */
132+
hCtx = CreatePopupMenu();
133+
if (mode == 0) {
134+
AppendMenuW(hCtx, MF_STRING, IDM_EXECUTE, L"&Execute\tCtrl+Enter");
135+
AppendMenuW(hCtx, MF_SEPARATOR, 0, NULL);
136+
AppendMenuW(hCtx, MF_STRING, IDM_FIND, L"&Find...\tCtrl+F");
137+
AppendMenuW(hCtx, MF_STRING, IDM_FINDNEXT, L"Find &Next\tF3");
138+
InsertMenuW(hCBMenu, 1, MF_BYPOSITION | MF_POPUP, (UINT)hCtx, L"&Query");
139+
} else if (mode == 1) {
140+
AppendMenuW(hCtx, MF_STRING, IDM_EXPORTCSV, L"Export as &CSV...");
141+
AppendMenuW(hCtx, MF_STRING, IDM_EXPORTTXT, L"Export as &Text...");
142+
AppendMenuW(hCtx, MF_SEPARATOR, 0, NULL);
143+
AppendMenuW(hCtx, MF_STRING, IDM_FIND, L"&Find...\tCtrl+F");
144+
AppendMenuW(hCtx, MF_STRING, IDM_FINDNEXT, L"Find &Next\tF3");
145+
InsertMenuW(hCBMenu, 1, MF_BYPOSITION | MF_POPUP, (UINT)hCtx, L"&Results");
146+
} else {
147+
AppendMenuW(hCtx, MF_STRING, IDM_REFRESH, L"&Refresh");
148+
InsertMenuW(hCBMenu, 1, MF_BYPOSITION | MF_POPUP, (UINT)hCtx, L"&Schema");
149+
}
150+
151+
g_lastMenuMode = mode;
152+
153+
/* Update View menu radio check - get from CommandBar's menu */
154+
{
155+
HMENU hViewSub = GetSubMenu(hCBMenu, 2); /* View is at position 2 */
156+
if (hViewSub) {
157+
CheckMenuRadioItem(hViewSub, IDM_VIEWQUERY, IDM_VIEWSCHEMA,
158+
mode == 0 ? IDM_VIEWQUERY : (mode == 1 ? IDM_VIEWRESULT : IDM_VIEWSCHEMA), MF_BYCOMMAND);
159+
}
160+
}
161+
162+
/* Force CommandBar to redraw */
163+
CommandBar_DrawMenuBar(g_hwndCB, 0);
164+
}
165+
116166
void SwitchView(int mode) {
117167
g_viewMode = mode;
118168
ShowWindow(g_hwndQuery, mode == 0 ? SW_SHOW : SW_HIDE);
@@ -121,6 +171,8 @@ void SwitchView(int mode) {
121171
if (g_hwndSchema) ShowWindow(g_hwndSchema, mode == 2 ? SW_SHOW : SW_HIDE);
122172
/* Disable exec-at-cursor button in non-query views */
123173
SendMessage(g_hwndCB, TB_ENABLEBUTTON, IDM_EXECATCURSOR, mode == 0);
174+
/* Update context menu */
175+
UpdateContextMenu(mode);
124176
if (mode == 0) {
125177
SetFocus(g_hwndQuery);
126178
UpdateLineCount();

src/sqlite-ce-edit/fileops.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,49 @@ void DoExportCSV(void) {
348348
if (buf) LocalFree(buf);
349349
}
350350

351+
/*============================================================================
352+
** Export Results to Text File
353+
**============================================================================*/
354+
355+
void DoExportTxt(void) {
356+
CE_OPENFILENAME ofn;
357+
wchar_t szFile[MAX_PATH] = L"results.txt";
358+
HANDLE hFile;
359+
DWORD dwLen, dwWritten;
360+
wchar_t *wbuf;
361+
char *buf;
362+
363+
memset(&ofn, 0, sizeof(ofn));
364+
ofn.lStructSize = sizeof(ofn);
365+
ofn.hwndOwner = g_hwndMain;
366+
ofn.lpstrFile = szFile;
367+
ofn.nMaxFile = MAX_PATH;
368+
ofn.lpstrFilter = L"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
369+
ofn.lpstrDefExt = L"txt";
370+
ofn.lpstrTitle = L"Export Results";
371+
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
372+
373+
if (!GetSaveFileNameW(&ofn)) return;
374+
375+
dwLen = GetWindowTextLengthW(g_hwndResult);
376+
if (dwLen == 0) return;
377+
378+
wbuf = (wchar_t*)LocalAlloc(LMEM_FIXED, (dwLen + 1) * sizeof(wchar_t));
379+
buf = (char*)LocalAlloc(LMEM_FIXED, dwLen + 1);
380+
if (wbuf && buf) {
381+
GetWindowTextW(g_hwndResult, wbuf, dwLen + 1);
382+
WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buf, dwLen + 1, NULL, NULL);
383+
384+
hFile = CreateFileW(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
385+
if (hFile != INVALID_HANDLE_VALUE) {
386+
WriteFile(hFile, buf, (DWORD)strlen(buf), &dwWritten, NULL);
387+
CloseHandle(hFile);
388+
}
389+
}
390+
if (wbuf) LocalFree(wbuf);
391+
if (buf) LocalFree(buf);
392+
}
393+
351394
/*============================================================================
352395
** Export Table to CSV (helper for DoExportDb)
353396
**============================================================================*/

src/sqlite-ce-edit/globals.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ BOOL WINAPI GetSaveFileNameW(CE_OPENFILENAME*);
9696
#define IDM_SAVEQUERY 106
9797
#define IDM_EXPORTCSV 107
9898
#define IDM_EXPORTDB 108
99+
#define IDM_EXPORTTXT 109
99100
#define IDM_IMPORTCSV 109
100101
#define IDM_IMPORTCEDB 110
101102
#define IDM_EXECUTE 201
@@ -119,6 +120,7 @@ BOOL WINAPI GetSaveFileNameW(CE_OPENFILENAME*);
119120
/* Schema context menu */
120121
#define IDM_SCHEMA_SELECT 700
121122
#define IDM_SCHEMA_DROP 701
123+
#define IDM_REFRESH 702
122124

123125
/* Timer IDs */
124126
#define IDT_TAPHOLD 1
@@ -223,6 +225,7 @@ void UpdateLineCount(void);
223225
void UpdateLineNumbers(void);
224226
void SyncLineNumScroll(void);
225227
void SwitchView(int mode);
228+
void BuildMenuBar(int mode);
226229
void UpdateQueryFont(void);
227230
void UpdateResultFont(void);
228231
void CycleFontSize(void);
@@ -269,6 +272,7 @@ void DoFileOpen(void);
269272
void DoOpenQuery(void);
270273
void DoSaveQuery(void);
271274
void DoExportCSV(void);
275+
void DoExportTxt(void);
272276
void DoExportDb(void);
273277
void DoImportCSV(void);
274278
void DoImportCEDB(void);

src/sqlite-ce-edit/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
248248
case IDM_OPENQUERY: DoOpenQuery(); break;
249249
case IDM_SAVEQUERY: DoSaveQuery(); break;
250250
case IDM_EXPORTCSV: DoExportCSV(); break;
251+
case IDM_EXPORTTXT: DoExportTxt(); break;
251252
case IDM_EXPORTDB: DoExportDb(); break;
252253
case IDM_IMPORTCSV: DoImportCSV(); break;
253254
case IDM_IMPORTCEDB: DoImportCEDB(); break;

0 commit comments

Comments
 (0)