source: Main/trunk/Server/gui.c@ 372

Last change on this file since 372 was 291, checked in by Nishi, on Oct 9, 2024 at 12:14:51 PM

fix stuff

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