Subversion Repositories Tewi

Rev

Rev 249 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
244 nishi 1
/* $Id: gui.c 244 2024-10-03 20:04:15Z nishi $ */
2
 
3
#include "../config.h"
4
 
5
#include "gui.h"
6
 
7
#include <windows.h>
8
#include <commctrl.h>
9
 
10
HINSTANCE hInst;
11
HBRUSH pbtewi_brush;
12
HWND logarea;
13
HWND button_start;
14
HWND button_stop;
15
HWND button_about;
16
HWND status;
17
BOOL tewi_alive;
18
BOOL was_starting;
19
BOOL exiting;
20
 
21
#define WINWIDTH(rc) (rc.right - rc.left)
22
#define WINHEIGHT(rc) (rc.bottom - rc.top)
23
 
24
#define DIALOG_X(n) (HIWORD(GetDialogBaseUnits()) * n)
25
#define DIALOG_Y(n) (LOWORD(GetDialogBaseUnits()) * n)
26
 
27
void ShowBitmapSize(HWND hWnd, HDC hdc, const char* name, int x, int y, int w, int h){
28
	HBITMAP hBitmap = LoadBitmap(hInst, name);
29
	BITMAP bmp;
30
	HDC hmdc;
31
	GetObject(hBitmap, sizeof(bmp), &bmp);
32
	hmdc = CreateCompatibleDC(hdc);
33
	SelectObject(hmdc, hBitmap);
34
	if(w == 0 && h == 0){
35
		StretchBlt(hdc, x, y, bmp.bmWidth, bmp.bmHeight, hmdc, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
36
	}else{
37
		StretchBlt(hdc, x, y, w, h, hmdc, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
38
	}
39
	DeleteDC(hmdc);
40
	DeleteObject(hBitmap);
41
}
42
 
43
void ShowBitmap(HWND hWnd, HDC hdc, const char* name, int x, int y){
44
	ShowBitmapSize(hWnd, hdc, name, x, y, 0, 0);
45
}
46
 
47
LRESULT CALLBACK VersionDialog(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){
48
	if(msg == WM_COMMAND){
49
		if(LOWORD(wp) == IDOK) EndDialog(hWnd, IDOK);
50
	}else if(msg == WM_PAINT){
51
		HDC hdc;
52
		PAINTSTRUCT ps;
53
		RECT size;
54
 
55
		size.left = size.top = 5;
56
		size.right = size.bottom = 32 + 5;
57
		MapDialogRect(hWnd, &size);
58
 
59
		hdc = BeginPaint(hWnd, &ps);
60
		ShowBitmapSize(hWnd, hdc, "TEWILOGO", size.left, size.top, WINWIDTH(size), WINWIDTH(size));
61
		EndPaint(hWnd, &ps);
62
	}else{
63
		return FALSE;
64
	}
65
	return TRUE;
66
}
67
 
68
void StartTewi(void){
69
	was_starting = TRUE;
70
}
71
 
72
void StopTewi(void){
73
	was_starting = TRUE;
74
}
75
 
76
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){
77
	if(msg == WM_COMMAND){
78
		int trig = LOWORD(wp);
79
		int ev = HIWORD(wp);
80
		if(trig == GUI_BUTTON_ABOUT){
81
			if(ev == BN_CLICKED){
82
				DialogBox(hInst, "VERSIONDLG", hWnd, VersionDialog);
83
			}
84
		}else if(trig == GUI_BUTTON_START){
85
			if(ev == BN_CLICKED){
86
				SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Starting Tewi HTTPd");
87
				StartTewi();
88
			}
89
		}else if(trig == GUI_BUTTON_STOP){
90
			if(ev == BN_CLICKED){
91
				SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Stopping Tewi HTTPd");
92
				StopTewi();
93
			}
94
		}else if(trig == GUI_BUTTON_EXIT){
95
			if(ev == BN_CLICKED){
96
				if(tewi_alive){
97
					SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Stopping Tewi HTTPd");
98
					StopTewi();
99
					exiting = TRUE;
100
				}else{
101
					SendMessage(hWnd, WM_CLOSE, 0, 0);
102
				}
103
			}
104
		}else if(trig == GUI_LOG){
105
		}
106
	}else if(msg == WM_CLOSE){
107
		DestroyWindow(hWnd);
108
	}else if(msg == WM_DESTROY){
109
		DeleteObject(pbtewi_brush);
110
		PostQuitMessage(0);
111
	}else if(msg == WM_CREATE){
112
		RECT rc, src;
113
		GetClientRect(hWnd, &rc);
114
 
115
		InitCommonControls();
116
 
117
		status = CreateStatusWindow(WS_CHILD | WS_VISIBLE | CCS_BOTTOM, NULL, hWnd, GUI_STATUS);
118
		SendMessage(status, SB_SIMPLE, 0, 0);
119
		SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Welcome to Tewi HTTPd");
120
		SendMessage(status, SB_GETRECT, 0, (LPARAM)&src);
121
 
122
		pbtewi_brush = CreateSolidBrush(RGB(0xf7, 0xc9, 0xf3));
123
		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);
124
		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);
125
		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);
126
		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);
127
		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);
128
		SetTimer(hWnd, TIMER_WATCH_TEWI, 100, NULL);
129
	}else if(msg == WM_TIMER){
130
		if(wp == TIMER_WATCH_TEWI){
131
			if(tewi_alive){
132
				if(was_starting){
133
					was_starting = FALSE;
134
					SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Started Tewi HTTPd");
135
				}
136
				EnableWindow(button_start, FALSE);
137
				EnableWindow(button_stop, TRUE);
138
			}else{
139
				if(was_starting){
140
					was_starting = FALSE;
141
					SendMessage(status, SB_SETTEXT, 0, (LPARAM)"Stopped Tewi HTTPd");
142
				}
143
				EnableWindow(button_start, TRUE);
144
				EnableWindow(button_stop, FALSE);
145
				if(exiting){
146
					KillTimer(hWnd, TIMER_WATCH_TEWI);
147
					SendMessage(hWnd, WM_CLOSE, 0, 0);
148
				}
149
			}
150
		}
151
	}else if(msg == WM_PAINT){
152
		HDC hdc;
153
		PAINTSTRUCT ps;
154
		RECT rc;
155
		RECT fill;
156
 
157
		GetClientRect(hWnd, &rc);
158
		hdc = BeginPaint(hWnd, &ps);
159
		SetRect(&fill, 0, 0, WINWIDTH(rc), 40);
160
		FillRect(hdc, &fill, pbtewi_brush);
161
		ShowBitmap(hWnd, hdc, "PBTEWI", 0, 0);
162
		EndPaint(hWnd, &ps);
163
	}else{
164
		return DefWindowProc(hWnd, msg, wp, lp);
165
	}
166
	return 0;
167
}
168
 
169
BOOL InitApp(void){
170
	WNDCLASS wc;
171
	wc.style = CS_HREDRAW | CS_VREDRAW;
172
	wc.lpfnWndProc = WndProc;
173
	wc.cbClsExtra = 0;
174
	wc.cbWndExtra = 0;
175
	wc.hInstance = hInst;
176
	wc.hIcon = LoadIcon(hInst, "TEWI");
177
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
178
	wc.hbrBackground = GetSysColorBrush(COLOR_MENU);
179
	wc.lpszMenuName = NULL;
180
	wc.lpszClassName = "tewihttpd";
181
	return RegisterClass(&wc);
182
}
183
 
184
BOOL InitWindow(int nCmdShow){
185
	HWND hWnd;
186
	RECT deskrc, rc;
187
	HWND hDeskWnd = GetDesktopWindow();
188
	GetWindowRect(hDeskWnd, &deskrc);
189
	hWnd = CreateWindow("tewihttpd", "Tewi HTTPd", (WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME) ^ WS_MAXIMIZEBOX, 0, 0, 600, 200, NULL, 0, hInst, NULL);
190
 
191
	if(!hWnd){
192
		return FALSE;
193
	}
194
	GetWindowRect(hWnd, &rc);
195
	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);
196
	ShowWindow(hWnd, nCmdShow);
197
	UpdateWindow(hWnd);
198
	return TRUE;
199
}
200
 
201
int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst, LPSTR lpsCmdLine, int nCmdShow){
202
	MSG msg;
203
	BOOL bret;
204
	hInst = hCurInst;
205
	tewi_alive = FALSE;
206
	was_starting = FALSE;
207
	exiting = FALSE;
208
	if(!InitApp()){
209
		return FALSE;
210
	}
211
	if(!InitWindow(nCmdShow)){
212
		return FALSE;
213
	}
214
 
215
	while((bret = GetMessage(&msg, NULL, 0, 0)) != 0){
216
		if(bret == -1) {
217
			break;
218
		} else {
219
			TranslateMessage(&msg);
220
			DispatchMessage(&msg);
221
		}
222
	}
223
	return (int)msg.wParam;
224
}