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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
extern crate libc;
use libc::{c_char, c_uchar, c_int};
use std::ffi::{CStr, CString};
use std::ptr;

extern {
    fn tinyfd_messageBox (
        aTitle: *const c_char ,
        aMessage: *const c_char,
        aDialogType: *const c_char,
        aIconType: *const c_char,
        aDefaultButton: c_int) -> c_int;

    fn tinyfd_inputBox (
        aTitle: *const c_char ,
        aMessage: *const c_char,
        aDefaultInput: *const c_char) -> *const c_char;

    fn tinyfd_saveFileDialog (
        aTitle: *const c_char,
        aDefaultPathAndFile: *const c_char,
        aNumOfFilterPatterns: c_int,
        aFilterPatterns: *const *const c_char,
        aSingleFilterDescription: *const c_char) -> *const c_char;

    fn tinyfd_openFileDialog (
        aTitle: *const c_char,
        aDefaultPathAndFile: *const c_char,
        aNumOfFilterPatterns: c_int,
        aFilterPatterns: *const *const c_char,
        aSingleFilterDescription: *const c_char,
        aAllowMultipleSelects: c_int) -> *const c_char;

    fn tinyfd_selectFolderDialog (
        aTitle: *const c_char,
        aDefaultPath: *const c_char) -> *const c_char;

    #[cfg(not(windows))]
    fn tinyfd_arrayDialog (
        aTitle: *const c_char,
        aNumOfColumns: c_int,
        aColumns: *const *const c_char,
        aNumOfRows: c_int,
        aCells: *const *const c_char) -> *const c_char;

    fn tinyfd_colorChooser (
        aTitle: *const c_char,
        aDefaultHexRGB: *const c_char,
        aDefaultRGB: *const c_uchar,
        aoResultRGB: *mut c_uchar) -> *const c_char;
}

fn message_box(title: &str, message: &str, box_type: &str, icon: &str, button: i32) -> i32 {
    let message_box_title          = CString::new(title).unwrap();
    let message_box_message        = CString::new(message).unwrap();
    let message_box_type           = CString::new(box_type).unwrap();
    let message_box_icon           = CString::new(icon).unwrap();

    unsafe {
        tinyfd_messageBox(
            message_box_title.as_ptr(),
            message_box_message.as_ptr(),
            message_box_type.as_ptr(),
            message_box_icon.as_ptr(),
            button)
    }
}

pub enum MessageBoxIcon {
    Info,
    Warning,
    Error,
    Question,
}

impl MessageBoxIcon {
    fn to_str(&self) -> &'static str {
        match *self {
            MessageBoxIcon::Info => "info",
            MessageBoxIcon::Warning => "warning",
            MessageBoxIcon::Error => "error",
            MessageBoxIcon::Question => "question",
        }
    }
}

pub fn message_box_ok(title: &str, message: &str, icon: MessageBoxIcon) {
    let _ = message_box(title, message, "ok", icon.to_str(), 0);
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum OkCancel {
    Cancel = 0,
    Ok,
}

pub fn message_box_ok_cancel(title: &str, message: &str, icon: MessageBoxIcon, default: OkCancel) -> OkCancel {
    match message_box(title, message, "okcancel", icon.to_str(), default as i32) {
        0 => OkCancel::Cancel,
        1 => OkCancel::Ok,
        _ => unreachable!(),
    }
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum YesNo {
    No = 0,
    Yes,
}

pub fn message_box_yes_no(title: &str, message: &str, icon: MessageBoxIcon, default: YesNo) -> YesNo {
    match message_box(title, message, "yesno", icon.to_str(), default as i32) {
        0 => YesNo::No,
        1 => YesNo::Yes,
        _ => unreachable!(),
    }
}

fn input_box_impl(title: &str, message: &str, default: Option<&str>) -> Option<String> {
    let input_box_title   = CString::new(title).unwrap();
    let input_box_message = CString::new(message).unwrap();
    let input_box_default = default.map(|default| CString::new(default).unwrap());

    let c_input = unsafe {
        tinyfd_inputBox(input_box_title.as_ptr(),
                        input_box_message.as_ptr(),
                        input_box_default.as_ref().map(|d| d.as_ptr()).unwrap_or(ptr::null()))
    };

    if !c_input.is_null() {
        unsafe {
            Some(CStr::from_ptr(c_input).to_string_lossy().into_owned())
        }
    } else {
        None
    }
}

pub fn input_box(title: &str, message: &str, default: &str) -> Option<String> {
    input_box_impl(title, message, Some(default))
}

pub fn password_box(title: &str, message: &str) -> Option<String> {
    input_box_impl(title, message, None)
}

fn save_file_dialog_impl(title: &str, path: &str, filter: Option<(&[&str], &str)>) -> Option<String> {
    let save_dialog_title                = CString::new(title).unwrap();
    let save_dialog_path                 = CString::new(path).unwrap();
    let save_dialog_des                  = CString::new(filter.map_or("", |f| f.1)).unwrap();

    let filter_patterns =
        filter.map_or(vec![], |f| f.0.iter().map(|s| CString::new(*s).unwrap()).collect());
    let ptr_filter_patterns = filter_patterns.iter().map(|c| c.as_ptr()).collect::<Vec<*const c_char>>();

    let c_file_name = unsafe {
        tinyfd_saveFileDialog(
            save_dialog_title.as_ptr(),
            save_dialog_path.as_ptr(),
            ptr_filter_patterns.len() as c_int,
            ptr_filter_patterns.as_ptr(),
            save_dialog_des.as_ptr())
    };

    if !c_file_name.is_null() {
        unsafe {
            Some(CStr::from_ptr(c_file_name).to_string_lossy().into_owned())
        }
    } else {
        None
    }
}

pub fn save_file_dialog_with_filter(title: &str,
                                    path: &str,
                                    filter_patterns: &[&str],
                                    description: &str) -> Option<String> {
    save_file_dialog_impl(title, path, Some((filter_patterns, description)))
}

pub fn save_file_dialog(title: &str, path: &str) -> Option<String> {
    save_file_dialog_impl(title, path, None)
}

fn open_file_dialog_impl(title: &str,
                         path: &str,
                         filter: Option<(&[&str], &str)>,
                         multi: bool) -> Option<Vec<String>> {
    let open_dialog_title                = CString::new(title).unwrap();
    let open_dialog_path                 = CString::new(path).unwrap();
    let open_dialog_des                  = CString::new(filter.map_or("", |f| f.1)).unwrap();

    let filter_patterns =
        filter.map_or(vec![], |f| f.0.iter().map(|s| CString::new(*s).unwrap()).collect());
    let ptr_filter_patterns =
        filter_patterns.iter().map(|c| c.as_ptr()).collect::<Vec<*const c_char>>();

    let c_file_name = unsafe {
        tinyfd_openFileDialog(
            open_dialog_title.as_ptr(),
            open_dialog_path.as_ptr(),
            ptr_filter_patterns.len() as c_int,
            ptr_filter_patterns.as_ptr(),
            open_dialog_des.as_ptr(),
            multi as c_int)
    };

    if !c_file_name.is_null() {
        let result = unsafe {
            CStr::from_ptr(c_file_name).to_string_lossy().into_owned()
        };
        Some(if multi {
            result.split('|').map(|s| s.to_owned()).collect()
        } else {
            vec![result]
        })
    } else {
        None
    }
}

pub fn open_file_dialog(title: &str,
                        path: &str,
                        filter: Option<(&[&str], &str)>) -> Option<String> {
    open_file_dialog_impl(title, path, filter, false).and_then(|v| v.into_iter().next())
}

pub fn open_file_dialog_multi(title: &str,
                              path: &str,
                              filter: Option<(&[&str], &str)>) -> Option<Vec<String>> {
    open_file_dialog_impl(title, path, filter, true)
}

pub fn select_folder_dialog(title: &str, path: &str) -> Option<String> {
    let select_folder_title = CString::new(title).unwrap();
    let select_folder_path  = CString::new(path).unwrap();

    let folder = unsafe {
        tinyfd_selectFolderDialog(select_folder_title.as_ptr(), select_folder_path.as_ptr())
    };

    if !folder.is_null() {
        unsafe {
            Some(CStr::from_ptr(folder).to_string_lossy().into_owned())
        }
    } else {
        None
    }
}

#[cfg(not(windows))]
pub fn list_dialog(title: &str,
                   columns: &[&str],
                   cells: Option<&[&str]>) -> Option<String> {
    let list_dialog_title = CString::new(title).unwrap();

    if columns.is_empty() {
        return None;
    }

    let list_dialog_columns =
        columns.iter().map(|s| CString::new(*s).unwrap()).collect::<Vec<CString>>();
    let ptr_list_dialog_columns =
        list_dialog_columns.iter().map(|c| c.as_ptr()).collect::<Vec<*const c_char>>();

    let list_dialog_cells =
        cells.map_or(vec![], |f| f.iter().map(|s| CString::new(*s).unwrap()).collect());
    let ptr_list_dialog_cells =
        list_dialog_cells.iter().map(|c| c.as_ptr()).collect::<Vec<*const c_char>>();

    let dialog = unsafe {
        tinyfd_arrayDialog(list_dialog_title.as_ptr(),
                           list_dialog_columns.len() as c_int,
                           ptr_list_dialog_columns.as_ptr(),
                           (list_dialog_cells.len() / list_dialog_columns.len()) as c_int,
                           ptr_list_dialog_cells.as_ptr())
    };
    if !dialog.is_null() {
        unsafe {
            Some(CStr::from_ptr(dialog).to_string_lossy().into_owned())
        }
    } else {
        None
    }
}

#[cfg(windows)]
pub fn list_dialog(_title: &str,
                   _columns: &[&str],
                   _cells: Option<&[&str]>) -> Option<String> {
    None
}

pub enum DefaultColorValue<'a> {
    Hex(&'a str),
    RGB(&'a [u8; 3]),
}

pub fn color_chooser_dialog(title: &str, default: DefaultColorValue)
                            -> Option<(String, [u8; 3])> {
    let color_title                      = CString::new(title).unwrap();
    let rubbish = [0, 0, 0];
    let (color_default_hex, color_default_rgb) = match default {
        DefaultColorValue::Hex(hex) => (Some(CString::new(hex).unwrap()), &rubbish),
        DefaultColorValue::RGB(rgb) => (None, rgb),
    };
    let mut color_result_rgb = [0, 0, 0];

    let result = unsafe {
        tinyfd_colorChooser(color_title.as_ptr(),
                            color_default_hex.map_or(ptr::null(), |h| h.as_ptr()),
                            color_default_rgb.as_ptr(),
                            color_result_rgb.as_mut_ptr())
    };

    if !result.is_null() {
        unsafe {
            Some((CStr::from_ptr(result).to_string_lossy().into_owned(), color_result_rgb))
        }
    } else {
        None
    }
}