- Changes in logging and debug output
[public/netxms.git] / src / console / win32 / DCIThresholdsPage.cpp
1 // DCIThresholdsPage.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6 #include "DCIThresholdsPage.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 // CDCIThresholdsPage property page
16
17 IMPLEMENT_DYNCREATE(CDCIThresholdsPage, CPropertyPage)
18
19 CDCIThresholdsPage::CDCIThresholdsPage() : CPropertyPage(CDCIThresholdsPage::IDD)
20 {
21 //{{AFX_DATA_INIT(CDCIThresholdsPage)
22 //}}AFX_DATA_INIT
23 }
24
25 CDCIThresholdsPage::~CDCIThresholdsPage()
26 {
27 }
28
29 void CDCIThresholdsPage::DoDataExchange(CDataExchange* pDX)
30 {
31 CPropertyPage::DoDataExchange(pDX);
32 //{{AFX_DATA_MAP(CDCIThresholdsPage)
33 DDX_Control(pDX, IDC_BUTTON_DELETE, m_wndButtonDelete);
34 DDX_Control(pDX, IDC_BUTTON_MODIFY, m_wndButtonModify);
35 DDX_Control(pDX, IDC_BUTTON_MOVEDOWN, m_wndButtonDown);
36 DDX_Control(pDX, IDC_BUTTON_MOVEUP, m_wndButtonUp);
37 DDX_Control(pDX, IDC_LIST_THRESHOLDS, m_wndListCtrl);
38 //}}AFX_DATA_MAP
39 }
40
41
42 BEGIN_MESSAGE_MAP(CDCIThresholdsPage, CPropertyPage)
43 //{{AFX_MSG_MAP(CDCIThresholdsPage)
44 ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
45 ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
46 ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
47 ON_BN_CLICKED(IDC_BUTTON_MOVEUP, OnButtonMoveup)
48 ON_BN_CLICKED(IDC_BUTTON_MOVEDOWN, OnButtonMovedown)
49 ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_THRESHOLDS, OnItemchangedListThresholds)
50 //}}AFX_MSG_MAP
51 END_MESSAGE_MAP()
52
53 /////////////////////////////////////////////////////////////////////////////
54 // CDCIThresholdsPage message handlers
55
56
57 //
58 // WM_INITDIALOG message handler
59 //
60
61 BOOL CDCIThresholdsPage::OnInitDialog()
62 {
63 RECT rect;
64 DWORD i;
65
66 CPropertyPage::OnInitDialog();
67
68 // Setup buttons
69 m_wndButtonUp.SetBitmap(CreateMappedBitmap(theApp.m_hInstance, IDB_UP_ARROW, 0, NULL, 0));
70 m_wndButtonDown.SetBitmap(CreateMappedBitmap(theApp.m_hInstance, IDB_DOWN_ARROW, 0, NULL, 0));
71
72 // Setup list view
73 m_wndListCtrl.GetClientRect(&rect);
74 m_wndListCtrl.InsertColumn(0, "Operation", LVCFMT_LEFT, rect.right - 150 - GetSystemMetrics(SM_CXVSCROLL));
75 m_wndListCtrl.InsertColumn(1, "Event", LVCFMT_LEFT, 150);
76 m_wndListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_TRACKSELECT | LVS_EX_UNDERLINEHOT);
77 m_wndListCtrl.SetHoverTime(0x7FFFFFFF);
78
79 // Add items to list
80 for(i = 0; i < m_pItem->dwNumThresholds; i++)
81 AddListEntry(i);
82
83 return TRUE;
84 }
85
86
87 //
88 // Add new entry to list
89 //
90
91 int CDCIThresholdsPage::AddListEntry(DWORD dwIndex)
92 {
93 int iItem;
94
95 iItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, "");
96 if (iItem != -1)
97 UpdateListEntry(iItem, dwIndex);
98 return iItem;
99 }
100
101
102 //
103 // Update list entry
104 //
105
106 void CDCIThresholdsPage::UpdateListEntry(int iItem, DWORD dwIndex)
107 {
108 char szBuffer[256], szArgs[256], szValue[MAX_DCI_STRING_VALUE];
109
110 m_wndListCtrl.SetItemData(iItem, dwIndex);
111
112 switch(m_pItem->pThresholdList[dwIndex].wFunction)
113 {
114 case F_LAST:
115 szArgs[0] = 0;
116 break;
117 case F_AVERAGE:
118 sprintf(szArgs, "%d", m_pItem->pThresholdList[dwIndex].dwArg1);
119 break;
120 case F_DEVIATION:
121 szArgs[0] = 0;
122 break;
123 }
124
125 switch(m_pItem->iDataType)
126 {
127 case DCI_DT_INT:
128 sprintf(szValue, "%ld", m_pItem->pThresholdList[dwIndex].value.dwInt32);
129 break;
130 case DCI_DT_UINT:
131 sprintf(szValue, "%lu", m_pItem->pThresholdList[dwIndex].value.dwInt32);
132 break;
133 case DCI_DT_INT64:
134 sprintf(szValue, "%I64d", m_pItem->pThresholdList[dwIndex].value.qwInt64);
135 break;
136 case DCI_DT_UINT64:
137 sprintf(szValue, "%I64u", m_pItem->pThresholdList[dwIndex].value.qwInt64);
138 break;
139 case DCI_DT_FLOAT:
140 sprintf(szValue, "%f", m_pItem->pThresholdList[dwIndex].value.dFloat);
141 break;
142 case DCI_DT_STRING:
143 sprintf(szValue, "\"%s\"", m_pItem->pThresholdList[dwIndex].value.szString);
144 break;
145 }
146
147 // Threshold expression
148 sprintf(szBuffer, "%s(%s) %s %s", g_pszThresholdFunction[m_pItem->pThresholdList[dwIndex].wFunction],
149 szArgs, g_pszThresholdOperation[m_pItem->pThresholdList[dwIndex].wOperation],
150 szValue);
151 m_wndListCtrl.SetItemText(iItem, 0, szBuffer);
152
153 // Event
154 m_wndListCtrl.SetItemText(iItem, 1, NXCGetEventName(m_pItem->pThresholdList[dwIndex].dwEvent));
155 }
156
157
158 //
159 // Edit threshold
160 //
161
162 BOOL CDCIThresholdsPage::EditThreshold(NXC_DCI_THRESHOLD *pThreshold)
163 {
164 CThresholdDlg dlg;
165 BOOL bResult = FALSE;
166 char szBuffer[32];
167
168 dlg.m_iFunction = pThreshold->wFunction;
169 dlg.m_iOperation = pThreshold->wOperation;
170 dlg.m_dwArg1 = pThreshold->dwArg1;
171 dlg.m_dwEventId = pThreshold->dwEvent;
172 switch(m_pItem->iDataType)
173 {
174 case DCI_DT_INT:
175 sprintf(szBuffer, "%d", pThreshold->value.dwInt32);
176 dlg.m_strValue = szBuffer;
177 break;
178 case DCI_DT_UINT:
179 sprintf(szBuffer, "%u", pThreshold->value.dwInt32);
180 dlg.m_strValue = szBuffer;
181 break;
182 case DCI_DT_INT64:
183 sprintf(szBuffer, "%I64d", pThreshold->value.qwInt64);
184 dlg.m_strValue = szBuffer;
185 break;
186 case DCI_DT_UINT64:
187 sprintf(szBuffer, "%I64u", pThreshold->value.qwInt64);
188 dlg.m_strValue = szBuffer;
189 break;
190 case DCI_DT_FLOAT:
191 sprintf(szBuffer, "%f", pThreshold->value.dFloat);
192 dlg.m_strValue = szBuffer;
193 break;
194 case DCI_DT_STRING:
195 dlg.m_strValue = pThreshold->value.szString;
196 break;
197 }
198 if (dlg.DoModal() == IDOK)
199 {
200 pThreshold->dwArg1 = dlg.m_dwArg1;
201 pThreshold->wFunction = (WORD)dlg.m_iFunction;
202 pThreshold->wOperation = (WORD)dlg.m_iOperation;
203 pThreshold->dwEvent = dlg.m_dwEventId;
204 switch(m_pItem->iDataType)
205 {
206 case DCI_DT_INT:
207 pThreshold->value.dwInt32 = strtol((LPCTSTR)dlg.m_strValue, NULL, 0);
208 break;
209 case DCI_DT_UINT:
210 pThreshold->value.dwInt32 = strtoul((LPCTSTR)dlg.m_strValue, NULL, 0);
211 break;
212 case DCI_DT_INT64:
213 /* TODO: add conversion for 64 bit integers */
214 break;
215 case DCI_DT_UINT64:
216 /* TODO: add conversion for 64 bit integers */
217 break;
218 case DCI_DT_FLOAT:
219 pThreshold->value.dFloat = strtod((LPCTSTR)dlg.m_strValue, NULL);
220 break;
221 case DCI_DT_STRING:
222 strcpy(pThreshold->value.szString, (LPCTSTR)dlg.m_strValue);
223 break;
224 }
225 bResult = TRUE;
226 }
227
228 return bResult;
229 }
230
231
232 //
233 // WM_COMMAND::IDC_BUTTON_ADD message handler
234 //
235
236 void CDCIThresholdsPage::OnButtonAdd()
237 {
238 NXC_DCI_THRESHOLD dct;
239
240 // Create default threshold structure
241 memset(&dct, 0, sizeof(NXC_DCI_THRESHOLD));
242 dct.dwArg1 = 1;
243 dct.dwEvent = EVENT_THRESHOLD_REACHED;
244
245 // Call threshold configuration dialog
246 if (EditThreshold(&dct))
247 {
248 DWORD dwIndex;
249
250 dwIndex = NXCAddThresholdToItem(m_pItem, &dct);
251 AddListEntry(dwIndex);
252 }
253 }
254
255
256 //
257 // WM_COMMAND::IDC_BUTTON_MODIFY message handler
258 //
259
260 void CDCIThresholdsPage::OnButtonModify()
261 {
262 int iItem;
263 DWORD dwIndex;
264 NXC_DCI_THRESHOLD dct;
265
266 iItem = m_wndListCtrl.GetNextItem(-1, LVNI_FOCUSED);
267 if (iItem != -1)
268 {
269 dwIndex = m_wndListCtrl.GetItemData(iItem);
270 memcpy(&dct, &m_pItem->pThresholdList[dwIndex], sizeof(NXC_DCI_THRESHOLD));
271 if (EditThreshold(&dct))
272 {
273 memcpy(&m_pItem->pThresholdList[dwIndex], &dct, sizeof(NXC_DCI_THRESHOLD));
274 UpdateListEntry(iItem, dwIndex);
275 }
276 }
277 }
278
279
280 //
281 // WM_COMMAND::IDC_BUTTON_DELETE message handler
282 //
283
284 void CDCIThresholdsPage::OnButtonDelete()
285 {
286 int iItem;
287 DWORD dwIndex;
288
289 iItem = m_wndListCtrl.GetNextItem(-1, LVNI_SELECTED);
290 while(iItem != -1)
291 {
292 dwIndex = m_wndListCtrl.GetItemData(iItem);
293 if (NXCDeleteThresholdFromItem(m_pItem, dwIndex))
294 {
295 m_wndListCtrl.DeleteItem(iItem);
296 RecalcIndexes(dwIndex);
297 }
298 iItem = m_wndListCtrl.GetNextItem(-1, LVNI_SELECTED);
299 }
300 }
301
302
303 //
304 // WM_COMMAND::IDC_BUTTON_MOVEUP message handler
305 //
306
307 void CDCIThresholdsPage::OnButtonMoveup()
308 {
309 int iItem;
310
311 if (m_wndListCtrl.GetSelectedCount() == 1)
312 {
313 iItem = m_wndListCtrl.GetNextItem(-1, LVNI_FOCUSED);
314 if (iItem > 0)
315 {
316 DWORD dwIndex1, dwIndex2;
317
318 dwIndex1 = m_wndListCtrl.GetItemData(iItem);
319 dwIndex2 = m_wndListCtrl.GetItemData(iItem - 1);
320 if (NXCSwapThresholds(m_pItem, dwIndex1, dwIndex2))
321 {
322 UpdateListEntry(iItem - 1, dwIndex2);
323 UpdateListEntry(iItem, dwIndex1);
324 SelectListViewItem(&m_wndListCtrl, iItem - 1);
325 }
326 }
327 }
328 }
329
330
331 //
332 // WM_COMMAND::IDC_BUTTON_MOVEDOWN message handler
333 //
334
335 void CDCIThresholdsPage::OnButtonMovedown()
336 {
337 int iItem;
338
339 if (m_wndListCtrl.GetSelectedCount() == 1)
340 {
341 iItem = m_wndListCtrl.GetNextItem(-1, LVNI_FOCUSED);
342 if (iItem < (m_wndListCtrl.GetItemCount() - 1))
343 {
344 DWORD dwIndex1, dwIndex2;
345
346 dwIndex1 = m_wndListCtrl.GetItemData(iItem);
347 dwIndex2 = m_wndListCtrl.GetItemData(iItem + 1);
348 if (NXCSwapThresholds(m_pItem, dwIndex1, dwIndex2))
349 {
350 UpdateListEntry(iItem + 1, dwIndex2);
351 UpdateListEntry(iItem, dwIndex1);
352 SelectListViewItem(&m_wndListCtrl, iItem + 1);
353 }
354 }
355 }
356 }
357
358
359 //
360 // Handler for LVN_ITEMCHANGED notification message
361 //
362
363 void CDCIThresholdsPage::OnItemchangedListThresholds(NMHDR* pNMHDR, LRESULT* pResult)
364 {
365 NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
366 BOOL bEnable;
367
368 // Disable some buttons if multiple items selected
369 bEnable = (m_wndListCtrl.GetSelectedCount() == 1);
370 m_wndButtonModify.EnableWindow(bEnable);
371 //m_wndButtonUp.EnableWindow(bEnable);
372 //m_wndButtonDown.EnableWindow(bEnable);
373 m_wndButtonDelete.EnableWindow(m_wndListCtrl.GetSelectedCount() > 0);
374
375 *pResult = 0;
376 }
377
378
379 //
380 // Recalculate item indexes after item deletion
381 //
382
383 void CDCIThresholdsPage::RecalcIndexes(DWORD dwIndex)
384 {
385 int iItem, iMaxItem;
386 DWORD dwData;
387
388 iMaxItem = m_wndListCtrl.GetItemCount();
389 for(iItem = 0; iItem < iMaxItem; iItem++)
390 {
391 dwData = m_wndListCtrl.GetItemData(iItem);
392 if (dwData > dwIndex)
393 {
394 dwData--;
395 m_wndListCtrl.SetItemData(iItem, dwData);
396 }
397 }
398 }