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
use super::base_helper::to_utf16; use crate::controls::ControlHandle; use winapi::shared::windef::HWND; use std::ptr; /** Enum of message box buttons (to use with `MessageParams` ) */ #[derive(Clone, PartialEq, Debug)] pub enum MessageButtons { AbortTryIgnore, CancelTryContinue, Ok, OkCancel, RetryCancel, YesNo, YesNoCancel, } /** Enum of message box icons (to use with `MessageParams` ) */ #[derive(Clone, PartialEq, Debug)] pub enum MessageIcons { Warning, Info, Question, Error, None } /** Return value of `message`. Define the button that the user clicked. If the user cancelled the message box by clicking on X button of the window, `MessageChoice::Cancel` is returned. */ #[derive(Clone, PartialEq, Debug)] pub enum MessageChoice { Abort, Cancel, Continue, Ignore, No, Ok, Retry, TryAgain, Yes } /** A structure that defines how a messagebox should look and behave. Members: * `title`: The title of the message box * `content`: The message of the message box * `buttons`: The button of the message box * `icons`: The message box icon */ #[derive(Clone, PartialEq, Debug)] pub struct MessageParams<'a> { pub title: &'a str, pub content: &'a str, pub buttons: MessageButtons, pub icons: MessageIcons } /// Inner function used by the message box function fn inner_message(parent: HWND, params: &MessageParams) -> MessageChoice { use winapi::um::winuser::{MB_ABORTRETRYIGNORE, MB_CANCELTRYCONTINUE, MB_OK, MB_OKCANCEL, MB_RETRYCANCEL, MB_YESNO, MB_YESNOCANCEL, MB_ICONSTOP, MB_ICONINFORMATION, MB_ICONQUESTION, MB_ICONEXCLAMATION}; use winapi::um::winuser::{IDABORT, IDCANCEL, IDCONTINUE, IDIGNORE, IDNO, IDOK, IDRETRY, IDTRYAGAIN, IDYES}; use winapi::um::winuser::MessageBoxW; let text = to_utf16(params.content); let title = to_utf16(params.title); let buttons = match params.buttons { MessageButtons::AbortTryIgnore => MB_ABORTRETRYIGNORE, MessageButtons::CancelTryContinue => MB_CANCELTRYCONTINUE, MessageButtons::Ok => MB_OK, MessageButtons::OkCancel => MB_OKCANCEL, MessageButtons::RetryCancel => MB_RETRYCANCEL, MessageButtons::YesNo => MB_YESNO, MessageButtons::YesNoCancel => MB_YESNOCANCEL }; let icons = match params.icons { MessageIcons::Error => MB_ICONSTOP, MessageIcons::Info => MB_ICONINFORMATION, MessageIcons::None => 0, MessageIcons::Question => MB_ICONQUESTION, MessageIcons::Warning => MB_ICONEXCLAMATION }; let answer = unsafe{ MessageBoxW(parent, text.as_ptr(), title.as_ptr(), buttons | icons) }; match answer { IDABORT => MessageChoice::Abort, IDCANCEL => MessageChoice::Cancel, IDCONTINUE => MessageChoice::Continue, IDIGNORE => MessageChoice::Ignore, IDNO => MessageChoice::No, IDOK => MessageChoice::Ok, IDRETRY => MessageChoice::Retry, IDTRYAGAIN => MessageChoice::TryAgain, IDYES => MessageChoice::Yes, _ => MessageChoice::Cancel } } /** Create an application wide message box. It is recommended to use `modal_message` because it locks the window that creates the message box. This method may be deprecated in the future Parameters: * params: A `MessageParams` structure that defines how the message box should look ```rust use native_windows_gui as nwg; fn test_message() { let p = nwg::MessageParams { title: "Hey", content: "Cats are cute", buttons: nwg::MessageButtons::Ok, icons: nwg::MessageIcons::Warning }; assert!(nwg::message(&p) == nwg::MessageChoice::Ok) } ``` */ pub fn message<'a>(params: &MessageParams) -> MessageChoice { inner_message(ptr::null_mut(), params) } /** Create a message box for a selected window. The window will be locked until the user close the message box. This functions panics if a non window control is used as parent (ex: a menu) Parameters: * parent: The reference to a window-like control * params: A `MessageParams` structure that defines how the message box should look ```rust use native_windows_gui as nwg; fn test_message(parent: &nwg::Window) { let p = nwg::MessageParams { title: "Hey", content: "Cats are cute", buttons: nwg::MessageButtons::Ok, icons: nwg::MessageIcons::Warning }; assert!(nwg::modal_message(parent, &p) == nwg::MessageChoice::Ok) } ``` */ pub fn modal_message<'a, P: Into<ControlHandle>>(parent: P, params: &MessageParams) -> MessageChoice { let control_handle = parent.into(); let hwnd = control_handle.hwnd().expect("expected window like control"); inner_message(hwnd, params) } /** Display a message box and then panic. The message box has for style `MessageButtons::Ok` and `MessageIcons::Error` . It is recommended to use `modal_fatal_message` because it locks the window that creates the message box. This method may be deprecated in the future Parameters: * title: The message box title * content: The message box message */ pub fn fatal_message<'a>(title: &'a str, content: &'a str) -> ! { error_message(title, content); panic!("{} - {}", title, content); } /** Display a message box and then panic. The message box has for style `MessageButtons::Ok` and `MessageIcons::Error` . This functions panics if a non window control is used as parent (ex: a menu) Parameters: * parent: Parent window to lock for the duration of the message box * title: The message box title * content: The message box message */ pub fn modal_fatal_message<'a, P: Into<ControlHandle>>(parent: P, title: &'a str, content: &'a str) -> ! { modal_error_message(parent, title, content); panic!("{} - {}", title, content); } /** Display a simple error message box. The message box has for style `MessageButtons::Ok` and `MessageIcons::Error`. It is recommended to use `modal_error_message` because it locks the window that creates the message box. This method may be deprecated in the future Parameters: * title: The message box title * content: The message box message */ pub fn error_message<'a>(title: &'a str, content: &'a str) -> MessageChoice { let params = MessageParams { title, content, buttons: MessageButtons::Ok, icons: MessageIcons::Error }; message(¶ms) } /** Display a simple error message box. The message box has for style `MessageButtons::Ok` and `MessageIcons::Error`. This functions panics if a non window control is used as parent (ex: a menu) Parameters: * parent: Parent window to lock for the duration of the message box * title: The message box title * content: The message box message */ pub fn modal_error_message<'a, P: Into<ControlHandle>>(parent: P, title: &'a str, content: &'a str) -> MessageChoice { let params = MessageParams { title, content, buttons: MessageButtons::Ok, icons: MessageIcons::Error }; modal_message(parent, ¶ms) } /** Display a simple message box. The message box has for style `MessageButtons::Ok` and `MessageIcons::Info`. It is recommended to use `modal_info_message` because it locks the window that creates the message box. This method may be deprecated in the future Parameters: * title: The message box title * content: The message box message */ pub fn simple_message<'a>(title: &'a str, content: &'a str) -> MessageChoice { let params = MessageParams { title, content, buttons: MessageButtons::Ok, icons: MessageIcons::Info }; message(¶ms) } /** Display a simple message box. The message box has for style `MessageButtons::Ok` and `MessageIcons::Info`. This functions panics if a non window control is used as parent (ex: a menu) Parameters: * parent: Parent window to lock for the duration of the message box * title: The message box title * content: The message box message */ pub fn modal_info_message<'a, P: Into<ControlHandle>>(parent: P, title: &'a str, content: &'a str) -> MessageChoice { let params = MessageParams { title, content, buttons: MessageButtons::Ok, icons: MessageIcons::Info }; modal_message(parent, ¶ms) }