#include #include #include #include #include #include #include #include #include #include "sqlshell.h" #include "sqlcmd.h" void ReadCommands(VARS *v) { FILE *f; char s[4098]; char *txt; int bytes = 0, len; if ((f = fopen("c:/query.dat", "rt"))) { txt = (char*)malloc(1); while(fgets(s, 4095, f)) { len = strlen(s); s[len - 1] = '\r'; txt = realloc(txt, bytes + len); memcpy(txt + bytes, s, len); bytes += len; } txt = realloc(txt, bytes + 1); *(txt + bytes) = '\0'; fclose(f); SetWindowText(v->hwndSQLCmdWnd, txt); free(txt); } } void WriteCommands(VARS *v) { FILE *f; int i, lines; char buf[4098]; f = fopen("c:/query.dat", "wt"); lines = SendMessage(v->hwndSQLCmdWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); for (i = 0; i < lines; i++) { memset(buf, 0, 4098); *((int*)buf) = 4098; SendMessage(v->hwndSQLCmdWnd, EM_GETLINE, (WPARAM)i, (LPARAM)(LPCSTR)buf); /* strip the carriage return paragraph marker */ buf[strlen(buf) - 1] = '\0'; /* skip last line if empty */ if ((i == (lines - 1)) && (*buf == '\0')) break; fprintf(f, "%s\n", buf); } fclose(f); } void ConfigChildren(VARS *v) { CHARFORMAT cf; PARAFORMAT pf; memset(&cf, 0, sizeof(CHARFORMAT)); cf.cbSize = sizeof(CHARFORMAT); cf.dwMask = CFM_FACE | CFM_SIZE | CFM_BOLD; cf.dwEffects = 0; cf.yHeight = 8 * 20; strcpy(cf.szFaceName, "Andale Mono"); memset(&pf, 0, sizeof(PARAFORMAT)); pf.cbSize = sizeof(PARAFORMAT); pf.dwMask = PFM_STARTINDENT; pf.dxStartIndent = 50; SendMessage(v->hwndSQLResWnd, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf); SendMessage(v->hwndSQLResWnd, EM_SETPARAFORMAT, (WPARAM)0, (LPARAM)&pf); SendMessage(v->hwndSQLCmdWnd, EM_SETBKGNDCOLOR, (WPARAM)FALSE, (LPARAM)0x00e0ffff); SendMessage(v->hwndSQLCmdWnd, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf); SendMessage(v->hwndSQLCmdWnd, EM_SETPARAFORMAT, (WPARAM)0, (LPARAM)&pf); ReadCommands(v); } void ResizeChildren(VARS *v) { RECT mainRect, statusRect; GetClientRect(v->hwndMain, &mainRect); GetWindowRect(v->hwndStatus, &statusRect); MoveWindow(v->hwndSQLResWnd, 0, 0, mainRect.right, mainRect.bottom - 161, TRUE); MoveWindow(v->hwndSQLCmdWnd, 0, mainRect.bottom - 160, mainRect.right, 160 - (statusRect.bottom - statusRect.top), TRUE); } BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: return TRUE; case WM_COMMAND : switch (LOWORD (wParam)) { case IDOK: case IDCANCEL: EndDialog (hDlg, 0); return TRUE ; } break; } return FALSE; } BOOL CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { WORD id, wScrollNotify; static VARS *v; switch (msg) { case WM_INITDIALOG: v = (VARS**)lParam; break; case WM_SIZE: ConfigChildren(v); ResizeChildren(v); SendMessage(v->hwndStatus, WM_SIZE, wParam, lParam); SetStatusParts(v->hwndStatus, 2); break; case WM_COMMAND: id = LOWORD(wParam); switch (id) { case IDM_EXIT: PostQuitMessage(0); break; case IDM_EXECUTE: if (v->sqlCmdActive) break; _beginthread (ExecuteCmd, 0, (void*)v); break; case IDM_ABOUT: DialogBox (v->hInst, "AboutBox", v->hwndMain, AboutDlgProc); break; case ID_SB_PAGEUP: case ID_SB_PAGEDOWN: case ID_SB_TOP: case ID_SB_BOTTOM: if (v->sqlCmdActive) break; switch (id) { case ID_SB_PAGEUP: wScrollNotify = SB_PAGEUP; break; case ID_SB_PAGEDOWN: wScrollNotify = SB_PAGEDOWN; break; case ID_SB_TOP: wScrollNotify = SB_TOP; break; case ID_SB_BOTTOM: wScrollNotify = SB_BOTTOM; break; } SendMessage(v->hwndSQLResWnd, WM_VSCROLL, MAKELONG(wScrollNotify, 0), 0L); break; case ID_FOCUS_NEXT: SendMessage(v->hwndMain, WM_NEXTDLGCTL, (WPARAM)0, MAKELPARAM(FALSE, 0)); break; case ID_FOCUS_PREV: SendMessage(v->hwndMain, WM_NEXTDLGCTL, (WPARAM)1, MAKELPARAM(FALSE, 0)); break; default: return FALSE; } break; case WM_CLOSE: if (! Prompt(hwnd, "SQLShell", "Do you really want to quit?", 1)) break; DestroyWindow(hwnd); break; case WM_DESTROY: SavePlacement(hwnd, "Software\\SQLShell"); SQLDisconnect(0); WriteCommands(v); PostQuitMessage(0); break; default: return FALSE; } return TRUE; } static VARS* Init(HINSTANCE hInst, LPSTR cmdLine, INT nCmdShow) { WNDCLASS wc; LOGINDATA loginData; VARS *v, **pv; char tmp[80]; RECT rect; if (! Login(&loginData)) return NULL; if (! SQLConnect(0, loginData.host, loginData.user, loginData.passwd)) return NULL; if (! LoadLibrary("riched20.dll")) { MessageBox(NULL, "Couldn't load riched20.dll", "SQLShell", MB_OK | MB_ICONERROR); return NULL; } v = (VARS*)malloc(sizeof(VARS)); v->hInst = hInst; v->sqlCmdActive = FALSE; GetClassInfo(NULL, "#32770", &wc); wc.hIcon = LoadIcon(hInst, "Icon"); wc.lpszMenuName = "MainMenu"; wc.lpszClassName = "MainWndClass"; if (! RegisterClass(&wc)) return NULL; v->hwndMain = CreateDialogParam(v->hInst, "MainWnd", 0, MainWndProc, (LPARAM)v); v->hwndSQLResWnd = GetDlgItem(v->hwndMain, IDC_EDIT1); v->hwndSQLCmdWnd = GetDlgItem(v->hwndMain, IDC_EDIT2); v->hwndStatus = GetDlgItem(v->hwndMain, IDC_STATUS); if (GetPlacement("Software\\SQLShell", &rect)) { MoveWindow(v->hwndMain, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, TRUE); } ShowWindow(v->hwndMain, nCmdShow); SetFocus(v->hwndSQLCmdWnd); return v; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow) { MSG msg; VARS *v; HACCEL hAccel; if (! (v = Init(hInstance, lpCmdLine, nCmdShow))) return 0; if (! (hAccel = LoadAccelerators(v->hInst, "Accel"))) return 0; while (GetMessage (&msg, NULL, 0, 0)) { if (! TranslateAccelerator(v->hwndMain, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } .