HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
dialog_win32_impl.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
8
9#include "dialog_intf.hpp"
10#include "../win32/win32.hpp"
11#include "../macros.hpp"
12#include <string_view>
13#include <expected>
14#include <system_error>
15
16hi_export_module(hikogui.utility.dialog : impl);
17
18hi_export namespace hi { inline namespace v1 {
19
20hi_export inline std::expected<dialog_button, std::error_code> dialog(std::string_view title, std::string_view text, dialog_button_mask button_mask) noexcept
21{
22 UINT type = 0;
23
24 switch (button_mask) {
26 // Just "ok" can only be a notification, so it must be an error as well.
27 type = MB_APPLMODAL | MB_CANCELTRYCONTINUE | MB_ICONWARNING;
28 break;
29
31 // Just "ok" can only be a notification, so it must be an error as well.
32 type = MB_APPLMODAL | MB_OK | MB_ICONINFORMATION;
33 break;
34
36 // Just "ok" / "cancel" is a serious request to the user.
37 type = MB_APPLMODAL | MB_OKCANCEL | MB_ICONEXCLAMATION;
38 break;
39
41 // Just "retry" / "cancel" there was an error.
42 type = MB_APPLMODAL | MB_OKCANCEL | MB_ICONWARNING;
43 break;
44
46 // Just "yes" / "no" is serious, so exclamation.
47 type = MB_APPLMODAL | MB_YESNO | MB_ICONQUESTION;
48 break;
49
51 // If we can cancel it must be a warning.
52 type = MB_APPLMODAL | MB_YESNOCANCEL | MB_ICONQUESTION;
53 break;
54
55 default:
57 }
58
59 if (auto const r = win32_MessageBox(nullptr, text, title, type)) {
60 switch (*r) {
61 case IDABORT: return dialog_button::cancel;
62 case IDCANCEL: return dialog_button::cancel;
63 case IDCONTINUE: return dialog_button::_continue;
64 case IDIGNORE: return dialog_button::_continue;
65 case IDNO: return dialog_button::no;
66 case IDOK: return dialog_button::ok;
67 case IDRETRY: return dialog_button::retry;
68 case IDTRYAGAIN: return dialog_button::retry;
69 case IDYES: return dialog_button::yes;
70 default: std::terminate();
71 }
72 } else {
73 return std::unexpected{make_error_code(r.error())};
74 }
75}
76
77}} // namespace hi::inline v1
Rules for working with win32 headers.
The HikoGUI namespace.
Definition array_generic.hpp:20
std::expected< dialog_button, std::error_code > dialog(std::string_view title, std::string_view text, dialog_button_mask button_mask=dialog_button_mask::ok) noexcept
Display a modal dialog.
Definition dialog_win32_impl.hpp:20
dialog_button_mask
Definition dialog_intf.hpp:28
@ yes_no
A dialog box with "yes" and "no" buttons.
@ ok
A dialog box with just a "ok" button.
@ yes_no_cancel
A dialog box with "yes", "no" and "cancel" buttons.
@ retry_cancel
A dialog box with "retry" and "cancel" buttons.
@ ok_cancel
A dialog box with "ok" and "cancel" buttons.
@ cancel_retry_continue
A dialog box with "cancel", "retry" and "continue" buttons.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
T terminate(T... args)
T unexpected(T... args)