This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsIRC_Wnd_Query.cpp
393 lines (315 loc) · 9.94 KB
/
zsIRC_Wnd_Query.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// zsIRC_Wnd_Query.cpp : implementation file
//
#include "stdafx.h"
#include "zsIRC.h"
#include "zsIRC_Wnd_Query.h"
#include "zsIRC_Settings.h"
#include "zsIRC_Line.h"
#include "ChildView.h"
#include "OffscreenDC.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// zsIRC_Wnd_Query
zsIRC_Wnd_Query::zsIRC_Wnd_Query(CChannelBar * c) : zsIRC_Wnd(c)
{
ptSelect = CPoint(-1,-1);
nSelectLine = -1;
}
zsIRC_Wnd_Query::~zsIRC_Wnd_Query()
{
}
BEGIN_MESSAGE_MAP(zsIRC_Wnd_Query, CWnd)
//{{AFX_MSG_MAP(zsIRC_Wnd_Query)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_VSCROLL()
ON_WM_DESTROY()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
// ON_WM_ACTIVATEAPP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#define isnum(x) ( _T('0')<=(x) && (x)<=_T('9') )
/////////////////////////////////////////////////////////////////////////////
// zsIRC_Wnd_Query message handlers
void zsIRC_Wnd_Query::OnPaint()
{
// CPaintDC dc(this); // device context for painting
COffscreenDC dc(this,CRect(0,0,0,0));
//int nScrollPos = GetScrollPos(SB_VERT);
// TODO: Add your message handler code here
CRect r;
GetClientRect(&r);
CBrush b(SETUP.cBackgroundColor);
dc.GetDC()->FillRect(&r,&b);
if (aLines.Num()<=0) return;
LOGFONT lf;
ZeroMemory(&lf,sizeof(LOGFONT));
_tcsnccpy(lf.lfFaceName,SETUP.szConversationFont,LF_FACESIZE);
lf.lfHeight = SETUP.nConversationFontSize;
lf.lfWeight = FW_NORMAL;
lf.lfPitchAndFamily = DEFAULT_PITCH;
HFONT font = CreateFontIndirect(&lf);
HFONT fontold = (HFONT)dc.GetDC()->SelectObject(font);
int nTop = r.bottom;
for (int i=nScrollPos-1; i>=0; i--) {
zsIRC_Line * line = (zsIRC_Line *) aLines[i];
CString str = line->toString();
if (SETUP.nStripColorCodes)
for (int j=0; j<str.GetLength(); j++) {
if (str[j]==_T('\x02')) { // BOLD
str = str.Left(j) + str.Mid(j+1);
}
else if (str[j]==_T('\x1F')) { // underline(?)
str = str.Left(j) + str.Mid(j+1);
}
else if (str[j]==_T('\x03')) { // color
TCHAR * szMasks[] = {
_T("xx,yy"),
_T("xx,y"),
_T("x,yy"),
_T("x,y"),
_T("xx"),
_T("x"),
};
int nFittingMask = -1;
for (int k=0; k<6; k++) {
if (str.GetLength() - j - 2 >= (int)_tcslen(szMasks[k])) {
int nMaskFits = 1;
for (int l=0; szMasks[k][l]; l++) {
if (szMasks[k][l] == _T('x') && !isnum(str[j+l+1])) nMaskFits = 0;
if (szMasks[k][l] == _T('y') && !isnum(str[j+l+1])) nMaskFits = 0;
if (szMasks[k][l] == _T(',') && str[j+l+1]!=_T(',')) nMaskFits = 0;
}
if (nMaskFits) {
nFittingMask = k;
break;
}
}
}
if (nFittingMask != -1) {
str = str.Left(j) + str.Mid(j+1+_tcslen(szMasks[nFittingMask]));
} else {
str = str.Left(j) + str.Mid(j+1);
}
}
}
int nTimes = 0;
while (1) {
if (nTimes++>20) break; // silly watchdog mechanism
if (!BreakString(str,r,*dc.GetDC())) break;
}
CRect rAdjust = r;
rAdjust.top = 0;
int nHeight = dc.GetDC()->DrawText(str,rAdjust,DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX);
nTop -= nHeight;
CRect rFinal = r;
rFinal.top = nTop;
rFinal.bottom = nTop+nHeight;
if (PtInRect(rFinal,ptSelect)) {
dc.GetDC()->SetBkColor(SETUP.cForegroundColor);
nSelectLine = i;
} else {
dc.GetDC()->SetBkColor(SETUP.cBackgroundColor);
}
dc.GetDC()->SetTextColor(line->cColor);
dc.GetDC()->DrawText(str,rFinal,DT_WORDBREAK | DT_NOPREFIX);
if (nTop<0) break;
}
dc.GetDC()->SetTextColor(SETUP.cForegroundColor);
DeleteObject(font);
// Do not call CWnd::OnPaint() for painting messages
}
void zsIRC_Wnd_Query::AddLine(TCHAR * szMessage,COLORREF cColor) {
zsIRC_Line * line = new zsIRC_Line(szMessage,cColor);
aLines.Add(line);
nScrollPos = aLines.Num();
SetScrollRange(SB_VERT,1,nScrollPos,TRUE);
SetScrollPos(SB_VERT,nScrollPos,TRUE);
/*
SCROLLINFO s;
ZeroMemory(&s,sizeof(SCROLLINFO));
s.cbSize=sizeof(SCROLLINFO);
s.nMin=1;
s.nMax=nScrollPos;
s.nPage=3;
s.fMask=SIF_ALL;
s.nPos=nScrollPos;
s.nTrackPos=0;
SetScrollInfo(SB_VERT,&s);
*/
InvalidateRect(NULL);
CString filename = "";
if (nType == ZSIRC_WT_STATUS) filename += _T("_");
filename += szCaption;
filename += _T(".log");
#ifndef _DEBUG
if (filename!=_T("_Debug.log"))
#endif
line->LogToFile(filename.LockBuffer());
if (cColor == SETUP.cForegroundColor) SetEventLevel(ZSIRC_WH_MESSAGE);
else SetEventLevel(ZSIRC_WH_EVENT);
if (GetChannelBar()) GetChannelBar()->InvalidateRect(NULL);
}
int zsIRC_Wnd_Query::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
evEvent = ZSIRC_WH_NOTHING;
SCROLLINFO s;
ZeroMemory(&s,sizeof(SCROLLINFO));
s.cbSize=sizeof(SCROLLINFO);
s.nMin=0;
s.nMax=0;
s.nPage=3;
s.fMask=SIF_ALL;
s.nPos=0;
s.nTrackPos=0;
SetScrollInfo(SB_VERT,&s);
nScrollPos = 0;
MarkLog(_T("Start"));
return 0;
}
void zsIRC_Wnd_Query::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CWnd::OnActivate(nState, pWndOther, bMinimized);
// TODO: Add your message handler code here
}
void zsIRC_Wnd_Query::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
int minpos;
int maxpos;
int curpos;
GetScrollRange(SB_VERT,&minpos,&maxpos);
maxpos = GetScrollLimit(SB_VERT);
curpos = GetScrollPos(SB_VERT);
switch (nSBCode)
{
case SB_LEFT: {
curpos = minpos;
} break;
case SB_RIGHT: {
curpos = maxpos;
} break;
case SB_ENDSCROLL: {
} break;
case SB_LINELEFT: {
if (curpos > minpos) curpos--;
} break;
case SB_LINERIGHT: {
if (curpos < maxpos) curpos++;
} break;
case SB_PAGEUP: {
SCROLLINFO info;
GetScrollInfo(SB_VERT, &info, SIF_ALL);
if (curpos > minpos)
curpos = max(minpos, curpos - (int) info.nPage);
} break;
case SB_PAGEDOWN: {
SCROLLINFO info;
GetScrollInfo(SB_VERT, &info, SIF_ALL);
if (curpos < maxpos)
curpos = min(maxpos, curpos + (int) info.nPage);
} break;
case SB_THUMBPOSITION: {
curpos = nPos + minpos;
} break;
case SB_THUMBTRACK: {
curpos = nPos + minpos;
} break;
}
curpos = min(curpos,maxpos);
curpos = max(curpos,minpos);
SetScrollPos(SB_VERT,curpos);
nScrollPos = curpos;
InvalidateRect(NULL);
}
void zsIRC_Wnd_Query::OnDestroy()
{
CWnd::OnDestroy();
MarkLog(_T("Close"));
}
void zsIRC_Wnd_Query::MarkLog(TCHAR * szSession) {
CString filename = "";
if (nType == ZSIRC_WT_STATUS) filename += _T("_");
filename += szCaption;
filename += _T(".log");
SYSTEMTIME st;
GetLocalTime(&st);
TCHAR ts[1000];
_sntprintf(ts,1000,_T("%sSession %s: %04d/%02d/%02d %02d:%02d:%02d\r\n"),
_tcscmp(szSession,_T("Start"))==0 ? _T("\r\n") : _T(""),
szSession,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
#ifndef _DEBUG
if (filename!=_T("_Debug.log"))
#endif
PrintToLogFile(filename.LockBuffer(),ts);
}
void zsIRC_Wnd_Query::SetCaption(TCHAR * szNewCaption) {
MarkLog(_T("Close"));
_tcsncpy(szCaption,szNewCaption,50);
MarkLog(_T("Start"));
}
int zsIRC_Wnd_Query::BreakString(CString &sString, CRect r, CPaintDC& dc)
{
CRect rAdjust = r;
rAdjust.top = 0;
int nHeight = dc.DrawText(sString,rAdjust,DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX);
if (rAdjust.right > r.right) {
// we need to force a linebreak somehow
for (int i=sString.GetLength()-1; i>0; i--) {
if (isspace(sString[i])) break; // we got to a whitespace, there isnt much more we can do from here.
if (!isalnum(sString[i]) && !isspace(sString[i]) && !isspace(sString[i-1])) {
rAdjust = r;
rAdjust.top = 0;
int nHeight2 = dc.DrawText(sString.Left(i),rAdjust,DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX);
if (rAdjust.right <= r.right) {
sString = sString.Left(i) + _T(" ") + sString.Mid(i);
nHeight = nHeight2;
return 1;
}
}
}
// we couldnt break a line at non-alpha chars, so we just mutilate the strings as is
for (i=sString.GetLength()-1; i>=0; i--) {
rAdjust = r;
rAdjust.top = 0;
int nHeight2 = dc.DrawText(sString.Left(i),rAdjust,DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX);
if (rAdjust.right <= r.right) {
sString = sString.Left(i) + _T(" ") + sString.Mid(i);
nHeight = nHeight2;
return 1;
}
}
}
return 0;
}
void zsIRC_Wnd_Query::OnLButtonDown(UINT nFlags, CPoint point) {
ptSelect = point;
InvalidateRect(NULL);
}
void zsIRC_Wnd_Query::OnLButtonUp(UINT nFlags, CPoint point) {
if (nSelectLine==-1) return;
if (!OpenClipboard()) return;
EmptyClipboard();
zsIRC_Line * line = (zsIRC_Line *) aLines[nSelectLine];
int nLen = line->szMessage.GetLength() + 1;
TCHAR * pGlobal = (TCHAR*)LocalAlloc(LPTR, sizeof(TCHAR) * nLen);
if (!pGlobal) {
CloseClipboard();
return;
}
CopyMemory(pGlobal,line->szMessage.LockBuffer(),sizeof(TCHAR) * nLen);
::SetClipboardData(CF_TTEXT,(HANDLE)pGlobal);
CloseClipboard();
//////////////////////////////////////////////////////////////////////////
ptSelect = CPoint(-1,-1);
nSelectLine = -1;
InvalidateRect(NULL);
}