1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
#include "stdafx.h"
#include "pinggraph.h"
#include <map>
#define ID_REPAINT_TIMER 10101
struct WindowData {
HANDLE hContact;
HistoryList list;
};
#define WM_REBUILDLIST (WM_USER + 1)
LRESULT CALLBACK GraphWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg) {
case WM_REBUILDLIST:
{
WindowData *wd = (WindowData *)GetWindowLong(hwnd, GWL_USERDATA);
bool found = false;
EnterCriticalSection(&data_list_cs);
for(PINGLIST::iterator i = data_list.begin(); i != data_list.end(); i++) {
if(i->hContact == wd->hContact) {
wd->list = i->history;
found = true;
break;
}
}
LeaveCriticalSection(&data_list_cs);
if(!found) {
PostMessage(hwnd, WM_CLOSE, 0, 0);
return TRUE;
}
InvalidateRect(hwnd, 0, FALSE);
}
return TRUE;
case WM_SHOWWINDOW:
if(wParam == TRUE && lParam == 0) {
//WindowData *wd = (WindowData *)GetWindowLong(hwnd, GWL_USERDATA);
KillTimer(hwnd, ID_REPAINT_TIMER);
#ifdef min
SetTimer(hwnd, ID_REPAINT_TIMER, min(options.ping_period * 1000, 5000), 0);
#else
SetTimer(hwnd, ID_REPAINT_TIMER, std::min(options.ping_period * 1000, 5000), 0);
#endif
SendMessage(hwnd, WM_REBUILDLIST, 0, 0);
}
break;
case WM_TIMER:
{
SendMessage(hwnd, WM_REBUILDLIST, 0, 0);
}
return TRUE;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
RECT r;
WindowData *wd = (WindowData *)GetWindowLong(hwnd, GWL_USERDATA);
if(hdc = BeginPaint(hwnd, &ps)) {
GetClientRect(hwnd, &r);
FillRect(hdc, &r, GetSysColorBrush(COLOR_WINDOW));
short max_value = -1; // this is minimum graph height, in ms
for(HistoryList::iterator hli = wd->list.begin(); hli != wd->list.end(); hli++) {
if(hli->first > max_value) max_value = hli->first;
}
max_value = (int)(max_value * 1.2f);
if(max_value < MIN_GRAPH_HEIGHT) max_value = MIN_GRAPH_HEIGHT;
#ifdef max
float unit_width = (r.right - r.left) / (float)max((int)wd->list.size(), MIN_BARS), // space for at least MIN_BARS bars
#else
float unit_width = (r.right - r.left) / (float)std::max((int)wd->list.size(), MIN_BARS), // space for at least MIN_BARS bars
#endif
unit_height = (r.bottom - r.top) / (float)max_value;
RECT bar;
bar.bottom = r.bottom;
float x = r.right - (unit_width * wd->list.size() + 0.5f);
bar.left = (int)(x + 0.5f);
bar.right = (int)(x + unit_width + 0.5f);
DWORD last_time = 0, time, start_time = 0;
if(wd->list.size())
start_time = wd->list.begin()->second;
HPEN hPenOld, hPen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW));
hPenOld = (HPEN)SelectObject(hdc, hPen);
for(HistoryList::iterator hi = wd->list.begin(); hi != wd->list.end(); hi++) {
if(hi->first != -1) {
bar.top = bar.bottom - (int)(hi->first * unit_height + 0.5f);
FillRect(hdc, &bar, GetSysColorBrush(COLOR_HOTLIGHT));
}
time = hi->second - start_time;
if(time / MARK_PERIOD != last_time / MARK_PERIOD) {
MoveToEx(hdc, bar.left, r.bottom, 0);
LineTo(hdc, bar.left, r.top);
}
last_time = time;
x += unit_width;
bar.left = bar.right;
bar.right = (int)(x + unit_width + 0.5f);
}
// draw horizontal lines to mark every 100ms
for(int li = 0; li < max_value; li += MARK_TIME) {
MoveToEx(hdc, r.left, r.bottom - (int)(li * unit_height + 0.5f), 0);
LineTo(hdc, r.right, r.bottom - (int)(li * unit_height + 0.5f));
}
SelectObject(hdc, hPenOld);
DeleteObject(hPen);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, GetSysColor(COLOR_3DDKSHADOW));
char buff[64];
sprintf(buff, "%dms", MARK_TIME);
TextOut(hdc, r.left + 10, r.bottom - (int)(unit_height * MARK_TIME + 0.5f), buff, 5);
EndPaint(hwnd, &ps);
}
}
return TRUE;
case WM_ERASEBKGND:
return TRUE;
case WM_SIZE:
InvalidateRect(hwnd, 0, FALSE);
break;
case WM_CLOSE:
{
KillTimer(hwnd, ID_REPAINT_TIMER);
WindowData *wd = (WindowData *)GetWindowLong(hwnd, GWL_USERDATA);
if(CallService(MS_DB_CONTACT_IS, (WPARAM)wd->hContact, 0))
Utils_SaveWindowPosition(hwnd, wd->hContact, PROTO, "pinggraphwnd");
}
break;
case WM_DESTROY:
{
WindowData *wd = (WindowData *)GetWindowLong(hwnd, GWL_USERDATA);
DBDeleteContactSetting(wd->hContact, PROTO, "WindowHandle");
delete wd;
}
// drop through
};
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int ShowGraph(WPARAM wParam, LPARAM lParam) {
HWND hGraphWnd = (HWND)DBGetContactSettingDword((HANDLE)wParam, PROTO, "WindowHandle", 0);
if(hGraphWnd) {
ShowWindow(hGraphWnd, SW_SHOW);
SetWindowPos(hGraphWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
return 0;
}
WNDCLASS wndclass;
wndclass.style = 0;
wndclass.lpfnWndProc = GraphWindowProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInst;
wndclass.hIcon = LoadSkinnedProtoIcon(PROTO, ID_STATUS_ONLINE);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = PROTO "GraphWindow";
RegisterClass(&wndclass);
DBVARIANT dbv;
char title[256];
strcpy(title, "Ping Graph");
if(!DBGetContactSetting((HANDLE)wParam, PROTO, "Nick", &dbv)) {
strcat(title, " - ");
strncat(title, dbv.pszVal, 256 - 13);
DBFreeVariant(&dbv);
}
HWND parent = 0;
hGraphWnd = CreateWindowEx(0, PROTO "GraphWindow", title,
(WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN) & ~CS_VREDRAW & ~CS_HREDRAW,
0,0,800,600,parent,NULL,hInst,NULL);
DBWriteContactSettingDword((HANDLE)wParam, PROTO, "WindowHandle", (DWORD)hGraphWnd);
WindowData *wd = new WindowData;
wd->hContact = (HANDLE)wParam; // wParam is contact handle
SetWindowLong(hGraphWnd, GWL_USERDATA, (LONG)wd);
if(CallService(MS_DB_CONTACT_IS, (WPARAM)wd->hContact, 0))
Utils_RestoreWindowPosition(hGraphWnd, wd->hContact, PROTO, "pinggraphwnd");
if(!IsWindowVisible(hGraphWnd))
ShowWindow(hGraphWnd, SW_SHOW);
return 0;
}
// save window positions, close windows
void graphs_cleanup() {
HWND hwnd;
char *proto;
HANDLE hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDFIRST, 0, 0 );
while ( hContact != NULL )
{
proto = (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
if (proto && lstrcmp( PROTO, proto) == 0) {
if((hwnd = (HWND)DBGetContactSettingDword(hContact, PROTO, "WindowHandle", 0)) != 0) {
DestroyWindow(hwnd);
DBWriteContactSettingByte(hContact, PROTO, "WindowWasOpen", 1);
}
}
hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDNEXT,( WPARAM )hContact, 0 );
}
}
// restore windows that were open when cleanup was called last
void graphs_init() {
char *proto;
HANDLE hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDFIRST, 0, 0 );
while ( hContact != NULL )
{
proto = (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
if (proto && lstrcmp( PROTO, proto) == 0) {
if(DBGetContactSettingByte(hContact, PROTO, "WindowWasOpen", 0) != 0) {
ShowGraph((WPARAM)hContact, 0);
DBWriteContactSettingByte(hContact, PROTO, "WindowWasOpen", 0);
}
}
hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDNEXT,( WPARAM )hContact, 0 );
}
}
|