-
Notifications
You must be signed in to change notification settings - Fork 5
/
LogWnd.cpp
586 lines (541 loc) · 13.7 KB
/
LogWnd.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
Copyright (c) 2006 Tristan Lorach (lorachnroll@gmail.com)
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdafx.h"
#include "SvcMFCUI.h"
#include "LogWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static int _gColText[] = { IDS_COL_1, IDS_COL_2};
static int _gColSize[] = { 200, 100};
void CLogWnd::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLogWnd)
DDX_Control(pDX, IDC_LISTLOGGING, m_ListLog);
//}}AFX_DATA_MAP
}
/**
** Instanciation of an object.
**/
const void * CLogWnd::NewObjectFunc(size_t lparam1, size_t lparam2)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CLogWnd *pDlg = new CLogWnd;
CWnd *pOwner;
if(lparam1) pOwner = CWnd::FromHandle((HWND)lparam1);
else pOwner = &g_WindowHandler;
BOOL bOK = pDlg->Create(IDD_LOG, pOwner);
if(bOK)
{
CWnd *pW = pDlg->GetParent();
pDlg->ShowWindow(SW_HIDE);
//pDlg->RedrawWindow();
//pDlg->PeekMyself();
return (void*)(static_cast<IWindowLog*>(pDlg));
}
delete pDlg;
return NULL;
}
static int GetImageId(CString &s);
/*----------------------------------------------------------------------------------*/ /**
handles incoming data to the console from another plug : lpFrom. lpTo is the console plug...
just dump the string to the console
**/ //----------------------------------------------------------------------------------
#ifdef USEPLUGS
bool CLogWnd::plug_IncomingData(IPlug *lpFrom, IPlug *lpTo, bool bSendEvent)
{
String str;
if(!(m_hWnd))
return false;
lpFrom->getValue(str);
if(!str.empty())
{
m_lastlogstr = str;
CString strbuff(str.c_str());
int pos, img;
do
{
img = 0;
//
// Get a line
//
CString aaa = strbuff.SpanExcluding("\n");
//
// Find a header
//
pos = aaa.Find(">>");
if(pos > 0)
{
CString header;
header = aaa.Left(pos);
aaa = aaa.Right(aaa.GetLength() - pos-2);
img = GetImageId(header);
}
//
// Insert it into the list of messages
//
if(aaa.GetLength() > 0)
{
CString bbb = aaa.SpanExcluding("||");
int ic = m_ListLog.GetItemCount();
m_ListLog.InsertItem(ic , bbb, img);
// For now let's do it once...
int subi = aaa.Find("||",0);
if(subi >= 0)
{
bbb = aaa.Right(aaa.GetLength() - subi - 2);
m_ListLog.SetItemText(ic, 1, bbb);
}
}
//
// Go to next one.
//
pos = strbuff.Find("\n",0);
if(pos >= 0)
{
aaa = strbuff.Right(strbuff.GetLength() - pos-1);
strbuff = aaa;
}
} while(pos >= 0);
m_ListLog.EnsureVisible(m_ListLog.GetItemCount()-1, TRUE);
}
// tell the connected readers that data changed. Set bSendEvent=false if you decide to send this
// method by your own.
if(bSendEvent)
lpTo->flush();
return false;
}
#endif
/////////////////////////////////////////////////////////////////////////////
// CLogWnd
//I MPLEMENT_DYNCREATE(CLogWnd, CDialog)
CLogWnd::CLogWnd() :
CDialog(CLogWnd::IDD, NULL)
#ifdef USEPLUGS
,m_pluglogstr(PLUG_STRING, 0, "log", &m_lastlogstr, this)
#endif
{
//
// because these aren't dynamicaly allocated
//
#ifdef USEPLUGS
m_pluglogstr.setDesc("plug to ...");
#endif
IMPLSMARTREFINIT
//{{AFX_DATA_INIT(CDlgProgress)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CLogWnd::~CLogWnd()
{
}
BEGIN_MESSAGE_MAP(CLogWnd, CDialog)
//{{AFX_MSG_MAP(CLogWnd)
ON_WM_CREATE()
ON_WM_CLOSE()
ON_WM_SIZE()
//}}AFX_MSG_MAP
ON_NOTIFY(LVN_ITEMACTIVATE, IDC_LISTLOGGING, &CLogWnd::OnLvnItemActivateListlogging)
ON_NOTIFY(HDN_ITEMCLICK, 0, &CLogWnd::OnHdnItemclickListlogging)
ON_NOTIFY(LVN_COLUMNCLICK, IDC_LISTLOGGING, &CLogWnd::OnLvnColumnclickListlogging)
ON_NOTIFY(NM_CLICK, IDC_LISTLOGGING, &CLogWnd::OnNMClickListlogging)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LISTLOGGING, &CLogWnd::OnLvnItemchangedListlogging)
END_MESSAGE_MAP()
IWindowLog* CLogWnd::Clear()
{
m_ListLog.DeleteAllItems();
return this;
}
/**
** return an index depending on what is the string
**/
static int GetImageId(CString &s)
{
s.MakeLower();
if(s == "warning")
{
return 1;
}
else if(s == "error")
{
return 2;
}
else if(s == "note")
{
return 3;
}
else if(s == "net")
{
return 4;
}
else if(s == "no")
{
return 5;
}
else if(s == "event")
{
return 6;
}
else if(s == "yes")
{
return 7;
}
return 0;
}
/**
**
**/
IWindowLog* CLogWnd::AddMessage(int level, LPCSTR fmt, ...)
{
//TODO: Add a way to free this up, later !
static char *fmt2 = NULL;
static size_t fmt2_sz = 0;
va_list vlist;
// Get output string
va_start(vlist, fmt);
int retval;
//size_t sz = strlen(fmt)+1;
if(!fmt2)
{
fmt2_sz = 2048;
fmt2 = (char*)malloc(fmt2_sz);
}
while((retval = _vsnprintf(fmt2, fmt2_sz, fmt, vlist)) < 0) // means there wasn't anough room
{
fmt2_sz *= 2;
if(fmt2) free(fmt2);
fmt2 = (char*)malloc(fmt2_sz);
}
CString strbuff(fmt2);
int pos;
do
{
//
// Get a line
//
CString aaa = strbuff.SpanExcluding("\n");
//
// Insert it into the list of messages
//
if(aaa.GetLength() > 0)
{
CString bbb = aaa.SpanExcluding("||");
int ic = m_ListLog.GetItemCount();
m_ListLog.InsertItem(ic , bbb, level);
// For now let's do it once...
int subi = aaa.Find("||",0);
if(subi >= 0)
{
bbb = aaa.Right(aaa.GetLength() - subi - 2);
m_ListLog.SetItemText(ic, 1, bbb);
}
}
//
// Go to next one.
//
pos = strbuff.Find("\n",0);
if(pos >= 0)
{
aaa = strbuff.Right(strbuff.GetLength() - pos-1);
strbuff = aaa;
}
} while(pos >= 0);
m_ListLog.EnsureVisible(m_ListLog.GetItemCount()-1, TRUE);
#ifdef USEPLUGS
m_pluglogstr.flush();
#endif
//PeekMyself();
return this;
}
IWindowLog* CLogWnd::AddMessage(LPCSTR fmt, ...)
{
//TODO: Add a way to free this up, later !
static char *fmt2 = NULL;
static size_t fmt2_sz = 0;
va_list vlist;
// Get output string
va_start(vlist, fmt);
int retval;
//size_t sz = strlen(fmt)+1;
if(!fmt2)
{
fmt2_sz = 2048;
fmt2 = (char*)malloc(fmt2_sz);
}
while((retval = _vsnprintf(fmt2, fmt2_sz, fmt, vlist)) < 0) // means there wasn't anough room
{
fmt2_sz *= 2;
if(fmt2) free(fmt2);
fmt2 = (char*)malloc(fmt2_sz);
}
#ifdef USEPLUGS
m_pluglogstr.setValue(fmt2);
#else
m_lastlogstr = fmt2;
CString strbuff(m_lastlogstr.c_str());
int pos, img;
do
{
img = 0;
//
// Get a line
//
CString aaa = strbuff.SpanExcluding("\n");
//
// Find a header
//
pos = aaa.Find(">>");
if(pos > 0)
{
CString header;
header = aaa.Left(pos);
aaa = aaa.Right(aaa.GetLength() - pos-2);
img = GetImageId(header);
}
//
// Insert it into the list of messages
//
if(aaa.GetLength() > 0)
{
CString bbb = aaa.SpanExcluding("||");
int ic = m_ListLog.GetItemCount();
m_ListLog.InsertItem(ic , bbb, img);
// For now let's do it once...
int subi = aaa.Find("||",0);
if(subi >= 0)
{
bbb = aaa.Right(aaa.GetLength() - subi - 2);
m_ListLog.SetItemText(ic, 1, bbb);
}
}
//
// Go to next one.
//
pos = strbuff.Find("\n",0);
if(pos >= 0)
{
aaa = strbuff.Right(strbuff.GetLength() - pos-1);
strbuff = aaa;
}
} while(pos >= 0);
m_ListLog.EnsureVisible(m_ListLog.GetItemCount()-1, TRUE);
#endif
va_end(vlist);
//PeekMyself();
return this;
}
/////////////////////////////////////////////////////////////////////////////
// CLogWnd message handlers
int CLogWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CLogWnd::BuildColumns(int nCols, int * nWidth, int * iCol)
{
ASSERT(nCols);
ASSERT(nWidth);
ASSERT(iCol);
ASSERT(m_ListLog);
//insert columns
int i;
LV_COLUMN lvc;
numCols = nCols;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
CString strTemp;
for(i = 0; i < nCols; i++)
{
lvc.iSubItem = i;
strTemp.LoadString(iCol[i]);
lvc.pszText = (char*)(LPCTSTR)strTemp;
lvc.cx = nWidth[i];
lvc.fmt = LVCFMT_LEFT;
m_ListLog.InsertColumn(i,&lvc);
}
}
void CLogWnd::OnClose()
{
//
// Send the event
//
for(unsigned int ic = 0; ic < m_pclients.size(); ic++)
{
// HACK : This forces this object to not be deleted inside an event.
AddRef();
m_pclients[ic]->WindowClosed(static_cast<IWindow*>(this));
Release();
}
for(unsigned int ic = 0; ic < g_WindowHandler.m_pclients.size(); ic++)
{
// HACK : This forces this object to not be deleted inside an event.
AddRef();
g_WindowHandler.m_pclients[ic]->WindowClosed(static_cast<IWindow*>(this));
Release();
}
//CDialog::OnClose();
}
void CLogWnd::OnSize(UINT nType, int cx, int cy)
{
CRect rc;
CDialog::OnSize(nType, cx, cy);
GetClientRect(&rc);
if(m_ListLog.m_hWnd) m_ListLog.SetWindowPos(NULL,rc.left,rc.top, rc.right, rc.bottom ,SWP_NOACTIVATE);
int w = (rc.right - rc.left);
for(int i=1; i<numCols; i++)
{
LV_COLUMN lvc;
lvc.mask = LVCF_WIDTH;
m_ListLog.GetColumn(i, &lvc);
w -= lvc.cx;
}
m_ListLog.SetColumnWidth(0, w);
}
BOOL CLogWnd::OnInitDialog()
{
CDialog::OnInitDialog();
BOOL bRes = m_imglist.Create(IDB_LOGIMG,16,1,0xFFFFFF);
m_ListLog.SetImageList(&m_imglist, LVSIL_SMALL);
m_ListLog.ModifyStyleEx(0, WS_EX_STATICEDGE);
m_ListLog.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
BuildColumns(1, _gColSize, _gColText);
/*m_ListLog.InsertItem(0, "Some sample text.");
m_ListLog.SetItem(0,1,LVIF_TEXT,"More text.",
0,LVIF_TEXT,LVIF_TEXT,0);*/
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CLogWnd::OnLvnItemActivateListlogging(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMIA = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
//m_ListLog.EditLabel(pNMIA->iItem);
for(unsigned int ic = 0; ic < m_pclients.size(); ic++)
{
if(m_pclients[ic])
m_pclients[ic]->ListBoxSelection(this, pNMIA->iItem, pNMIA->iItem, true);
}
for(unsigned int ic = 0; ic < g_WindowHandler.m_pclients.size(); ic++)
{
if(g_WindowHandler.m_pclients[ic])
g_WindowHandler.m_pclients[ic]->ListBoxSelection(this, pNMIA->iItem, pNMIA->iItem, true);
}
*pResult = 0;
}
void CLogWnd::OnHdnItemclickListlogging(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
*pResult = 0;
}
void CLogWnd::OnLvnColumnclickListlogging(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
*pResult = 0;
}
void CLogWnd::OnNMClickListlogging(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
for(unsigned int ic = 0; ic < m_pclients.size(); ic++)
{
if(m_pclients[ic])
m_pclients[ic]->ListBoxSelection(this, pNMItemActivate->iItem, pNMItemActivate->iItem, false);
}
for(unsigned int ic = 0; ic < g_WindowHandler.m_pclients.size(); ic++)
{
if(g_WindowHandler.m_pclients[ic])
g_WindowHandler.m_pclients[ic]->ListBoxSelection(this, pNMItemActivate->iItem, pNMItemActivate->iItem, false);
}
*pResult = 0;
}
void CLogWnd::OnLvnItemchangedListlogging(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
}
IWindowLog* CLogWnd::AddColumns(int n, int* psz, LPSTR *pstr)
{
LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
int w;
if((psz == NULL) && (numCols == 1))
{
RECT rc;
GetClientRect(&rc);
w = (rc.right - rc.left)/(numCols + n);
if(w < 10) w = 10;
m_ListLog.SetColumnWidth(0, w);
}
for(int i = 0; i < n; i++)
{
lvc.iSubItem = numCols + i;
lvc.pszText = "...";
if(pstr && pstr[i])
lvc.pszText = pstr[i];
lvc.cx = w;
if(psz)
lvc.cx = psz[i];
lvc.fmt = LVCFMT_LEFT;
m_ListLog.InsertColumn(numCols + i,&lvc);
}
numCols += n;
return this;
}
IWindowLog* CLogWnd::DelColumns(int n=1)
{
assert(!"TODO");
return this;
}
int CLogWnd::GetNumColumns()
{
return numCols;
}
IWindowLog* CLogWnd::SetColumSize(int c, int sz)
{
m_ListLog.SetColumnWidth(c, sz);
return this;
}
int CLogWnd::GetNumItems()
{
int ic = m_ListLog.GetItemCount();
return ic;
}
IWindow* CLogWnd::SetTitle(LPCSTR title)
{
LV_COLUMN lvc;
lvc.mask = LVCF_TEXT;
lvc.pszText = (LPSTR)title;
m_ListLog.SetColumn(0,&lvc);
return this;
}
IWindow* CLogWnd::GetTitle(char * title, int maxlen)
{
LV_COLUMN lvc;
lvc.mask = LVCF_TEXT;
m_ListLog.GetColumn(0, &lvc);
if(lvc.pszText)
strncpy(title, lvc.pszText, maxlen);
return this;
}