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
//! Opening a message box.

use super::*;

/// `const_modify_u32!(flag, mask, word)`
macro_rules! const_modify_u32 {
  ($flag:expr, $mask:expr, $word:expr) => {
    $word ^= (u32::wrapping_sub(0u32, $flag as u32) ^ $word) & $mask;
  };
}

/// Opens a [message
/// box](https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-messageboxw).
///
/// ## Failure
///
/// Error results are also possible, but MSDN won't tell you which ones exactly
/// because it depends on your Windows version and stuff.
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())
    },
  }
}

/// A newtype for a particular type of message box to show.
///
/// Use a [MessageBoxTypeBuilder](MessageBoxTypeBuilder) to get one of these.
#[derive(Debug, Clone, Copy, Default)]
#[repr(transparent)]
pub struct MessageBoxType(u32);
impl MessageBoxType {
  /// Same as default, but this version is const.
  pub const fn new() -> Self {
    Self(0)
  }

  const fn is_service_notification(self) -> bool {
    (self.0 & MB_SERVICE_NOTIFICATION) > 0
  }
}

/// Which set of buttons the message box should show.
#[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 {
  /// The default is `Ok`
  fn default() -> Self {
    MessageBoxButtons::Ok
  }
}

/// What icon should be shown in the message box.
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum MessageBoxIcon {
  /// No icon
  None = 0,
  /// An exclamation point
  ///
  /// ![exclamation](https://docs.microsoft.com/en-us/windows/desktop/api/winuser/images/mb_iconexclamation.png)
  Exclamation = MB_ICONEXCLAMATION,
  /// An `i` in a circle.
  ///
  /// ![information](https://docs.microsoft.com/en-us/windows/desktop/api/winuser/images/mb_iconasterisk.png)
  Information = MB_ICONINFORMATION,
  /// An `x` in a red circle
  ///
  /// ![stop](https://docs.microsoft.com/en-us/windows/desktop/api/winuser/images/mb_iconhand.png)
  Stop = MB_ICONSTOP,
}
impl Default for MessageBoxIcon {
  /// The default is `None`
  fn default() -> Self {
    MessageBoxIcon::None
  }
}

/// Which button should be the default button?
///
/// [MessageBoxButtons](MessageBoxButtons) has variants for up to 3 buttons
/// normally. If you enable the `help` button then it will be at the end of the
/// button list as the 4th button.
#[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 {
  /// The default is `Button1`
  fn default() -> Self {
    MessageBoxDefaultButton::Button1
  }
}

/// Affects what windows this message blocks while it's open.
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum MessageBoxModality {
  /// User must respond to the message box before they can return to owning
  /// window (if any) or any of its children windows.
  ///
  /// Windows on _other_ threads are unaffected. Pop-up windows (from any
  /// thread) are unaffected.
  AppModal = MB_APPLMODAL,
  /// As `AppModal` but also sets `WS_EX_TOPMOST` style on the message box.
  SystemModal = MB_SYSTEMMODAL,
  /// As `AppModal` but all top-level windows in this thread are blocked even if
  /// this message box doesn't have an owner.
  TaskModal = MB_TASKMODAL,
}
impl Default for MessageBoxModality {
  /// The default is `AppModal`
  fn default() -> Self {
    MessageBoxModality::AppModal
  }
}

/// Provides a "can't forget anything" builder for [MessageBoxType](MessageBoxType) values.
#[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,
  /// Adds a "Help" button at the end of the buttons.
  pub help_button: bool,
  /// Forces the user to respond to this message box on the default desktop.
  pub default_desktop_only: bool,
  /// Text in the window is right-justified.
  pub right_justified_text: bool,
  /// Text in the window is right-to-left (Hebrew / Arabic)
  pub right_to_left_text: bool,
  /// The message box becomes the foreground window
  pub set_foreground: bool,
  /// The message box is created with `WS_EX_TOPMOST` style
  pub topmost: bool,
  /// This message box is a system service delivering a message.
  ///
  /// The message box appears on the current desktop even if no one is logged
  /// in. If this flag is set, there must be no associated window given. This
  /// can produce interactions even on a locked desktop, so use it as sparingly
  /// as possible.
  pub is_service_notification: bool,
}
impl MessageBoxTypeBuilder {
  /// Collapses all the settings into a single [MessageBoxType](MessageBoxType)
  /// value.
  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)
  }
}

/// The button that the user clicked to close the message box.
#[allow(missing_docs)]
pub enum MessageBoxReturn {
  Abort,
  Cancel,
  Continue,
  Ignore,
  No,
  Ok,
  Retry,
  TryAgain,
  Yes,
}
impl MessageBoxReturn {
  /// Converts a raw `i32` from `winapi` into this type
  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,
    }
  }
}