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,
19 // Implementation of the Config dialog
23 #include "winconfig.h"
27 // Constructor/destructor
28 upsConfig::upsConfig(HINSTANCE appinst
, InstanceManager
*instmgr
) :
35 upsConfig::~upsConfig()
39 // Dialog box handling functions
40 void upsConfig::Show(MonitorConfig
&mcfg
)
49 DialogBoxParam(_appinst
,
50 MAKEINTRESOURCE(IDD_CONFIG
),
57 BOOL CALLBACK
upsConfig::DialogProc(
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
;
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(
95 // Save a copy of our window handle for later use
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);
115 SetForegroundWindow(hwnd
);
119 switch (LOWORD(wParam
)) {
122 // Fetch and validate hostname
123 SendMessage(_hhost
, WM_GETTEXT
, sizeof(tmp
), (LONG
)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
145 _config
.refresh
= refresh
;
146 _config
.popups
= popups
;
147 _instmgr
->UpdateInstance(_config
);
148 EndDialog(hwnd
, TRUE
);
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
);
165 EndDialog(hwnd
, TRUE
);
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));