source reorder
[public/netxms.git] / src / console / win32 / TrapSelDlg.cpp
CommitLineData
eb15f346
VK
1// TrapSelDlg.cpp : implementation file
2//
3
4#include "stdafx.h"
5#include "nxcon.h"
6#include "TrapSelDlg.h"
7
8#ifdef _DEBUG
9#define new DEBUG_NEW
10#undef THIS_FILE
11static char THIS_FILE[] = __FILE__;
12#endif
13
14/////////////////////////////////////////////////////////////////////////////
15// CTrapSelDlg dialog
16
17
18CTrapSelDlg::CTrapSelDlg(CWnd* pParent /*=NULL*/)
19 : CDialog(CTrapSelDlg::IDD, pParent)
20{
21 //{{AFX_DATA_INIT(CTrapSelDlg)
22 // NOTE: the ClassWizard will add member initialization here
23 //}}AFX_DATA_INIT
24
25 m_pdwTrapList = NULL;
26 m_dwNumTraps = 0;
27 m_dwTrapCfgSize = 0;
e04c3358
VK
28
29 m_iSortMode = theApp.GetProfileInt(_T("TrapSelDlg"), _T("SortMode"), 0);
30 m_iSortDir = theApp.GetProfileInt(_T("TrapSelDlg"), _T("SortDir"), 1);
eb15f346
VK
31}
32
33CTrapSelDlg::~CTrapSelDlg()
34{
35 safe_free(m_pdwTrapList);
adb514e0
VK
36 safe_free(m_pdwEventList);
37 safe_free(m_ppszNames);
38
39 theApp.WriteProfileInt(_T("TrapSelDlg"), _T("SortMode"), m_iSortMode);
e04c3358 40 theApp.WriteProfileInt(_T("TrapSelDlg"), _T("SortDir"), m_iSortDir);
eb15f346
VK
41}
42
43void CTrapSelDlg::DoDataExchange(CDataExchange* pDX)
44{
45 CDialog::DoDataExchange(pDX);
46 //{{AFX_DATA_MAP(CTrapSelDlg)
47 DDX_Control(pDX, IDC_LIST_TRAPS, m_wndListCtrl);
48 //}}AFX_DATA_MAP
49}
50
51
52BEGIN_MESSAGE_MAP(CTrapSelDlg, CDialog)
53 //{{AFX_MSG_MAP(CTrapSelDlg)
adb514e0 54 ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST_TRAPS, OnColumnclickListTraps)
eb15f346
VK
55 //}}AFX_MSG_MAP
56END_MESSAGE_MAP()
57
58/////////////////////////////////////////////////////////////////////////////
59// CTrapSelDlg message handlers
60
61
62//
63// WM_INITDIALOG message handler
64//
65
66BOOL CTrapSelDlg::OnInitDialog()
67{
68 DWORD i;
69 int nItem;
70 TCHAR szBuffer[1024];
e04c3358 71 RECT rect;
eb15f346
VK
72
73 CDialog::OnInitDialog();
e04c3358
VK
74
75 m_pImageList = CreateEventImageList();
76 m_iLastEventImage = m_pImageList->GetImageCount();
77 m_pImageList->Add(theApp.LoadIcon(IDI_SORT_UP));
78 m_pImageList->Add(theApp.LoadIcon(IDI_SORT_DOWN));
79 m_wndListCtrl.SetImageList(m_pImageList, LVSIL_SMALL);
80 m_wndListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_INFOTIP);
eb15f346 81
e04c3358 82 m_wndListCtrl.GetClientRect(&rect);
eb15f346 83 m_wndListCtrl.InsertColumn(0, _T("Trap OID"), LVCFMT_LEFT, 150);
e04c3358
VK
84 m_wndListCtrl.InsertColumn(1, _T("Event"), LVCFMT_LEFT, 150);
85 m_wndListCtrl.InsertColumn(2, _T("Description"), LVCFMT_LEFT, rect.right - 300 - GetSystemMetrics(SM_CXVSCROLL));
eb15f346
VK
86
87 for(i = 0; i < m_dwTrapCfgSize; i++)
88 {
89 SNMPConvertOIDToText(m_pTrapCfg[i].dwOidLen, m_pTrapCfg[i].pdwObjectId, szBuffer, 1024);
e04c3358 90 nItem = m_wndListCtrl.InsertItem(i, szBuffer, NXCGetEventSeverity(g_hSession, m_pTrapCfg[i].dwEventCode));
eb15f346
VK
91 if (nItem != -1)
92 {
93 m_wndListCtrl.SetItemText(nItem, 1, NXCGetEventName(g_hSession, m_pTrapCfg[i].dwEventCode));
94 m_wndListCtrl.SetItemText(nItem, 2, m_pTrapCfg[i].szDescription);
95 m_wndListCtrl.SetItemData(nItem, i);
96 }
97 }
e04c3358 98 SortList();
eb15f346
VK
99
100 return TRUE;
101}
e04c3358
VK
102
103
104//
105// "OK" button handler
106//
107
108void CTrapSelDlg::OnOK()
109{
110 int nItem;
adb514e0 111 DWORD i, dwIndex;
e04c3358
VK
112
113 m_dwNumTraps = m_wndListCtrl.GetSelectedCount();
114 if (m_dwNumTraps > 0)
115 {
116 m_pdwTrapList = (DWORD *)malloc(sizeof(DWORD) * m_dwNumTraps);
adb514e0
VK
117 m_pdwEventList = (DWORD *)malloc(sizeof(DWORD) * m_dwNumTraps);
118 m_ppszNames = (TCHAR **)malloc(sizeof(TCHAR *) * m_dwNumTraps);
e04c3358
VK
119 for(i = 0, nItem = -1; i < m_dwNumTraps; i++)
120 {
121 nItem = m_wndListCtrl.GetNextItem(nItem, LVIS_SELECTED);
122 if (nItem != -1)
123 {
adb514e0
VK
124 dwIndex = m_wndListCtrl.GetItemData(nItem);
125 m_pdwTrapList[i] = m_pTrapCfg[dwIndex].dwId;
126 m_pdwEventList[i] = m_pTrapCfg[dwIndex].dwEventCode;
127 m_ppszNames[i] = m_pTrapCfg[dwIndex].szDescription;
e04c3358
VK
128 }
129 }
130 }
131
132 CDialog::OnOK();
133}
134
135
136//
137// Callback for sort function
138//
139
140static int CALLBACK CompareListItems(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
141{
142 return ((CTrapSelDlg *)lParamSort)->CompareItems(lParam1, lParam2);
143}
144
145
146//
147// Sort trap list
148//
149
150void CTrapSelDlg::SortList()
151{
152 LVCOLUMN lvCol;
153
adb514e0 154 m_wndListCtrl.SortItems(CompareListItems, (LPARAM)this);
e04c3358
VK
155
156 // Mark sorting column
157 lvCol.mask = LVCF_IMAGE | LVCF_FMT;
158 lvCol.fmt = LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE | LVCFMT_LEFT;
159 lvCol.iImage = (m_iSortDir == 1) ? m_iLastEventImage : (m_iLastEventImage + 1);
160 m_wndListCtrl.SetColumn(m_iSortMode, &lvCol);
161}
162
163
adb514e0
VK
164//
165// Compare two OIDs
166//
167
168static int CompareOIDs(DWORD dwLen1, DWORD *pdwOid1, DWORD dwLen2, DWORD *pdwOid2)
169{
170 DWORD i, dwLen;
171
172 dwLen = min(dwLen1, dwLen2);
173 for(i = 0; i < dwLen; i++)
174 {
175 if (pdwOid1[i] < pdwOid2[i])
176 return -1;
177 if (pdwOid1[i] > pdwOid2[i])
178 return 1;
179 }
180 return COMPARE_NUMBERS(dwLen1, dwLen2);
181}
182
183
e04c3358
VK
184//
185// Compare two list items
186//
187
188int CTrapSelDlg::CompareItems(LPARAM nItem1, LPARAM nItem2)
189{
adb514e0
VK
190 int nRet;
191
192 switch(m_iSortMode)
193 {
194 case 0: // OID
195 nRet = CompareOIDs(m_pTrapCfg[nItem1].dwOidLen, m_pTrapCfg[nItem1].pdwObjectId,
196 m_pTrapCfg[nItem2].dwOidLen, m_pTrapCfg[nItem2].pdwObjectId);
197 break;
198 case 1: // Event
199 nRet = _tcsicmp(NXCGetEventName(g_hSession, m_pTrapCfg[nItem1].dwEventCode),
200 NXCGetEventName(g_hSession, m_pTrapCfg[nItem2].dwEventCode));
201 break;
202 case 2: // Description
203 nRet = _tcsicmp(m_pTrapCfg[nItem1].szDescription, m_pTrapCfg[nItem2].szDescription);
204 break;
205 }
206 return nRet * m_iSortDir;
207}
208
209
210//
211// Handler for list control column click
212//
213
214void CTrapSelDlg::OnColumnclickListTraps(NMHDR* pNMHDR, LRESULT* pResult)
215{
216 NM_LISTVIEW *pNMListView = (NM_LISTVIEW *)pNMHDR;
217 LVCOLUMN lvCol;
218
219 // Unmark current sorting column
220 lvCol.mask = LVCF_FMT;
221 lvCol.fmt = LVCFMT_LEFT;
222 m_wndListCtrl.SetColumn(m_iSortMode, &lvCol);
e04c3358 223
adb514e0
VK
224 if (pNMListView->iSubItem == m_iSortMode)
225 {
226 // Same column, change sort direction
227 m_iSortDir = -m_iSortDir;
228 }
229 else
230 {
231 // Another sorting column
232 m_iSortMode = pNMListView->iSubItem;
233 }
234
235 SortList();
236 *pResult = 0;
e04c3358 237}