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

Last change on this file since 253 was 253, checked in by Nishi, on Oct 4, 2024 at 12:13:36 PM

fix stuff

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