-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathform.cpp
594 lines (491 loc) · 16.5 KB
/
form.cpp
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//
// form.cpp - form implementation
//
// lecui user interface library, part of the liblec library
// Copyright (c) 2019 Alec Musasa (alecmus at live dot com)
//
// Released under the MIT license. For full details see the
// file LICENSE.txt
//
#include "form.h"
#include "appearance.h"
#include "controls.h"
#include "utilities/timer.h"
#include "form_impl/form_impl.h"
#include "error/win_error.h"
// Windows headers
#include <Windows.h>
namespace liblec {
namespace lecui {
// this is the constructor that all the others below call
form::form(const std::string& caption) :
_d(*new impl(*this, caption)) {}
form::form(const std::string& caption, form& parent) :
form::form(caption) {
// copy the parent's default theme setting
appearance aprnc(*this);
aprnc.theme(parent._d._theme);
if (IsWindow(parent._d._hWnd)) {
// close the parent's tooltips
parent._d.close_tooltips();
// capture parent
_d._p_parent = &parent;
_d._hWnd_parent = parent._d._hWnd;
// this is a child window. add it to the parent's map of children.
parent._d._m_children.insert(std::make_pair(this, this));
}
}
form::~form() {
if (_d._hWnd_parent && _d._p_parent) {
// this is a child window. remove it from the parent's map of children
try { _d._p_parent->_d._m_children.erase(this); }
catch (const std::exception&) {}
}
delete& _d;
}
std::string form::menu_form_caption() {
return std::string("lecui::form::menu");
}
std::string form::tooltip_form_caption() {
return std::string("lecui::form::tooltip");
}
void form::move(const point& point) {
_d._user_pos = true;
_d._preset_pos = false;
_d._point = point;
}
void form::move(const form_position& form_position) {
_d._user_pos = true;
_d._preset_pos = true;
_d._form_position = form_position;
}
void form::force_instance() {
_d._force_instance = true;
}
bool form::create(std::string& error) {
if (_d._create_called) {
error = "Library usage error: form::create";
return false;
}
else {
_d._create_called = true;
if (!_d._initialized) {
error = "Library initialization error: form::create";
return false;
}
}
if (_d._p_instance) {
if (_d._p_instance->another_instance_running()) {
log("another instance running");
if (!_d._force_instance) {
_d.open_existing_instance();
return true;
}
}
}
// don't allow a parent to have more than one modal form
if (IsWindow(_d._hWnd_parent)) {
if (_d._p_parent) {
if (_d._p_parent->_d._m_children.size() > 1) {
log("Another modal form is open");
error = "Another modal form is open";
return false;
}
}
}
// get resource module handle
if (!_d._resource_dll_filename.empty()) {
_d._resource_module_handle =
LoadLibrary(convert_string(_d._resource_dll_filename).c_str());
if (!_d._resource_module_handle) {
// LoadLibrary failed
error = "Loading " + _d._resource_dll_filename + " failed: " + get_last_error();
return false;
}
}
else
_d._resource_module_handle = GetModuleHandle(nullptr);
// call the initialize handler
if (events().initialize) {
if (!events().initialize(error))
return false;
}
// check if close was called
if (_d._close_called)
return true;
// call the layout handler
if (events().layout) {
if (!events().layout(error))
return false;
}
// handle menu form and tooltip form
if (_d._menu_form || _d._tooltip_form) {
_d._allow_resizing = false;
_d._allow_minimize = false;
if (_d._tooltip_form)
_d._activate = false;
}
// create form controls
if (!(_d._menu_form || _d._tooltip_form)) {
_d.create_close_button([&]() {
if (events().close)
events().close();
else
close();
});
if (_d._allow_resizing)
_d.create_maximize_button();
if (_d._allow_minimize)
_d.create_minimize_button();
_d.create_form_caption();
_d.create_form_menu();
}
// register window class
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = _d.window_procedure;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.hInstance = GetModuleHandle(nullptr);
// load main application icon
if (_d._idi_icon)
wcex.hIcon = LoadIcon(_d._resource_module_handle, MAKEINTRESOURCE(_d._idi_icon));
// load small application icon
if (_d._idi_icon_small)
wcex.hIconSm = (HICON)LoadImage(_d._resource_module_handle,
MAKEINTRESOURCE(_d._idi_icon_small),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR
);
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = nullptr;
wcex.lpszClassName = L"liblec::lecui::form";
RegisterClassEx(&wcex);
if (_d._user_pos) {
if (_d._preset_pos)
_d.set_position(_d._form_position, _d._design_size.get_width(), _d._design_size.get_height());
else
_d.set_position(_d._point.get_x(), _d._point.get_y(), _d._design_size.get_width(), _d._design_size.get_height());
}
else {
if (IsWindow(_d._hWnd_parent) && IsWindowEnabled(_d._hWnd_parent))
_d.set_position(form_position::center_to_parent,
_d._design_size.get_width(), _d._design_size.get_height());
else
_d.set_position(form_position::center_to_working_area,
_d._design_size.get_width(), _d._design_size.get_height());
}
// Register this instance so other instances can find this form and open it
if (!_d._guid.empty() && !IsWindow(_d._hWnd_parent))
_d._reg_id = RegisterWindowMessageA(_d._guid.c_str());
// perform initialization (_d._hWnd will be captured in WM_CREATE)
if (!CreateWindowEx(_d._tooltip_form ? NULL : _d._top_most == true ? WS_EX_TOPMOST : NULL, wcex.lpszClassName,
convert_string(_d._caption_plain).c_str(),
_d._tooltip_form ? WS_POPUP : static_cast<DWORD>(form::impl::style::aero_borderless),
static_cast<int>(.5f + _d._point.get_x() * _d._dpi_scale),
static_cast<int>(.5f + _d._point.get_y() * _d._dpi_scale),
static_cast<int>(.5f + _d._design_size.get_width() * _d._dpi_scale),
static_cast<int>(.5f + _d._design_size.get_height() * _d._dpi_scale),
_d._hWnd_parent, nullptr, wcex.hInstance, this)) {
error = get_last_error();
return false;
}
ShowWindow(_d._hWnd, _d._start_hidden ? SW_HIDE : (_d._activate ? SW_SHOW : SW_SHOWNA));
UpdateWindow(_d._hWnd);
// Update the user interface. This is important for widgets that are moved into
// special panes. If this is not done the widgets will not be drawn fully. One
// typical symptom of this is that before the mouse is moved at least once over an
// active widget the drawing will be incomplete, e.g. the selected text in comboboxes
// won't initially be displayed.
update();
if (_d._activate && !_d._start_hidden && !_d._tooltip_form)
SetForegroundWindow(_d._hWnd);
// Disable parent if this is neither a menu form nor a tooltip form.
// If this is a menu form or tooltip form, do not disable parent because we will need to be
// able to click the parent. The parent, is responsible for ignoring mouse movements
// and such things when it detects that a child menu form is open
if (!(_d._menu_form || _d._tooltip_form) && IsWindow(_d._hWnd_parent) && IsWindowEnabled(_d._hWnd_parent))
EnableWindow(_d._hWnd_parent, FALSE);
MSG msg = {};
// main message loop
BOOL result = NULL;
while ((result = GetMessage(&msg, nullptr, 0, 0)) != 0) { // GetMessage returns 0 when WM_QUIT is seen
if (result == -1) {
// error occurred
error = get_last_error();
log(error);
break;
}
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!IsWindow(_d._hWnd))
break;
}
return true;
}
void form::close() {
_d._close_called = true;
if (IsWindow(_d._hWnd)) {
// stop all timers
for (auto& it : _d._timers)
{
timer_manager timer(*this);
if (timer.running(it.first))
timer.stop(it.first);
}
if (!_d._m_children.empty()) {
// children exist, close them all
for (auto& child : _d._m_children)
child.second->close();
}
// Enable parent if this is neither a menu form nor a tooltip form.
// If this is a menu form or a tooltip form, do not enable parent because the menu form is not
// responsible for disabling the parent (if it is disabled)
if (!(_d._menu_form || _d._tooltip_form) && IsWindow(_d._hWnd_parent) && !IsWindowEnabled(_d._hWnd_parent))
EnableWindow(_d._hWnd_parent, TRUE);
DestroyWindow(_d._hWnd);
}
}
bool form::prompt(const std::string& question) {
class prompt_form : public form {
const size _min_size = { 220.f, 120.f };
const size _max_size = { 420.f, 400.f };
const std::string _question;
const float _margin = 10.f;
const size _button_size = widgets::button().rect().size();
bool& _user_agreed;
public:
prompt_form(const std::string& title, const std::string& question, form& parent, bool& user_agreed) :
form(title, parent),
_question(question),
_user_agreed(user_agreed) {
controls ctrls(*this);
ctrls
.allow_resize(false)
.allow_minimize(false);
appearance apprnc(*this);
apprnc
.caption_icon(parent._d._png_caption_icon);
// impose maximums
D2D1_RECT_F rect = D2D1::RectF(0.f, 0.f, _max_size.get_width(), _max_size.get_height());
// measure the question
widgets::label specs_lbl;
rect = widgets::measure_label(_d._p_directwrite_factory, question,
specs_lbl.font(), specs_lbl.font_size(), text_alignment::left, paragraph_alignment::top, rect);
auto width = (rect.right - rect.left) + 2 * _margin;
auto height = _d._caption_bar_height + _margin + (rect.bottom - rect.top) +
_margin + _button_size.get_height() + _margin;
// impose minimums
width = largest(width, _min_size.get_width());
height = largest(height, _min_size.get_height());
dimensions dim(*this);
dim.set_size({ width, height });
events().layout = [this](std::string& error) {
return on_layout(error);
};
}
bool on_layout(std::string& error) {
page_manager page_man(*this);
auto& home_page = page_man.add("home");
auto& label = widgets::label::add(home_page);
label
.text(_question).multiline(true)
.rect(rect()
.left(_margin)
.right(home_page.size().get_width())
.top(_margin)
.bottom(home_page.size().get_height() - _margin - _button_size.get_height() - _margin));
// add yes and no buttons, in that order for tab navigation
auto& button_yes = widgets::button::add(home_page, "button_yes");
auto& button_no = widgets::button::add(home_page, "button_no");
// position the no button on the bottom right
button_no.text("No")
.rect().place(rect()
.left(_margin)
.right(home_page.size().get_width() - _margin)
.top(_margin)
.bottom(home_page.size().get_height() - _margin),
100.f, 100.f);
button_no.events().action = [&]() {
_user_agreed = false;
close();
};
// snap the yes button to the no button
button_yes
.text("Yes")
.rect().snap_to(button_no.rect(), lecui::rect::snap_type::left, _margin);
button_yes.events().action = [&]() {
_user_agreed = true;
close();
};
page_man.show("home");
widget_manager widget_man(*this);
widget_man.select("home/button_yes");
return true;
}
};
// prompt the user
std::string error;
bool user_agreed = false;
if (!prompt_form(_d._caption_formatted, question, *this, user_agreed).create(error))
log(error);
return user_agreed;
}
void form::message(const std::string& message) {
if (!message.empty()) {
class message_form : public form {
const size _min_size = { 220.f, 120.f };
const size _max_size = { 420.f, 400.f };
const std::string _message;
const float _margin = 10.f;
const size _button_size = widgets::button().rect().size();
public:
message_form(const std::string& title, const std::string& message, form& parent) :
form(title, parent),
_message(message) {
controls ctrls(*this);
ctrls
.allow_resize(false)
.allow_minimize(false);
appearance apprnc(*this);
apprnc
.caption_icon(parent._d._png_caption_icon);
// impose maximums
D2D1_RECT_F rect = D2D1::RectF(0.f, 0.f, _max_size.get_width(), _max_size.get_height());
// measure the message
widgets::label specs_lbl;
rect = widgets::measure_label(_d._p_directwrite_factory, message,
specs_lbl.font(), specs_lbl.font_size(), text_alignment::left, paragraph_alignment::top, rect);
auto width = (rect.right - rect.left) + 2 * _margin;
auto height = _d._caption_bar_height + _margin + (rect.bottom - rect.top) +
_margin + _button_size.get_height() + _margin;
// impose minimums
width = largest(width, _min_size.get_width());
height = largest(height, _min_size.get_height());
dimensions dim(*this);
dim.set_size({ width, height });
events().layout = [this](std::string& error) {
return on_layout(error);
};
}
bool on_layout(std::string& error) {
page_manager page_man(*this);
auto& home_page = page_man.add("home");
auto& label = widgets::label::add(home_page);
label
.text(_message).multiline(true)
.rect(rect()
.left(_margin)
.right(home_page.size().get_width())
.top(_margin)
.bottom(home_page.size().get_height() - _margin - _button_size.get_height() - _margin));
// add the ok button on the bottom right
auto& button = widgets::button::add(home_page, "button_ok");
button
.text("Ok")
.rect().place(rect()
.left(_margin)
.right(home_page.size().get_width() - _margin)
.top(_margin)
.bottom(home_page.size().get_height() - _margin),
100.f, 100.f);
button.events().action = [&]() { close(); };
page_man.show("home");
widget_manager widget_man(*this);
widget_man.select("home/button_ok");
return true;
}
};
// display the message
std::string error;
if (!message_form(_d._caption_formatted, message, *this).create(error))
log(error);
}
}
void form::update() { _d.update(); }
// this is an expensive call. only use if update() doesn't get the job done.
void form::reload() { _d.discard_device_resources(); _d.update(); }
bool form::keep_alive() {
MSG msg = {};
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
// WM_QUIT detected ... don't process;
// repost it since it was removed from the queue by PM_REMOVE
// then return false to notify caller to stop the lengthy operation immediately
PostQuitMessage(0);
return false;
}
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return true;
}
float form::get_dpi_scale() {
return _d.get_dpi_scale();
}
void form::allow_minimize() {
if (!_d._allow_minimize)
return;
if (IsWindow(_d._hWnd))
ShowWindow(_d._hWnd, SW_MINIMIZE);
}
void form::restore() {
if (IsWindow(_d._hWnd)) {
if (!IsWindowVisible(_d._hWnd))
ShowWindow(_d._hWnd, SW_SHOW);
else
ShowWindow(_d._hWnd, SW_RESTORE);
SetForegroundWindow(_d._hWnd);
}
}
void form::maximize() {
if (!_d._allow_resizing)
return;
if (IsWindow(_d._hWnd))
ShowWindow(_d._hWnd, SW_MAXIMIZE);
}
void form::hide() {
if (IsWindow(_d._hWnd))
ShowWindow(_d._hWnd, SW_HIDE);
}
bool form::visible() {
if (IsWindow(_d._hWnd))
return IsWindowVisible(_d._hWnd) == TRUE;
else
return false;
}
void form::show() {
if (IsWindow(_d._hWnd)) {
ShowWindow(_d._hWnd, SW_SHOW);
SetForegroundWindow(_d._hWnd);
}
}
bool form::minimized() {
if (IsWindow(_d._hWnd))
return IsIconic(_d._hWnd) == TRUE;
else
return false;
}
void form::prevent_quit() {
if (_d._p_close_button)
// disable the close button
// no need to do anything else to prevent the app from exiting ...
// once the close button is disabled even Alt+F4 won't work
_d._p_close_button->enable(false);
update();
}
void form::allow_quit() {
if (_d._p_close_button)
// enable the close button
_d._p_close_button->enable(true);
update();
}
form::form_events& form::events() { return _d._form_events; }
}
}