Rollback from r3608 to r3606
[public/netxms.git] / src / console / win32 / ObjectTree.cpp
1 // ObjectTree.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6 #include "ObjectTree.h"
7
8 #ifdef _DEBUG
9 #define new DEBUG_NEW
10 #undef THIS_FILE
11 static char THIS_FILE[] = __FILE__;
12 #endif
13
14
15 #define STATUS_BAR_WIDTH 20
16
17
18 /////////////////////////////////////////////////////////////////////////////
19 // CObjectTree
20
21 CObjectTree::CObjectTree()
22 {
23 m_pwndTreeCtrl = NULL;
24 m_bUseIcons = theApp.GetProfileInt(_T("ObjectTree"), _T("UseIcons"), FALSE);
25 m_bHideNormal = theApp.GetProfileInt(_T("ObjectTree"), _T("HideNormal"), TRUE);
26 m_bHideUnknown = theApp.GetProfileInt(_T("ObjectTree"), _T("HideUnknown"), TRUE);
27 m_bHideUnmanaged = theApp.GetProfileInt(_T("ObjectTree"), _T("HideUnmanaged"), TRUE);
28 m_bHideNonOperational = theApp.GetProfileInt(_T("ObjectTree"), _T("HideNonOperational"), TRUE);
29 m_hFirstVisibleItem = NULL;
30 }
31
32 CObjectTree::~CObjectTree()
33 {
34 theApp.WriteProfileInt(_T("ObjectTree"), _T("HideNormal"), m_bHideNormal);
35 theApp.WriteProfileInt(_T("ObjectTree"), _T("HideUnknown"), m_bHideUnknown);
36 theApp.WriteProfileInt(_T("ObjectTree"), _T("HideUnmanaged"), m_bHideUnmanaged);
37 theApp.WriteProfileInt(_T("ObjectTree"), _T("HideNonOperational"), m_bHideNonOperational);
38 theApp.WriteProfileInt(_T("ObjectTree"), _T("UseIcons"), m_bUseIcons);
39 }
40
41
42 BEGIN_MESSAGE_MAP(CObjectTree, CWnd)
43 //{{AFX_MSG_MAP(CObjectTree)
44 ON_WM_CREATE()
45 ON_WM_SIZE()
46 ON_WM_PAINT()
47 ON_WM_CONTEXTMENU()
48 ON_COMMAND(ID_STATUS_HIDENORMAL, OnStatusHidenormal)
49 ON_COMMAND(ID_STATUS_HIDENONOPERATIONAL, OnStatusHidenonoperational)
50 ON_COMMAND(ID_STATUS_HIDEUNKNOWN, OnStatusHideunknown)
51 ON_COMMAND(ID_STATUS_HIDEUNMANAGED, OnStatusHideunmanaged)
52 ON_COMMAND(ID_STATUS_ICONS, OnStatusIcons)
53 //}}AFX_MSG_MAP
54 ON_MESSAGE(NXCM_CHILD_VSCROLL, OnTreeViewVScroll)
55 END_MESSAGE_MAP()
56
57
58 /////////////////////////////////////////////////////////////////////////////
59 // CObjectTree message handlers
60
61 BOOL CObjectTree::PreCreateWindow(CREATESTRUCT& cs)
62 {
63 if (cs.lpszClass == NULL)
64 cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,
65 LoadCursor(NULL, IDC_ARROW),
66 NULL, NULL);
67 return CWnd::PreCreateWindow(cs);
68 }
69
70
71 //
72 // WM_CREATE message handler
73 //
74
75 int CObjectTree::OnCreate(LPCREATESTRUCT lpCreateStruct)
76 {
77 if (CWnd::OnCreate(lpCreateStruct) == -1)
78 return -1;
79
80 m_imageList.Create(16, 16, ILC_COLOR24 | ILC_MASK, 5, 1);
81 m_imageList.Add(theApp.LoadIcon(IDI_SEVERITY_NORMAL));
82 m_imageList.Add(theApp.LoadIcon(IDI_SEVERITY_WARNING));
83 m_imageList.Add(theApp.LoadIcon(IDI_SEVERITY_MINOR));
84 m_imageList.Add(theApp.LoadIcon(IDI_SEVERITY_MAJOR));
85 m_imageList.Add(theApp.LoadIcon(IDI_SEVERITY_CRITICAL));
86
87 return 0;
88 }
89
90
91 //
92 // WM_SIZE message handler
93 //
94
95 void CObjectTree::OnSize(UINT nType, int cx, int cy)
96 {
97 CWnd::OnSize(nType, cx, cy);
98 if (m_pwndTreeCtrl != NULL)
99 m_pwndTreeCtrl->SetWindowPos(NULL, STATUS_BAR_WIDTH, 0, cx - STATUS_BAR_WIDTH, cy, SWP_NOZORDER);
100 }
101
102
103 //
104 // WM_NOTIFY message handler
105 //
106
107 BOOL CObjectTree::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
108 {
109 HTREEITEM hItem;
110
111 if (m_pwndTreeCtrl != NULL)
112 {
113 if (wParam == (WPARAM)m_pwndTreeCtrl->GetDlgCtrlID())
114 {
115 NMTREEVIEW *pn = (LPNMTREEVIEW)lParam;
116
117 switch(pn->hdr.code)
118 {
119 case TVN_ITEMEXPANDED:
120 UpdateStatusBar();
121 break;
122 case TVN_SELCHANGED:
123 hItem = m_pwndTreeCtrl->GetNextItem(NULL, TVGN_FIRSTVISIBLE);
124 if (hItem != m_hFirstVisibleItem)
125 {
126 UpdateStatusBar();
127 m_hFirstVisibleItem = hItem;
128 }
129 break;
130 }
131 }
132 }
133 *pResult = GetParent()->SendMessage(WM_NOTIFY, wParam, lParam);
134 return TRUE;
135 }
136
137
138 //
139 // Check if mark for given status should be shown
140 //
141
142 BOOL CObjectTree::IsStatusVisible(int nStatus)
143 {
144 switch(nStatus)
145 {
146 case STATUS_NORMAL:
147 return !m_bHideNormal;
148 case STATUS_UNKNOWN:
149 return !m_bHideUnknown;
150 case STATUS_UNMANAGED:
151 return !m_bHideUnmanaged;
152 case STATUS_DISABLED:
153 case STATUS_TESTING:
154 return !m_bHideNonOperational;
155 }
156 return TRUE;
157 }
158
159
160 //
161 // WM_PAINT message handler
162 //
163
164 void CObjectTree::OnPaint()
165 {
166 CPaintDC dc(this); // device context for painting
167 CDC mdc;
168 CBitmap bitmap, *oldBitmap;
169 RECT rcClient, rcItem;
170 CPen pen, *pOldPen;
171 CBrush brush, *pOldBrush;
172 HTREEITEM hItem;
173 int nStatus, nSize;
174
175 GetClientRect(&rcClient);
176
177 pen.CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
178 pOldPen = dc.SelectObject(&pen);
179 dc.MoveTo(STATUS_BAR_WIDTH - 1, 0);
180 dc.LineTo(STATUS_BAR_WIDTH - 1, rcClient.bottom);
181 dc.SelectObject(pOldPen);
182
183 mdc.CreateCompatibleDC(&dc);
184 bitmap.CreateCompatibleBitmap(&dc, STATUS_BAR_WIDTH - 1, rcClient.bottom);
185 oldBitmap = mdc.SelectObject(&bitmap);
186 mdc.FillSolidRect(0, 0, STATUS_BAR_WIDTH - 1, rcClient.bottom, GetSysColor(COLOR_WINDOW));
187
188 hItem = m_pwndTreeCtrl->GetNextItem(NULL, TVGN_FIRSTVISIBLE);
189 while(hItem != NULL)
190 {
191 nStatus = m_pwndTreeCtrl->GetItemState(hItem, TVIS_OVERLAYMASK) >> 8;
192 m_pwndTreeCtrl->GetItemRect(hItem, &rcItem, FALSE);
193 if (IsStatusVisible(nStatus))
194 {
195 if (m_bUseIcons)
196 {
197 m_imageList.Draw(&mdc, nStatus, CPoint(2, rcItem.top), ILD_TRANSPARENT);
198 }
199 else
200 {
201 nSize = min(rcItem.bottom - rcItem.top, STATUS_BAR_WIDTH - 5);
202 if (nSize > 10)
203 nSize = 10;
204 rcItem.left = (STATUS_BAR_WIDTH - nSize) / 2;
205 rcItem.right = rcItem.left + nSize;
206 rcItem.top = rcItem.top + (rcItem.bottom - rcItem.top - nSize) / 2;
207 rcItem.bottom = rcItem.top + nSize;
208
209 brush.CreateSolidBrush(g_statusColorTable[nStatus]);
210 pOldBrush = mdc.SelectObject(&brush);
211 mdc.Ellipse(&rcItem);
212 //dc.FillSolidRect(&rcItem, g_statusColorTable[nStatus]);
213 mdc.SelectObject(pOldBrush);
214 brush.DeleteObject();
215 }
216 }
217 hItem = m_pwndTreeCtrl->GetNextItem(hItem, TVGN_NEXTVISIBLE);
218 }
219
220 dc.BitBlt(0, 0, STATUS_BAR_WIDTH - 1, rcClient.bottom, &mdc, 0, 0, SRCCOPY);
221 mdc.SelectObject(oldBitmap);
222 mdc.DeleteDC();
223 }
224
225
226 //
227 // WM_CONTEXTMENU message handler
228 //
229
230 void CObjectTree::OnContextMenu(CWnd* pWnd, CPoint point)
231 {
232 CPoint pt = point;
233
234 ScreenToClient(&pt);
235 if ((pt.x >= 0) && (pt.x < STATUS_BAR_WIDTH))
236 {
237 CMenu *pMenu;
238
239 pMenu = theApp.GetContextMenu(25);
240 pMenu->CheckMenuItem(ID_STATUS_HIDENORMAL, MF_BYCOMMAND | (m_bHideNormal ? MF_CHECKED : MF_UNCHECKED));
241 pMenu->CheckMenuItem(ID_STATUS_HIDEUNKNOWN, MF_BYCOMMAND | (m_bHideUnknown ? MF_CHECKED : MF_UNCHECKED));
242 pMenu->CheckMenuItem(ID_STATUS_HIDEUNMANAGED, MF_BYCOMMAND | (m_bHideUnmanaged ? MF_CHECKED : MF_UNCHECKED));
243 pMenu->CheckMenuItem(ID_STATUS_HIDENONOPERATIONAL, MF_BYCOMMAND | (m_bHideNonOperational ? MF_CHECKED : MF_UNCHECKED));
244 pMenu->CheckMenuItem(ID_STATUS_ICONS, MF_BYCOMMAND | (m_bUseIcons ? MF_CHECKED : MF_UNCHECKED));
245 pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
246 }
247 else
248 {
249 // Pass to parent
250 GetParent()->SendMessage(WM_CONTEXTMENU, (WPARAM)pWnd->m_hWnd, MAKELPARAM(point.x, point.y));
251 }
252 }
253
254
255 //
256 // Handlers for context menu items
257 //
258
259 void CObjectTree::OnStatusHidenormal()
260 {
261 m_bHideNormal = !m_bHideNormal;
262 InvalidateRect(NULL);
263 }
264
265 void CObjectTree::OnStatusHidenonoperational()
266 {
267 m_bHideNonOperational = !m_bHideNonOperational;
268 InvalidateRect(NULL);
269 }
270
271 void CObjectTree::OnStatusHideunknown()
272 {
273 m_bHideUnknown = !m_bHideUnknown;
274 InvalidateRect(NULL);
275 }
276
277 void CObjectTree::OnStatusHideunmanaged()
278 {
279 m_bHideUnmanaged = !m_bHideUnmanaged;
280 InvalidateRect(NULL);
281 }
282
283 void CObjectTree::OnStatusIcons()
284 {
285 m_bUseIcons = !m_bUseIcons;
286 InvalidateRect(NULL);
287 }
288
289
290 //
291 // Update status bar
292 //
293
294 void CObjectTree::UpdateStatusBar()
295 {
296 RECT rect;
297
298 GetClientRect(&rect);
299 rect.right = STATUS_BAR_WIDTH;
300 InvalidateRect(&rect, FALSE);
301 }
302
303
304 //
305 // NXCM_CHILD_VSCROLL message handler
306 //
307
308 void CObjectTree::OnTreeViewVScroll(WPARAM wParam, LPARAM lParam)
309 {
310 UpdateStatusBar();
311 }