UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / win32 / winevents.cpp
blobbf378b77ecc215bdb9d0a6e28e38bd0258c059d3
1 // This file has been adapted to the Win32 version of Apcupsd
2 // by Kern E. Sibbald. Many thanks to ATT and James Weatherall,
3 // the original author, for providing an excellent template.
4 //
5 // Rewrite/Refactoring by Adam Kropelin
6 //
7 // Copyright (2009) Adam D. Kropelin
8 // Copyright (2000) Kern E. Sibbald
9 //
11 // Implementation of the Events dialogue
13 #include <windows.h>
14 #include "winevents.h"
15 #include "resource.h"
16 #include "statmgr.h"
17 #include "listview.h"
18 #include "wintray.h"
20 // Constructor/destructor
21 upsEvents::upsEvents(HINSTANCE appinst, upsMenu *menu) :
22 _appinst(appinst),
23 _hwnd(NULL),
24 _menu(menu)
28 upsEvents::~upsEvents()
32 // Dialog box handling functions
33 void upsEvents::Show()
35 if (!_hwnd)
37 DialogBoxParam(_appinst,
38 MAKEINTRESOURCE(IDD_EVENTS),
39 NULL,
40 (DLGPROC)DialogProc,
41 (LONG)this);
45 BOOL CALLBACK upsEvents::DialogProc(
46 HWND hwnd,
47 UINT uMsg,
48 WPARAM wParam,
49 LPARAM lParam)
51 upsEvents *_this;
53 // Retrieve virtual 'this' pointer. When we come in here the first time for
54 // the WM_INITDIALOG message, the pointer is in lParam. We then store it in
55 // the user data so it can be retrieved on future calls.
56 if (uMsg == WM_INITDIALOG)
58 // Set dialog user data to our "this" pointer which comes in via lParam.
59 // On subsequent calls, this will be retrieved by the code below.
60 SetWindowLong(hwnd, GWL_USERDATA, lParam);
61 _this = (upsEvents *)lParam;
63 else
65 // We've previously been initialized, so retrieve pointer from user data
66 _this = (upsEvents *)GetWindowLong(hwnd, GWL_USERDATA);
69 // Call thru to non-static member function
70 return _this->DialogProcess(hwnd, uMsg, wParam, lParam);
73 BOOL upsEvents::DialogProcess(
74 HWND hwnd,
75 UINT uMsg,
76 WPARAM wParam,
77 LPARAM lParam)
79 switch (uMsg) {
80 case WM_INITDIALOG:
81 // Silly: Save initial window size for use as minimum size. There's
82 // probably some programmatic way to fetch this from the resource when
83 // we need it, but I can't find it. So we'll save it at runtime.
84 GetWindowRect(hwnd, &_rect);
86 // Initialize control wrappers
87 _events = new ListView(hwnd, IDC_LIST, 1);
89 // Save a copy of our window handle for later use.
90 // Important to do this AFTER everything needed by Update() is
91 // initialized and ready to go since that function may be called at any
92 // time from the wintray timer thread.
93 _hwnd = hwnd;
95 // Show the dialog
96 _menu->Refresh();
97 SetForegroundWindow(hwnd);
98 return TRUE;
100 case WM_GETMINMAXINFO:
101 // Restrict minimum size to initial window size
102 MINMAXINFO *mmi = (MINMAXINFO*)lParam;
103 mmi->ptMinTrackSize.x = _rect.right - _rect.left;
104 mmi->ptMinTrackSize.y = _rect.bottom - _rect.top;
105 return TRUE;
107 case WM_SIZE:
109 // Fetch new window size (esp client area size)
110 WINDOWINFO wininfo;
111 wininfo.cbSize = sizeof(wininfo);
112 GetWindowInfo(hwnd, &wininfo);
114 // Fetch current listview position
115 HWND ctrl = GetDlgItem(hwnd, IDC_LIST);
116 RECT gridrect;
117 GetWindowRect(ctrl, &gridrect);
119 // Calculate new position and size of listview
120 int left = gridrect.left - wininfo.rcClient.left;
121 int top = gridrect.top - wininfo.rcClient.top;
122 int width = wininfo.rcClient.right - wininfo.rcClient.left - 2*left;
123 int height = wininfo.rcClient.bottom - wininfo.rcClient.top - top - left;
125 // Resize listview
126 SetWindowPos(
127 ctrl, NULL, left, top, width, height, SWP_NOZORDER | SWP_SHOWWINDOW);
129 return TRUE;
132 case WM_COMMAND:
133 switch (LOWORD(wParam)) {
134 case IDCANCEL:
135 case IDOK:
136 // Close the dialog
137 EndDialog(hwnd, TRUE);
138 return TRUE;
140 break;
142 case WM_DESTROY:
143 _mutex.lock();
144 _hwnd = NULL;
145 delete _events;
146 _mutex.unlock();
147 return TRUE;
150 return 0;
153 void upsEvents::Update(StatMgr *statmgr)
155 // Bail if window is not open
156 _mutex.lock();
157 if (!_hwnd)
159 _mutex.unlock();
160 return;
163 // Fetch events from apcupsd
164 alist<astring> events;
165 if (!statmgr->GetEvents(events) || events.empty())
167 _mutex.unlock();
168 return;
171 // Update listview
172 alist<astring> *data[] = {&events};
173 _events->UpdateAll(data);
175 _mutex.unlock();