summaryrefslogtreecommitdiffabout
path: root/MainFrm.cpp
blob: 97de7b2250457e505f3b2ab999962573f67c7a9d (plain)
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
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "BigBrother.h"

#include "MainFrm.h"
#include "BigBrotherView.h"
#include "ActivityView.h"
#include "BigBrotherDoc.h"
#include "HostPropertyPages.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_UPDATE_COMMAND_UI(ID_VIEW_MAINWINDOW, OnUpdateViewMainwindow)
	ON_COMMAND(ID_VIEW_MAINWINDOW, OnViewMainwindow)
	ON_WM_WINDOWPOSCHANGING()
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_PINGBAR, OnUpdateIndicatorPingbar)
	ON_WM_CLOSE()
	ON_MESSAGE(WM_TRAYICONMESSAGE, OnNotifyIcon)
	ON_WM_QUERYENDSESSION()
	//}}AFX_MSG_MAP
	// Global help commands
	ON_COMMAND(ID_HELP_FINDER, CFrameWnd::OnHelpFinder)
	ON_COMMAND(ID_HELP, CFrameWnd::OnHelp)
	ON_COMMAND(ID_CONTEXT_HELP, CFrameWnd::OnContextHelp)
	ON_COMMAND(ID_DEFAULT_HELP, CFrameWnd::OnHelpFinder)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_INDICATOR_PINGBAR,
	ID_SEPARATOR           // status line indicator
/*	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL*/
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	m_bExiting=FALSE;
	m_bShuttingDown=FALSE;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.Create(this) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this,WS_CHILD | WS_VISIBLE | CBRS_BOTTOM|CBRS_SIZE_DYNAMIC) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	m_wndStatusBar.SetPaneStyle(1,m_wndStatusBar.GetPaneStyle(1)|SBPS_STRETCH);
	VERIFY(m_PingBar.Create(WS_CHILD|WS_VISIBLE|ACS_TRANSPARENT,CRect(0,0,0,0),&m_wndStatusBar,ID_INDICATOR_PINGBAR));
	m_PingBar.Open(IDR_PINGBAR);
CRect rc;
	m_PingBar.GetWindowRect(rc);
int nHorz, nVert, nSpacing;
	m_wndStatusBar.GetStatusBarCtrl().GetBorders(nHorz,nVert,nSpacing);
	m_wndStatusBar.GetStatusBarCtrl().SetMinHeight(rc.Height()+nVert*2);
	m_wndStatusBar.SetTheMinHeight(rc.Height()+nVert*2);
	m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(ID_INDICATOR_PINGBAR),ID_INDICATOR_PINGBAR,SBPS_NORMAL,rc.Width());
	m_PingBar.SetWindowPos(NULL,nHorz+nSpacing+1,nVert+1,0,0,SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOZORDER);

	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
		CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

NOTIFYICONDATA nid;
	memset(&nid,0,sizeof(nid));
	nid.cbSize=sizeof(nid);
	nid.hWnd=m_hWnd;
	nid.uID=IDC_TRAYICON;
	nid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
	nid.uCallbackMessage=WM_TRAYICONMESSAGE;
	nid.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	// *** Load from resource
	strcpy(nid.szTip,"Big Brother");
	VERIFY(Shell_NotifyIcon(NIM_ADD,&nid));
	return 0;
}

BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{
	if(!m_wndSplitter.CreateStatic(this,2,1)){
		TRACE0("Failed to create static splitter\n");
		return FALSE;
	}
CRect rc;
	GetClientRect(rc);
int cySize = AfxGetApp()->GetProfileInt("SplitterPosition","TreeSize",-1);
	if(!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CBigBrotherView),CSize(0,(cySize<0)?(rc.Height()/2):cySize),pContext)){
		TRACE0("Failed to create Tree View\n");
		return FALSE;
	}
	cySize = AfxGetApp()->GetProfileInt("SplitterPosition","ActivitySize",-1);
	if(!m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CActivityView),CSize(0,(cySize<0)?(rc.Height()/2):cySize),pContext)){
		TRACE0("Failed to create Activity View\n");
		return FALSE;
	}
	return TRUE;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
CRect rc;
CWinApp *app = AfxGetApp();
	rc.left=app->GetProfileInt("FramePosition","left",-1);
	rc.top=app->GetProfileInt("FramePosition","top",-1);
	rc.right=app->GetProfileInt("FramePosition","right",-1);
	rc.bottom=app->GetProfileInt("FramePosition","bottom",-1);
	if(rc.left>=0 && rc.top>=0 && rc.right>=0 && rc.bottom>=0){
		cs.x=rc.left, cs.y=rc.top;
		cs.cx=rc.Width(), cs.cy=rc.Height();
	}
int	tmp = app->GetProfileInt("FramePosition","shown",-1);
	if(!tmp){
		cs.style&=~WS_VISIBLE;
		m_bShown=FALSE;
	}else
		m_bShown=TRUE;
	cs.style&=~WS_MINIMIZEBOX|WS_MAXIMIZEBOX;
	return CFrameWnd::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnDestroy() 
{
	CFrameWnd::OnDestroy();
	
	m_bExiting=TRUE;

	// Save window size and position
CRect rc;
	GetWindowRect(rc);
CWinApp *app = AfxGetApp();
	app->WriteProfileInt("FramePosition","left",rc.left);
	app->WriteProfileInt("FramePosition","top",rc.top);
	app->WriteProfileInt("FramePosition","right",rc.right);
	app->WriteProfileInt("FramePosition","bottom",rc.bottom);
int cyCur,cyMin;
	m_wndSplitter.GetRowInfo(0,cyCur,cyMin);
	app->WriteProfileInt("SplitterPosition","TreeSize",cyCur);
	m_wndSplitter.GetRowInfo(1,cyCur,cyMin);
	app->WriteProfileInt("SplitterPosition","ActivitySize",cyCur);
	app->WriteProfileInt("FramePosition","shown",m_bShown);

NOTIFYICONDATA nid;
	memset(&nid,0,sizeof(nid));
	nid.cbSize=sizeof(nid);
	nid.hWnd=m_hWnd;
	nid.uID=IDC_TRAYICON;
	nid.uFlags=0;
	VERIFY(Shell_NotifyIcon(NIM_DELETE,&nid));
}

LRESULT CMainFrame::OnNotifyIcon(WPARAM wP,LPARAM lP)
{
	ASSERT(wP==IDC_TRAYICON);
	switch(lP){
	case WM_LBUTTONDOWN:
		OnViewMainwindow();
		break;
	case WM_RBUTTONDOWN:
		{
		CMenu menu;
			VERIFY(menu.LoadMenu(IDM_POPUPS));
		CMenu *popUp = menu.GetSubMenu(0);
			ASSERT(popUp);
		CPoint pt;
			VERIFY(::GetCursorPos(&pt));
			popUp->TrackPopupMenu(TPM_RIGHTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x,pt.y,this,NULL);
		}
		break;
	default:
/*		{
		CString tmp;
			tmp.Format("NIMESSAGE: %08lX\n",lP);
			TRACE0(tmp);
		}*/
		break;
	}
	return 0;
}

void CMainFrame::FrameDisplayState(BOOL bShow)
{
	m_bShown=bShow;
	ShowWindow(bShow?SW_SHOW:SW_HIDE);
CBigBrotherDoc *doc = (CBigBrotherDoc*)GetActiveDocument();
	if(doc){
		ASSERT(doc->IsKindOf(RUNTIME_CLASS(CBigBrotherDoc)));
		ASSERT(doc->m_BBView);
		ASSERT(doc->m_BBView->m_Pages);
		ASSERT(::IsWindow(doc->m_BBView->m_Pages->m_hWnd));
		doc->m_BBView->m_Pages->ShowWindow(bShow?SW_SHOW:SW_HIDE);
	}
	if(bShow){
		SetForegroundWindow();
		SetFocus();
	}
}

void CMainFrame::OnUpdateViewMainwindow(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(IsWindowVisible()?1:0);
}

void CMainFrame::OnViewMainwindow() 
{
	FrameDisplayState(!IsWindowVisible());
}

void CMainFrame::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos) 
{
	if(!m_bExiting){
		if(m_bShown){
			lpwndpos->flags&=~SWP_HIDEWINDOW;
			lpwndpos->flags|=SWP_SHOWWINDOW;
		}else{
			lpwndpos->flags&=~SWP_SHOWWINDOW;
			lpwndpos->flags|=SWP_HIDEWINDOW;
		}
	}
	CFrameWnd::OnWindowPosChanging(lpwndpos);
}

void CMainFrame::OnUpdateIndicatorPingbar(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(TRUE);
}

void CMainFrame::OnClose() 
{
	m_bExiting=TRUE;
	CFrameWnd::OnClose();
}

BOOL CMainFrame::OnQueryEndSession() 
{
	m_bShuttingDown=TRUE;
	if (!CFrameWnd::OnQueryEndSession())
		return FALSE;
	::PostQuitMessage(0);
	return TRUE;
}