- Finished migration to new client library model
[public/netxms.git] / src / console / win32 / MIBBrowserDlg.cpp
CommitLineData
34870fc0
VK
1// MIBBrowserDlg.cpp : implementation file
2//
3
4#include "stdafx.h"
5#include "nxcon.h"
6#include "MIBBrowserDlg.h"
9d2d285e 7#include "DataQueryDlg.h"
34870fc0
VK
8
9#ifdef _DEBUG
10#define new DEBUG_NEW
11#undef THIS_FILE
12static char THIS_FILE[] = __FILE__;
13#endif
14
15
16//
ec2aa1a9
VK
17// Externals
18//
19
20void BuildOIDString(struct tree *pNode, char *pszBuffer);
9d2d285e 21char *BuildSymbolicOIDString(struct tree *pNode, DWORD dwInstance);
ec2aa1a9
VK
22
23
24//
34870fc0
VK
25// Control id's in expandable area
26//
27
28static int m_iExtCtrlId[] = { IDC_STATIC_DESCRIPTION, IDC_EDIT_DESCRIPTION,
29 IDC_STATIC_TYPE, IDC_EDIT_TYPE, IDC_STATIC_ACCESS,
30 IDC_EDIT_ACCESS, IDC_STATIC_STATUS, IDC_EDIT_STATUS, -1 };
31
32
33/////////////////////////////////////////////////////////////////////////////
34// CMIBBrowserDlg dialog
35
36
37CMIBBrowserDlg::CMIBBrowserDlg(CWnd* pParent /*=NULL*/)
38 : CDialog(CMIBBrowserDlg::IDD, pParent)
39{
40 //{{AFX_DATA_INIT(CMIBBrowserDlg)
41 m_strOID = _T("");
9d2d285e 42 m_dwInstance = 0;
34870fc0
VK
43 //}}AFX_DATA_INIT
44 m_bIsExpanded = FALSE;
9d2d285e
VK
45 m_bDisableOIDUpdate = FALSE;
46 m_pNode = NULL;
21ea6058 47 m_bUseInstance = TRUE;
34870fc0
VK
48}
49
50
51void CMIBBrowserDlg::DoDataExchange(CDataExchange* pDX)
52{
53 CDialog::DoDataExchange(pDX);
54 //{{AFX_DATA_MAP(CMIBBrowserDlg)
9d2d285e 55 DDX_Control(pDX, IDC_EDIT_INSTANCE, m_wndEditInstance);
34870fc0
VK
56 DDX_Control(pDX, IDC_EDIT_TYPE, m_wndEditType);
57 DDX_Control(pDX, IDC_EDIT_STATUS, m_wndEditStatus);
58 DDX_Control(pDX, IDC_EDIT_OID_TEXT, m_wndEditOIDText);
59 DDX_Control(pDX, IDC_EDIT_ACCESS, m_wndEditAccess);
60 DDX_Control(pDX, IDC_BUTTON_DETAILS, m_wndButtonDetails);
61 DDX_Control(pDX, IDC_EDIT_DESCRIPTION, m_wndEditDescription);
62 DDX_Control(pDX, IDC_TREE_MIB, m_wndTreeCtrl);
63 DDX_Control(pDX, IDC_EDIT_OID, m_wndEditOID);
64 DDX_Text(pDX, IDC_EDIT_OID, m_strOID);
9d2d285e 65 DDX_Text(pDX, IDC_EDIT_INSTANCE, m_dwInstance);
34870fc0
VK
66 //}}AFX_DATA_MAP
67}
68
69
70BEGIN_MESSAGE_MAP(CMIBBrowserDlg, CDialog)
71 //{{AFX_MSG_MAP(CMIBBrowserDlg)
72 ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_MIB, OnSelchangedTreeMib)
73 ON_BN_CLICKED(IDC_BUTTON_DETAILS, OnButtonDetails)
74 ON_WM_CTLCOLOR()
9d2d285e
VK
75 ON_EN_CHANGE(IDC_EDIT_OID, OnChangeEditOid)
76 ON_EN_CHANGE(IDC_EDIT_INSTANCE, OnChangeEditInstance)
77 ON_BN_CLICKED(IDC_BUTTON_GET, OnButtonGet)
34870fc0
VK
78 //}}AFX_MSG_MAP
79END_MESSAGE_MAP()
80
81/////////////////////////////////////////////////////////////////////////////
82// CMIBBrowserDlg message handlers
83
84
85//
86// WM_INITDIALOG message handler
87//
88
89BOOL CMIBBrowserDlg::OnInitDialog()
90{
91 RECT rect1, rect2;
c46c78f7
VK
92 oid oidName[MAX_OID_LEN];
93 unsigned int uNameLen = MAX_OID_LEN;
34870fc0
VK
94
95 CDialog::OnInitDialog();
96
97 // Get expanded dialog box size
98 m_sizeExpanded = GetWindowSize(this);
99
100 // Calculate collapsed dialog box size
101 m_wndButtonDetails.GetWindowRect(&rect1);
102 ScreenToClient(&rect1);
103 m_wndEditDescription.GetWindowRect(&rect2);
104 ScreenToClient(&rect2);
105 m_sizeCollapsed = m_sizeExpanded;
106 m_sizeCollapsed.cy -= rect2.bottom - rect1.bottom;
107
108 // By default, dialog is collapsed
109 Collapse();
110
111 // Fill MIB tree
112 memset(&m_mibTreeRoot, 0, sizeof(struct tree));
113 m_mibTreeRoot.label = "[root]";
114 m_mibTreeRoot.child_list = get_tree_head();
115 AddTreeNode(TVI_ROOT, &m_mibTreeRoot);
c46c78f7
VK
116
117 // Select node with given OID (or closest match)
118 if (read_objid((LPCTSTR)m_strOID, oidName, &uNameLen) != 0)
9d2d285e 119 {
c46c78f7 120 SelectNode(m_wndTreeCtrl.GetChildItem(TVI_ROOT), oidName, uNameLen);
9d2d285e
VK
121 }
122
123 // Change dialog title
124 if (m_pNode != NULL)
125 {
126 CString strTitle;
127
128 GetWindowText(strTitle);
129 strTitle += " (";
130 strTitle += m_pNode->szName;
131 strTitle += ")";
132 SetWindowText(strTitle);
133 }
134
21ea6058 135 // Disable "instance" field if it's not needed
1943e452 136 // If instance is disabled, hide "Get..." button as well
21ea6058
VK
137 if (!m_bUseInstance)
138 {
139 CWnd *pWnd;
140
141 m_wndEditInstance.EnableWindow(FALSE);
142 m_wndEditInstance.SetWindowText(_T(""));
143 pWnd = GetDlgItem(IDC_STATIC_INSTANCE);
144 if (pWnd != NULL)
145 pWnd->EnableWindow(FALSE);
1943e452
VK
146
147 pWnd = GetDlgItem(IDC_BUTTON_GET);
148 if (pWnd != NULL)
149 pWnd->ShowWindow(SW_HIDE);
21ea6058
VK
150 }
151
34870fc0
VK
152 return TRUE;
153}
154
155
156//
157// Add node to tree
158//
159
160void CMIBBrowserDlg::AddTreeNode(HTREEITEM hRoot, tree *pCurrNode)
161{
162 struct tree *pNode;
163 HTREEITEM hItem;
164
165 hItem = m_wndTreeCtrl.InsertItem(pCurrNode->label, 0, 0, hRoot, TVI_LAST);
166 m_wndTreeCtrl.SetItemData(hItem, (DWORD)pCurrNode);
167 for(pNode = pCurrNode->child_list; pNode != NULL; pNode = pNode->next_peer)
168 AddTreeNode(hItem, pNode);
169 m_wndTreeCtrl.SortChildren(hItem);
170}
171
172
173//
174// Handler for selection change in MIB tree
175//
176
177void CMIBBrowserDlg::OnSelchangedTreeMib(NMHDR *pNMHDR, LRESULT *pResult)
178{
179 NMTREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
180 struct tree *pNode;
181 char *pszTemp, szBuffer[MAX_OID_LEN * 8];
182
183 pNode = (struct tree *)m_wndTreeCtrl.GetItemData(pNMTreeView->itemNew.hItem);
9d2d285e
VK
184
185 if (!m_bDisableOIDUpdate)
186 {
187 BuildOIDString(pNode, szBuffer);
188 m_wndEditOID.SetWindowText(szBuffer);
189 }
190
34870fc0
VK
191 pszTemp = TranslateUNIXText(pNode->description != NULL ? pNode->description : "");
192 m_wndEditDescription.SetWindowText(pszTemp);
193 free(pszTemp);
9d2d285e 194
34870fc0
VK
195 m_wndEditStatus.SetWindowText(CodeToText(pNode->status, g_ctSnmpMibStatus, ""));
196 m_wndEditAccess.SetWindowText(CodeToText(pNode->access, g_ctSnmpMibAccess, ""));
197 m_wndEditType.SetWindowText(CodeToText(pNode->type, g_ctSnmpMibType, ""));
9d2d285e
VK
198
199 pszTemp = BuildSymbolicOIDString(pNode, m_dwInstance);
34870fc0
VK
200 m_wndEditOIDText.SetWindowText(pszTemp);
201 free(pszTemp);
9d2d285e 202
34870fc0
VK
203 *pResult = 0;
204}
205
206
207//
208// Expand dialog box
209//
210
211void CMIBBrowserDlg::Expand()
212{
213 SetWindowPos(NULL, 0, 0, m_sizeExpanded.cx, m_sizeExpanded.cy, SWP_NOZORDER | SWP_NOMOVE);
214 ShowExtControls(TRUE);
215 m_wndButtonDetails.SetWindowText("&Details <<<");
216 m_bIsExpanded = TRUE;
217}
218
219
220//
221// Collapse dialog box
222//
223
224void CMIBBrowserDlg::Collapse()
225{
226 ShowExtControls(FALSE);
227 SetWindowPos(NULL, 0, 0, m_sizeCollapsed.cx, m_sizeCollapsed.cy, SWP_NOZORDER | SWP_NOMOVE);
228 m_wndButtonDetails.SetWindowText("&Details >>>");
229 m_bIsExpanded = FALSE;
230}
231
232
233//
234// Show or hide controls in expandable area
235//
236
237void CMIBBrowserDlg::ShowExtControls(BOOL bShow)
238{
239 int i;
240
241 for(i = 0; m_iExtCtrlId[i] != -1; i++)
242 ::ShowWindow(::GetDlgItem(m_hWnd, m_iExtCtrlId[i]), bShow ? SW_SHOW : SW_HIDE);
243}
244
245
246//
247// WM_COMMAND::IDC_BUTTON_DETAILS message handler
248//
249
250void CMIBBrowserDlg::OnButtonDetails()
251{
252 if (m_bIsExpanded)
253 Collapse();
254 else
255 Expand();
256}
257
258
259//
260// WM_CTLCOLOR message handler
261//
262
263HBRUSH CMIBBrowserDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
264{
265 int nId;
266 HBRUSH hbr;
267
268 nId = pWnd->GetDlgCtrlID();
9d2d285e 269 if ((nId == IDC_EDIT_DESCRIPTION) ||
34870fc0
VK
270 (nId == IDC_EDIT_STATUS) || (nId == IDC_EDIT_OID_TEXT) ||
271 (nId == IDC_EDIT_ACCESS) || (nId == IDC_EDIT_TYPE))
272 {
273 hbr = GetSysColorBrush(COLOR_INFOBK);
274 pDC->SetTextColor(GetSysColor(COLOR_INFOTEXT));
275 pDC->SetBkColor(GetSysColor(COLOR_INFOBK));
276 }
277 else
278 {
279 hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
280 }
281 return hbr;
282}
c46c78f7
VK
283
284
285//
286// Select node with given OID
287//
288
289void CMIBBrowserDlg::SelectNode(HTREEITEM hRoot, oid *oidName, unsigned int uNameLen)
290{
291 HTREEITEM hItem;
292 struct tree *pNode;
293 BOOL bMatch = FALSE;
294
295 for(hItem = m_wndTreeCtrl.GetChildItem(hRoot); hItem != NULL;
296 hItem = m_wndTreeCtrl.GetNextItem(hItem, TVGN_NEXT))
297 {
298 pNode = (struct tree *)m_wndTreeCtrl.GetItemData(hItem);
299 if (pNode->subid == oidName[0])
300 {
301 bMatch = TRUE;
302 if (uNameLen == 1)
303 m_wndTreeCtrl.SelectItem(hItem);
304 else
305 SelectNode(hItem, &oidName[1], uNameLen - 1);
306 }
307 }
308 if ((!bMatch) && (hRoot != TVI_ROOT))
309 m_wndTreeCtrl.SelectItem(hRoot);
310}
9d2d285e
VK
311
312
313//
314// Handle manual OID change
315//
316
317void CMIBBrowserDlg::OnChangeEditOid()
318{
319 TCHAR szBuffer[1024];
320 oid oidName[MAX_OID_LEN];
321 unsigned int uNameLen = MAX_OID_LEN;
322
323 // Select node with given OID (or closest match)
324 m_wndEditOID.GetWindowText(szBuffer, 1024);
325 if (read_objid(szBuffer, oidName, &uNameLen) != 0)
326 {
327 m_bDisableOIDUpdate = TRUE; // Prevent edit box text replacement when tree selection changes
328 SelectNode(m_wndTreeCtrl.GetChildItem(TVI_ROOT), oidName, uNameLen);
329 m_bDisableOIDUpdate = FALSE;
330 }
331}
332
333
334//
335// Handle instance change
336//
337
338void CMIBBrowserDlg::OnChangeEditInstance()
339{
340 TCHAR szBuffer[32];
341 HTREEITEM hItem;
342
343 m_wndEditInstance.GetWindowText(szBuffer, 32);
344 m_dwInstance = strtoul(szBuffer, NULL, 10);
345
346 // Update symbolic OID
347 hItem = m_wndTreeCtrl.GetNextItem(NULL, TVGN_CARET);
348 if (hItem != NULL)
349 {
350 struct tree *pNode;
351 char *pszTemp;
352
353 pNode = (struct tree *)m_wndTreeCtrl.GetItemData(hItem);
354
355 pszTemp = BuildSymbolicOIDString(pNode, m_dwInstance);
356 m_wndEditOIDText.SetWindowText(pszTemp);
357 free(pszTemp);
358 }
359}
360
361
362//
363// Get requested parameter from host immediately
364//
365
366void CMIBBrowserDlg::OnButtonGet()
367{
368 // Select node object if none selected
369 if (m_pNode == NULL)
370 {
371 CObjectSelDlg dlg;
372
373 dlg.m_dwAllowedClasses = SCL_NODE;
374 if (dlg.DoModal() == IDOK)
375 {
babb8280 376 m_pNode = NXCFindObjectById(g_hSession, dlg.m_pdwObjectList[0]);
9d2d285e
VK
377 if (m_pNode != NULL)
378 {
379 CString strTitle;
380
381 GetWindowText(strTitle);
382 strTitle += " (";
383 strTitle += m_pNode->szName;
384 strTitle += ")";
385 SetWindowText(strTitle);
386 }
387 }
388 }
389
390 // If node is selected, continue
391 if (m_pNode != NULL)
392 {
393 CDataQueryDlg dlg;
394 TCHAR szBuffer[1024];
395
396 m_wndEditOID.GetWindowText(szBuffer, 1024);
397 dlg.m_dwObjectId = m_pNode->dwId;
398 dlg.m_strNode = (LPCTSTR)m_pNode->szName;
399 dlg.m_strParameter = (LPCTSTR)szBuffer;
400 _stprintf(szBuffer, _T(".%ld"), m_dwInstance);
401 dlg.m_strParameter += szBuffer;
402 dlg.m_iOrigin = DS_SNMP_AGENT;
403 dlg.DoModal();
404 }
405}
1943e452
VK
406
407
408//
409// Handler for "OK" button
410//
411
412void CMIBBrowserDlg::OnOK()
413{
414 // To avoid errors in data exchange we should
415 // set input box text to correct number
416 if (!m_bUseInstance)
417 m_wndEditInstance.SetWindowText(_T("0"));
418
419 CDialog::OnOK();
420}