Skip to content

Commit 972d93a

Browse files
committed
Implemented new options dialogue with specifiable default database path, consolidated command bar menus
1 parent ae9c26a commit 972d93a

6 files changed

Lines changed: 183 additions & 23 deletions

File tree

src/sqlite-ce-edit/dialogs.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,128 @@ int PromptForPath(const wchar_t *title, const wchar_t *defPath) {
138138

139139
return DialogBoxIndirectW(g_hInst, &dlg.tmpl, g_hwndMain, PathDlgProc) == IDOK;
140140
}
141+
142+
/*============================================================================
143+
** Options Dialog
144+
**============================================================================*/
145+
146+
#define IDC_OPT_CLEAREXEC 1001
147+
#define IDC_OPT_EXECATCURSOR 1002
148+
#define IDC_OPT_LINENUMS 1003
149+
#define IDC_OPT_ERRORMSGBOX 1004
150+
#define IDC_OPT_DBPATH 1005
151+
152+
static int g_optClearExec, g_optExecAtCursor, g_optLineNums, g_optErrorMsgBox;
153+
static wchar_t g_optDbPath[MAX_PATH];
154+
static HWND g_hwndOptions = NULL;
155+
static int g_optResult = 0;
156+
157+
static void ApplyOptions(HWND hwnd) {
158+
g_optClearExec = SendMessage(GetDlgItem(hwnd, IDC_OPT_CLEAREXEC), BM_GETCHECK, 0, 0);
159+
g_optExecAtCursor = SendMessage(GetDlgItem(hwnd, IDC_OPT_EXECATCURSOR), BM_GETCHECK, 0, 0);
160+
g_optLineNums = SendMessage(GetDlgItem(hwnd, IDC_OPT_LINENUMS), BM_GETCHECK, 0, 0);
161+
g_optErrorMsgBox = SendMessage(GetDlgItem(hwnd, IDC_OPT_ERRORMSGBOX), BM_GETCHECK, 0, 0);
162+
GetWindowTextW(GetDlgItem(hwnd, IDC_OPT_DBPATH), g_optDbPath, MAX_PATH);
163+
g_optResult = 1;
164+
}
165+
166+
static LRESULT CALLBACK OptionsWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
167+
switch (msg) {
168+
case WM_CREATE: {
169+
/* Left column - checkboxes */
170+
CreateWindowW(L"BUTTON", L"Clear results on execute",
171+
WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
172+
10, 10, 165, 20, hwnd, (HMENU)IDC_OPT_CLEAREXEC, g_hInst, NULL);
173+
CreateWindowW(L"BUTTON", L"Execute at cursor",
174+
WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
175+
10, 32, 165, 20, hwnd, (HMENU)IDC_OPT_EXECATCURSOR, g_hInst, NULL);
176+
CreateWindowW(L"BUTTON", L"Show line numbers",
177+
WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
178+
10, 54, 165, 20, hwnd, (HMENU)IDC_OPT_LINENUMS, g_hInst, NULL);
179+
CreateWindowW(L"BUTTON", L"Message box on error",
180+
WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
181+
10, 76, 165, 20, hwnd, (HMENU)IDC_OPT_ERRORMSGBOX, g_hInst, NULL);
182+
183+
/* Right column - paths */
184+
CreateWindowW(L"STATIC", L"Default database path:",
185+
WS_CHILD | WS_VISIBLE,
186+
185, 10, 150, 16, hwnd, NULL, g_hInst, NULL);
187+
CreateWindowW(L"EDIT", g_optDbPath,
188+
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
189+
185, 28, 180, 22, hwnd, (HMENU)IDC_OPT_DBPATH, g_hInst, NULL);
190+
191+
SendMessage(GetDlgItem(hwnd, IDC_OPT_CLEAREXEC), BM_SETCHECK, g_optClearExec, 0);
192+
SendMessage(GetDlgItem(hwnd, IDC_OPT_EXECATCURSOR), BM_SETCHECK, g_optExecAtCursor, 0);
193+
SendMessage(GetDlgItem(hwnd, IDC_OPT_LINENUMS), BM_SETCHECK, g_optLineNums, 0);
194+
SendMessage(GetDlgItem(hwnd, IDC_OPT_ERRORMSGBOX), BM_SETCHECK, g_optErrorMsgBox, 0);
195+
return 0;
196+
}
197+
case WM_COMMAND:
198+
if (LOWORD(wParam) == IDOK) {
199+
ApplyOptions(hwnd);
200+
DestroyWindow(hwnd);
201+
return 0;
202+
}
203+
break;
204+
case WM_CLOSE:
205+
DestroyWindow(hwnd);
206+
return 0;
207+
case WM_DESTROY:
208+
g_hwndOptions = NULL;
209+
SetFocus(g_viewMode == 0 ? g_hwndQuery : g_hwndResult);
210+
return 0;
211+
}
212+
return DefWindowProc(hwnd, msg, wParam, lParam);
213+
}
214+
215+
void DoOptions(void) {
216+
WNDCLASSW wc = {0};
217+
RECT rc;
218+
MSG msg;
219+
220+
if (g_hwndOptions) {
221+
SetFocus(g_hwndOptions);
222+
return;
223+
}
224+
225+
g_optClearExec = g_clearOnExec;
226+
g_optExecAtCursor = g_execAtCursor;
227+
g_optLineNums = g_showLineNumbers;
228+
g_optErrorMsgBox = g_showErrorMsgBox;
229+
lstrcpyW(g_optDbPath, g_szDefaultDbPath);
230+
g_optResult = 0;
231+
232+
wc.lpfnWndProc = OptionsWndProc;
233+
wc.hInstance = g_hInst;
234+
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
235+
wc.lpszClassName = L"SQLiteCEOptions";
236+
RegisterClassW(&wc);
237+
238+
GetWindowRect(g_hwndMain, &rc);
239+
g_hwndOptions = CreateWindowExW(WS_EX_CAPTIONOKBTN,
240+
L"SQLiteCEOptions", L"Options",
241+
WS_POPUP | WS_CAPTION | WS_SYSMENU,
242+
rc.left + 20, rc.top + 30, 380, 125,
243+
g_hwndMain, NULL, g_hInst, NULL);
244+
ShowWindow(g_hwndOptions, SW_SHOW);
245+
246+
/* Modal message loop */
247+
while (g_hwndOptions && GetMessage(&msg, NULL, 0, 0)) {
248+
TranslateMessage(&msg);
249+
DispatchMessage(&msg);
250+
}
251+
252+
if (g_optResult) {
253+
g_clearOnExec = g_optClearExec;
254+
g_execAtCursor = g_optExecAtCursor;
255+
g_showErrorMsgBox = g_optErrorMsgBox;
256+
lstrcpyW(g_szDefaultDbPath, g_optDbPath);
257+
258+
/* Handle line numbers toggle */
259+
if (g_optLineNums != g_showLineNumbers) {
260+
g_showLineNumbers = g_optLineNums;
261+
ShowWindow(g_hwndLineNum, (g_viewMode == 0 && g_showLineNumbers) ? SW_SHOW : SW_HIDE);
262+
SendMessage(g_hwndMain, WM_SIZE, 0, 0);
263+
}
264+
}
265+
}

src/sqlite-ce-edit/execute.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ void ExecuteQuery(void) {
155155
char *sql;
156156
char *errmsg = NULL;
157157
wchar_t *wsql;
158+
wchar_t lastError[256];
158159
DWORD selStart, selEnd;
159160

160161
if (!g_db) {
@@ -308,11 +309,14 @@ void ExecuteQuery(void) {
308309
if (rc != SQLITE_OK) {
309310
int ln = 1, i;
310311
char lb[16]; char *lp = lb + 14;
312+
const char *es = errmsg ? errmsg : sqlite_error_string(rc);
311313
for (i = 0; i < stmtOffset; i++) if (sql[i] == '\n') ln++;
312314
lb[15] = '\0'; *lp = '\0';
313315
while (ln > 0) { *--lp = '0' + (ln % 10); ln /= 10; }
314316
Output("Line "); Output(lp); Output(": ");
315-
OutputLine(errmsg ? errmsg : sqlite_error_string(rc));
317+
OutputLine(es);
318+
for (i = 0; es[i] && i < 255; i++) lastError[i] = (wchar_t)(unsigned char)es[i];
319+
lastError[i] = 0;
316320
if (errmsg) sqlite_freemem(errmsg);
317321
errorOffset = stmtOffset;
318322
hadError = 1;
@@ -341,12 +345,15 @@ void ExecuteQuery(void) {
341345
if (rc != SQLITE_OK) {
342346
int ln = 1, i;
343347
char lb[16]; char *lp = lb + 14;
348+
const char *es = errmsg ? errmsg : sqlite_error_string(rc);
344349
for (i = 0; i < stmtOffset; i++) if (sql[i] == '\n') ln++;
345350
lb[15] = '\0'; *lp = '\0';
346351
while (ln > 0) { *--lp = '0' + (ln % 10); ln /= 10; }
347352
errorOffset = stmtOffset;
348353
Output("Line "); Output(lp); Output(": ");
349-
OutputLine(errmsg ? errmsg : sqlite_error_string(rc));
354+
OutputLine(es);
355+
for (i = 0; es[i] && i < 255; i++) lastError[i] = (wchar_t)(unsigned char)es[i];
356+
lastError[i] = 0;
350357
if (errmsg) sqlite_freemem(errmsg);
351358
hadError = 1;
352359
} else if (g_nRows > 0) {
@@ -380,6 +387,8 @@ void ExecuteQuery(void) {
380387
SendMessageW(g_hwndStatus, SB_SETTEXTW, 1, (LPARAM)wbuf);
381388
g_suppressLineCount = 3;
382389
MessageBeep(MB_ICONEXCLAMATION);
390+
if (g_showErrorMsgBox)
391+
MessageBoxW(g_hwndMain, lastError, L"SQL Error", MB_OK | MB_ICONERROR);
383392
} else {
384393
wchar_t wbuf[64];
385394
Output("Query executed in ");

src/sqlite-ce-edit/fileops.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@
1010

1111
void DoFileNew(void) {
1212
CE_OPENFILENAME ofn;
13-
wchar_t szFile[MAX_PATH] = L"new.db";
13+
wchar_t szFile[MAX_PATH];
14+
int createdDir = 0;
15+
16+
/* Try to create default directory if it doesn't exist */
17+
if (g_szDefaultDbPath[0]) {
18+
DWORD attr = GetFileAttributesW(g_szDefaultDbPath);
19+
if (attr == 0xFFFFFFFF) {
20+
if (CreateDirectoryW(g_szDefaultDbPath, NULL))
21+
createdDir = 1;
22+
}
23+
lstrcpyW(szFile, L"new.db");
24+
} else {
25+
lstrcpyW(szFile, L"new.db");
26+
}
1427

1528
memset(&ofn, 0, sizeof(ofn));
1629
ofn.lStructSize = sizeof(ofn);
@@ -20,11 +33,15 @@ void DoFileNew(void) {
2033
ofn.lpstrFilter = L"Database Files (*.db)\0*.db\0All Files (*.*)\0*.*\0";
2134
ofn.lpstrDefExt = L"db";
2235
ofn.lpstrTitle = L"New Database";
36+
ofn.lpstrInitialDir = g_szDefaultDbPath[0] ? g_szDefaultDbPath : NULL;
2337
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
2438

2539
if (GetSaveFileNameW(&ofn)) {
2640
DeleteFileW(szFile);
2741
OpenDatabase(szFile);
42+
} else if (createdDir) {
43+
/* Remove directory if we created it and user cancelled */
44+
RemoveDirectoryW(g_szDefaultDbPath);
2845
}
2946
}
3047

src/sqlite-ce-edit/globals.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ wchar_t g_findText[128] = L"";
4646
int g_searchMode = 0;
4747
wchar_t g_szQueryPath[MAX_PATH] = L"";
4848
int g_queryDirty = 0;
49+
int g_showErrorMsgBox = 0;
50+
wchar_t g_szDefaultDbPath[MAX_PATH] = L"\\My Documents\\Data";

src/sqlite-ce-edit/globals.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
#include <windbase.h>
1212
#include "sqlite.h"
1313

14+
/*============================================================================
15+
** CE Extended Window Styles (may not be in CE 2.0 SDK headers)
16+
**============================================================================*/
17+
18+
#ifndef WS_EX_CAPTIONOKBTN
19+
#define WS_EX_CAPTIONOKBTN 0x80000000L
20+
#endif
21+
1422
/*============================================================================
1523
** Common File Dialog (may not be in CE 2.0 SDK headers)
1624
**============================================================================*/
@@ -81,6 +89,7 @@ BOOL WINAPI GetSaveFileNameW(CE_OPENFILENAME*);
8189
#define IDM_VIEWQUERY 401
8290
#define IDM_VIEWRESULT 402
8391
#define IDM_FONTSIZE 403
92+
#define IDM_OPTIONS 404
8493
#define IDM_ABOUT 501
8594

8695
/*============================================================================
@@ -147,6 +156,8 @@ extern wchar_t g_findText[128];
147156
extern int g_searchMode;
148157
extern wchar_t g_szQueryPath[MAX_PATH];
149158
extern int g_queryDirty;
159+
extern int g_showErrorMsgBox;
160+
extern wchar_t g_szDefaultDbPath[MAX_PATH];
150161

151162
/*============================================================================
152163
** Function Declarations - Output (output.c)
@@ -215,6 +226,7 @@ void DoFindNext(void);
215226
**============================================================================*/
216227

217228
void DoAbout(void);
229+
void DoOptions(void);
218230
int PromptForPath(const wchar_t *title, const wchar_t *defPath);
219231

220232
#endif /* GLOBALS_H */

src/sqlite-ce-edit/main.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
1818
case WM_CREATE: {
1919
RECT rc;
2020
int cbHeight;
21-
HMENU hMenu, hFile, hQuery, hView;
21+
HMENU hMenu, hFile, hQuery;
2222
TBBUTTON tbButtons[11];
2323

2424
g_hBrushWhite = CreateSolidBrush(RGB(255, 255, 255));
@@ -29,13 +29,16 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
2929
hMenu = CreateMenu();
3030
hFile = CreatePopupMenu();
3131
{
32-
HMENU hExport, hImport;
33-
AppendMenuW(hFile, MF_STRING, IDM_NEW, L"&New Database...");
34-
AppendMenuW(hFile, MF_STRING, IDM_OPEN, L"&Open Database...");
35-
AppendMenuW(hFile, MF_STRING, IDM_CLOSE, L"&Close Database");
36-
AppendMenuW(hFile, MF_SEPARATOR, 0, NULL);
37-
AppendMenuW(hFile, MF_STRING, IDM_OPENQUERY, L"Open &Query...\tCtrl+O");
38-
AppendMenuW(hFile, MF_STRING, IDM_SAVEQUERY, L"&Save Query...\tCtrl+S");
32+
HMENU hDatabase, hQueryFile, hExport, hImport;
33+
hDatabase = CreatePopupMenu();
34+
AppendMenuW(hDatabase, MF_STRING, IDM_NEW, L"&New...");
35+
AppendMenuW(hDatabase, MF_STRING, IDM_OPEN, L"&Open...");
36+
AppendMenuW(hDatabase, MF_STRING, IDM_CLOSE, L"&Close");
37+
AppendMenuW(hFile, MF_POPUP, (UINT)hDatabase, L"&Database");
38+
hQueryFile = CreatePopupMenu();
39+
AppendMenuW(hQueryFile, MF_STRING, IDM_OPENQUERY, L"&Open...\tCtrl+O");
40+
AppendMenuW(hQueryFile, MF_STRING, IDM_SAVEQUERY, L"&Save...\tCtrl+S");
41+
AppendMenuW(hFile, MF_POPUP, (UINT)hQueryFile, L"&Query");
3942
AppendMenuW(hFile, MF_SEPARATOR, 0, NULL);
4043
hExport = CreatePopupMenu();
4144
AppendMenuW(hExport, MF_STRING, IDM_EXPORTCSV, L"&Results...");
@@ -46,17 +49,14 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
4649
AppendMenuW(hImport, MF_STRING, IDM_IMPORTCEDB, L"CE &Database...");
4750
AppendMenuW(hFile, MF_POPUP, (UINT)hImport, L"&Import");
4851
AppendMenuW(hFile, MF_SEPARATOR, 0, NULL);
52+
AppendMenuW(hFile, MF_STRING, IDM_OPTIONS, L"&Options...");
53+
AppendMenuW(hFile, MF_SEPARATOR, 0, NULL);
4954
AppendMenuW(hFile, MF_STRING, IDM_EXIT, L"E&xit\tAlt+X");
5055
}
5156
AppendMenuW(hMenu, MF_POPUP, (UINT)hFile, L"&File");
5257

53-
hView = CreatePopupMenu();
54-
AppendMenuW(hView, MF_STRING | MF_CHECKED, IDM_CLEAR, L"&Clear on Execute");
55-
AppendMenuW(hMenu, MF_POPUP, (UINT)hView, L"&View");
56-
5758
hQuery = CreatePopupMenu();
5859
AppendMenuW(hQuery, MF_STRING, IDM_EXECUTE, L"&Execute\tCtrl+Enter");
59-
AppendMenuW(hQuery, MF_STRING, IDM_EXECATCURSOR, L"Execute at &Cursor");
6060
AppendMenuW(hQuery, MF_SEPARATOR, 0, NULL);
6161
AppendMenuW(hQuery, MF_STRING, IDM_FIND, L"&Find...\tCtrl+F");
6262
AppendMenuW(hQuery, MF_STRING, IDM_FINDNEXT, L"Find &Next\tF3");
@@ -215,16 +215,11 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
215215
case IDM_EXECUTE: ExecuteQuery(); break;
216216
case IDM_FIND: DoFind(); break;
217217
case IDM_FINDNEXT: DoFindNext(); break;
218-
case IDM_EXECATCURSOR:
219-
g_execAtCursor = !g_execAtCursor;
220-
CheckMenuItem(g_hMenu, IDM_EXECATCURSOR, g_execAtCursor ? MF_CHECKED : MF_UNCHECKED);
221-
break;
222218
case IDM_ABOUT:
223219
DoAbout();
224220
break;
225-
case IDM_CLEAR:
226-
g_clearOnExec = !g_clearOnExec;
227-
CheckMenuItem(g_hMenu, IDM_CLEAR, g_clearOnExec ? MF_CHECKED : MF_UNCHECKED);
221+
case IDM_OPTIONS:
222+
DoOptions();
228223
break;
229224
case IDM_VIEWQUERY:
230225
SwitchView(0);

0 commit comments

Comments
 (0)