Console compiles on UNIX with UNICODE
[public/netxms.git] / src / client / console / plugins / AlarmBrowser / view.cpp
index 014e658..83aeee3 100644 (file)
 
 BEGIN_EVENT_TABLE(nxAlarmView, nxView)
        EVT_SIZE(nxAlarmView::OnSize)
-       EVT_LIST_ITEM_RIGHT_CLICK(wxID_LIST_CTRL, nxAlarmView::OnListItemRightClick)
+//     EVT_LIST_ITEM_RIGHT_CLICK(wxID_LIST_CTRL, nxAlarmView::OnListItemRightClick)
        EVT_LIST_COL_CLICK(wxID_LIST_CTRL, nxAlarmView::OnListColumnClick)
+       EVT_CONTEXT_MENU(nxAlarmView::OnContextMenu)
        EVT_MENU(XRCID("menuAlarmAck"), nxAlarmView::OnAlarmAck)
+       EVT_UPDATE_UI(XRCID("menuAlarmAck"), nxAlarmView::OnUpdateAlarmAck)
        EVT_MENU(XRCID("menuAlarmTerminate"), nxAlarmView::OnAlarmTerminate)
+       EVT_UPDATE_UI(XRCID("menuAlarmTerminate"), nxAlarmView::OnUpdateAlarmTerminate)
        EVT_MENU(XRCID("menuAlarmDelete"), nxAlarmView::OnAlarmDelete)
+       EVT_UPDATE_UI(XRCID("menuAlarmDelete"), nxAlarmView::OnUpdateAlarmDelete)
+       EVT_MENU(XRCID("menuAlarmIncreaseFont"), nxAlarmView::OnAlarmIncreaseFont)
+       EVT_MENU(XRCID("menuAlarmDecreaseFont"), nxAlarmView::OnAlarmDecreaseFont)
 END_EVENT_TABLE()
 
 
@@ -42,17 +48,21 @@ END_EVENT_TABLE()
 // Constructor
 //
 
-nxAlarmView::nxAlarmView(wxWindow *parent, const TCHAR *context)
+nxAlarmView::nxAlarmView(wxWindow *parent, const TCHAR *context, NXC_OBJECT *object)
             : nxView(parent)
 {
+       int fontSize;
+
        wxConfigBase *cfg = wxConfig::Get();
        wxString path = cfg->GetPath();
 
        m_context = context;
+       m_object = object;
 
        cfg->SetPath(m_context);
        cfg->Read(_T("AlarmView/SortMode"), &m_sortMode, 0);
        cfg->Read(_T("AlarmView/SortDir"), &m_sortDir, -1);
+       cfg->Read(_T("AlarmView/FontSize"), &fontSize, -1);
 
        m_wndListCtrl = new wxListView(this, wxID_LIST_CTRL, wxDefaultPosition, wxDefaultSize,
                                       wxLC_REPORT | wxLC_HRULES | wxLC_VRULES);
@@ -72,7 +82,18 @@ nxAlarmView::nxAlarmView(wxWindow *parent, const TCHAR *context)
        m_wndListCtrl->AssignImageList(imgList, wxIMAGE_LIST_SMALL);
        NXMCLoadListCtrlColumns(cfg, *m_wndListCtrl, _T("AlarmView"));
 
+       if (fontSize != -1)
+       {
+               wxFont font;
+
+               font = m_wndListCtrl->GetFont();
+               font.SetPointSize(fontSize);
+               m_wndListCtrl->SetFont(font);
+       }
+
        cfg->SetPath(path);
+       
+       SetIcon(wxXmlResource::Get()->LoadIcon(_T("icoAlarmView")));
 
        NXMCEvtConnect(nxEVT_NXC_ALARM_CHANGE, wxCommandEventHandler(nxAlarmView::OnAlarmChange), this);
 }
@@ -91,6 +112,7 @@ nxAlarmView::~nxAlarmView()
        cfg->SetPath(m_context);
        cfg->Write(_T("AlarmView/SortMode"), m_sortMode);
        cfg->Write(_T("AlarmView/SortDir"), m_sortDir);
+       cfg->Write(_T("AlarmView/FontSize"), m_wndListCtrl->GetFont().GetPointSize());
        NXMCSaveListCtrlColumns(cfg, *m_wndListCtrl, _T("AlarmView"));
        cfg->SetPath(path);
 }
@@ -121,7 +143,8 @@ void nxAlarmView::RefreshView()
        
        for(it = list->begin(); it != list->end(); it++)
        {
-               AddAlarm(it->second);
+               if (MatchAlarm(it->second))
+                       AddAlarm(it->second);
        }
 
        NXMCUnlockAlarmList();
@@ -130,6 +153,20 @@ void nxAlarmView::RefreshView()
 }
 
 
+//
+// Check if alarm matched to current filter settings
+//
+
+bool nxAlarmView::MatchAlarm(NXC_ALARM *alarm)
+{
+       if (m_object == NULL)
+               return true;
+       if (m_object->dwId == alarm->dwSourceObject)
+               return true;
+       return NXCIsParent(NXMCGetSession(), m_object->dwId, alarm->dwSourceObject) ? true : false;
+}
+
+
 //
 // Add alarm to view
 //
@@ -171,7 +208,7 @@ void nxAlarmView::UpdateAlarm(long item, NXC_ALARM *alarm)
 
        m_wndListCtrl->SetItem(item, 3, alarm->szMessage);
 
-       _stprintf(temp, _T("%d"), alarm->dwRepeatCount);
+       _sntprintf(temp, 64, _T("%d"), alarm->dwRepeatCount);
        m_wndListCtrl->SetItem(item, 4, temp);
 
        m_wndListCtrl->SetItem(item, 5, NXMCFormatTimeStamp(alarm->dwCreationTime, temp, TS_LONG_DATE_TIME));
@@ -255,7 +292,7 @@ static int wxCALLBACK CompareAlarms(long item1, long item2, long sortData)
 
 
 //
-// Sort aarm list
+// Sort alarm list
 //
 
 void nxAlarmView::SortAlarmList()
@@ -296,25 +333,31 @@ void nxAlarmView::OnListColumnClick(wxListEvent &event)
 void nxAlarmView::OnAlarmChange(wxCommandEvent &event)
 {
        long item;
+       NXC_ALARM *alarm;
        
+       alarm = (NXC_ALARM *)event.GetClientData();
        switch(event.GetInt())
        {
                case NX_NOTIFY_NEW_ALARM:
                case NX_NOTIFY_ALARM_CHANGED:
-                       item = m_wndListCtrl->FindItem(-1, ((NXC_ALARM *)event.GetClientData())->dwAlarmId);
+                       item = m_wndListCtrl->FindItem(-1, alarm->dwAlarmId);
                        if (item != -1)
                        {
-                               UpdateAlarm(item, (NXC_ALARM *)event.GetClientData());
+                               if (MatchAlarm(alarm))
+                                       UpdateAlarm(item, (NXC_ALARM *)event.GetClientData());
+                               else
+                                       m_wndListCtrl->DeleteItem(item);                // Alarm no longer match filter
                        }
                        else
                        {
-                               AddAlarm((NXC_ALARM *)event.GetClientData());
+                               if (MatchAlarm(alarm))
+                                       AddAlarm(alarm);
                        }
                        SortAlarmList();
                        break;
                case NX_NOTIFY_ALARM_TERMINATED:
                case NX_NOTIFY_ALARM_DELETED:
-                       item = m_wndListCtrl->FindItem(-1, ((NXC_ALARM *)event.GetClientData())->dwAlarmId);
+                       item = m_wndListCtrl->FindItem(-1, alarm->dwAlarmId);
                        if (item != -1)
                                m_wndListCtrl->DeleteItem(item);
                        break;
@@ -356,7 +399,13 @@ void nxAlarmView::OnAlarmAck(wxCommandEvent &event)
                        break;
                idList[i] = m_wndListCtrl->GetItemData(item);
        }
-       DoRequestArg2((void *)AckAlarms, count, CAST_FROM_POINTER(idList, wxUIntPtr));
+       if (count > 0)
+               DoRequestArg2((void *)AckAlarms, count, CAST_FROM_POINTER(idList, wxUIntPtr), _T("Cannot acknowledge alarm: %s"));
+}
+
+void nxAlarmView::OnUpdateAlarmAck(wxUpdateUIEvent &event)
+{
+       event.Enable(m_wndListCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1);
 }
 
 
@@ -393,7 +442,13 @@ void nxAlarmView::OnAlarmTerminate(wxCommandEvent &event)
                        break;
                idList[i] = m_wndListCtrl->GetItemData(item);
        }
-       DoRequestArg2((void *)TerminateAlarms, count, CAST_FROM_POINTER(idList, wxUIntPtr));
+       if (count > 0)
+               DoRequestArg2((void *)TerminateAlarms, count, CAST_FROM_POINTER(idList, wxUIntPtr), _T("Cannot terminate alarm: %s"));
+}
+
+void nxAlarmView::OnUpdateAlarmTerminate(wxUpdateUIEvent &event)
+{
+       event.Enable(m_wndListCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1);
 }
 
 
@@ -430,6 +485,65 @@ void nxAlarmView::OnAlarmDelete(wxCommandEvent &event)
                        break;
                idList[i] = m_wndListCtrl->GetItemData(item);
        }
-       DoRequestArg2((void *)DeleteAlarms, count, CAST_FROM_POINTER(idList, wxUIntPtr));
+       if (count > 0)
+               DoRequestArg2((void *)DeleteAlarms, count, CAST_FROM_POINTER(idList, wxUIntPtr), _T("Cannot delete alarm: %s"));
 }
 
+void nxAlarmView::OnUpdateAlarmDelete(wxUpdateUIEvent &event)
+{
+       event.Enable(m_wndListCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1);
+}
+
+
+//
+// Increase font
+//
+
+void nxAlarmView::OnAlarmIncreaseFont(wxCommandEvent &event)
+{
+       wxFont font;
+
+       font = m_wndListCtrl->GetFont();
+       if (font.GetPointSize() < 100)
+       {
+               font.SetPointSize(font.GetPointSize() + 1);
+               m_wndListCtrl->SetFont(font);
+       }
+}
+
+
+//
+// Decrease font
+//
+
+void nxAlarmView::OnAlarmDecreaseFont(wxCommandEvent &event)
+{
+       wxFont font;
+
+       font = m_wndListCtrl->GetFont();
+       if (font.GetPointSize() > 4)
+       {
+               font.SetPointSize(font.GetPointSize() - 1);
+               m_wndListCtrl->SetFont(font);
+       }
+}
+
+
+//
+// Context menu handler
+//
+
+void nxAlarmView::OnContextMenu(wxContextMenuEvent &event)
+{
+       wxMenu *menu;
+
+       if (!IsBusy())
+       {
+               menu = wxXmlResource::Get()->LoadMenu(_T("menuCtxAlarm"));
+               if (menu != NULL)
+               {
+                       PopupMenu(menu);
+                       delete menu;
+               }
+       }
+}