summaryrefslogtreecommitdiffabout
path: root/BigBrotherView.cpp
Unidiff
Diffstat (limited to 'BigBrotherView.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--BigBrotherView.cpp485
1 files changed, 485 insertions, 0 deletions
diff --git a/BigBrotherView.cpp b/BigBrotherView.cpp
new file mode 100644
index 0000000..1fb529b
--- a/dev/null
+++ b/BigBrotherView.cpp
@@ -0,0 +1,485 @@
1// BigBrotherView.cpp : implementation of the CBigBrotherView class
2//
3
4#include "stdafx.h"
5#include "BigBrother.h"
6
7#include "BigBrotherDoc.h"
8#include "BigBrotherView.h"
9#include "HostPropertyPages.h"
10#include "MainFrm.h"
11
12#ifdef _DEBUG
13#define new DEBUG_NEW
14#undef THIS_FILE
15static char THIS_FILE[] = __FILE__;
16#endif
17
18/////////////////////////////////////////////////////////////////////////////
19// CBigBrotherView
20
21IMPLEMENT_DYNCREATE(CBigBrotherView, CTreeView)
22
23BEGIN_MESSAGE_MAP(CBigBrotherView, CTreeView)
24 //{{AFX_MSG_MAP(CBigBrotherView)
25 ON_WM_DESTROY()
26 ON_WM_CREATE()
27 ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
28 ON_NOTIFY_REFLECT(NM_RETURN, OnReturn)
29 ON_MESSAGE(WM_ACTIVITYCOUNT, OnActivityCount)
30 ON_MESSAGE(WM_UPDATETREEBROTHER, OnUpdateTreeBrother)
31 ON_MESSAGE(WM_CHECKQUEUE, OnCheckQueue)
32 ON_WM_TIMER()
33 ON_WM_CONTEXTMENU()
34 ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
35 ON_WM_CHAR()
36 ON_MESSAGE(WM_BROTHERUPDOWN, OnUpDown)
37 ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded)
38 ON_WM_SETFOCUS()
39 //}}AFX_MSG_MAP
40 // Standard printing commands
41 ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
42 ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
43 ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
44END_MESSAGE_MAP()
45
46/////////////////////////////////////////////////////////////////////////////
47// CBigBrotherView construction/destruction
48
49CBigBrotherView::CBigBrotherView()
50 : m_Pages(NULL)
51{
52 // Load Images
53 m_Images.Create(16,16,TRUE,5,2);
54CWinApp *app=AfxGetApp();
55 VERIFY((m_iNoHost=m_Images.Add(app->LoadIcon(IDI_NOHOST)))>=0);
56 VERIFY((m_iHost=m_Images.Add(app->LoadIcon(IDI_HOST)))>=0);
57 VERIFY((m_iGoingDown=m_Images.Add(app->LoadIcon(IDI_GOINGDOWN)))>=0);
58 VERIFY((m_iHostDown=m_Images.Add(app->LoadIcon(IDI_HOSTDOWN)))>=0);
59 VERIFY((m_iPending=m_Images.Add(app->LoadIcon(IDI_PENDING)))>=0);
60 VERIFY((m_iPinging=m_Images.Add(app->LoadIcon(IDI_PINGING)))>=0);
61}
62
63CBigBrotherView::~CBigBrotherView()
64{
65}
66
67BOOL CBigBrotherView::PreCreateWindow(CREATESTRUCT& cs)
68{
69 return CTreeView::PreCreateWindow(cs);
70}
71
72/////////////////////////////////////////////////////////////////////////////
73// CBigBrotherView drawing
74
75void CBigBrotherView::OnDraw(CDC* pDC)
76{
77 CBigBrotherDoc* pDoc = GetDocument();
78 ASSERT(pDoc);
79 ASSERT_VALID(pDoc);
80}
81
82void CBigBrotherView::OnInitialUpdate()
83{
84 CTreeView::OnInitialUpdate();
85
86CWinApp *app=AfxGetApp();
87 ASSERT(app);
88CTreeCtrl& ctl = GetTreeCtrl();
89CBigBrotherDoc* doc = GetDocument();
90 ASSERT(doc);
91 ctl.SetImageList(&m_Images,TVSIL_NORMAL);
92 ctl.ModifyStyle(0,TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS);
93 ctl.DeleteAllItems();
94POSITION p = doc->m_Brotherhood.GetHeadPosition();
95 VERIFY(doc->m_RootBrother);
96 AddBrother(doc->m_RootBrother);
97 while(p){
98 CBrother *b = (CBrother*)doc->m_Brotherhood.GetNext(p);
99 ASSERT(b->m_Daddy);
100 AddBrother(b);
101 }
102 SetupExpansion(doc->m_RootBrother);
103 doc->UpdateAllViews(this);
104CRect rc;
105 GetClientRect(rc);
106 ClientToScreen(rc);
107CRect prc;
108 m_Pages->GetWindowRect(prc);
109CRect nrc;
110 if(m_Pages->m_InitialPosition.x<0 || m_Pages->m_InitialPosition.y<0){
111 nrc.bottom=rc.bottom; nrc.right=rc.right;
112 nrc.top=nrc.bottom-prc.Height(); nrc.left=nrc.right-prc.Width();
113 }else{
114 nrc.left=m_Pages->m_InitialPosition.x;nrc.top=m_Pages->m_InitialPosition.y;
115 nrc.right=nrc.left+prc.Width(); nrc.bottom=nrc.top+prc.Height();
116 }
117 m_Pages->MoveWindow(nrc,FALSE);
118 m_Pages->ShowWindow((doc->flags&CBigBrotherDoc::flagsShowProps)?SW_SHOW:SW_HIDE);
119}
120
121/////////////////////////////////////////////////////////////////////////////
122// CBigBrotherView printing
123
124BOOL CBigBrotherView::OnPreparePrinting(CPrintInfo* pInfo)
125{
126 // default preparation
127 return DoPreparePrinting(pInfo);
128}
129
130void CBigBrotherView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
131{
132}
133
134void CBigBrotherView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
135{
136}
137
138/////////////////////////////////////////////////////////////////////////////
139// CBigBrotherView diagnostics
140
141#ifdef _DEBUG
142void CBigBrotherView::AssertValid() const
143{
144 CTreeView::AssertValid();
145}
146
147void CBigBrotherView::Dump(CDumpContext& dc) const
148{
149 CTreeView::Dump(dc);
150}
151
152CBigBrotherDoc* CBigBrotherView::GetDocument() // non-debug version is inline
153{
154 if(m_pDocument)
155 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBigBrotherDoc)));
156 return (CBigBrotherDoc*)m_pDocument;
157}
158#endif //_DEBUG
159
160/////////////////////////////////////////////////////////////////////////////
161// CBigBrotherView message handlers
162
163void CBigBrotherView::OnDestroy()
164{
165 CTreeView::OnDestroy();
166
167 if(m_Pages){
168 m_Pages->DestroyWindow();
169 delete m_Pages;
170 m_Pages=NULL;
171 }
172}
173
174int CBigBrotherView::OnCreate(LPCREATESTRUCT lpCreateStruct)
175{
176 if (CTreeView::OnCreate(lpCreateStruct) == -1)
177 return -1;
178
179 m_Pages = new CHostPropertyPages(IDS_PROPS_TITLE);
180 m_Pages->m_Daddy=this;
181 m_Pages->Create(this,WS_POPUP||WS_CLIPSIBLINGS|WS_BORDER|WS_DLGFRAME,WS_EX_TOOLWINDOW|WS_EX_WINDOWEDGE);
182
183 return 0;
184}
185
186HTREEITEM CBigBrotherView::FindItem(CBrother* brother)
187{
188DWORD b = (DWORD)brother;
189CTreeCtrl& ctl = GetTreeCtrl();
190HTREEITEM rv = ctl.GetRootItem();
191 ASSERT(rv);
192 do{
193 if(ctl.GetItemData(rv)==b)
194 return rv;
195 }while((rv=ctl.GetNextItem(rv,TVGN_NEXT)));
196 ASSERT(0);
197 return NULL;
198}
199
200CBrother* CBigBrotherView::GetCurrentBrother()
201{
202 HTREEITEMhItem = GetTreeCtrl().GetSelectedItem();
203 if(!hItem)
204 return NULL;
205CBrother *rv = (CBrother*)GetTreeCtrl().GetItemData(hItem);
206 ASSERT(rv);
207 return rv;
208}
209
210void CBigBrotherView::AddBrother(CBrother *b)
211{
212HTREEITEM hDaddy=TVI_ROOT;
213 if(b->m_Daddy){
214 ASSERT(b->m_Daddy->m_Item);
215 hDaddy=b->m_Daddy->m_Item;
216 }
217CString tmp;
218 tmp=b->m_Desc;
219 if(tmp.IsEmpty())
220 tmp.LoadString(IDS_NODESC);
221 b->AppendSeparator();
222 b->m_Item=GetTreeCtrl().InsertItem(TVIF_PARAM|TVIF_TEXT,tmp,0,0,0,0,(LPARAM)b,hDaddy,TVI_LAST);
223 ASSERT(b->m_Item);
224 UpdateBrother(b);
225}
226
227void CBigBrotherView::GotoBrother(CBrother *b)
228{
229 ASSERT(b->m_Item);
230 GetTreeCtrl().SelectItem(b->m_Item);
231}
232
233void CBigBrotherView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
234{
235 NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
236 m_Pages->SetBrother(GetCurrentBrother());
237CBigBrotherDoc *pDoc = GetDocument();
238 if(pDoc)
239 pDoc->UpdateAllViews(this);
240 *pResult = 0;
241}
242
243void CBigBrotherView::UpdateBrother(CBrother* b)
244{
245 if(!b->m_Item)
246 return;
247 ASSERT(b->m_Item);
248CString tmp;
249 tmp=b->m_Desc;
250 if(tmp.IsEmpty())
251 tmp.LoadString(IDS_NODESC);
252 GetTreeCtrl().SetItemText(b->m_Item,tmp);
253 intimage = m_iNoHost;
254 if(!b->m_Host.IsEmpty()){
255 if(b->m_bPending)
256 image = m_iPending;
257 else if(b->m_bPinging)
258 image=m_iPinging;
259 else if(b->m_bPinged){
260 if(b->m_bIsUp){
261 image=m_iHost;
262 if(!b->m_bUp){
263 PostMessage(WM_BROTHERUPDOWN,TRUE,(LPARAM)b);
264 b->m_bUp=TRUE;
265 }
266 }else{
267 if(b->m_HowDown>=b->m_Retries){
268 image=m_iHostDown;
269 if(b->m_bUp){
270 PostMessage(WM_BROTHERUPDOWN,FALSE,(LPARAM)b);
271 b->m_bUp=FALSE;
272 }
273 }else
274 image=m_iGoingDown;
275 b->m_HowDown=min(b->m_HowDown,b->m_Retries+1);
276 }
277 }else
278 image=m_iHost;
279 }
280 GetTreeCtrl().SetItemImage(b->m_Item,image,image);
281 PostMessage(WM_CHECKQUEUE);
282}
283
284void CBigBrotherView::OnReturn(NMHDR* pNMHDR, LRESULT* pResult)
285{
286CBigBrotherDoc *pDoc = GetDocument();
287 ASSERT(pDoc);
288 pDoc->flags|=CBigBrotherDoc::flagsShowProps;
289 m_Pages->ShowWindow(SW_SHOW);
290 m_Pages->SetFocus();
291 *pResult = 0;
292}
293
294LRESULT CBigBrotherView::OnActivityCount(WPARAM wP,LPARAM)
295{
296CMainFrame *mf = (CMainFrame*)(AfxGetApp()->m_pMainWnd);
297 ASSERT(mf);
298 ASSERT(mf->IsKindOf(RUNTIME_CLASS(CMainFrame)));
299int crement = (int)wP;
300CBigBrotherDoc *doc = GetDocument();
301 ASSERT(doc);
302 ASSERT(doc->IsKindOf(RUNTIME_CLASS(CBigBrotherDoc)));
303 doc->m_Threads+=crement;
304 if(doc->m_Threads<0)
305 doc->m_Threads=0;
306 if(doc->m_Threads){
307 mf->m_PingBar.Play(0,(UINT)-1,(UINT)-1);
308 }else{
309 mf->m_PingBar.Stop();
310 mf->m_PingBar.Seek(0);
311 }
312 if(crement<0)
313 PostMessage(WM_CHECKQUEUE);
314 return doc->m_Threads;
315}
316
317LRESULT CBigBrotherView::OnUpdateTreeBrother(WPARAM wP,LPARAM lP)
318{
319CBrother *b = (CBrother*)lP;
320 ASSERT(b);
321 if(wP)
322 b->m_bPinging=FALSE;
323 UpdateBrother(b);
324CBigBrotherDoc *pDoc = GetDocument();
325 if(pDoc)
326 pDoc->UpdateAllViews(this,0,b);
327 return 0;
328}
329
330LRESULT CBigBrotherView::OnCheckQueue(WPARAM,LPARAM)
331{
332CBigBrotherDoc *doc = GetDocument();
333 if(doc)
334 doc->CheckPendingQueue();
335 return 0;
336}
337
338void CBigBrotherView::OnTimer(UINT nIDEvent)
339{
340 switch(nIDEvent){
341 case TM_CHECK:
342 KillTimer(TM_CHECK);
343 PostMessage(WM_CHECKQUEUE);
344 break;
345 }
346 CTreeView::OnTimer(nIDEvent);
347}
348
349void CBigBrotherView::GetFamily(CBrother* b,CBrotherList* bh)
350{
351 if(b->IsValuable())
352 bh->AddTail(b);
353 ASSERT(b->m_Item);
354CTreeCtrl& ctl = GetTreeCtrl();
355 if(!(ctl.GetItemState(b->m_Item,TVIS_EXPANDED)&TVIS_EXPANDED))
356 return;
357HTREEITEM hItem = ctl.GetNextItem(b->m_Item,TVGN_CHILD);
358 while(hItem){
359 CBrother *bb = (CBrother*)ctl.GetItemData(hItem);
360 ASSERT(bb);
361 GetFamily(bb,bh);
362 hItem = ctl.GetNextItem(hItem,TVGN_NEXT);
363 }
364}
365
366void CBigBrotherView::OnContextMenu(CWnd* pWnd, CPoint point)
367{
368 if(point.x<0 || point.y<0){
369 CBrother *b = GetCurrentBrother();
370 ASSERT(b);
371 CRect rc;
372 GetTreeCtrl().GetItemRect(b->m_Item,&rc,TRUE);
373 point = rc.TopLeft();
374 ClientToScreen(&point);
375 }
376 ContextMenu(point);
377}
378
379void CBigBrotherView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
380{
381CPoint pt;
382 VERIFY(GetCursorPos(&pt));
383 ContextMenu(pt);
384 *pResult = 0;
385}
386
387void CBigBrotherView::ContextMenu(CPoint pt)
388{
389CMenu m;
390 VERIFY(m.LoadMenu(IDR_MAINFRAME));
391 CMenu *popUp = m.GetSubMenu(1);// Brothers
392 ASSERT(popUp);
393 popUp->TrackPopupMenu(TPM_CENTERALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x,pt.y,AfxGetApp()->m_pMainWnd);
394}
395
396void CBigBrotherView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
397{
398}
399
400LRESULT CBigBrotherView::OnUpDown(WPARAM wP,LPARAM lP)
401{
402BOOL isUp = (BOOL)wP;
403CBrother *b = (CBrother*)lP;
404 ASSERT(b);
405 UINTstrResource;
406 b->ParentalAdjust();
407 if(isUp){
408 b->m_Up.PerformAction(b);
409 strResource = IDS_LOG_HOSTUP;
410 }else{
411 b->m_Down.PerformAction(b);
412 strResource = IDS_LOG_HOSTDOWN;
413 }
414CString logLine;
415 logLine.Format(strResource,(LPCTSTR)b->m_Desc,(LPCTSTR)b->m_Host);
416 logLine=b->m_Pinged.Format(IDS_LOG_DATEFORMAT)+" "+logLine;
417CBigBrotherDoc *pDoc = GetDocument();
418 ASSERT(pDoc);
419 pDoc->LogLine(logLine);
420 return 0;
421}
422
423void CBigBrotherView::KillBrother(CBrother *b)
424{
425 ASSERT(b->m_Item);
426CTreeCtrl& ctl = GetTreeCtrl();
427HTREEITEM hItem = ctl.GetNextItem(b->m_Item,TVGN_CHILD);
428 while(hItem){
429 CBrother *bb = (CBrother*)ctl.GetItemData(hItem);
430 ASSERT(bb);
431 KillBrother(bb);
432 hItem = ctl.GetNextItem(b->m_Item,TVGN_CHILD);
433 }
434 if(b->m_Daddy)
435 KillOneBrother(b);
436 // ??? Else - Cleanup?
437}
438
439void CBigBrotherView::KillOneBrother(CBrother *b)
440{
441 ASSERT(b);
442 ASSERT(b->m_Item);
443CTreeCtrl& ctl = GetTreeCtrl();
444 ASSERT(!ctl.ItemHasChildren(b->m_Item));
445 ctl.DeleteItem(b->m_Item);
446 VERIFY(b->m_Doc=GetDocument());
447 b->m_Item=NULL;
448 b->Suicide();
449}
450
451void CBigBrotherView::SetupExpansion(CBrother *b)
452{
453 ASSERT(b->m_Item);
454CTreeCtrl& ctl = GetTreeCtrl();
455 if(b->flags&CBrother::flagsExpandedTree)
456 ctl.Expand(b->m_Item,TVE_EXPAND);
457HTREEITEM hItem = ctl.GetNextItem(b->m_Item,TVGN_CHILD);
458 while(hItem){
459 CBrother *bb = (CBrother*)ctl.GetItemData(hItem);
460 ASSERT(bb);
461 SetupExpansion(bb);
462 hItem = ctl.GetNextItem(bb->m_Item,TVGN_NEXT);
463 }
464 if(b->flags&CBrother::flagsCurrentBrother)
465 GotoBrother(b);
466}
467
468void CBigBrotherView::OnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult)
469{
470NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
471CBigBrotherDoc *pDoc = GetDocument();
472 if(pDoc)
473 pDoc->UpdateAllViews(this);
474 *pResult = 0;
475}
476
477void CBigBrotherView::OnSetFocus(CWnd* pOldWnd)
478{
479 CTreeView::OnSetFocus(pOldWnd);
480
481CBigBrotherDoc *pDoc = GetDocument();
482 if(!(pDoc && m_Pages))
483 return;
484 m_Pages->ShowWindow((pDoc->flags&CBigBrotherDoc::flagsShowProps)?SW_SHOW:SW_HIDE);
485}