Menu

[r121]: / trunk / DisplayPowerOff / SettingsDialog.cpp  Maximize  Restore  History

Download this file

436 lines (340 with data), 12.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include "stdafx.h"
#include "resource.h"
#include "App.h"
#include "Config.h"
#include "Engine.h"
#include "Options.h"
#include "Platform.h"
#include "SettingsDialog.h"
#include "NotificationAreaIcon.h"
#include "UserMessage.h"
#include "WindowsStartup.h"
namespace
{
constexpr auto trackTitleLength = 64;
HWND hwndSettingsDialog;
CharBuffer<trackTitleLength> czsDisplayDelayText;
[[nodiscard]] constexpr UINT GetCheck(bool checked) noexcept { return checked ? BST_CHECKED : BST_UNCHECKED; };
[[nodiscard]] bool IsChecked(HWND hwnd, int buttonId) { return IsDlgButtonChecked(hwnd, buttonId) == BST_CHECKED; };
INT_PTR CALLBACK SettingsDialogProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
}
namespace Standalone
{
namespace
{
[[nodiscard]] bool Standalone() { return !Options::AsScreensaver(); }
[[nodiscard]] bool NotificationAreaIcon() { return Options::NotificationAreaIcon(); }
bool defaultRunAtStartUp;
}
void UpdateTriggerCheckText(HWND hwnd)
{
if (NotificationAreaIcon())
{
CharBuffer<256> cszText;
App::LoadString(IsChecked(hwnd, IDC_ALWAYS_LAUNCH_CHECK) ? IDS_SCREENSAVER_ON_LOCK_CHECK : IDS_TURN_OFF_ON_LOCK_CHECK, cszText);
VERIFY(SetDlgItemText(hwnd, IDC_TRIGGER_ON_LOCK_CHECK, cszText));
}
}
void UpdateControls(HWND hwnd, bool init, bool defaults)
{
const auto standalone = Standalone();
if (init || standalone)
{
CheckDlgButton(hwnd, IDC_ALWAYS_LAUNCH_CHECK, defaults ? Config::DefaultAlwaysLaunchScreensaver : GetCheck(Config::AlwaysLaunchScreensaver()));
CheckDlgButton(hwnd, IDC_ALWAYS_ENABLE_CHECK, defaults ? Config::DefaultAlwaysEnableScreensaver : GetCheck(Config::AlwaysEnableScreensaver()));
if(init || NotificationAreaIcon())
{
CheckDlgButton(hwnd, IDC_TRIGGER_ON_LOCK_CHECK, defaults ? Config::DefaultTriggerOnLockWorkStation : GetCheck(Config::TriggerOnLockWorkStation()));
#pragma warning(suppress: 6011)
CheckDlgButton(hwnd, IDC_RUN_AT_STARTUP_CHECK, defaults ? WindowsStartup::DefaultEnabled : defaultRunAtStartUp = GetCheck(WindowsStartup::Enabled()));
UpdateTriggerCheckText(hwnd);
}
}
if (!standalone)
{
VERIFY(ShowWindow(GetDlgItem(hwnd, IDC_EDIT_SYSLINK), SW_HIDE));
}
}
template<int count>
void AdjustControls(HWND hwnd, const Array<int, count> idsToHide)
{
const auto hideControls = [=]()
{
struct { HWND firstToHide; HWND lastToHide; } result = {};
for (const auto id : idsToHide.Elems)
{
auto hwndControl = GetDlgItem(hwnd, id);
ASSERT(hwndControl);
if (!result.firstToHide) result.firstToHide = hwndControl;
result.lastToHide = hwndControl;
VERIFY(ShowWindow(hwndControl, SW_HIDE));
}
return result;
};
const auto moveControls = [=](HWND firstToHide, HWND lastToHide)
{
const auto getControlRect = [=](HWND hwndFrom)
{
RECT rect;
VERIFY(GetClientRect(hwndFrom, &rect));
VERIFY(MapWindowPoints(hwndFrom, hwnd, reinterpret_cast<LPPOINT>(&rect), 2));
return rect;
};
const auto rectTop = getControlRect(firstToHide);
const auto rectBottom = getControlRect(lastToHide);
const auto deltaY = rectBottom.bottom - rectTop.top;
const int idsToMove[]{ IDC_DEFAULTS_SYSLINK, IDC_REVERT_SYSLINK, IDC_EDIT_SYSLINK, IDOK, IDCANCEL };
for (const int id : idsToMove)
{
const auto hwndControl = GetDlgItem(hwnd, id);
ASSERT(hwndControl);
RECT rect = getControlRect(hwndControl);
rect.top -= deltaY;
VERIFY(SetWindowPos(hwndControl, nullptr, rect.left, rect.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER));
}
return deltaY;
};
const auto [firstToHide, lastToHide] = hideControls();
const auto deltaY = moveControls(firstToHide, lastToHide);
RECT rectHwnd;
VERIFY(GetWindowRect(hwnd, &rectHwnd));
VERIFY(SetWindowPos(hwnd, nullptr, 0, 0, rectHwnd.right - rectHwnd.left, rectHwnd.bottom - rectHwnd.top - deltaY, SWP_NOREPOSITION | SWP_NOZORDER));
}
void OnInitDialog(HWND hwnd)
{
const auto setTitle = [=](int id)
{
CharBuffer<64> szTitle;
App::LoadString(id, szTitle);
VERIFY(SetWindowText(hwnd, szTitle));
};
if (Options::ShowHelp())
{
AdjustControls(hwnd, Array<int, 2>{ IDC_TRIGGER_ON_LOCK_CHECK, IDC_RUN_AT_STARTUP_CHECK });
}
if (Standalone())
{
if (NotificationAreaIcon())
{
setTitle(IDS_NOTIFICATION_AREA_SETTINGS);
}
VERIFY(GetDlgItemText(hwnd, IDC_DISPLAY_DELAY_STATIC, czsDisplayDelayText, czsDisplayDelayText));
return;
}
App::LoadString(IDS_DISPLAY_DELAY, czsDisplayDelayText);
setTitle(IDS_SCREENSAVER_SETTINGS);
AdjustControls(hwnd, Array<int, 4>{ IDC_ALWAYS_LAUNCH_CHECK, IDC_ALWAYS_ENABLE_CHECK, IDC_TRIGGER_ON_LOCK_CHECK, IDC_RUN_AT_STARTUP_CHECK });
}
void OnOk(HWND hwnd)
{
if (NotificationAreaIcon())
{
NotificationAreaIcon::ApplySettings(GetParent(hwnd));
const auto runAtStartUp = IsChecked(hwnd, IDC_RUN_AT_STARTUP_CHECK);
if (defaultRunAtStartUp != runAtStartUp)
{
WindowsStartup::Enable(runAtStartUp);
}
}
}
}
void SettingsDialog::SetTriggerOnWorkStationLockCheck(bool set)
{
if (IsWindow(hwndSettingsDialog))
{
CheckDlgButton(hwndSettingsDialog, IDC_TRIGGER_ON_LOCK_CHECK, GetCheck(set));
}
}
void SettingsDialog::SetRunAtStartupCheck(bool set)
{
if (IsWindow(hwndSettingsDialog))
{
CheckDlgButton(hwndSettingsDialog, IDC_RUN_AT_STARTUP_CHECK, GetCheck(set));
}
}
void SettingsDialog::MakeForeground()
{
if (IsWindow(hwndSettingsDialog))
{
ShowWindow(hwndSettingsDialog, SW_SHOWNORMAL);
VERIFY(SetForegroundWindow(hwndSettingsDialog));
}
}
void SettingsDialog::Show(HWND hwnd)
{
if (!IsWindow(hwndSettingsDialog))
{
hwndSettingsDialog = CreateDialog(App::GetInstance(), MAKEINTRESOURCE(IDD_SETTINGS), hwnd, SettingsDialogProc);
Platform::SetIcon(hwndSettingsDialog, App::GetInstance(), IDI_APP_ICON);
}
ASSERT(IsWindow(hwndSettingsDialog));
MakeForeground();
}
bool SettingsDialog::Process(MSG& msg)
{
return hwndSettingsDialog != nullptr && IsDialogMessage(hwndSettingsDialog, &msg);
}
namespace
{
constexpr auto tick = 500;
constexpr auto quant = 100;
constexpr auto trackHeaderLength = 32;
CharBuffer<trackTitleLength> czsLockDelayText;
CharBuffer<trackHeaderLength> czsDelayMs;
CharBuffer<trackHeaderLength> czsDelaySec;
CharBuffer<trackHeaderLength> czsNoDelay;
constexpr auto millisecondsInSecond = 1000;
constexpr auto millisecondsFraction = 100;
[[nodiscard]] auto FormatPosition(int nPosition)
{
CharBuffer<8, ClearBuffer::All> szText;
if (nPosition < millisecondsInSecond)
{
VERIFY_SPRINTF(wnsprintf(szText, szText, TEXT("%d"), nPosition));
return szText;
}
if (nPosition == millisecondsInSecond)
{
VERIFY_STR(StringCchCopy(szText, szText, TEXT("1")));
return szText;
}
const auto div = nPosition / millisecondsInSecond;
const auto mod = nPosition % millisecondsInSecond;
const auto fraction = mod / millisecondsFraction;
VERIFY_SPRINTF(wnsprintf(szText, szText, fraction ? TEXT("%d.%d") : TEXT("%d"), div, fraction));
return szText;
}
void UpdateTrackBarTitle(HWND hwnd, UINT uId, int nPosition)
{
CharBuffer<trackHeaderLength+trackTitleLength+16, ClearBuffer::All> szText;
const auto getDelayHeader = [nPosition]()
{
return nPosition
? nPosition < millisecondsInSecond
? czsDelayMs
: czsDelaySec
: czsNoDelay;
};
VERIFY_SPRINTF(wnsprintf(szText, szText,
getDelayHeader(),
FormatPosition(nPosition).Chars,
nPosition <= 1 || nPosition == millisecondsInSecond ? TEXT("") : TEXT("s")));
VERIFY_STR(StringCchCat(szText, szText, TEXT(" ")));
VERIFY_STR(StringCchCat(szText, szText, uId == IDC_DISPLAY_DELAY_STATIC ? czsDisplayDelayText : czsLockDelayText));
SetDlgItemText(hwnd, uId, szText);
}
void UpdateTrackBar(HWND hwnd, UINT uId, int nPosition)
{
SendDlgItemMessage(hwnd, uId, TBM_SETRANGE, FALSE, MAKELPARAM(0, Config::MaxTimeout));
SendDlgItemMessage(hwnd, uId, TBM_SETLINESIZE, 0, 1);
SendDlgItemMessage(hwnd, uId, TBM_SETPAGESIZE, 0, quant);
SendDlgItemMessage(hwnd, uId, TBM_SETTICFREQ, tick, 0);
SendDlgItemMessage(hwnd, uId, TBM_SETPOS, TRUE, nPosition);
}
enum class UpdateControlsAction
{
None,
Init,
Defaults
};
void UpdateControls(HWND hwnd, UpdateControlsAction updateControlsAction = UpdateControlsAction::None)
{
const bool defaults = updateControlsAction == UpdateControlsAction::Defaults;
const auto startupDelay = defaults ? Config::DefaultStartupDelay : Config::StartupDelay();
const auto lockWorkstationDelay = defaults ? Config::DefaultLockWorkStationDelay : Config::LockWorkStationDelay();
UpdateTrackBar(hwnd, IDC_DISPLAY_DELAY_SLIDER, startupDelay);
UpdateTrackBar(hwnd, IDC_LOCK_DELAY_SLIDER, lockWorkstationDelay);
UpdateTrackBarTitle(hwnd, IDC_DISPLAY_DELAY_STATIC, startupDelay);
UpdateTrackBarTitle(hwnd, IDC_LOCK_DELAY_STATIC, lockWorkstationDelay);
CheckDlgButton(hwnd, IDC_ALWAYS_LOCK_CHECK, defaults ? Config::DefaultAlwaysLockWorkStation : GetCheck(Config::AlwaysLockWorkStation()));
Standalone::UpdateControls(hwnd, updateControlsAction == UpdateControlsAction::Init, defaults);
}
[[nodiscard]] int GetTrackBarPosition(HWND hwndCtrl)
{
#pragma warning(suppress: 26472)
const auto nPosition = static_cast<int>(SendMessage(hwndCtrl, TBM_GETPOS, 0, 0));
return nPosition < quant ? nPosition : nPosition / quant * quant;
}
[[nodiscard]] int GetTrackBarPosition(HWND hwnd, int id)
{
return GetTrackBarPosition(GetDlgItem(hwnd, id));
}
void OnHScroll(HWND hwnd, HWND hwndCtl, int, int)
{
UpdateTrackBarTitle(
hwnd,
GetDlgCtrlID(hwndCtl) == IDC_DISPLAY_DELAY_SLIDER ? IDC_DISPLAY_DELAY_STATIC : IDC_LOCK_DELAY_STATIC,
GetTrackBarPosition(hwndCtl));
}
BOOL OnInitDialog(HWND hwnd, HWND, LPARAM)
{
Config::Read(true);
Standalone::OnInitDialog(hwnd);
App::LoadString(IDS_DELAY_MS, czsDelayMs);
App::LoadString(IDS_DELAY_SEC, czsDelaySec);
App::LoadString(IDS_NO_DELAY, czsNoDelay);
VERIFY(GetDlgItemText(hwnd, IDC_LOCK_DELAY_STATIC, czsLockDelayText, czsLockDelayText));
UpdateControls(hwnd, UpdateControlsAction::Init);
Platform::CenterWindow(hwnd);
return TRUE;
}
void OnCommand(HWND hwnd, int id, HWND, UINT)
{
const auto destroyWindow = [=]() { VERIFY(DestroyWindow(hwnd)); hwndSettingsDialog = nullptr; };
const auto writeConfig = [&, hwnd]()
{
const auto succeeded = Config::Write(
GetTrackBarPosition(hwnd, IDC_DISPLAY_DELAY_SLIDER),
GetTrackBarPosition(hwnd, IDC_LOCK_DELAY_SLIDER),
IsChecked(hwnd, IDC_ALWAYS_LOCK_CHECK),
IsChecked(hwnd, IDC_ALWAYS_LAUNCH_CHECK),
IsChecked(hwnd, IDC_ALWAYS_ENABLE_CHECK),
IsChecked(hwnd, IDC_TRIGGER_ON_LOCK_CHECK)
);
if (!succeeded)
{
UserMessage::ConfigSaveSucceeded(false, hwndSettingsDialog);
}
};
const auto onOk = [&, hwnd]()
{
if (Engine::Busy())
{
UserMessage::InProgress(hwndSettingsDialog);
return;
}
writeConfig();
Standalone::OnOk(hwnd);
destroyWindow();
};
HANDLE_COMMAND(IDOK, onOk())
HANDLE_COMMAND(IDCANCEL, destroyWindow())
HANDLE_COMMAND(IDC_ALWAYS_LAUNCH_CHECK, Standalone::UpdateTriggerCheckText(hwnd))
}
BOOL OnNotify(HWND hwnd, int id, const NMHDR* pnmhdr)
{
const auto code = pnmhdr->code;
#pragma warning(push)
#pragma warning(disable: 26454)
if (code == NM_CLICK || code == NM_RETURN)
#pragma warning(pop)
{
HANDLE_NOTIFY_COMMAND(IDC_EDIT_SYSLINK, Config::Open())
HANDLE_NOTIFY_COMMAND(IDC_REVERT_SYSLINK, Config::Read(true); UpdateControls(hwnd))
HANDLE_NOTIFY_COMMAND(IDC_DEFAULTS_SYSLINK, UpdateControls(hwnd, UpdateControlsAction::Defaults))
}
return TRUE;
}
INT_PTR CALLBACK SettingsDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
#pragma warning (suppress: 26818)
switch (uMsg)
{
HANDLE_MSG(hwnd, WM_INITDIALOG, OnInitDialog);
HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
HANDLE_MSG(hwnd, WM_NOTIFY, OnNotify);
HANDLE_MSG(hwnd, WM_HSCROLL, OnHScroll);
}
return FALSE;
}
}