UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / win32 / winconfig.cpp
blob1510662dde97009a46407868c6e838eaed867c68
1 /*
2 * Copyright (C) 2009 Adam Kropelin
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General
6 * Public License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the Free
15 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
19 // Implementation of the Config dialog
21 #include <windows.h>
22 #include <commctrl.h>
23 #include "winconfig.h"
24 #include "resource.h"
25 #include "instmgr.h"
27 // Constructor/destructor
28 upsConfig::upsConfig(HINSTANCE appinst, InstanceManager *instmgr) :
29 _hwnd(NULL),
30 _appinst(appinst),
31 _instmgr(instmgr)
35 upsConfig::~upsConfig()
39 // Dialog box handling functions
40 void upsConfig::Show(MonitorConfig &mcfg)
42 if (!_hwnd)
44 _config = mcfg;
45 _hostvalid = true;
46 _portvalid = true;
47 _refreshvalid = true;
49 DialogBoxParam(_appinst,
50 MAKEINTRESOURCE(IDD_CONFIG),
51 NULL,
52 (DLGPROC)DialogProc,
53 (LONG)this);
57 BOOL CALLBACK upsConfig::DialogProc(
58 HWND hwnd,
59 UINT uMsg,
60 WPARAM wParam,
61 LPARAM lParam)
63 upsConfig *_this;
65 // Retrieve virtual 'this' pointer. When we come in here the first time for
66 // the WM_INITDIALOG message, the pointer is in lParam. We then store it in
67 // the user data so it can be retrieved on future calls.
68 if (uMsg == WM_INITDIALOG)
70 // Set dialog user data to our "this" pointer which comes in via lParam.
71 // On subsequent calls, this will be retrieved by the code below.
72 SetWindowLong(hwnd, GWL_USERDATA, lParam);
73 _this = (upsConfig *)lParam;
75 else
77 // We've previously been initialized, so retrieve pointer from user data
78 _this = (upsConfig *)GetWindowLong(hwnd, GWL_USERDATA);
81 // Call thru to non-static member function
82 return _this->DialogProcess(hwnd, uMsg, wParam, lParam);
85 BOOL upsConfig::DialogProcess(
86 HWND hwnd,
87 UINT uMsg,
88 WPARAM wParam,
89 LPARAM lParam)
91 char tmp[256] = {0};
93 switch (uMsg) {
94 case WM_INITDIALOG:
95 // Save a copy of our window handle for later use
96 _hwnd = hwnd;
98 // Fetch handles for all controls. We'll use these multiple times later
99 // so it makes sense to cache them.
100 _hhost = GetDlgItem(hwnd, IDC_HOSTNAME);
101 _hport = GetDlgItem(hwnd, IDC_PORT);
102 _hrefresh = GetDlgItem(hwnd, IDC_REFRESH);
103 _hpopups = GetDlgItem(hwnd, IDC_POPUPS);
105 // Initialize fields with current config settings
106 SendMessage(_hhost, WM_SETTEXT, 0, (LONG)_config.host.str());
107 snprintf(tmp, sizeof(tmp), "%d", _config.port);
108 SendMessage(_hport, WM_SETTEXT, 0, (LONG)tmp);
109 snprintf(tmp, sizeof(tmp), "%d", _config.refresh);
110 SendMessage(_hrefresh, WM_SETTEXT, 0, (LONG)tmp);
111 SendMessage(_hpopups, BM_SETCHECK,
112 _config.popups ? BST_CHECKED : BST_UNCHECKED, 0);
114 // Show the dialog
115 SetForegroundWindow(hwnd);
116 return TRUE;
118 case WM_COMMAND:
119 switch (LOWORD(wParam)) {
120 case IDOK:
122 // Fetch and validate hostname
123 SendMessage(_hhost, WM_GETTEXT, sizeof(tmp), (LONG)tmp);
124 astring host(tmp);
125 _hostvalid = !host.trim().empty();
127 // Fetch and validate port
128 SendMessage(_hport, WM_GETTEXT, sizeof(tmp), (LONG)tmp);
129 int port = atoi(tmp);
130 _portvalid = (port >= 1 && port <= 65535);
132 // Fetch and validate refresh
133 SendMessage(_hrefresh, WM_GETTEXT, sizeof(tmp), (LONG)tmp);
134 int refresh = atoi(tmp);
135 _refreshvalid = (refresh >= 1);
137 // Fetch popups on/off
138 bool popups = SendMessage(_hpopups, BM_GETCHECK, 0, 0) == BST_CHECKED;
140 if (_hostvalid && _portvalid && _refreshvalid)
142 // Config is valid: Save it and close the dialog
143 _config.host = host;
144 _config.port = port;
145 _config.refresh = refresh;
146 _config.popups = popups;
147 _instmgr->UpdateInstance(_config);
148 EndDialog(hwnd, TRUE);
150 else
152 // Set keyboard focus to first invalid field
153 if (!_hostvalid) SetFocus(_hhost);
154 else if (!_portvalid) SetFocus(_hport);
155 else SetFocus(_hrefresh);
157 // Force redraw to get background color change
158 RedrawWindow(hwnd, NULL, NULL, RDW_INTERNALPAINT|RDW_INVALIDATE);
160 return TRUE;
163 case IDCANCEL:
164 // Close the dialog
165 EndDialog(hwnd, TRUE);
166 return TRUE;
168 break;
170 case WM_CTLCOLOREDIT:
172 // Set edit control background red if data was invalid
173 if (((HWND)lParam == _hhost && !_hostvalid) ||
174 ((HWND)lParam == _hport && !_portvalid) ||
175 ((HWND)lParam == _hrefresh && !_refreshvalid))
177 SetBkColor((HDC)wParam, RGB(255,0,0));
178 return (BOOL)CreateSolidBrush(RGB(255,0,0));
180 return FALSE;
183 case WM_DESTROY:
184 _hwnd = NULL;
185 return TRUE;
188 return 0;