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
use super::*;
macro_rules! const_modify_u32 {
($flag:expr, $mask:expr, $word:expr) => {
$word ^= (u32::wrapping_sub(0u32, $flag as u32) ^ $word) & $mask;
};
}
pub fn message_box(
owner: HWND,
text: &str,
opt_title: Option<&str>,
box_type: MessageBoxType,
) -> Result<MessageBoxReturn, DWORD> {
if box_type.is_service_notification() && owner.is_null() {
Err(ERROR_INVALID_DATA)?
};
match opt_title {
Some(title) => unsafe {
let text_wn: Vec<u16> = wide_null(text);
let title_wn: Vec<u16> = wide_null(title);
MessageBoxReturn::from_i32(MessageBoxW(
owner,
text_wn.as_ptr(),
title_wn.as_ptr(),
box_type.0,
))
.ok_or_else(|| GetLastError())
},
None => unsafe {
let text_wn: Vec<u16> = wide_null(text);
MessageBoxReturn::from_i32(MessageBoxW(owner, text_wn.as_ptr(), &0, box_type.0))
.ok_or_else(|| GetLastError())
},
}
}
#[derive(Debug, Clone, Copy, Default)]
#[repr(transparent)]
pub struct MessageBoxType(u32);
impl MessageBoxType {
pub const fn new() -> Self {
Self(0)
}
const fn is_service_notification(self) -> bool {
(self.0 & MB_SERVICE_NOTIFICATION) > 0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
#[allow(missing_docs)]
pub enum MessageBoxButtons {
CancelTryContinue = MB_CANCELTRYCONTINUE,
Ok = MB_OK,
OkCancel = MB_OKCANCEL,
RetryCancel = MB_RETRYCANCEL,
YesNo = MB_YESNO,
YesNoCancel = MB_YESNOCANCEL,
}
impl Default for MessageBoxButtons {
fn default() -> Self {
MessageBoxButtons::Ok
}
}
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum MessageBoxIcon {
None = 0,
Exclamation = MB_ICONEXCLAMATION,
Information = MB_ICONINFORMATION,
Stop = MB_ICONSTOP,
}
impl Default for MessageBoxIcon {
fn default() -> Self {
MessageBoxIcon::None
}
}
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
#[allow(missing_docs)]
pub enum MessageBoxDefaultButton {
Button1 = MB_DEFBUTTON1,
Button2 = MB_DEFBUTTON2,
Button3 = MB_DEFBUTTON3,
Button4 = MB_DEFBUTTON4,
}
impl Default for MessageBoxDefaultButton {
fn default() -> Self {
MessageBoxDefaultButton::Button1
}
}
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum MessageBoxModality {
AppModal = MB_APPLMODAL,
SystemModal = MB_SYSTEMMODAL,
TaskModal = MB_TASKMODAL,
}
impl Default for MessageBoxModality {
fn default() -> Self {
MessageBoxModality::AppModal
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct MessageBoxTypeBuilder {
#[allow(missing_docs)]
pub buttons: MessageBoxButtons,
#[allow(missing_docs)]
pub icon: MessageBoxIcon,
#[allow(missing_docs)]
pub default_button: MessageBoxDefaultButton,
#[allow(missing_docs)]
pub modality: MessageBoxModality,
pub help_button: bool,
pub default_desktop_only: bool,
pub right_justified_text: bool,
pub right_to_left_text: bool,
pub set_foreground: bool,
pub topmost: bool,
pub is_service_notification: bool,
}
impl MessageBoxTypeBuilder {
pub const fn into_message_box_type(self) -> MessageBoxType {
let mut out = self.buttons as u32;
out |= self.icon as u32;
out |= self.default_button as u32;
out |= self.modality as u32;
const_modify_u32!(self.help_button, MB_HELP, out);
const_modify_u32!(self.default_desktop_only, MB_DEFAULT_DESKTOP_ONLY, out);
const_modify_u32!(self.right_justified_text, MB_RIGHT, out);
const_modify_u32!(self.right_to_left_text, MB_RTLREADING, out);
const_modify_u32!(self.set_foreground, MB_SETFOREGROUND, out);
const_modify_u32!(self.topmost, MB_TOPMOST, out);
const_modify_u32!(self.is_service_notification, MB_SERVICE_NOTIFICATION, out);
MessageBoxType(out)
}
}
#[allow(missing_docs)]
pub enum MessageBoxReturn {
Abort,
Cancel,
Continue,
Ignore,
No,
Ok,
Retry,
TryAgain,
Yes,
}
impl MessageBoxReturn {
pub fn from_i32(i: i32) -> Option<Self> {
match i {
IDABORT => Some(MessageBoxReturn::Abort),
IDCANCEL => Some(MessageBoxReturn::Cancel),
IDCONTINUE => Some(MessageBoxReturn::Continue),
IDIGNORE => Some(MessageBoxReturn::Ignore),
IDNO => Some(MessageBoxReturn::No),
IDOK => Some(MessageBoxReturn::Ok),
IDRETRY => Some(MessageBoxReturn::Retry),
IDTRYAGAIN => Some(MessageBoxReturn::TryAgain),
IDYES => Some(MessageBoxReturn::Yes),
_ => None,
}
}
}