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
use crate::window::Frame;
#[cfg(windows)]
pub struct MessageBox {
pub title: String,
pub message: String,
pub style: windows_sys::Win32::UI::WindowsAndMessaging::MESSAGEBOX_STYLE,
pub button_style: windows_sys::Win32::UI::WindowsAndMessaging::MESSAGEBOX_STYLE,
}
#[cfg(unix)]
pub struct MessageBox {
pub title: String,
pub message: String,
pub style: gtk::MessageType,
pub button_style: gtk::ButtonsType,
}
#[cfg(windows)]
impl MessageBox {
pub fn new(
title: String,
message: String,
style: windows_sys::Win32::UI::WindowsAndMessaging::MESSAGEBOX_STYLE,
button_style: windows_sys::Win32::UI::WindowsAndMessaging::MESSAGEBOX_STYLE,
) -> Self {
Self {
title,
message,
style,
button_style,
}
}
pub fn show(&self,frame: Frame) -> windows_sys::Win32::UI::WindowsAndMessaging::MESSAGEBOX_RESULT {
unsafe {
use windows_sys::{
Win32::Foundation::*, Win32::System::Threading::*,
Win32::UI::WindowsAndMessaging::*,
};
enable_visual_style();
let event = CreateEventW(std::ptr::null(), 1, 0, std::ptr::null());
SetEvent(event);
WaitForSingleObject(event, 0);
CloseHandle(event);
let result = MessageBoxW(
frame.get_window(),
self.message.encode_utf16().collect::<Vec<u16>>().as_mut_ptr(),
self.title.encode_utf16().collect::<Vec<u16>>().as_mut_ptr(),
self.style | self.button_style,
);
result
}
}
}
#[cfg(unix)]
impl MessageBox {
pub fn new(
title: String,
message: String,
style: gtk::MessageType,
button_style: gtk::ButtonsType,
) -> Self {
Self {
title,
message,
style,
button_style,
}
}
pub fn show(&self,frame: Frame) -> gtk::ResponseType {
use gtk::prelude::*;
gtk::init().expect("Failed to initialize gtk.");
let window = gtk::Window::new(gtk::WindowType::Toplevel);
window.set_title(self.title.as_str());
let dialog = gtk::MessageDialog::new(
Some(&window),
gtk::DialogFlags::empty(),
self.style,
self.button_style,
self.message.as_str(),
);
let result = dialog.run();
unsafe {
dialog.destroy();
}
result
}
}
#[cfg(windows)]
fn enable_visual_style() {
use winapi::shared::minwindef::{ULONG, DWORD};
use winapi::shared::basetsd::ULONG_PTR;
use winapi::um::winbase::{ACTCTXW, CreateActCtxW, ActivateActCtx};
use winapi::um::sysinfoapi::GetSystemDirectoryW;
use std::{mem, ptr};
use windows_sys::Win32::Foundation::MAX_PATH;
const ACTCTX_FLAG_RESOURCE_NAME_VALID: DWORD = 0x008;
const ACTCTX_FLAG_SET_PROCESS_DEFAULT: DWORD = 0x010;
const ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID: DWORD = 0x004;
let mut sys_dir: Vec<u16> = Vec::with_capacity(MAX_PATH as usize);
unsafe {
sys_dir.set_len(MAX_PATH as usize);
GetSystemDirectoryW(sys_dir.as_mut_ptr(), MAX_PATH as u32);
}
let mut source = "shell32.dll".encode_utf16().collect::<Vec<u16>>();
let mut activation_cookie: ULONG_PTR = 0;
let mut act_ctx = ACTCTXW {
cbSize: mem::size_of::<ACTCTXW> as ULONG,
dwFlags: ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_SET_PROCESS_DEFAULT | ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID,
lpSource: source.as_mut_ptr(),
wProcessorArchitecture: 0,
wLangId: 0,
lpAssemblyDirectory: sys_dir.as_mut_ptr(),
lpResourceName: unsafe { mem::transmute(124usize) },
lpApplicationName: ptr::null_mut(),
hModule: ptr::null_mut()
};
unsafe {
let handle = CreateActCtxW(&mut act_ctx);
ActivateActCtx(handle, &mut activation_cookie);
}
}