- dia->cy = ((nelems + 1)/2 + 1) * HEIGHT + 30;
- DialogBoxIndirectParam(NULL, dia, NULL, DlgProc, (LPARAM) ctxp);
- guard--;
-}
-
-static BOOL CALLBACK
-LogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- static struct shttpd_ctx *ctx;
- static HWND hStatus;
- HWND hEdit;
- RECT rect, rect2, rect3, rect4;
- int len, up, widths[] = {120, 220, 330, 460, -1};
- char text[256], buf[1024 * 64];
-
- switch (msg) {
-
- case WM_CLOSE:
- KillTimer(hDlg, ID_TIMER);
- DestroyWindow(hDlg);
- break;
-
- case WM_APP:
- hEdit = GetDlgItem(hDlg, ID_LOG);
- len = GetWindowText(hEdit, buf, sizeof(buf));
- if (len > sizeof(buf) * 4 / 5)
- len = sizeof(buf) * 4 / 5;
- my_snprintf(buf + len, sizeof(buf) - len,
- "%s\r\n", (char *) lParam);
- SetWindowText(hEdit, buf);
- SendMessage(hEdit, WM_VSCROLL, SB_BOTTOM, 0);
- break;
-
- case WM_TIMER:
- /* Print statistics on a status bar */
- up = current_time - ctx->start_time;
- (void) my_snprintf(text, sizeof(text),
- " Up: %3d h %2d min %2d sec",
- up / 3600, up / 60 % 60, up % 60);
- SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM) text);
- (void) my_snprintf(text, sizeof(text),
- " Requests: %u", ctx->nrequests);
- SendMessage(hStatus, SB_SETTEXT, 1, (LPARAM) text);
- (void) my_snprintf(text, sizeof(text),
- " Sent: %4.2f Mb", (double) ctx->out / 1048576);
- SendMessage(hStatus, SB_SETTEXT, 2, (LPARAM) text);
- (void) my_snprintf(text, sizeof(text),
- " Received: %4.2f Mb", (double) ctx->in / 1048576);
- SendMessage(hStatus, SB_SETTEXT, 3, (LPARAM) text);
- break;
-
- case WM_INITDIALOG:
- ctx = (struct shttpd_ctx *) lParam;
- SendMessage(hDlg,WM_SETICON,(WPARAM)ICON_SMALL,(LPARAM)hIcon);
- SendMessage(hDlg,WM_SETICON,(WPARAM)ICON_BIG,(LPARAM)hIcon);
- hStatus = CreateStatusWindow(WS_CHILD | WS_VISIBLE,
- "", hDlg, ID_STATUS);
- SendMessage(hStatus, SB_SETPARTS, 5, (LPARAM) widths);
- SendMessage(hStatus, SB_SETTEXT, 4, (LPARAM) " Running");
- SetWindowText(hDlg, "SHTTPD web server log");
- SetTimer(hDlg, ID_TIMER, 1000, NULL);
- GetWindowRect(GetDesktopWindow(), &rect3);
- GetWindowRect(hDlg, &rect4);
- GetClientRect(hDlg, &rect);
- GetClientRect(hStatus, &rect2);
- SetWindowPos(GetDlgItem(hDlg, ID_LOG), 0,
- 0, 0, rect.right, rect.bottom - rect2.bottom, 0);
- SetWindowPos(hDlg, HWND_TOPMOST,
- rect3.right - (rect4.right - rect4.left),
- rect3.bottom - (rect4.bottom - rect4.top) - 30,
- 0, 0, SWP_NOSIZE);
- SetFocus(hStatus);
- SendMessage(hDlg, WM_TIMER, 0, 0);
- hLog = hDlg;
- break;
- default:
- break;
- }
-
-
- return (FALSE);
-}
-
-static void
-show_log_window(struct shttpd_ctx *ctx)
-{
- unsigned char mem[4096], *p;
- DWORD style;
- DLGTEMPLATE *dia = (DLGTEMPLATE *) mem;
- WORD cl, x, y, width, nelems = 0;
-
- static struct {
- DLGTEMPLATE template; /* 18 bytes */
- WORD menu, class;
- wchar_t caption[1];
- WORD fontsiz;
- wchar_t fontface[7];
- } dialog_header = {{WS_CAPTION | WS_POPUP | WS_VISIBLE | WS_SYSMENU |
- DS_SETFONT | WS_DLGFRAME, WS_EX_TOOLWINDOW,
- 0, 200, 200, 400, 100}, 0, 0, L"", 8, L"Tahoma"};
-
- if (hLog != NULL)
- return;
-
- (void) memset(mem, 0, sizeof(mem));
- (void) memcpy(mem, &dialog_header, sizeof(dialog_header));
- p = mem + sizeof(dialog_header);
-
- add_control(&p, dia, 0x81, ID_LOG, WS_CHILD | WS_VISIBLE |
- WS_BORDER | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL |
- ES_READONLY, 5, 5, WIDTH - 10, 60, "");
-
- DialogBoxIndirectParam(NULL, dia, NULL, LogProc, (LPARAM) ctx);
-
- hLog = NULL;
-}
-
-static LRESULT CALLBACK
-WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- static NOTIFYICONDATA ni;
- static struct shttpd_ctx *ctx;
- DWORD tid; /* Thread ID */
- HMENU hMenu;
- POINT pt;
-
- switch (msg) {
- case WM_CREATE:
- ctx = ((CREATESTRUCT *) lParam)->lpCreateParams;
- memset(&ni, 0, sizeof(ni));
- ni.cbSize = sizeof(ni);
- ni.uID = ID_TRAYICON;
- ni.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
- ni.hIcon = hIcon;
- ni.hWnd = hWnd;
- my_snprintf(ni.szTip, sizeof(ni.szTip), "SHTTPD web server");
- ni.uCallbackMessage = WM_USER;
- Shell_NotifyIcon(NIM_ADD, &ni);
- ctx->ev[0] = CreateEvent(0, TRUE, FALSE, 0);
- ctx->ev[1] = CreateEvent(0, TRUE, FALSE, 0);
- _beginthread(run_server, 0, ctx);
- break;
- case WM_CLOSE:
- Shell_NotifyIcon(NIM_DELETE, &ni);
- PostQuitMessage(0);
- break;
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case ID_SETTINGS:
- show_settings_dialog(&ctx);
- break;
- case ID_QUIT:
- SendMessage(hWnd, WM_CLOSE, wParam, lParam);
- PostQuitMessage(0);
- break;
- case ID_SHOWLOG:
- show_log_window(ctx);
- break;
- }
- break;
- case WM_USER:
- switch (lParam) {
- case WM_RBUTTONUP:
- case WM_LBUTTONUP:
- case WM_LBUTTONDBLCLK:
- hMenu = CreatePopupMenu();
- AppendMenu(hMenu, 0, ID_SETTINGS, "Settings");
- AppendMenu(hMenu, 0, ID_SHOWLOG, "Show Log");
- AppendMenu(hMenu, 0, ID_QUIT, "Exit SHTTPD");
- GetCursorPos(&pt);
- TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
- DestroyMenu(hMenu);
- break;
- }
- break;
- }