Subversion Repositories Tewi

Rev

Rev 249 | Rev 255 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 249 Rev 253
Line 1... Line 1...
1
/* $Id: gui.c 249 2024-10-03 20:59:55Z nishi $ */
1
/* $Id: gui.c 253 2024-10-04 03:13:36Z nishi $ */
2
 
2
 
3
#include "../config.h"
3
#include "../config.h"
4
 
4
 
5
#include "gui.h"
5
#include "gui.h"
-
 
6
#include "tw_server.h"
6
 
7
 
-
 
8
#include <cm_log.h>
-
 
9
 
-
 
10
#include <stdio.h>
7
#include <windows.h>
11
#include <windows.h>
-
 
12
#include <process.h>
8
#include <commctrl.h>
13
#include <commctrl.h>
9
 
14
 
10
HINSTANCE hInst;
15
HINSTANCE hInst;
11
HBRUSH pbtewi_brush;
16
HBRUSH pbtewi_brush;
12
HWND logarea;
17
HWND logarea;
13
HWND button_start;
18
HWND button_start;
14
HWND button_stop;
19
HWND button_stop;
15
HWND button_about;
20
HWND button_about;
16
HWND status;
21
HWND status;
-
 
22
HFONT monospace;
17
BOOL tewi_alive;
23
BOOL tewi_alive;
18
BOOL was_starting;
24
BOOL was_starting;
19
BOOL exiting;
25
BOOL exiting;
-
 
26
BOOL idle;
-
 
27
extern FILE* logfile;
-
 
28
extern int running;
20
 
29
 
21
#define WINWIDTH(rc) (rc.right - rc.left)
30
#define WINWIDTH(rc) (rc.right - rc.left)
22
#define WINHEIGHT(rc) (rc.bottom - rc.top)
31
#define WINHEIGHT(rc) (rc.bottom - rc.top)
23
 
32
 
24
#define DIALOG_X(n) (HIWORD(GetDialogBaseUnits()) * n)
-
 
25
#define DIALOG_Y(n) (LOWORD(GetDialogBaseUnits()) * n)
33
int startup(int argc, char** argv);
26
 
34
 
27
void ShowBitmapSize(HWND hWnd, HDC hdc, const char* name, int x, int y, int w, int h){
35
void ShowBitmapSize(HWND hWnd, HDC hdc, const char* name, int x, int y, int w, int h){
28
	HBITMAP hBitmap = LoadBitmap(hInst, name);
36
	HBITMAP hBitmap = LoadBitmap(hInst, name);
29
	BITMAP bmp;
37
	BITMAP bmp;
30
	HDC hmdc;
38
	HDC hmdc;
Line 42... Line 50...
42
 
50
 
43
void ShowBitmap(HWND hWnd, HDC hdc, const char* name, int x, int y){
51
void ShowBitmap(HWND hWnd, HDC hdc, const char* name, int x, int y){
44
	ShowBitmapSize(hWnd, hdc, name, x, y, 0, 0);
52
	ShowBitmapSize(hWnd, hdc, name, x, y, 0, 0);
45
}
53
}
46
 
54
 
-
 
55
int max = 0;
-
 
56
void 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
 
47
LRESULT CALLBACK VersionDialog(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){
74
LRESULT CALLBACK VersionDialog(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){
48
	if(msg == WM_COMMAND){
75
	if(msg == WM_COMMAND){
49
		if(LOWORD(wp) == IDOK) EndDialog(hWnd, IDOK);
76
		if(LOWORD(wp) == IDOK) EndDialog(hWnd, IDOK);
50
	}else if(msg == WM_PAINT){
77
	}else if(msg == WM_PAINT){
51
		HDC hdc;
78
		HDC hdc;
Line 60... Line 87...
60
		ShowBitmapSize(hWnd, hdc, "TEWILOGO", size.left, size.top, WINWIDTH(size), WINWIDTH(size));
87
		ShowBitmapSize(hWnd, hdc, "TEWILOGO", size.left, size.top, WINWIDTH(size), WINWIDTH(size));
61
		EndPaint(hWnd, &ps);
88
		EndPaint(hWnd, &ps);
62
	}else if(msg == WM_CTLCOLORDLG || msg == WM_CTLCOLORSTATIC){
89
	}else if(msg == WM_CTLCOLORDLG || msg == WM_CTLCOLORSTATIC){
63
		HDC dc = (HDC)wp;
90
		HDC dc = (HDC)wp;
64
		SetBkMode(dc, TRANSPARENT);
91
		SetBkMode(dc, TRANSPARENT);
65
		return GetSysColorBrush(COLOR_MENU);
92
		return (LRESULT)GetSysColorBrush(COLOR_MENU);
66
	}else{
93
	}else{
67
		return FALSE;
94
		return FALSE;
68
	}
95
	}
69
	return TRUE;
96
	return TRUE;
70
}
97
}
71
 
98
 
72
void StartTewi(void){
99
void 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;
73
	was_starting = TRUE;
113
	was_starting = TRUE;
-
 
114
	idle = FALSE;
-
 
115
	_endthread();
-
 
116
}
-
 
117
 
-
 
118
void StartTewi(void){
-
 
119
	EnableWindow(button_start, FALSE);
-
 
120
	EnableWindow(button_stop, FALSE);
-
 
121
	_beginthread(tewi_thread, 0, NULL);
74
}
122
}
75
 
123
 
76
void StopTewi(void){
124
void StopTewi(void){
-
 
125
	EnableWindow(button_start, FALSE);
-
 
126
	EnableWindow(button_stop, FALSE);
77
	was_starting = TRUE;
127
	running = 0;
78
}
128
}
79
 
129
 
80
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){
130
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){
81
	if(msg == WM_COMMAND){
131
	if(msg == WM_COMMAND){
82
		int trig = LOWORD(wp);
132
		int trig = LOWORD(wp);
Line 116... Line 166...
116
		RECT rc, src;
166
		RECT rc, src;
117
		GetClientRect(hWnd, &rc);
167
		GetClientRect(hWnd, &rc);
118
 
168
 
119
		InitCommonControls();
169
		InitCommonControls();
120
 
170
 
-
 
171
		monospace = (HFONT)GetStockObject(SYSTEM_FIXED_FONT);
-
 
172
 
121
		status = CreateStatusWindow(WS_CHILD | WS_VISIBLE | CCS_BOTTOM, NULL, hWnd, GUI_STATUS);
173
		status = CreateStatusWindow(WS_CHILD | WS_VISIBLE | CCS_BOTTOM, NULL, hWnd, GUI_STATUS);
122
		SendMessage(status, SB_SIMPLE, 0, 0);
174
		SendMessage(status, SB_SIMPLE, 0, 0);
123
		SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Welcome to Tewi HTTPd");
175
		SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Welcome to Tewi HTTPd");
124
		SendMessage(status, SB_GETRECT, 0, (LPARAM)&src);
176
		SendMessage(status, SB_GETRECT, 0, (LPARAM)&src);
125
 
177
 
126
		pbtewi_brush = CreateSolidBrush(RGB(0xf7, 0xc9, 0xf3));
178
		pbtewi_brush = CreateSolidBrush(RGB(0xf7, 0xc9, 0xf3));
127
		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);
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);
128
		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);
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);
129
		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);
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);
130
		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);
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);
131
		logarea = CreateWindow("LISTBOX", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL, 0, 40, WINWIDTH(rc) - 100, WINHEIGHT(rc) - 40 - WINHEIGHT(src), hWnd, (HMENU)GUI_LOG, 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
 
132
		SetTimer(hWnd, TIMER_WATCH_TEWI, 100, NULL);
187
		SetTimer(hWnd, TIMER_WATCH_TEWI, 100, NULL);
133
	}else if(msg == WM_TIMER){
188
	}else if(msg == WM_TIMER){
134
		if(wp == TIMER_WATCH_TEWI){
189
		if(wp == TIMER_WATCH_TEWI){
-
 
190
			if(idle){
135
			if(tewi_alive){
191
			}else if(tewi_alive){
136
				if(was_starting){
192
				if(was_starting){
137
					was_starting = FALSE;
193
					was_starting = FALSE;
138
					SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Started Tewi HTTPd");
194
					SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Started Tewi HTTPd");
139
				}
195
				}
140
				EnableWindow(button_start, FALSE);
196
				EnableWindow(button_start, FALSE);
141
				EnableWindow(button_stop, TRUE);
197
				EnableWindow(button_stop, TRUE);
-
 
198
				idle = TRUE;
142
			}else{
199
			}else{
143
				if(was_starting){
200
				if(was_starting){
144
					was_starting = FALSE;
201
					was_starting = FALSE;
145
					SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Stopped Tewi HTTPd");
202
					SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Stopped Tewi HTTPd");
146
				}
203
				}
Line 148... Line 205...
148
				EnableWindow(button_stop, FALSE);
205
				EnableWindow(button_stop, FALSE);
149
				if(exiting){
206
				if(exiting){
150
					KillTimer(hWnd, TIMER_WATCH_TEWI);
207
					KillTimer(hWnd, TIMER_WATCH_TEWI);
151
					SendMessage(hWnd, WM_CLOSE, 0, 0);
208
					SendMessage(hWnd, WM_CLOSE, 0, 0);
152
				}
209
				}
-
 
210
				idle = TRUE;
153
			}
211
			}
154
		}
212
		}
155
	}else if(msg == WM_PAINT){
213
	}else if(msg == WM_PAINT){
156
		HDC hdc;
214
		HDC hdc;
157
		PAINTSTRUCT ps;
215
		PAINTSTRUCT ps;
Line 169... Line 227...
169
	}
227
	}
170
	return 0;
228
	return 0;
171
}
229
}
172
 
230
 
173
BOOL InitApp(void){
231
BOOL InitApp(void){
174
	WNDCLASS wc;
232
	WNDCLASSEX wc;
-
 
233
	wc.cbSize = sizeof(WNDCLASSEX);
175
	wc.style = CS_HREDRAW | CS_VREDRAW;
234
	wc.style = CS_HREDRAW | CS_VREDRAW;
176
	wc.lpfnWndProc = WndProc;
235
	wc.lpfnWndProc = WndProc;
177
	wc.cbClsExtra = 0;
236
	wc.cbClsExtra = 0;
178
	wc.cbWndExtra = 0;
237
	wc.cbWndExtra = 0;
179
	wc.hInstance = hInst;
238
	wc.hInstance = hInst;
180
	wc.hIcon = LoadIcon(hInst, "TEWI");
239
	wc.hIcon = LoadIcon(hInst, "TEWI");
181
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
240
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
182
	wc.hbrBackground = GetSysColorBrush(COLOR_MENU);
241
	wc.hbrBackground = GetSysColorBrush(COLOR_MENU);
183
	wc.lpszMenuName = NULL;
242
	wc.lpszMenuName = NULL;
184
	wc.lpszClassName = "tewihttpd";
243
	wc.lpszClassName = "tewihttpd";
-
 
244
	wc.hIconSm = LoadIcon(hInst, "TEWI");
185
	return RegisterClass(&wc);
245
	return RegisterClassEx(&wc);
186
}
246
}
187
 
247
 
188
BOOL InitWindow(int nCmdShow){
248
BOOL InitWindow(int nCmdShow){
189
	HWND hWnd;
249
	HWND hWnd;
190
	RECT deskrc, rc;
250
	RECT deskrc, rc;
191
	HWND hDeskWnd = GetDesktopWindow();
251
	HWND hDeskWnd = GetDesktopWindow();
192
	GetWindowRect(hDeskWnd, &deskrc);
252
	GetWindowRect(hDeskWnd, &deskrc);
193
	hWnd = CreateWindow("tewihttpd", "Tewi HTTPd", (WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME) ^ WS_MAXIMIZEBOX, 0, 0, 600, 200, NULL, 0, hInst, NULL);
253
	hWnd = CreateWindow("tewihttpd", "Tewi HTTPd", (WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME) ^ WS_MAXIMIZEBOX, 0, 0, 600, 400, NULL, 0, hInst, NULL);
194
 
254
 
195
	if(!hWnd){
255
	if(!hWnd){
196
		return FALSE;
256
		return FALSE;
197
	}
257
	}
198
	GetWindowRect(hWnd, &rc);
258
	GetWindowRect(hWnd, &rc);
Line 207... Line 267...
207
	BOOL bret;
267
	BOOL bret;
208
	hInst = hCurInst;
268
	hInst = hCurInst;
209
	tewi_alive = FALSE;
269
	tewi_alive = FALSE;
210
	was_starting = FALSE;
270
	was_starting = FALSE;
211
	exiting = FALSE;
271
	exiting = FALSE;
-
 
272
	idle = TRUE;
-
 
273
	logfile = stderr;
212
	if(!InitApp()){
274
	if(!InitApp()){
213
		return FALSE;
275
		return FALSE;
214
	}
276
	}
215
	if(!InitWindow(nCmdShow)){
277
	if(!InitWindow(nCmdShow)){
216
		return FALSE;
278
		return FALSE;