[244] | 1 | /* $Id: gui.c 254 2024-10-04 04:08:33Z nishi $ */
|
---|
| 2 |
|
---|
| 3 | #include "../config.h"
|
---|
| 4 |
|
---|
| 5 | #include "gui.h"
|
---|
[253] | 6 | #include "tw_server.h"
|
---|
[244] | 7 |
|
---|
[253] | 8 | #include <cm_log.h>
|
---|
| 9 |
|
---|
| 10 | #include <stdio.h>
|
---|
[244] | 11 | #include <windows.h>
|
---|
[253] | 12 | #include <process.h>
|
---|
[244] | 13 | #include <commctrl.h>
|
---|
| 14 |
|
---|
| 15 | HINSTANCE hInst;
|
---|
| 16 | HBRUSH pbtewi_brush;
|
---|
| 17 | HWND logarea;
|
---|
| 18 | HWND button_start;
|
---|
| 19 | HWND button_stop;
|
---|
| 20 | HWND button_about;
|
---|
[254] | 21 | HWND button_reset;
|
---|
| 22 | HWND button_exit;
|
---|
[244] | 23 | HWND status;
|
---|
[253] | 24 | HFONT monospace;
|
---|
[244] | 25 | BOOL tewi_alive;
|
---|
| 26 | BOOL was_starting;
|
---|
| 27 | BOOL exiting;
|
---|
[253] | 28 | BOOL idle;
|
---|
| 29 | extern FILE* logfile;
|
---|
| 30 | extern int running;
|
---|
[244] | 31 |
|
---|
| 32 | #define WINWIDTH(rc) (rc.right - rc.left)
|
---|
| 33 | #define WINHEIGHT(rc) (rc.bottom - rc.top)
|
---|
| 34 |
|
---|
[253] | 35 | int startup(int argc, char** argv);
|
---|
[244] | 36 |
|
---|
[254] | 37 | void ShowBitmapSize(HWND hWnd, HDC hdc, const char* name, int x, int y, int w, int h) {
|
---|
[244] | 38 | HBITMAP hBitmap = LoadBitmap(hInst, name);
|
---|
| 39 | BITMAP bmp;
|
---|
| 40 | HDC hmdc;
|
---|
| 41 | GetObject(hBitmap, sizeof(bmp), &bmp);
|
---|
| 42 | hmdc = CreateCompatibleDC(hdc);
|
---|
| 43 | SelectObject(hmdc, hBitmap);
|
---|
[254] | 44 | if(w == 0 && h == 0) {
|
---|
[244] | 45 | StretchBlt(hdc, x, y, bmp.bmWidth, bmp.bmHeight, hmdc, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
|
---|
[254] | 46 | } else {
|
---|
[244] | 47 | StretchBlt(hdc, x, y, w, h, hmdc, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
|
---|
| 48 | }
|
---|
| 49 | DeleteDC(hmdc);
|
---|
| 50 | DeleteObject(hBitmap);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[254] | 53 | void ShowBitmap(HWND hWnd, HDC hdc, const char* name, int x, int y) { ShowBitmapSize(hWnd, hdc, name, x, y, 0, 0); }
|
---|
[244] | 54 |
|
---|
[253] | 55 | int max = 0;
|
---|
[254] | 56 | void AddLog(const char* str) {
|
---|
[253] | 57 | HDC hdc;
|
---|
| 58 | PAINTSTRUCT ps;
|
---|
| 59 | SIZE sz;
|
---|
| 60 |
|
---|
| 61 | SendMessage(logarea, LB_ADDSTRING, 0, (LPARAM)str);
|
---|
| 62 |
|
---|
| 63 | hdc = CreateCompatibleDC(NULL);
|
---|
| 64 | SelectObject(hdc, monospace);
|
---|
| 65 | GetTextExtentPoint32(hdc, str, strlen(str), &sz);
|
---|
| 66 | DeleteDC(hdc);
|
---|
[254] | 67 |
|
---|
| 68 | if(max < sz.cx) {
|
---|
[253] | 69 | max = sz.cx;
|
---|
| 70 | SendMessage(logarea, LB_SETHORIZONTALEXTENT, max, 0);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[254] | 74 | LRESULT CALLBACK VersionDialog(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
|
---|
| 75 | if(msg == WM_COMMAND) {
|
---|
[244] | 76 | if(LOWORD(wp) == IDOK) EndDialog(hWnd, IDOK);
|
---|
[254] | 77 | } else if(msg == WM_PAINT) {
|
---|
[244] | 78 | HDC hdc;
|
---|
| 79 | PAINTSTRUCT ps;
|
---|
| 80 | RECT size;
|
---|
| 81 |
|
---|
| 82 | size.left = size.top = 5;
|
---|
| 83 | size.right = size.bottom = 32 + 5;
|
---|
| 84 | MapDialogRect(hWnd, &size);
|
---|
| 85 |
|
---|
| 86 | hdc = BeginPaint(hWnd, &ps);
|
---|
| 87 | ShowBitmapSize(hWnd, hdc, "TEWILOGO", size.left, size.top, WINWIDTH(size), WINWIDTH(size));
|
---|
| 88 | EndPaint(hWnd, &ps);
|
---|
[254] | 89 | } else if(msg == WM_CTLCOLORDLG || msg == WM_CTLCOLORSTATIC) {
|
---|
[249] | 90 | HDC dc = (HDC)wp;
|
---|
| 91 | SetBkMode(dc, TRANSPARENT);
|
---|
[253] | 92 | return (LRESULT)GetSysColorBrush(COLOR_MENU);
|
---|
[254] | 93 | } else {
|
---|
[244] | 94 | return FALSE;
|
---|
| 95 | }
|
---|
| 96 | return TRUE;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[254] | 99 | void tewi_thread(void* ptr) {
|
---|
[253] | 100 | int st = startup(0, NULL);
|
---|
[244] | 101 | was_starting = TRUE;
|
---|
[254] | 102 | if(st == -1) {
|
---|
[253] | 103 | tewi_alive = TRUE;
|
---|
| 104 | idle = FALSE;
|
---|
[254] | 105 | } else {
|
---|
[253] | 106 | cm_force_log("Config error");
|
---|
| 107 | idle = FALSE;
|
---|
| 108 | _endthread();
|
---|
| 109 | }
|
---|
| 110 | running = 1;
|
---|
| 111 | tw_server_loop();
|
---|
| 112 | tewi_alive = FALSE;
|
---|
| 113 | was_starting = TRUE;
|
---|
| 114 | idle = FALSE;
|
---|
| 115 | _endthread();
|
---|
[244] | 116 | }
|
---|
| 117 |
|
---|
[254] | 118 | void StartTewi(void) {
|
---|
[253] | 119 | EnableWindow(button_start, FALSE);
|
---|
| 120 | EnableWindow(button_stop, FALSE);
|
---|
| 121 | _beginthread(tewi_thread, 0, NULL);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[254] | 124 | void StopTewi(void) {
|
---|
[253] | 125 | EnableWindow(button_start, FALSE);
|
---|
| 126 | EnableWindow(button_stop, FALSE);
|
---|
| 127 | running = 0;
|
---|
[244] | 128 | }
|
---|
| 129 |
|
---|
[254] | 130 | LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
|
---|
| 131 | if(msg == WM_COMMAND) {
|
---|
[244] | 132 | int trig = LOWORD(wp);
|
---|
| 133 | int ev = HIWORD(wp);
|
---|
[254] | 134 | if(trig == GUI_BUTTON_ABOUT) {
|
---|
| 135 | if(ev == BN_CLICKED) {
|
---|
[249] | 136 | DialogBox(hInst, "VERSIONDLG", hWnd, (DLGPROC)VersionDialog);
|
---|
[244] | 137 | }
|
---|
[254] | 138 | } else if(trig == GUI_BUTTON_START) {
|
---|
| 139 | if(ev == BN_CLICKED) {
|
---|
| 140 | SendMessage(status, SB_SETTEXT, 0, (LPARAM) "Starting Tewi HTTPd");
|
---|
[244] | 141 | StartTewi();
|
---|
| 142 | }
|
---|
[254] | 143 | } else if(trig == GUI_BUTTON_STOP) {
|
---|
| 144 | if(ev == BN_CLICKED) {
|
---|
| 145 | SendMessage(status, SB_SETTEXT, 0, (LPARAM) "Stopping Tewi HTTPd");
|
---|
[244] | 146 | StopTewi();
|
---|
| 147 | }
|
---|
[254] | 148 | } else if(trig == GUI_BUTTON_RESET) {
|
---|
| 149 | if(ev == BN_CLICKED) {
|
---|
| 150 | SendMessage(logarea, LB_RESETCONTENT, 0, 0);
|
---|
| 151 | max = 0;
|
---|
| 152 | SendMessage(logarea, LB_SETHORIZONTALEXTENT, max, 0);
|
---|
| 153 | }
|
---|
| 154 | } else if(trig == GUI_BUTTON_EXIT) {
|
---|
| 155 | if(ev == BN_CLICKED) {
|
---|
| 156 | if(tewi_alive) {
|
---|
| 157 | SendMessage(status, SB_SETTEXT, 0, (LPARAM) "Stopping Tewi HTTPd");
|
---|
[244] | 158 | StopTewi();
|
---|
| 159 | exiting = TRUE;
|
---|
[254] | 160 | } else {
|
---|
[244] | 161 | SendMessage(hWnd, WM_CLOSE, 0, 0);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
[254] | 164 | } else if(trig == GUI_LOG) {
|
---|
[244] | 165 | }
|
---|
[254] | 166 | } else if(msg == WM_CLOSE) {
|
---|
[244] | 167 | DestroyWindow(hWnd);
|
---|
[254] | 168 | } else if(msg == WM_DESTROY) {
|
---|
[244] | 169 | DeleteObject(pbtewi_brush);
|
---|
| 170 | PostQuitMessage(0);
|
---|
[254] | 171 | } else if(msg == WM_CREATE) {
|
---|
[244] | 172 | RECT rc, src;
|
---|
| 173 | GetClientRect(hWnd, &rc);
|
---|
| 174 |
|
---|
| 175 | InitCommonControls();
|
---|
| 176 |
|
---|
[253] | 177 | monospace = (HFONT)GetStockObject(SYSTEM_FIXED_FONT);
|
---|
| 178 |
|
---|
[244] | 179 | status = CreateStatusWindow(WS_CHILD | WS_VISIBLE | CCS_BOTTOM, NULL, hWnd, GUI_STATUS);
|
---|
| 180 | SendMessage(status, SB_SIMPLE, 0, 0);
|
---|
[254] | 181 | SendMessage(status, SB_SETTEXT, 0, (LPARAM) "Welcome to Tewi HTTPd");
|
---|
[244] | 182 | SendMessage(status, SB_GETRECT, 0, (LPARAM)&src);
|
---|
| 183 |
|
---|
| 184 | pbtewi_brush = CreateSolidBrush(RGB(0xf7, 0xc9, 0xf3));
|
---|
| 185 | button_start = CreateWindow("BUTTON", "&Start", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, WINWIDTH(rc) - 100, 20 * 0, 100, 20, hWnd, (HMENU)GUI_BUTTON_START, hInst, NULL);
|
---|
| 186 | button_stop = CreateWindow("BUTTON", "S&top", WS_CHILD | WS_VISIBLE | WS_DISABLED | BS_PUSHBUTTON, WINWIDTH(rc) - 100, 20 * 1, 100, 20, hWnd, (HMENU)GUI_BUTTON_STOP, hInst, NULL);
|
---|
| 187 | button_about = CreateWindow("BUTTON", "&About", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, WINWIDTH(rc) - 100, 20 * 2, 100, 20, hWnd, (HMENU)GUI_BUTTON_ABOUT, hInst, NULL);
|
---|
[254] | 188 | button_reset = CreateWindow("BUTTON", "&Reset", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, WINWIDTH(rc) - 100, WINHEIGHT(rc) - WINHEIGHT(src) - 20 - 20, 100, 20, hWnd, (HMENU)GUI_BUTTON_RESET, hInst, NULL);
|
---|
| 189 | button_exit = CreateWindow("BUTTON", "E&xit", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, WINWIDTH(rc) - 100, WINHEIGHT(rc) - WINHEIGHT(src) - 20, 100, 20, hWnd, (HMENU)GUI_BUTTON_EXIT, hInst, NULL);
|
---|
[253] | 190 | logarea = CreateWindow("LISTBOX", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | LBS_NOINTEGRALHEIGHT | LBS_NOSEL, 0, 40, WINWIDTH(rc) - 100, WINHEIGHT(rc) - 40 - WINHEIGHT(src), hWnd, (HMENU)GUI_LOG, hInst, NULL);
|
---|
| 191 |
|
---|
| 192 | SendMessage(logarea, WM_SETFONT, (WPARAM)monospace, TRUE);
|
---|
| 193 |
|
---|
[244] | 194 | SetTimer(hWnd, TIMER_WATCH_TEWI, 100, NULL);
|
---|
[254] | 195 | } else if(msg == WM_TIMER) {
|
---|
| 196 | if(wp == TIMER_WATCH_TEWI) {
|
---|
| 197 | if(idle) {
|
---|
| 198 | } else if(tewi_alive) {
|
---|
| 199 | if(was_starting) {
|
---|
[244] | 200 | was_starting = FALSE;
|
---|
[254] | 201 | SendMessage(status, SB_SETTEXT, 0, (LPARAM) "Started Tewi HTTPd");
|
---|
[244] | 202 | }
|
---|
| 203 | EnableWindow(button_start, FALSE);
|
---|
| 204 | EnableWindow(button_stop, TRUE);
|
---|
[253] | 205 | idle = TRUE;
|
---|
[254] | 206 | } else {
|
---|
| 207 | if(was_starting) {
|
---|
[244] | 208 | was_starting = FALSE;
|
---|
[254] | 209 | SendMessage(status, SB_SETTEXT, 0, (LPARAM) "Stopped Tewi HTTPd");
|
---|
[244] | 210 | }
|
---|
| 211 | EnableWindow(button_start, TRUE);
|
---|
| 212 | EnableWindow(button_stop, FALSE);
|
---|
[254] | 213 | if(exiting) {
|
---|
[244] | 214 | KillTimer(hWnd, TIMER_WATCH_TEWI);
|
---|
| 215 | SendMessage(hWnd, WM_CLOSE, 0, 0);
|
---|
| 216 | }
|
---|
[253] | 217 | idle = TRUE;
|
---|
[244] | 218 | }
|
---|
| 219 | }
|
---|
[254] | 220 | } else if(msg == WM_PAINT) {
|
---|
[244] | 221 | HDC hdc;
|
---|
| 222 | PAINTSTRUCT ps;
|
---|
| 223 | RECT rc;
|
---|
| 224 | RECT fill;
|
---|
| 225 |
|
---|
| 226 | GetClientRect(hWnd, &rc);
|
---|
| 227 | hdc = BeginPaint(hWnd, &ps);
|
---|
| 228 | SetRect(&fill, 0, 0, WINWIDTH(rc), 40);
|
---|
| 229 | FillRect(hdc, &fill, pbtewi_brush);
|
---|
| 230 | ShowBitmap(hWnd, hdc, "PBTEWI", 0, 0);
|
---|
| 231 | EndPaint(hWnd, &ps);
|
---|
[254] | 232 | } else {
|
---|
[244] | 233 | return DefWindowProc(hWnd, msg, wp, lp);
|
---|
| 234 | }
|
---|
| 235 | return 0;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[254] | 238 | BOOL InitApp(void) {
|
---|
[253] | 239 | WNDCLASSEX wc;
|
---|
| 240 | wc.cbSize = sizeof(WNDCLASSEX);
|
---|
[244] | 241 | wc.style = CS_HREDRAW | CS_VREDRAW;
|
---|
| 242 | wc.lpfnWndProc = WndProc;
|
---|
| 243 | wc.cbClsExtra = 0;
|
---|
| 244 | wc.cbWndExtra = 0;
|
---|
| 245 | wc.hInstance = hInst;
|
---|
| 246 | wc.hIcon = LoadIcon(hInst, "TEWI");
|
---|
| 247 | wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
---|
| 248 | wc.hbrBackground = GetSysColorBrush(COLOR_MENU);
|
---|
| 249 | wc.lpszMenuName = NULL;
|
---|
| 250 | wc.lpszClassName = "tewihttpd";
|
---|
[253] | 251 | wc.hIconSm = LoadIcon(hInst, "TEWI");
|
---|
| 252 | return RegisterClassEx(&wc);
|
---|
[244] | 253 | }
|
---|
| 254 |
|
---|
[254] | 255 | BOOL InitWindow(int nCmdShow) {
|
---|
[244] | 256 | HWND hWnd;
|
---|
| 257 | RECT deskrc, rc;
|
---|
| 258 | HWND hDeskWnd = GetDesktopWindow();
|
---|
| 259 | GetWindowRect(hDeskWnd, &deskrc);
|
---|
[253] | 260 | hWnd = CreateWindow("tewihttpd", "Tewi HTTPd", (WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME) ^ WS_MAXIMIZEBOX, 0, 0, 600, 400, NULL, 0, hInst, NULL);
|
---|
[244] | 261 |
|
---|
[254] | 262 | if(!hWnd) {
|
---|
[244] | 263 | return FALSE;
|
---|
| 264 | }
|
---|
| 265 | GetWindowRect(hWnd, &rc);
|
---|
| 266 | SetWindowPos(hWnd, HWND_TOP, (deskrc.right - (rc.right - rc.left)) / 2, (deskrc.bottom - (rc.bottom - rc.top)) / 2, rc.right - rc.left, rc.bottom - rc.top, SWP_SHOWWINDOW);
|
---|
| 267 | ShowWindow(hWnd, nCmdShow);
|
---|
| 268 | UpdateWindow(hWnd);
|
---|
| 269 | return TRUE;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[254] | 272 | int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst, LPSTR lpsCmdLine, int nCmdShow) {
|
---|
[244] | 273 | MSG msg;
|
---|
| 274 | BOOL bret;
|
---|
| 275 | hInst = hCurInst;
|
---|
| 276 | tewi_alive = FALSE;
|
---|
| 277 | was_starting = FALSE;
|
---|
| 278 | exiting = FALSE;
|
---|
[253] | 279 | idle = TRUE;
|
---|
| 280 | logfile = stderr;
|
---|
[254] | 281 | if(!InitApp()) {
|
---|
[244] | 282 | return FALSE;
|
---|
| 283 | }
|
---|
[254] | 284 | if(!InitWindow(nCmdShow)) {
|
---|
[244] | 285 | return FALSE;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[254] | 288 | while((bret = GetMessage(&msg, NULL, 0, 0)) != 0) {
|
---|
[244] | 289 | if(bret == -1) {
|
---|
| 290 | break;
|
---|
| 291 | } else {
|
---|
| 292 | TranslateMessage(&msg);
|
---|
| 293 | DispatchMessage(&msg);
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 | return (int)msg.wParam;
|
---|
| 297 | }
|
---|